fix(dcl): derive tag-resolution health counts from per-tag authoritative state

Closes arch-review remediation residual #1 (DCL unsubscribe-during-reconnect
count staleness).

DataConnectionActor tracked TotalSubscribedTags/ResolvedTags as two int fields
incremented and decremented at five independent sites. ReSubscribeAll clears the
very maps those decrements key off (_subscriptionIds, _unresolvedTags) while
deliberately preserving _subscriptionsByInstance, so an unsubscribe landing
inside a reconnect window matched NEITHER decrement branch: the total leaked +1
per subscribe/reconnect/unsubscribe churn cycle, permanently and cumulatively.
The 37f13e2e discard gate stopped the orphan-handle half of that race; it could
not stop the counters drifting, because they were state of their own.

Both counts are now DERIVED at report time from the authoritative per-tag
collections, which makes the drift unrepresentable rather than merely guarded:

  total    = _instancesByTag.Count   (the per-tag counted set the residual
                                      called for — distinct tags with at least
                                      one subscribing instance)
  resolved = _subscriptionIds.Count  (tags for which the adapter holds a handle)

Two semantic corrections fall out of the derivation:

- A tag whose subscribe failed at CONNECTION level now counts toward the total.
  It was excluded before, yet the reconnect re-subscribe re-issued it from
  _subscriptionsByInstance and booked it as resolved — resolved above total, and
  a total driven negative by the eventual unsubscribe.
- _tagSubscriberCount is deleted. It duplicated _instancesByTag exactly, so
  HandleUnsubscribe's last-subscriber test is now "did UnindexTag drop the key?"
  — still O(1), with no parallel count that can disagree about when a handle is
  released. The subscribe-success promotion split (fresh vs. unresolved→resolved)
  also goes: it existed only to pick which scalar to bump; set sizes get
  DataConnectionLayer-020's double-count cases right for free.

Behavior is otherwise unchanged — same logging, same handle release, same
unresolved-tag probing, same in-flight-unsubscribe discard semantics (the long
comment block there is updated for the mechanics that changed).

Tests: five TagResolutionCounts_* cases in DataConnectionActorBatchTests
covering the churn repro (3 cycles), a shared tag losing one instance mid
reconnect, connection-level failure then recovery, plain subscribe/unsubscribe
cycles, and a completed reconnect re-subscribe. Verified failing against the
pre-fix actor (churn: total 1 not 0; connection-level: total 0 not 1) and
passing after. Full DCL suite 319/319; solution builds with 0 warnings.

Docs: Component-DataConnectionLayer.md health-reporting section describes the
derived counts; residuals register item 1 marked RESOLVED.
This commit is contained in:
Joseph Doherty
2026-08-15 02:05:35 -04:00
parent 986e6e7ad5
commit 491df111ea
5 changed files with 389 additions and 125 deletions
@@ -31,8 +31,13 @@ public sealed class FakeBatchDataConnection
/// <summary>Tags reported as failed rows (per-tag resolution failure).</summary>
public readonly HashSet<string> FailingTags = new(StringComparer.Ordinal);
/// <summary>When set, every batch subscribe throws this — a batch-level fault.</summary>
public Func<Exception>? BatchSubscribeThrows;
/// <summary>
/// When set, a batch subscribe throws whatever this returns — a batch-level fault.
/// Returning <c>null</c> lets that call through, so a test can make the fault TRANSIENT
/// (e.g. fail the initial subscribe at connection level, then let the reconnect
/// re-subscribe succeed).
/// </summary>
public Func<Exception?>? BatchSubscribeThrows;
/// <summary>When true, bulk reads never return until the caller's token cancels.</summary>
public bool HangReads;
/// <summary>
@@ -81,8 +86,8 @@ public sealed class FakeBatchDataConnection
SubscribeBatchTimes.Enqueue(DateTimeOffset.UtcNow);
ValueCallback = callback;
if (BatchSubscribeThrows is { } factory)
throw factory();
if (BatchSubscribeThrows?.Invoke() is { } fault)
throw fault;
if (SubscribeGate is { } gate)
await gate;