diff --git a/cli/command_cache_clear.go b/cli/command_cache_clear.go index d775cc30d..704089aac 100644 --- a/cli/command_cache_clear.go +++ b/cli/command_cache_clear.go @@ -13,7 +13,7 @@ var cacheClearCommand = cacheCommands.Command("clear", "Clears the cache") func runCacheClearCommand(ctx context.Context, rep *repo.DirectRepository) error { - if d := rep.Content.CachingOptions.CacheDirectory; d != "" { + if d := rep.Cache.CacheDirectory; d != "" { log(ctx).Infof("Clearing cache directory: %v.", d) // close repository before removing cache diff --git a/cli/command_cache_info.go b/cli/command_cache_info.go index 7198e533a..63c5ee9ea 100644 --- a/cli/command_cache_info.go +++ b/cli/command_cache_info.go @@ -19,18 +19,18 @@ func runCacheInfoCommand(ctx context.Context, rep *repo.DirectRepository) error { if *cacheInfoPathOnly { - fmt.Println(rep.Content.CachingOptions.CacheDirectory) + fmt.Println(rep.Cache.CacheDirectory) return nil } - entries, err := ioutil.ReadDir(rep.Content.CachingOptions.CacheDirectory) + entries, err := ioutil.ReadDir(rep.Cache.CacheDirectory) if err != nil { return errors.Wrap(err, "unable to scan cache directory") } path2Limit := map[string]int64{ - "contents": rep.Content.CachingOptions.MaxCacheSizeBytes, - "metadata": rep.Content.CachingOptions.MaxMetadataCacheSizeBytes, + "contents": rep.Cache.MaxCacheSizeBytes, + "metadata": rep.Cache.MaxMetadataCacheSizeBytes, } for _, ent := range entries { @@ -38,7 +38,7 @@ func runCacheInfoCommand(ctx context.Context, rep *repo.DirectRepository) error continue } - subdir := filepath.Join(rep.Content.CachingOptions.CacheDirectory, ent.Name()) + subdir := filepath.Join(rep.Cache.CacheDirectory, ent.Name()) fileCount, totalFileSize, err := scanCacheDir(subdir) if err != nil { diff --git a/cli/command_cache_set.go b/cli/command_cache_set.go index 290261bb2..c0132fae4 100644 --- a/cli/command_cache_set.go +++ b/cli/command_cache_set.go @@ -19,7 +19,7 @@ ) func runCacheSetCommand(ctx context.Context, rep *repo.DirectRepository) error { - opts := rep.Content.CachingOptions.CloneOrDefault() + opts := rep.Cache.CloneOrDefault() changed := 0 diff --git a/internal/server/api_repo.go b/internal/server/api_repo.go index 406fbfd7b..96bf797ca 100644 --- a/internal/server/api_repo.go +++ b/internal/server/api_repo.go @@ -49,7 +49,7 @@ func (s *Server) handleRepoStatus(ctx context.Context, r *http.Request, body []b return &serverapi.StatusResponse{ Connected: true, ConfigFile: dr.ConfigFile, - CacheDir: dr.Content.CachingOptions.CacheDirectory, + CacheDir: dr.Cache.CacheDirectory, Hash: dr.Content.Format.Hash, Encryption: dr.Content.Format.Encryption, MaxPackSize: dr.Content.Format.MaxPackSize, diff --git a/repo/content/content_formatter_test.go b/repo/content/content_formatter_test.go index a3f2c9018..2de43ba43 100644 --- a/repo/content/content_formatter_test.go +++ b/repo/content/content_formatter_test.go @@ -97,7 +97,7 @@ func verifyEndToEndFormatter(ctx context.Context, t *testing.T, hashAlgo, encryp keyTime := map[blob.ID]time.Time{} st := blobtesting.NewMapStorage(data, keyTime, nil) - bm, err := newManagerWithOptions(testlogging.Context(t), st, &FormattingOptions{ + bm, err := NewManager(testlogging.Context(t), st, &FormattingOptions{ Hash: hashAlgo, Encryption: encryptionAlgo, HMACSecret: hmacSecret, diff --git a/repo/content/content_manager.go b/repo/content/content_manager.go index 4cce8c5d6..a68b07fc2 100644 --- a/repo/content/content_manager.go +++ b/repo/content/content_manager.go @@ -74,8 +74,6 @@ type IndexBlobInfo struct { // Manager builds content-addressable storage with encryption, deduplication and packaging on top of BLOB store. type Manager struct { - CachingOptions CachingOptions - mu *sync.RWMutex cond *sync.Cond flushing bool @@ -706,27 +704,18 @@ func NewManager(ctx context.Context, st blob.Storage, f *FormattingOptions, cach options.TimeNow = clock.Now } - return newManagerWithOptions(ctx, st, f, caching, options) -} - -func newManagerWithOptions(ctx context.Context, st blob.Storage, f *FormattingOptions, caching *CachingOptions, options *ManagerOptions) (*Manager, error) { - if f.Version < minSupportedReadVersion || f.Version > currentWriteVersion { - return nil, errors.Errorf("can't handle repositories created using version %v (min supported %v, max supported %v)", f.Version, minSupportedReadVersion, maxSupportedReadVersion) - } - - if f.Version < minSupportedWriteVersion || f.Version > currentWriteVersion { - return nil, errors.Errorf("can't handle repositories created using version %v (min supported %v, max supported %v)", f.Version, minSupportedWriteVersion, maxSupportedWriteVersion) - } - - caching = caching.CloneOrDefault() - readManager, err := newReadManager(ctx, st, f, caching, options) if err != nil { return nil, errors.Wrap(err, "error initializing read manager") } + return newManagerWithReadManager(ctx, f, readManager, options), nil +} + +func newManagerWithReadManager(ctx context.Context, f *FormattingOptions, readManager *CommittedReadManager, options *ManagerOptions) *Manager { mu := &sync.RWMutex{} - m := &Manager{ + + return &Manager{ lockFreeManager: lockFreeManager{ Format: *f, maxPackSize: f.MaxPackSize, @@ -739,7 +728,6 @@ func newManagerWithOptions(ctx context.Context, st blob.Storage, f *FormattingOp encryptionBufferPool: buf.NewPool(ctx, defaultEncryptionBufferPoolSegmentSize+readManager.encryptor.MaxOverhead(), "content-manager-encryption"), }, - CachingOptions: *caching, CommittedReadManager: readManager, mu: mu, @@ -749,10 +737,4 @@ func newManagerWithOptions(ctx context.Context, st blob.Storage, f *FormattingOp pendingPacks: map[blob.ID]*pendingPackInfo{}, packIndexBuilder: make(packIndexBuilder), } - - if _, _, err := m.loadPackIndexesUnlocked(ctx); err != nil { - return nil, errors.Wrap(err, "error loading indexes") - } - - return m, nil } diff --git a/repo/content/content_manager_test.go b/repo/content/content_manager_test.go index 9ab166dfd..39e11f341 100644 --- a/repo/content/content_manager_test.go +++ b/repo/content/content_manager_test.go @@ -315,7 +315,7 @@ func TestContentManagerFailedToWritePack(t *testing.T) { } st = faulty - bm, err := newManagerWithOptions(testlogging.Context(t), st, &FormattingOptions{ + bm, err := NewManager(testlogging.Context(t), st, &FormattingOptions{ Version: 1, Hash: "HMAC-SHA256-128", Encryption: "AES256-GCM-HMAC-SHA256", diff --git a/repo/content/content_read_manager.go b/repo/content/content_read_manager.go index 5ba8c6a2f..d5c9af607 100644 --- a/repo/content/content_read_manager.go +++ b/repo/content/content_read_manager.go @@ -306,6 +306,14 @@ func (rm *CommittedReadManager) setupReadManagerCaches(ctx context.Context, cach } func newReadManager(ctx context.Context, st blob.Storage, f *FormattingOptions, caching *CachingOptions, opts *ManagerOptions) (*CommittedReadManager, error) { + if f.Version < minSupportedReadVersion || f.Version > currentWriteVersion { + return nil, errors.Errorf("can't handle repositories created using version %v (min supported %v, max supported %v)", f.Version, minSupportedReadVersion, maxSupportedReadVersion) + } + + if f.Version < minSupportedWriteVersion || f.Version > currentWriteVersion { + return nil, errors.Errorf("can't handle repositories created using version %v (min supported %v, max supported %v)", f.Version, minSupportedWriteVersion, maxSupportedWriteVersion) + } + hasher, encryptor, err := CreateHashAndEncryptor(f) if err != nil { return nil, err @@ -325,5 +333,9 @@ func newReadManager(ctx context.Context, st blob.Storage, f *FormattingOptions, return nil, errors.Wrap(err, "error setting up read manager caches") } + if _, _, err := rm.loadPackIndexesUnlocked(ctx); err != nil { + return nil, errors.Wrap(err, "error loading indexes") + } + return rm, nil } diff --git a/repo/open.go b/repo/open.go index ccb6d0495..f3eb85e2e 100644 --- a/repo/open.go +++ b/repo/open.go @@ -183,6 +183,7 @@ func OpenWithConfig(ctx context.Context, st blob.Storage, lc *LocalConfig, passw } dr := &DirectRepository{ + Cache: *caching, Content: cm, Objects: om, Blobs: st, diff --git a/repo/repository.go b/repo/repository.go index 0e65ec1a1..ac1b93f55 100644 --- a/repo/repository.go +++ b/repo/repository.go @@ -53,6 +53,7 @@ type DirectRepository struct { Content *content.Manager Objects *object.Manager Manifests *manifest.Manager + Cache content.CachingOptions UniqueID []byte ConfigFile string