diff options
author | Karthik Nayak <knayak@gitlab.com> | 2023-05-15 20:20:20 +0300 |
---|---|---|
committer | Karthik Nayak <knayak@gitlab.com> | 2023-05-15 20:36:36 +0300 |
commit | 50f49f8e6a759de6ea193e289db091cb5afe7c38 (patch) | |
tree | 5ba9aff87b63c9251f3ee118880c2804c9310916 | |
parent | 6bc5d6991282a4c8bd7ba037d2960bb26188b059 (diff) |
housekeeping: Introduce and use `CleanStaleDataConfig`
We introduce a new struct `CleanStaleDataConfig`, which is used to hold
the configuration around which cleanup functions are to be run inside
`CleanStaleData`.
With this we also introduce a function `DefaultStaleDataCleanup` which
will provide the default configuration as it currently exists. We then
use this in `CleanStaleDataConfig` to provide the configuration.
In following commits we will make this an argument to
`CleanStaleDataConfig`.
-rw-r--r-- | internal/git/housekeeping/clean_stale_data.go | 54 |
1 files changed, 37 insertions, 17 deletions
diff --git a/internal/git/housekeeping/clean_stale_data.go b/internal/git/housekeeping/clean_stale_data.go index 24bdcb767..df571d7fc 100644 --- a/internal/git/housekeeping/clean_stale_data.go +++ b/internal/git/housekeeping/clean_stale_data.go @@ -47,8 +47,42 @@ type ( cleanupRepoWithTxManagerFunc func(context.Context, *localrepo.Repo, transaction.Manager) (int, error) ) +// CleanStaleDataConfig is the configuration for running CleanStaleData. It is used to define +// the different types of cleanups we want to run. +type CleanStaleDataConfig struct { + staleFileFinders map[string]findStaleFileFunc + repoCleanups map[string]cleanupRepoFunc + repoCleanupWithTxManagers map[string]cleanupRepoWithTxManagerFunc +} + +// DefaultStaleDataCleanup is the default configuration for CleanStaleData +// which contains all the cleanup functions. +func DefaultStaleDataCleanup() CleanStaleDataConfig { + return CleanStaleDataConfig{ + staleFileFinders: map[string]findStaleFileFunc{ + "objects": findTemporaryObjects, + "locks": findStaleLockfiles, + "refs": findBrokenLooseReferences, + "reflocks": findStaleReferenceLocks(referenceLockfileGracePeriod), + "packfilelocks": findStalePackFileLocks, + "packedrefslock": findPackedRefsLock, + "packedrefsnew": findPackedRefsNew, + "serverinfo": findServerInfo, + }, + repoCleanups: map[string]cleanupRepoFunc{ + "refsemptydir": removeRefEmptyDirs, + "configsections": pruneEmptyConfigSections, + }, + repoCleanupWithTxManagers: map[string]cleanupRepoWithTxManagerFunc{ + "configkeys": removeUnnecessaryConfig, + }, + } +} + // CleanStaleData cleans up any stale data in the repository. func (m *RepositoryManager) CleanStaleData(ctx context.Context, repo *localrepo.Repo) error { + cfg := DefaultStaleDataCleanup() + span, ctx := tracing.StartSpanIfHasParent(ctx, "housekeeping.CleanStaleData", nil) defer span.Finish() @@ -76,16 +110,7 @@ func (m *RepositoryManager) CleanStaleData(ctx context.Context, repo *localrepo. }() var filesToPrune []string - for staleFileType, staleFileFinder := range map[string]findStaleFileFunc{ - "objects": findTemporaryObjects, - "locks": findStaleLockfiles, - "refs": findBrokenLooseReferences, - "reflocks": findStaleReferenceLocks(referenceLockfileGracePeriod), - "packfilelocks": findStalePackFileLocks, - "packedrefslock": findPackedRefsLock, - "packedrefsnew": findPackedRefsNew, - "serverinfo": findServerInfo, - } { + for staleFileType, staleFileFinder := range cfg.staleFileFinders { staleFiles, err := staleFileFinder(ctx, repoPath) if err != nil { return fmt.Errorf("housekeeping failed to find %s: %w", staleFileType, err) @@ -105,10 +130,7 @@ func (m *RepositoryManager) CleanStaleData(ctx context.Context, repo *localrepo. } } - for repoCleanupName, repoCleanupFn := range map[string]cleanupRepoFunc{ - "refsemptydir": removeRefEmptyDirs, - "configsections": pruneEmptyConfigSections, - } { + for repoCleanupName, repoCleanupFn := range cfg.repoCleanups { cleanupCount, err := repoCleanupFn(ctx, repo) staleDataByType[repoCleanupName] = cleanupCount if err != nil { @@ -116,9 +138,7 @@ func (m *RepositoryManager) CleanStaleData(ctx context.Context, repo *localrepo. } } - for repoCleanupName, repoCleanupFn := range map[string]cleanupRepoWithTxManagerFunc{ - "configkeys": removeUnnecessaryConfig, - } { + for repoCleanupName, repoCleanupFn := range cfg.repoCleanupWithTxManagers { cleanupCount, err := repoCleanupFn(ctx, repo, m.txManager) staleDataByType[repoCleanupName] = cleanupCount if err != nil { |