Skip to content

perf(coderd/x/plugins): renew plugin leases without reading full rows - #360

Merged
ibetitsmike merged 2 commits into
mainfrom
mike/issue-305-lease-renewal
Nov 16, 2026
Merged

ibetitsmike merged 2 commits into
mainfrom
mike/issue-305-lease-renewal

Conversation

@ibetitsmike

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Part of #305. This PR does item 2 (renew without reading rows) and closes out item 3. Item 4 (restart backoff) remains and comes in a follow-up PR stacked on this branch.

What changed

  • New query RenewPluginLeases: UPDATE plugins SET lease_expires_at = now() + lease WHERE runner_id = $1 AND enabled RETURNING id, updated_at. It returns no config, so a renewal reads and decrypts nothing.
  • AcquirePluginLeases now takes only plugins no replica holds (runner_id IS DISTINCT FROM me and the lease is null or expired), limited to the free slots (MaxPlugins - renewed).
  • Manager.reconcile renews first, then acquires. For a renewed plugin with the same updated_at as its runner, it reuses the runner's plugin row: a live runner keeps running and an exited one restarts with no read. It calls GetPluginByID (decrypted through dbcrypt) only when updated_at changed or no runner exists. A failed read keeps the current runner and retries on the next tick. The idle path (GetPluginLeaseSummary) is unchanged.
  • Wrappers: dbauthz (ActionUpdate on ResourceSystem, same as AcquirePluginLeases) with its TestForkQuerier entry, dbmetrics, and a note in the dbcrypt wrapper that the renewal passes through because it returns no secret.
  • Generated with flock ~/.fork-go.lock ./coderd/database/forkdb/generate.sh. make gen does not run the forkdb generator.

Archived chat bindings are kept, not deleted

Deleting a binding would lose its cursor and its delivered mark, so an unarchived chat would replay its whole assistant history (a chat created after the link rebinds at cursor 0) or be handed to another idle link. The due query already skips archived chats, so a kept binding costs one small row. TestPluginChatBindings now pins that an archived chat is not due and that unarchiving keeps its cursor and delivered mark.

Tests

All run with flock ~/.fork-go.lock.

  • go test -race -count=10 ./coderd/x/plugins/ -run TestManager -v: 70 PASS, 0 FAIL (7 tests x 10).
  • go test -race -count=1 ./coderd/x/plugins/...: ok (plugins, pluginhost, sandbox).
  • go test -count=1 ./coderd/database/forkdb -run TestPlugin -v: 7 PASS, 0 FAIL.
  • go test -count=1 ./coderd/database/dbauthz -run TestForkQuerier: ok, including TestForkQuerier/RenewPluginLeases.
  • go test -count=1 ./coderd/database/dbmetrics/... ./enterprise/dbcrypt/... ./coderd/database/forkdb/...: ok.
  • go test -count=1 ./coderd ./enterprise/coderd -run TestPlugin -v: 15 PASS, 0 FAIL.
  • go build ./coderd/... ./enterprise/...: ok. golangci-lint run --fix=false on the touched packages: 0 issues. gofmt -l: clean.

New test TestManagerRenewalReadsNoRows: a live runner (its first query blocks until it stops) is renewed over 4 ticks with 0 GetPluginByID calls and 1 full row in total (the first acquire). Then a changed updated_at gives exactly 1 GetPluginByID and a restart with the new config, and the next tick reads nothing.

Negative controls

  • Always read the row on renewal (drop the updated_at shortcut): TestManagerRenewalReadsNoRows fails, "Should be zero, but was 3".
  • Never read on a changed updated_at: the same test fails, the restart never comes (TryReceive: context expired).
  • manager.go from main: TestManagerRenewalReadsNoRows fails (no renewals).
  • Old AcquirePluginLeases clause (includes own rows): TestPluginLeases fails, "acquire skips the holder's own lease".
  • Drop NOT c.archived from GetPluginChatBindingsDue: TestPluginChatBindings fails, "an archived chat is not delivered".

Not verified

  • Not run on a multi-replica deployment. The lease handover between replicas is covered by the SQL test and the existing unit tests only.
  • No measurement of statement or decrypt counts on a live server.

RenewPluginLeases renews this replica's leases and returns only id and
updated_at, so a renewal reads and decrypts no plugin config. The
manager reads a full row with GetPluginByID only when updated_at
changed or no runner exists. AcquirePluginLeases now takes only plugins
no replica holds, up to the free slots.

Bindings of archived chats are kept on purpose: deleting them loses the
cursor and the delivered mark, so an unarchived chat would replay its
history or move to another link. A test pins that archived chats are
not due and keep both.

Part of #305.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T02:13:50.150366Z 5a2b9d7 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: da2035e83d

ℹ️ 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".

@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged main (2523de1, #357) into this branch. No conflicts. Focused tests pass again under the lock.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 5a2b9d72a8

ℹ️ 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".

@ibetitsmike
ibetitsmike merged commit 29781c8 into main Nov 16, 2026
9 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-305-lease-renewal branch November 16, 2026 02:55
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged at head 5a2b9d72a878e8dd7946ad3e0fa863e586cd672c as 29781c8, on main 2523de10674bd4d4e48253db92224ff64d5ff07f, with the server running that main.

Gates on this exact head:

  1. The head matched the SHA I verified (--match-head-commit).
  2. Every check run passed. fork-ci run 43790623281 passed on this head.
  3. Codex receipt for this head: perf(coderd/x/plugins): renew plugin leases without reading full rows #360 (comment) ("Didn't find any major issues").
  4. No unresolved review thread.
  5. 0 commits behind main.
  6. The PR commit da2035e83d is unchanged since I tested it (git range-diff shows it identical). The diff of each of the 11 files between the tested head and this head equals main's own delta in that range, so the head only adds a main merge.

Tests I ran on the PR commit at da2035e83d (detached worktree, under flock ~/.fork-go.lock, Postgres 16 at 127.0.0.1:5432):

  • go test -count=1 -v -race ./coderd/x/plugins/...: 44 PASS, 0 FAIL.
  • go test -count=1 -v -run TestPlugin ./coderd/database/forkdb/: 7 PASS, 0 FAIL.
  • go test -count=1 -v -run TestForkQuerier ./coderd/database/dbauthz/: 835 PASS, 0 FAIL.
  • go test -count=1 -v -run TestPlugin ./coderd/: 11 PASS, 0 FAIL.
  • go test -count=1 -v ./enterprise/dbcrypt/: 197 PASS, 0 FAIL, 1 SKIP.
  • Negative control: with the renewal forced to read every row (false && in the unchanged-plugin check in leases), TestManagerRenewalReadsNoRows fails; the other 6 manager tests pass. The source was restored after.

Decision: bindings of archived chats are kept, not deleted. Deleting one loses its cursor and delivered state, so an unarchived chat would replay its history or move to another link. The due query already skips archived chats, so the cost is one small row per archived chat.

Not exercised: a multi-replica deployment, and live statement or decrypt counts on the deployed server. #305 stays open for #361 (restart backoff).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant