Merge review fixes: termination watchdog exits 1 on cluster leave/down under Akka.Hosting (#980, windev SCM gate passed); Gateway alarm writer isolates poison events (#976)
This commit is contained in:
+61
@@ -98,6 +98,67 @@ public sealed class ActorSystemTerminationWatchdogTests
|
||||
exits.ExitCodes.ShouldBeEmpty("termination during an in-progress host shutdown must not exit as a failure");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #980 review: Akka.Hosting's termination hook (registered earlier) calls <c>StopApplication</c> AFTER
|
||||
/// the system terminated and BEFORE the watchdog's continuation. That stop must not read as "expected".
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task Stop_requested_by_a_termination_hook_after_termination_still_exits()
|
||||
{
|
||||
var system = ActorSystem.Create("watchdog-hosting-hook-order");
|
||||
var lifetime = new FakeHostApplicationLifetime();
|
||||
var exits = new ExitRecorder();
|
||||
|
||||
// Mimic AkkaHostedService.TerminationHook: registered first, runs first.
|
||||
_ = system.WhenTerminated.ContinueWith(_ => lifetime.TriggerStopping(), TaskContinuationOptions.ExecuteSynchronously);
|
||||
var watchdog = Create(system, lifetime, exits);
|
||||
await watchdog.StartAsync(CancellationToken.None);
|
||||
|
||||
await system.Terminate();
|
||||
|
||||
await WaitForAsync(() => exits.ExitCodes.Count > 0, TimeSpan.FromSeconds(5));
|
||||
exits.ExitCodes.ShouldBe([ActorSystemTerminationWatchdog.FailureExitCode]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A host stop that arrives AFTER an unexpected termination (the host reacting to Akka.Hosting's
|
||||
/// StopApplication) neither cancels the exit nor completes before it is issued — otherwise the Windows
|
||||
/// service host could report SERVICE_STOPPED first.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task Stop_after_unexpected_termination_waits_for_the_exit()
|
||||
{
|
||||
var system = ActorSystem.Create("watchdog-stop-after-termination");
|
||||
var lifetime = new FakeHostApplicationLifetime();
|
||||
var exits = new ExitRecorder();
|
||||
var exitGate = new ManualResetEventSlim(false);
|
||||
var watchdog = new ActorSystemTerminationWatchdog(
|
||||
() => system, lifetime, NullLogger<ActorSystemTerminationWatchdog>.Instance,
|
||||
code => { exitGate.Wait(TimeSpan.FromSeconds(10)); exits.Exit(code); }, exits.Flush);
|
||||
await watchdog.StartAsync(CancellationToken.None);
|
||||
|
||||
await system.Terminate();
|
||||
var stop = watchdog.StopAsync(CancellationToken.None);
|
||||
|
||||
await Task.Delay(300, TestContext.Current.CancellationToken);
|
||||
stop.IsCompleted.ShouldBeFalse("host shutdown must be held until the failure exit is issued");
|
||||
exitGate.Set();
|
||||
await stop.WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken);
|
||||
exits.ExitCodes.ShouldBe([ActorSystemTerminationWatchdog.FailureExitCode]);
|
||||
}
|
||||
|
||||
/// <summary>The expected/unexpected verdict by CoordinatedShutdown reason.</summary>
|
||||
[Fact]
|
||||
public void Only_a_host_stop_or_ClrExitReason_is_expected()
|
||||
{
|
||||
ActorSystemTerminationWatchdog.IsExpectedTermination(CoordinatedShutdown.ClrExitReason.Instance, false).ShouldBeTrue();
|
||||
ActorSystemTerminationWatchdog.IsExpectedTermination(CoordinatedShutdown.ClusterLeavingReason.Instance, true).ShouldBeTrue();
|
||||
ActorSystemTerminationWatchdog.IsExpectedTermination(CoordinatedShutdown.ClusterLeavingReason.Instance, false).ShouldBeFalse();
|
||||
ActorSystemTerminationWatchdog.IsExpectedTermination(CoordinatedShutdown.ClusterDowningReason.Instance, false).ShouldBeFalse();
|
||||
ActorSystemTerminationWatchdog.IsExpectedTermination(CoordinatedShutdown.ActorSystemTerminateReason.Instance, false).ShouldBeFalse();
|
||||
ActorSystemTerminationWatchdog.IsExpectedTermination(null, false).ShouldBeFalse();
|
||||
}
|
||||
|
||||
private static ActorSystemTerminationWatchdog Create(
|
||||
ActorSystem system, IHostApplicationLifetime lifetime, ExitRecorder exits) =>
|
||||
new(() => system, lifetime, NullLogger<ActorSystemTerminationWatchdog>.Instance, exits.Exit, exits.Flush);
|
||||
|
||||
+202
@@ -0,0 +1,202 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using Akka.Actor;
|
||||
using Akka.Cluster;
|
||||
using Akka.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Cluster;
|
||||
using ZB.MOM.WW.OtOpcUa.Host;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Host.IntegrationTests;
|
||||
|
||||
/// <summary>
|
||||
/// #980 review: the termination watchdog under a REAL Akka.Hosting host with clustering, wired exactly as
|
||||
/// <c>Program.cs</c> does (production cluster bootstrap, <see cref="GracefulClusterLeaveService"/>, the
|
||||
/// watchdog registration).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <see cref="ActorSystemTerminationWatchdogTests"/> uses a bare <see cref="ActorSystem"/>, which has no
|
||||
/// Akka.Hosting termination hook — and that hook is the whole bug: for a cluster leave/down
|
||||
/// <c>AkkaHostedService</c> sets <c>Environment.ExitCode = -1</c> and calls <c>StopApplication()</c>
|
||||
/// BEFORE the watchdog's continuation runs, so a watchdog keyed on "the host is already stopping" stayed
|
||||
/// quiet for every manual failover, endpoint step-down and auto-down, and the node stopped gracefully
|
||||
/// (SERVICE_STOPPED, exit 0) where SCM never restarts it. These tests fail on that version.
|
||||
/// </remarks>
|
||||
|
||||
public sealed class TerminationWatchdogAkkaHostingTests
|
||||
{
|
||||
private static readonly TimeSpan ExitWait = TimeSpan.FromSeconds(30);
|
||||
|
||||
/// <summary>
|
||||
/// <c>Cluster.Leave(self)</c> — what manual failover (<c>ManualFailoverService</c>) and the endpoint
|
||||
/// step-down actor do — must end the process non-zero, not in a graceful host stop.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task Cluster_leave_of_self_exits_the_process_non_zero()
|
||||
{
|
||||
var exits = new ExitRecorder();
|
||||
using var host = await StartNodeAsync(exits);
|
||||
|
||||
var cluster = Akka.Cluster.Cluster.Get(host.Services.GetRequiredService<ActorSystem>());
|
||||
cluster.Leave(cluster.SelfAddress);
|
||||
|
||||
(await exits.WaitForExitAsync(ExitWait)).ShouldBeTrue(
|
||||
"a cluster leave the host did not initiate must exit the process so SCM restarts it");
|
||||
exits.Codes.ShouldBe([ActorSystemTerminationWatchdog.FailureExitCode]);
|
||||
CoordinatedShutdown.Get(host.Services.GetRequiredService<ActorSystem>()).ShutdownReason
|
||||
.ShouldBeOfType<CoordinatedShutdown.ClusterLeavingReason>();
|
||||
|
||||
await StopQuietlyAsync(host);
|
||||
}
|
||||
|
||||
/// <summary><c>Cluster.Down(self)</c> — the auto-down / SBR shape — must also exit non-zero.</summary>
|
||||
[Fact]
|
||||
public async Task Cluster_down_of_self_exits_the_process_non_zero()
|
||||
{
|
||||
var exits = new ExitRecorder();
|
||||
using var host = await StartNodeAsync(exits);
|
||||
|
||||
var cluster = Akka.Cluster.Cluster.Get(host.Services.GetRequiredService<ActorSystem>());
|
||||
cluster.Down(cluster.SelfAddress);
|
||||
|
||||
(await exits.WaitForExitAsync(ExitWait)).ShouldBeTrue("a self-down must exit the process non-zero");
|
||||
exits.Codes.ShouldBe([ActorSystemTerminationWatchdog.FailureExitCode]);
|
||||
|
||||
await StopQuietlyAsync(host);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Positive control: a host-initiated stop (what SCM stop / Ctrl-C reach via <c>IHost.StopAsync</c>)
|
||||
/// leaves the cluster through <see cref="GracefulClusterLeaveService"/> and must stay exit-free.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task Host_initiated_stop_stays_exit_free()
|
||||
{
|
||||
var exits = new ExitRecorder();
|
||||
var host = await StartNodeAsync(exits);
|
||||
var system = host.Services.GetRequiredService<ActorSystem>();
|
||||
|
||||
await host.StopAsync(TestContext.Current.CancellationToken);
|
||||
await system.WhenTerminated.WaitAsync(ExitWait, TestContext.Current.CancellationToken);
|
||||
|
||||
(await exits.WaitForExitAsync(TimeSpan.FromSeconds(2))).ShouldBeFalse(
|
||||
"a host-initiated stop is a planned stop, not a crash");
|
||||
host.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Positive control: an SCM-style stop (<c>StopApplication</c> while the system still runs — what
|
||||
/// <c>WindowsServiceLifetime.OnStop</c> does) followed by the host's own stop stays exit-free, even
|
||||
/// without <see cref="GracefulClusterLeaveService"/> (Akka.Hosting's StopAsync runs ClrExitReason).
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task Stop_application_while_running_stays_exit_free_without_graceful_leave()
|
||||
{
|
||||
var exits = new ExitRecorder();
|
||||
var host = await StartNodeAsync(exits, withGracefulLeave: false);
|
||||
var system = host.Services.GetRequiredService<ActorSystem>();
|
||||
|
||||
host.Services.GetRequiredService<IHostApplicationLifetime>().StopApplication();
|
||||
await host.StopAsync(TestContext.Current.CancellationToken);
|
||||
await system.WhenTerminated.WaitAsync(ExitWait, TestContext.Current.CancellationToken);
|
||||
|
||||
(await exits.WaitForExitAsync(TimeSpan.FromSeconds(2))).ShouldBeFalse(
|
||||
"a supervisor-requested stop is a planned stop, not a crash");
|
||||
host.Dispose();
|
||||
}
|
||||
|
||||
private static async Task<IHost> StartNodeAsync(ExitRecorder exits, bool withGracefulLeave = true)
|
||||
{
|
||||
var systemName = "wd980";
|
||||
var port = FreePort();
|
||||
var self = $"akka.tcp://{systemName}@127.0.0.1:{port}";
|
||||
var options = new AkkaClusterOptions
|
||||
{
|
||||
SystemName = systemName,
|
||||
Hostname = "127.0.0.1",
|
||||
PublicHostname = "127.0.0.1",
|
||||
Port = port,
|
||||
Roles = ["admin", "driver"],
|
||||
SeedNodes = [self],
|
||||
};
|
||||
|
||||
var builder = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder();
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
services.AddSingleton<IOptions<AkkaClusterOptions>>(Options.Create(options));
|
||||
services.AddAkka(systemName, (ab, sp) => ab.WithOtOpcUaClusterBootstrap(sp));
|
||||
// Same order as Program.cs: both after AddAkka.
|
||||
if (withGracefulLeave)
|
||||
services.AddOtOpcUaGracefulClusterLeave();
|
||||
services.AddOtOpcUaTerminationWatchdog(exits.Exit, () => { });
|
||||
});
|
||||
|
||||
var host = builder.Build();
|
||||
await host.StartAsync(TestContext.Current.CancellationToken);
|
||||
|
||||
var cluster = Akka.Cluster.Cluster.Get(host.Services.GetRequiredService<ActorSystem>());
|
||||
var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(30);
|
||||
while (cluster.SelfMember.Status != MemberStatus.Up && DateTime.UtcNow < deadline)
|
||||
await Task.Delay(100, TestContext.Current.CancellationToken);
|
||||
cluster.SelfMember.Status.ShouldBe(MemberStatus.Up, "the single-node cluster must form before the test acts");
|
||||
return host;
|
||||
}
|
||||
|
||||
private static async Task StopQuietlyAsync(IHost host)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
|
||||
await host.StopAsync(cts.Token);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// The host may already be stopping on Akka.Hosting's StopApplication; nothing to assert here.
|
||||
}
|
||||
}
|
||||
|
||||
private static int FreePort()
|
||||
{
|
||||
var listener = new TcpListener(IPAddress.Loopback, 0);
|
||||
listener.Start();
|
||||
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
|
||||
listener.Stop();
|
||||
return port;
|
||||
}
|
||||
|
||||
/// <summary>Records the watchdog's exit instead of ending the test process.</summary>
|
||||
private sealed class ExitRecorder
|
||||
{
|
||||
private readonly TaskCompletionSource _exited = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
private readonly object _gate = new();
|
||||
private readonly List<int> _codes = [];
|
||||
|
||||
public IReadOnlyList<int> Codes
|
||||
{
|
||||
get { lock (_gate) return _codes.ToArray(); }
|
||||
}
|
||||
|
||||
public void Exit(int code)
|
||||
{
|
||||
lock (_gate) _codes.Add(code);
|
||||
_exited.TrySetResult();
|
||||
}
|
||||
|
||||
public async Task<bool> WaitForExitAsync(TimeSpan timeout)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _exited.Task.WaitAsync(timeout);
|
||||
return true;
|
||||
}
|
||||
catch (TimeoutException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user