diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-03-29 12:41:52 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-03-29 12:46:53 +0300 |
commit | 6cc6d4b4ac491dddbeff72b0b2525c188d0cd23b (patch) | |
tree | 683d896356d7f4bbf72830a7c8cbb36405487de1 | |
parent | b0451fae5cf4583b871125c57768966abc6020e9 (diff) |
maintenance: Convert repo optimizer to use new random walker
Now that we have a separate implementation of the random filesystem walk
via the `randomWalker`, this commit converts the `OptimizeRepository`
implementation to use it. This allows us to convert the current
recursive implementation into a simple loop, which is easier to reason
about and also easier to extend in subsequent commits.
No functional changes are expected from this commit.
-rw-r--r-- | internal/gitaly/maintenance/optimize.go | 41 |
1 files changed, 17 insertions, 24 deletions
diff --git a/internal/gitaly/maintenance/optimize.go b/internal/gitaly/maintenance/optimize.go index 69121325c..53f4f323a 100644 --- a/internal/gitaly/maintenance/optimize.go +++ b/internal/gitaly/maintenance/optimize.go @@ -2,7 +2,7 @@ package maintenance import ( "context" - "io/ioutil" + "errors" "math/rand" "os" "path/filepath" @@ -68,40 +68,31 @@ func optimizeRepoAtPath(ctx context.Context, l logrus.FieldLogger, s config.Stor return nil } -func walkReposShuffled(ctx context.Context, randSrc *rand.Rand, l logrus.FieldLogger, path string, s config.Storage, o Optimizer) error { - entries, err := ioutil.ReadDir(path) - switch { - case os.IsNotExist(err): - return nil // race condition: someone deleted it - case err != nil: - return err - } - - shuffleFileInfos(randSrc, entries) - - for _, e := range entries { +func walkReposShuffled(ctx context.Context, walker *randomWalker, l logrus.FieldLogger, s config.Storage, o Optimizer) error { + for { if err := ctx.Err(); err != nil { return err } - if !e.IsDir() { - continue + fi, path, err := walker.next() + switch { + case errors.Is(err, errIterOver): + return nil + case os.IsNotExist(err): + continue // race condition: someone deleted it + case err != nil: + return err } - absPath := filepath.Join(path, e.Name()) - if !storage.IsGitDirectory(absPath) { - if err := walkReposShuffled(ctx, randSrc, l, absPath, s, o); err != nil { - return err - } + if !fi.IsDir() || !storage.IsGitDirectory(path) { continue } + walker.skipDir() - if err := optimizeRepoAtPath(ctx, l, s, absPath, o); err != nil { + if err := optimizeRepoAtPath(ctx, l, s, path, o); err != nil { return err } } - - return nil } // OptimizeReposRandomly returns a function to walk through each storage and @@ -133,7 +124,9 @@ func OptimizeReposRandomly(storages []config.Storage, optimizer Optimizer, rand l.WithField("storage_path", storage.Path). Info("maintenance: optimizing repos in storage") - if err := walkReposShuffled(ctx, rand, l, storage.Path, storage, optimizer); err != nil { + walker := newRandomWalker(storage.Path, rand) + + if err := walkReposShuffled(ctx, walker, l, storage, optimizer); err != nil { l.WithError(err). WithField("storage_path", storage.Path). Errorf("maintenance: unable to completely walk storage") |