fix(audit): reconciliation pulls every site node with a per-(site, node) cursor (AUD-1, WP4 task 4.8)

This commit is contained in:
Joseph Doherty
2026-12-03 10:08:20 -05:00
parent c15bd210f7
commit 4070f7efca
10 changed files with 514 additions and 60 deletions
@@ -132,7 +132,12 @@ public class SiteCallAuditActor : ReceiveActor
/// next tick re-pulls from the start and idempotent upsert dedupes). Mirrors
/// <c>SiteAuditReconciliationActor</c>'s cursor (now composite).
/// </summary>
private readonly Dictionary<string, (DateTime Since, string? AfterId)> _reconciliationCursors = new();
/// <remarks>
/// Keyed per <b>(site, node endpoint)</b> since AUD-1 (2026-12): on a site pair that does not
/// run LocalDb replication each node's operation-tracking store is its own, so each node is
/// pulled with its own cursor; one per-site cursor fed by NodeA's rows skipped NodeB's.
/// </remarks>
private readonly Dictionary<(string SiteId, string Endpoint), (DateTime Since, string? AfterId)> _reconciliationCursors = new();
/// <summary>
/// Per-site "pinned" latch — <c>true</c> once a site's composite
@@ -500,6 +505,7 @@ public class SiteCallAuditActor : ReceiveActor
}
PruneRemovedSites(sites);
(client as IGrpcChannelCachePruner)?.RetainOnly(sites);
if (sites.Count == 0)
{
@@ -536,12 +542,35 @@ public class SiteCallAuditActor : ReceiveActor
_reconciliationPinned.Remove(siteId);
}
foreach (var siteId in _reconciliationCursors.Keys.Where(k => !live.Contains(k)).ToList())
var liveNodes = new HashSet<(string, string)>(
sites.SelectMany(s => NodeEndpoints(s).Select(e => (s.SiteId, e))));
foreach (var key in _reconciliationCursors.Keys.Where(k => !liveNodes.Contains(k)).ToList())
{
_reconciliationCursors.Remove(siteId);
_reconciliationCursors.Remove(key);
}
}
/// <summary>
/// The distinct node endpoints of a site: the primary (NodeA where configured) and, when a
/// distinct one exists, the second node (AUD-1 — both are pulled every tick).
/// </summary>
private static IEnumerable<string> NodeEndpoints(SiteEntry site)
{
if (!string.IsNullOrWhiteSpace(site.GrpcEndpoint))
{
yield return site.GrpcEndpoint;
}
if (!string.IsNullOrWhiteSpace(site.FallbackGrpcEndpoint)
&& !string.Equals(site.FallbackGrpcEndpoint, site.GrpcEndpoint, StringComparison.Ordinal))
{
yield return site.FallbackGrpcEndpoint;
}
}
/// <summary>Test seam: number of (site, node) reconciliation cursors held.</summary>
internal int ReconciliationCursorCountForTests => _reconciliationCursors.Count;
/// <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
@@ -640,7 +669,37 @@ public class SiteCallAuditActor : ReceiveActor
private async Task ReconcileSiteAsync(
SiteEntry site, IPullSiteCallsClient client, ISiteCallAuditRepository repository)
{
var cursor = _reconciliationCursors.TryGetValue(site.SiteId, out var c)
// AUD-1: pull EVERY node with its own cursor. One node faulting must not skip the
// other; the site is pinned when any node is.
var anyPinned = false;
foreach (var endpoint in NodeEndpoints(site))
{
try
{
anyPinned |= await ReconcileNodeAsync(site, endpoint, client, repository).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.LogWarning(ex,
"SiteCallAudit reconciliation pull failed for site {SiteId} node {Endpoint}; the site's other node continues.",
site.SiteId, endpoint);
anyPinned |= _reconciliationPinned.TryGetValue(site.SiteId, out var wasPinned) && wasPinned;
}
}
SetReconciliationPinned(site.SiteId, anyPinned);
}
/// <summary>
/// Drains one node of a site (see <see cref="ReconcileSiteAsync"/> for the cursor, page and
/// pinned semantics). Returns <c>true</c> when the node's cursor is pinned (a legacy node that
/// ignores the <c>after_id</c> keyset).
/// </summary>
private async Task<bool> ReconcileNodeAsync(
SiteEntry site, string endpoint, IPullSiteCallsClient client, ISiteCallAuditRepository repository)
{
var cursorKey = (site.SiteId, endpoint);
var cursor = _reconciliationCursors.TryGetValue(cursorKey, out var c)
? c
: (Since: DateTime.MinValue, AfterId: (string?)null);
@@ -655,7 +714,7 @@ public class SiteCallAuditActor : ReceiveActor
var since = cursor.Since;
var afterId = cursor.AfterId;
var response = await client
.PullAsync(site.SiteId, since, afterId, _options.ReconciliationBatchSize, CancellationToken.None)
.PullFromEndpointAsync(site.SiteId, endpoint, since, afterId, _options.ReconciliationBatchSize, CancellationToken.None)
.ConfigureAwait(false);
var maxUpdated = since;
@@ -694,19 +753,12 @@ public class SiteCallAuditActor : ReceiveActor
// Persist the advanced cursor after every page so a fault on a later
// page (caught per-site upstream) still keeps the rows already drained.
cursor = (maxUpdated, maxAfterId);
_reconciliationCursors[site.SiteId] = cursor;
// Progress resumed → clear any prior pinned latch (publishes the
// Pinned=false transition exactly once).
if (advanced)
{
SetReconciliationPinned(site.SiteId, pinned: false);
}
_reconciliationCursors[cursorKey] = cursor;
if (!response.MoreAvailable)
{
// Backlog fully drained for this site this tick.
return;
// Backlog fully drained for this node this tick.
return false;
}
if (!advanced)
@@ -716,16 +768,16 @@ public class SiteCallAuditActor : ReceiveActor
// site), so a continuation would re-pull the identical window
// forever. Latch + publish the pinned state and stop; the next tick
// re-checks and unpins if the site catches up.
SetReconciliationPinned(site.SiteId, pinned: true);
_logger.LogWarning(
"SiteCallAudit reconciliation for site {SiteId} is pinned: the composite "
"SiteCallAudit reconciliation for site {SiteId} node {Endpoint} is pinned: the composite "
+ "(UpdatedAtUtc, TrackedOperationId) cursor did not advance past ({CursorUtc:o}, {AfterId}) "
+ "yet the site reports more rows available — the site does not honour the after_id keyset "
+ "(legacy). The backlog tail will not reconcile until the site is upgraded.",
site.SiteId,
endpoint,
since,
afterId ?? "(none)");
return;
return true;
}
}
@@ -734,11 +786,13 @@ public class SiteCallAuditActor : ReceiveActor
// simply more than MaxReconciliationPagesPerTick × batch-size of it. The
// next tick resumes from the advanced cursor.
_logger.LogInformation(
"SiteCallAudit reconciliation for site {SiteId} hit the per-tick page ceiling "
"SiteCallAudit reconciliation for site {SiteId} node {Endpoint} hit the per-tick page ceiling "
+ "({MaxPages} pages); the cursor advanced each page and the remaining backlog "
+ "drains on the next tick.",
site.SiteId,
endpoint,
MaxReconciliationPagesPerTick);
return false;
}
/// <summary>