feat: preserve grpc write admission and uncertain outcomes
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
||||
|
||||
public enum DataValueKind { Unspecified = 0, Null = 1, Boolean = 2, Int8 = 3, Int16 = 4, Int32 = 5, Int64 = 6, UInt8 = 7, UInt16 = 8, UInt32 = 9, UInt64 = 10, Float = 11, Double = 12, String = 13, Bytes = 14, Timestamp = 15, Guid = 16, Array = 17 }
|
||||
@@ -90,6 +92,32 @@ public sealed class DataValue : IDisposable
|
||||
public DataValueKind ArrayElementKind { get { ThrowIfDisposed(); return (_payload as ArrayPayload)?.Kind ?? throw new InvalidOperationException(); } }
|
||||
public IReadOnlyList<object> GetArray() { ThrowIfDisposed(); return _payload is ArrayPayload a ? Array.AsReadOnly((object[])a.Elements.Clone()) : throw new InvalidOperationException(); }
|
||||
public DataValue Retain() { ThrowIfDisposed(); return new(Kind, _payload, Dimensions); }
|
||||
/// <summary>Appends a private canonical value encoding without exposing or copying owned payload storage.</summary>
|
||||
internal void AppendSemanticFingerprint(IncrementalHash hash)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(hash);
|
||||
ThrowIfDisposed();
|
||||
DataFingerprintEncoding.AppendInt32(hash, 1);
|
||||
DataFingerprintEncoding.AppendInt32(hash, (int)Kind);
|
||||
if (Kind == DataValueKind.Null) return;
|
||||
if (Kind == DataValueKind.Bytes)
|
||||
{
|
||||
var bytes = (byte[])_payload!;
|
||||
DataFingerprintEncoding.AppendInt32(hash, bytes.Length);
|
||||
hash.AppendData(bytes);
|
||||
return;
|
||||
}
|
||||
if (_payload is ArrayPayload array)
|
||||
{
|
||||
DataFingerprintEncoding.AppendInt32(hash, (int)array.Kind);
|
||||
DataFingerprintEncoding.AppendInt32(hash, Dimensions!.Count);
|
||||
for (var i = 0; i < Dimensions.Count; i++) DataFingerprintEncoding.AppendInt32(hash, Dimensions[i]);
|
||||
DataFingerprintEncoding.AppendInt32(hash, array.Elements.Length);
|
||||
foreach (var element in array.Elements) DataFingerprintEncoding.AppendScalar(hash, array.Kind, element);
|
||||
return;
|
||||
}
|
||||
DataFingerprintEncoding.AppendScalar(hash, Kind, _payload!);
|
||||
}
|
||||
public void Dispose() => _disposed = true;
|
||||
private void ThrowIfDisposed() => ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
private sealed record ArrayPayload(DataValueKind Kind, object[] Elements);
|
||||
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
using System.Buffers.Binary;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
||||
|
||||
// Internal fingerprint framing only. This is not a transport or persisted format.
|
||||
internal static class DataFingerprintEncoding
|
||||
{
|
||||
internal static void AppendByte(IncrementalHash hash, byte value)
|
||||
{
|
||||
Span<byte> scratch = stackalloc byte[1];
|
||||
scratch[0] = value;
|
||||
hash.AppendData(scratch);
|
||||
}
|
||||
|
||||
internal static void AppendInt32(IncrementalHash hash, int value)
|
||||
{
|
||||
Span<byte> scratch = stackalloc byte[4];
|
||||
BinaryPrimitives.WriteInt32LittleEndian(scratch, value);
|
||||
hash.AppendData(scratch);
|
||||
}
|
||||
|
||||
internal static void AppendInt64(IncrementalHash hash, long value)
|
||||
{
|
||||
Span<byte> scratch = stackalloc byte[8];
|
||||
BinaryPrimitives.WriteInt64LittleEndian(scratch, value);
|
||||
hash.AppendData(scratch);
|
||||
}
|
||||
|
||||
internal static void AppendString(IncrementalHash hash, string value)
|
||||
{
|
||||
AppendInt32(hash, value.Length);
|
||||
Span<byte> scratch = stackalloc byte[256];
|
||||
var offset = 0;
|
||||
while (offset < value.Length)
|
||||
{
|
||||
var chars = Math.Min(scratch.Length / 2, value.Length - offset);
|
||||
for (var i = 0; i < chars; i++)
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(scratch.Slice(i * 2, 2), value[offset + i]);
|
||||
hash.AppendData(scratch[..(chars * 2)]);
|
||||
offset += chars;
|
||||
}
|
||||
}
|
||||
|
||||
internal static void AppendScalar(IncrementalHash hash, DataValueKind kind, object value)
|
||||
{
|
||||
Span<byte> scratch = stackalloc byte[16];
|
||||
var count = 0;
|
||||
switch (kind)
|
||||
{
|
||||
case DataValueKind.Boolean: scratch[0] = (bool)value ? (byte)1 : (byte)0; count = 1; break;
|
||||
case DataValueKind.Int8: scratch[0] = unchecked((byte)(sbyte)value); count = 1; break;
|
||||
case DataValueKind.UInt8: scratch[0] = (byte)value; count = 1; break;
|
||||
case DataValueKind.Int16: BinaryPrimitives.WriteInt16LittleEndian(scratch, (short)value); count = 2; break;
|
||||
case DataValueKind.UInt16: BinaryPrimitives.WriteUInt16LittleEndian(scratch, (ushort)value); count = 2; break;
|
||||
case DataValueKind.Int32: BinaryPrimitives.WriteInt32LittleEndian(scratch, (int)value); count = 4; break;
|
||||
case DataValueKind.UInt32: BinaryPrimitives.WriteUInt32LittleEndian(scratch, (uint)value); count = 4; break;
|
||||
case DataValueKind.Int64: BinaryPrimitives.WriteInt64LittleEndian(scratch, (long)value); count = 8; break;
|
||||
case DataValueKind.UInt64: BinaryPrimitives.WriteUInt64LittleEndian(scratch, (ulong)value); count = 8; break;
|
||||
case DataValueKind.Float: BinaryPrimitives.WriteInt32LittleEndian(scratch, BitConverter.SingleToInt32Bits((float)value)); count = 4; break;
|
||||
case DataValueKind.Double: BinaryPrimitives.WriteInt64LittleEndian(scratch, BitConverter.DoubleToInt64Bits((double)value)); count = 8; break;
|
||||
case DataValueKind.Timestamp: BinaryPrimitives.WriteInt64LittleEndian(scratch, ((DateTimeOffset)value).UtcTicks); count = 8; break;
|
||||
case DataValueKind.Guid: ((Guid)value).TryWriteBytes(scratch); count = 16; break;
|
||||
case DataValueKind.String: AppendString(hash, (string)value); return;
|
||||
default: throw new ArgumentOutOfRangeException(nameof(kind));
|
||||
}
|
||||
hash.AppendData(scratch[..count]);
|
||||
}
|
||||
}
|
||||
+2
@@ -11,6 +11,8 @@
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="README.md" />
|
||||
<InternalsVisibleTo Include="ZB.MOM.WW.OtOpcUa.Core.Abstractions.Tests" />
|
||||
<InternalsVisibleTo Include="ZB.MOM.WW.OtOpcUa.Runtime" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -22,6 +22,8 @@ public sealed class DataOperationPolicy
|
||||
{
|
||||
/// <summary>The currently installed local authority generation, rechecked at publication boundaries.</summary>
|
||||
public long AuthorityEpoch => _authority.Current.GenerationId;
|
||||
/// <summary>Checks that a mutation gate and this policy use the same authority publisher.</summary>
|
||||
public bool UsesAuthority(IDataPlaneAuthoritySource authority) => ReferenceEquals(_authority, authority);
|
||||
private readonly IDataPlaneAuthoritySource _authority;
|
||||
private readonly IPermissionEvaluator _evaluator;
|
||||
private readonly TimeProvider _clock;
|
||||
|
||||
@@ -563,6 +563,11 @@ public interface IDataPlaneAuthoritySink
|
||||
/// </remarks>
|
||||
public sealed class DataPlaneAuthorityState : IDataPlaneAuthoritySource, IDataPlaneAuthoritySink
|
||||
{
|
||||
/// <summary>Creates an authority state ordered with rich-write dispatch when a shared fence is supplied.</summary>
|
||||
public DataPlaneAuthorityState(DataWriteDispatchFence? dispatchFence = null) => DispatchFence = dispatchFence ?? new();
|
||||
|
||||
/// <summary>The ordering fence used for publication of this authority.</summary>
|
||||
public DataWriteDispatchFence DispatchFence { get; }
|
||||
/// <summary>
|
||||
/// Generations of the trie retained per cluster. One is enough for correctness now — every
|
||||
/// authorization call stamps itself with the current generation — but an in-flight request that
|
||||
@@ -637,7 +642,7 @@ public sealed class DataPlaneAuthorityState : IDataPlaneAuthoritySource, IDataPl
|
||||
clusterId, revisionHash, generation, staged.Keys, staged.Grants, staged.ScopePaths, dropped,
|
||||
staged.EquipmentScopePaths, staged.ScriptedAlarmOwners, staged.UnsFolderScopes,
|
||||
staged.TagReferencingEquipment);
|
||||
Volatile.Write(ref _current, published);
|
||||
using (DispatchFence.Enter()) Volatile.Write(ref _current, published);
|
||||
}
|
||||
|
||||
// Outside the lock, deliberately. Subscribers walk live subscriptions and take the SDK's own
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
namespace ZB.MOM.WW.OtOpcUa.Core.Authorization;
|
||||
|
||||
/// <summary>Orders mutation dispatch commitments with security and routing publications.</summary>
|
||||
/// <remarks>The holder performs only bounded state publication or validation; never invoke external code while held.</remarks>
|
||||
public sealed class DataWriteDispatchFence
|
||||
{
|
||||
private readonly object _gate = new();
|
||||
|
||||
/// <summary>Enter synchronously. Dispose on the same thread; never hold across an await.</summary>
|
||||
public IDisposable Enter()
|
||||
{
|
||||
Monitor.Enter(_gate);
|
||||
return new Releaser(_gate);
|
||||
}
|
||||
|
||||
/// <summary>Attempts entry without waiting; callers must fail closed when contention prevents entry.</summary>
|
||||
public bool TryEnter(out IDisposable? lease)
|
||||
{
|
||||
if (!Monitor.TryEnter(_gate)) { lease = null; return false; }
|
||||
lease = new Releaser(_gate);
|
||||
return true;
|
||||
}
|
||||
|
||||
private sealed class Releaser(object gate) : IDisposable
|
||||
{
|
||||
private object? _gate = gate;
|
||||
public void Dispose()
|
||||
{
|
||||
var held = Interlocked.Exchange(ref _gate, null);
|
||||
if (held is not null) Monitor.Exit(held);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user