From 2fdf9d87c2cd364d5ea605418169358fb74845a3 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Tue, 24 Nov 2026 10:44:00 -0500 Subject: [PATCH] =?UTF-8?q?fix(grpc-data):=20A1=20review=20nit=20=E2=80=94?= =?UTF-8?q?=20colliding=20condition=20placements=20are=20logged=20at=20War?= =?UTF-8?q?ning=20(locally)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DistinctPlacements still drops every placement of an address claimed twice (fail closed); Compile takes an optional logger (the coordinator passes its own) and the drop is reported in the local log only. --- .../LocalCatalogCoordinator.cs | 2 +- .../LocalExportCatalog.Compilation.cs | 23 ++++++++++--- .../GrpcDataConditionPlacementTests.cs | 34 +++++++++++++++++++ 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/LocalCatalogCoordinator.cs b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/LocalCatalogCoordinator.cs index 39c29d926..548e0df61 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/LocalCatalogCoordinator.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/LocalCatalogCoordinator.cs @@ -168,7 +168,7 @@ public sealed class LocalCatalogCoordinator && string.Equals(node.ClusterId, snapshot.ClusterId, StringComparison.OrdinalIgnoreCase) && node.Enabled && !node.MaintenanceMode)) return false; var token = _activation.CaptureStagedAuthority(attempt.Epoch, state.AuthorityEpoch); - _staged = LocalExportCatalog.Compile(snapshot, serving, token); + _staged = LocalExportCatalog.Compile(snapshot, serving, token, logger: Logger); if (_staged.DroppedUnknownOperations.Count != 0 && !string.Equals(_droppedExportRevision, attempt.Revision, StringComparison.Ordinal)) dropped = _staged.DroppedUnknownOperations; _droppedExportRevision = attempt.Revision; diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/LocalExportCatalog.Compilation.cs b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/LocalExportCatalog.Compilation.cs index 9b4969bd1..9d6077ce3 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/LocalExportCatalog.Compilation.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/LocalExportCatalog.Compilation.cs @@ -21,7 +21,8 @@ public sealed partial class LocalExportCatalog /// Compile origin views from the deployed snapshot using the same identity and ACL producers as the SDK. public static LocalExportCatalog Compile(DraftSnapshot snapshot, ServingIdentity serving, - IDataMappedCatalogAuthority activation, int maximumNodes = 100_000) + IDataMappedCatalogAuthority activation, int maximumNodes = 100_000, + Microsoft.Extensions.Logging.ILogger? logger = null) { ArgumentNullException.ThrowIfNull(snapshot); serving.Validate(requireEpochs: true); @@ -184,7 +185,7 @@ public sealed partial class LocalExportCatalog kind == DataValueKind.Unspecified ? DataValueKind.Null : kind, supported: kind != DataValueKind.Unspecified, enabled: value.Enabled); } AddConditionPlacements(); - return new(cluster, serving, activation, nodes, snapshot.DataExports, maximumNodes, DistinctPlacements(conditionPlacements)); + return new(cluster, serving, activation, nodes, snapshot.DataExports, maximumNodes, DistinctPlacements(conditionPlacements, logger)); // A1 (§5.2): the UNS placements. Native: the RESERVED reference id Area/Line/Equipment/EffectiveName of every // reference to an alarm-bearing tag — the same derivation as AddressSpaceComposer.BuildUnsReferenceVariables, which @@ -231,8 +232,22 @@ public sealed partial class LocalExportCatalog /// An address claimed by two placements is ambiguous: neither is listed (fail closed for conditions), and the v1 /// catalog compiles exactly as before. /// - private static IReadOnlyList DistinctPlacements(IEnumerable placements) => - placements.GroupBy(x => (x.View.Realm, x.View.Address.Identifier)).Where(x => x.Count() == 1).Select(x => x.Single()).ToArray(); + /// Review nit: the drop is logged at Warning — LOCALLY only (the identifiers may be RawPaths; nothing about + /// the collision reaches a peer, which simply does not see those conditions listed). + private static IReadOnlyList DistinctPlacements(IEnumerable placements, + Microsoft.Extensions.Logging.ILogger? logger) + { + var groups = placements.GroupBy(x => (x.View.Realm, x.View.Address.Identifier)).ToList(); + var colliding = groups.Where(x => x.Count() > 1).ToList(); + if (colliding.Count > 0 && logger is not null) + { + Microsoft.Extensions.Logging.LoggerExtensions.LogWarning(logger, + "Origin catalog: {Count} condition placement address(es) are claimed by more than one placement and are not listed (fail closed): {Addresses}", + colliding.Count, string.Join(", ", colliding.Take(10).Select(g => $"{g.Key.Realm}:{g.Key.Identifier} x{g.Count()}"))); + } + + return groups.Where(x => x.Count() == 1).Select(x => x.Single()).ToArray(); + } private static DataValueKind ValueKind(string type) => type switch { diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataConditionPlacementTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataConditionPlacementTests.cs index 2402ebfb3..b6b0bb006 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataConditionPlacementTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataConditionPlacementTests.cs @@ -101,6 +101,40 @@ public sealed class GrpcDataConditionPlacementTests catalog.TryResolveConditionPlacement(placements[2].View with { MappingId = "m", MappingRevision = 1 }, out _).ShouldBeFalse(); } + /// Review nit: an address claimed by two placements drops both (fail closed) and says so at Warning in the + /// LOCAL log; the listing simply does not carry them. + [Fact] + public void Colliding_placements_are_dropped_and_logged_at_Warning() + { + using var fixture = new LocalDataFixture(); + var b = Fixed(); + var clash = new ScriptedAlarm + { + ScriptedAlarmId = "Area/Line/Filler/OverTemp", EquipmentId = "eq-1", Name = "Clash", AlarmType = "AlarmCondition", + MessageTemplate = "x", PredicateScriptId = "script", + }; + var log = new CapturingLogger(); + + var catalog = LocalExportCatalog.Compile(With(b, scripted: [.. b.ScriptedAlarms, clash]), fixture.Serving, + fixture.Activation.CaptureAuthority(), logger: log); + + catalog.ConditionPlacements.ShouldNotContain(p => p.View.Address.Identifier == "Area/Line/Filler/OverTemp"); + catalog.ConditionPlacements.Count.ShouldBe(3); + log.Warnings.ShouldHaveSingleItem().ShouldContain("Area/Line/Filler/OverTemp"); + } + + private sealed class CapturingLogger : Microsoft.Extensions.Logging.ILogger + { + public List Warnings { get; } = []; + public IDisposable? BeginScope(TState state) where TState : notnull => null; + public bool IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel) => true; + public void Log(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, TState state, + Exception? exception, Func formatter) + { + if (logLevel == Microsoft.Extensions.Logging.LogLevel.Warning) Warnings.Add(formatter(state, exception)); + } + } + [Fact] public void The_v1_catalog_is_unchanged_by_the_enumeration() {