diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-07-21 15:55:16 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-07-22 15:17:25 +0300 |
commit | 5d4ede3e06833fbbeecbd35714f02c4ab9a47681 (patch) | |
tree | 9ef3ab2b6d2e07a74db7cb30e6c66a125822feaf /internal/cache | |
parent | 608b8ae5609cb8b6a7678b948075a00a489c6aa8 (diff) |
cache: Convert to use the locator
Convert the cache package to use the locator interface to determine
locations of cache and state directories. This will eventually allow us
to get rid of the old interface.
Diffstat (limited to 'internal/cache')
-rw-r--r-- | internal/cache/diskcache.go | 2 | ||||
-rw-r--r-- | internal/cache/keyer.go | 12 | ||||
-rw-r--r-- | internal/cache/walker.go | 48 | ||||
-rw-r--r-- | internal/cache/walker_test.go | 11 |
4 files changed, 50 insertions, 23 deletions
diff --git a/internal/cache/diskcache.go b/internal/cache/diskcache.go index fac695dcc..35570ef40 100644 --- a/internal/cache/diskcache.go +++ b/internal/cache/diskcache.go @@ -73,6 +73,7 @@ func withDisabledWalker() Option { // DiskCache stores and retrieves byte streams for repository related RPCs type DiskCache struct { + locator storage.Locator storages []config.Storage keyer leaseKeyer af activeFiles @@ -99,6 +100,7 @@ func New(cfg config.Cfg, locator storage.Locator, opts ...Option) *DiskCache { } cache := &DiskCache{ + locator: locator, storages: cfg.Storages, af: activeFiles{ Mutex: &sync.Mutex{}, diff --git a/internal/cache/keyer.go b/internal/cache/keyer.go index aa1c046a9..b5b45ea6f 100644 --- a/internal/cache/keyer.go +++ b/internal/cache/keyer.go @@ -19,7 +19,6 @@ import ( "gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/storage" "gitlab.com/gitlab-org/gitaly/v14/internal/metadata/featureflag" "gitlab.com/gitlab-org/gitaly/v14/internal/safe" - "gitlab.com/gitlab-org/gitaly/v14/internal/tempdir" "gitlab.com/gitlab-org/gitaly/v14/internal/version" "gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb" "google.golang.org/grpc" @@ -184,12 +183,12 @@ func (keyer leaseKeyer) newPendingLease(repo *gitalypb.Repository) (string, erro // cacheDir is $STORAGE/+gitaly/cache func (keyer leaseKeyer) cacheDir(repo *gitalypb.Repository) (string, error) { - storagePath, err := keyer.locator.GetStorageByName(repo.StorageName) + cacheDir, err := keyer.locator.CacheDir(repo.StorageName) if err != nil { - return "", fmt.Errorf("storage not found for %v", repo) + return "", fmt.Errorf("cache dir not found for %v", repo) } - return tempdir.AppendCacheDir(storagePath), nil + return cacheDir, nil } func (keyer leaseKeyer) getRepoStatePath(repo *gitalypb.Repository) (string, error) { @@ -198,7 +197,10 @@ func (keyer leaseKeyer) getRepoStatePath(repo *gitalypb.Repository) (string, err return "", fmt.Errorf("getRepoStatePath: storage not found for %v", repo) } - stateDir := tempdir.AppendStateDir(storagePath) + stateDir, err := keyer.locator.StateDir(repo.StorageName) + if err != nil { + return "", fmt.Errorf("getRepoStatePath: state dir not found for %v", repo) + } relativePath := repo.GetRelativePath() if len(relativePath) == 0 { diff --git a/internal/cache/walker.go b/internal/cache/walker.go index bdc54b494..037102674 100644 --- a/internal/cache/walker.go +++ b/internal/cache/walker.go @@ -6,6 +6,7 @@ package cache import ( + "fmt" "io/ioutil" "os" "path/filepath" @@ -13,8 +14,8 @@ import ( "github.com/sirupsen/logrus" "gitlab.com/gitlab-org/gitaly/v14/internal/dontpanic" + "gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/v14/internal/log" - "gitlab.com/gitlab-org/gitaly/v14/internal/tempdir" ) func (c *DiskCache) logWalkErr(err error, path, msg string) { @@ -104,26 +105,30 @@ func (c *DiskCache) walkLoop(walkPath string) { }) } -func (c *DiskCache) startCleanWalker(storagePath string) { +func (c *DiskCache) startCleanWalker(cacheDir, stateDir string) { if c.cacheConfig.disableWalker { return } - c.walkLoop(tempdir.AppendCacheDir(storagePath)) - c.walkLoop(tempdir.AppendStateDir(storagePath)) + c.walkLoop(cacheDir) + c.walkLoop(stateDir) } // moveAndClear will move the cache to the storage location's // temporary folder, and then remove its contents asynchronously -func (c *DiskCache) moveAndClear(storagePath string) error { +func (c *DiskCache) moveAndClear(storage config.Storage) error { if c.cacheConfig.disableMoveAndClear { return nil } - logger := logrus.WithField("path", storagePath) + logger := logrus.WithField("storage", storage.Name) logger.Info("clearing disk cache object folder") - tempPath := tempdir.AppendTempDir(storagePath) + tempPath, err := c.locator.TempDir(storage.Name) + if err != nil { + return fmt.Errorf("temp dir: %w", err) + } + if err := os.MkdirAll(tempPath, 0755); err != nil { return err } @@ -146,7 +151,12 @@ func (c *DiskCache) moveAndClear(storagePath string) error { }() logger.Infof("moving disk cache object folder to %s", tmpDir) - cachePath := tempdir.AppendCacheDir(storagePath) + + cachePath, err := c.locator.CacheDir(storage.Name) + if err != nil { + return fmt.Errorf("cache dir: %w", err) + } + if err := os.Rename(cachePath, filepath.Join(tmpDir, "moved")); err != nil { if os.IsNotExist(err) { logger.Info("disk cache object folder doesn't exist, no need to remove") @@ -162,16 +172,28 @@ func (c *DiskCache) moveAndClear(storagePath string) error { // StartWalkers starts the cache walker Goroutines. Initially, this function will try to clean up // any preexisting cache directories. func (c *DiskCache) StartWalkers() error { - pathSet := map[string]struct{}{} + // Deduplicate storages by path. + storageByPath := map[string]config.Storage{} for _, storage := range c.storages { - pathSet[storage.Path] = struct{}{} + storageByPath[storage.Path] = storage } - for sPath := range pathSet { - if err := c.moveAndClear(sPath); err != nil { + for _, storage := range storageByPath { + cacheDir, err := c.locator.CacheDir(storage.Name) + if err != nil { + return fmt.Errorf("cache dir: %w", err) + } + + stateDir, err := c.locator.StateDir(storage.Name) + if err != nil { + return fmt.Errorf("state dir: %w", err) + } + + if err := c.moveAndClear(storage); err != nil { return err } - c.startCleanWalker(sPath) + + c.startCleanWalker(cacheDir, stateDir) } return nil diff --git a/internal/cache/walker_test.go b/internal/cache/walker_test.go index f2a82a879..0af071c53 100644 --- a/internal/cache/walker_test.go +++ b/internal/cache/walker_test.go @@ -13,13 +13,13 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config" - "gitlab.com/gitlab-org/gitaly/v14/internal/tempdir" "gitlab.com/gitlab-org/gitaly/v14/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v14/internal/testhelper/testcfg" ) func TestDiskCacheObjectWalker(t *testing.T) { cfg := testcfg.Build(t) + locator := config.NewLocator(cfg) var shouldExist, shouldNotExist []string @@ -33,7 +33,8 @@ func TestDiskCacheObjectWalker(t *testing.T) { {"2b/ancient", 24 * time.Hour, true}, {"cd/baby", time.Second, false}, } { - cacheDir := tempdir.CacheDir(cfg.Storages[0]) + cacheDir, err := locator.CacheDir(cfg.Storages[0].Name) + require.NoError(t, err) path := filepath.Join(cacheDir, tt.name) require.NoError(t, os.MkdirAll(filepath.Dir(path), 0755)) @@ -51,7 +52,6 @@ func TestDiskCacheObjectWalker(t *testing.T) { } } - locator := config.NewLocator(cfg) cache := New(cfg, locator, withDisabledMoveAndClear()) require.NoError(t, cache.StartWalkers()) @@ -68,14 +68,15 @@ func TestDiskCacheObjectWalker(t *testing.T) { func TestDiskCacheInitialClear(t *testing.T) { cfg := testcfg.Build(t) + locator := config.NewLocator(cfg) - cacheDir := tempdir.CacheDir(cfg.Storages[0]) + cacheDir, err := locator.CacheDir(cfg.Storages[0].Name) + require.NoError(t, err) canary := filepath.Join(cacheDir, "canary.txt") require.NoError(t, os.MkdirAll(filepath.Dir(canary), 0755)) require.NoError(t, ioutil.WriteFile(canary, []byte("chirp chirp"), 0755)) - locator := config.NewLocator(cfg) cache := New(cfg, locator, withDisabledWalker()) require.NoError(t, cache.StartWalkers()) |