diff options
author | Jacob Vosmaer <jacob@gitlab.com> | 2019-08-19 21:41:23 +0300 |
---|---|---|
committer | Paul Okstad <pokstad@gitlab.com> | 2019-08-19 21:41:23 +0300 |
commit | 292a78410114da3511bc676386921636d08c6e8f (patch) | |
tree | 14df10ebcc76988fd008b60b340f00a50a8e5a2a /internal/cache | |
parent | b10c2f83e5598b17d4985e277f607149627b5052 (diff) |
Prevent lazy config lookups in tempdir cleaners
Diffstat (limited to 'internal/cache')
-rw-r--r-- | internal/cache/keyer.go | 8 | ||||
-rw-r--r-- | internal/cache/walk_test.go | 3 | ||||
-rw-r--r-- | internal/cache/walker.go | 27 | ||||
-rw-r--r-- | internal/cache/walker_test.go | 6 |
4 files changed, 18 insertions, 26 deletions
diff --git a/internal/cache/keyer.go b/internal/cache/keyer.go index b8d4609d0..ab60e7f63 100644 --- a/internal/cache/keyer.go +++ b/internal/cache/keyer.go @@ -13,6 +13,7 @@ import ( "github.com/golang/protobuf/proto" "github.com/google/uuid" + "gitlab.com/gitlab-org/gitaly/internal/config" "gitlab.com/gitlab-org/gitaly/internal/helper" "gitlab.com/gitlab-org/gitaly/internal/safe" "gitlab.com/gitlab-org/gitaly/internal/tempdir" @@ -213,7 +214,12 @@ func newPendingLease(repo *gitalypb.Repository) (string, error) { // cacheDir is $STORAGE/+gitaly/cache func cacheDir(repo *gitalypb.Repository) (string, error) { - return tempdir.CacheDir(repo.GetStorageName()) + storage, ok := config.Config.Storage(repo.StorageName) + if !ok { + return "", fmt.Errorf("storage not found for %v", repo) + } + + return tempdir.CacheDir(storage), nil } func currentLeases(repo *gitalypb.Repository) ([]os.FileInfo, error) { diff --git a/internal/cache/walk_test.go b/internal/cache/walk_test.go index bfe037ff4..e7260214b 100644 --- a/internal/cache/walk_test.go +++ b/internal/cache/walk_test.go @@ -4,9 +4,10 @@ import ( "testing" "github.com/stretchr/testify/assert" + "gitlab.com/gitlab-org/gitaly/internal/config" ) func TestcleanWalkDirNotExists(t *testing.T) { - err := cleanWalk("/path/that/does/not/exist") + err := cleanWalk(config.Storage{Name: "foo", Path: "/path/that/does/not/exist"}) assert.NoError(t, err, "cleanWalk returned an error for a non existing directory") } diff --git a/internal/cache/walker.go b/internal/cache/walker.go index 6e0ba685a..aacd3c58a 100644 --- a/internal/cache/walker.go +++ b/internal/cache/walker.go @@ -16,13 +16,8 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/tempdir" ) -func cleanWalk(storageName string) error { - cachePath, err := tempdir.CacheDir(storageName) - if err != nil { - return err - } - - walkErr := filepath.Walk(cachePath, func(path string, info os.FileInfo, err error) error { +func cleanWalk(storage config.Storage) error { + walkErr := filepath.Walk(tempdir.CacheDir(storage), func(path string, info os.FileInfo, err error) error { if err != nil { return err } @@ -71,7 +66,7 @@ func startCleanWalker(storage config.Storage) { walkTick := time.NewTicker(cleanWalkFrequency) go func() { for { - if err := cleanWalk(storage.Name); err != nil { + if err := cleanWalk(storage); err != nil { logrus.WithField("storage", storage.Name).Error(err) } @@ -95,16 +90,7 @@ func moveAndClear(storage config.Storage) error { logger := logrus.WithField("storage", storage.Name) logger.Info("clearing disk cache object folder") - cachePath, err := tempdir.CacheDir(storage.Name) - if err != nil { - return err - } - - tempPath, err := tempdir.TempDir(storage.Name) - if err != nil { - return err - } - + tempPath := tempdir.TempDir(storage) if err := os.MkdirAll(tempPath, 0755); err != nil { return err } @@ -115,6 +101,7 @@ func moveAndClear(storage config.Storage) error { } logger.Infof("moving disk cache object folder to %s", tmpDir) + cachePath := tempdir.CacheDir(storage) 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") @@ -137,8 +124,8 @@ func moveAndClear(storage config.Storage) error { } func init() { - config.RegisterHook(func() error { - for _, storage := range config.Config.Storages { + config.RegisterHook(func(cfg config.Cfg) error { + for _, storage := range cfg.Storages { if err := moveAndClear(storage); err != nil { return err } diff --git a/internal/cache/walker_test.go b/internal/cache/walker_test.go index b3d3044d6..efef681b0 100644 --- a/internal/cache/walker_test.go +++ b/internal/cache/walker_test.go @@ -31,8 +31,7 @@ func TestDiskCacheObjectWalker(t *testing.T) { {"2b/ancient", 24 * time.Hour, true}, {"cd/baby", time.Second, false}, } { - cacheDir, err := tempdir.CacheDir(t.Name()) // test name is storage name - require.NoError(t, err) + cacheDir := tempdir.CacheDir(config.Config.Storages[0]) path := filepath.Join(cacheDir, tt.name) require.NoError(t, os.MkdirAll(filepath.Dir(path), 0755)) @@ -76,8 +75,7 @@ func TestDiskCacheInitialClear(t *testing.T) { cleanup := setupDiskCacheWalker(t) defer cleanup() - cacheDir, err := tempdir.CacheDir(t.Name()) // test name is storage name - require.NoError(t, err) + cacheDir := tempdir.CacheDir(config.Config.Storages[0]) canary := filepath.Join(cacheDir, "canary.txt") require.NoError(t, os.MkdirAll(filepath.Dir(canary), 0755)) |