Skip to content

Commit

Permalink
In GrainDirectoryResolver, replace HasNonDefaultDirectory with IsUsin…
Browse files Browse the repository at this point in the history
…gDhtDirectory (#8632) (#8660)
  • Loading branch information
benjaminpetit authored Oct 16, 2023
1 parent e0b65b3 commit bf0112d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Orleans.Runtime/Catalog/Catalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ private void OnSiloStatusChange(SiloAddress updatedSilo, SiloStatus status)
var activationData = activation.Value;
var placementStrategy = activationData.GetComponent<PlacementStrategy>();
var isUsingGrainDirectory = placementStrategy is { IsUsingGrainDirectory: true };
if (!isUsingGrainDirectory || grainDirectoryResolver.HasNonDefaultDirectory(activationData.GrainId.Type)) continue;
if (!isUsingGrainDirectory || !grainDirectoryResolver.IsUsingDhtDirectory(activationData.GrainId.Type)) continue;
if (!updatedSilo.Equals(directory.GetPrimaryForGrain(activationData.GrainId))) continue;

activationsToShutdown.Add(activationData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public GrainDirectoryResolver(

public IGrainDirectory Resolve(GrainType grainType) => this.directoryPerType.GetOrAdd(grainType, this.getGrainDirectoryInternal);

public bool HasNonDefaultDirectory(GrainType grainType) => !ReferenceEquals(Resolve(grainType), this.DefaultGrainDirectory);
public bool IsUsingDhtDirectory(GrainType grainType) => Resolve(grainType) == null;

private IGrainDirectory GetGrainDirectoryPerType(GrainType grainType)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Orleans.Runtime/GrainDirectory/GrainLocatorResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public IGrainLocator GetGrainLocatorInternal(GrainType grainType)
{
result = this._clientGrainLocator ??= _servicesProvider.GetRequiredService<ClientGrainLocator>();
}
else if (this.grainDirectoryResolver.HasNonDefaultDirectory(grainType))
else if (this.grainDirectoryResolver.IsUsingDhtDirectory(grainType))
{
result = this.cachedGrainLocator;
result = this.dhtGrainLocator;
}
else
{
result = this.dhtGrainLocator;
result = this.cachedGrainLocator;
}

return result;
Expand Down
68 changes: 68 additions & 0 deletions test/NonSilo.Tests/Directory/GrainLocatorResolverTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using NSubstitute;
using Orleans.GrainDirectory;
using Orleans.Hosting;
using Orleans.Runtime;
using Orleans.Runtime.GrainDirectory;
using TestExtensions;
using UnitTests.Grains.Directories;
using Xunit;
using Xunit.Abstractions;

namespace NonSilo.Tests.Directory
{
[TestCategory("BVT"), TestCategory("Directory")]
public class GrainLocatorResolverTests
{
private readonly IGrainDirectory customDirectory;
private readonly IHost host;
private readonly GrainLocatorResolver target;

public GrainLocatorResolverTests(ITestOutputHelper output)
{
this.customDirectory = Substitute.For<IGrainDirectory>();

var hostBuilder = new HostBuilder();
hostBuilder.UseOrleans((ctx, siloBuilder) =>
{
siloBuilder
.ConfigureServices(svc => svc.AddSingleton(Substitute.For<DhtGrainLocator>(null, null)))
.ConfigureServices(svc => svc.AddSingletonNamedService(CustomDirectoryGrain.DIRECTORY, (sp, nameof) => this.customDirectory))
.ConfigureLogging(builder => builder.AddProvider(new XunitLoggerProvider(output)))
.UseLocalhostClustering();
});
this.host = hostBuilder.Build();

this.target = this.host.Services.GetRequiredService<GrainLocatorResolver>();
}

[Fact]
public void ReturnsDhtGrainLocatorWhenUsingDhtDirectory()
{
var grainLocator = this.host.Services.GetRequiredService<DhtGrainLocator>();
Assert.Same(grainLocator, target.GetGrainLocator(GrainType.Create(DefaultDirectoryGrain.DIRECTORY)));
}

[Fact]
public void ReturnsCachedGrainLocatorWhenUsingCustomDirectory()
{
var grainLocator = this.host.Services.GetRequiredService<CachedGrainLocator>();
Assert.Same(grainLocator, target.GetGrainLocator(GrainType.Create(CustomDirectoryGrain.DIRECTORY)));
}

[Fact]
public void ReturnsClientGrainLocatorWhenUsingClient()
{
var grainLocator = this.host.Services.GetRequiredService<ClientGrainLocator>();
Assert.Same(grainLocator, target.GetGrainLocator(ClientGrainId.Create("client").GrainId.Type));
}
}
}

0 comments on commit bf0112d

Please sign in to comment.