feat(grpc-data): X0 reserve DataOperations bits 32-512, DataOperationSets authority, DataFeatures; capabilities mask unknown bits
This commit is contained in:
@@ -226,7 +226,46 @@ public sealed record DataBudget(TimeSpan Timeout, int MaxItems, long MaxBytes)
|
||||
{
|
||||
public void Validate() { if (Timeout <= TimeSpan.Zero || MaxItems <= 0 || MaxBytes <= 0) throw new ArgumentOutOfRangeException(nameof(Timeout)); }
|
||||
}
|
||||
[Flags] public enum DataOperations { None = 0, Browse = 1, Metadata = 2, Read = 4, Write = 8, Subscribe = 16 }
|
||||
/// <summary>
|
||||
/// Data operation grants. The values are persisted (ConfigDb int) and carried on the wire (uint); never renumber.
|
||||
/// Bits 32..512 are RESERVED by phase X0 of the history/alarm plan: no node implements them yet, so every reader
|
||||
/// masks them to <see cref="DataOperationSets.Understood"/> (an unknown grant is no grant) and the authoring
|
||||
/// validator keeps rejecting them until the phase that implements each one (H2, A2).
|
||||
/// </summary>
|
||||
[Flags] public enum DataOperations
|
||||
{
|
||||
None = 0, Browse = 1, Metadata = 2, Read = 4, Write = 8, Subscribe = 16,
|
||||
/// <summary>Reserved (H2): Raw/Processed/AtTime history reads.</summary>
|
||||
HistoryRead = 32,
|
||||
/// <summary>Reserved (A2): root-scoped condition items (origin-side grant).</summary>
|
||||
AlarmSubscribe = 64,
|
||||
/// <summary>Reserved (A2): Acknowledge and AddComment.</summary>
|
||||
AlarmAcknowledge = 128,
|
||||
/// <summary>Reserved (A2): Confirm.</summary>
|
||||
AlarmConfirm = 256,
|
||||
/// <summary>Reserved for the deferred shelve methods.</summary>
|
||||
AlarmShelve = 512,
|
||||
}
|
||||
/// <summary>The one authority for which <see cref="DataOperations"/> bits this build understands.</summary>
|
||||
public static class DataOperationSets
|
||||
{
|
||||
/// <summary>The v1 operation set.</summary>
|
||||
public const DataOperations V1 = DataOperations.Browse | DataOperations.Metadata | DataOperations.Read |
|
||||
DataOperations.Write | DataOperations.Subscribe;
|
||||
/// <summary>
|
||||
/// What tolerant readers keep; every other bit is dropped fail-closed. Grows only in the phase that implements
|
||||
/// a bit (H2 adds <see cref="DataOperations.HistoryRead"/>; A2 the alarm bits).
|
||||
/// </summary>
|
||||
public const DataOperations Understood = V1;
|
||||
/// <summary>Bits reserved by X0 for later phases.</summary>
|
||||
public const DataOperations Reserved = DataOperations.HistoryRead | DataOperations.AlarmSubscribe |
|
||||
DataOperations.AlarmAcknowledge | DataOperations.AlarmConfirm | DataOperations.AlarmShelve;
|
||||
}
|
||||
/// <summary>
|
||||
/// Optional per-call protocol features negotiated through the <c>x-otopcua-data-features</c> request header.
|
||||
/// A call without the header (or with an empty effective set) gets exactly the v1 behaviour.
|
||||
/// </summary>
|
||||
[Flags] public enum DataFeatures { None = 0, History = 1, Conditions = 2 }
|
||||
/// <summary>
|
||||
/// Negotiated limits. When a peer omits the per-value element limit, the protocol default is 100,000.
|
||||
/// </summary>
|
||||
@@ -250,6 +289,10 @@ public sealed record DataCapabilities(int ProtocolMajor, int ProtocolMinor, Serv
|
||||
bool WriteServingGenerationEnforced = false, bool SubscriptionFreshnessChallenges = false,
|
||||
bool MetadataItemLeases = false)
|
||||
{
|
||||
private readonly DataOperations _operations = Operations & DataOperationSets.Understood;
|
||||
/// <summary>Masked to <see cref="DataOperationSets.Understood"/> on construction and on every <c>with</c>.</summary>
|
||||
public DataOperations Operations { get => _operations; init => _operations = value & DataOperationSets.Understood; }
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(ClusterId);
|
||||
@@ -268,12 +311,7 @@ public sealed record DataCapabilities(int ProtocolMajor, int ProtocolMinor, Serv
|
||||
{
|
||||
DataContractValidation.Required(kind);
|
||||
}
|
||||
const DataOperations known = DataOperations.Browse | DataOperations.Metadata | DataOperations.Read |
|
||||
DataOperations.Write | DataOperations.Subscribe;
|
||||
if ((Operations & ~known) != 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(Operations));
|
||||
}
|
||||
// Operations is masked by its accessor, so unknown bits from a newer peer never reach here (X0).
|
||||
}
|
||||
}
|
||||
public sealed record DataMetadata(DataView View, string BrowseName, string NodeKind, DataValueKind ValueKind, int ValueRank,
|
||||
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.Tests.DataConnections;
|
||||
|
||||
/// <summary>X0: reserved operation bits and feature flags are pinned; unknown bits are tolerated fail-closed.</summary>
|
||||
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "X0")]
|
||||
public sealed class GrpcDataOperationToleranceTests
|
||||
{
|
||||
private static readonly DataLimits Limits = new(10, 10, 1024, 10, 1, 1024,
|
||||
TimeSpan.FromSeconds(10), TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1));
|
||||
private static readonly HashSet<DataValueKind> Kinds = [DataValueKind.Int32];
|
||||
|
||||
[Fact]
|
||||
public void Every_operation_bit_keeps_its_wire_value()
|
||||
{
|
||||
// Persisted as int in ConfigDb and carried as uint on the wire: never renumber.
|
||||
var expected = new Dictionary<string, int>
|
||||
{
|
||||
["None"] = 0, ["Browse"] = 1, ["Metadata"] = 2, ["Read"] = 4, ["Write"] = 8, ["Subscribe"] = 16,
|
||||
["HistoryRead"] = 32, ["AlarmSubscribe"] = 64, ["AlarmAcknowledge"] = 128, ["AlarmConfirm"] = 256, ["AlarmShelve"] = 512,
|
||||
};
|
||||
Enum.GetNames<DataOperations>().Order().ShouldBe(expected.Keys.Order());
|
||||
foreach (var (name, value) in expected) ((int)Enum.Parse<DataOperations>(name)).ShouldBe(value, name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Feature_flags_keep_their_values()
|
||||
{
|
||||
Enum.GetNames<DataFeatures>().ShouldBe(["None", "History", "Conditions"]);
|
||||
((int)DataFeatures.None).ShouldBe(0);
|
||||
((int)DataFeatures.History).ShouldBe(1);
|
||||
((int)DataFeatures.Conditions).ShouldBe(2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void X0_understands_only_v1_and_reserves_the_five_new_bits()
|
||||
{
|
||||
DataOperationSets.V1.ShouldBe(DataOperations.Browse | DataOperations.Metadata | DataOperations.Read | DataOperations.Write | DataOperations.Subscribe);
|
||||
DataOperationSets.Understood.ShouldBe(DataOperationSets.V1);
|
||||
DataOperationSets.Reserved.ShouldBe(DataOperations.HistoryRead | DataOperations.AlarmSubscribe | DataOperations.AlarmAcknowledge
|
||||
| DataOperations.AlarmConfirm | DataOperations.AlarmShelve);
|
||||
(DataOperationSets.Reserved & DataOperationSets.Understood).ShouldBe(DataOperations.None);
|
||||
((int)DataOperationSets.Reserved).ShouldBe(0x3E0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Capabilities_mask_unknown_bits_on_construction_instead_of_throwing()
|
||||
{
|
||||
var unknown = DataOperations.Read | DataOperations.HistoryRead | DataOperations.AlarmSubscribe | (DataOperations)(1 << 20);
|
||||
var capabilities = Create(unknown);
|
||||
capabilities.Operations.ShouldBe(DataOperations.Read);
|
||||
Should.NotThrow(capabilities.Validate);
|
||||
capabilities.ShouldBe(Create(DataOperations.Read));
|
||||
capabilities.GetHashCode().ShouldBe(Create(DataOperations.Read).GetHashCode());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Capabilities_mask_unknown_bits_through_with_expressions()
|
||||
{
|
||||
var capabilities = Create(DataOperations.Browse) with { Operations = DataOperations.Write | DataOperations.AlarmConfirm };
|
||||
capabilities.Operations.ShouldBe(DataOperations.Write);
|
||||
Should.NotThrow(capabilities.Validate);
|
||||
(capabilities with { Operations = DataOperations.AlarmShelve }).Operations.ShouldBe(DataOperations.None);
|
||||
// Other members are untouched by the masking accessor.
|
||||
(capabilities with { ClusterId = "other" }).Operations.ShouldBe(DataOperations.Write);
|
||||
}
|
||||
|
||||
private static DataCapabilities Create(DataOperations operations) => new(1, 0,
|
||||
new ServingIdentity("node", "incarnation", "primary", 1, 1), DataReadiness.Ready, operations,
|
||||
Kinds, Limits, false, 0, "cluster-a");
|
||||
}
|
||||
Reference in New Issue
Block a user