134 lines
6.3 KiB
C#
134 lines
6.3 KiB
C#
using System.Collections.Immutable;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Authorization;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
|
|
|
|
internal sealed record FrozenWriteScope(string ClusterId, NodeHierarchyKind Kind, ImmutableArray<string> Segments);
|
|
internal sealed record WriteRouteTarget(string DriverInstanceId, string DriverReference, string DriverIncarnation,
|
|
long RouteGeneration, long PrimaryGeneration);
|
|
internal sealed record WriteRouteProof(DataWriteAdmission Admission, FrozenWriteScope Scope, string EntityKind,
|
|
string EntityId, BackingSourceKey? Source, string DriverInstanceId, string DriverReference,
|
|
long RouteGeneration, long PrimaryGeneration, ServingIdentity? Serving = null);
|
|
|
|
/// <summary>Owns the value and its byte charge until rejection or actual driver completion.</summary>
|
|
internal sealed class WriteAdmissionTicket : IDisposable
|
|
{
|
|
private IDisposable? _byteLease;
|
|
private int _disposed;
|
|
public WriteAdmissionTicket(WriteRouteProof proof, DataValue value, long issuedAt, IDisposable? byteLease = null)
|
|
{ Proof = proof; Value = value; IssuedAt = issuedAt; _byteLease = byteLease; }
|
|
public WriteRouteProof Proof { get; }
|
|
public DataValue Value { get; }
|
|
public long IssuedAt { get; }
|
|
public void Dispose()
|
|
{
|
|
if (Interlocked.Exchange(ref _disposed, 1) != 0) return;
|
|
try { Value.Dispose(); }
|
|
finally { Interlocked.Exchange(ref _byteLease, null)?.Dispose(); }
|
|
}
|
|
}
|
|
|
|
internal interface IWriteDispatchGate
|
|
{
|
|
DataWriteOutcome? TryCommit(WriteAdmissionTicket ticket, WriteClaim claim);
|
|
}
|
|
|
|
internal interface IDataWriteRoute
|
|
{
|
|
bool TryResolve(DataView view, out WriteRouteTarget? target);
|
|
Task<DataWriteOutcome> DispatchAsync(WriteAdmissionTicket ticket, WriteClaim claim, CancellationToken ct);
|
|
}
|
|
|
|
/// <summary>Immutable, fenced route and physical actor-lifetime snapshots.</summary>
|
|
internal sealed class WriteRouteState(DataWriteDispatchFence fence)
|
|
{
|
|
private sealed record Snapshot(long Generation, ImmutableDictionary<DataView, WriteRouteTarget> Routes,
|
|
ImmutableDictionary<string, string> Drivers);
|
|
private Snapshot _current = new(0, ImmutableDictionary<DataView, WriteRouteTarget>.Empty,
|
|
ImmutableDictionary<string, string>.Empty.WithComparers(StringComparer.Ordinal));
|
|
public DataWriteDispatchFence DispatchFence => fence;
|
|
public string? CurrentDriverIncarnation(string driver) => Volatile.Read(ref _current).Drivers.GetValueOrDefault(driver);
|
|
|
|
public string BeginDriverIncarnation(string driver)
|
|
{
|
|
var incarnation = Guid.NewGuid().ToString("N");
|
|
Update(s =>
|
|
{
|
|
var generation = checked(s.Generation + 1);
|
|
var routes = s.Routes;
|
|
foreach (var pair in s.Routes)
|
|
if (pair.Value.DriverInstanceId == driver)
|
|
routes = routes.SetItem(pair.Key, pair.Value with { DriverIncarnation = incarnation, RouteGeneration = generation });
|
|
return s with { Generation = generation, Drivers = s.Drivers.SetItem(driver, incarnation), Routes = routes };
|
|
});
|
|
return incarnation;
|
|
}
|
|
|
|
public void RetireDriver(string driver, string incarnation) => Update(s =>
|
|
s.Drivers.TryGetValue(driver, out var current) && current == incarnation
|
|
? s with { Generation = checked(s.Generation + 1), Drivers = s.Drivers.Remove(driver) } : s);
|
|
|
|
public void ReplaceRoutes(IEnumerable<(DataView View, string Driver, string Reference)> desired)
|
|
{
|
|
var items = desired.ToArray();
|
|
Update(s =>
|
|
{
|
|
var generation = checked(s.Generation + 1);
|
|
var next = ImmutableDictionary.CreateBuilder<DataView, WriteRouteTarget>();
|
|
foreach (var (view, driver, reference) in items)
|
|
{
|
|
var incarnation = s.Drivers.GetValueOrDefault(driver) ?? string.Empty;
|
|
if (!next.TryAdd(view, new(driver, reference, incarnation, generation, 0)))
|
|
throw new InvalidOperationException("Duplicate write view.");
|
|
}
|
|
var same = next.Count == s.Routes.Count && next.All(pair => s.Routes.TryGetValue(pair.Key, out var old)
|
|
&& old.DriverInstanceId == pair.Value.DriverInstanceId && old.DriverReference == pair.Value.DriverReference
|
|
&& old.DriverIncarnation == pair.Value.DriverIncarnation);
|
|
return same ? s : s with { Generation = generation, Routes = next.ToImmutable() };
|
|
});
|
|
}
|
|
|
|
public WriteRouteTarget Publish(DataView view, string driver, string reference, string incarnation)
|
|
{
|
|
WriteRouteTarget? result = null;
|
|
Update(s =>
|
|
{
|
|
if (s.Routes.TryGetValue(view, out var existing) && existing.DriverInstanceId == driver
|
|
&& existing.DriverReference == reference && existing.DriverIncarnation == incarnation
|
|
&& s.Drivers.TryGetValue(driver, out var active) && active == incarnation)
|
|
{ result = existing; return s; }
|
|
var next = checked(s.Generation + 1);
|
|
result = new(driver, reference, incarnation, next, 0);
|
|
return s with { Generation = next, Routes = s.Routes.SetItem(view, result),
|
|
Drivers = s.Drivers.ContainsKey(driver) ? s.Drivers : s.Drivers.Add(driver, incarnation) };
|
|
});
|
|
return result!;
|
|
}
|
|
|
|
public bool TryResolve(DataView view, out WriteRouteTarget? target)
|
|
{
|
|
var current = Volatile.Read(ref _current);
|
|
if (current.Routes.TryGetValue(view, out var found) && current.Drivers.TryGetValue(found.DriverInstanceId, out var incarnation)
|
|
&& incarnation == found.DriverIncarnation) { target = found; return true; }
|
|
target = null; return false;
|
|
}
|
|
public bool IsCurrent(DataView view, WriteRouteTarget target) => TryResolve(view, out var current)
|
|
&& current == (target with { PrimaryGeneration = current!.PrimaryGeneration });
|
|
|
|
private void Update(Func<Snapshot, Snapshot> prepare)
|
|
{
|
|
while (true)
|
|
{
|
|
var before = Volatile.Read(ref _current);
|
|
var after = prepare(before);
|
|
using (fence.Enter())
|
|
{
|
|
if (!ReferenceEquals(before, _current)) continue;
|
|
Volatile.Write(ref _current, after);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|