fix(runtime): bound DependencyMux last-value retention to replay-opting refs (fix round 1 I2)

DependencyMuxActor retained the last value of every ref ever registered,
and never removed the entries. The continuous-historization recorder
registers every historized ref, so on a historizing node the map grew
with the historized address space and never shrank. The class remark
claimed a bound that did not hold.

- Retain only refs that at least one subscriber registered with
  ReplayRetained: true names (VT actors, the Calculation mux adapter, the
  scripted-alarm host). A non-opting subscriber is still delivered to but
  retains nothing.
- When a ref's last replay-opting subscriber leaves, its value is kept
  for a grace (default 2 min, Props(retentionGrace)) so a restart
  (old adapter gone, new one not yet registered) still replays. A
  periodic sweep then drops it along with the entry. Entries with no
  subscriber and nothing retained are removed.
- New SourceRemoved(DriverInstanceId) evicts the values a removed driver
  produced, so its last Good is never replayed. The mux records the
  publishing driver per retained ref. No production sender exists yet:
  DriverHostActor is outside this lane (follow-up).
- Remark rewritten. Tests: retention bounded to replay-opting refs,
  release after grace (with an in-grace replay as the positive control),
  SourceRemoved eviction, and the restart-replay scenario still green.

Refs #734

Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
This commit is contained in:
Joseph Doherty
2026-10-22 13:31:41 -04:00
parent 6aa5edaf30
commit ad6be56850
2 changed files with 286 additions and 60 deletions
@@ -12,10 +12,13 @@ using ZB.MOM.WW.OtOpcUa.Runtime.VirtualTags;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.VirtualTags;
/// <summary>
/// R07:H6 — the mux retains the last value per ever-registered ref and replays it to a registration
/// that asks (<see cref="DependencyMuxActor.RegisterInterest.ReplayRetained"/>). A Calculation driver
/// Restart (new driver + new adapter) therefore republishes from current inputs instead of sitting
/// <c>BadWaitingForInitialData</c> until every input happens to change.
/// R07:H6 — the mux retains the last value of every ref a REPLAY-OPTING subscriber
/// (<see cref="DependencyMuxActor.RegisterInterest.ReplayRetained"/>) is registered for, keeps it for a
/// short grace after the last such subscriber leaves, and replays it to a registration that asks. A
/// Calculation driver Restart (new driver + new adapter) therefore republishes from current inputs instead
/// of sitting <c>BadWaitingForInitialData</c> until every input happens to change. Fix round 1 (I2): a
/// non-opting subscriber (the historization recorder) retains nothing, retention is released after the
/// grace, and a removed source's values are evicted so its last Good is never replayed.
/// </summary>
public sealed class DependencyMuxReplayTests : RuntimeActorTestBase
{
@@ -27,7 +30,7 @@ public sealed class DependencyMuxReplayTests : RuntimeActorTestBase
{
var mux = Sys.ActorOf(DependencyMuxActor.Props());
var first = CreateTestProbe();
mux.Tell(new DependencyMuxActor.RegisterInterest(new[] { "a", "b" }, first.Ref));
mux.Tell(new DependencyMuxActor.RegisterInterest(new[] { "a", "b" }, first.Ref, ReplayRetained: true));
mux.Tell(Burst(("a", 1), ("b", 2), ("c", 3)));
first.ExpectMsg<VirtualTagActor.DependencyValueBatch>(PresenceBudget).Items.Count.ShouldBe(2);
@@ -38,7 +41,7 @@ public sealed class DependencyMuxReplayTests : RuntimeActorTestBase
mux.Tell(new DependencyMuxActor.RegisterInterest(new[] { "a", "c" }, second.Ref, ReplayRetained: true));
var replay = second.ExpectMsg<VirtualTagActor.DependencyValueBatch>(PresenceBudget);
replay.Items.Select(i => i.TagId).ShouldBe(new[] { "a" }, "'c' was never registered by anyone, so it is not retained; 'b' is not in this subscriber's refs");
replay.Items.Select(i => i.TagId).ShouldBe(new[] { "a" }, "'c' was never registered by a replay-opting subscriber, so it is not retained; 'b' is not in this subscriber's refs");
replay.Items[0].Value.ShouldBe(1);
}
@@ -60,6 +63,86 @@ public sealed class DependencyMuxReplayTests : RuntimeActorTestBase
fresh.ExpectNoMsg(TimeSpan.FromMilliseconds(300));
}
/// <summary>I2 — a subscriber that did NOT opt into replay (the continuous-historization recorder registers
/// every historized ref) retains nothing; retention is bounded to the refs a replay-opting subscriber named.</summary>
[Fact]
public void Only_refs_named_by_a_replay_opting_subscriber_are_retained()
{
var mux = ActorOfAsTestActorRef<DependencyMuxActor>(DependencyMuxActor.Props());
var recorder = CreateTestProbe();
var calc = CreateTestProbe();
mux.Tell(new DependencyMuxActor.RegisterInterest(new[] { "h1", "h2", "h3", "shared" }, recorder.Ref));
mux.Tell(new DependencyMuxActor.RegisterInterest(new[] { "r", "shared" }, calc.Ref, ReplayRetained: true));
mux.Tell(Burst(("h1", 1), ("h2", 2), ("h3", 3), ("r", 4), ("shared", 5)));
recorder.ExpectMsg<VirtualTagActor.DependencyValueBatch>(PresenceBudget).Items.Count.ShouldBe(4, "the recorder is still DELIVERED to");
calc.ExpectMsg<VirtualTagActor.DependencyValueBatch>(PresenceBudget);
mux.UnderlyingActor.RetainedRefCount.ShouldBe(2, "only 'r' and 'shared' — the recorder-only refs retain nothing");
var late = CreateTestProbe();
mux.Tell(new DependencyMuxActor.RegisterInterest(new[] { "h1", "r" }, late.Ref, ReplayRetained: true));
late.ExpectMsg<VirtualTagActor.DependencyValueBatch>(PresenceBudget)
.Items.Select(i => i.TagId).ShouldBe(new[] { "r" }, "positive control: the opted-in ref replays; 'h1' was never retained");
// Unregistering everyone ends up with no entries at all once the grace is over — tested below; here
// the recorder leaving changes nothing about retention.
mux.Tell(new DependencyMuxActor.UnregisterInterest(recorder.Ref));
mux.UnderlyingActor.RetainedRefCount.ShouldBe(2);
}
/// <summary>I2 — when the last replay-opting subscriber of a ref leaves, its retained value survives a short
/// grace (the restart window: old adapter gone, new one not yet registered) and is then dropped.</summary>
[Fact]
public void Retention_is_released_after_the_grace_once_no_replay_subscriber_remains()
{
var mux = ActorOfAsTestActorRef<DependencyMuxActor>(DependencyMuxActor.Props(TimeSpan.FromMilliseconds(200)));
var gen1 = CreateTestProbe();
mux.Tell(new DependencyMuxActor.RegisterInterest(new[] { "a" }, gen1.Ref, ReplayRetained: true));
mux.Tell(Burst(("a", 1)));
gen1.ExpectMsg<VirtualTagActor.DependencyValueBatch>(PresenceBudget);
mux.Tell(new DependencyMuxActor.UnregisterInterest(gen1.Ref));
// Positive control: inside the grace a returning subscriber still gets the value.
var gen2 = CreateTestProbe();
mux.Tell(new DependencyMuxActor.RegisterInterest(new[] { "a" }, gen2.Ref, ReplayRetained: true));
gen2.ExpectMsg<VirtualTagActor.DependencyValueBatch>(PresenceBudget).Items.Single().Value.ShouldBe(1);
mux.Tell(new DependencyMuxActor.UnregisterInterest(gen2.Ref));
// Past the grace: nothing retained, no entry left behind, and a returning subscriber replays nothing.
AwaitAssert(() =>
{
mux.UnderlyingActor.RetainedRefCount.ShouldBe(0);
mux.UnderlyingActor.TrackedRefCount.ShouldBe(0);
}, PresenceBudget);
var gen3 = CreateTestProbe();
mux.Tell(new DependencyMuxActor.RegisterInterest(new[] { "a" }, gen3.Ref, ReplayRetained: true));
gen3.ExpectNoMsg(TimeSpan.FromMilliseconds(300));
}
/// <summary>I2 — a removed source's retained values are evicted, so its last Good is never replayed to a
/// subscriber that (re)registers after the removal. Values from other sources are untouched.</summary>
[Fact]
public void SourceRemoved_evicts_that_sources_retained_values_only()
{
var mux = ActorOfAsTestActorRef<DependencyMuxActor>(DependencyMuxActor.Props());
var sub = CreateTestProbe();
mux.Tell(new DependencyMuxActor.RegisterInterest(new[] { "a", "b" }, sub.Ref, ReplayRetained: true));
mux.Tell(new DriverInstanceActor.AttributeValueBatchPublished("driver-1",
[new DriverInstanceActor.AttributeValueItem("a", 1, OpcUaStatus.Good, DateTime.UtcNow)]));
mux.Tell(new DriverInstanceActor.AttributeValueBatchPublished("driver-2",
[new DriverInstanceActor.AttributeValueItem("b", 2, OpcUaStatus.Good, DateTime.UtcNow)]));
sub.ExpectMsg<VirtualTagActor.DependencyValueBatch>(PresenceBudget);
sub.ExpectMsg<VirtualTagActor.DependencyValueBatch>(PresenceBudget);
mux.Tell(new DependencyMuxActor.SourceRemoved("driver-1"));
mux.UnderlyingActor.RetainedRefCount.ShouldBe(1);
var restarted = CreateTestProbe();
mux.Tell(new DependencyMuxActor.RegisterInterest(new[] { "a", "b" }, restarted.Ref, ReplayRetained: true));
restarted.ExpectMsg<VirtualTagActor.DependencyValueBatch>(PresenceBudget)
.Items.Select(i => i.TagId).ShouldBe(new[] { "b" }, "driver-1 was removed: its last Good must not be replayed");
}
/// <summary>The user-facing scenario: a calc tag computes, the driver is restarted (new instance + new
/// adapter) with NO input changing afterwards, and the new instance republishes the current value.</summary>
[Fact]