[LocalDb] Guard against deleted-row resurrection when a peer was offline longer than TombstoneRetention #16

Closed
opened 2026-11-06 15:24:44 -05:00 by dohertj2 · 3 comments
Owner

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 (TombstoneRetention option)

**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` (`TombstoneRetention` option)
dohertj2 added the type:featurepriority:p3migrated labels 2026-11-06 15:24:44 -05:00
Author
Owner

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:

  • A (recommended) keep a tombstone until it is past retention AND the peer has synced since it was written (optional hard cap). No wire change, keeps availability; cost = ledger growth while the peer is down (matters for high-churn tables like ScadaBridge sf_messages). Fits the family's availability-over-partition-safety preference.
  • B authoritative replace-resync when the stale node's last sync is older than retention (its unsynced writes are lost; needs a 'which side is current' rule + capability flag).
  • C fail closed — refuse the session; operator rebuilds the stale node.
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: - **A (recommended)** keep a tombstone until it is past retention AND the peer has synced since it was written (optional hard cap). No wire change, keeps availability; cost = ledger growth while the peer is down (matters for high-churn tables like ScadaBridge sf_messages). Fits the family's availability-over-partition-safety preference. - **B** authoritative replace-resync when the stale node's last sync is older than retention (its unsynced writes are lost; needs a 'which side is current' rule + capability flag). - **C** fail closed — refuse the session; operator rebuilds the stale node.
Author
Owner

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.

**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.
Author
Owner

Fixed (option A) in be5e9fd on main (index eab617f). LocalDb is now 0.3.7, unpublished.

Rule (OplogStore.PruneTombstonesAsync): a tombstone is pruned only once it is past TombstoneRetention and the peer is known to hold it. It is held while:

  • database-wide snapshot debt (__localdb_peer_state.needs_snapshot) or per-table debt (__localdb_snapshot_state) is outstanding;
  • it is a local delete that is still unacked (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_utc is restamped: in RecordPeerAckAsync for tombstones in the newly acked range, and in ClearSnapshotDebtAsync for 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 optional LocalDb:Replication:TombstoneHardCap (default null = off, validated ≥ TombstoneRetention) bounds how long an undelivered tombstone is kept; past the cap, the old risk returns. No proto, lib_schema_version or on-disk schema change.

Second path (snapshot built from the ledger): the debt hold covers it. The age/row cap sets needs_snapshot in 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_DeleteStillWinsOnBothNodes is 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.
  • Positive controls:
    • Control_SameScenarioWithoutTombstonePrune_DeleteWins.
    • AfterPeerReceivesTombstone_ItIsPrunedOneRetentionLater, which shows pruning still happens after delivery.
  • Five controlled-clock tests in OplogStoreTests cover: undelivered → ack → aged from the ack; db-wide debt; per-table debt; remote-origin; hard cap. Two validator/binding tests were added in MaintenanceTests.
  • Mutation testing: 7 mutants, each killed — age-only prune; no restamp on ack; no restamp on snapshot clear; no per-table hold; no node_id refinement; no hard cap; no db-wide hold.
  • dotnet build ZB.MOM.WW.LocalDb.slnx: 0 warnings. Tests: 259/259 (was 249).

Cost / not covered (documented in README Ops notes):

  • While the peer is down, every delete's tombstone stays in the ledger (relevant for high-churn tables like sf_messages).
  • A peer restored from an old backup of its own file can still reintroduce stale rows. Rebuild such a peer empty.
Fixed (option A) in `be5e9fd` on `main` (index `eab617f`). LocalDb is now `0.3.7`, **unpublished**. **Rule** (`OplogStore.PruneTombstonesAsync`): a tombstone is pruned only once it is past `TombstoneRetention` **and** the peer is known to hold it. It is held while: - database-wide snapshot debt (`__localdb_peer_state.needs_snapshot`) or per-table debt (`__localdb_snapshot_state`) is outstanding; - it is a **local** delete that is still unacked (`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_utc` is restamped: in `RecordPeerAckAsync` for tombstones in the newly acked range, and in `ClearSnapshotDebtAsync` for 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 optional `LocalDb:Replication:TombstoneHardCap` (default `null` = off, validated ≥ `TombstoneRetention`) bounds how long an undelivered tombstone is kept; past the cap, the old risk returns. No proto, `lib_schema_version` or on-disk schema change. **Second path (snapshot built from the ledger):** the debt hold covers it. The age/row cap sets `needs_snapshot` in 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_DeleteStillWinsOnBothNodes` is 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. - Positive controls: - `Control_SameScenarioWithoutTombstonePrune_DeleteWins`. - `AfterPeerReceivesTombstone_ItIsPrunedOneRetentionLater`, which shows pruning still happens after delivery. - Five controlled-clock tests in `OplogStoreTests` cover: undelivered → ack → aged from the ack; db-wide debt; per-table debt; remote-origin; hard cap. Two validator/binding tests were added in `MaintenanceTests`. - Mutation testing: 7 mutants, each killed — age-only prune; no restamp on ack; no restamp on snapshot clear; no per-table hold; no node_id refinement; no hard cap; no db-wide hold. - `dotnet build ZB.MOM.WW.LocalDb.slnx`: 0 warnings. Tests: 259/259 (was 249). **Cost / not covered (documented in README Ops notes):** - While the peer is down, every delete's tombstone stays in the ledger (relevant for high-churn tables like `sf_messages`). - A peer **restored from an old backup** of its own file can still reintroduce stale rows. Rebuild such a peer empty.
Sign in to join this conversation.