f2efeb37b7
Tasks 5 and 6 of the Phase 2 plan, committed together because their test
fallout is entangled — several fixtures construct both stores.
StoreAndForwardStorage and SiteStorageService now take ILocalDb. Connections
come from ILocalDb.CreateConnection(), which hands out an already-open,
pragma-configured connection carrying the zb_hlc_next() UDF the capture triggers
call; a raw connection would lack the UDF and every write to a replicated table
would fail closed. Deleted with the connection strings: S&F's
EnsureDatabaseDirectoryExists and its per-open busy_timeout pragma, and the site
service's BusyTimeoutFloorSeconds normalization — LocalDb owns all of it now.
DI: AddSiteRuntime's string overload is gone (nothing left to supply), so the
Host calls the no-arg form. ScadaBridge:Database:SiteDbPath and
StoreAndForwardOptions.SqliteDbPath survive only as the migrator's source
locations in Tasks 8/9.
Two things the plan did not anticipate, both worth reading:
1. FOUND A REAL LATENT DEFECT, from Phase 1, now fixed. The plan assumed
directory creation simply moved to LocalDb along with file ownership. It did
not: the LocalDb library never creates the parent directory, and
SqliteLocalDb opens the file eagerly in its constructor — so a missing
directory is a hard boot failure ("SQLite Error 14: unable to open database
file"), not a degraded start. The default site config points at the RELATIVE
path ./data/site-localdb.db, so any site node without a pre-existing data/
directory fails to boot. The docker rig escapes only because its volume mount
happens to create /app/data — a coincidence that would have hidden this until
a bare-metal or fresh deployment. This has been latent since Phase 1 made
LocalDb:Path required; deleting S&F's EnsureDatabaseDirectoryExists here
would have widened it. Re-established the guarantee at the layer that now
owns the path (SiteLocalDbDirectory.Ensure, called before AddZbLocalDb) and
pinned it with SiteLocalDbDirectoryTests. Non-vacuity is not assumed: two
tests written against the wrong assumption failed with exactly this
SQLite Error 14 before the fix existed.
2. Test fallout was ~7x the plan's estimate. The plan named "fixtures" in one
project; the constructor change actually reaches 40 files across 7 test
projects, and most used Mode=Memory;Cache=Shared — which LocalDb has no
equivalent for, so every one had to move to a real temp file. Rather than
copy the Phase 1 TestLocalDb fixture into 7 projects, added a shared
tests/ZB.MOM.WW.ScadaBridge.TestSupport library (not a test project) so the
WAL-sidecar cleanup and the "real, not stubbed" rationale live in one place.
Retargeted rather than deleted, in both directions: the S&F WAL test now asserts
against the LocalDb-backed store (WAL genuinely is LocalDb's job), while the
directory-creation test moved to Host.Tests (that guarantee is NOT LocalDb's).
SiteStorageServiceTests.Initialize_EnablesWalJournalMode got the same treatment.
DeploymentManagerMediumFindingsTests induced a persistence failure via an
unopenable path, which no longer reaches the assertion since the fixture now
throws first; it induces the same failure shape via an uninitialized store.
Verified: full solution build 0 warnings; SiteRuntime 532, Host 318,
AuditLog 355, ExternalSystemGateway 142, HealthMonitoring 97,
StoreAndForward 153 — 1597 passed, 0 failed.
Claude-Session: https://claude.ai/code/session_01BL2Vu1ESDQ9SCN4gVKkdts
234 lines
9.6 KiB
C#
234 lines
9.6 KiB
C#
using Microsoft.Extensions.Logging.Abstractions;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
|
|
using ZB.MOM.WW.ScadaBridge.StoreAndForward;
|
|
using ZB.MOM.WW.ScadaBridge.TestSupport;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.IntegrationTests;
|
|
|
|
/// <summary>
|
|
/// WP-3 (Phase 8): Dual-node failure recovery.
|
|
/// Both nodes down, first up forms cluster, rebuilds from persistent storage.
|
|
/// Tests for both central and site topologies.
|
|
/// </summary>
|
|
public class DualNodeRecoveryTests
|
|
{
|
|
[Trait("Category", "Integration")]
|
|
[Fact]
|
|
public async Task SiteTopology_BothNodesDown_FirstNodeRebuildsFromSQLite()
|
|
{
|
|
// Scenario: both site nodes crash. First node to restart opens the existing
|
|
// SQLite database and finds all buffered S&F messages intact.
|
|
var dbPath = Path.Combine(Path.GetTempPath(), $"sf_dual_{Guid.NewGuid():N}.db");
|
|
|
|
// The store takes an ILocalDb; the "restarted node" opens its own local database
|
|
// over the SAME file, which is how this test models recovery from disk.
|
|
TestLocalDb? crashedDb = null;
|
|
TestLocalDb? recoveryDb = null;
|
|
|
|
try
|
|
{
|
|
// Setup: populate SQLite with messages (simulating pre-crash state)
|
|
crashedDb = TestLocalDb.Create(dbPath);
|
|
var storage = new StoreAndForwardStorage(crashedDb.Db, NullLogger<StoreAndForwardStorage>.Instance);
|
|
await storage.InitializeAsync();
|
|
|
|
var messageIds = new List<string>();
|
|
for (var i = 0; i < 10; i++)
|
|
{
|
|
var msg = new StoreAndForwardMessage
|
|
{
|
|
Id = Guid.NewGuid().ToString("N"),
|
|
Category = StoreAndForwardCategory.ExternalSystem,
|
|
Target = $"api-{i % 3}",
|
|
PayloadJson = $$"""{"index":{{i}}}""",
|
|
RetryCount = i,
|
|
MaxRetries = 50,
|
|
RetryIntervalMs = 30000,
|
|
CreatedAt = DateTimeOffset.UtcNow.AddMinutes(-i),
|
|
Status = StoreAndForwardMessageStatus.Pending,
|
|
OriginInstanceName = $"instance-{i % 2}"
|
|
};
|
|
await storage.EnqueueAsync(msg);
|
|
messageIds.Add(msg.Id);
|
|
}
|
|
|
|
// Both nodes down — simulate by creating a fresh storage instance
|
|
// (new process connecting to same SQLite file)
|
|
recoveryDb = TestLocalDb.Create(dbPath);
|
|
var recoveryStorage = new StoreAndForwardStorage(recoveryDb.Db, NullLogger<StoreAndForwardStorage>.Instance);
|
|
await recoveryStorage.InitializeAsync();
|
|
|
|
// Verify all messages are available for retry
|
|
var pending = await recoveryStorage.GetMessagesForRetryAsync();
|
|
Assert.Equal(10, pending.Count);
|
|
|
|
// Verify messages are ordered by creation time (oldest first)
|
|
for (var i = 1; i < pending.Count; i++)
|
|
{
|
|
Assert.True(pending[i].CreatedAt >= pending[i - 1].CreatedAt);
|
|
}
|
|
|
|
// Verify per-instance message counts
|
|
var instance0Count = await recoveryStorage.GetMessageCountByOriginInstanceAsync("instance-0");
|
|
var instance1Count = await recoveryStorage.GetMessageCountByOriginInstanceAsync("instance-1");
|
|
Assert.Equal(5, instance0Count);
|
|
Assert.Equal(5, instance1Count);
|
|
}
|
|
finally
|
|
{
|
|
// Dispose before deleting — the master connections anchor the WAL sidecars.
|
|
recoveryDb?.Dispose();
|
|
crashedDb?.Dispose();
|
|
TestLocalDb.DeleteFiles(dbPath);
|
|
}
|
|
}
|
|
|
|
[Trait("Category", "Integration")]
|
|
[Fact]
|
|
public async Task SiteTopology_DualCrash_ParkedMessagesPreserved()
|
|
{
|
|
var dbPath = Path.Combine(Path.GetTempPath(), $"sf_dual_parked_{Guid.NewGuid():N}.db");
|
|
|
|
TestLocalDb? crashedDb = null;
|
|
TestLocalDb? recoveryDb = null;
|
|
|
|
try
|
|
{
|
|
crashedDb = TestLocalDb.Create(dbPath);
|
|
var storage = new StoreAndForwardStorage(crashedDb.Db, NullLogger<StoreAndForwardStorage>.Instance);
|
|
await storage.InitializeAsync();
|
|
|
|
// Mix of pending and parked messages
|
|
await storage.EnqueueAsync(new StoreAndForwardMessage
|
|
{
|
|
Id = "pending-1",
|
|
Category = StoreAndForwardCategory.ExternalSystem,
|
|
Target = "api",
|
|
PayloadJson = "{}",
|
|
MaxRetries = 50,
|
|
RetryIntervalMs = 30000,
|
|
CreatedAt = DateTimeOffset.UtcNow,
|
|
Status = StoreAndForwardMessageStatus.Pending,
|
|
});
|
|
|
|
await storage.EnqueueAsync(new StoreAndForwardMessage
|
|
{
|
|
Id = "parked-1",
|
|
Category = StoreAndForwardCategory.Notification,
|
|
Target = "alerts",
|
|
PayloadJson = "{}",
|
|
MaxRetries = 3,
|
|
RetryIntervalMs = 10000,
|
|
CreatedAt = DateTimeOffset.UtcNow.AddHours(-2),
|
|
RetryCount = 3,
|
|
Status = StoreAndForwardMessageStatus.Parked,
|
|
LastError = "SMTP unreachable"
|
|
});
|
|
|
|
// Dual crash recovery
|
|
recoveryDb = TestLocalDb.Create(dbPath);
|
|
var recoveryStorage = new StoreAndForwardStorage(recoveryDb.Db, NullLogger<StoreAndForwardStorage>.Instance);
|
|
await recoveryStorage.InitializeAsync();
|
|
|
|
var pendingCount = await recoveryStorage.GetMessageCountByStatusAsync(StoreAndForwardMessageStatus.Pending);
|
|
var parkedCount = await recoveryStorage.GetMessageCountByStatusAsync(StoreAndForwardMessageStatus.Parked);
|
|
|
|
Assert.Equal(1, pendingCount);
|
|
Assert.Equal(1, parkedCount);
|
|
|
|
// Parked message can be retried after recovery
|
|
var success = await recoveryStorage.RetryParkedMessageAsync("parked-1");
|
|
Assert.True(success);
|
|
|
|
pendingCount = await recoveryStorage.GetMessageCountByStatusAsync(StoreAndForwardMessageStatus.Pending);
|
|
parkedCount = await recoveryStorage.GetMessageCountByStatusAsync(StoreAndForwardMessageStatus.Parked);
|
|
Assert.Equal(2, pendingCount);
|
|
Assert.Equal(0, parkedCount);
|
|
}
|
|
finally
|
|
{
|
|
// Dispose before deleting — the master connections anchor the WAL sidecars.
|
|
recoveryDb?.Dispose();
|
|
crashedDb?.Dispose();
|
|
TestLocalDb.DeleteFiles(dbPath);
|
|
}
|
|
}
|
|
|
|
[Trait("Category", "Integration")]
|
|
[Fact]
|
|
public void CentralTopology_BothNodesDown_FirstNodeFormsSingleNodeCluster()
|
|
{
|
|
// Structural verification: Akka.NET cluster config uses min-nr-of-members = 1,
|
|
// so a single node can form a cluster. The keep-oldest split-brain resolver
|
|
// with down-if-alone handles the partition scenario.
|
|
//
|
|
// When both central nodes crash, the first node to restart:
|
|
// 1. Forms a single-node cluster (min-nr-of-members = 1)
|
|
// 2. Connects to SQL Server (which persists all deployment state)
|
|
// 3. Becomes the active node and accepts traffic
|
|
//
|
|
// The second node joins the existing cluster when it starts.
|
|
|
|
// Verify the deployment status model supports recovery from SQL Server
|
|
var statuses = new[]
|
|
{
|
|
new Commons.Messages.Deployment.DeploymentStatusResponse(
|
|
"dep-1", "inst-1", Commons.Types.Enums.DeploymentStatus.Success,
|
|
null, DateTimeOffset.UtcNow),
|
|
new Commons.Messages.Deployment.DeploymentStatusResponse(
|
|
"dep-1", "inst-2", Commons.Types.Enums.DeploymentStatus.InProgress,
|
|
null, DateTimeOffset.UtcNow),
|
|
};
|
|
|
|
// Each instance has independent status — recovery reads from DB
|
|
Assert.Equal(DeploymentStatus.Success, statuses[0].Status);
|
|
Assert.Equal(DeploymentStatus.InProgress, statuses[1].Status);
|
|
}
|
|
|
|
[Trait("Category", "Integration")]
|
|
[Fact]
|
|
public async Task SQLiteStorage_InitializeIdempotent_SafeOnRecovery()
|
|
{
|
|
// CREATE TABLE IF NOT EXISTS is idempotent — safe to call on recovery
|
|
var dbPath = Path.Combine(Path.GetTempPath(), $"sf_idempotent_{Guid.NewGuid():N}.db");
|
|
|
|
TestLocalDb? localDb1 = null;
|
|
TestLocalDb? localDb2 = null;
|
|
|
|
try
|
|
{
|
|
localDb1 = TestLocalDb.Create(dbPath);
|
|
var storage1 = new StoreAndForwardStorage(localDb1.Db, NullLogger<StoreAndForwardStorage>.Instance);
|
|
await storage1.InitializeAsync();
|
|
|
|
await storage1.EnqueueAsync(new StoreAndForwardMessage
|
|
{
|
|
Id = "test-1",
|
|
Category = StoreAndForwardCategory.ExternalSystem,
|
|
Target = "api",
|
|
PayloadJson = "{}",
|
|
MaxRetries = 50,
|
|
RetryIntervalMs = 30000,
|
|
CreatedAt = DateTimeOffset.UtcNow,
|
|
Status = StoreAndForwardMessageStatus.Pending,
|
|
});
|
|
|
|
// Second InitializeAsync on same DB should be safe (no data loss)
|
|
localDb2 = TestLocalDb.Create(dbPath);
|
|
var storage2 = new StoreAndForwardStorage(localDb2.Db, NullLogger<StoreAndForwardStorage>.Instance);
|
|
await storage2.InitializeAsync();
|
|
|
|
var msg = await storage2.GetMessageByIdAsync("test-1");
|
|
Assert.NotNull(msg);
|
|
Assert.Equal("api", msg!.Target);
|
|
}
|
|
finally
|
|
{
|
|
// Dispose before deleting — the master connections anchor the WAL sidecars.
|
|
localDb2?.Dispose();
|
|
localDb1?.Dispose();
|
|
TestLocalDb.DeleteFiles(dbPath);
|
|
}
|
|
}
|
|
}
|