fix(checker): take work as soon as a slot frees
With every slot busy, the poll loop paused a fixed second before asking for work again, so a checker took at most one batch of its slots per second, whatever its machine could do. Under load, runs piled up in the checker's queue and started late. The loop now waits for the first freed slot, at most fullHeartbeat (5s), so a checker whose slots stay busy still polls as a heartbeat. 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
6aabf9a364
commit
e776143ee0
2 files changed
+148
-4
No files matched your search
@@ -30,6 +30,9 @@ const (
|
||||
// quickPoll is a long poll answered empty faster than any wait would.
|
||||
quickPoll = 200 * time.Millisecond
|
||||
flushInterval = 2 * time.Second
|
||||
// fullHeartbeat is how often a checker with every slot busy still polls,
|
||||
// so the API keeps it live; well below schedule.StaleAfter.
|
||||
fullHeartbeat = 5 * time.Second
|
||||
// maxPending bounds results kept while the API is unreachable.
|
||||
maxPending = 50_000
|
||||
)
|
||||
@@ -39,6 +42,8 @@ type Runner struct {
|
||||
api API
|
||||
log *slog.Logger
|
||||
slots chan struct{}
|
||||
// freed is signalled when a slot is freed, for a poll waiting for one.
|
||||
freed chan struct{}
|
||||
run func(context.Context, check.Spec) check.Outcome
|
||||
|
||||
results chan agentapi.Result
|
||||
@@ -80,6 +85,7 @@ func New(api API, concurrency int, log *slog.Logger) *Runner {
|
||||
api: api,
|
||||
log: log,
|
||||
slots: make(chan struct{}, max(concurrency, 1)),
|
||||
freed: make(chan struct{}, 1),
|
||||
run: check.Run,
|
||||
results: make(chan agentapi.Result, 1024),
|
||||
|
||||
@@ -170,19 +176,49 @@ func (r *Runner) poll(ctx context.Context, running *sync.WaitGroup) error {
|
||||
r.slots <- struct{}{}
|
||||
running.Add(1)
|
||||
go func() {
|
||||
defer func() { <-r.slots; running.Done() }()
|
||||
defer func() { r.release(); running.Done() }()
|
||||
r.results <- r.execute(ctx, j)
|
||||
}()
|
||||
}
|
||||
// A long poll answered empty at once, as by an instance shutting
|
||||
// down, is not repeated at once either.
|
||||
if free == 0 || (len(jobs) == 0 && wait > 0 && time.Since(asked) < quickPoll) {
|
||||
switch {
|
||||
case free == 0:
|
||||
// Take work again as soon as a check finishes, not after a
|
||||
// fixed pause, which would cap throughput at slots per pause.
|
||||
r.waitForSlot(ctx, fullHeartbeat)
|
||||
case len(jobs) == 0 && wait > 0 && time.Since(asked) < quickPoll:
|
||||
// A long poll answered empty at once, as by an instance
|
||||
// shutting down, is not repeated at once either.
|
||||
sleep(ctx, time.Second)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// release frees a slot and wakes a poll waiting for one.
|
||||
func (r *Runner) release() {
|
||||
<-r.slots
|
||||
select {
|
||||
case r.freed <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
// waitForSlot returns once a slot is free, ctx is done or at most after
|
||||
// d.
|
||||
func (r *Runner) waitForSlot(ctx context.Context, d time.Duration) {
|
||||
t := time.NewTimer(d)
|
||||
defer t.Stop()
|
||||
for len(r.slots) == cap(r.slots) {
|
||||
select {
|
||||
case <-r.freed:
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-t.C:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Runner) execute(ctx context.Context, j agentapi.Job) agentapi.Result {
|
||||
// Checks already started finish even during shutdown; their timeout
|
||||
// bounds them.
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
package checker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/check"
|
||||
)
|
||||
|
||||
// backlogAPI hands out queued jobs as fast as the checker asks for them.
|
||||
type backlogAPI struct {
|
||||
mu sync.Mutex
|
||||
queued int
|
||||
polls int
|
||||
reported atomic.Int64
|
||||
}
|
||||
|
||||
func (b *backlogAPI) Jobs(ctx context.Context, max int, wait time.Duration) ([]agentapi.Job, error) {
|
||||
b.mu.Lock()
|
||||
b.polls++
|
||||
n := min(max, b.queued)
|
||||
b.queued -= n
|
||||
b.mu.Unlock()
|
||||
if n == 0 {
|
||||
select {
|
||||
case <-time.After(min(wait, 50*time.Millisecond)):
|
||||
case <-ctx.Done():
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
jobs := make([]agentapi.Job, n)
|
||||
for i := range jobs {
|
||||
jobs[i] = agentapi.Job{MonitorID: uuid.New(), Type: "tcp", Target: "x:1"}
|
||||
}
|
||||
return jobs, nil
|
||||
}
|
||||
|
||||
func (b *backlogAPI) Report(_ context.Context, rs []agentapi.Result) error {
|
||||
b.reported.Add(int64(len(rs)))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *backlogAPI) Release(context.Context) error { return nil }
|
||||
|
||||
// TestFullCheckerTakesWorkAsSlotsFree runs a backlog through a single slot.
|
||||
// A checker pausing a second whenever its slots are full would need about a
|
||||
// second per job.
|
||||
func TestFullCheckerTakesWorkAsSlotsFree(t *testing.T) {
|
||||
const jobs = 10
|
||||
api := &backlogAPI{queued: jobs}
|
||||
r := New(api, 1, slog.New(slog.DiscardHandler))
|
||||
var ran atomic.Int64
|
||||
r.run = func(context.Context, check.Spec) check.Outcome {
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
ran.Add(1)
|
||||
return check.Outcome{OK: true}
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- r.Run(ctx, time.Second) }()
|
||||
|
||||
start := time.Now()
|
||||
for ran.Load() < jobs && time.Since(start) < 5*time.Second {
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
took := time.Since(start)
|
||||
cancel()
|
||||
if err := <-done; err != nil {
|
||||
t.Errorf("Run() = %v", err)
|
||||
}
|
||||
if n := ran.Load(); n != jobs {
|
||||
t.Fatalf("ran %d checks in %v, want %d", n, took, jobs)
|
||||
}
|
||||
if took > 2*time.Second {
|
||||
t.Errorf("%d checks through one slot took %v, want well under a second each", jobs, took)
|
||||
}
|
||||
if n := api.reported.Load(); n != jobs {
|
||||
t.Errorf("reported %d results, want %d", n, jobs)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWaitForSlotKeepsPolling bounds the wait, so a checker whose slots
|
||||
// stay busy still polls as a heartbeat.
|
||||
func TestWaitForSlotKeepsPolling(t *testing.T) {
|
||||
r := New(nil, 1, slog.New(slog.DiscardHandler))
|
||||
r.slots <- struct{}{}
|
||||
start := time.Now()
|
||||
r.waitForSlot(context.Background(), 50*time.Millisecond)
|
||||
if took := time.Since(start); took < 50*time.Millisecond || took > time.Second {
|
||||
t.Errorf("waitForSlot() with every slot busy returned after %v, want the bound of 50ms", took)
|
||||
}
|
||||
go func() {
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
r.release()
|
||||
}()
|
||||
start = time.Now()
|
||||
r.waitForSlot(context.Background(), 5*time.Second)
|
||||
if took := time.Since(start); took > time.Second {
|
||||
t.Errorf("waitForSlot() returned %v after a slot was freed, want at once", took)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user