feat(reconcile): central handler — gap diff + fresh tokens + orphans

This commit is contained in:
Joseph Doherty
2026-06-26 16:20:17 -04:00
parent ec2aa2bbac
commit 96192950a0
5 changed files with 462 additions and 0 deletions
@@ -7,6 +7,7 @@ using Microsoft.Extensions.DependencyInjection;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Communication;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Deployment;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Health;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Notification;
using ZB.MOM.WW.ScadaBridge.HealthMonitoring;
@@ -213,6 +214,12 @@ public class CentralCommunicationActor : ReceiveActor
// Audit Log (#23 M3) combined-telemetry ingest: routes to the same proxy
// the same way; the proxy replies with an IngestCachedTelemetryReply.
Receive<IngestCachedTelemetryCommand>(HandleIngestCachedTelemetry);
// Startup reconciliation: a site node forwards its local deployed inventory on
// startup via ClusterClient. Resolve the scoped ReconcileService, diff the
// inventory against central's expected set, and pipe the ReconcileSiteResponse
// (gap fetch tokens + orphans) straight back to the site node's ClusterClient.
Receive<ReconcileSiteRequest>(HandleReconcileSiteRequest);
}
private void HandleNotificationSubmit(NotificationSubmit msg)
@@ -297,6 +304,48 @@ public class CentralCommunicationActor : ReceiveActor
.PipeTo(replyTo);
}
/// <summary>
/// Startup reconciliation (site→central over ClusterClient): resolve the scoped
/// <see cref="ReconcileService"/> in a DI scope, diff the node's reported inventory
/// against central's expected set, and pipe the <see cref="ReconcileSiteResponse"/>
/// back to the site node's ClusterClient path. The actor stays thin — all the diff
/// and staging logic lives in the service. Mirrors the DB-access pattern used by
/// <see cref="LoadSiteAddressesFromDb"/> (Task.Run + CreateScope + PipeTo) and the
/// Sender-preservation pattern of <see cref="HandleIngestAuditEvents"/>.
///
/// On a faulted task PipeTo delivers a <see cref="Status.Failure"/> to the node; its
/// Ask faults and it simply retries reconcile on the next startup — reconcile is
/// best-effort, so the fault is allowed to propagate rather than being swallowed.
/// </summary>
private void HandleReconcileSiteRequest(ReconcileSiteRequest msg)
{
// Capture Sender before the async/PipeTo — Akka resets Sender between dispatches.
var replyTo = Sender;
// Bound the DB work by the actor lifecycle (Communication-019). The CTS may have
// been disposed by PostStop on a racing late message; treat that as "actor gone".
CancellationToken ct;
try
{
ct = _lifecycleCts.Token;
}
catch (ObjectDisposedException)
{
return;
}
_log.Debug(
"Handling ReconcileSiteRequest from site {0} node {1} ({2} local instance(s))",
msg.SiteIdentifier, msg.NodeId, msg.LocalNameToRevisionHash.Count);
Task.Run(async () =>
{
using var scope = _serviceProvider.CreateScope();
var service = scope.ServiceProvider.GetRequiredService<ReconcileService>();
return await service.ReconcileAsync(msg, ct).ConfigureAwait(false);
}).PipeTo(replyTo);
}
private void HandleHeartbeat(HeartbeatMessage heartbeat)
{
var aggregator = _serviceProvider.GetService<ICentralHealthAggregator>();