feat(schedule): spread monitor runs evenly over their interval
A monitor's next run was its last due time plus the interval, so monitors created together, by an import or a test, stayed due in the same second for good, and late runs all gathered at the dispatch time. In prod, regions fell behind in waves every interval although the checkers kept up on average. Each monitor now has a fixed phase within its interval, from its ID. After the first run, still right away, it runs on its phase; late runs snap to their next slot. Reconcile adds lost monitors on their phase, so rebuilding a schedule causes no burst either. Monitors scheduled before phases existed keep the old rule until reconciliation adds their phase. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
6d4842f4a3
commit
050c9dd0a1
2 files changed
+220
-14
No files matched your search
@@ -100,11 +100,20 @@ func TestDispatchReschedulesAndKeepsRegionsApart(t *testing.T) {
|
||||
if n := mustDispatch(t, s); n != 0 {
|
||||
t.Errorf("Dispatch() before next interval = %d, want 0", n)
|
||||
}
|
||||
*now = now.Add(30 * time.Second)
|
||||
// The next run is on the monitor's phase, at least half an interval
|
||||
// after the first one.
|
||||
next := dueAt(t, s, "eu", m.ID)
|
||||
if off := (next.UnixMilli() - phaseOf(m.ID, m.Interval)) % m.Interval.Milliseconds(); off != 0 {
|
||||
t.Errorf("next run %v is %dms off its phase", next, off)
|
||||
}
|
||||
if gap := next.Sub(*now); gap < m.Interval/2 || gap > 3*m.Interval/2 {
|
||||
t.Errorf("next run %v after the first, want between half and one and a half intervals", gap)
|
||||
}
|
||||
*now = next
|
||||
_ = s.Heartbeat(ctx, "eu", eu)
|
||||
_ = s.Heartbeat(ctx, "us", us)
|
||||
if n := mustDispatch(t, s); n != 2 {
|
||||
t.Errorf("Dispatch() after interval = %d, want 2", n)
|
||||
t.Errorf("Dispatch() on the next slot = %d, want 2", n)
|
||||
}
|
||||
|
||||
// Leaving a region stops runs there.
|
||||
@@ -177,6 +186,9 @@ func TestRemoveAndReconcile(t *testing.T) {
|
||||
if len(got) != 1 || got[0].MonitorID != keep.ID {
|
||||
t.Errorf("jobs after Reconcile = %+v, want only %v", got, keep.ID)
|
||||
}
|
||||
if s.rdb.HExists(ctx, keyPhases, gone.ID.String()).Val() {
|
||||
t.Error("Reconcile left the phase of a removed monitor")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTakeWaits(t *testing.T) {
|
||||
@@ -277,3 +289,135 @@ func TestOverdueCountsRunsLeftBehind(t *testing.T) {
|
||||
t.Errorf("Overdue() after the instance took the runs = %d, want 0", n)
|
||||
}
|
||||
}
|
||||
|
||||
func dueAt(t *testing.T, s *Scheduler, region string, id uuid.UUID) time.Time {
|
||||
t.Helper()
|
||||
score, err := s.rdb.ZScore(context.Background(), keyDue(region), id.String()).Result()
|
||||
if err != nil {
|
||||
t.Fatalf("no due time for %v in %s: %v", id, region, err)
|
||||
}
|
||||
return time.UnixMilli(int64(score))
|
||||
}
|
||||
|
||||
// TestPhaseSpreadsBurst creates many monitors at once. Their first runs are
|
||||
// right away; after that they spread over the interval instead of all
|
||||
// running in the same second again.
|
||||
func TestPhaseSpreadsBurst(t *testing.T) {
|
||||
s, now := newTest(t)
|
||||
ctx := context.Background()
|
||||
in := uuid.New()
|
||||
const monitors = 600
|
||||
for range monitors {
|
||||
if err := s.Upsert(ctx, Monitor{ID: uuid.New(), Interval: time.Minute, Regions: []string{"eu"}}, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
perSecond := make([]int, 180)
|
||||
for sec := range perSecond {
|
||||
_ = s.Heartbeat(ctx, "eu", in)
|
||||
perSecond[sec] = mustDispatch(t, s)
|
||||
takeAll(t, s, in)
|
||||
*now = now.Add(time.Second)
|
||||
}
|
||||
if perSecond[0] != monitors {
|
||||
t.Errorf("first second dispatched %d, want all %d first runs", perSecond[0], monitors)
|
||||
}
|
||||
total, peak := 0, 0
|
||||
for _, n := range perSecond[60:] {
|
||||
total += n
|
||||
peak = max(peak, n)
|
||||
}
|
||||
if total != 2*monitors {
|
||||
t.Errorf("seconds 60 to 180 dispatched %d runs, want %d: one per monitor and minute", total, 2*monitors)
|
||||
}
|
||||
// 10 per second on average; all 600 at once without phases.
|
||||
if peak > 30 {
|
||||
t.Errorf("busiest second dispatched %d runs, want them spread at about 10 per second", peak)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLateRunSnapsToPhase dispatches runs long overdue. Each comes back on
|
||||
// its own phase instead of all gathering at the dispatch time.
|
||||
func TestLateRunSnapsToPhase(t *testing.T) {
|
||||
s, now := newTest(t)
|
||||
ctx := context.Background()
|
||||
in := uuid.New()
|
||||
ms := []Monitor{}
|
||||
for range 50 {
|
||||
m := Monitor{ID: uuid.New(), Interval: time.Minute, Regions: []string{"eu"}}
|
||||
ms = append(ms, m)
|
||||
_ = s.Upsert(ctx, m, nil)
|
||||
}
|
||||
_ = s.Heartbeat(ctx, "eu", in)
|
||||
mustDispatch(t, s)
|
||||
// No instance for three intervals.
|
||||
*now = now.Add(3*time.Minute + 7*time.Second)
|
||||
_ = s.Heartbeat(ctx, "eu", in)
|
||||
if n := mustDispatch(t, s); n != len(ms) {
|
||||
t.Fatalf("Dispatch() of the overdue runs = %d, want %d", n, len(ms))
|
||||
}
|
||||
distinct := map[time.Time]bool{}
|
||||
for _, m := range ms {
|
||||
got := dueAt(t, s, "eu", m.ID)
|
||||
want := time.UnixMilli(nextSlot(now.UnixMilli(), phaseOf(m.ID, m.Interval), m.Interval.Milliseconds()))
|
||||
if !got.Equal(want) {
|
||||
t.Errorf("monitor next due %v, want its next slot %v", got, want)
|
||||
}
|
||||
distinct[got] = true
|
||||
}
|
||||
if len(distinct) < len(ms)-5 {
|
||||
t.Errorf("%d monitors share %d due times, want them apart", len(ms), len(distinct))
|
||||
}
|
||||
}
|
||||
|
||||
// TestReconcileAddsOnPhase rebuilds a lost schedule without a burst.
|
||||
func TestReconcileAddsOnPhase(t *testing.T) {
|
||||
s, now := newTest(t)
|
||||
ctx := context.Background()
|
||||
in := uuid.New()
|
||||
_ = s.Heartbeat(ctx, "eu", in)
|
||||
ms := []Monitor{}
|
||||
for range 20 {
|
||||
ms = append(ms, Monitor{ID: uuid.New(), Interval: time.Minute, Regions: []string{"eu"}})
|
||||
}
|
||||
if err := s.Reconcile(ctx, ms, *now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, m := range ms {
|
||||
want := time.UnixMilli(nextSlot(now.UnixMilli(), phaseOf(m.ID, m.Interval), m.Interval.Milliseconds()))
|
||||
if got := dueAt(t, s, "eu", m.ID); !got.Equal(want) {
|
||||
t.Errorf("reconciled monitor due %v, want its slot %v", got, want)
|
||||
}
|
||||
}
|
||||
if n := mustDispatch(t, s); n != 0 {
|
||||
t.Errorf("Dispatch() right after Reconcile = %d, want 0", n)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPhaseFallback covers monitors scheduled before phases existed.
|
||||
func TestPhaseFallback(t *testing.T) {
|
||||
s, now := newTest(t)
|
||||
ctx := context.Background()
|
||||
in := uuid.New()
|
||||
_ = s.Heartbeat(ctx, "eu", in)
|
||||
m := Monitor{ID: uuid.New(), Interval: time.Minute, Regions: []string{"eu"}}
|
||||
_ = s.Upsert(ctx, m, nil)
|
||||
s.rdb.HDel(ctx, keyPhases, m.ID.String())
|
||||
mustDispatch(t, s)
|
||||
if got, want := dueAt(t, s, "eu", m.ID), now.Add(time.Minute); !got.Equal(want) {
|
||||
t.Errorf("monitor without phase next due %v, want one interval later, %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNextSlot(t *testing.T) {
|
||||
for _, c := range []struct{ now, phase, interval, want int64 }{
|
||||
{now: 100, phase: 30, interval: 60, want: 150},
|
||||
{now: 150, phase: 30, interval: 60, want: 210},
|
||||
{now: 10, phase: 30, interval: 60, want: 30},
|
||||
{now: 100, phase: 0, interval: 0, want: 100},
|
||||
} {
|
||||
if got := nextSlot(c.now, c.phase, c.interval); got != c.want {
|
||||
t.Errorf("nextSlot(%d, %d, %d) = %d, want %d", c.now, c.phase, c.interval, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user