diff --git a/internal/strategy/git/git.go b/internal/strategy/git/git.go index eb7c450f..e702de4e 100644 --- a/internal/strategy/git/git.go +++ b/internal/strategy/git/git.go @@ -58,6 +58,7 @@ func Register(r *strategy.Registry, scheduler jobscheduler.Provider, cloneManage type Config struct { SnapshotInterval time.Duration `hcl:"snapshot-interval,optional" help:"How often to generate tar.zstd workstation snapshots. 0 disables snapshots." default:"0"` + SnapshotStartupSpread time.Duration `hcl:"snapshot-startup-spread,optional" help:"Upper bound of the random delay before each coordinated replica's first snapshot run, so replicas deployed together do not claim within one metadata sync window. 0 runs immediately." default:"5m"` SnapshotMaxAge time.Duration `hcl:"snapshot-max-age,optional" help:"How long an unchanged snapshot (same HEAD commit) may be served before regeneration. Requires shared metadata; keep well below the cache max-ttl. 0 regenerates every interval." default:"24h"` MirrorSnapshotInterval time.Duration `hcl:"mirror-snapshot-interval,optional" help:"How often to generate mirror snapshots for pod bootstrap. 0 uses snapshot-interval. Defaults to 2h." default:"2h"` RepackInterval time.Duration `hcl:"repack-interval,optional" help:"How often to run full repack. 0 disables." default:"0"` diff --git a/internal/strategy/git/snapshot.go b/internal/strategy/git/snapshot.go index 42f1e1cf..b0ea8792 100644 --- a/internal/strategy/git/snapshot.go +++ b/internal/strategy/git/snapshot.go @@ -381,12 +381,6 @@ func (s *Strategy) snapshotUnchanged(ctx context.Context, job string, key cache. return err == nil } -// snapshotStartupSpread staggers each replica's first coordinated snapshot -// run after registration. Periodic jobs run immediately when this pod has no -// recorded last run, so on a deploy every replica would otherwise decide -// within the same metadata sync window, before peers' claims propagate. -const snapshotStartupSpread = 5 * time.Minute - func (s *Strategy) scheduleSnapshotJobs(repo *gitclone.Repository) { if _, preparing := s.mirrorPreparations.Load(repo.UpstreamURL()); preparing { return @@ -435,8 +429,15 @@ func (s *Strategy) scheduleSnapshotJobsAfter(repo *gitclone.Repository, baseDela }) } -func startupSpreadDelay() time.Duration { - return rand.N(snapshotStartupSpread) //nolint:gosec // scheduling jitter needs no cryptographic randomness +// startupSpreadDelay staggers each replica's first coordinated snapshot run +// after registration. Periodic jobs run immediately when this pod has no +// recorded last run, so on a deploy every replica would otherwise decide +// within the same metadata sync window, before peers' claims propagate. +func startupSpreadDelay(spread time.Duration) time.Duration { + if spread <= 0 { + return 0 + } + return rand.N(spread) //nolint:gosec // scheduling jitter needs no cryptographic randomness } // Spread and jitter only help when replicas coordinate through shared @@ -446,7 +447,7 @@ func (s *Strategy) snapshotSchedule(interval time.Duration) (delay, jittered tim if s.snapshotCoord.Load() == nil { return 0, interval } - return startupSpreadDelay(), jitterInterval(interval) + return startupSpreadDelay(s.config.SnapshotStartupSpread), jitterInterval(interval) } // coordinatedSnapshotJob wraps a snapshot generation job with cross-replica diff --git a/internal/strategy/git/snapshotcoord_test.go b/internal/strategy/git/snapshotcoord_test.go index a10bfb58..a4461116 100644 --- a/internal/strategy/git/snapshotcoord_test.go +++ b/internal/strategy/git/snapshotcoord_test.go @@ -496,10 +496,11 @@ func TestJitterInterval(t *testing.T) { } func TestStartupSpreadDelay(t *testing.T) { + assert.Equal(t, time.Duration(0), startupSpreadDelay(0)) for range 100 { - d := startupSpreadDelay() + d := startupSpreadDelay(5 * time.Minute) assert.True(t, d >= 0) - assert.True(t, d < snapshotStartupSpread) + assert.True(t, d < 5*time.Minute) } } @@ -509,11 +510,11 @@ func TestSnapshotSchedule(t *testing.T) { assert.Equal(t, time.Duration(0), delay) assert.Equal(t, time.Hour, interval) - coordinated := &Strategy{} + coordinated := &Strategy{config: Config{SnapshotStartupSpread: 5 * time.Minute}} coordinated.snapshotCoord.Store(newTestSnapshotCoordinators(t, time.Now, 1)[0]) for range 100 { delay, interval = coordinated.snapshotSchedule(time.Hour) - assert.True(t, delay >= 0 && delay < snapshotStartupSpread) + assert.True(t, delay >= 0 && delay < 5*time.Minute) assert.True(t, interval >= time.Hour && interval < time.Hour+time.Hour/8) } }