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
@@ -7,6 +7,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;
@@ -28,6 +29,11 @@ public class ApiMethodCompileDiagnosticsTests : TestKit
private readonly InboundScriptExecutor _executor =
new(NullLogger<InboundScriptExecutor>.Instance, Substitute.For<IServiceProvider>());
private readonly ApiMethod _method = new("Broken", BrokenScript) { Id = 5, TimeoutSeconds = 10 };
// The other central node, reached only through the peer invalidation (ScadaBridge#103).
private readonly InboundScriptExecutor _peerExecutor =
new(NullLogger<InboundScriptExecutor>.Instance, Substitute.For<IServiceProvider>());
private readonly ApiMethodCache _peerMethodCache = new(TimeSpan.FromHours(1));
private readonly PeerCacheInvalidationPublisher _publisher;
/// <summary>Initializes the harness with a real executor and a stubbed repository.</summary>
public ApiMethodCompileDiagnosticsTests()
@@ -35,6 +41,9 @@ public class ApiMethodCompileDiagnosticsTests : TestKit
_services.AddScoped(_ => _apiRepo);
_services.AddScoped(_ => _audit);
_services.AddSingleton(_executor);
_publisher = new PeerCacheInvalidationPublisher(
new InboundApiCacheInvalidationHandler(_peerExecutor, _peerMethodCache));
_services.AddSingleton<ICentralCacheInvalidationPublisher>(_publisher);
_apiRepo.GetApiMethodByIdAsync(5, Arg.Any<CancellationToken>()).Returns(_method);
_apiRepo.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(1);
}
@@ -116,6 +125,41 @@ public class ApiMethodCompileDiagnosticsTests : TestKit
Assert.Contains("doesNotExist", state.LastCompileError);
}
/// <summary>
/// ScadaBridge#103: compile state is per node, so a recompile used to clear the known-bad
/// record only on the node that answered — the peer kept refusing the unchanged script with
/// SCRIPT_COMPILE_FAILED until it restarted. The recompile now drops the peer's record (and
/// its cached row), so the peer compiles the current text on its next request.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Recompile_ClearsTheKnownBadRecordOnThePeerNode()
{
Assert.False(_peerExecutor.CompileAndRegister(_method));
Assert.True(_peerExecutor.GetCompileState("Broken", BrokenScript).IsKnownBad); // positive control
await _peerMethodCache.GetOrFetchAsync("Broken", _ => Task.FromResult<ApiMethod?>(_method), CancellationToken.None);
Send(new RecompileApiMethodCommand(5));
Assert.False(_peerExecutor.GetCompileState("Broken", BrokenScript).IsKnownBad);
var refetched = false;
await _peerMethodCache.GetOrFetchAsync("Broken", _ => { refetched = true; return Task.FromResult<ApiMethod?>(_method); }, CancellationToken.None);
Assert.True(refetched);
Assert.Equal(new[] { new InboundApiMethodInvalidated("Broken") }, _publisher.Published);
}
/// <summary>Negative control: validate changes nothing anywhere, so it tells the peer nothing.</summary>
[Fact]
public void Validate_DoesNotTouchThePeer()
{
Assert.False(_peerExecutor.CompileAndRegister(_method));
Send(new ValidateApiMethodCommand(5));
Assert.True(_peerExecutor.GetCompileState("Broken", BrokenScript).IsKnownBad);
Assert.Empty(_publisher.Published);
}
/// <summary>Validate and recompile are Designer operations, like UpdateApiMethod.</summary>
/// <param name="command">The command name to send.</param>
[Theory]