diff --git a/docs/release-control/v6/internal/subsystems/monitoring.md b/docs/release-control/v6/internal/subsystems/monitoring.md index e37fda9370..2732c86fad 100644 --- a/docs/release-control/v6/internal/subsystems/monitoring.md +++ b/docs/release-control/v6/internal/subsystems/monitoring.md @@ -9982,9 +9982,14 @@ not flap the watchdog while another target polled less often than the threshold sits idle between returns. A newer claim still inside its budget neither causes nor clears a stall, an empty queue is never stalled, a dispatcher handing back a claim it never admitted is not a return, and a -target removed from the queue stops counting as progress. +target removed from the queue stops counting as progress. A claim deferred +before execution by a circuit breaker's retry deadline keeps its protected +retry slot without counting as a return; a subsequent admitted poll coming +back still counts. `TestPollClaimProgressStall`, `TestTaskQueueTracksClaimsForPollStall`, and `TestDeadManMonitoringProgressReportsWedgedPolls` pin these boundaries. +`TestWatchdogDoesNotCountBreakerDeferralAsPollProgress` exercises the worker +deferral and retry through the external signal and local stall alert. Agent intake is checked the same way, because host, Docker and Kubernetes reports are applied on request goroutines rather than through the poll scheduler, and an agent-only install has an empty queue. diff --git a/internal/monitoring/poll_task_workers.go b/internal/monitoring/poll_task_workers.go index ad2ffec00a..42a0d27e91 100644 --- a/internal/monitoring/poll_task_workers.go +++ b/internal/monitoring/poll_task_workers.go @@ -147,7 +147,7 @@ func (m *Monitor) executeAndRescheduleTask(ctx context.Context, task ScheduledTa // A plan or explicit request can be due before the breaker permits a // probe. Nothing ran: don't reschedule from the previous result or // record a second dead letter for the same failed read. - next, reschedule = task, pollAfterBackoff + next, reschedule = task, pollDeferredByBreaker next.NextRun = retryAt return } diff --git a/internal/monitoring/task_queue.go b/internal/monitoring/task_queue.go index d3ccba2404..35ae35f3d2 100644 --- a/internal/monitoring/task_queue.go +++ b/internal/monitoring/task_queue.go @@ -23,6 +23,7 @@ const ( pollNotStarted pollOnCadence pollAfterBackoff + pollDeferredByBreaker // protect a retry deadline without recording poll progress ) type scheduledTaskEntry struct { @@ -267,7 +268,7 @@ func (q *TaskQueue) completeClaim(task, next ScheduledTask, reschedule pollResch if !ok || entry.index >= 0 { return } - q.claimProgress.release(key, time.Now(), reschedule != pollNotStarted) + q.claimProgress.release(key, time.Now(), reschedule != pollNotStarted && reschedule != pollDeferredByBreaker) switch entry.followup { case followExplicitSchedule: // Upsert has already stored the replacement in the claimed entry. @@ -282,7 +283,7 @@ func (q *TaskQueue) completeClaim(task, next ScheduledTask, reschedule pollResch // its explicit/backoff protection rather than planning a new slot. default: entry.task = next - entry.keepSchedule = reschedule == pollAfterBackoff + entry.keepSchedule = reschedule == pollAfterBackoff || reschedule == pollDeferredByBreaker } case dropWorkerSchedule: q.forgetEntryLocked(key) diff --git a/internal/monitoring/task_queue_claim_progress_test.go b/internal/monitoring/task_queue_claim_progress_test.go index 0994e6fc13..268cd218db 100644 --- a/internal/monitoring/task_queue_claim_progress_test.go +++ b/internal/monitoring/task_queue_claim_progress_test.go @@ -2,8 +2,12 @@ package monitoring import ( "context" + "net/http" "testing" + "testing/synctest" "time" + + "github.com/rcourtman/pulse-go-rewrite/internal/alerts" ) func TestPollClaimProgressStall(t *testing.T) { @@ -252,3 +256,93 @@ func TestDeadManMonitoringProgressReportsWedgedPolls(t *testing.T) { t.Fatal("wedged poll progress was lost without a loop mark") } } + +func TestWatchdogDoesNotCountBreakerDeferralAsPollProgress(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + ctx := context.Background() + q := NewTaskQueue() + // A VE claim entered the scheduler earlier and has never returned. + hung := ScheduledTask{InstanceType: InstanceTypePVE, InstanceName: "fixture-ve", NextRun: time.Now()} + q.Upsert(hung) + if _, ok := q.WaitNext(ctx); !ok { + t.Fatal("could not claim fixture VE poll") + } + time.Sleep(time.Minute + deadManPollStallGrace + time.Second) + + task := ScheduledTask{InstanceType: InstanceTypePBS, InstanceName: "fixture-pbs", Interval: time.Minute, NextRun: time.Now()} + breaker := newCircuitBreaker(1, time.Minute, 5*time.Minute, time.Minute) + breaker.recordFailure(time.Now()) + _, _, retryAt := breaker.State() + m := &Monitor{ + taskQueue: q, pollTimeout: time.Minute, + circuitBreakers: map[string]*circuitBreaker{schedulerKey(task.InstanceType, task.InstanceName): breaker}, + } + m.executor = newRealExecutor(m) + built, ran := 0, 0 + if err := m.RegisterPollProvider(testPollProvider{ + providerType: task.InstanceType, + buildPollTask: func(string) (PollTask, error) { + built++ + return PollTask{Run: func(context.Context) { ran++ }}, nil + }, + }); err != nil { + t.Fatal(err) + } + + manager := alerts.NewManagerWithDataDir(t.TempDir(), alerts.WithoutPersistedAlertRestore()) + defer manager.Stop() + var path string + d := newDeadManTestRuntime(t, time.Now(), deadManRoundTripFunc(func(r *http.Request) (*http.Response, error) { + path = r.URL.Path + return deadManResponse(http.StatusOK, "OK"), nil + })) + d.now = time.Now + checkCycle := func(wantPath string, wantAlert bool) { + t.Helper() + m.markDeadManMonitoringProgress(time.Now()) + d.runCycle(ctx, func() string { return "https://watchdog.example.com/ping/fixture" }, m.deadManMonitoringProgress) + d.alertProjection.drain(manager) + if path != wantPath { + t.Errorf("watchdog path = %q, want %q", path, wantPath) + } + found := false + for _, alert := range manager.GetActiveAlerts() { + found = found || alert.Type == alerts.DeadManMonitoringStalledAlertType + } + if found != wantAlert { + t.Errorf("active monitoring stall alert = %t, want %t", found, wantAlert) + } + } + + q.Upsert(task) + runNext := func() { + t.Helper() + claimed, ok := q.WaitNext(ctx) + if !ok || claimed.InstanceName != task.InstanceName { + t.Fatalf("could not claim fixture PBS poll: %+v", claimed) + } + m.executeAndRescheduleTask(ctx, claimed, nil, 0) + } + runNext() + if built != 0 || ran != 0 { + t.Fatalf("breaker deferral built %d and ran %d provider tasks; want neither", built, ran) + } + if retry, ok := q.Get(task.InstanceType, task.InstanceName); !ok || !retry.NextRun.Equal(retryAt) || retry.Interval != task.Interval { + t.Fatalf("deferred retry = %+v, present=%t, want existing interval and retry at %v", retry, ok, retryAt) + } + // A routine plan refresh cannot erase the retry deadline. + q.upsertPlan(task, 0) + if retry, ok := q.Get(task.InstanceType, task.InstanceName); !ok || !retry.NextRun.Equal(retryAt) { + t.Fatalf("plan refresh lost deferred retry: %+v, present=%t", retry, ok) + } + checkCycle("/ping/fixture/fail", true) + + // The next claim waits for the half-open probe and actually runs. + // A returning sibling still suppresses a false whole-scheduler alarm. + runNext() + if built != 1 || ran != 1 || !time.Now().Equal(retryAt) { + t.Fatalf("probe built=%d ran=%d at %v, want one probe at %v", built, ran, time.Now(), retryAt) + } + checkCycle("/ping/fixture", false) + }) +}