Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 93 additions & 14 deletions src/Orleans.Reminders.TestKit/ReminderServiceLifecycleTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Task WaitForOwnerCountAsync(
/// <summary>Returns whether a silo's current ring range owns a grain identity.</summary>
bool IsOwner(SiloAddress siloAddress, GrainId grainId);

/// <summary>Gets a snapshot of a silo's current ring range.</summary>
IRingRange GetOwnedRange(SiloAddress siloAddress);

/// <summary>Waits until the current owner has armed its persisted schedule.</summary>
Task WaitForScheduleAsync(GrainId grainId, string reminderName, CancellationToken cancellationToken);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -694,26 +698,101 @@ private IReminderServiceTestGrain CreateGrain(string label)
return _harness.GrainFactory.GetGrain<IReminderServiceTestGrain>(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<IReminderServiceTestGrain>(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<IReminderServiceTestGrain>(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<IReminderServiceTestGrain>(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<Guid, bool> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IReminderServiceLifecycleHarness, LifecycleRunner> createRunner,
Func<LifecycleRunner, CancellationToken, Task> scenario,
Expand Down Expand Up @@ -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<SiloAddress> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,16 @@ public IReadOnlyList<SiloAddress> GetOwners(GrainId grainId, string reminderName

/// <inheritdoc />
public bool IsOwner(SiloAddress siloAddress, GrainId grainId)
=> GetOwnedRange(siloAddress).InRange(grainId);

/// <inheritdoc />
public IRingRange GetOwnedRange(SiloAddress siloAddress)
{
var silo = _cluster.GetSiloForAddress(siloAddress)
?? throw new InvalidOperationException($"Silo {siloAddress} is not active.");
return silo.ServiceProvider
.GetRequiredService<IConsistentRingProvider>()
.GetMyRange()
.InRange(grainId);
.GetMyRange();
}

/// <inheritdoc />
Expand Down
Loading