fix(dcl): seeds never overwrite a newer live value; Good on reconnect resets only the subscribe backoff (DCL-1, SRT-6, WP3 tasks 3.1 + 3.8)

3.1: seed values are flagged on TagValueReceived; a reconnect/resolution
re-seed for a tag that already delivered a live value in the current
adapter generation is dropped, an initial-subscribe seed is replaced by the
latest live value (a joining instance may have missed earlier live values).
3.8: the connection actor tells subscribers ConnectionQualityChanged(Good)
on re-entering Connected; InstanceActor's Good branch only resets the
Lever-1 backoff and no longer stamps bound attributes Good.
This commit is contained in:
Joseph Doherty
2026-12-03 09:49:57 -05:00
parent 1aa8710e72
commit 6adfc9f449
8 changed files with 411 additions and 13 deletions
@@ -1260,6 +1260,7 @@ public partial class DataConnectionActor : UntypedActor, IWithTimers
ReSubscribeAllAlarms();
BecomeConnected();
PushConnectionRestored();
}
else
{
@@ -1970,7 +1971,7 @@ public partial class DataConnectionActor : UntypedActor, IWithTimers
foreach (var seed in msg.SeedValues)
{
if (_subscriptionIds.ContainsKey(seed.TagPath))
Self.Tell(new TagValueReceived(seed.TagPath, seed.Value, _adapterGeneration));
Self.Tell(new TagValueReceived(seed.TagPath, seed.Value, _adapterGeneration, SeedKind.InitialSubscribe));
}
}
@@ -2633,7 +2634,7 @@ public partial class DataConnectionActor : UntypedActor, IWithTimers
{
var seeds = await SeedTagsAsync(adapter, resolved);
foreach (var seed in seeds)
self.Tell(new TagValueReceived(seed.TagPath, seed.Value, generation));
self.Tell(new TagValueReceived(seed.TagPath, seed.Value, generation, SeedKind.Reseed));
}
catch (Exception ex)
{
@@ -2819,6 +2820,27 @@ public partial class DataConnectionActor : UntypedActor, IWithTimers
ScheduleTagResolutionRetry();
}
// ── Connection-restored signal ──
/// <summary>
/// SRT-6 / WP3 task 3.8: tells every subscriber <c>ConnectionQualityChanged(Good)</c> when the
/// connection re-enters Connected after a reconnect. It is a RESTORE SIGNAL only — the Instance
/// Actor uses it to reset its Lever-1 tag-subscribe backoff so a subscribe that failed during
/// the outage is retried at the floor instead of up to <c>TagSubscribeRetryMaxIntervalMs</c>
/// later. It deliberately says nothing about any tag's value: per-tag quality is restored by
/// the re-subscribe and the re-seed, and the Instance Actor must not stamp bound attributes
/// Good with their last-known values before the device re-reports.
/// </summary>
private void PushConnectionRestored()
{
var now = DateTimeOffset.UtcNow;
foreach (var instanceName in _subscriptionsByInstance.Keys)
{
if (_subscribers.TryGetValue(instanceName, out var subscriber))
subscriber.Tell(new ConnectionQualityChanged(_connectionName, QualityCode.Good, now));
}
}
// ── Bad Quality Push ──
private void PushBadQualityForAllTags()
@@ -2993,7 +3015,7 @@ public partial class DataConnectionActor : UntypedActor, IWithTimers
{
var seeds = await SeedTagsAsync(reseedAdapter, allTags, reseedToken);
foreach (var seed in seeds)
self.Tell(new TagValueReceived(seed.TagPath, seed.Value, generation));
self.Tell(new TagValueReceived(seed.TagPath, seed.Value, generation, SeedKind.Reseed));
}
catch (Exception ex)
{
@@ -3041,9 +3063,24 @@ public partial class DataConnectionActor : UntypedActor, IWithTimers
private void UnindexTag(string tag, string instance)
{
if (_instancesByTag.TryGetValue(tag, out var insts) && insts.Remove(instance) && insts.Count == 0)
{
_instancesByTag.Remove(tag);
_lastLiveValue.Remove(tag);
}
}
/// <summary>
/// DCL-1: the latest LIVE (subscription-delivered, non-seed) value per tag in the generation
/// recorded in <see cref="_liveValuesGeneration"/>. A tag present here has already delivered a
/// live value in the current subscribe generation, so a later seed for it is stale. Cleared
/// lazily when the adapter generation advances (every reconnect / adapter swap); an entry is
/// dropped when the tag loses its last subscriber.
/// </summary>
private readonly Dictionary<string, TagValue> _lastLiveValue = new();
/// <summary>The adapter generation <see cref="_lastLiveValue"/> belongs to.</summary>
private int _liveValuesGeneration = -1;
private void HandleTagValueReceived(TagValueReceived msg)
{
// Drop values delivered by a disposed adapter. After a
@@ -3064,6 +3101,38 @@ public partial class DataConnectionActor : UntypedActor, IWithTimers
if (!_instancesByTag.TryGetValue(msg.TagPath, out var interestedInstances))
return;
// DCL-1: a seed must never overwrite a newer live value. The subscription is live
// before the seed read starts, and a seed is held until its whole seed operation
// completes, so a live change inside that window would otherwise be regressed to the
// older seed value — with Good quality, indefinitely, for a tag that changes rarely.
// The rule is generation-based (decided 2026-12-03), NOT a timestamp comparison,
// which would depend on device/server clocks and the configured timestamp mode.
if (_liveValuesGeneration != _adapterGeneration)
{
_lastLiveValue.Clear();
_liveValuesGeneration = _adapterGeneration;
}
var value = msg.Value;
if (msg.Seed == SeedKind.None)
{
_lastLiveValue[msg.TagPath] = value;
}
else if (_lastLiveValue.TryGetValue(msg.TagPath, out var latestLive))
{
if (msg.Seed == SeedKind.Reseed)
{
// Every interested instance was registered while the subscription was live,
// so each already holds the live value: drop the seed outright.
_log.Debug("[{0}] Dropping seed for {1}: a live value already arrived in generation {2}",
_connectionName, msg.TagPath, _adapterGeneration);
return;
}
// Initial subscribe: the joining instance may have missed live values delivered
// before it registered, so re-deliver the LATEST live value instead of the seed.
value = latestLive;
}
// Fan out to exactly the instances subscribed to this tag via the reverse index —
// O(subscribers of this tag) instead of scanning every instance's tag set per update.
foreach (var instanceName in interestedInstances)
@@ -3071,7 +3140,7 @@ public partial class DataConnectionActor : UntypedActor, IWithTimers
if (_subscribers.TryGetValue(instanceName, out var subscriber))
{
subscriber.Tell(new TagValueUpdate(
_connectionName, msg.TagPath, msg.Value.Value, msg.Value.Quality, msg.Value.Timestamp));
_connectionName, msg.TagPath, value.Value, value.Quality, value.Timestamp));
}
}
@@ -3082,7 +3151,7 @@ public partial class DataConnectionActor : UntypedActor, IWithTimers
// 30s. Now: unchanged quality is a no-op; a genuine transition updates the buckets
// and arms the coalescing flush timer.
var hadPrevious = _lastTagQuality.TryGetValue(msg.TagPath, out var prevQuality);
if (hadPrevious && prevQuality == msg.Value.Quality)
if (hadPrevious && prevQuality == value.Quality)
return;
if (hadPrevious)
@@ -3096,13 +3165,13 @@ public partial class DataConnectionActor : UntypedActor, IWithTimers
}
}
// Increment new quality bucket
switch (msg.Value.Quality)
switch (value.Quality)
{
case QualityCode.Good: _tagsGoodQuality++; break;
case QualityCode.Bad: _tagsBadQuality++; break;
case QualityCode.Uncertain: _tagsUncertainQuality++; break;
}
_lastTagQuality[msg.TagPath] = msg.Value.Quality;
_lastTagQuality[msg.TagPath] = value.Quality;
MarkQualityDirty();
}
@@ -3593,7 +3662,35 @@ public partial class DataConnectionActor : UntypedActor, IWithTimers
/// swapped-out adapter can be dropped (<see cref="IsStaleDisconnect"/>).
/// </summary>
internal record AdapterDisconnected(int AdapterGeneration);
internal record TagValueReceived(string TagPath, TagValue Value, int AdapterGeneration);
/// <summary>
/// A tag value marshalled onto the actor thread. <paramref name="Seed"/> marks a value that
/// came from a seed READ rather than the live subscription (DCL-1): a seed is held until its
/// whole seed operation completes (up to <see cref="DataConnectionOptions.SeedOverallTimeout"/>),
/// so it can be older than a live value the subscription has already delivered.
/// </summary>
internal record TagValueReceived(string TagPath, TagValue Value, int AdapterGeneration, SeedKind Seed = SeedKind.None);
/// <summary>Origin of a <see cref="TagValueReceived"/> (DCL-1).</summary>
internal enum SeedKind
{
/// <summary>A live subscription notification.</summary>
None,
/// <summary>
/// A seed read for tags every interested instance was already registered for while the
/// subscription was live: the reconnect re-seed and the tag-resolution re-seed. Dropped
/// when the tag already delivered a live value in the current generation.
/// </summary>
Reseed,
/// <summary>
/// The seed read of an initial <see cref="SubscribeTagsRequest"/>. The requesting instance
/// may have joined a tag that was ALREADY live for another instance, so it never saw the
/// earlier live values; when the tag has delivered a live value in the current generation
/// the stale seed is replaced by that latest live value rather than dropped outright.
/// </summary>
InitialSubscribe,
}
internal record RetryTagResolution;
/// <summary>Coalescing-timer tick for the tag-quality counter push.</summary>
@@ -1415,11 +1415,16 @@ public class InstanceActor : ReceiveActor
private void HandleConnectionQualityChanged(ConnectionQualityChanged qualityChanged)
{
_logger.LogWarning("Connection {Connection} quality changed to {Quality} for instance {Instance}",
qualityChanged.ConnectionName, qualityChanged.Quality, _instanceUniqueName);
if (qualityChanged.Quality == QualityCode.Good)
_logger.LogInformation("Connection {Connection} restored for instance {Instance}",
qualityChanged.ConnectionName, _instanceUniqueName);
else
_logger.LogWarning("Connection {Connection} quality changed to {Quality} for instance {Instance}",
qualityChanged.ConnectionName, qualityChanged.Quality, _instanceUniqueName);
// Lever 1 (2026-08-28): a connection returning to Good is the Instance Actor's
// only restore signal (the DCL re-subscribes transparently). If our own subscribe
// only restore signal (the DCL re-subscribes transparently; it sends Good when the
// connection re-enters Connected — SRT-6, WP3 task 3.8). If our own subscribe
// on it is still unconfirmed, retry at the floor now rather than in up to
// TagSubscribeRetryMaxIntervalMs — recovery must be prompt. Timer != null ⇔ the
// subscribe is unconfirmed (it is only nulled by a Success); if a request is in
@@ -1436,6 +1441,12 @@ public class InstanceActor : ReceiveActor
ArmTagSubscribeWatchdog(qualityChanged.ConnectionName, subState);
}
// Good is a restore signal ONLY (decided 2026-12-03, SRT-6): it must NOT stamp the
// bound attributes Good with their last-known values — those values are from before
// the outage, and the device has not re-reported yet. Per-tag quality comes back
// through the DCL's re-subscribe and re-seed as ordinary TagValueUpdates.
if (qualityChanged.Quality == QualityCode.Good) return;
if (_configuration == null) return;
// Mark all attributes bound to this connection with the new quality