Skip to content

fix(replicator): bound LeaseGrant on the peer, batch syncFull puts-before-deletes, derive the handoff test window from the lease TTL (#772) - #778

Merged
bpalermo merged 4 commits into
mainfrom
fix/772-a7-replicator-flake
Sep 15, 2026
Merged

bpalermo merged 4 commits into
mainfrom
fix/772-a7-replicator-flake

Conversation

@bpalermo

Copy link
Copy Markdown
Owner

Phase A7 of #772. Refs #597 (kept open: it is about the e2e harness's data-path withdrawal, which this does not measure) and #599.

fix(registrar): bound the origin-heartbeat lease grant on a peer (#772) (#597)

The replicator's per-peer mirror loop could park forever on its very first
RPC. startLease grants the origin-heartbeat lease with the replicator's
long-lived context, and that is the one call in the loop that cannot use a
lease-scoped context, because the lease does not exist yet. Every other peer
RPC runs on connCtx, which the keepalive watchdog cancels when the lease
dies — the grant has no such rescue.

clientv3 does not bound it either, and the field name suggested otherwise:

  • Config.DialTimeout does NOT bound connection establishment. Client
    creation has been non-blocking since etcd #21832 (client/[email protected]
    config.go:36-44 says so outright), so clientv3.New returns before a
    byte has crossed the wire.
  • grpc.WaitForReady(true) is a default call option (options.go:29,62),
    so an RPC to a peer that accepts the connection and then says nothing
    waits rather than failing fast.

Net effect against an unresponsive peer: LeaseGrant blocks, runPeer
never comes back around, aether_replicator_mirror_connected stays false
for that peer and nothing retries until the registrar is restarted.

The fix gives the grant — and only the grant — a deadline, reusing
DialTimeout as the peer-connect budget it was always meant to be. The
KeepAlive stream deliberately keeps the unbounded context: it must live as
long as the connection, and its loss is the signal that redials.

DialTimeout is left at its 5s default in production (registrar/internal/ cmd/root.go:343 sets none of the tuning fields). Lowering it, as was
originally proposed, would be a regression rather than a hardening: clientv3
derives the first keepalive's deadline from it as DialTimeout+1s
(lease.go:193), so a 500ms dial timeout would also cut the first-keepalive
grace from 6s to 1.5s and make spurious "keepalive lost" redials MORE likely
under load. That is now written down on the field.

TestReplicator_RetriesAgainstUnresponsivePeer pins it, and needs no etcd:
a listener that accepts and never speaks gRPC, with the retry observed as
repeated connections. Verified as a real discriminator — with the deadline
removed it fails in 10s with one connection and the client logging
"context canceled while waiting for connections to become ready".

fix(registrar): re-attach the mirror before pruning it, in bounded txns (#772) (#597)

syncFull is where a registrar leader handoff actually happens. The
successor grants a fresh origin-heartbeat lease and every mirrored key is
value-equal but still attached to the PREDECESSOR's lease, so the re-put at
replicator.go:268 is the lease hop — and the mirror only survives if it
completes inside whatever the predecessor's lease has left.

Two things spent that budget badly:

  • Stale-key pruning ran FIRST, one Delete round trip per key, ahead of
    the puts that actually rescue the mirror. Pruning is cleanup; it can
    wait behind the hop.
  • Both loops issued one round trip per key, so a partial failure left
    some keys migrated to the live lease and the rest riding the dying one
    out — a mirror observed half-moved, which reads downstream as a hop
    that "happened late".

Now the sync builds the put and delete sets first, commits the puts, then
the deletes, each as transactions of at most 128 ops. A batch lands
atomically, and the whole set costs one round trip per batch instead of one
per key, which is the term that grows with the size of the region's
partition. Batched rather than one transaction because the peer enforces
--max-txn-ops (128 by default) while an own-partition subtree is
unbounded: a single transaction would simply fail at scale.

Put keys are sorted, so a given input always batches the same way. The sync
is observable on the peer one batch at a time, and map iteration order would
otherwise move that boundary run to run (same class as #135).

Behaviour is unchanged otherwise: the put and delete sets are disjoint by
construction (a key is either absent locally, hence deleted, or present,
hence a put candidate), so reordering them is safe.

test(registrar): derive the lease-handoff window from the measured TTL (#772) (#597)

TestReplicator_LeaseHandoffKeepsMirrorAlive deliberately watches THROUGH
the predecessor lease's expiry, so its entire safety margin is that lease's
remaining TTL minus the successor's time-to-re-attach. It picked that window
as a flat 14s wall-clock from stop(), which assumed a lease phase the test
does not control: clientv3 refreshes a lease at TTL/3 (client/v3 lease.go:542),
so at stop() a nominally-10s lease holds anywhere between 6.67s and 10s.
The test was therefore asserting against a margin that varied by ~50% run to
run, and whether it even reached the expiry it claims to test was luck.

It now reads the predecessor's remaining TTL off the peer with TimeToLive
at the moment of the handoff and watches until that measured expiry plus 3s.
No timeout was lengthened; the window is the same size, just derived instead
of guessed.

Two assertions added while the lease is in hand, both of which the old shape
could not make:

  • the key must end up on a DIFFERENT lease than the predecessor's — the
    property the test's own comment claims ("if the sync skipped it on value
    equality alone…") but never actually checked. Surviving the window on
    the predecessor's lease used to pass.
  • the predecessor's lease must be gone (TTL == -1) once the loop ends,
    so the test cannot silently degrade into one that stops short of the
    expiry.

No production code changes here.

Flake rate: unchanged at zero locally, 20/20 before and after
(--runs_per_test=20, 23-28s per run), plus 1/1 under --config=race. The
#597 failures are CI-only and this workstation does not reproduce them, so
the 20-run bar confirms no regression rather than a fixed rate; what the
change removes is the uncontrolled variable that made the margin unknowable
when it does fail.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

bpalermo and others added 4 commits September 15, 2026 19:30
… (#597)

The replicator's per-peer mirror loop could park forever on its very first
RPC. `startLease` grants the origin-heartbeat lease with the replicator's
long-lived context, and that is the one call in the loop that cannot use a
lease-scoped context, because the lease does not exist yet. Every other peer
RPC runs on `connCtx`, which the keepalive watchdog cancels when the lease
dies — the grant has no such rescue.

clientv3 does not bound it either, and the field name suggested otherwise:

  * `Config.DialTimeout` does NOT bound connection establishment. Client
    creation has been non-blocking since etcd #21832 (client/[email protected]
    config.go:36-44 says so outright), so `clientv3.New` returns before a
    byte has crossed the wire.
  * `grpc.WaitForReady(true)` is a default call option (options.go:29,62),
    so an RPC to a peer that accepts the connection and then says nothing
    waits rather than failing fast.

Net effect against an unresponsive peer: `LeaseGrant` blocks, `runPeer`
never comes back around, `aether_replicator_mirror_connected` stays false
for that peer and nothing retries until the registrar is restarted.

The fix gives the grant — and only the grant — a deadline, reusing
`DialTimeout` as the peer-connect budget it was always meant to be. The
`KeepAlive` stream deliberately keeps the unbounded context: it must live as
long as the connection, and its loss is the signal that redials.

`DialTimeout` is left at its 5s default in production (`registrar/internal/
cmd/root.go:343` sets none of the tuning fields). Lowering it, as was
originally proposed, would be a regression rather than a hardening: clientv3
derives the first keepalive's deadline from it as `DialTimeout+1s`
(lease.go:193), so a 500ms dial timeout would also cut the first-keepalive
grace from 6s to 1.5s and make spurious "keepalive lost" redials MORE likely
under load. That is now written down on the field.

`TestReplicator_RetriesAgainstUnresponsivePeer` pins it, and needs no etcd:
a listener that accepts and never speaks gRPC, with the retry observed as
repeated connections. Verified as a real discriminator — with the deadline
removed it fails in 10s with one connection and the client logging
"context canceled while waiting for connections to become ready".

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
…ns (#772) (#597)

`syncFull` is where a registrar leader handoff actually happens. The
successor grants a fresh origin-heartbeat lease and every mirrored key is
value-equal but still attached to the PREDECESSOR's lease, so the re-put at
replicator.go:268 is the lease hop — and the mirror only survives if it
completes inside whatever the predecessor's lease has left.

Two things spent that budget badly:

  * Stale-key pruning ran FIRST, one `Delete` round trip per key, ahead of
    the puts that actually rescue the mirror. Pruning is cleanup; it can
    wait behind the hop.
  * Both loops issued one round trip per key, so a partial failure left
    some keys migrated to the live lease and the rest riding the dying one
    out — a mirror observed half-moved, which reads downstream as a hop
    that "happened late".

Now the sync builds the put and delete sets first, commits the puts, then
the deletes, each as transactions of at most 128 ops. A batch lands
atomically, and the whole set costs one round trip per batch instead of one
per key, which is the term that grows with the size of the region's
partition. Batched rather than one transaction because the peer enforces
`--max-txn-ops` (128 by default) while an own-partition subtree is
unbounded: a single transaction would simply fail at scale.

Put keys are sorted, so a given input always batches the same way. The sync
is observable on the peer one batch at a time, and map iteration order would
otherwise move that boundary run to run (same class as #135).

Behaviour is unchanged otherwise: the put and delete sets are disjoint by
construction (a key is either absent locally, hence deleted, or present,
hence a put candidate), so reordering them is safe.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
#772) (#597)

`TestReplicator_LeaseHandoffKeepsMirrorAlive` deliberately watches THROUGH
the predecessor lease's expiry, so its entire safety margin is that lease's
remaining TTL minus the successor's time-to-re-attach. It picked that window
as a flat 14s wall-clock from `stop()`, which assumed a lease phase the test
does not control: clientv3 refreshes a lease at TTL/3 (client/v3 lease.go:542),
so at `stop()` a nominally-10s lease holds anywhere between 6.67s and 10s.
The test was therefore asserting against a margin that varied by ~50% run to
run, and whether it even reached the expiry it claims to test was luck.

It now reads the predecessor's remaining TTL off the peer with `TimeToLive`
at the moment of the handoff and watches until that measured expiry plus 3s.
No timeout was lengthened; the window is the same size, just derived instead
of guessed.

Two assertions added while the lease is in hand, both of which the old shape
could not make:

  * the key must end up on a DIFFERENT lease than the predecessor's — the
    property the test's own comment claims ("if the sync skipped it on value
    equality alone…") but never actually checked. Surviving the window on
    the predecessor's lease used to pass.
  * the predecessor's lease must be gone (`TTL == -1`) once the loop ends,
    so the test cannot silently degrade into one that stops short of the
    expiry.

No production code changes here.

Flake rate: unchanged at zero locally, 20/20 before and after
(`--runs_per_test=20`, 23-28s per run), plus 1/1 under `--config=race`. The
#597 failures are CI-only and this workstation does not reproduce them, so
the 20-run bar confirms no regression rather than a fixed rate; what the
change removes is the uncontrolled variable that made the margin unknowable
when it does fail.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
@bpalermo
bpalermo merged commit ee93fb7 into main Sep 15, 2026
19 checks passed
@bpalermo
bpalermo deleted the fix/772-a7-replicator-flake branch September 15, 2026 22:38
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