[LocalDb] Guard against deleted-row resurrection when a peer was offline longer than TombstoneRetention #16
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What: A node that is offline longer than
TombstoneRetention(default 7 days) can resurrect rows deleted on its peer when it rejoins — currently documented only as an operational constraint ("a pair stops/starts together; a node offline > TombstoneRetention can resurrect deleted rows"). Consider a library guard: on reconnect, if the peer's watermark predates the oldest retained tombstone / pruned oplog horizon, force a snapshot full-resync from the up-to-date side (or refuse to push the stale node's rows) instead of LWW-merging.Verify first: confirm the failure mode with a test (offline peer, delete + prune on the other side, reconnect) — the snapshot-resync path added in 0.3.x may already cover part of it. If it is real, it is a correctness issue for any consumer that enables replication (ScadaBridge site pairs, OtOpcUa QA pair).
Source:
ZB.MOM.WW.LocalDb/CLAUDE.md(component row, operational constraints);CLAUDE.md(LocalDb row);ZB.MOM.WW.LocalDb/README.md(TombstoneRetentionoption)Not implemented — confirmed real, needs an owner decision. Two-node repro over the real sync session (not committed): A,B sync row 1 → B updates, A deletes (newer) → advance A's clock 8 days so oplog age cap + tombstone prune run → reconnect ⇒ row resurrected on both (
A=1=b-old B=1=b-old); control with tombstone prune skipped ⇒ deleted on both. Second path: snapshot (full resync) is built from the row-version ledger, so it can't carry a pruned tombstone either.Options:
Owner decision 2026-11-09: option A — keep a tombstone until it is past retention AND the peer has synced since it was written (optional hard cap). No wire change.
Fixed (option A) in
be5e9fdonmain(indexeab617f). LocalDb is now0.3.7, unpublished.Rule (
OplogStore.PruneTombstonesAsync): a tombstone is pruned only once it is pastTombstoneRetentionand the peer is known to hold it. It is held while:__localdb_peer_state.needs_snapshot) or per-table debt (__localdb_snapshot_state) is outstanding;hlc >=the first unacked oplog row's HLC; the ledger and oplog share the HLC, and local HLCs rise with seq).When the tombstone is delivered,
tombstone_utcis restamped: inRecordPeerAckAsyncfor tombstones in the newly acked range, and inClearSnapshotDebtAsyncfor the snapshot's scope. Retention therefore runs from delivery, so the returning peer's stale queued writes get a full retention to drain. Remote-origin tombstones age on retention alone. The new optionalLocalDb:Replication:TombstoneHardCap(defaultnull= off, validated ≥TombstoneRetention) bounds how long an undelivered tombstone is kept; past the cap, the old risk returns. No proto,lib_schema_versionor on-disk schema change.Second path (snapshot built from the ledger): the debt hold covers it. The age/row cap sets
needs_snapshotin the same transaction that drops undelivered oplog rows, so the ledger tombstone the snapshot is built from survives until that snapshot is acknowledged. The new regression test exercises exactly this path: A's delete is age-capped out of the oplog and the tombstone reaches B via the snapshot.Evidence
Convergence/TombstoneResurrectionTests.PeerOfflinePastRetention_DeleteStillWinsOnBothNodesis the issue's repro, committed. It uses two real nodes over loopback gRPC: B updates offline, A deletes later, then A's clock is advanced 8 days and a caps + tombstone prune runs before reconnecting. On 0.3.6 code it fails: A's tombstone is pruned, and with that assertion removed the row is resurrected (Expected 0, Actual 1). With the fix it passes, with the row deleted on both nodes and B holding the tombstone.Control_SameScenarioWithoutTombstonePrune_DeleteWins.AfterPeerReceivesTombstone_ItIsPrunedOneRetentionLater, which shows pruning still happens after delivery.OplogStoreTestscover: undelivered → ack → aged from the ack; db-wide debt; per-table debt; remote-origin; hard cap. Two validator/binding tests were added inMaintenanceTests.dotnet build ZB.MOM.WW.LocalDb.slnx: 0 warnings. Tests: 259/259 (was 249).Cost / not covered (documented in README Ops notes):
sf_messages).