diff options
author | Karthik Nayak <knayak@gitlab.com> | 2022-11-09 22:57:17 +0300 |
---|---|---|
committer | Karthik Nayak <knayak@gitlab.com> | 2022-11-10 17:17:31 +0300 |
commit | 6c4c312c381145f95bdb3418a6bfd5588c2e0933 (patch) | |
tree | 2a025db2cd9c9e1adbf9442e5078a291156b896e | |
parent | 7a8f7c377bd013483aba14ced8eafd073c631d4a (diff) |
housekeeping: Remove usage of `Time.AddDate(...)`
Currently we use `Time.AddDate(...)` in a few places in the housekeeping
package. The problem with this is that it directly adds/subtracts days
from the current time. This would not work well with DST changes. For
e.g. if you're subtracting day around the time of when DST changes
happen, you could potentially gain/loose an hour.
Instead lets use `Time.Add(...)` where we'd specify the number of hours
to be added/subtracted.
-rw-r--r-- | internal/git/housekeeping/optimization_strategy.go | 6 | ||||
-rw-r--r-- | internal/git/housekeeping/optimization_strategy_test.go | 2 | ||||
-rw-r--r-- | internal/git/housekeeping/optimize_repository_test.go | 4 |
3 files changed, 8 insertions, 4 deletions
diff --git a/internal/git/housekeeping/optimization_strategy.go b/internal/git/housekeeping/optimization_strategy.go index 30a402664..eb436f8d6 100644 --- a/internal/git/housekeeping/optimization_strategy.go +++ b/internal/git/housekeeping/optimization_strategy.go @@ -32,6 +32,10 @@ type OptimizationStrategy interface { ShouldWriteCommitGraph() (bool, WriteCommitGraphConfig) } +// CutOffTime is time delta that is used to indicate cutoff wherein an object would be considered +// old. Currently this is set to being 2 weeks (2 * 7days * 24hours). +const CutOffTime = -14 * 24 * time.Hour + // HeuristicalOptimizationStrategy is an optimization strategy that is based on a set of // heuristics. type HeuristicalOptimizationStrategy struct { @@ -91,7 +95,7 @@ func NewHeuristicalOptimizationStrategy(ctx context.Context, repo *localrepo.Rep return strategy, fmt.Errorf("estimating loose object count: %w", err) } - strategy.oldLooseObjectCount, err = estimateLooseObjectCount(repo, time.Now().AddDate(0, 0, -14)) + strategy.oldLooseObjectCount, err = estimateLooseObjectCount(repo, time.Now().Add(CutOffTime)) if err != nil { return strategy, fmt.Errorf("estimating old loose object count: %w", err) } diff --git a/internal/git/housekeeping/optimization_strategy_test.go b/internal/git/housekeeping/optimization_strategy_test.go index f9b293267..b1db18b57 100644 --- a/internal/git/housekeeping/optimization_strategy_test.go +++ b/internal/git/housekeeping/optimization_strategy_test.go @@ -74,7 +74,7 @@ func TestNewHeuristicalOptimizationStrategy_variousParameters(t *testing.T) { // ... and one stale object. staleObjectPath := filepath.Join(shard, "5678") require.NoError(t, os.WriteFile(staleObjectPath, nil, 0o644)) - twoWeeksAgo := time.Now().Add(-1 * 2 * 7 * 24 * time.Hour) + twoWeeksAgo := time.Now().Add(CutOffTime) require.NoError(t, os.Chtimes(staleObjectPath, twoWeeksAgo, twoWeeksAgo)) return repoProto diff --git a/internal/git/housekeeping/optimize_repository_test.go b/internal/git/housekeeping/optimize_repository_test.go index 67ae373e3..f69c8840b 100644 --- a/internal/git/housekeeping/optimize_repository_test.go +++ b/internal/git/housekeeping/optimize_repository_test.go @@ -277,7 +277,7 @@ gitaly_housekeeping_tasks_total{housekeeping_task="total", status="success"} 1 // We set the object's mtime to be almost two weeks ago. Given that // our timeout is at exactly two weeks this shouldn't caused them to // get pruned. - almostTwoWeeksAgo := time.Now().AddDate(0, 0, -14).Add(time.Minute) + almostTwoWeeksAgo := time.Now().Add(CutOffTime).Add(time.Minute) for i := 0; i < 10; i++ { blobPath := filepath.Join(repoPath, "objects", "17", fmt.Sprintf("%d", i)) @@ -309,7 +309,7 @@ gitaly_housekeeping_tasks_total{housekeeping_task="total", status="success"} 1 // broken, and thus we'll retry to prune them afterwards. require.NoError(t, os.MkdirAll(filepath.Join(repoPath, "objects", "17"), 0o755)) - moreThanTwoWeeksAgo := time.Now().AddDate(0, 0, -14).Add(-time.Minute) + moreThanTwoWeeksAgo := time.Now().Add(CutOffTime).Add(-time.Minute) for i := 0; i < 10; i++ { blobPath := filepath.Join(repoPath, "objects", "17", fmt.Sprintf("%d", i)) |