diff --git a/src/Orleans.Reminders.TestKit/ReminderServiceLifecycleTestRunner.cs b/src/Orleans.Reminders.TestKit/ReminderServiceLifecycleTestRunner.cs
index 8a004ca9a13..b9753defd3e 100644
--- a/src/Orleans.Reminders.TestKit/ReminderServiceLifecycleTestRunner.cs
+++ b/src/Orleans.Reminders.TestKit/ReminderServiceLifecycleTestRunner.cs
@@ -55,6 +55,9 @@ Task WaitForOwnerCountAsync(
/// Returns whether a silo's current ring range owns a grain identity.
bool IsOwner(SiloAddress siloAddress, GrainId grainId);
+ /// Gets a snapshot of a silo's current ring range.
+ IRingRange GetOwnedRange(SiloAddress siloAddress);
+
/// Waits until the current owner has armed its persisted schedule.
Task WaitForScheduleAsync(GrainId grainId, string reminderName, CancellationToken cancellationToken);
@@ -120,6 +123,7 @@ Task RegisterOnSiloAsync(
public abstract class ReminderServiceLifecycleTestRunner
{
private static readonly TimeSpan CleanupPhaseTimeout = TimeSpan.FromSeconds(30);
+ private static readonly TimeSpan IdentitySelectionTimeout = TimeSpan.FromSeconds(30);
private static readonly TimeSpan Period = TimeSpan.FromMinutes(2);
private readonly IReminderServiceLifecycleHarness _harness;
private readonly int _seed;
@@ -412,7 +416,7 @@ await ExecuteWithCleanupAsync(
try
{
joined = await _harness.JoinOneSiloAsync(cancellationToken);
- var grain = CreateGrainOwnedBy(joined, Guarantee);
+ var grain = CreateGrainOwnedBy(joined, Guarantee, cancellationToken);
const string Name = "stale-owner-registration";
reminders.Add((grain, Name));
staleOwner = _harness.ActiveSilos
@@ -590,7 +594,7 @@ await ExecuteWithCleanupAsync(
{
joined = await _harness.JoinOneSiloAsync(cancellationToken);
await _harness.WaitForTopologyReconciliationAsync(cancellationToken);
- var grain = CreateGrainOwnedBy(joined, Guarantee);
+ var grain = CreateGrainOwnedBy(joined, Guarantee, cancellationToken);
const string Name = "join-leave-owner";
reminders.Add((grain, Name));
var due = TimeSpan.FromSeconds(3);
@@ -694,26 +698,101 @@ private IReminderServiceTestGrain CreateGrain(string label)
return _harness.GrainFactory.GetGrain(key);
}
- private IReminderServiceTestGrain CreateGrainOwnedBy(SiloAddress owner, string label)
+ private IReminderServiceTestGrain CreateGrainOwnedBy(
+ SiloAddress owner,
+ string label,
+ CancellationToken cancellationToken)
{
var ordinal = Interlocked.Increment(ref _grainCounter);
- for (var candidate = 0; candidate < ushort.MaxValue; candidate++)
+ var grainType = _harness.GrainFactory
+ .GetGrain(Guid.Empty)
+ .GetGrainId()
+ .Type;
+ var ownerRange = _harness.GetOwnedRange(owner);
+ var attempts = 0;
+ Guid? key;
+ using var selectionCancellation = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
+ selectionCancellation.CancelAfter(IdentitySelectionTimeout);
+ try
{
- var key = ReminderTestData.CreateGuid(
+ key = FindOwnedGrainKey(
_seed,
- $"{ProviderName}/{label}/{ordinal}/{candidate.ToString(CultureInfo.InvariantCulture)}");
- var grain = _harness.GrainFactory.GetGrain(key);
- if (_harness.IsOwner(owner, grain.GetGrainId()))
+ ProviderName,
+ label,
+ ordinal,
+ candidateKey =>
+ {
+ attempts++;
+ var grainId = GrainId.Create(grainType, GrainIdKeyExtensions.CreateGuidKey(candidateKey));
+ return ownerRange.InRange(grainId);
+ },
+ selectionCancellation.Token);
+ }
+ catch (OperationCanceledException exception) when (selectionCancellation.IsCancellationRequested)
+ {
+ if (cancellationToken.IsCancellationRequested)
+ {
+ throw new OperationCanceledException(
+ $"Reminder identity selection for {owner} was canceled after "
+ + $"{attempts.ToString("N0", CultureInfo.InvariantCulture)} deterministic candidates.",
+ exception,
+ cancellationToken);
+ }
+
+ Fail(label, "identity selection")
+ .WithExpected($"a deterministic grain identity owned by {owner}")
+ .WithObserved(
+ $"no owned identity after {attempts.ToString("N0", CultureInfo.InvariantCulture)} candidates "
+ + $"within {IdentitySelectionTimeout}")
+ .Throw();
+ return null!;
+ }
+
+ if (key is null)
+ {
+ Fail(label, "identity selection")
+ .WithExpected($"a deterministic grain identity owned by {owner}")
+ .WithObserved(
+ $"no owned identity in {int.MaxValue.ToString("N0", CultureInfo.InvariantCulture)} "
+ + "deterministic candidates")
+ .Throw();
+ return null!;
+ }
+
+ var grain = _harness.GrainFactory.GetGrain(key.Value);
+ if (!_harness.IsOwner(owner, grain.GetGrainId()))
+ {
+ Fail(label, "identity selection")
+ .WithIdentity(grain.GetGrainId(), null)
+ .WithExpected($"the selected grain remains owned by {owner}")
+ .WithObserved("the owner range changed during identity selection")
+ .Throw();
+ }
+
+ return grain;
+ }
+
+ internal static Guid? FindOwnedGrainKey(
+ int seed,
+ string providerName,
+ string label,
+ int ordinal,
+ Func isOwned,
+ CancellationToken cancellationToken)
+ {
+ for (var candidate = 0; candidate < int.MaxValue; candidate++)
+ {
+ cancellationToken.ThrowIfCancellationRequested();
+ var key = ReminderTestData.CreateGuid(
+ seed,
+ $"{providerName}/{label}/{ordinal}/{candidate.ToString(CultureInfo.InvariantCulture)}");
+ if (isOwned(key))
{
- return grain;
+ return key;
}
}
- Fail(label, "identity selection")
- .WithExpected($"a deterministic grain identity owned by {owner}")
- .WithObserved("no owned identity in 65,535 deterministic candidates")
- .Throw();
- return null!;
+ return null;
}
private async Task ExecuteWithCleanupAsync(
diff --git a/test/Orleans.Reminders.TestKit.Tests/ReminderServiceLifecycleConformanceTests.cs b/test/Orleans.Reminders.TestKit.Tests/ReminderServiceLifecycleConformanceTests.cs
index 682a2ca8fd7..70d73634f13 100644
--- a/test/Orleans.Reminders.TestKit.Tests/ReminderServiceLifecycleConformanceTests.cs
+++ b/test/Orleans.Reminders.TestKit.Tests/ReminderServiceLifecycleConformanceTests.cs
@@ -310,6 +310,32 @@ public Task JoinLeaveAdvancesToTheExactRemainingDueTime()
});
}
+ [Fact]
+ public void OwnedIdentitySelectionSearchesBeyondUShortCandidateSpace()
+ {
+ // Reproduces the narrow joined-silo range from #11325, whose first matching candidate is 85,916.
+ var ownerRange = RangeFactory.CreateRange(0x4341E660, 0x434393A0);
+ var grainType = GrainType.Create("reminderservicetest");
+ var attempts = 0;
+
+ var key = ReminderServiceLifecycleTestRunner.FindOwnedGrainKey(
+ seed: 42,
+ providerName: "IdealizedReminderTable",
+ label: nameof(ReminderServiceLifecycleTestRunner.ReminderService_OneSiloJoinLeaveTransfersOwnership),
+ ordinal: 1,
+ isOwned: candidate =>
+ {
+ attempts++;
+ var grainId = GrainId.Create(grainType, GrainIdKeyExtensions.CreateGuidKey(candidate));
+ return ownerRange.InRange(grainId);
+ },
+ TestContext.Current.CancellationToken);
+
+ Assert.Equal(new Guid("8dd62ce3-228e-67e4-5301-c5f75f92e1a5"), key);
+ Assert.Equal(85_917, attempts);
+ Assert.True(attempts > ushort.MaxValue);
+ }
+
private static async Task RunFaultAsync(
Func createRunner,
Func scenario,
@@ -358,6 +384,7 @@ public virtual Task RegisterOnSiloAsync(SiloAddress siloAddress, GrainId grainId
public virtual Task WaitForOwnerCountAsync(GrainId grainId, string reminderName, int count, CancellationToken cancellationToken) => Inner.WaitForOwnerCountAsync(grainId, reminderName, count, cancellationToken);
public virtual IReadOnlyList GetOwners(GrainId grainId, string reminderName) => Inner.GetOwners(grainId, reminderName);
public bool IsOwner(SiloAddress siloAddress, GrainId grainId) => Inner.IsOwner(siloAddress, grainId);
+ public IRingRange GetOwnedRange(SiloAddress siloAddress) => Inner.GetOwnedRange(siloAddress);
public virtual Task WaitForScheduleAsync(GrainId grainId, string reminderName, CancellationToken cancellationToken) => Inner.WaitForScheduleAsync(grainId, reminderName, cancellationToken);
public virtual int GetLocalStartCount(GrainId grainId, string reminderName) => Inner.GetLocalStartCount(grainId, reminderName);
public int GetLocalStopCount(GrainId grainId, string reminderName) => Inner.GetLocalStopCount(grainId, reminderName);
diff --git a/test/Orleans.Testing.Reminders/ReminderServiceLifecycleHarness.cs b/test/Orleans.Testing.Reminders/ReminderServiceLifecycleHarness.cs
index 7ebf752c7bf..db9461837f2 100644
--- a/test/Orleans.Testing.Reminders/ReminderServiceLifecycleHarness.cs
+++ b/test/Orleans.Testing.Reminders/ReminderServiceLifecycleHarness.cs
@@ -124,13 +124,16 @@ public IReadOnlyList GetOwners(GrainId grainId, string reminderName
///
public bool IsOwner(SiloAddress siloAddress, GrainId grainId)
+ => GetOwnedRange(siloAddress).InRange(grainId);
+
+ ///
+ public IRingRange GetOwnedRange(SiloAddress siloAddress)
{
var silo = _cluster.GetSiloForAddress(siloAddress)
?? throw new InvalidOperationException($"Silo {siloAddress} is not active.");
return silo.ServiceProvider
.GetRequiredService()
- .GetMyRange()
- .InRange(grainId);
+ .GetMyRange();
}
///