test: validate grpc data connections with local docker and opcua cli
This commit is contained in:
@@ -13,7 +13,10 @@ internal sealed class SamplePartition : IDisposable
|
||||
private readonly object _gate = new();
|
||||
private readonly ConcurrentDictionary<BackingSourceKey, SourceState> _sources = new();
|
||||
private readonly Queue<Pending> _queue = new();
|
||||
private readonly Dictionary<BackingSourceKey, Pending> _dirty = new();
|
||||
private readonly ConcurrentQueue<Pending> _mailbox = new();
|
||||
private readonly ConcurrentQueue<SourceState> _dirty = new();
|
||||
private int _dirtyCount;
|
||||
private int _producerState; // sign bit closes admission; remaining bits count active writers.
|
||||
private readonly LinkedList<(long Version, BackingSourceKey Key)> _changes = new();
|
||||
private readonly Dictionary<long, LinkedListNode<(long Version, BackingSourceKey Key)>> _changeIndex = new();
|
||||
private long _epoch = 1;
|
||||
@@ -82,7 +85,7 @@ internal sealed class SamplePartition : IDisposable
|
||||
state.Fence = fence; state.PartitionEpoch = _epoch;
|
||||
Volatile.Write(ref state.Gate, new SourceGate(ticket.Generation, ticket.Binding));
|
||||
state.AppliedInvalidations = 0;
|
||||
state.LastSubmitted = 0; state.LastApplied = 0; state.Barrier = 0; ReleaseCache(state);
|
||||
state.LastApplied = 0; state.Barrier = 0; ReleaseCache(state);
|
||||
RecordChange(ticket.Key); return true;
|
||||
}
|
||||
var charge = SourceCharge(ticket.Key);
|
||||
@@ -147,63 +150,151 @@ internal sealed class SamplePartition : IDisposable
|
||||
_hub.Schedule(this);
|
||||
}
|
||||
Interlocked.Increment(ref gate.Invalidated);
|
||||
Interlocked.Increment(ref gate.ProducerStamp);
|
||||
}
|
||||
finally { Control.EndQuiet(); }
|
||||
}
|
||||
|
||||
private static bool TryPin(SourceState state)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (Volatile.Read(ref state.RegistryRetired) != 0) return false;
|
||||
var count = Volatile.Read(ref state.MetadataOwners);
|
||||
if (count <= 0 || count == int.MaxValue) return false;
|
||||
if (Interlocked.CompareExchange(ref state.MetadataOwners, count + 1, count) == count) return true;
|
||||
}
|
||||
}
|
||||
private static bool TryIncrementBounded(ref int count, int maximum)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var current = Volatile.Read(ref count);
|
||||
if (current >= maximum) return false;
|
||||
if (Interlocked.CompareExchange(ref count, current + 1, current) == current) return true;
|
||||
}
|
||||
}
|
||||
private static bool TryAdvance(SourceGate gate, long sequence)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var current = Interlocked.Read(ref gate.LastSubmitted);
|
||||
if (sequence <= current) return false;
|
||||
if (Interlocked.CompareExchange(ref gate.LastSubmitted, sequence, current) == current) return true;
|
||||
}
|
||||
}
|
||||
internal bool TryReserve(SourceTicket ticket, long estimated, out IIngressReservation? reservation)
|
||||
{
|
||||
reservation = null;
|
||||
if (!Monitor.TryEnter(_gate)) { Signal(ticket); return false; }
|
||||
try
|
||||
{
|
||||
if (_disposed || !Current(ticket, out var state)) return false;
|
||||
if (estimated <= 0 || estimated > long.MaxValue - PartitionedSampleHub.SampleOverhead ||
|
||||
Volatile.Read(ref _reservations) >= _hub.Limits.MaxIngressItemsPerPartition + _hub.Limits.MaxDirtySourcesPerPartition ||
|
||||
!_hub.Resources.TryReserve(estimated + PartitionedSampleHub.SampleOverhead))
|
||||
{ InvalidateSource(state); return false; }
|
||||
Interlocked.Increment(ref _reservations);
|
||||
Interlocked.Increment(ref state.Reservations);
|
||||
Interlocked.Increment(ref state.MetadataOwners);
|
||||
reservation = new Reservation(this, state, ticket, estimated, estimated + PartitionedSampleHub.SampleOverhead,
|
||||
state.InvalidationVersion, _epoch, _hub.Clock.GetTimestamp());
|
||||
return true;
|
||||
}
|
||||
finally { Monitor.Exit(_gate); }
|
||||
if (_hub.IsDisposed || Volatile.Read(ref _disposed) || _hub.RoutingEpoch != _routingEpoch ||
|
||||
!_sources.TryGetValue(ticket.Key, out var state) || !TryPin(state)) return false;
|
||||
var gate = Volatile.Read(ref state.Gate);
|
||||
var epoch = Interlocked.Read(ref _epoch);
|
||||
var stamp = Interlocked.Read(ref gate.ProducerStamp);
|
||||
var signal = Interlocked.Read(ref gate.Invalidated);
|
||||
if (Volatile.Read(ref state.RegistryRetired) != 0 || !_sources.TryGetValue(ticket.Key, out var current) ||
|
||||
!ReferenceEquals(current, state) || !IsTicketCurrentNonBlocking(ticket) ||
|
||||
!ReferenceEquals(gate, Volatile.Read(ref state.Gate)))
|
||||
{ ReleaseMetadataOwner(state); return false; }
|
||||
if (estimated <= 0 || estimated > long.MaxValue - PartitionedSampleHub.SampleOverhead ||
|
||||
!TryIncrementBounded(ref _reservations, (int)Math.Min(int.MaxValue, (long)_hub.Limits.MaxIngressItemsPerPartition + _hub.Limits.MaxDirtySourcesPerPartition)))
|
||||
{ ReleaseMetadataOwner(state); SourceIngressDiagnostics.Count(SourceIngressDiagnostics.Event.ReserveCapacity); Signal(ticket); return false; }
|
||||
var charge = estimated + PartitionedSampleHub.SampleOverhead;
|
||||
if (!_hub.Resources.TryReserve(charge))
|
||||
{ Interlocked.Decrement(ref _reservations); ReleaseMetadataOwner(state); SourceIngressDiagnostics.Count(SourceIngressDiagnostics.Event.ReserveCapacity); Signal(ticket); return false; }
|
||||
Interlocked.Increment(ref state.Reservations);
|
||||
if (!IsTicketCurrentNonBlocking(ticket) || !ReferenceEquals(gate, Volatile.Read(ref state.Gate)) ||
|
||||
Interlocked.Read(ref _epoch) != epoch || Interlocked.Read(ref gate.ProducerStamp) != stamp || Interlocked.Read(ref gate.Invalidated) != signal)
|
||||
{ ReleaseReservation(state); _hub.Resources.Release(charge); return false; }
|
||||
reservation = new Reservation(this, state, gate, ticket, estimated, charge, stamp, signal, epoch, _hub.Clock.GetTimestamp());
|
||||
return true;
|
||||
}
|
||||
private bool Commit(SourceState reservedState, SourceTicket ticket, long estimate, long charge, long invalidationVersion,
|
||||
long partitionEpoch, long receivedTimestamp, DataSample sample, long sequence)
|
||||
private bool Commit(SourceState state, SourceGate gate, SourceTicket ticket, long estimate, long charge, long stamp,
|
||||
long signal, long partitionEpoch, long receivedTimestamp, DataSample sample, long sequence)
|
||||
{
|
||||
if (!TryEnterProducer()) { ReleaseReservation(state); _hub.Resources.Release(charge); return false; }
|
||||
if (Interlocked.CompareExchange(ref gate.Committing, 1, 0) != 0)
|
||||
{
|
||||
ReleaseReservation(state); _hub.Resources.Release(charge); Signal(ticket); ExitProducer(); return false;
|
||||
}
|
||||
try { return CommitEntered(state, gate, ticket, estimate, charge, stamp, signal, partitionEpoch, receivedTimestamp, sample, sequence); }
|
||||
finally { Volatile.Write(ref gate.Committing, 0); ExitProducer(); }
|
||||
}
|
||||
private bool CommitEntered(SourceState state, SourceGate gate, SourceTicket ticket, long estimate, long charge, long stamp,
|
||||
long signal, long partitionEpoch, long receivedTimestamp, DataSample sample, long sequence)
|
||||
{
|
||||
if (!Monitor.TryEnter(_gate))
|
||||
{ ReleaseReservation(reservedState); _hub.Resources.Release(charge); Signal(ticket); return false; }
|
||||
try
|
||||
{
|
||||
ReleaseReservation(reservedState);
|
||||
if (_disposed || !Current(ticket, out var state)) { _hub.Resources.Release(charge); return false; }
|
||||
if (state.InvalidationVersion != invalidationVersion || _epoch != partitionEpoch) { _hub.Resources.Release(charge); return false; }
|
||||
try
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(sample.Value);
|
||||
ArgumentNullException.ThrowIfNull(sample.Serving);
|
||||
sample.Serving.Validate(requireEpochs: false);
|
||||
if (sequence <= state.LastSubmitted || sample.Version != sequence || sample.Value.EstimatedRetainedBytes > estimate ||
|
||||
sample.Serving.NodeId != ticket.Key.ServingNodeId || sample.Serving.Incarnation != ticket.Key.ServingIncarnation)
|
||||
{ _hub.Resources.Release(charge); InvalidateSource(state); return false; }
|
||||
}
|
||||
catch (Exception e) when (e is ArgumentException or ObjectDisposedException or OverflowException)
|
||||
{ _hub.Resources.Release(charge); InvalidateSource(state); return false; }
|
||||
var pending = new Pending(ticket, sequence, ++_ordinal, sample, charge, receivedTimestamp) { AdmissionEpoch = state.InvalidationVersion, PartitionEpoch = partitionEpoch };
|
||||
if (_queuedValues < _hub.Limits.MaxIngressItemsPerPartition)
|
||||
{ _queue.Enqueue(pending); _queuedValues++; }
|
||||
else if (_dirty.TryGetValue(ticket.Key, out var previous))
|
||||
{ ReleasePending(previous); _dirty[ticket.Key] = pending; }
|
||||
else if (_dirty.Count < _hub.Limits.MaxDirtySourcesPerPartition) _dirty.Add(ticket.Key, pending);
|
||||
else { _hub.Resources.Release(charge); InvalidatePartition(); return false; }
|
||||
state.LastSubmitted = sequence;
|
||||
_hub.Schedule(this);
|
||||
return true;
|
||||
ArgumentNullException.ThrowIfNull(sample.Value);
|
||||
ArgumentNullException.ThrowIfNull(sample.Serving);
|
||||
sample.Serving.Validate(requireEpochs: false);
|
||||
if (sample.Version != sequence || sample.Value.EstimatedRetainedBytes > estimate ||
|
||||
sample.Serving.NodeId != ticket.Key.ServingNodeId || sample.Serving.Incarnation != ticket.Key.ServingIncarnation)
|
||||
throw new ArgumentException("Invalid ingress sample.");
|
||||
}
|
||||
catch (Exception e) when (e is ArgumentException or ObjectDisposedException or OverflowException)
|
||||
{ SourceIngressDiagnostics.Count(SourceIngressDiagnostics.Event.CommitInvalid); ReleaseReservation(state); _hub.Resources.Release(charge); Signal(ticket); return false; }
|
||||
if (!IsTicketCurrentNonBlocking(ticket) || !ReferenceEquals(gate, Volatile.Read(ref state.Gate)) ||
|
||||
partitionEpoch != Interlocked.Read(ref _epoch) || stamp != Interlocked.Read(ref gate.ProducerStamp) || signal != Interlocked.Read(ref gate.Invalidated))
|
||||
{ ReleaseReservation(state); _hub.Resources.Release(charge); return false; }
|
||||
if (!TryAdvance(gate, sequence))
|
||||
{ ReleaseReservation(state); _hub.Resources.Release(charge); Signal(ticket); return false; }
|
||||
_hub.AfterValueSequenceForTests?.Invoke();
|
||||
var pending = new Pending(ticket, sequence, Interlocked.Increment(ref _ordinal), sample, charge, receivedTimestamp)
|
||||
{ PartitionEpoch = partitionEpoch, Owner = state, Gate = gate, ProducerStamp = stamp, SignalStamp = signal };
|
||||
var admitted = EnqueueValue(state, pending);
|
||||
if (admitted != EnqueueResult.Accepted)
|
||||
{
|
||||
ReleaseReservation(state); _hub.Resources.Release(charge);
|
||||
if (admitted == EnqueueResult.Capacity) InvalidatePartition();
|
||||
else Signal(ticket);
|
||||
return false;
|
||||
}
|
||||
// The mailbox owns the metadata pin; the in-flight copy permit ends at transfer.
|
||||
Interlocked.Decrement(ref _reservations);
|
||||
var lastReservation = Interlocked.Decrement(ref state.Reservations) == 0;
|
||||
if (lastReservation && Volatile.Read(ref state.Gate.Retired) != 0 && !Volatile.Read(ref _disposed)) RequestReclaim();
|
||||
_hub.Schedule(this);
|
||||
return true;
|
||||
}
|
||||
private bool TryEnterProducer()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var current = Volatile.Read(ref _producerState);
|
||||
if (current < 0 || current == int.MaxValue) return false;
|
||||
if (Interlocked.CompareExchange(ref _producerState, current + 1, current) == current) return true;
|
||||
}
|
||||
}
|
||||
private void ExitProducer()
|
||||
{
|
||||
if (Interlocked.Decrement(ref _producerState) < 0) Schedule();
|
||||
}
|
||||
private enum EnqueueResult { Accepted, Stale, Capacity }
|
||||
private EnqueueResult EnqueueValue(SourceState state, Pending pending)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var existing = Volatile.Read(ref state.Dirty);
|
||||
if (existing is not null)
|
||||
{
|
||||
if (ReferenceEquals(existing.Gate, pending.Gate) && existing.Sequence >= pending.Sequence ||
|
||||
!ReferenceEquals(existing.Gate, pending.Gate) && existing.Gate is not null && pending.Gate is not null &&
|
||||
Compare(existing.Gate.Generation, pending.Gate.Generation) >= 0) return EnqueueResult.Stale;
|
||||
if (Interlocked.CompareExchange(ref state.Dirty, pending, existing) != existing) continue;
|
||||
ReleasePending(existing); return EnqueueResult.Accepted;
|
||||
}
|
||||
if (TryIncrementBounded(ref _queuedValues, _hub.Limits.MaxIngressItemsPerPartition))
|
||||
{
|
||||
if (Volatile.Read(ref state.Dirty) is not null) { Interlocked.Decrement(ref _queuedValues); continue; }
|
||||
_mailbox.Enqueue(pending); return EnqueueResult.Accepted;
|
||||
}
|
||||
if (!TryIncrementBounded(ref _dirtyCount, _hub.Limits.MaxDirtySourcesPerPartition))
|
||||
{ if (Volatile.Read(ref state.Dirty) is not null) continue; return EnqueueResult.Capacity; }
|
||||
if (Interlocked.CompareExchange(ref state.Dirty, pending, null) is null)
|
||||
{ _dirty.Enqueue(state); return EnqueueResult.Accepted; }
|
||||
Interlocked.Decrement(ref _dirtyCount);
|
||||
}
|
||||
finally { Monitor.Exit(_gate); }
|
||||
}
|
||||
private void ReleaseReservation(SourceState state)
|
||||
{
|
||||
@@ -213,7 +304,7 @@ internal sealed class SamplePartition : IDisposable
|
||||
if (lastReservation && Volatile.Read(ref state.Gate.Retired) != 0 && !Volatile.Read(ref _disposed)) RequestReclaim();
|
||||
}
|
||||
private void Abandon(SourceState state, long charge) { ReleaseReservation(state); _hub.Resources.Release(charge); }
|
||||
internal void Invalidate(SourceTicket ticket) => Signal(ticket);
|
||||
internal void Invalidate(SourceTicket ticket) { SourceIngressDiagnostics.Count(SourceIngressDiagnostics.Event.ExternalInvalidate); Signal(ticket); }
|
||||
private void InvalidateSource(SourceState state)
|
||||
{
|
||||
Control.BeginQuiet();
|
||||
@@ -227,13 +318,13 @@ internal sealed class SamplePartition : IDisposable
|
||||
try
|
||||
{
|
||||
var partitionEpoch = Interlocked.Read(ref _epoch);
|
||||
if (_disposed || !Current(ticket, out var state) || sequence <= state.LastSubmitted) return false;
|
||||
if (_disposed || !Current(ticket, out var state) || !TryAdvance(Volatile.Read(ref state.Gate), sequence)) return false;
|
||||
if (_queuedControls >= _hub.Limits.MaxControlItemsPerPartition || !_hub.Resources.TryReserve(ControlOverhead)) { InvalidatePartition(); return false; }
|
||||
var wasReady = state.Readiness == DataReadiness.Ready;
|
||||
Control.Begin();
|
||||
try { state.Readiness = DataReadiness.Invalidated; Interlocked.Increment(ref state.InvalidationVersion); state.Barrier = sequence; state.LastSubmitted = sequence; }
|
||||
try { state.Readiness = DataReadiness.Invalidated; Interlocked.Increment(ref state.InvalidationVersion); Interlocked.Increment(ref state.Gate.ProducerStamp); state.Barrier = sequence; }
|
||||
finally { Control.End(); }
|
||||
_queue.Enqueue(new Pending(ticket, sequence, ++_ordinal, status, source, server, wasReady, ControlOverhead) { AdmissionEpoch = state.InvalidationVersion, PartitionEpoch = partitionEpoch }); _queuedControls++;
|
||||
_queue.Enqueue(new Pending(ticket, sequence, Interlocked.Increment(ref _ordinal), status, source, server, wasReady, ControlOverhead) { AdmissionEpoch = state.InvalidationVersion, PartitionEpoch = partitionEpoch }); _queuedControls++;
|
||||
_hub.Schedule(this); return true;
|
||||
}
|
||||
finally { Monitor.Exit(_gate); }
|
||||
@@ -246,17 +337,17 @@ internal sealed class SamplePartition : IDisposable
|
||||
// Retirement must still be able to release a source after its shared fence closed.
|
||||
if (_disposed || !_sources.TryGetValue(ticket.Key, out var state)) return false;
|
||||
ApplyGate(state); ApplyPartitionEpoch(state);
|
||||
if (state.Removed || state.Ticket != ticket || Volatile.Read(ref state.Gate.Retired) != 0 || sequence <= state.LastSubmitted) return false;
|
||||
if (state.Removed || state.Ticket != ticket || Volatile.Read(ref state.Gate.Retired) != 0 || !TryAdvance(Volatile.Read(ref state.Gate), sequence)) return false;
|
||||
Control.BeginQuiet();
|
||||
try
|
||||
{
|
||||
if (_queuedControls >= _hub.Limits.MaxControlItemsPerPartition || !_hub.Resources.TryReserve(ControlOverhead))
|
||||
{ state.Removed = true; ticket.Binding!.Retire(); Interlocked.Exchange(ref state.Gate.Retired, 1); RequestReclaim(); InvalidatePartition(); return false; }
|
||||
state.Removed = true; ticket.Binding!.Retire(); Interlocked.Exchange(ref state.Gate.Retired, 1); state.Readiness = DataReadiness.Invalidated; Interlocked.Increment(ref state.InvalidationVersion); state.LastSubmitted = sequence; ReleaseCache(state);
|
||||
state.Removed = true; ticket.Binding!.Retire(); Interlocked.Exchange(ref state.Gate.Retired, 1); state.Readiness = DataReadiness.Invalidated; Interlocked.Increment(ref state.InvalidationVersion); Interlocked.Increment(ref state.Gate.ProducerStamp); ReleaseCache(state);
|
||||
}
|
||||
finally { Control.EndQuiet(); }
|
||||
RequestReclaim();
|
||||
_queue.Enqueue(new Pending(ticket, sequence, ++_ordinal, ControlOverhead) { PartitionEpoch = _epoch }); _queuedControls++;
|
||||
_queue.Enqueue(new Pending(ticket, sequence, Interlocked.Increment(ref _ordinal), ControlOverhead) { PartitionEpoch = _epoch }); _queuedControls++;
|
||||
_hub.Schedule(this); return true;
|
||||
}
|
||||
finally { Monitor.Exit(_gate); }
|
||||
@@ -273,38 +364,63 @@ internal sealed class SamplePartition : IDisposable
|
||||
finally
|
||||
{
|
||||
Volatile.Write(ref _scheduled, 0);
|
||||
lock (_gate) if (!_disposed && (_cleanedEpoch != _epoch || Volatile.Read(ref _retiredSweepRequested) != 0 || Volatile.Read(ref _reclaimRequested) != 0 || _queue.Count != 0 || _dirty.Count != 0)) Schedule();
|
||||
lock (_gate) if ((!_disposed && (_cleanedEpoch != _epoch || Volatile.Read(ref _retiredSweepRequested) != 0 || Volatile.Read(ref _reclaimRequested) != 0 || _queue.Count != 0)) || !_mailbox.IsEmpty || !_dirty.IsEmpty) Schedule();
|
||||
}
|
||||
}
|
||||
internal void Drain()
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
CleanupInvalidatedEpoch();
|
||||
CleanupRetiredSources();
|
||||
while (!_disposed && (_queue.Count != 0 || _dirty.Count != 0))
|
||||
if (!_disposed) { CleanupInvalidatedEpoch(); CleanupRetiredSources(); }
|
||||
// Controls present at entry cannot be replenished while this gate is held. Drain
|
||||
// that finite bounded set first, then give values their own bounded work slice.
|
||||
var controlsRemaining = _queue.Count;
|
||||
var valueWork = 0;
|
||||
while (controlsRemaining > 0 || valueWork < 128)
|
||||
{
|
||||
Pending? dirty = null;
|
||||
foreach (var value in _dirty.Values) if (dirty is null || value.Ordinal < dirty.Ordinal) dirty = value;
|
||||
Pending p;
|
||||
if (dirty is not null && (_queue.Count == 0 || dirty.Ordinal < _queue.Peek().Ordinal))
|
||||
{ p = dirty; _dirty.Remove(p.Ticket.Key); }
|
||||
else
|
||||
{ p = _queue.Dequeue(); if (p.IsControl) _queuedControls--; else _queuedValues--; }
|
||||
if (p.PartitionEpoch != _epoch || !Current(p.Ticket, out var state) || p.AdmissionEpoch != state.InvalidationVersion || p.Sequence <= state.LastApplied)
|
||||
Pending? p = null;
|
||||
if (controlsRemaining > 0 && _queue.TryDequeue(out var control)) { controlsRemaining--; p = control; _queuedControls--; }
|
||||
else if (++valueWork > 128) break;
|
||||
else if (_mailbox.TryPeek(out var first))
|
||||
{
|
||||
var dirtyFirst = _dirty.TryPeek(out var dirtyState) ? Volatile.Read(ref dirtyState.Dirty) : null;
|
||||
if (dirtyFirst is not null && dirtyFirst.Ordinal < first.Ordinal)
|
||||
{
|
||||
if (!_dirty.TryDequeue(out dirtyState)) continue;
|
||||
Interlocked.Decrement(ref _dirtyCount);
|
||||
p = Interlocked.Exchange(ref dirtyState.Dirty, null);
|
||||
}
|
||||
else if (_mailbox.TryDequeue(out p)) Interlocked.Decrement(ref _queuedValues);
|
||||
}
|
||||
else if (_dirty.TryDequeue(out var dirtyState))
|
||||
{
|
||||
Interlocked.Decrement(ref _dirtyCount);
|
||||
p = Interlocked.Exchange(ref dirtyState.Dirty, null);
|
||||
}
|
||||
else break;
|
||||
if (p is null) continue;
|
||||
if (_disposed || p.PartitionEpoch != _epoch ||
|
||||
p.Owner is not null && (!ReferenceEquals(p.Gate, Volatile.Read(ref p.Owner.Gate)) ||
|
||||
p.Gate is null || p.ProducerStamp != Interlocked.Read(ref p.Gate.ProducerStamp) || p.SignalStamp != Interlocked.Read(ref p.Gate.Invalidated)))
|
||||
{ ReleasePending(p); continue; }
|
||||
if (p.Owner is not null) _hub.BeforePendingValueCurrentForTests?.Invoke();
|
||||
if (!Current(p.Ticket, out var state) || p.IsControl && p.AdmissionEpoch != state.InvalidationVersion || p.Sequence <= state.LastApplied ||
|
||||
p.Owner is not null && (!ReferenceEquals(p.Owner, state) || p.PartitionEpoch != Interlocked.Read(ref _epoch) ||
|
||||
!ReferenceEquals(p.Gate, Volatile.Read(ref state.Gate)) || p.Gate is null ||
|
||||
p.ProducerStamp != Interlocked.Read(ref p.Gate.ProducerStamp) || p.SignalStamp != Interlocked.Read(ref p.Gate.Invalidated)))
|
||||
{ ReleasePending(p); continue; }
|
||||
state.LastApplied = p.Sequence;
|
||||
if (p.Sample is not null)
|
||||
{
|
||||
ReleaseCache(state); state.Sample = p.Sample; state.SampleCharge = p.Charge; state.Received = p.Received;
|
||||
state.Readiness = p.Sequence < state.Barrier ? DataReadiness.Invalidated : DataReadiness.Ready;
|
||||
if (p.Owner is not null) ReleaseMetadataOwner(p.Owner);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (p.IsStatus && state.Sample is not null)
|
||||
{
|
||||
var old = state.Sample;
|
||||
// Missing timestamps on a status-only event mean no new acquisition timestamp.
|
||||
state.Sample = old with { StatusCode = p.Status,
|
||||
SourceTimestampUtc = p.SourceTimestamp ?? old.SourceTimestampUtc,
|
||||
ServerTimestampUtc = p.ServerTimestamp ?? old.ServerTimestampUtc, Version = p.Sequence };
|
||||
@@ -315,7 +431,17 @@ internal sealed class SamplePartition : IDisposable
|
||||
}
|
||||
RecordChange(p.Ticket.Key);
|
||||
}
|
||||
ReclaimTombstones();
|
||||
if (!_disposed) ReclaimTombstones();
|
||||
}
|
||||
}
|
||||
// Deterministic fixture helper: production workers always use the fixed Drain slice.
|
||||
internal void DrainForTests()
|
||||
{
|
||||
var maximum = (long)_hub.Limits.MaxIngressItemsPerPartition + _hub.Limits.MaxDirtySourcesPerPartition + _hub.Limits.MaxControlItemsPerPartition;
|
||||
for (long work = 0; work <= maximum / 128 + 1; work++)
|
||||
{
|
||||
Drain();
|
||||
lock (_gate) if (_queue.Count == 0 && _mailbox.IsEmpty && _dirty.IsEmpty) return;
|
||||
}
|
||||
}
|
||||
private void RecordChange(BackingSourceKey key)
|
||||
@@ -328,6 +454,7 @@ internal sealed class SamplePartition : IDisposable
|
||||
}
|
||||
internal void InvalidatePartition()
|
||||
{
|
||||
SourceIngressDiagnostics.Count(SourceIngressDiagnostics.Event.PartitionInvalidated);
|
||||
Control.BeginQuiet();
|
||||
try { Interlocked.Increment(ref _epoch); }
|
||||
finally { Control.EndQuiet(); }
|
||||
@@ -386,7 +513,7 @@ internal sealed class SamplePartition : IDisposable
|
||||
_hub.UntrackSource();
|
||||
}
|
||||
private void ReleaseCache(SourceState state) { if (state.Sample is not null) { state.Sample.Dispose(); state.Sample = null; _hub.Resources.Release(state.SampleCharge); state.SampleCharge = 0; } }
|
||||
private void ReleasePending(Pending p) { p.Sample?.Dispose(); _hub.Resources.Release(p.Charge); }
|
||||
private void ReleasePending(Pending p) { p.Sample?.Dispose(); _hub.Resources.Release(p.Charge); if (p.Owner is not null) ReleaseMetadataOwner(p.Owner); }
|
||||
|
||||
internal CachedSourceRead Read(SourceTicket ticket) => Read(ticket, out _);
|
||||
internal CachedSourceRead Read(SourceTicket ticket, out long watermark)
|
||||
@@ -396,12 +523,12 @@ internal sealed class SamplePartition : IDisposable
|
||||
watermark = _watermark;
|
||||
if (_disposed || !Current(ticket, out var state)) return _hub.EmptyRead(DataItemStatus.NotFound, DataReadiness.Unavailable, _epoch);
|
||||
if (state.Readiness != DataReadiness.Ready || state.Sample is null)
|
||||
return _hub.EmptyRead(state.Readiness == DataReadiness.WaitingForInitialData ? DataItemStatus.WaitingForInitialData : DataItemStatus.Unavailable, state.Readiness, _epoch);
|
||||
{ SourceIngressDiagnostics.Count(SourceIngressDiagnostics.Event.ReadNotReady); return _hub.EmptyRead(state.Readiness == DataReadiness.WaitingForInitialData ? DataItemStatus.WaitingForInitialData : DataItemStatus.Unavailable, state.Readiness, _epoch); }
|
||||
var gate = Volatile.Read(ref state.Gate);
|
||||
var signal = Volatile.Read(ref gate.Invalidated);
|
||||
if (signal != state.AppliedInvalidations) return _hub.EmptyRead(DataItemStatus.Unavailable, DataReadiness.Invalidated, _epoch);
|
||||
if (signal != state.AppliedInvalidations) { SourceIngressDiagnostics.Count(SourceIngressDiagnostics.Event.ReadInvalidated); return _hub.EmptyRead(DataItemStatus.Unavailable, DataReadiness.Invalidated, _epoch); }
|
||||
var charge = checked(state.Sample.Value.EstimatedRetainedBytes + PartitionedSampleHub.ReferenceOverhead);
|
||||
if (!_hub.Resources.TryReserve(charge)) return _hub.EmptyRead(DataItemStatus.ResourceExhausted, DataReadiness.Unavailable, _epoch);
|
||||
if (!_hub.Resources.TryReserve(charge)) { SourceIngressDiagnostics.Count(SourceIngressDiagnostics.Event.ReadExhausted); return _hub.EmptyRead(DataItemStatus.ResourceExhausted, DataReadiness.Unavailable, _epoch); }
|
||||
Interlocked.Increment(ref state.Readers);
|
||||
Interlocked.Increment(ref state.MetadataOwners);
|
||||
return new CachedSourceRead(_hub.Resources, DataItemStatus.Good, state.Readiness,
|
||||
@@ -477,15 +604,15 @@ internal sealed class SamplePartition : IDisposable
|
||||
scanned = 0;
|
||||
lock (_gate)
|
||||
{
|
||||
if (_disposed || epoch != _epoch) return false;
|
||||
if (_disposed || epoch != _epoch) { SourceIngressDiagnostics.Count(SourceIngressDiagnostics.Event.InterestEpoch); return false; }
|
||||
if (position == _watermark) return true;
|
||||
if (!_changeIndex.TryGetValue(position + 1, out var node)) return false;
|
||||
if (!_changeIndex.TryGetValue(position + 1, out var node)) { SourceIngressDiagnostics.Count(SourceIngressDiagnostics.Event.InterestHistoryGap); return false; }
|
||||
while (node is not null && scanned < maximum)
|
||||
{
|
||||
var change = node.Value; position = change.Version; scanned++;
|
||||
if (tickets.TryGetValue(change.Key, out var ticket))
|
||||
{
|
||||
if (!Current(ticket, out _)) return false;
|
||||
if (!Current(ticket, out _)) { SourceIngressDiagnostics.Count(SourceIngressDiagnostics.Event.InterestTicket); return false; }
|
||||
changes.Add(new(ticket, change.Version));
|
||||
}
|
||||
node = node.Next;
|
||||
@@ -519,19 +646,26 @@ internal sealed class SamplePartition : IDisposable
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Interlocked.Or(ref _producerState, int.MinValue);
|
||||
lock (_gate)
|
||||
{
|
||||
if (_disposed) return; _disposed = true; InvalidatePartition(); CleanupInvalidatedEpoch();
|
||||
while (_queue.TryDequeue(out var p)) ReleasePending(p);
|
||||
foreach (var p in _dirty.Values) ReleasePending(p);
|
||||
_dirty.Clear(); _queuedValues = 0; _queuedControls = 0;
|
||||
while (_mailbox.TryDequeue(out var value)) { Interlocked.Decrement(ref _queuedValues); ReleasePending(value); }
|
||||
while (_dirty.TryDequeue(out var dirtyState))
|
||||
{
|
||||
Interlocked.Decrement(ref _dirtyCount);
|
||||
var dirty = Interlocked.Exchange(ref dirtyState.Dirty, null);
|
||||
if (dirty is not null) ReleasePending(dirty);
|
||||
}
|
||||
_queuedControls = 0;
|
||||
foreach (var pair in _sources) { pair.Value.Removed = true; RetireSourceState(pair.Value); }
|
||||
_sources.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class SourceGate(SourceGeneration generation, SourceBindingFence binding)
|
||||
{ public readonly SourceGeneration Generation = generation; public readonly SourceBindingFence Binding = binding; public long Invalidated; public int Retired; }
|
||||
{ public readonly SourceGeneration Generation = generation; public readonly SourceBindingFence Binding = binding; public long Invalidated, ProducerStamp, LastSubmitted; public int Retired, Committing; }
|
||||
private sealed class SourceState(SourceTicket ticket, long sourceCharge, ProducerFence? fence, long partitionEpoch)
|
||||
{
|
||||
public SourceTicket Ticket = ticket; public SourceGeneration Generation = ticket.Generation; public readonly long SourceCharge = sourceCharge;
|
||||
@@ -543,7 +677,7 @@ internal sealed class SamplePartition : IDisposable
|
||||
public int Readers; public int Reservations; public int Snapshots;
|
||||
public bool Removed; public DataReadiness Readiness = DataReadiness.WaitingForInitialData;
|
||||
public DataSample? Sample; public long SampleCharge; public long Received;
|
||||
public long LastSubmitted; public long LastApplied; public long Barrier; public long InvalidationVersion;
|
||||
public long LastApplied; public long Barrier; public long InvalidationVersion; public Pending? Dirty;
|
||||
}
|
||||
private sealed class Pending
|
||||
{
|
||||
@@ -553,14 +687,15 @@ internal sealed class SamplePartition : IDisposable
|
||||
{ Ticket = ticket; Sequence = sequence; Ordinal = ordinal; Status = status; SourceTimestamp = source; ServerTimestamp = server; WasReady = wasReady; Charge = charge; IsControl = true; IsStatus = true; }
|
||||
public Pending(SourceTicket ticket, long sequence, long ordinal, long charge) { Ticket = ticket; Sequence = sequence; Ordinal = ordinal; Charge = charge; IsControl = true; }
|
||||
public SourceTicket Ticket; public long Sequence; public long Ordinal; public DataSample? Sample; public long Charge; public long Received; public long AdmissionEpoch; public long PartitionEpoch;
|
||||
public SourceState? Owner; public SourceGate? Gate; public long ProducerStamp, SignalStamp;
|
||||
public bool IsControl; public bool IsStatus; public uint Status; public DateTimeOffset? SourceTimestamp; public DateTimeOffset? ServerTimestamp; public bool WasReady;
|
||||
}
|
||||
private sealed class Reservation(SamplePartition partition, SourceState state, SourceTicket ticket, long estimate, long charge,
|
||||
long invalidationVersion, long partitionEpoch, long receivedTimestamp) : IIngressReservation
|
||||
private sealed class Reservation(SamplePartition partition, SourceState state, SourceGate gate, SourceTicket ticket, long estimate, long charge,
|
||||
long producerStamp, long signal, long partitionEpoch, long receivedTimestamp) : IIngressReservation
|
||||
{
|
||||
private int _completed;
|
||||
public bool TryCommit(DataSample owned, long sequence)
|
||||
{ ArgumentNullException.ThrowIfNull(owned); return Interlocked.Exchange(ref _completed, 1) == 0 && partition.Commit(state, ticket, estimate, charge, invalidationVersion, partitionEpoch, receivedTimestamp, owned, sequence); }
|
||||
{ ArgumentNullException.ThrowIfNull(owned); return Interlocked.Exchange(ref _completed, 1) == 0 && partition.Commit(state, gate, ticket, estimate, charge, producerStamp, signal, partitionEpoch, receivedTimestamp, owned, sequence); }
|
||||
public void Dispose() { if (Interlocked.Exchange(ref _completed, 1) == 0) partition.Abandon(state, charge); }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user