fix(health): wall-clock-anchored report sequence, nack stale reports, drain standby counters (COMM-1, COMM-6, WP4 task 4.1)
This commit is contained in:
@@ -51,6 +51,7 @@ public class HealthReportAckTests : TestKit
|
||||
var mockRepo = Substitute.For<ISiteRepository>();
|
||||
mockRepo.GetAllSitesAsync(Arg.Any<CancellationToken>()).Returns(new List<Site>());
|
||||
var aggregator = Substitute.For<ICentralHealthAggregator>();
|
||||
aggregator.ProcessReport(Arg.Any<SiteHealthReport>()).Returns(true);
|
||||
|
||||
var services = new ServiceCollection();
|
||||
services.AddScoped(_ => mockRepo);
|
||||
@@ -66,4 +67,31 @@ public class HealthReportAckTests : TestKit
|
||||
Assert.Equal(3, ack.SequenceNumber);
|
||||
AwaitAssert(() => aggregator.Received().ProcessReport(Arg.Is<SiteHealthReport>(r => r.SequenceNumber == 3)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// COMM-1 regression: a report the aggregator rejects as stale is nacked (Accepted=false with an
|
||||
/// error), so the site's transport throws and the drop is visible instead of being acked as accepted.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void CentralCommunicationActor_OnStaleReport_NacksWithError()
|
||||
{
|
||||
var mockRepo = Substitute.For<ISiteRepository>();
|
||||
mockRepo.GetAllSitesAsync(Arg.Any<CancellationToken>()).Returns(new List<Site>());
|
||||
var aggregator = Substitute.For<ICentralHealthAggregator>();
|
||||
aggregator.ProcessReport(Arg.Any<SiteHealthReport>()).Returns(false);
|
||||
|
||||
var services = new ServiceCollection();
|
||||
services.AddScoped(_ => mockRepo);
|
||||
services.AddSingleton(aggregator);
|
||||
var sp = services.BuildServiceProvider();
|
||||
|
||||
var transport = Substitute.For<ISiteCommandTransport>();
|
||||
var actor = Sys.ActorOf(Props.Create(() => new CentralCommunicationActor(sp, transport)));
|
||||
|
||||
actor.Tell(SampleReport(seq: 4));
|
||||
var ack = ExpectMsg<SiteHealthReportAck>();
|
||||
Assert.False(ack.Accepted);
|
||||
Assert.Equal(4, ack.SequenceNumber);
|
||||
Assert.False(string.IsNullOrEmpty(ack.Error));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,6 +113,17 @@ public class CentralHealthAggregatorTests
|
||||
}
|
||||
|
||||
/// <summary>Verifies that a report with a sequence number equal to the stored one is rejected.</summary>
|
||||
/// <summary>COMM-1: ProcessReport reports its verdict so the caller can nack a dropped report.</summary>
|
||||
[Fact]
|
||||
public void ProcessReport_ReturnsFalseOnlyWhenRejectedAsStale()
|
||||
{
|
||||
Assert.True(_aggregator.ProcessReport(MakeReport("site-1", 5)));
|
||||
Assert.True(_aggregator.ProcessReport(MakeReport("site-1", 6)));
|
||||
Assert.False(_aggregator.ProcessReport(MakeReport("site-1", 6)));
|
||||
Assert.False(_aggregator.ProcessReport(MakeReport("site-1", 2)));
|
||||
Assert.Equal(6, _aggregator.GetSiteState("site-1")!.LastSequenceNumber);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProcessReport_RejectsEqualSequence()
|
||||
{
|
||||
|
||||
@@ -28,7 +28,7 @@ public class CentralHealthReportLoopTests
|
||||
/// <summary>Gets the reports passed to <see cref="ProcessReport"/>.</summary>
|
||||
public List<SiteHealthReport> Processed { get; } = [];
|
||||
/// <inheritdoc />
|
||||
public void ProcessReport(SiteHealthReport report) => Processed.Add(report);
|
||||
public bool ProcessReport(SiteHealthReport report) { Processed.Add(report); return true; }
|
||||
/// <inheritdoc />
|
||||
public void MarkHeartbeat(string siteId, DateTimeOffset receivedAt) { }
|
||||
/// <inheritdoc />
|
||||
@@ -262,7 +262,7 @@ public class CentralHealthReportLoopTests
|
||||
public List<SiteHealthReport> Processed { get; } = [];
|
||||
|
||||
/// <inheritdoc />
|
||||
public void ProcessReport(SiteHealthReport report)
|
||||
public bool ProcessReport(SiteHealthReport report)
|
||||
{
|
||||
var n = Interlocked.Increment(ref _callCount);
|
||||
if (n == 1)
|
||||
@@ -270,6 +270,7 @@ public class CentralHealthReportLoopTests
|
||||
throw new InvalidOperationException("aggregator temporarily unavailable");
|
||||
}
|
||||
Processed.Add(report);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
@@ -478,4 +478,104 @@ public class HealthReportSenderTests
|
||||
|
||||
Assert.Equal(fixedInstant.ToUnixTimeMilliseconds(), sender.CurrentSequenceNumber);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// COMM-1 regression: two site nodes started 5 s apart; the older one (A) is active and has
|
||||
/// sent 6 000 reports (~50 h at 30 s). B becomes active. Under the old "seed at construction,
|
||||
/// +1 per report" scheme B's first sequence (T_B + 1) sorted below A's last (T_A + 6000) and
|
||||
/// central rejected every B report. With <c>max(prev + 1, now-ms)</c> B's first report sorts
|
||||
/// above anything A ever sent.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void NextSequenceNumber_AfterFailover_SortsAboveLongRunningPeer()
|
||||
{
|
||||
var t0 = new DateTimeOffset(2026, 12, 1, 0, 0, 0, TimeSpan.Zero);
|
||||
var clock = new TestTimeProvider(t0);
|
||||
|
||||
long seqA = t0.ToUnixTimeMilliseconds();
|
||||
long seqB = t0.AddSeconds(5).ToUnixTimeMilliseconds();
|
||||
|
||||
long lastA = 0;
|
||||
for (var i = 0; i < 6000; i++)
|
||||
{
|
||||
clock.Advance(TimeSpan.FromSeconds(30));
|
||||
lastA = HealthReportSender.NextSequenceNumber(ref seqA, clock);
|
||||
}
|
||||
|
||||
clock.Advance(TimeSpan.FromSeconds(30));
|
||||
var firstB = HealthReportSender.NextSequenceNumber(ref seqB, clock);
|
||||
|
||||
Assert.True(firstB > lastA, $"B's first sequence {firstB} must sort above A's last {lastA}");
|
||||
// The old scheme would have produced T_B + 1, below A's T_A + 6000.
|
||||
Assert.True(t0.AddSeconds(5).ToUnixTimeMilliseconds() + 1 < t0.ToUnixTimeMilliseconds() + 6000);
|
||||
}
|
||||
|
||||
/// <summary>COMM-1: the sequence stays strictly monotonic when the wall clock steps backwards.</summary>
|
||||
[Fact]
|
||||
public void NextSequenceNumber_ClockStepsBack_StillStrictlyIncreasing()
|
||||
{
|
||||
var t0 = new DateTimeOffset(2026, 12, 1, 0, 0, 0, TimeSpan.Zero);
|
||||
var clock = new TestTimeProvider(t0);
|
||||
long seq = t0.ToUnixTimeMilliseconds();
|
||||
|
||||
var a = HealthReportSender.NextSequenceNumber(ref seq, clock);
|
||||
clock.Advance(TimeSpan.FromMinutes(-10));
|
||||
var b = HealthReportSender.NextSequenceNumber(ref seq, clock);
|
||||
var c = HealthReportSender.NextSequenceNumber(ref seq, clock);
|
||||
|
||||
Assert.True(b > a);
|
||||
Assert.True(c > b);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// COMM-6 regression: a standby node drains its per-interval counters every tick, so the first
|
||||
/// report after it becomes active does not carry its whole standby life as one interval.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task StandbyTicks_DrainIntervalCounters_SoFirstActiveReportIsOneInterval()
|
||||
{
|
||||
var transport = new FakeTransport();
|
||||
var collector = new SiteHealthCollector();
|
||||
collector.SetActiveNode(false);
|
||||
for (var i = 0; i < 4000; i++) collector.IncrementDeadLetter();
|
||||
collector.IncrementScriptError();
|
||||
|
||||
var sender = new HealthReportSender(
|
||||
collector,
|
||||
transport,
|
||||
Options.Create(new HealthMonitoringOptions { ReportInterval = TimeSpan.FromMilliseconds(30) }),
|
||||
NullLogger<HealthReportSender>.Instance,
|
||||
new FakeSiteIdentityProvider());
|
||||
|
||||
await sender.StartAsync(CancellationToken.None);
|
||||
try
|
||||
{
|
||||
// Let several standby ticks run (30 ms interval), then promote the node.
|
||||
await Task.Delay(300);
|
||||
Assert.Empty(transport.SentReports);
|
||||
|
||||
collector.SetActiveNode(true);
|
||||
await WaitUntilAsync(() => transport.SentReports.Count > 0, TimeSpan.FromSeconds(5));
|
||||
}
|
||||
finally
|
||||
{
|
||||
await sender.StopAsync(CancellationToken.None);
|
||||
}
|
||||
|
||||
var first = transport.SentReports[0];
|
||||
Assert.Equal(0, first.DeadLetterCount);
|
||||
Assert.Equal(0, first.ScriptErrorCount);
|
||||
}
|
||||
|
||||
private static async Task WaitUntilAsync(Func<bool> condition, TimeSpan timeout)
|
||||
{
|
||||
var deadline = DateTime.UtcNow + timeout;
|
||||
while (DateTime.UtcNow < deadline)
|
||||
{
|
||||
if (condition()) return;
|
||||
await Task.Delay(20);
|
||||
}
|
||||
Assert.Fail("Condition not met within timeout");
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -100,7 +100,7 @@ public class CentralHealthAuditBacklogProviderTests
|
||||
States.TryGetValue(siteId, out var state) ? state : null;
|
||||
|
||||
/// <inheritdoc />
|
||||
public void ProcessReport(SiteHealthReport report) => throw new NotSupportedException();
|
||||
public bool ProcessReport(SiteHealthReport report) => throw new NotSupportedException();
|
||||
|
||||
/// <inheritdoc />
|
||||
public void MarkHeartbeat(string siteId, DateTimeOffset receivedAt) => throw new NotSupportedException();
|
||||
|
||||
+1
-1
@@ -265,7 +265,7 @@ public class SiteHealthKpiSampleSourceTests
|
||||
States.TryGetValue(siteId, out var state) ? state : null;
|
||||
|
||||
/// <inheritdoc />
|
||||
public void ProcessReport(SiteHealthReport report) =>
|
||||
public bool ProcessReport(SiteHealthReport report) =>
|
||||
throw new NotSupportedException();
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
Reference in New Issue
Block a user