Repository navigation
Keep a cluster node's lease on a thread of its own - #8631
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #8631 +/- ##
==========================================
- Coverage 67.13% 67.04% -0.09%
==========================================
Files 822 820 -2
Lines 212065 211936 -129
Branches 32461 32379 -82
==========================================
- Hits 142362 142103 -259
- Misses 50673 50852 +179
+ Partials 19030 18981 -49 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
18fda85 to
98b6d48
Compare
There was a problem hiding this comment.
Superseded by the review of
ce76f072.
This review, of 98b6d481
💬 Approved with comments — The lease-thread design is sound and well tested. Two INFO notes on the fence path: a fence with little lease left exits without logging why, and the process is not fenced if the lease thread dies outside a spawned task.
Findings: 2 info (2 are inline comments on the diff)
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📋 Code Quality: 💬 2 info
The commands, the shared expiry atomic and the fence path fit together correctly. Two gaps remain in the fence path.
Recommendation: 💬 Optional Nits
- [INFO] A fenced node exits without logging any reason when its lease has 20 ms or less left. (
src/workerd/server/server/cluster.rs:443) - [INFO] Nothing fences the process if the lease thread ends outside a spawned task. (
src/workerd/server/cluster/keeper.rs:333)
View full review
The keeper thread, its commands, the Held atomic and the armed-exit fence fit together correctly. Two smaller gaps remain in the fence path:
- Silent exit.
fence_log_waitreturnsNonewhen the lease hasFENCE_SLACK(20 ms) or less left. The fence closure then callscli_exit(1)without logging anything. A lease that lapsed before it could be renewed is the case most likely to reach this branch, and it is the one an operator most needs explained. On main,leaving the cluster: …was always logged. - Lease thread death. If the lease thread dies outside a spawned task (in
serve, the runtime, orwithin), no fence runs.Thread::senddrops its error, solease_holds()only blocks opening new storage, while Durable Objects that are already running keep serving after peers may take them over.
✅ All other checks passed
🔒 Security: ✅ LGTM
The change adds no injection, authentication bypass, secret exposure or data-exposure problem.
📖 AGENTS.md: ✅ LGTM
No issues found.
⚡ Performance: ✅ LGTM
The change adds one thread, one current-thread tokio runtime and one bucket client per node. All blocking waits are bounded, and the renewal and listing cadences are unchanged.
📝 Documentation: ✅ LGTM
No issues found.
Reviewed commit: 98b6d481
98b6d48 to
ce76f07
Compare
There was a problem hiding this comment.
Superseded by the review of
1728f154.
This review, of ce76f072
✅ Approved — Both earlier fence-path notes are fixed at this commit and no new issues were found.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
The change adds no injection, authentication, secret-exposure or data-exposure problem.
📖 AGENTS.md: ✅ LGTM
No issues found.
📋 Code Quality: ✅ LGTM
The earlier notes on the silent exit and on the lease thread ending outside a task are both fixed:
- Silent exit:
server/cluster.rsnow callsfatal_nonblockingbeforecli_exit(1). - Lease thread ending: the
Watchdrop guard inkeeper.rsfences the process.keeper-test.rstests a closed command channel, a panic outside a task, an end before the rounds start, an end after leaving, and fencing only once.
⚡ Performance: ✅ LGTM
- The change adds one thread, one current-thread tokio runtime and one bucket client per node.
lease_holdsis now one atomic load.- The teardown wait and the fence wait are both bounded.
📝 Documentation: ✅ LGTM
No issues found.
Reviewed commit: ce76f072
ry
left a comment
There was a problem hiding this comment.
[AI] Approving. This matches celld, whose ownership actor keeps the node lease on its own current_thread runtime, and the exit is stricter than celld's: celld hands the fence to its main runtime, which flushes logs and calls exit, while this calls _exit from the lease thread with the log wait bounded. Needs a rebase.
ce76f07 to
1728f15
Compare
There was a problem hiding this comment.
Superseded by the review of
088fe065.
This review, of 1728f154
✅ Approved — No issues in the code changed since the last review, and the earlier fence-path fixes are still in place.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
No security issues in the changed code.
📖 AGENTS.md: ✅ LGTM
No issues found.
📋 Code Quality: ✅ LGTM
run.rs drops the renewal before serving and the event-loop exit task. The lease thread now handles both. The new cluster-test.mjs tests have time limits and clean up nodes that do not exit.
⚡ Performance: ✅ LGTM
start_cluster no longer waits for a renewal before serving, so startup does less work. The new tests poll with time limits.
📝 Documentation: ✅ LGTM
No issues found.
Reviewed commit: 1728f154
A cluster node renewed its lease from a task on the server's event loop, so a renewal waited for whatever turn the loop was in. One turn longer than the lease's slack (a Worker that computes synchronously, a slow SQLite query, an fsync or a directory removal on a slow disk) fenced the node, which then dropped every object it owned. The lease is now kept by a `cluster-lease` thread with its own tokio runtime and its own bucket client (`cluster/keeper.rs`): * The thread writes the node's record and runs the renewal rounds. It publishes the lease's expiry through an atomic, which the loop reads where it used to read the lease (`Cluster::lease_holds`). * The loop sends it commands for what it used to do itself: the renewal at join, the drain announcement and the deregistration. Each runs as a task beside the rounds. * The rounds start at join, not once the node serves, so the lease holds however long linking takes: it opens every namespace's storage, which opens only under the lease, and on a slow disk took longer than one. The renewal before serving that made up for that is gone, and storage need not open without a lease until the node serves, as #8564 has it do. * A lease that cannot be kept ends the process from the lease thread, with `_exit`, since the loop may be in a turn that does not end. The exit is armed on a thread of its own before the reason is logged, so that a log write that blocks cannot delay it, for 200 ms or until shortly before the lease stops holding, whichever comes first. With no time left, the reason goes to stderr in one write that does not block (none on Windows) before the process exits. Under structured logging the reason also goes to stderr, where a supervisor looks for why a process ended, as it does for a failed start. * A lease thread that ends without being told to stop, by a panic outside its tasks or otherwise, fences the node as rounds that end do, unless its rounds have not started or the node left the cluster. The fence runs once, whichever comes first. * The thread logs through the loop thread's logger, which it installs as KJ does for a thread of its own (`getThreadInitializer`, through the server's bridge): under structured logging its lines are JSON. * Dropping the cluster stops the thread, cancelling its requests, and waits a second at most for it to end, leaving it behind after that. * The peer listing stays on the loop, in a loop of its own (`Cluster::list_loop`): a late listing only delays routing to a new peer. It no longer sits between two renewals. This changes what the lease attests: that the process runs and reaches the bucket, not that its event loop takes turns. A node whose loop is stuck for good keeps its lease and its objects until something else stops it; before, peers took its objects over after a lease. A bucket that fails after join, while the node links, now fences it like a serving node (it logs why and exits) instead of failing the renewal before serving with an error. A bucket that fails at join still fails startup with an error. Tests of the crate that drive a `Cluster` over their own bucket keep the lease on their own thread and renew by hand, as before. The lease thread's own tests use leases of a few hundred ms.
1728f15 to
088fe06
Compare
There was a problem hiding this comment.
✅ Approved — No findings in the code changed since the last review. The lease-thread design and the earlier fence-path fixes are still in place.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
The change is internal lease handling, and its log writes go only to stderr and stdout. No security issues found.
📖 AGENTS.md: ✅ LGTM
No issues found.
📋 Code Quality: ✅ LGTM
No issues found in the changed code.
⚡ Performance: ✅ LGTM
- Lease thread: it runs a single-thread runtime, and its commands are low-volume control messages.
- Shutdown: the drop wait for the lease thread is capped at 1 s and runs only at shutdown.
- Reads on the event loop:
lease_holdsandmembershipnow read atomics instead of aRefCell. - Extra bucket client: the lease thread opens a third bucket client per node, a one-time cost.
- Listing:
list_loopkeeps the same request cadence as before.
📝 Documentation: ✅ LGTM
No issues found.
Reviewed commit: 088fe065
This PR moves cluster lease renewal to its own thread so work on the
server event loop cannot delay renewal and fence an otherwise healthy
node. It is a cluster-only alternative to #8564 + #8594 for the lease
problem: instead of shortening event-loop turns and changing how tokio
is scheduled, it takes renewal off that loop entirely.
Background
In cluster mode, several nodes share a bucket such as S3, R2, or GCS.
Each Durable Object has one owner node. Every node regularly extends
the expiry time in its bucket record; that expiry time is its lease.
Peers consider a node live while its lease holds. If the node cannot
renew, it must stop, or "fence", before expiry so that two nodes cannot
run the same Durable Object.
On main, renewal runs on the server's KJ event loop, with a tokio
runtime on the same thread. While one KJ turn is running, such as
synchronous JavaScript, a slow SQLite query, or an fsync on a slow
disk, nothing else on that thread runs. Tokio only runs when the KJ
queue is empty. A busy or blocked event loop can therefore delay
renewal and fence a healthy node, dropping every Durable Object it
owned. This was the main remaining cause of cluster-test CI flakes.
What changes
cluster-leasethread has its own tokio runtime and bucketclient in
cluster/keeper.rs. It writes the node's record, runsrenewal rounds, and publishes the lease expiry through an atomic.
The event loop reads that atomic in
Cluster::lease_holds.itself: renewal at join, the drain announcement, and deregistration.
Each command runs as a task beside the renewal rounds.
This keeps the lease alive throughout startup linking, which opens
every namespace's storage under the lease and can take longer than a
lease on a slow disk. The renewal before serving is removed. This
covers the startup failure addressed differently by Keep a cluster node's storage work from starving its lease #8564's first
commit, which opens storage without a lease until the node serves.
_exit, because the event loop may be in a turn that never ends.Before logging the reason, it arms the exit on another thread, which
ends the process after at most 200 ms or the remaining lease time
minus slack, whichever is less. A blocked log write therefore cannot
keep the node serving past its lease. Under structured
logging, the reason also goes to stderr as JSON, where supervisors
read startup failures. The exit status remains 1, as it was when
main lost a lease.
output is JSON when structured logging is enabled.
It waits at most one second for the thread to finish, then leaves it
behind.
Cluster::list_loop. A late listing now only delays routing to a newpeer; it no longer sits between two renewals.
now logs the reason and fences instead of returning a startup error.
A bucket failure at join still returns a startup error.
The trade-off
The lease now proves that the process is running and can reach the
bucket, not that its event loop is taking turns. If an event loop is
wedged permanently, the node keeps its lease and its Durable Objects
until something else stops it. On main, peers took over those objects
after one lease.
A loop watchdog with a bound well above the lease would restore that
protection. The watchdog is follow-up work and is not part of this PR.
Tests
keeper-test.rsverifies that the lease thread renews independently,fences with time remaining when the bucket refuses writes, and waits
for the thread to end when dropped. Its leases last a few hundred
milliseconds, so the tests add under one second.
cluster-test.rsverifies that the lease remains valid however longstartup takes after join. It fails if renewal rounds do not begin at
join.
lease-test.rsand the server cluster tests cover the remaining-timecalculation and bounded wait for logging.
cluster-test.mjsverifies that a node continues serving after onerequest runs JavaScript for longer than a lease, which fences the
node on main. It also checks that lease-thread warnings are JSON
under structured logging, and that total renewal failure produces a
JSON "leaving the cluster" line on stderr and exit code 1.
each node.
Clusterover their own bucket continue tokeep the lease on their own thread and renew it manually.
Results
In #8564's stress test, 64 Durable Objects are opened at once on one
node and the test checks that the node retains its lease. Each version
ran 500 times on bazelflare.
None of this PR's 500 logs contained a failed renewal.
Cluster targets pass 40 runs on bazelflare, with 200 runs of
cluster-test.//src/workerd/server/...also passes.The cluster crate's
cluster_testunit tests take about 40 seconds indebug and ASan builds; the new lease-thread tests add under one second.