refactor(health.akka): review polish (internal decision helper, role guard, factory results, test coverage) + fix SPEC §4 gate description

This commit is contained in:
Joseph Doherty
2026-06-01 07:04:29 -04:00
parent edbc79204f
commit 1c2b23cbbb
6 changed files with 39 additions and 13 deletions
@@ -1,8 +1,11 @@
using System.Runtime.CompilerServices;
using Akka.Actor;
using Akka.Cluster;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
[assembly: InternalsVisibleTo("ZB.MOM.WW.Health.Akka.Tests")]
namespace ZB.MOM.WW.Health.Akka;
/// <summary>
@@ -10,7 +13,7 @@ namespace ZB.MOM.WW.Health.Akka;
/// <see cref="ActiveNodeHealthCheck"/> so the role-less and role-filtered matrices are exhaustively
/// table-testable without forming a real cluster.
/// </summary>
public static class ActiveNodeDecision
internal static class ActiveNodeDecision
{
/// <summary>
/// Maps the resolved cluster facts to a <see cref="HealthStatus"/>.
@@ -87,7 +90,8 @@ public sealed class ActiveNodeHealthCheck : IHealthCheck
public ActiveNodeHealthCheck(IServiceProvider serviceProvider, string role)
{
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
_role = role ?? throw new ArgumentNullException(nameof(role));
ArgumentException.ThrowIfNullOrWhiteSpace(role);
_role = role;
}
/// <inheritdoc />
@@ -119,15 +123,26 @@ public sealed class ActiveNodeHealthCheck : IHealthCheck
}
var health = ActiveNodeDecision.Evaluate(selfUp, isLeader, hasRole, _role);
return Task.FromResult(new HealthCheckResult(health, DescribeResult(health, self.Status)));
var description = DescribeResult(health, self.Status, selfUp, isLeader);
var result = health switch
{
HealthStatus.Healthy => HealthCheckResult.Healthy(description),
HealthStatus.Degraded => HealthCheckResult.Degraded(description),
_ => HealthCheckResult.Unhealthy(description),
};
return Task.FromResult(result);
}
private string DescribeResult(HealthStatus health, MemberStatus status)
private string DescribeResult(HealthStatus health, MemberStatus status, bool selfUp, bool isLeader)
{
if (_role is null)
return health == HealthStatus.Healthy
? "Active node (cluster leader)."
: $"Standby node (status: {status}).";
{
if (health == HealthStatus.Healthy)
return "Active node (cluster leader).";
return selfUp && !isLeader
? "Standby: node is Up but not the cluster leader."
: $"Standby: node is not Up (status: {status}).";
}
return health switch
{
@@ -45,7 +45,12 @@ public sealed class AkkaClusterHealthCheck : IHealthCheck
var status = Cluster.Get(system).SelfMember.Status;
var health = _policy.Evaluate(status);
var description = $"Akka cluster member status: {status}";
return Task.FromResult(new HealthCheckResult(health, description));
var result = health switch
{
HealthStatus.Healthy => HealthCheckResult.Healthy(description),
HealthStatus.Degraded => HealthCheckResult.Degraded(description),
_ => HealthCheckResult.Unhealthy(description),
};
return Task.FromResult(result);
}
}