Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/strategy/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
19 changes: 10 additions & 9 deletions internal/strategy/git/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions internal/strategy/git/snapshotcoord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}