fix(inbound-api): API-method recompile/invalidate reaches the peer central node; stale doc; locked-override name rule (ScadaBridge#103)

(a) Inbound API compile state (#75) is per central node: a recompile cleared the
known-bad record only on the node that answered, and the peer kept refusing the
unchanged script with SCRIPT_COMPILE_FAILED (the per-request revision check only
notices a changed script). Create / update / delete (ManagementActor post-commit),
RecompileApiMethod, and API methods written by a Transport bundle import now publish
InboundApiMethodInvalidated on the central cache-invalidation topic (#100 item 3).
The new InboundApiCacheInvalidationHandler drops that method's compiled handler,
known-bad record and cached ApiMethod row on the peer, which compiles the current
text on its next request.

(b) docs/components/InboundAPI.md no longer claims a direct DB edit stays stale
until restart: the per-request revision check picks it up once the node's cached
row expires (ApiMethodCacheTtl, 5 s). Component-InboundAPI, Component-Host and the
#85 cache audit describe the peer invalidation.

(c) Decision: the import's locked-override warnings keep EXACT (ordinal) name
matching, because FlatteningService matches override names ordinally. An override
differing from a locked member only by case never reaches that member's lock check
(the flattener drops it as unresolved), so a "targets a LOCKED member" warning for it
would be false. The comparer is now named (LockedOverrideNameComparer) and documented,
and pinned by tests on both sides (flattener + import).

Tests: the peer's known-bad record is cleared by a recompile (and validate leaves it
alone); create/update/delete and the import publish the invalidation; the handler
drops only the named method; composition root registers the handler; case-only
override gets no lock warning; the flattener matches override names exactly.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 16:04:07 -05:00
parent 153609ef8a
commit 0c2e852a62
16 changed files with 304 additions and 24 deletions
@@ -6,6 +6,7 @@ using NSubstitute;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.InboundApi;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Communication;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;
using ZB.MOM.WW.ScadaBridge.InboundAPI;
@@ -24,6 +25,7 @@ public class ApiMethodCacheInvalidationTests : TestKit
// A TTL far longer than the test, so only an explicit invalidation can clear an entry.
private readonly ApiMethodCache _cache = new(TimeSpan.FromHours(1));
private readonly PeerCacheInvalidationPublisher _publisher = new();
/// <summary>Initializes the harness with a long-TTL method cache.</summary>
public ApiMethodCacheInvalidationTests()
@@ -31,6 +33,7 @@ public class ApiMethodCacheInvalidationTests : TestKit
_services.AddScoped(_ => _apiRepo);
_services.AddScoped(_ => Substitute.For<IAuditService>());
_services.AddSingleton(_cache);
_services.AddSingleton<ICentralCacheInvalidationPublisher>(_publisher);
_apiRepo.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(1);
}
@@ -111,4 +114,31 @@ public class ApiMethodCacheInvalidationTests : TestKit
Assert.False(await Refetches("Other"));
}
/// <summary>
/// ScadaBridge#103: create, update and delete tell the peer central node to drop the method's
/// handler, known-bad record and cached row, once each and after commit.
/// </summary>
[Fact]
public void CreateUpdateDelete_TellThePeer()
{
var existing = new ApiMethod("Peer", "return 1;") { Id = 11, TimeoutSeconds = 10 };
_apiRepo.GetApiMethodByIdAsync(11, Arg.Any<CancellationToken>()).Returns(existing);
var actor = CreateActor();
foreach (var command in new object[]
{
new CreateApiMethodCommand("Peer", "return 1;", 30, null, null),
new UpdateApiMethodCommand(11, "return 2;", 10, null, null),
new DeleteApiMethodCommand(11),
})
{
actor.Tell(Envelope(command));
ExpectMsg<ManagementSuccess>(TimeSpan.FromSeconds(30));
}
Assert.Equal(
Enumerable.Repeat<ICentralCacheInvalidation>(new InboundApiMethodInvalidated("Peer"), 3),
_publisher.Published);
}
}