Repository navigation
Refetch a maintenance only when its status will change - #47
Merged
Merged
Conversation
The scan now decides from the batch-loaded statuses whether the computed status differs and refetches only then, instead of one GetIncident per event. Every event is still scanned, so out-of-band DB edits stay reconciled. The checker's event query no longer preloads components, which it never reads.
Member
Author
|
@ecosquad-autoreview review |
There was a problem hiding this comment.
Summary
This PR cuts per-tick GetIncident round-trips from the maintenance checker: processMaintenance now only refetches (and only runs the version-checked WithTx write) when the batch-loaded state disagrees with the status the checker would compute. It also drops the now-unused WithComponents preload from the shared getEventsByType query. CI is green (build, vet, unit tests, Docker acceptance tests, CodeQL, lint).
I verified the optimization is sound and the preload removal is safe. No blocking issues; approved.
Correctness (verified, no issues)
internal/checker/maintenance.go(~line 87,needsRefetch) — the skip decision comparesmn.StatusagainstcalculateCurrentMntStatus(calculateMntStatusHistory(mn), mn), which is exactly the valueevaluateAndFixMntStatusreturns. So!needsRefetch(mn)is a faithful early exit: the same code that used to computeactualStatusand findmn.Status == actualStatus(and no-op) now short-circuits before the refetch. Behavior for the write path (refetch → recompute →WithTxmodify+publish →Notify) is unchanged, so the version-conflict window and notification semantics are preserved. The second check (fresh.Status == actualStatus) after the refetch is still needed and correctly retained, covering the race where the batch state flagged a change but the fresh read is already at the target.- Out-of-band reconciliation — every event is still scanned in full each tick; only the refetch/write is conditional. Manual DB edits that change status or backfill state still produce
needsRefetch == trueand get reconciled, as the PR claims. internal/db/event_types.go(getEventsByType) — this helper is shared byGetMaintenancesandGetInfoEvents. I checked both consumers:internal/checker/maintenance.goandinternal/checker/info.gonever readComponents(only Status, dates, Text, ContactEmail, Statuses). No other callers exist, and build + acceptance tests confirm nothing depends on the edge being preloaded. Safe to remove.
Suggestions (non-blocking)
internal/checker/info.go(~line 63, pre-existing, not introduced by this PR) —CheckInfoEventscallsfixInfoMissedStatuses(...)before theif info.Status != actualStatusguard, so on every tick for a steady-state active info event it appends a redundant status entry in memory (no-op'd by the guard, then discarded sinceModifyIncidentis skipped). It's harmless today only because the discarded object is thrown away, but it's dead work per event per tick and will be a bug the moment anyone adds a side effect insidefixInfoMissedStatuses. The maintenance side of this PR now avoids exactly this pattern vianeedsRefetch. Consider mirroring that guard (if info.Status != actualStatus { fix + ModifyIncident }) in a follow-up, and be aware that if the info path ever needs to persist backfilled intermediate statuses, that logic currently lives after the no-change guard.internal/checker/maintenance_test.go(TestNeedsRefetch) — nice table tests. One untested case worth adding: aMaintenanceInProgressevent with a nilEndDate(open-ended, steady-state, expectsfalse) — it exercises the nil-pointer branch ofcalculateCurrentMntStatusthat the existing "completed" case doesn't reach.tests/checker_notifications_test.go— the newTestChecker_SteadyState_SkipsRefetchis a good acceptance-level guard on version bumps and outbox rows. A matching test for the transition path under this optimization (planned with a past start date → in_progress, asserting exactly one version bump and one outbox row across a scan) would pin the refetch path symmetrically.
Verdict
Approve. The change is correct, well-tested, and the perf win is real for large event sets. The suggestions are optional follow-ups.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Cut the per-tick query count of the maintenance scan. The checker now refetches an event only when the batch-loaded state disagrees with the status it would compute, instead of one
GetIncidentper event. The event set is still scanned in full, so out-of-band database edits stay reconciled.Changes
WithTxonly when the status changes.WithComponentspreload from the checker's event query; the checker never reads components.TestNeedsRefetchandTestChecker_SteadyState_SkipsRefetch.Verification
go build ./...,go vet ./...,go test -count=1 ./internal/...- passgo test -c ./tests/compiles; the integration suite needs Docker and runs in CINo new configuration and no new migrations.