fix(site-call-audit): reconciliation pull serves only un-acked tracking rows (node-local ack ledger), batched page upsert, prune removed sites' cursors (review 2026-10-21 06/PERF-2 + lane H follow-up)

This commit is contained in:
Joseph Doherty
2026-10-22 09:35:30 -04:00
parent e69be1654f
commit 88273693f9
12 changed files with 789 additions and 10 deletions
@@ -499,6 +499,8 @@ public class SiteCallAuditActor : ReceiveActor
return;
}
PruneRemovedSites(sites);
if (sites.Count == 0)
{
return;
@@ -516,6 +518,30 @@ public class SiteCallAuditActor : ReceiveActor
await ReconcileSitesAsync(sites, client, repository).ConfigureAwait(false);
}
/// <summary>
/// Drops the per-site cursor and pinned latch of every site the enumerator no longer
/// returns (a deleted or renamed site), so the dictionaries do not grow with site
/// churn for the singleton's lifetime. A site that was pinned publishes its
/// <c>Pinned=false</c> transition first, so a health subscriber does not keep showing
/// a pinned site that no longer exists. Runs on the single-flight pass that owns the
/// dictionaries. A site that later reappears simply starts again from
/// <c>DateTime.MinValue</c>, which the idempotent upsert absorbs.
/// </summary>
private void PruneRemovedSites(IReadOnlyList<SiteEntry> sites)
{
var live = new HashSet<string>(sites.Select(s => s.SiteId), StringComparer.Ordinal);
foreach (var siteId in _reconciliationPinned.Keys.Where(k => !live.Contains(k)).ToList())
{
SetReconciliationPinned(siteId, pinned: false);
_reconciliationPinned.Remove(siteId);
}
foreach (var siteId in _reconciliationCursors.Keys.Where(k => !live.Contains(k)).ToList())
{
_reconciliationCursors.Remove(siteId);
}
}
/// <summary>
/// Reconciles every site in the tick against a single resolved repository,
/// isolating per-site faults so one bad site never sinks the rest of the
@@ -635,15 +661,19 @@ public class SiteCallAuditActor : ReceiveActor
var maxUpdated = since;
var maxAfterId = afterId;
var nowUtc = DateTime.UtcNow;
// IngestedAtUtc is the "central ingested (or last refreshed) this row" stamp —
// owned by the central actor, exactly as OnUpsertAsync does for the telemetry
// path. The whole page is written through ONE batched monotonic upsert (a few
// round trips per page instead of one per row — review 2026-10-21 06/PERF-2);
// a row already present from a prior push is a silent no-op. A fault propagates
// to the per-site catch BEFORE the cursor below moves, so the page is re-pulled.
await repository
.UpsertManyAsync(response.SiteCalls.Select(row => row with { IngestedAtUtc = nowUtc }).ToList())
.ConfigureAwait(false);
foreach (var row in response.SiteCalls)
{
// IngestedAtUtc is the "central ingested (or last refreshed) this
// row" stamp — owned by the central actor, exactly as OnUpsertAsync
// does for the telemetry path. Monotonic UpsertAsync makes a row
// already present (from a prior push) a silent no-op.
var siteCall = row with { IngestedAtUtc = nowUtc };
await repository.UpsertAsync(siteCall).ConfigureAwait(false);
// Advance the composite max: greater timestamp wins; on a tie the
// greater TrackedOperationId (ordinal, matching the site's SQLite
// BINARY text collation) wins.