Closes Stream D per docs/v2/implementation/phase-6-1-resilience-and-observability.md. New Configuration.LocalCache types (alongside the existing single-file LiteDbConfigCache): - GenerationSealedCache — file-per-generation sealed snapshots per decision #148. Each SealAsync writes <cache-root>/<clusterId>/<generationId>.db as a read-only LiteDB file, then atomically publishes the CURRENT pointer via temp-file + File.Replace. Prior-generation files stay on disk for audit. Mixed-generation reads are structurally impossible: ReadCurrentAsync opens the single file named by CURRENT. Corruption of the pointer or the sealed file raises GenerationCacheUnavailableException — fails closed, never falls back silently to an older generation. TryGetCurrentGenerationId returns the pointer value or null for diagnostics. - StaleConfigFlag — thread-safe (Volatile.Read/Write) bool. MarkStale when a read fell back to the cache; MarkFresh when a central-DB read succeeded. Surfaced on /healthz body and Admin /hosts (Stream C wiring already in place). - ResilientConfigReader — wraps a central-DB fetch function with the Stream D.2 pipeline: timeout 2 s → retry N× jittered (skipped when retryCount=0) → fallback to the sealed cache. Toggles StaleConfigFlag per outcome. Read path only — the write path is expected to bypass this wrapper and fail hard on DB outage so inconsistent writes never land. Cancellation passes through and is NOT retried. Configuration.csproj: - Polly.Core 8.6.6 + Microsoft.Extensions.Logging.Abstractions added. Tests (17 new, all pass): - GenerationSealedCacheTests (10): first-boot-no-snapshot throws GenerationCacheUnavailableException (D.4 scenario C), seal-then-read round trip, sealed file is ReadOnly on disk, pointer advances to latest, prior generation file preserved, corrupt sealed file fails closed, missing sealed file fails closed, corrupt pointer fails closed (D.4 scenario B), same generation sealed twice is idempotent, independent clusters don't interfere. - ResilientConfigReaderTests (4): central-DB success returns value + marks fresh; central-DB failure exhausts retries + falls back to cache + marks stale (D.4 scenario A); central-DB + cache both unavailable throws; cancellation not retried. - StaleConfigFlagTests (3): default is fresh; toggles; concurrent writes converge. Full solution dotnet test: 1033 passing (baseline 906, +127 net across Phase 6.1 Streams A/B/C/D). Pre-existing Client.CLI Subscribe flake unchanged. Integration into Configuration read paths (DriverInstance enumeration, LdapGroupRoleMapping fetches, etc.) + the sp_PublishGeneration hook that writes sealed files lands in the Phase 6.1 Stream E / Admin-refresh PR where the DB integration surfaces are already touched. Existing LiteDbConfigCache continues serving its single-file role for the NodeBootstrap path. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
155 lines
4.9 KiB
C#
155 lines
4.9 KiB
C#
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.LocalCache;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Configuration.Tests;
|
|
|
|
[Trait("Category", "Unit")]
|
|
public sealed class ResilientConfigReaderTests : IDisposable
|
|
{
|
|
private readonly string _root = Path.Combine(Path.GetTempPath(), $"otopcua-reader-{Guid.NewGuid():N}");
|
|
|
|
public void Dispose()
|
|
{
|
|
try
|
|
{
|
|
if (!Directory.Exists(_root)) return;
|
|
foreach (var f in Directory.EnumerateFiles(_root, "*", SearchOption.AllDirectories))
|
|
File.SetAttributes(f, FileAttributes.Normal);
|
|
Directory.Delete(_root, recursive: true);
|
|
}
|
|
catch { /* best-effort */ }
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CentralDbSucceeds_ReturnsValue_MarksFresh()
|
|
{
|
|
var cache = new GenerationSealedCache(_root);
|
|
var flag = new StaleConfigFlag { };
|
|
flag.MarkStale(); // pre-existing stale state
|
|
var reader = new ResilientConfigReader(cache, flag, NullLogger<ResilientConfigReader>.Instance);
|
|
|
|
var result = await reader.ReadAsync(
|
|
"cluster-a",
|
|
_ => ValueTask.FromResult("fresh-from-db"),
|
|
_ => "from-cache",
|
|
CancellationToken.None);
|
|
|
|
result.ShouldBe("fresh-from-db");
|
|
flag.IsStale.ShouldBeFalse("successful central-DB read clears stale flag");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CentralDbFails_ExhaustsRetries_FallsBackToCache_MarksStale()
|
|
{
|
|
var cache = new GenerationSealedCache(_root);
|
|
await cache.SealAsync(new GenerationSnapshot
|
|
{
|
|
ClusterId = "cluster-a", GenerationId = 99, CachedAt = DateTime.UtcNow,
|
|
PayloadJson = "{\"cached\":true}",
|
|
});
|
|
var flag = new StaleConfigFlag();
|
|
var reader = new ResilientConfigReader(cache, flag, NullLogger<ResilientConfigReader>.Instance,
|
|
timeout: TimeSpan.FromSeconds(10), retryCount: 2);
|
|
var attempts = 0;
|
|
|
|
var result = await reader.ReadAsync(
|
|
"cluster-a",
|
|
_ =>
|
|
{
|
|
attempts++;
|
|
throw new InvalidOperationException("SQL dead");
|
|
#pragma warning disable CS0162
|
|
return ValueTask.FromResult("never");
|
|
#pragma warning restore CS0162
|
|
},
|
|
snap => snap.PayloadJson,
|
|
CancellationToken.None);
|
|
|
|
attempts.ShouldBe(3, "1 initial + 2 retries = 3 attempts");
|
|
result.ShouldBe("{\"cached\":true}");
|
|
flag.IsStale.ShouldBeTrue("cache fallback flips stale flag true");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CentralDbFails_AndCacheAlsoUnavailable_Throws()
|
|
{
|
|
var cache = new GenerationSealedCache(_root);
|
|
var flag = new StaleConfigFlag();
|
|
var reader = new ResilientConfigReader(cache, flag, NullLogger<ResilientConfigReader>.Instance,
|
|
timeout: TimeSpan.FromSeconds(10), retryCount: 0);
|
|
|
|
await Should.ThrowAsync<GenerationCacheUnavailableException>(async () =>
|
|
{
|
|
await reader.ReadAsync<string>(
|
|
"cluster-a",
|
|
_ => throw new InvalidOperationException("SQL dead"),
|
|
_ => "never",
|
|
CancellationToken.None);
|
|
});
|
|
|
|
flag.IsStale.ShouldBeFalse("no snapshot ever served, so flag stays whatever it was");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Cancellation_NotRetried()
|
|
{
|
|
var cache = new GenerationSealedCache(_root);
|
|
var flag = new StaleConfigFlag();
|
|
var reader = new ResilientConfigReader(cache, flag, NullLogger<ResilientConfigReader>.Instance,
|
|
timeout: TimeSpan.FromSeconds(10), retryCount: 5);
|
|
using var cts = new CancellationTokenSource();
|
|
cts.Cancel();
|
|
var attempts = 0;
|
|
|
|
await Should.ThrowAsync<OperationCanceledException>(async () =>
|
|
{
|
|
await reader.ReadAsync<string>(
|
|
"cluster-a",
|
|
ct =>
|
|
{
|
|
attempts++;
|
|
ct.ThrowIfCancellationRequested();
|
|
return ValueTask.FromResult("ok");
|
|
},
|
|
_ => "cache",
|
|
cts.Token);
|
|
});
|
|
|
|
attempts.ShouldBeLessThanOrEqualTo(1);
|
|
}
|
|
}
|
|
|
|
[Trait("Category", "Unit")]
|
|
public sealed class StaleConfigFlagTests
|
|
{
|
|
[Fact]
|
|
public void Default_IsFresh()
|
|
{
|
|
new StaleConfigFlag().IsStale.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void MarkStale_ThenFresh_Toggles()
|
|
{
|
|
var flag = new StaleConfigFlag();
|
|
flag.MarkStale();
|
|
flag.IsStale.ShouldBeTrue();
|
|
flag.MarkFresh();
|
|
flag.IsStale.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void ConcurrentWrites_Converge()
|
|
{
|
|
var flag = new StaleConfigFlag();
|
|
Parallel.For(0, 1000, i =>
|
|
{
|
|
if (i % 2 == 0) flag.MarkStale(); else flag.MarkFresh();
|
|
});
|
|
flag.MarkFresh();
|
|
flag.IsStale.ShouldBeFalse();
|
|
}
|
|
}
|