2017-06-10 11:10:08 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2017-09-25 13:21:16 +00:00
|
|
|
"sync"
|
2017-06-10 11:10:08 +00:00
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
2017-06-10 11:10:08 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Backend wraps a restic.Backend and adds a cache.
|
|
|
|
type Backend struct {
|
2023-10-01 09:40:12 +00:00
|
|
|
backend.Backend
|
2017-09-24 20:24:11 +00:00
|
|
|
*Cache
|
2017-09-25 13:21:16 +00:00
|
|
|
|
|
|
|
// inProgress contains the handle for all files that are currently
|
|
|
|
// downloaded. The channel in the value is closed as soon as the download
|
|
|
|
// is finished.
|
|
|
|
inProgressMutex sync.Mutex
|
2023-10-01 09:40:12 +00:00
|
|
|
inProgress map[backend.Handle]chan struct{}
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
// ensure Backend implements backend.Backend
|
|
|
|
var _ backend.Backend = &Backend{}
|
2017-06-10 11:10:08 +00:00
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
func newBackend(be backend.Backend, c *Cache) *Backend {
|
2017-09-25 13:21:16 +00:00
|
|
|
return &Backend{
|
|
|
|
Backend: be,
|
|
|
|
Cache: c,
|
2023-10-01 09:40:12 +00:00
|
|
|
inProgress: make(map[backend.Handle]chan struct{}),
|
2017-09-25 13:21:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-10 11:10:08 +00:00
|
|
|
// Remove deletes a file from the backend and the cache if it has been cached.
|
2023-10-01 09:40:12 +00:00
|
|
|
func (b *Backend) Remove(ctx context.Context, h backend.Handle) error {
|
2017-06-10 11:10:08 +00:00
|
|
|
debug.Log("cache Remove(%v)", h)
|
|
|
|
err := b.Backend.Remove(ctx, h)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-02 17:27:52 +00:00
|
|
|
return b.Cache.remove(h)
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
func autoCacheTypes(h backend.Handle) bool {
|
2020-07-28 08:13:11 +00:00
|
|
|
switch h.Type {
|
2023-10-01 09:40:12 +00:00
|
|
|
case backend.IndexFile, backend.SnapshotFile:
|
2020-07-28 08:13:11 +00:00
|
|
|
return true
|
2023-10-01 09:40:12 +00:00
|
|
|
case backend.PackFile:
|
2023-10-01 08:52:57 +00:00
|
|
|
return h.IsMetadata
|
2020-07-28 08:13:11 +00:00
|
|
|
}
|
|
|
|
return false
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 19:58:24 +00:00
|
|
|
// Save stores a new file in the backend and the cache.
|
2023-10-01 09:40:12 +00:00
|
|
|
func (b *Backend) Save(ctx context.Context, h backend.Handle, rd backend.RewindReader) error {
|
2020-07-28 08:13:11 +00:00
|
|
|
if !autoCacheTypes(h) {
|
2017-06-10 11:10:08 +00:00
|
|
|
return b.Backend.Save(ctx, h, rd)
|
|
|
|
}
|
|
|
|
|
2017-09-24 20:24:01 +00:00
|
|
|
debug.Log("Save(%v): auto-store in the cache", h)
|
2017-10-17 19:58:24 +00:00
|
|
|
|
2018-03-03 13:20:54 +00:00
|
|
|
// make sure the reader is at the start
|
|
|
|
err := rd.Rewind()
|
2017-06-10 11:10:08 +00:00
|
|
|
if err != nil {
|
2018-03-03 13:20:54 +00:00
|
|
|
return err
|
2017-10-17 19:58:24 +00:00
|
|
|
}
|
|
|
|
|
2018-03-03 13:20:54 +00:00
|
|
|
// first, save in the backend
|
2017-10-17 19:58:24 +00:00
|
|
|
err = b.Backend.Save(ctx, h, rd)
|
2017-06-10 11:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-03 13:20:54 +00:00
|
|
|
// next, save in the cache
|
|
|
|
err = rd.Rewind()
|
2017-06-10 11:10:08 +00:00
|
|
|
if err != nil {
|
2018-03-03 13:20:54 +00:00
|
|
|
return err
|
2017-10-17 19:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = b.Cache.Save(h, rd)
|
|
|
|
if err != nil {
|
|
|
|
debug.Log("unable to save %v to cache: %v", h, err)
|
2020-03-02 17:27:52 +00:00
|
|
|
_ = b.Cache.remove(h)
|
2023-02-10 21:39:40 +00:00
|
|
|
return err
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
2017-10-17 19:58:24 +00:00
|
|
|
|
2017-06-10 11:10:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
func (b *Backend) cacheFile(ctx context.Context, h backend.Handle) error {
|
2017-09-25 13:21:16 +00:00
|
|
|
finish := make(chan struct{})
|
|
|
|
|
|
|
|
b.inProgressMutex.Lock()
|
|
|
|
other, alreadyDownloading := b.inProgress[h]
|
|
|
|
if !alreadyDownloading {
|
|
|
|
b.inProgress[h] = finish
|
|
|
|
}
|
|
|
|
b.inProgressMutex.Unlock()
|
|
|
|
|
|
|
|
if alreadyDownloading {
|
|
|
|
debug.Log("readahead %v is already performed by somebody else, delegating...", h)
|
|
|
|
<-other
|
|
|
|
debug.Log("download %v finished", h)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-10 21:39:40 +00:00
|
|
|
defer func() {
|
|
|
|
// signal other waiting goroutines that the file may now be cached
|
|
|
|
close(finish)
|
|
|
|
|
|
|
|
// remove the finish channel from the map
|
|
|
|
b.inProgressMutex.Lock()
|
|
|
|
delete(b.inProgress, h)
|
|
|
|
b.inProgressMutex.Unlock()
|
|
|
|
}()
|
|
|
|
|
2018-10-04 12:30:07 +00:00
|
|
|
// test again, maybe the file was cached in the meantime
|
2018-10-04 15:09:43 +00:00
|
|
|
if !b.Cache.Has(h) {
|
|
|
|
// nope, it's still not in the cache, pull it from the repo and save it
|
|
|
|
err := b.Backend.Load(ctx, h, 0, 0, func(rd io.Reader) error {
|
|
|
|
return b.Cache.Save(h, rd)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
// try to remove from the cache, ignore errors
|
2020-03-02 17:27:52 +00:00
|
|
|
_ = b.Cache.remove(h)
|
2018-10-04 15:09:43 +00:00
|
|
|
}
|
2023-02-10 21:39:40 +00:00
|
|
|
return err
|
2017-09-24 20:50:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 12:30:07 +00:00
|
|
|
return nil
|
2017-09-24 20:50:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 20:08:05 +00:00
|
|
|
// loadFromCache will try to load the file from the cache.
|
2023-10-01 09:40:12 +00:00
|
|
|
func (b *Backend) loadFromCache(h backend.Handle, length int, offset int64, consumer func(rd io.Reader) error) (bool, error) {
|
2020-03-02 17:27:52 +00:00
|
|
|
rd, err := b.Cache.load(h, length, offset)
|
2018-01-17 04:59:16 +00:00
|
|
|
if err != nil {
|
2021-09-20 20:08:05 +00:00
|
|
|
return false, err
|
2017-09-25 13:21:16 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 04:59:16 +00:00
|
|
|
err = consumer(rd)
|
|
|
|
if err != nil {
|
2018-06-09 15:22:33 +00:00
|
|
|
_ = rd.Close() // ignore secondary errors
|
2021-09-20 20:08:05 +00:00
|
|
|
return true, err
|
2018-01-17 04:59:16 +00:00
|
|
|
}
|
2021-09-20 20:08:05 +00:00
|
|
|
return true, rd.Close()
|
2017-09-25 13:21:16 +00:00
|
|
|
}
|
|
|
|
|
2017-06-10 11:10:08 +00:00
|
|
|
// Load loads a file from the cache or the backend.
|
2023-10-01 09:40:12 +00:00
|
|
|
func (b *Backend) Load(ctx context.Context, h backend.Handle, length int, offset int64, consumer func(rd io.Reader) error) error {
|
2017-09-25 13:21:16 +00:00
|
|
|
b.inProgressMutex.Lock()
|
|
|
|
waitForFinish, inProgress := b.inProgress[h]
|
|
|
|
b.inProgressMutex.Unlock()
|
|
|
|
|
|
|
|
if inProgress {
|
|
|
|
debug.Log("downloading %v is already in progress, waiting for finish", h)
|
|
|
|
<-waitForFinish
|
|
|
|
debug.Log("downloading %v finished", h)
|
|
|
|
}
|
|
|
|
|
2022-08-19 18:59:06 +00:00
|
|
|
// try loading from cache without checking that the handle is actually cached
|
2023-05-18 17:21:28 +00:00
|
|
|
inCache, err := b.loadFromCache(h, length, offset, consumer)
|
2021-09-20 20:08:05 +00:00
|
|
|
if inCache {
|
2021-09-20 20:12:00 +00:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// drop from cache and retry once
|
|
|
|
_ = b.Cache.remove(h)
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
2022-08-19 18:59:06 +00:00
|
|
|
debug.Log("error loading %v from cache: %v", h, err)
|
2017-06-10 11:10:08 +00:00
|
|
|
|
2017-09-24 20:50:35 +00:00
|
|
|
// if we don't automatically cache this file type, fall back to the backend
|
2020-07-28 08:13:11 +00:00
|
|
|
if !autoCacheTypes(h) {
|
2017-09-24 20:50:35 +00:00
|
|
|
debug.Log("Load(%v, %v, %v): delegating to backend", h, length, offset)
|
2018-01-17 04:59:16 +00:00
|
|
|
return b.Backend.Load(ctx, h, length, offset, consumer)
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
debug.Log("auto-store %v in the cache", h)
|
2022-08-19 18:59:06 +00:00
|
|
|
err = b.cacheFile(ctx, h)
|
2023-02-10 21:39:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-18 17:21:28 +00:00
|
|
|
inCache, err = b.loadFromCache(h, length, offset, consumer)
|
2023-02-10 21:39:40 +00:00
|
|
|
if inCache {
|
|
|
|
return err
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
2017-09-24 20:50:35 +00:00
|
|
|
debug.Log("error caching %v: %v, falling back to backend", h, err)
|
2018-01-17 04:59:16 +00:00
|
|
|
return b.Backend.Load(ctx, h, length, offset, consumer)
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stat tests whether the backend has a file. If it does not exist but still
|
|
|
|
// exists in the cache, it is removed from the cache.
|
2023-10-01 09:40:12 +00:00
|
|
|
func (b *Backend) Stat(ctx context.Context, h backend.Handle) (backend.FileInfo, error) {
|
2017-06-10 11:10:08 +00:00
|
|
|
debug.Log("cache Stat(%v)", h)
|
|
|
|
|
|
|
|
fi, err := b.Backend.Stat(ctx, h)
|
|
|
|
if err != nil {
|
|
|
|
if b.Backend.IsNotExist(err) {
|
|
|
|
// try to remove from the cache, ignore errors
|
2020-03-02 17:27:52 +00:00
|
|
|
_ = b.Cache.remove(h)
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fi, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fi, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsNotExist returns true if the error is caused by a non-existing file.
|
|
|
|
func (b *Backend) IsNotExist(err error) bool {
|
|
|
|
return b.Backend.IsNotExist(err)
|
|
|
|
}
|
2023-04-08 10:53:43 +00:00
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
func (b *Backend) Unwrap() backend.Backend {
|
2023-04-08 10:53:43 +00:00
|
|
|
return b.Backend
|
|
|
|
}
|