fix(localdb): keep a tombstone until the peer holds it — no resurrection past TombstoneRetention (scadaproj#16)
0.3.7 (unpublished), owner-chosen option A. A tombstone is now pruned only when it is past TombstoneRetention AND the peer is known to hold it: - held while database-wide or per-table snapshot debt is outstanding (the snapshot is built from the row-version ledger, so a pruned tombstone is a delete it can no longer carry — the issue's second path); - a local tombstone held while its delete is still in the unacked oplog. Delivery (delta ack covering the delete, or snapshot debt discharged) restamps tombstone_utc so retention runs from delivery. New optional LocalDb:Replication:TombstoneHardCap (default off, >= TombstoneRetention) bounds an undelivered tombstone's life. No proto, lib_schema_version or on-disk schema change. Regression test TombstoneResurrectionTests (two real nodes over loopback gRPC): offline update on B, newer delete on A, A's clock +8 days (oplog age cap + tombstone prune) -> reconnect. Fails on 0.3.6 (row resurrected), passes now; plus a no-prune positive control and a pruned-after-delivery control, and five controlled-clock OplogStore tests. Each guard clause mutation-killed. 259/259. Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
@@ -122,6 +122,113 @@ public sealed class OplogStoreTests : IDisposable
|
||||
Assert.True(peer.NeedsSnapshot);
|
||||
}
|
||||
|
||||
// ---- scadaproj#16: a tombstone is pruned only once the peer holds it --------------------
|
||||
|
||||
private static async Task<long> TombstoneCount(SqliteLocalDb db) =>
|
||||
(await db.QueryAsync(
|
||||
"SELECT COUNT(*) FROM __localdb_row_version WHERE is_tombstone = 1", static r => r.GetInt64(0)))[0];
|
||||
|
||||
[Fact]
|
||||
public async Task Prune_KeepsUndeliveredLocalTombstonePastRetention_ThenAgesItFromDelivery()
|
||||
{
|
||||
using var db = await NewOrdersDb();
|
||||
await db.ExecuteAsync("INSERT INTO orders (id, sku, qty) VALUES (1, 'A', 1)");
|
||||
await db.ExecuteAsync("DELETE FROM orders WHERE id = 1");
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var store = new OplogStore(db, new ReplicationOptions(), () => now);
|
||||
|
||||
// Peer offline 8 days: past the 7-day retention, but the delete is still unacked.
|
||||
now += TimeSpan.FromDays(8);
|
||||
Assert.Equal(0, await store.PruneTombstonesAsync(default));
|
||||
Assert.Equal(1, await TombstoneCount(db));
|
||||
|
||||
// The peer comes back and acks it: retention restarts at the ack, not at the delete.
|
||||
await store.RecordPeerAckAsync(await store.GetMaxSeqAsync(), default);
|
||||
now += TimeSpan.FromDays(6);
|
||||
Assert.Equal(0, await store.PruneTombstonesAsync(default));
|
||||
Assert.Equal(1, await TombstoneCount(db));
|
||||
|
||||
now += TimeSpan.FromDays(2);
|
||||
Assert.Equal(1, await store.PruneTombstonesAsync(default));
|
||||
Assert.Equal(0, await TombstoneCount(db));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Prune_KeepsTombstonesWhileDatabaseWideSnapshotDebtIsOutstanding()
|
||||
{
|
||||
using var db = await NewOrdersDb();
|
||||
await db.ExecuteAsync("INSERT INTO orders (id, sku, qty) VALUES (1, 'A', 1)");
|
||||
await db.ExecuteAsync("DELETE FROM orders WHERE id = 1");
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var store = new OplogStore(db, new ReplicationOptions(), () => now);
|
||||
|
||||
// The age cap drops the delete from the oplog (never delivered) and flags a snapshot — the
|
||||
// snapshot is built from the ledger, so the tombstone is the only carrier of the delete.
|
||||
now += TimeSpan.FromDays(8);
|
||||
Assert.True(await store.EnforceCapsAsync(default));
|
||||
Assert.Equal(0, await store.GetMaxSeqAsync());
|
||||
Assert.Equal(0, await store.PruneTombstonesAsync(default));
|
||||
Assert.Equal(1, await TombstoneCount(db));
|
||||
|
||||
// The snapshot is acknowledged: debt cleared, tombstone restamped, aged from there.
|
||||
await store.ClearSnapshotDebtAsync([], default);
|
||||
now += TimeSpan.FromDays(6);
|
||||
Assert.Equal(0, await store.PruneTombstonesAsync(default));
|
||||
now += TimeSpan.FromDays(2);
|
||||
Assert.Equal(1, await store.PruneTombstonesAsync(default));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Prune_KeepsTombstonesOfATableWithPerTableSnapshotDebt()
|
||||
{
|
||||
using var db = await NewOrdersDb();
|
||||
await db.ExecuteAsync("INSERT INTO orders (id, sku, qty) VALUES (1, 'A', 1)");
|
||||
await db.ExecuteAsync("DELETE FROM orders WHERE id = 1");
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var store = new OplogStore(db, new ReplicationOptions(), () => now);
|
||||
await store.RecordPeerAckAsync(await store.GetMaxSeqAsync(), default);
|
||||
await db.ExecuteAsync(
|
||||
"INSERT INTO __localdb_snapshot_state (table_name, needs_snapshot) VALUES ('orders', 1)");
|
||||
|
||||
now += TimeSpan.FromDays(8);
|
||||
Assert.Equal(0, await store.PruneTombstonesAsync(default));
|
||||
|
||||
await store.ClearSnapshotDebtAsync(["orders"], default);
|
||||
now += TimeSpan.FromDays(8);
|
||||
Assert.Equal(1, await store.PruneTombstonesAsync(default));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Prune_RemoteOriginTombstone_AgesOnRetentionAlone_EvenWithLocalBacklog()
|
||||
{
|
||||
using var db = await NewOrdersDb();
|
||||
// A local write the peer has not acked — it must not hold the PEER's own delete.
|
||||
await db.ExecuteAsync("INSERT INTO orders (id, sku, qty) VALUES (9, 'LOCAL', 1)");
|
||||
await db.ExecuteAsync(
|
||||
"INSERT INTO __localdb_row_version (table_name, pk_json, hlc, node_id, is_tombstone, tombstone_utc) " +
|
||||
"VALUES ('orders', '{\"id\":1}', 9223372036854775000, 'the-peer', 1, @utc)",
|
||||
new { utc = DateTimeOffset.UtcNow.UtcDateTime.ToString(OplogStore.TombstoneUtcFormat) });
|
||||
var now = DateTimeOffset.UtcNow + TimeSpan.FromDays(8);
|
||||
var store = new OplogStore(db, new ReplicationOptions(), () => now);
|
||||
|
||||
Assert.Equal(1, await store.PruneTombstonesAsync(default));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Prune_HardCap_DropsAnUndeliveredTombstoneOnceExceeded()
|
||||
{
|
||||
using var db = await NewOrdersDb();
|
||||
await db.ExecuteAsync("INSERT INTO orders (id, sku, qty) VALUES (1, 'A', 1)");
|
||||
await db.ExecuteAsync("DELETE FROM orders WHERE id = 1");
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var store = new OplogStore(db, new ReplicationOptions { TombstoneHardCap = TimeSpan.FromDays(30) }, () => now);
|
||||
|
||||
now += TimeSpan.FromDays(29);
|
||||
Assert.Equal(0, await store.PruneTombstonesAsync(default));
|
||||
now += TimeSpan.FromDays(2);
|
||||
Assert.Equal(1, await store.PruneTombstonesAsync(default));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Prune_DropsExpiredTombstoneRowVersions_KeepsLive()
|
||||
{
|
||||
@@ -129,10 +236,13 @@ public sealed class OplogStoreTests : IDisposable
|
||||
await db.ExecuteAsync("INSERT INTO orders (id, sku, qty) VALUES (1,'A',1),(2,'B',2),(3,'C',3)");
|
||||
await db.ExecuteAsync("DELETE FROM orders WHERE id = 1"); // will be backdated -> expired
|
||||
await db.ExecuteAsync("DELETE FROM orders WHERE id = 3"); // fresh tombstone -> kept
|
||||
var store = new OplogStore(db, new ReplicationOptions());
|
||||
// The peer has both deletes (scadaproj#16: an undelivered tombstone is never pruned on age);
|
||||
// backdate AFTER the ack, which restamps delivered tombstones.
|
||||
await store.RecordPeerAckAsync(await store.GetMaxSeqAsync(), default);
|
||||
await db.ExecuteAsync(
|
||||
"UPDATE __localdb_row_version SET tombstone_utc = '2000-01-01T00:00:00.000Z' WHERE pk_json = '{\"id\":1}'");
|
||||
|
||||
var store = new OplogStore(db, new ReplicationOptions());
|
||||
await store.PruneTombstonesAsync(default);
|
||||
|
||||
var rv = await db.QueryAsync(
|
||||
|
||||
Reference in New Issue
Block a user