Repository navigation
perf(chatd): count wake-up runs in hourly buckets - #357
Conversation
The daily run caps counted chat_wakeup_runs rows over 24 hours on every delivery, one scan of a chat and one of a whole organization. A delivery now upserts a per-chat hourly bucket in chat_wakeup_run_counts in the send's transaction, and one statement sums at most 25 buckets for the chat and the organization. A chatdmig node creates the table and backfills it from the delivered runs of the last 25 hours. Refs #297
Codex Review Summary: ✅ CompletedThis 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. |
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: db5b612a27
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
A replica that does not know chat_wakeup_run_counts still inserts delivered runs during a rolling upgrade. A trigger on chat_wakeup_runs counts each delivered run in the inserting transaction, so those runs count against the daily caps too. The Go upsert is removed.
|
$> Coder Agents acted on behalf of @ibetitsmike.\n\n@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…cap-counters-dd93dbf9
|
$> Coder Agents acted on behalf of @ibetitsmike.\n\n@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…cap-counters-dd93dbf9
|
$> Coder Agents acted on behalf of @ibetitsmike.\n\n@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Merged at head Gates on this exact head:
Tests I ran on the PR commits at
Rollback: the new chatdmig node adds a table, a function and a trigger and has no down step. A rollback past this PR needs the database snapshot taken before the deploy. Old code keeps working on the new schema; the trigger only fills a table old code does not read. Not exercised: a live deploy, the backfill on the real database, a rolling upgrade with an old replica, and how long the trigger creation holds its lock on |
Part 1 of 2 for #297 (perf plan #311). Part 2, #358, stacked on this branch, adds the organization spend cache and the purge of old counter rows.
What changed
01a1e197-..._chat_wakeup_run_countscreateschatd.chat_wakeup_run_counts: one row per chat and UTC hour (hour timestamptz, truncated to the hour) withorganization_idandruns. Primary key(chat_id, hour), indexchat_wakeup_run_counts_org_hour_idxon(organization_id, hour) INCLUDE (runs).trigger_count_delivered_chat_wakeup_run,AFTER INSERT ON chat_wakeup_runs FOR EACH ROW WHEN (NEW.status = 'delivered'). It upserts the hour bucket of the run'screated_atin the transaction that inserts the run, which is the transaction of the chat message. A delivery that rolls back counts nothing. A replica still on the old code during a rolling upgrade also inserts delivered runs, so its deliveries count too. No Go code writes the counter.CREATE TRIGGERholds a lock that blocks inserts intochat_wakeup_runsuntil the node commits, so each run is counted once, by the trigger or by the backfill.wakeupCapReachedreplaces the twoCOUNT(*)scans ofchat_wakeup_runswith one statement,SumChatWakeupRunCountsSince, that sums the chat's buckets on the primary key and the organization's buckets on the new index. The window is still a rolling 24 hours. It starts atsince.UTC().Truncate(time.Hour), so it can count up to one hour more than 24 hours and never less.CountChatWakeupRunsSinceandCountOrganizationChatWakeupRunsSinceare removed.coderd/x/chatd/ARCHITECTURE.mddescribes the counter table and the cap check.Why
chat_wakeup_runsstaysIt is still used on main.
admitWakeupDeliveryinserts the delivered run withON CONFLICT (wakeup_id, run_key) DO NOTHING, which is the occurrence guard that returnserrWakeupOccurrenceRan, andfinishWakeupSendrecords failed and skipped runs there. So this PR does not drop it. The guard looks redundant withlockDueWakeup, so the table could go in a follow-up.Rollback
The new node adds a table, a function and a trigger and has no down step, like every chatdmig node. A rollback past this PR needs the database snapshot taken before the deploy. Old code that runs against the new schema keeps working; the trigger only fills a table it does not read.
EXPLAIN
Scratch database with 100,800 counter rows (20 organizations, 600 chats, 168 hours each), after
VACUUM (ANALYZE, FREEZE). The preparedSumChatWakeupRunCountsSincestatement:The org sum is an index-only scan on the new index with no heap fetches. A plain
VACUUMleftrelallvisibleat 0 on the fresh table; autovacuum sets it on a live table.Tests
All run locally against Postgres 16:
go test ./coderd/x/chatd/...passed.go test ./coderd/database/forkdb/ ./coderd/database/dbauthz/ ./coderd/database/dbpurge/passed.CHATDMIG_TEST_DATABASE_URL=... go test ./coderd/database/migrations/chatdmig/...passed, includingTestUpgradeMatchesFresh,TestForkNodesOnlyAddToChatdand the newTestWakeupRunCountsBackfill.TestWakeupRunCounts:EachDeliveryCounts: three deliveries for one chat sum to 3.BusyChatDoesNotCount: a chat that turns busy during the send gets no message and no count.RolledBackAdmissionDoesNotCount:admitWakeupDeliveryin a transaction that rolls back leaves no count; the same call committed leaves 1.OrganizationRunCapStopsThirdChat: with the org run cap at 2, three chats deliver in turn and the third pauses withorg_daily_run_cap.TestChatWakeupRuns(forkdb) inserts delivered and failed runs in different hours and checks the trigger's buckets and the chat and org sums.TestWakeupRunCountsBackfill(chatdmig) checks the backfill and then that the trigger counts a delivered run after the node and skips a failed one.golangci-lint run --fix=falseon the touched packages reports nothing in the changed files.gofmt -lis clean.Negative control: with the
CREATE TRIGGERremoved from the node,EachDeliveryCounts,RolledBackAdmissionDoesNotCount,OrganizationRunCapStopsThirdChat,TestWakeupRunCountsBackfillandTestChatWakeupRunsfail.BusyChatDoesNotCountstill passes, as expected (see below).Not verified
errWakeupChatBusyinforkAdmitbeforeadmitWakeupDeliveryruns, soBusyChatDoesNotCountproves nothing is counted but does not roll back a counted run.RolledBackAdmissionDoesNotCountcovers the rollback directly.Refs #297. #358 closes it.