Repository navigation
fix(control): no SQL in the control tick for curtailment identity - #1886
Merged
Merged
Conversation
PendingDrivers looked up every driver's device identity on each tick, even with nothing curtailed, and each lookup was a context-free SQLite query on the four-connection state pool. Skip the lookups when no curtailed identity is stored, and make LookupDeviceByDriverName read the committed in-memory device cache instead of SQL. This also removes the silent fall back to the driver name in batteryModelKey on a read error. Item A8 of the stable plan. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
…ored The previous commit read len(f.ids) under f.mu, dropped the lock, skipped the identity lookups, then took f.mu again. In that gap persist could move an active entry into f.ids, so the driver dropped out of the pending list and its release retry slipped a tick. TestForecastCurtailmentReleaseRequiresEvidence failed in about 3% of -race runs because of it. Now PendingDrivers drops f.mu only when it has identities to look up. Add a race test for the moved entry and a test that the device cache gives the same answer as the old SQL query across swaps, renames and a reopen. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
Item A8 of the stable plan.
Problem
On every tick,
forecastCurtail.PendingDrivers(reg.Names())(main.go:3224) callsLookupDeviceByDriverNameonce per driver, even when nothing is curtailed. Each call is as.db.QueryRowwith no context (state/devices.go:219) on a state.db pool capped at 4 connections. With 4 drivers and the default 2 s interval, that is 120 queries a minute that cannot change the answer. If something else holds all 4 connections, the tick waits with no bound. Any read error also returns nil, sobatteryModelKey(store.go:1392) quietly falls back to the driver name.Change
PendingDriverstakesf.muonce. When no curtailed identity is stored (f.idsempty), it keeps the lock and skips the identity lookups. It still walksf.entries, so a driver curtailed in this run without a known identity still shows as pending. Only when it has identities to look up does it drop the lock for the lookups, as on master.LookupDeviceByDriverNamereads the committeddeviceCacheunder its read lock and picks the newestLastSeenMsfor that driver name, as the SQL did.RegisterDeviceis the only writer ofdevicesand updates the cache after each write, andOpenloads the cache. This drops the SQL and the error fallback.The first commit read
len(f.ids), droppedf.mu, skipped the lookups and then tookf.muagain. In that gap the backgroundpersistcould move an active entry fromf.entriesintof.ids, and the driver dropped out of the pending list. That delayed a PV curtailment release retry by one tick. It also madeTestForecastCurtailmentReleaseRequiresEvidencefail in 20 of 600-raceruns, against 0 of 600 on master. An earlier note called that failure an existing flaky test. That was wrong: this PR caused it. The second commit fixes it.Tests
TestPendingDriversSkipsIdentityLookupsWithNothingCurtailed(from audit worktree caa-47): 30 ticks with 4 drivers. It counted 120 lookups on master and counts 0 now.TestLookupDeviceByDriverNameDoesNotWaitForStatePool(from caa-141): holds all 4 pool connections. On master the lookup waited over 1 s; it now answers from the cache.TestPendingDriversKeepsEntryMovedByPersist(from review): recordspvas curtailed, then callsPendingDriversover 20001 names while the worker persists. On the first commitpvwent missing in 73 of 100 runs under-raceand 61 of 100 without. With the fix it passes.TestLookupDeviceByDriverNameCacheMatchesSQL(from review): compares the cache lookup with the oldORDER BY last_seen_ms DESCquery across a hardware swap, a rename, the old device's return, a MAC-only id and a store reopen. It also checksbatteryModelKeyafter the reopen.go test -race ./cmd/ftw -run TestForecastCurtailmentReleaseRequiresEvidence -count=600: ok, 0 of 600 fail. This rerun shows the fix closes the gap.go test -race ./cmd/ftw -run 'Curtail|PendingDrivers' -count=5: okgo test -race ./cmd/ftw ./internal/state/... ./internal/notifications/... -count=1: okgo vet ./cmd/ftw ./internal/stateandgo build ./...: cleanRisk
Low. Lookups return the same row as before, unless the in-memory cache and the table disagree. That can only happen if a process outside Core edits the table while Core runs. Notifications, battery-model keys and the battery-model key migration use the same lookup and now read the cache too. No effect on sign handling, stale-meter stop, default mode, planner validation, fuse or SoC floor.
🤖 Generated with Claude Code