refactor(host): CentralSingletonRegistrar -> SingletonRegistrar with optional role scope — unblocks site-singleton drains (plan R2-01 T10)
This commit is contained in:
@@ -456,14 +456,14 @@ akka {{
|
|||||||
var outboxAuditWriter = _serviceProvider
|
var outboxAuditWriter = _serviceProvider
|
||||||
.GetRequiredService<ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services.ICentralAuditWriter>();
|
.GetRequiredService<ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services.ICentralAuditWriter>();
|
||||||
|
|
||||||
// All central singletons below are created through CentralSingletonRegistrar.Start,
|
// All central singletons below are created through SingletonRegistrar.Start,
|
||||||
// which pins the actor to the active central node (ClusterSingletonManager), exposes a
|
// which pins the actor to the active central node (ClusterSingletonManager), exposes a
|
||||||
// stable address (ClusterSingletonProxy), and — crucially — registers a PhaseClusterLeave
|
// stable address (ClusterSingletonProxy), and — crucially — registers a PhaseClusterLeave
|
||||||
// GracefulStop drain task so an in-flight EF write drains before handover (review 01
|
// GracefulStop drain task so an in-flight EF write drains before handover (review 01
|
||||||
// [Medium]: notification-outbox and audit-log-ingest previously had NO drain task). Names
|
// [Medium]: notification-outbox and audit-log-ingest previously had NO drain task). Names
|
||||||
// are unchanged ({name}-singleton / {name}-proxy). The two SITE singletons stay
|
// are unchanged ({name}-singleton / {name}-proxy). Site singletons use the same
|
||||||
// hand-rolled below because they are role-scoped (.WithRole(siteRole)).
|
// registrar with `role: siteRole` (Task 11 / round-2 N5).
|
||||||
var outbox = CentralSingletonRegistrar.Start(
|
var outbox = SingletonRegistrar.Start(
|
||||||
_actorSystem!, "notification-outbox",
|
_actorSystem!, "notification-outbox",
|
||||||
Props.Create(() => new ZB.MOM.WW.ScadaBridge.NotificationOutbox.NotificationOutboxActor(
|
Props.Create(() => new ZB.MOM.WW.ScadaBridge.NotificationOutbox.NotificationOutboxActor(
|
||||||
_serviceProvider,
|
_serviceProvider,
|
||||||
@@ -496,7 +496,7 @@ akka {{
|
|||||||
var auditIngestLogger = _serviceProvider.GetRequiredService<ILoggerFactory>()
|
var auditIngestLogger = _serviceProvider.GetRequiredService<ILoggerFactory>()
|
||||||
.CreateLogger<ZB.MOM.WW.ScadaBridge.AuditLog.Central.AuditLogIngestActor>();
|
.CreateLogger<ZB.MOM.WW.ScadaBridge.AuditLog.Central.AuditLogIngestActor>();
|
||||||
|
|
||||||
var auditIngest = CentralSingletonRegistrar.Start(
|
var auditIngest = SingletonRegistrar.Start(
|
||||||
_actorSystem!, "audit-log-ingest",
|
_actorSystem!, "audit-log-ingest",
|
||||||
Props.Create(() => new ZB.MOM.WW.ScadaBridge.AuditLog.Central.AuditLogIngestActor(
|
Props.Create(() => new ZB.MOM.WW.ScadaBridge.AuditLog.Central.AuditLogIngestActor(
|
||||||
_serviceProvider,
|
_serviceProvider,
|
||||||
@@ -561,7 +561,7 @@ akka {{
|
|||||||
var siteCallAuditOptions = _serviceProvider
|
var siteCallAuditOptions = _serviceProvider
|
||||||
.GetRequiredService<IOptions<ZB.MOM.WW.ScadaBridge.SiteCallAudit.SiteCallAuditOptions>>().Value;
|
.GetRequiredService<IOptions<ZB.MOM.WW.ScadaBridge.SiteCallAudit.SiteCallAuditOptions>>().Value;
|
||||||
|
|
||||||
var siteCallAudit = CentralSingletonRegistrar.Start(
|
var siteCallAudit = SingletonRegistrar.Start(
|
||||||
_actorSystem!, "site-call-audit",
|
_actorSystem!, "site-call-audit",
|
||||||
Props.Create(() => new ZB.MOM.WW.ScadaBridge.SiteCallAudit.SiteCallAuditActor(
|
Props.Create(() => new ZB.MOM.WW.ScadaBridge.SiteCallAudit.SiteCallAuditActor(
|
||||||
_serviceProvider,
|
_serviceProvider,
|
||||||
@@ -606,7 +606,7 @@ akka {{
|
|||||||
var auditLogOptions = _serviceProvider
|
var auditLogOptions = _serviceProvider
|
||||||
.GetRequiredService<IOptions<ZB.MOM.WW.ScadaBridge.AuditLog.Configuration.AuditLogOptions>>();
|
.GetRequiredService<IOptions<ZB.MOM.WW.ScadaBridge.AuditLog.Configuration.AuditLogOptions>>();
|
||||||
|
|
||||||
CentralSingletonRegistrar.Start(
|
SingletonRegistrar.Start(
|
||||||
_actorSystem!, "audit-log-purge",
|
_actorSystem!, "audit-log-purge",
|
||||||
Props.Create(() => new ZB.MOM.WW.ScadaBridge.AuditLog.Central.AuditLogPurgeActor(
|
Props.Create(() => new ZB.MOM.WW.ScadaBridge.AuditLog.Central.AuditLogPurgeActor(
|
||||||
_serviceProvider,
|
_serviceProvider,
|
||||||
@@ -629,7 +629,7 @@ akka {{
|
|||||||
var auditReconClient = _serviceProvider
|
var auditReconClient = _serviceProvider
|
||||||
.GetRequiredService<ZB.MOM.WW.ScadaBridge.AuditLog.Central.IPullAuditEventsClient>();
|
.GetRequiredService<ZB.MOM.WW.ScadaBridge.AuditLog.Central.IPullAuditEventsClient>();
|
||||||
|
|
||||||
CentralSingletonRegistrar.Start(
|
SingletonRegistrar.Start(
|
||||||
_actorSystem!, "site-audit-reconciliation",
|
_actorSystem!, "site-audit-reconciliation",
|
||||||
Props.Create(() => new ZB.MOM.WW.ScadaBridge.AuditLog.Central.SiteAuditReconciliationActor(
|
Props.Create(() => new ZB.MOM.WW.ScadaBridge.AuditLog.Central.SiteAuditReconciliationActor(
|
||||||
auditReconSites,
|
auditReconSites,
|
||||||
@@ -660,7 +660,7 @@ akka {{
|
|||||||
var kpiHistoryLogger = _serviceProvider.GetRequiredService<ILoggerFactory>()
|
var kpiHistoryLogger = _serviceProvider.GetRequiredService<ILoggerFactory>()
|
||||||
.CreateLogger<ZB.MOM.WW.ScadaBridge.KpiHistory.KpiHistoryRecorderActor>();
|
.CreateLogger<ZB.MOM.WW.ScadaBridge.KpiHistory.KpiHistoryRecorderActor>();
|
||||||
|
|
||||||
CentralSingletonRegistrar.Start(
|
SingletonRegistrar.Start(
|
||||||
_actorSystem!, "kpi-history-recorder",
|
_actorSystem!, "kpi-history-recorder",
|
||||||
Props.Create(() => new ZB.MOM.WW.ScadaBridge.KpiHistory.KpiHistoryRecorderActor(
|
Props.Create(() => new ZB.MOM.WW.ScadaBridge.KpiHistory.KpiHistoryRecorderActor(
|
||||||
_serviceProvider,
|
_serviceProvider,
|
||||||
@@ -686,7 +686,7 @@ akka {{
|
|||||||
var pendingPurgeCommunicationOptions =
|
var pendingPurgeCommunicationOptions =
|
||||||
_serviceProvider.GetRequiredService<IOptions<CommunicationOptions>>();
|
_serviceProvider.GetRequiredService<IOptions<CommunicationOptions>>();
|
||||||
|
|
||||||
CentralSingletonRegistrar.Start(
|
SingletonRegistrar.Start(
|
||||||
_actorSystem!, "pending-deployment-purge",
|
_actorSystem!, "pending-deployment-purge",
|
||||||
Props.Create(() => new PendingDeploymentPurgeActor(
|
Props.Create(() => new PendingDeploymentPurgeActor(
|
||||||
_serviceProvider,
|
_serviceProvider,
|
||||||
|
|||||||
+22
-11
@@ -5,21 +5,24 @@ using Microsoft.Extensions.Logging;
|
|||||||
namespace ZB.MOM.WW.ScadaBridge.Host.Actors;
|
namespace ZB.MOM.WW.ScadaBridge.Host.Actors;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Registers a central cluster singleton with the canonical naming scheme
|
/// Registers a cluster singleton with the canonical naming scheme
|
||||||
/// (<c>{name}-singleton</c> / <c>{name}-proxy</c>), a PoisonPill termination
|
/// (<c>{name}-singleton</c> / <c>{name}-proxy</c>), a PoisonPill termination
|
||||||
/// message, and a PhaseClusterLeave drain task that GracefulStops the manager
|
/// message, and a PhaseClusterLeave drain task that GracefulStops the manager
|
||||||
/// so in-flight EF work completes before handover. Extracted from five
|
/// so in-flight EF (central) or SQLite (site) work completes before handover.
|
||||||
/// copy-pasted ~60-line blocks (review 01 [Low]) whose drift left the two
|
/// Extracted from copy-pasted ~60-line blocks (review 01 [Low]) whose drift
|
||||||
/// busiest singletons (notification-outbox, audit-log-ingest) without drain
|
/// left the two busiest singletons (notification-outbox, audit-log-ingest)
|
||||||
/// tasks (review 01 [Medium]).
|
/// without drain tasks (review 01 [Medium]). The optional <c>role</c> maps to
|
||||||
|
/// <see cref="ClusterSingletonManagerSettings"/>/<see cref="ClusterSingletonProxySettings"/>
|
||||||
|
/// <c>.WithRole</c>, so role-scoped site singletons get the same drain
|
||||||
|
/// (review 01 round-2 N5).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal static class CentralSingletonRegistrar
|
internal static class SingletonRegistrar
|
||||||
{
|
{
|
||||||
internal sealed record Handle(IActorRef Manager, IActorRef Proxy);
|
internal sealed record Handle(IActorRef Manager, IActorRef Proxy);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates the <see cref="ClusterSingletonManager"/> and proxy for a central
|
/// Creates the <see cref="ClusterSingletonManager"/> and proxy for a cluster
|
||||||
/// cluster singleton under the canonical naming scheme, and registers a
|
/// singleton under the canonical naming scheme, and registers a
|
||||||
/// PhaseClusterLeave drain task that GracefulStops the manager on shutdown.
|
/// PhaseClusterLeave drain task that GracefulStops the manager on shutdown.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="system">The actor system to register the singleton on.</param>
|
/// <param name="system">The actor system to register the singleton on.</param>
|
||||||
@@ -27,19 +30,24 @@ internal static class CentralSingletonRegistrar
|
|||||||
/// <param name="singletonProps">The <see cref="Props"/> used to create the singleton's managed actor.</param>
|
/// <param name="singletonProps">The <see cref="Props"/> used to create the singleton's managed actor.</param>
|
||||||
/// <param name="logger">Logger used to report drain progress/failures.</param>
|
/// <param name="logger">Logger used to report drain progress/failures.</param>
|
||||||
/// <param name="drainTimeout">Maximum time to wait for the drain task to complete; defaults to 10 seconds.</param>
|
/// <param name="drainTimeout">Maximum time to wait for the drain task to complete; defaults to 10 seconds.</param>
|
||||||
|
/// <param name="role">Optional cluster role to scope the singleton to (applied to both the manager and proxy settings).</param>
|
||||||
/// <returns>A <see cref="Handle"/> carrying the singleton manager and proxy actor refs.</returns>
|
/// <returns>A <see cref="Handle"/> carrying the singleton manager and proxy actor refs.</returns>
|
||||||
internal static Handle Start(
|
internal static Handle Start(
|
||||||
ActorSystem system,
|
ActorSystem system,
|
||||||
string name,
|
string name,
|
||||||
Props singletonProps,
|
Props singletonProps,
|
||||||
ILogger logger,
|
ILogger logger,
|
||||||
TimeSpan? drainTimeout = null)
|
TimeSpan? drainTimeout = null,
|
||||||
|
string? role = null)
|
||||||
{
|
{
|
||||||
|
var managerSettings = ClusterSingletonManagerSettings.Create(system).WithSingletonName(name);
|
||||||
|
if (role is not null) managerSettings = managerSettings.WithRole(role);
|
||||||
|
|
||||||
var manager = system.ActorOf(
|
var manager = system.ActorOf(
|
||||||
ClusterSingletonManager.Props(
|
ClusterSingletonManager.Props(
|
||||||
singletonProps,
|
singletonProps,
|
||||||
PoisonPill.Instance,
|
PoisonPill.Instance,
|
||||||
ClusterSingletonManagerSettings.Create(system).WithSingletonName(name)),
|
managerSettings),
|
||||||
$"{name}-singleton");
|
$"{name}-singleton");
|
||||||
|
|
||||||
var timeout = drainTimeout ?? TimeSpan.FromSeconds(10);
|
var timeout = drainTimeout ?? TimeSpan.FromSeconds(10);
|
||||||
@@ -61,10 +69,13 @@ internal static class CentralSingletonRegistrar
|
|||||||
return Akka.Done.Instance;
|
return Akka.Done.Instance;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var proxySettings = ClusterSingletonProxySettings.Create(system).WithSingletonName(name);
|
||||||
|
if (role is not null) proxySettings = proxySettings.WithRole(role);
|
||||||
|
|
||||||
var proxy = system.ActorOf(
|
var proxy = system.ActorOf(
|
||||||
ClusterSingletonProxy.Props(
|
ClusterSingletonProxy.Props(
|
||||||
$"/user/{name}-singleton",
|
$"/user/{name}-singleton",
|
||||||
ClusterSingletonProxySettings.Create(system).WithSingletonName(name)),
|
proxySettings),
|
||||||
$"{name}-proxy");
|
$"{name}-proxy");
|
||||||
|
|
||||||
return new Handle(manager, proxy);
|
return new Handle(manager, proxy);
|
||||||
@@ -51,9 +51,9 @@ public class CoordinatedShutdownTests
|
|||||||
|
|
||||||
// Review 01 [Medium]: notification-outbox and audit-log-ingest were the
|
// Review 01 [Medium]: notification-outbox and audit-log-ingest were the
|
||||||
// only central singletons WITHOUT a cluster-leave drain task. All seven
|
// only central singletons WITHOUT a cluster-leave drain task. All seven
|
||||||
// now go through CentralSingletonRegistrar.Start, which always adds the
|
// now go through SingletonRegistrar.Start, which always adds the
|
||||||
// PhaseClusterLeave GracefulStop drain.
|
// PhaseClusterLeave GracefulStop drain.
|
||||||
Assert.Contains("CentralSingletonRegistrar.Start(", content);
|
Assert.Contains("SingletonRegistrar.Start(", content);
|
||||||
foreach (var name in new[] { "notification-outbox", "audit-log-ingest", "site-call-audit",
|
foreach (var name in new[] { "notification-outbox", "audit-log-ingest", "site-call-audit",
|
||||||
"audit-log-purge", "site-audit-reconciliation",
|
"audit-log-purge", "site-audit-reconciliation",
|
||||||
"kpi-history-recorder", "pending-deployment-purge" })
|
"kpi-history-recorder", "pending-deployment-purge" })
|
||||||
|
|||||||
+21
-2
@@ -5,7 +5,7 @@ using ZB.MOM.WW.ScadaBridge.Host.Actors;
|
|||||||
|
|
||||||
namespace ZB.MOM.WW.ScadaBridge.Host.Tests;
|
namespace ZB.MOM.WW.ScadaBridge.Host.Tests;
|
||||||
|
|
||||||
public class CentralSingletonRegistrarTests
|
public class SingletonRegistrarTests
|
||||||
{
|
{
|
||||||
private sealed class EchoActor : ReceiveActor
|
private sealed class EchoActor : ReceiveActor
|
||||||
{
|
{
|
||||||
@@ -16,7 +16,7 @@ public class CentralSingletonRegistrarTests
|
|||||||
public async Task Start_CreatesManagerAndProxy_WithCanonicalNames_AndDrainTask()
|
public async Task Start_CreatesManagerAndProxy_WithCanonicalNames_AndDrainTask()
|
||||||
{
|
{
|
||||||
using var system = CreateSingleNodeClusterSystem();
|
using var system = CreateSingleNodeClusterSystem();
|
||||||
var handle = CentralSingletonRegistrar.Start(
|
var handle = SingletonRegistrar.Start(
|
||||||
system, "test-widget", Props.Create(() => new EchoActor()),
|
system, "test-widget", Props.Create(() => new EchoActor()),
|
||||||
NullLogger.Instance, drainTimeout: TimeSpan.FromSeconds(2));
|
NullLogger.Instance, drainTimeout: TimeSpan.FromSeconds(2));
|
||||||
|
|
||||||
@@ -36,6 +36,25 @@ public class CentralSingletonRegistrarTests
|
|||||||
Assert.True(handle.Manager.IsNobody() || system.WhenTerminated.IsCompleted);
|
Assert.True(handle.Manager.IsNobody() || system.WhenTerminated.IsCompleted);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Start_WithRoleScope_CreatesRoleScopedSingleton_ThatAnswers()
|
||||||
|
{
|
||||||
|
// Round-2 N5: role scoping is what kept the two SITE singletons
|
||||||
|
// (deployment-manager, event-log-handler) hand-rolled and drain-less.
|
||||||
|
using var system = CreateSingleNodeClusterSystem(); // roles = ["Central"]
|
||||||
|
var handle = SingletonRegistrar.Start(
|
||||||
|
system, "role-widget", Props.Create(() => new EchoActor()),
|
||||||
|
NullLogger.Instance, drainTimeout: TimeSpan.FromSeconds(2), role: "Central");
|
||||||
|
|
||||||
|
Assert.EndsWith("/user/role-widget-singleton", handle.Manager.Path.ToString());
|
||||||
|
Assert.EndsWith("/user/role-widget-proxy", handle.Proxy.Path.ToString());
|
||||||
|
|
||||||
|
// The node holds the role => manager instantiates the singleton and
|
||||||
|
// the role-scoped proxy resolves it.
|
||||||
|
var echo = await handle.Proxy.Ask<string>("hi", TimeSpan.FromSeconds(20));
|
||||||
|
Assert.Equal("hi", echo);
|
||||||
|
}
|
||||||
|
|
||||||
private static ActorSystem CreateSingleNodeClusterSystem()
|
private static ActorSystem CreateSingleNodeClusterSystem()
|
||||||
{
|
{
|
||||||
var port = FreePort();
|
var port = FreePort();
|
||||||
Reference in New Issue
Block a user