fix(historian): refuse aggregates not valid for the node's DataType up front (#897)
v2-ci / unit-tests (push) Has been cancelled
v2-ci / unit-tests (push) Has been cancelled
TimeAverage / Total on a Boolean node over the Gateway backend (AVEVA discrete tag, live SysPulse) went to the gateway since native TimeAverage/Total became default-on (#694). AVEVA refuses both modes at StartQuery; the gateway reports that only as Internal / HistorianServerOperationException (the same answer as a historian storage fault), so the node answered BadHistoryOperationUnsupported and the read counted as a backend failure. HistoryRead-Processed now checks the node's scalar DataType before calling any backend and answers BadAggregateNotSupported (Part 11 6.5.4: "If the Aggregate is not valid for the Node then the StatusCode shall be Bad_AggregateNotSupported"). Nothing is sent, nothing is counted. - Shared, every backend: HistoryAggregateDataTypes (Core.Abstractions) applies Part 13 5.4.2.3 Table 50: the numeric-only aggregates (Average, TimeAverage(2), Total(2), Min/Max, ActualTime, Range, Delta, DeltaBounds, Interpolative, stddev/variance) on a String or DateTime node are refused. Client-visible: the SQL backends used to answer every interval BadAggregateInvalidInputs. - Per backend: IHistorianDataSource.SupportsAggregateFor(aggregate, TypeCode?) (default true; every production backend declares it, guard-tested). Gateway: false for native TimeAverage/Total on a Boolean node. SqlServer/QuestDb: true, since they serve Boolean as 0/1 (stepped), so Boolean stays served there, as do the Gateway's raw-evaluated aggregates. The residual case (a node declared numeric whose AVEVA tag is discrete) cannot be told apart from a historian fault on the wire and still counts; that needs the gateway to surface AVEVA's native error code as a deterministic status. Tests: HistoryAggregateDataTypesTests, guard test, node-manager String/DateTime and per-type refusals, Gateway capability matrix, DiscreteTagTimeWeightedWireTests (production Gateway backend over an AVEVA-shaped fake: unsent, 0 health failures, positive controls). Each mutation (no check, no shared rule, no backend hint, Gateway hint true, shared rule always true, SQL inheriting the default) fails at least one. Live gate re-run read-only against AVEVA 2023 R2 via a private HistorianGateway fa9cf31 container: SysPulse TimeAverage on the wire = BadAggregateNotSupported, health failures unchanged; #801 gate 244 cases pass. Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
This commit is contained in:
+7
-3
@@ -270,7 +270,9 @@ public sealed class AvevaGatewayHistorianFollowupsLiveGateTests
|
||||
})
|
||||
{
|
||||
var intervalMs = (end - start).TotalMilliseconds / 2;
|
||||
var failuresBefore = source.GetHealthSnapshot().TotalFailures;
|
||||
var p = await ReadProcessedAsync(session, Node(node), start, end, intervalMs, aggId, ct);
|
||||
var failuresAfter = source.GetHealthSnapshot().TotalFailures;
|
||||
ZB.MOM.WW.OtOpcUa.Core.Abstractions.HistoryReadResult? d = null;
|
||||
string? directError = null;
|
||||
try { d = await source.ReadProcessedAsync(tag, start, end, TimeSpan.FromMilliseconds(intervalMs), aggType, ct); }
|
||||
@@ -279,10 +281,12 @@ public sealed class AvevaGatewayHistorianFollowupsLiveGateTests
|
||||
$"{Types(p.Rows)} direct={directError ?? "ok"}", p.Rows));
|
||||
if (label == "boolean" && aggType == HistoryAggregateType.TimeAverage)
|
||||
{
|
||||
// AVEVA refuses its native time-weighted modes on a discrete tag (StartQuery fails): recorded; the node
|
||||
// must answer Bad, never a value.
|
||||
Check(failures, p.Status.StartsWith("Bad", StringComparison.Ordinal) && p.Rows.Count == 0,
|
||||
// AVEVA refuses its native time-weighted modes on a discrete tag (StartQuery fails — the direct read
|
||||
// records it). Since #897 the node is refused BadAggregateNotSupported up front: nothing sent, no failure.
|
||||
Check(failures, p.Status == "BadAggregateNotSupported" && p.Rows.Count == 0,
|
||||
$"700.existing.processed.TimeAverage.boolean: {p.Status} rows={p.Rows.Count} (direct: {directError ?? "served"})");
|
||||
Check(failures, failuresAfter == failuresBefore,
|
||||
$"700.existing.processed.TimeAverage.boolean: the wire read counted {failuresAfter - failuresBefore} backend failure(s)");
|
||||
continue;
|
||||
}
|
||||
if (d is null) { failures.Add($"700.existing.processed.{aggName}.{label}: direct read failed: {directError}"); continue; }
|
||||
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using Google.Protobuf.WellKnownTypes;
|
||||
using RpcException = Grpc.Core.RpcException;
|
||||
using RpcStatus = Grpc.Core.Status;
|
||||
using RpcStatusCode = Grpc.Core.StatusCode;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Opc.Ua;
|
||||
using Opc.Ua.Client;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.HistorianGateway.Contracts.Grpc;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
||||
using ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway;
|
||||
using static ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests.ServerEventRig;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests;
|
||||
|
||||
/// <summary>
|
||||
/// lmxopcua#897 over a real SDK session, with the production Gateway backend over a deterministic AVEVA-shaped gateway:
|
||||
/// TimeAverage / Total on a Boolean node (AVEVA discrete tag, live <c>SysPulse</c>) are refused
|
||||
/// <c>BadAggregateNotSupported</c> up front — nothing is sent to the gateway and the historian health snapshot counts no
|
||||
/// failure — where AVEVA's refusal (gateway <c>Internal</c> / <c>HistorianServerOperationException</c>, the same
|
||||
/// answer as a historian storage fault) used to come back <c>BadHistoryOperationUnsupported</c> and count as a backend
|
||||
/// failure. A numeric node keeps the native pair, and the Boolean node keeps the raw-evaluated Average.
|
||||
/// </summary>
|
||||
public sealed class DiscreteTagTimeWeightedWireTests
|
||||
{
|
||||
private const string Discrete = "SysPulse";
|
||||
private const string Analog = "SysTimeSec";
|
||||
private static readonly DateTime S0 = new(2026, 11, 11, 14, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
[Fact]
|
||||
public async Task TimeAverage_and_Total_on_a_Boolean_node_are_refused_up_front_and_never_count_as_a_backend_failure()
|
||||
{
|
||||
var ct = TestContext.Current.CancellationToken;
|
||||
await using var rig = await StartAsync("DiscreteTimeWeighted", encrypted: false, ct);
|
||||
var gateway = new AvevaShapedGateway();
|
||||
var source = new GatewayHistorianDataSource(gateway, NullLogger<GatewayHistorianDataSource>.Instance);
|
||||
rig.Nm.HistorianDataSource = source;
|
||||
rig.Enforce(false, [Key()]);
|
||||
var boolNode = rig.EnsureRawVariable("HistBool", "Boolean", writable: false, Discrete);
|
||||
var intNode = rig.EnsureRawVariable("HistInt", "Int32", writable: false, Analog);
|
||||
|
||||
using var session = await rig.ConnectAsync(anonymous: true, ct);
|
||||
var ns = (ushort)session.NamespaceUris.GetIndex(V3NodeIds.RawNamespaceUri);
|
||||
|
||||
foreach (var aggregate in new[] { ObjectIds.AggregateFunction_TimeAverage, ObjectIds.AggregateFunction_Total })
|
||||
{
|
||||
var (status, _) = await ReadProcessedAsync(session, new NodeId(boolNode, ns), aggregate, ct);
|
||||
status.Code.ShouldBe(StatusCodes.BadAggregateNotSupported.Code, $"{aggregate} on a Boolean node");
|
||||
}
|
||||
gateway.AggregateTags.ShouldNotContain(Discrete, "the refusal is decided before the gateway is called");
|
||||
var health = source.GetHealthSnapshot();
|
||||
health.TotalFailures.ShouldBe(0, "a refused aggregate is a client request, not a backend failure");
|
||||
health.ConsecutiveFailures.ShouldBe(0);
|
||||
health.LastError.ShouldBeNull();
|
||||
|
||||
// Positive controls: the native pair on a numeric node reaches the gateway and is served; the Boolean node's
|
||||
// raw-evaluated Average is served (0/1, as on the SQL backends).
|
||||
var (intStatus, intRows) = await ReadProcessedAsync(session, new NodeId(intNode, ns), ObjectIds.AggregateFunction_TimeAverage, ct);
|
||||
intStatus.Code.ShouldBe(StatusCodes.Good.Code);
|
||||
intRows.ShouldBeGreaterThan(0);
|
||||
gateway.AggregateTags.ShouldContain(Analog);
|
||||
var (avgStatus, avgRows) = await ReadProcessedAsync(session, new NodeId(boolNode, ns), ObjectIds.AggregateFunction_Average, ct);
|
||||
avgStatus.Code.ShouldBe(StatusCodes.Good.Code);
|
||||
avgRows.ShouldBe(2);
|
||||
source.GetHealthSnapshot().TotalFailures.ShouldBe(0);
|
||||
}
|
||||
|
||||
private static async Task<(StatusCode Status, int Rows)> ReadProcessedAsync(ISession session, NodeId node, NodeId aggregate,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var details = new ReadProcessedDetails
|
||||
{
|
||||
StartTime = S0, EndTime = S0.AddMinutes(4), ProcessingInterval = 120_000,
|
||||
AggregateType = new List<NodeId> { aggregate },
|
||||
AggregateConfiguration = new AggregateConfiguration { UseServerCapabilitiesDefaults = true },
|
||||
};
|
||||
var response = await session.HistoryReadAsync(null, new ExtensionObject(details), TimestampsToReturn.Both, false,
|
||||
new HistoryReadValueId[] { new() { NodeId = node } }, ct);
|
||||
var result = response.Results.ToList().ShouldHaveSingleItem();
|
||||
var rows = !result.HistoryData.IsNull && ExtensionObject.ToEncodeable(result.HistoryData) is HistoryData data
|
||||
? data.DataValues.Count
|
||||
: 0;
|
||||
return (result.StatusCode, rows);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// AVEVA's behaviour as the live #694 gate saw it: the native TimeWeightedAverage / Integral modes on a discrete tag
|
||||
/// fail StartQuery, which the gateway answers <c>Internal</c> with the redacted detail
|
||||
/// <c>HistorianServerOperationException</c>; an analog tag answers one row per interval. Raw reads answer a 0/1
|
||||
/// toggle every 30 s for the discrete tag.
|
||||
/// </summary>
|
||||
private sealed class AvevaShapedGateway : IHistorianGatewayClient
|
||||
{
|
||||
private readonly List<string> _aggregateTags = [];
|
||||
|
||||
public IReadOnlyList<string> AggregateTags { get { lock (_aggregateTags) return _aggregateTags.ToList(); } }
|
||||
|
||||
public Task<bool> TagExistsAsync(string tag, CancellationToken ct) => Task.FromResult(true);
|
||||
|
||||
public async IAsyncEnumerable<HistorianAggregateSample> ReadAggregateAsync(string tag, DateTime startUtc, DateTime endUtc,
|
||||
RetrievalMode mode, TimeSpan interval, [EnumeratorCancellation] CancellationToken ct)
|
||||
{
|
||||
lock (_aggregateTags) _aggregateTags.Add(tag);
|
||||
await Task.Yield();
|
||||
if (tag == Discrete)
|
||||
throw new RpcException(new RpcStatus(RpcStatusCode.Internal, "HistorianServerOperationException"));
|
||||
for (var t = startUtc; t < endUtc; t += interval)
|
||||
yield return new HistorianAggregateSample
|
||||
{
|
||||
Value = 29.5, OpcQuality = 192, StartTime = Timestamp.FromDateTime(t), EndTime = Timestamp.FromDateTime(t + interval),
|
||||
};
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<HistorianSample> ReadRawAsync(string tag, DateTime startUtc, DateTime endUtc, int maxValues,
|
||||
[EnumeratorCancellation] CancellationToken ct)
|
||||
{
|
||||
await Task.Yield();
|
||||
var n = 0;
|
||||
for (var t = S0.AddMinutes(-10); t < S0.AddMinutes(10) && n < maxValues; t = t.AddSeconds(30))
|
||||
{
|
||||
if (t < startUtc || t > endUtc) continue;
|
||||
n++;
|
||||
yield return new HistorianSample
|
||||
{
|
||||
Tag = tag, Timestamp = Timestamp.FromDateTime(t), NumericValue = (t - S0).TotalSeconds / 30 % 2 == 0 ? 0 : 1,
|
||||
OpcQuality = 192, Quality = 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public Task<IReadOnlyList<HistorianSample>> ReadAtTimeAsync(string tag, IReadOnlyList<DateTime> timestampsUtc, CancellationToken ct) =>
|
||||
throw new NotSupportedException();
|
||||
public IAsyncEnumerable<HistorianEvent> ReadEventsAsync(string? sourceName, DateTime startUtc, DateTime endUtc, int maxEvents,
|
||||
CancellationToken ct) => throw new NotSupportedException();
|
||||
public Task<WriteAck> WriteLiveValuesAsync(string tag, IReadOnlyList<HistorianLiveValue> values, CancellationToken ct) =>
|
||||
throw new NotSupportedException();
|
||||
public Task<WriteAck> SendEventAsync(HistorianEvent evt, CancellationToken ct) => throw new NotSupportedException();
|
||||
public Task<TagOperationResults> EnsureTagsAsync(IReadOnlyList<HistorianTagDefinition> definitions, CancellationToken ct) =>
|
||||
throw new NotSupportedException();
|
||||
public Task<bool> ProbeAsync(CancellationToken ct) => Task.FromResult(true);
|
||||
public Task<ConnectionStatus> GetConnectionStatusAsync(CancellationToken ct) => Task.FromResult(new ConnectionStatus());
|
||||
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -979,6 +979,103 @@ public sealed class NodeManagerHistoryReadTests : IDisposable
|
||||
finally { await host.DisposeAsync(); }
|
||||
}
|
||||
|
||||
private static ReadProcessedDetails ProcessedDetails(NodeId aggregate) => new()
|
||||
{
|
||||
StartTime = DateTime.UtcNow.AddHours(-1),
|
||||
EndTime = DateTime.UtcNow,
|
||||
ProcessingInterval = 10_000.0,
|
||||
AggregateConfiguration = new AggregateConfiguration { UseServerCapabilitiesDefaults = true },
|
||||
AggregateType = new List<NodeId> { aggregate },
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// lmxopcua#897 — Part 11 §6.5.4 ("If the Aggregate is not valid for the Node then the StatusCode shall be
|
||||
/// Bad_AggregateNotSupported") with Part 13 Table 50 (TimeAverage, Total, Average, … are numeric only): a String or
|
||||
/// DateTime node asked for a numeric aggregate is refused up front on EVERY backend, without a backend call, while
|
||||
/// an all-types aggregate (Count) on the same node and the numeric one on a numeric node still reach the backend.
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[InlineData("String", "TimeAverage")]
|
||||
[InlineData("String", "Total")]
|
||||
[InlineData("String", "Average")]
|
||||
[InlineData("String", "Maximum")]
|
||||
[InlineData("DateTime", "TimeAverage")]
|
||||
[InlineData("DateTime", "Delta")]
|
||||
public async Task A_numeric_aggregate_on_a_non_numeric_node_is_refused_unsent(string dataType, string aggregate)
|
||||
{
|
||||
var (host, server) = await BootAsync();
|
||||
try
|
||||
{
|
||||
var nm = server.NodeManager!;
|
||||
var fake = new RecordingHistorianDataSource { ProcessedResult = Empty() };
|
||||
nm.HistorianDataSource = fake;
|
||||
nm.EnsureVariable("eq/text", null, "Text", dataType, false, historianTagname: "T.Text", realm: AddressSpaceRealm.Uns);
|
||||
nm.EnsureVariable("eq/num", null, "Num", "Double", false, historianTagname: "T.Num", realm: AddressSpaceRealm.Uns);
|
||||
var aggregateId = aggregate switch
|
||||
{
|
||||
"TimeAverage" => ObjectIds.AggregateFunction_TimeAverage,
|
||||
"Total" => ObjectIds.AggregateFunction_Total,
|
||||
"Average" => ObjectIds.AggregateFunction_Average,
|
||||
"Maximum" => ObjectIds.AggregateFunction_Maximum,
|
||||
_ => ObjectIds.AggregateFunction_Delta,
|
||||
};
|
||||
|
||||
var (_, errors) = InvokeHistoryRead(server, nm, ProcessedDetails(aggregateId), nm.TryGetVariable("eq/text")!.NodeId);
|
||||
errors[0].StatusCode.Code.ShouldBe(StatusCodes.BadAggregateNotSupported.Code);
|
||||
fake.LastCall.ShouldBeNull("the refusal is decided before any backend read");
|
||||
|
||||
// Positive controls: an all-types aggregate on the same node, and the same aggregate on a numeric node.
|
||||
InvokeHistoryRead(server, nm, ProcessedDetails(ObjectIds.AggregateFunction_Count), nm.TryGetVariable("eq/text")!.NodeId)
|
||||
.Item2[0].StatusCode.Code.ShouldBe(StatusCodes.Good.Code);
|
||||
fake.LastCall.ShouldBe("Processed");
|
||||
fake.LastAggregate.ShouldBe(HistoryAggregateType.Count);
|
||||
InvokeHistoryRead(server, nm, ProcessedDetails(aggregateId), nm.TryGetVariable("eq/num")!.NodeId)
|
||||
.Item2[0].StatusCode.Code.ShouldBe(StatusCodes.Good.Code);
|
||||
fake.LastTagname.ShouldBe("T.Num");
|
||||
}
|
||||
finally { await host.DisposeAsync(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// lmxopcua#897: a backend's own per-type limit (<see cref="IHistorianDataSource.SupportsAggregateFor"/> — the
|
||||
/// Gateway refuses native TimeAverage / Total on a Boolean node) is consulted with the node's scalar type and answers
|
||||
/// <c>BadAggregateNotSupported</c> without a backend read; the same aggregate on another type, and another aggregate
|
||||
/// on the Boolean node, are served.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task A_backend_per_type_refusal_answers_BadAggregateNotSupported_unsent()
|
||||
{
|
||||
var (host, server) = await BootAsync();
|
||||
try
|
||||
{
|
||||
var nm = server.NodeManager!;
|
||||
var fake = new RecordingHistorianDataSource
|
||||
{
|
||||
ProcessedResult = Empty(),
|
||||
SupportsFor = (a, t) => !(t == TypeCode.Boolean && a == HistoryAggregateType.TimeAverage),
|
||||
};
|
||||
nm.HistorianDataSource = fake;
|
||||
nm.EnsureVariable("eq/pulse", null, "Pulse", "Boolean", false, historianTagname: "SysPulse", realm: AddressSpaceRealm.Uns);
|
||||
nm.EnsureVariable("eq/sec", null, "Sec", "Int32", false, historianTagname: "SysTimeSec", realm: AddressSpaceRealm.Uns);
|
||||
var pulse = nm.TryGetVariable("eq/pulse")!.NodeId;
|
||||
|
||||
var (_, errors) = InvokeHistoryRead(server, nm, ProcessedDetails(ObjectIds.AggregateFunction_TimeAverage), pulse);
|
||||
errors[0].StatusCode.Code.ShouldBe(StatusCodes.BadAggregateNotSupported.Code);
|
||||
fake.LastCall.ShouldBeNull();
|
||||
fake.SupportsForCalls.ShouldContain((HistoryAggregateType.TimeAverage, (TypeCode?)TypeCode.Boolean),
|
||||
"the node's scalar type is what the backend is asked about");
|
||||
|
||||
InvokeHistoryRead(server, nm, ProcessedDetails(ObjectIds.AggregateFunction_Average), pulse)
|
||||
.Item2[0].StatusCode.Code.ShouldBe(StatusCodes.Good.Code);
|
||||
fake.LastAggregate.ShouldBe(HistoryAggregateType.Average);
|
||||
InvokeHistoryRead(server, nm, ProcessedDetails(ObjectIds.AggregateFunction_TimeAverage), nm.TryGetVariable("eq/sec")!.NodeId)
|
||||
.Item2[0].StatusCode.Code.ShouldBe(StatusCodes.Good.Code);
|
||||
fake.LastTagname.ShouldBe("SysTimeSec");
|
||||
fake.LastAggregate.ShouldBe(HistoryAggregateType.TimeAverage);
|
||||
}
|
||||
finally { await host.DisposeAsync(); }
|
||||
}
|
||||
|
||||
private static HistorianRead Empty() => new(Array.Empty<DataValueSnapshot>(), null);
|
||||
|
||||
/// <summary>A recording fake historian source — captures the last call's kind + arguments and returns
|
||||
@@ -992,6 +1089,13 @@ public sealed class NodeManagerHistoryReadTests : IDisposable
|
||||
public bool LimitExceeded { get; init; }
|
||||
public IReadOnlySet<HistoryAggregateType>? Supported { get; init; } = HistoryAggregateTypes.All;
|
||||
public IReadOnlySet<HistoryAggregateType> SupportedAggregates => Supported!;
|
||||
public Func<HistoryAggregateType, TypeCode?, bool>? SupportsFor { get; init; }
|
||||
public List<(HistoryAggregateType Aggregate, TypeCode? Type)> SupportsForCalls { get; } = [];
|
||||
public bool SupportsAggregateFor(HistoryAggregateType aggregate, TypeCode? valueType)
|
||||
{
|
||||
lock (SupportsForCalls) SupportsForCalls.Add((aggregate, valueType));
|
||||
return SupportsFor?.Invoke(aggregate, valueType) ?? true;
|
||||
}
|
||||
public HistorianRead RawResult { get; set; } = new(Array.Empty<DataValueSnapshot>(), null);
|
||||
public HistorianRead ProcessedResult { get; set; } = new(Array.Empty<DataValueSnapshot>(), null);
|
||||
public HistorianRead AtTimeResult { get; set; } = new(Array.Empty<DataValueSnapshot>(), null);
|
||||
|
||||
Reference in New Issue
Block a user