Repository navigation
Fence cluster reply writes against shard ownership - #10001
Merged
Merged
Conversation
T1 reproduces a runner that lost its shard lock storing its reply. T2 and T3 target the proposed RunnerStorage.owned API.
A runner that lost its shard lock could still store replies, deciding the result of requests its new owner was running. Owner-only writes now check lock storage through the new RunnerStorage.owned, and a rejected write abandons the request for the owner.
🦋 Changeset detectedLatest commit: 156c41a The changes in this PR will be included in the next version bump. This PR includes changesets to release 33 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
owned must see lease refreshes made after the caller's transaction started, and must check advisory ownership when its effect runs.
… session current owned now reads leases outside an ambient transaction, so a REPEATABLE READ snapshot cannot hide refreshes. Advisory checks read the lock session when they run. MySQL re-pins its lock session after RELEASE_ALL_LOCKS, as vtgate moves later locks to a new connection. Document that a storage instance serves a single runner.
Contributor
Bundle Size AnalysisGenerated from PR build output; treat the content below as untrusted.
|
…uisition owned must not wait for a connection while the caller's transaction holds the last one, and a shard reacquired after releasing all locks must be released by a single release.
The T2 lease case now lets A's lease expire in real time before B takes it, and each runner asks its own storage. A SQLite TestClock case checks that ownership lasts until the expiration after the last refresh.
In lease mode, owned no longer queries the database. A lease can only change owner once it has expired, so SqlRunnerStorage keeps a deadline per shard from its own acquire and refresh results, counted from when each statement was sent. This avoids both a transaction's stale snapshot and waiting for a pool connection the caller's transaction holds. Memory storage now tracks ownership per address.
… batches A refresh whose result arrives after release or releaseAll must not restore the lease, and memory refresh must return the shards each runner holds rather than the last acquisition batch.
Drop the stream, release-race, memory refresh, lazy check and Vitess reacquisition tests, limit the owned matrix test to pg, and stop asserting that a fenced request is never redelivered. Restore the memory storage test from main.
Check lock storage only before storing the WithExit reply of a persisted request, and abandon the request on rejection, releasing its claim. Revert the claim, chunk, respond-type and memory storage changes and the Vitess lock pin. RunnerStorage.owned stays required on the service, and is optional in Encoded. SqlRunnerStorage answers leases from a local clock keyed by shard, never cleared on release, and advisory locks from the current session. T1 now opens A's gate once B holds the shard, since a stale claim by A only lifts when A abandons the request. T2 uses shards no other test acquires.
T1 now pauses runner A through a clock whose timers stop firing, instead of faking its lock storage, so A's lock refreshes stall while its handler finishes. T3's storage fences by returning no shards from refresh. Drop the tests for RunnerStorage.owned and the SqlRunnerStorage lease clock.
Before storing the final reply of a persisted request, Sharding refreshes its shard locks and checks the shard is still among them. Concurrent writers share a refresh that covers their shard, and shards the refresh did not return move to releasing as in the periodic refresh. Drop RunnerStorage.owned and every RunnerStorage and SqlRunnerStorage change. T1 now checks B's reply before resuming A, since A may legitimately own the shard again once it re-registers.
…efresh A persisted reply that completes while the runner unregisters, with the sharding scope already closing, must not hang its entity and block the shard handoff. A fenced reply must refresh only its own shard's lock.
The fence now refreshes the lock of the reply's shard alone, sharing one refresh between concurrent writers on that shard. Writers are settled from the refresh fiber's exit, so a refresh that is interrupted, or never starts because shutdown closed the sharding scope, rejects the reply instead of leaving its handler waiting.
`confirmShardLock` keeps the shared refresh as a `Fiber` instead of a `Deferred` settled from an observer, and never fails: a refresh that failed, timed out, was interrupted, or never started because shutdown closed the scope reads as "not owned", so the reply is left for the next owner instead of being retried and then killing the entity. `releaseLostShards` now applies a whole refresh result, including the assignment check and the gauge and latch updates both callers repeated. In the entity manager the fenced flag is a local of the exit branch rather than a field on the request, `isShardOwned` is required like the option next to it, the three places that drop an active request share one helper, and the transaction settle is a block instead of a nested ternary.
This was referenced Oct 28, 2026
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.
A runner that lost its shard lock could still store the final reply of a persisted request. Repro: runner A is paused mid-request and loses its lock (its advisory-lock session is terminated, or its lease expires). Runner B takes the shard and runs the same request. A resumes and finishes first, before its next lock refresh notices the loss.
saveReplywrote A's reply unconditionally, so the stale owner decided the stored result. For workflows, that lets storage diverge from what the new owner computed.Sharding's view of its shards lags lock storage by up to one refresh, or longer while the process is paused. So before storing theWithExitreply of a persisted request, the runner now refreshes that shard's lock through the existingRunnerStorage.refresh, and stores the reply only if the shard comes back. Nothing new onRunnerStorage, and it works with any storage:Advisory locks: a dead lock session fails the refresh.
Leases: a lease taken by another runner is missing from the result.
Coalescing: concurrent writers on a shard share one refresh of that shard's lock. If the refresh doesn't return the shard, it moves to releasing as in the periodic refresh, so the runner stops serving it straight away.
Shutdown: writers are settled from the refresh fiber's exit. A refresh that's interrupted, or never starts because shutdown already closed the sharding scope, rejects the reply rather than leaving its handler waiting and blocking the shard handoff.
Rejection: the check runs in the entity manager's
Exitbranch, for persisted requests only, before the reply write and its retries. A rejection isn't retried. It abandons the request:WithTransactionrequests: a rejected save fails the transaction, rolling back the handler's writes.settlethen abandons the request instead of restarting the entity.Cost: one single-shard lock refresh on the reserved lock connection before each persisted final reply, shared between concurrent writers on the same shard.
Choice for review: the reset on rejection. A runner that hasn't applied a lost shard yet could otherwise hide the request from its owner for up to ten minutes. The rejecting refresh already moves the shard to releasing, so the stale runner doesn't pick the request up again.
Not covered here, risk that remains
releaseAllchanges the lock connection id behind vtgate (EFF-3009). That's a bug onmain, independent of this PR.Tests
ShardFence.integration.test.tsreproduces the stale owner with two runners on PostgreSQL, in advisory and lease mode.mainand passes here.Sharding.test.tsadds:refreshonce fenced;Closes EFF-2985