Repository navigation
perf(chatd): cache wake-up organization spend and purge old run counts - #358
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
The organization spend cap summed the cost of a day of chat messages in every chat that ever had a wake-up, on every delivery. Each Server now caches that sum per organization for 60 seconds on its quartz clock, and the query takes its chat set from the run counts of the window. dbpurge deletes run counts older than 7 days. Refs #297
@codex review |
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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6f7ca8b4ab
ℹ️ 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.
…ws-297-spend-cache # Conflicts: # coderd/x/chatd/ARCHITECTURE.md
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: daba8ed0fd
ℹ️ 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".
…sing A cached spend at the cap could pause a wake-up after the spend fell under the cap. Document that the organization spend cap covers the chats with a wake-up run in the window, and the one-minute cache lag.
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f56110b58a
ℹ️ 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".
…cap-counters-dd93dbf9
…ws-297-spend-cache
@codex review |
|
Codex Review: Didn't find any major issues. Nice work! 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
…ws-297-spend-cache
|
$> Coder Agents acted on behalf of @ibetitsmike.\n\n@codex review |
|
Codex Review: Didn't find any major issues. Delightful! 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". |
# Conflicts: # coderd/database/forkdb/chat_wakeups.sql.go # coderd/database/forkdb/querier.go # coderd/database/forkdb/queries/chat_wakeups.sql # coderd/x/chatd/ARCHITECTURE.md # coderd/x/chatd/fork_wakeup_caps.go # coderd/x/chatd/fork_wakeup_run_counts_internal_test.go
|
$> Coder Agents acted on behalf of @ibetitsmike.\n\n@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! 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". |
|
$> Coder Agents acted on behalf of @ibetitsmike.\n\n@codex review |
|
Codex Review: Didn't find any major issues. 🎉 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". |
|
$> Coder Agents acted on behalf of @ibetitsmike.\n\n@codex review |
|
Codex Review: Didn't find any major issues. More of your lovely PRs 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
On
Behavior note: the organization spend cap now counts only chats with a delivered run in the last 24 hours, as the PR body states. Not exercised: a live deploy, and the per-replica spend cache with more than one replica. |
Part 2 of 2 for #297 (perf plan #311). Part 1, #357, merged as 2523de1 and added the counter table and its trigger. This PR is based on
main.What changed
SumOrganizationWakeupChatCostMicrosSincetakes its chat set fromchat_wakeup_run_countsin the cap window (runs_since, the start of the hour 24 hours back) instead of every chat of the organization that ever had a wake-up. A chat whose wake-ups ran only before the window no longer counts.Servercaches the organization spend per organization for 60 seconds (wakeupSpendCache, hook lineforkWakeupSpendin theServerstruct). The cache uses thenowof the check, which comes from the Server's quartz clock. It reads again once the value is 60 s old, whennowis before the time it was read, or when the cached value is at or above the organization's cap, so a pause never rests on a cached value. A delivery under the caps with the spend cached issues 3 cap statements: caps row, run sums, chat spend.DeleteOldChatWakeupRunCounts, system delete in dbauthz) and logs the count at debug level.ARCHITECTURE.mddescribes the cache, its lag and the purge.docs/fork/pstack-adapter.mdsays the organization spend limit covers the chats with a wake-up run in the last 24 hours and that it can stop runs up to a minute late.Tradeoff
The organization spend cap can lag by up to 60 seconds, per replica. At the default cap that lets an organization overshoot by the spend of one minute of wake-ups. The run caps are not cached. The organization spend no longer counts a chat with a wake-up but no delivered run in the last 24 hours, so human spend in such a chat does not hold back other chats' wake-ups.
Rollback
This PR adds no migration node. Rolling back this PR alone needs no snapshot. A rollback past #357 needs the database snapshot, as #357 says.
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.TestWakeupCapStatements(gomock forkdbmock, each statementTimes(1)): the first check issues 4 statements, a check 59 s later issues 3, a check at 60 s reads the spend again, a check with the clock moved back reads it again, and a cached spend at the cap pauses once and is read again on the next check.TestWakeupCaps/OrganizationSpendOfChatsWithoutRecentRunsIgnored: spend of a chat whose last run is older than the window does not reach the cap.TestSumChatCostMicrosSince(forkdb) adds a stale chat that must not count. Its runs are inserted as deliveredchat_wakeup_runs, so the perf(chatd): count wake-up runs in hourly buckets #357 trigger fills the counter.TestDeleteOldChatWakeupRunCounts(dbpurge): rows older than 7 days go, newer rows stay.golangci-lint run --fix=falseon the touched packages reports nothing new in the changed files. It reports one existing finding infork_dbpurge.go:29(a short log message on main).gofmt -lis clean.Negative controls: with the cache hit disabled,
TestWakeupCapStatementsfails with an unexpected call toSumOrganizationWakeupChatCostMicrosSince. With the at-cap re-read removed, it fails because the next check still pauses.Not verified
Closes #297