fix(schedule): drop runs left queued for too long; shorter result claims
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m48s
ci / test (pull_request) Successful in 13m35s
ci / build (pull_request) Successful in 10m22s
ci / image (pull_request) Successful in 3m35s

A checker that fell behind kept working off runs from minutes ago while
dispatch queued newer runs of the same monitors behind them, so a backlog
never shrank: pawhost and realtoxmedia stayed at about 50K overdue checks.
Queued runs now record when they were dispatched, and Take drops runs
that waited longer than two intervals. A newer run of the monitor has
been queued by then, since the next run is due at most one and a half
intervals after a dispatch. uptimesebi_checks_stale_total counts them.

Result claims, one Redis key per check result, lived 2 hours although
retries come within minutes. At about 530 results a second that was
4.5 million keys and 1.1 GiB of the shared Redis. They now live 10
minutes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-29 19:41:54 +01:00
1 parent 71219d8031
commit 94c529a97e
5 files changed
+216 -44

No files matched your search

+93 -5
View File
@@ -33,7 +33,7 @@ func newTest(t *testing.T) (*Scheduler, *time.Time) {
func takeAll(t *testing.T, s *Scheduler, inst uuid.UUID) []Entry {
t.Helper()
e, err := s.Take(context.Background(), inst, 1000, 0)
e, _, err := s.Take(context.Background(), inst, 1000, 0)
if err != nil {
t.Fatalf("Take() failed: %v", err)
}
@@ -196,7 +196,7 @@ func TestTakeWaits(t *testing.T) {
ctx := context.Background()
in := uuid.New()
start := time.Now()
got, err := s.Take(ctx, in, 10, 300*time.Millisecond)
got, _, err := s.Take(ctx, in, 10, 300*time.Millisecond)
if err != nil || len(got) != 0 {
t.Fatalf("Take() on empty queue = %v, %v", got, err)
}
@@ -214,7 +214,7 @@ func TestTakeGetsAJobDuringAShortWait(t *testing.T) {
time.Sleep(100 * time.Millisecond)
s.rdb.RPush(ctx, jobsPrefix+in.String(), monitor.String()+"|1800000000000")
}()
got, err := s.Take(ctx, in, 10, 600*time.Millisecond)
got, _, err := s.Take(ctx, in, 10, 600*time.Millisecond)
if err != nil || len(got) != 1 || got[0].MonitorID != monitor {
t.Fatalf("Take() = %v, %v; want the job pushed while waiting", got, err)
}
@@ -225,7 +225,7 @@ func TestTakeReturnsWhenCancelled(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
go func() { time.Sleep(100 * time.Millisecond); cancel() }()
start := time.Now()
_, _ = s.Take(ctx, uuid.New(), 10, 20*time.Second)
_, _, _ = s.Take(ctx, uuid.New(), 10, 20*time.Second)
if took := time.Since(start); took > 2*time.Second {
t.Errorf("Take() returned %v after cancellation, want within about a second", took)
}
@@ -282,7 +282,7 @@ func TestOverdueCountsRunsLeftBehind(t *testing.T) {
if n := overdue(); n != 2 {
t.Errorf("Overdue() with the runs left in a queue = %d, want 2", n)
}
if _, err := s.Take(ctx, busy, 10, 0); err != nil {
if _, _, err := s.Take(ctx, busy, 10, 0); err != nil {
t.Fatal(err)
}
if n := overdue(); n != 0 {
@@ -421,3 +421,91 @@ func TestNextSlot(t *testing.T) {
}
}
}
// TestTakeDropsStaleRuns lets a busy instance fall behind: the runs it left
// in its queue for more than two intervals are dropped, the newer ones of
// the same monitors are handed out.
func TestTakeDropsStaleRuns(t *testing.T) {
s, now := newTest(t)
ctx := context.Background()
busy := uuid.New()
m := Monitor{ID: uuid.New(), Interval: time.Minute, Regions: []string{"eu"}}
_ = s.Upsert(ctx, m, nil)
_ = s.Heartbeat(ctx, "eu", busy)
mustDispatch(t, s)
// Within two intervals the run is still handed out.
*now = now.Add(2 * time.Minute)
if got, stale, err := s.Take(ctx, busy, 10, 0); err != nil || len(got) != 1 || stale != 0 {
t.Fatalf("Take() after two intervals = %v, %d stale, %v; want the run", got, stale, err)
}
// Left waiting longer, it is dropped in favour of the newer run.
_ = s.Heartbeat(ctx, "eu", busy)
mustDispatch(t, s)
*now = now.Add(2*time.Minute + time.Second)
_ = s.Heartbeat(ctx, "eu", busy)
if n := mustDispatch(t, s); n != 1 {
t.Fatalf("Dispatch() of the newer run = %d, want 1", n)
}
got, stale, err := s.Take(ctx, busy, 10, 0)
if err != nil {
t.Fatal(err)
}
if stale != 1 || len(got) != 1 || !got[0].DispatchedAt.Equal(*now) {
t.Errorf("Take() = %+v with %d stale, want only the run dispatched now and 1 stale", got, stale)
}
}
// TestTakeSkipsAllStaleRuns does not return empty-handed while fresh runs
// wait behind a batch of stale ones.
func TestTakeSkipsAllStaleRuns(t *testing.T) {
s, now := newTest(t)
ctx := context.Background()
in := uuid.New()
for range 5 {
_ = s.Upsert(ctx, Monitor{ID: uuid.New(), Interval: time.Minute, Regions: []string{"eu"}}, nil)
}
_ = s.Heartbeat(ctx, "eu", in)
mustDispatch(t, s)
*now = now.Add(3 * time.Minute)
fresh := Monitor{ID: uuid.New(), Interval: time.Minute, Regions: []string{"eu"}}
_ = s.Upsert(ctx, fresh, nil)
_ = s.Heartbeat(ctx, "eu", in)
mustDispatch(t, s)
// The first pop of 5 holds only stale runs.
got, stale, err := s.Take(ctx, in, 5, 0)
if err != nil {
t.Fatal(err)
}
if stale != 5 {
t.Errorf("stale runs = %d, want 5", stale)
}
// It pops on and hands out the next runs, all dispatched now.
if len(got) != 5 {
t.Fatalf("Take() = %d runs, want 5", len(got))
}
for _, e := range got {
if !e.DispatchedAt.Equal(*now) {
t.Errorf("Take() handed out %+v, want only runs dispatched now", e)
}
}
if rest := takeAll(t, s, in); len(rest) != 1 || rest[0].MonitorID != fresh.ID {
t.Errorf("remaining runs = %+v, want the new monitor's", rest)
}
}
func TestParseEntry(t *testing.T) {
id := uuid.New()
if e, ok := parseEntry(id.String() + "|1800000000000"); !ok || !e.DispatchedAt.IsZero() || e.ScheduledAt.UnixMilli() != 1_800_000_000_000 {
t.Errorf("entry without dispatch time = %+v, %v", e, ok)
}
if e, ok := parseEntry(id.String() + "|1800000000000|1800000005000"); !ok || e.DispatchedAt.UnixMilli() != 1_800_000_005_000 {
t.Errorf("entry with dispatch time = %+v, %v", e, ok)
}
for _, bad := range []string{"x|1", id.String(), id.String() + "|1|2|3", id.String() + "|1|x"} {
if _, ok := parseEntry(bad); ok {
t.Errorf("parseEntry(%q) accepted", bad)
}
}
}