Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataMetadataRenewalCacheCapacityTests.cs
T

218 lines
12 KiB
C#

using System.Collections;
using System.Reflection;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
public sealed partial class GrpcDataMetadataRenewalCacheTests
{
[Theory, Trait("Phase", "P11Renewal")]
[InlineData(false)]
[InlineData(true)]
public void Only_the_same_expired_overlap_can_add_a_recovery_pin_without_a_new_cache_entry(bool sweepExpired)
{
using var fixture = new RenewalCacheFixture();
var predecessor = fixture.Install();
fixture.Cache.TryPinProjected(DataRealm.Raw, predecessor.LocalIdentifier, out var oldPin).ShouldBeTrue();
fixture.Clock.Advance(TimeSpan.FromSeconds(5));
fixture.Cache.TryRenewOverlap(predecessor, fixture.Proof(), 1, out var successor).ShouldBeTrue();
var current = successor.ShouldNotBeNull();
fixture.Cache.TryPinProjected(DataRealm.Raw, current.LocalIdentifier, out var currentPin).ShouldBeTrue();
try
{
fixture.Clock.Advance(TimeSpan.FromSeconds(16));
predecessor.Lease.IsValid.ShouldBeFalse(); current.Lease.IsValid.ShouldBeFalse();
if (sweepExpired) Sweep(fixture);
var before = RetainedBytes(fixture);
fixture.Cache.TryPinExpiredRecoveryPair(predecessor, current, out var recoveryPin).ShouldBeTrue();
var ownedRecoveryPin = recoveryPin.ShouldNotBeNull();
RetainedBytes(fixture).ShouldBe(before, "the recovery pin cannot admit another physical cache entry");
fixture.Cache.IsUsableProof(predecessor).ShouldBeFalse(); fixture.Cache.IsUsableProof(current).ShouldBeFalse();
ownedRecoveryPin.Dispose();
RetainedBytes(fixture).ShouldBe(before);
}
finally { oldPin?.Dispose(); currentPin?.Dispose(); }
}
[Theory, Trait("Phase", "P11Renewal")]
[InlineData("both_live")]
[InlineData("successor_live")]
[InlineData("route_dead")]
[InlineData("binding_dead")]
[InlineData("generation_dead")]
[InlineData("invalidated")]
[InlineData("unrelated")]
[InlineData("route_mismatch")]
[InlineData("binding_mismatch")]
[InlineData("target_mismatch")]
[InlineData("serving_mismatch")]
[InlineData("schema_mismatch")]
public void Invalid_recovery_pair_never_adds_a_pin_or_charge(string failure)
{
using var fixture = new RenewalCacheFixture();
var predecessor = fixture.Install();
fixture.Cache.TryPinProjected(DataRealm.Raw, predecessor.LocalIdentifier, out var oldPin).ShouldBeTrue();
fixture.Clock.Advance(TimeSpan.FromSeconds(5));
fixture.Cache.TryRenewOverlap(predecessor, fixture.Proof(), 1, out var successor).ShouldBeTrue();
var current = successor.ShouldNotBeNull();
fixture.Cache.TryPinProjected(DataRealm.Raw, current.LocalIdentifier, out var currentPin).ShouldBeTrue();
try
{
var candidate = failure switch
{
"unrelated" => fixture.Proof(),
"route_mismatch" => fixture.Proof("route"),
"binding_mismatch" => fixture.Proof("binding"),
"target_mismatch" => fixture.Proof("target"),
"serving_mismatch" => fixture.Proof("role"),
"schema_mismatch" => current with { Metadata = current.Metadata with { BrowseName = "different" } },
_ => current
};
if (failure == "successor_live") fixture.Clock.Advance(TimeSpan.FromSeconds(10));
else if (failure != "both_live") fixture.Clock.Advance(TimeSpan.FromSeconds(16));
switch (failure)
{
case "route_dead": fixture.RouteCurrent = false; break;
case "binding_dead": fixture.BindingCurrent = false; break;
case "generation_dead": fixture.Cache.ForgetMapping("mapping"); break;
case "invalidated": fixture.Invalidate(predecessor); break;
}
var before = RetainedBytes(fixture);
fixture.Cache.TryPinExpiredRecoveryPair(predecessor, candidate, out var pin).ShouldBeFalse();
pin.ShouldBeNull();
RetainedBytes(fixture).ShouldBe(before, "a rejected pairing cannot add a cache charge");
}
finally { oldPin?.Dispose(); currentPin?.Dispose(); }
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void Browse_refuses_cross_serving_replacement_of_a_healthy_or_different_placement(bool expiredWithDifferentPermissions)
{
using var fixture = new RenewalCacheFixture(clusterTarget: true);
var original = fixture.Install();
if (expiredWithDifferentPermissions) fixture.Clock.Advance(TimeSpan.FromSeconds(16));
var incoming = fixture.Proof(expiredWithDifferentPermissions ? "acl" : null, original.Lease.Serving with
{
NodeId = "node-b", Incarnation = "serving-2", ServingGeneration = 2
});
var before = fixture.Cache.CaptureRetainedDiagnostics(DataRealm.Raw, original.LocalIdentifier);
before.Retired.ShouldBeFalse();
before.RemoteLeaseValid.ShouldBe(!expiredWithDifferentPermissions);
var beforeBytes = RetainedBytes(fixture);
var commits = 0;
(bool Success, string? Cursor) Commit() { commits++; return (true, null); }
// With one allowed entry, the expired case would fit if admission
// first swept that proof and then compared only the surviving entries.
fixture.TryPublishPage(incoming, Commit, out var committed, maximumMappingNodes: 1).ShouldBeFalse();
committed.ShouldBeNull();
commits.ShouldBe(0);
fixture.Cache.CaptureRetainedDiagnostics(DataRealm.Raw, original.LocalIdentifier).ShouldBe(before);
RetainedBytes(fixture).ShouldBe(beforeBytes);
if (!expiredWithDifferentPermissions)
fixture.Cache.IsCurrentProof(original).ShouldBeTrue();
}
[Theory]
[InlineData(2)]
[InlineData(3)]
public void Browse_replaces_an_expired_identical_placement_but_refuses_a_third_charged_serving(int maximumMappingNodes)
{
using var fixture = new RenewalCacheFixture(clusterTarget: true);
var predecessor = fixture.Install();
var identifier = predecessor.LocalIdentifier;
fixture.Cache.TryPinProjected(DataRealm.Raw, identifier, out var predecessorPin).ShouldBeTrue();
using var ownedPredecessor = predecessorPin.ShouldNotBeNull();
fixture.Clock.Advance(TimeSpan.FromSeconds(16));
var successor = fixture.Proof(alternateServing: predecessor.Lease.Serving with
{
NodeId = "node-b", Incarnation = "serving-2", ServingGeneration = 2
});
var commits = 0;
(bool Success, string? Cursor) Commit() { commits++; return (true, null); }
fixture.TryPublishPage(successor, Commit, out var accepted).ShouldBeTrue();
var current = accepted.ShouldNotBeNull().Items.ShouldHaveSingleItem();
commits.ShouldBe(1);
fixture.Cache.IsCurrentProof(current).ShouldBeTrue();
fixture.Cache.IsUsableProof(predecessor).ShouldBeFalse();
fixture.Cache.TryPinProjected(DataRealm.Raw, identifier, out var currentPin).ShouldBeTrue();
using var ownedCurrent = currentPin.ShouldNotBeNull();
fixture.Clock.Advance(TimeSpan.FromSeconds(16));
const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
var cacheType = fixture.Cache.GetType();
var indexed = (IDictionary)cacheType.GetField("_nodes", flags)!.GetValue(fixture.Cache)!;
var retired = (IDictionary)cacheType.GetField("_retiredByProof", flags)!.GetValue(fixture.Cache)!;
var mappingCounts = (IDictionary)cacheType.GetField("_mappingNodeCounts", flags)!.GetValue(fixture.Cache)!;
var nodeKey = (DataRealm.Raw, identifier);
var currentEntry = indexed[nodeKey].ShouldNotBeNull();
var predecessorEntry = retired[predecessor].ShouldNotBeNull();
var pins = currentEntry.GetType().GetField("Pins", flags)!;
pins.GetValue(currentEntry).ShouldBe(1);
pins.GetValue(predecessorEntry).ShouldBe(1);
retired.Count.ShouldBe(1);
mappingCounts["mapping"].ShouldBe(2);
var beforeBytes = RetainedBytes(fixture);
var incoming = fixture.Proof(alternateServing: current.Lease.Serving with
{
NodeId = "node-c", Incarnation = "serving-3", ServingGeneration = 3
});
fixture.TryPublishPage(incoming, Commit, out var rejected, maximumMappingNodes).ShouldBeFalse();
rejected.ShouldBeNull();
commits.ShouldBe(1);
indexed[nodeKey].ShouldBeSameAs(currentEntry);
retired[predecessor].ShouldBeSameAs(predecessorEntry);
pins.GetValue(currentEntry).ShouldBe(1);
pins.GetValue(predecessorEntry).ShouldBe(1);
retired.Count.ShouldBe(1);
mappingCounts["mapping"].ShouldBe(2);
RetainedBytes(fixture).ShouldBe(beforeBytes);
}
private static long RetainedBytes(RenewalCacheFixture fixture) =>
(long)fixture.Cache.GetType().GetField("_retainedBytes", BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(fixture.Cache)!;
private static void Sweep(RenewalCacheFixture fixture)
{
const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
var gate = fixture.Cache.GetType().GetField("_sync", flags)!.GetValue(fixture.Cache)!;
lock (gate) fixture.Cache.GetType().GetMethod("ReclaimExpired", flags)!.Invoke(fixture.Cache, null);
}
[Fact, Trait("Phase", "P11Renewal")]
public void Two_pinned_retired_proofs_cannot_admit_a_third_physical_entry()
{
using var fixture = new RenewalCacheFixture();
var predecessor = fixture.Install();
fixture.Cache.TryPinProjected(DataRealm.Raw, predecessor.LocalIdentifier, out var oldPin).ShouldBeTrue();
fixture.Clock.Advance(TimeSpan.FromSeconds(5));
fixture.Cache.TryRenewOverlap(predecessor, fixture.Proof(), 1, out var successor).ShouldBeTrue();
var current = successor.ShouldNotBeNull();
fixture.Cache.TryPinProjected(DataRealm.Raw, current.LocalIdentifier, out var currentPin).ShouldBeTrue();
try
{
fixture.Clock.Advance(TimeSpan.FromSeconds(16));
predecessor.Lease.IsValid.ShouldBeFalse(); current.Lease.IsValid.ShouldBeFalse();
const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
var gate = fixture.Cache.GetType().GetField("_sync", flags)!.GetValue(fixture.Cache)!;
lock (gate)
{
fixture.Cache.GetType().GetMethod("ReclaimExpired", flags)!.Invoke(fixture.Cache, null);
var retired = (IDictionary)fixture.Cache.GetType().GetField("_retiredPerNode", flags)!.GetValue(fixture.Cache)!;
retired.Values.Cast<int>().ShouldHaveSingleItem().ShouldBe(2);
}
fixture.Cache.TryRenewExact(current, fixture.Proof(), 2, out _).ShouldBeFalse(
"two separately pinned physical predecessors exhaust the per-node ownership limit even after the live entry was swept");
oldPin!.Dispose(); oldPin = null;
fixture.Cache.TryRenewExact(current, fixture.Proof(), 2, out var recovered).ShouldBeTrue(
"after one owner releases its own pin, one predecessor and its replacement fit the bound");
recovered.ShouldNotBeNull();
}
finally { oldPin?.Dispose(); currentPin?.Dispose(); }
}
}