Skip to content

Commit

Permalink
Use GrainDirectoryCacheFactory to construct a IGrainDirectoryCache (#…
Browse files Browse the repository at this point in the history
…8844) (#8898)

---------

Co-authored-by: Mostafa Abdalla <[email protected]>
Co-authored-by: Reuben Bond <[email protected]>
(cherry picked from commit ab644bf)

Co-authored-by: Mostafa Goher <[email protected]>
  • Loading branch information
ReubenBond and Mostafa-Goher authored Mar 8, 2024
1 parent 6981e70 commit 9aab3f7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/Orleans.Runtime/GrainDirectory/CachedGrainLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Orleans.Configuration;
using Orleans.GrainDirectory;
using Orleans.Internal;
Expand Down Expand Up @@ -30,12 +31,14 @@ internal interface ITestAccessor
MembershipVersion ITestAccessor.LastMembershipVersion { get; set; }

public CachedGrainLocator(
IServiceProvider serviceProvider,
GrainDirectoryResolver grainDirectoryResolver,
IClusterMembershipService clusterMembershipService)
IClusterMembershipService clusterMembershipService,
IOptions<GrainDirectoryOptions> grainDirectoryOptions)
{
this.grainDirectoryResolver = grainDirectoryResolver;
this.clusterMembershipService = clusterMembershipService;
this.cache = new LRUBasedGrainDirectoryCache(GrainDirectoryOptions.DEFAULT_CACHE_SIZE, GrainDirectoryOptions.DEFAULT_MAXIMUM_CACHE_TTL);
this.cache = GrainDirectoryCacheFactory.CreateCustomGrainDirectoryCache(serviceProvider, grainDirectoryOptions.Value);
}

public async ValueTask<GrainAddress> Lookup(GrainId grainId)
Expand Down
13 changes: 13 additions & 0 deletions src/Orleans.Runtime/GrainDirectory/GrainDirectoryCacheFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ public static IGrainDirectoryCache CreateGrainDirectoryCache(IServiceProvider se
}
}

internal static IGrainDirectoryCache CreateCustomGrainDirectoryCache(IServiceProvider services, GrainDirectoryOptions options)
{
var grainDirectoryCache = services.GetService<IGrainDirectoryCache>();
if (grainDirectoryCache != null)
{
return grainDirectoryCache;
}
else
{
return new LRUBasedGrainDirectoryCache(options.CacheSize, options.MaximumCacheTTL);
}
}

internal static AdaptiveDirectoryCacheMaintainer CreateGrainDirectoryCacheMaintainer(
LocalGrainDirectory router,
IGrainDirectoryCache cache,
Expand Down
11 changes: 9 additions & 2 deletions test/NonSilo.Tests/Directory/CachedGrainLocatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// Ignore Spelling: Locator

using System.Collections.Immutable;
using System.Net;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NSubstitute;
using NSubstitute.ReceivedExtensions;
using Orleans.Configuration;
using Orleans.GrainDirectory;
using Orleans.Metadata;
using Orleans.Runtime;
Expand All @@ -19,7 +23,7 @@ public class CachedGrainLocatorTests
{
private readonly LoggerFactory loggerFactory;
private readonly SiloLifecycleSubject lifecycle;

private readonly IOptions<GrainDirectoryOptions> grainDirectoryOptions;
private readonly IGrainDirectory grainDirectory;
private readonly GrainDirectoryResolver grainDirectoryResolver;
private readonly MockClusterMembershipService mockMembershipService;
Expand All @@ -42,9 +46,12 @@ public CachedGrainLocatorTests(ITestOutputHelper output)
Array.Empty<IGrainDirectoryResolver>());
this.mockMembershipService = new MockClusterMembershipService();

grainDirectoryOptions = Options.Create(new GrainDirectoryOptions());
this.grainLocator = new CachedGrainLocator(
services,
this.grainDirectoryResolver,
this.mockMembershipService.Target);
this.mockMembershipService.Target,
grainDirectoryOptions);

this.grainLocator.Participate(this.lifecycle);
}
Expand Down

0 comments on commit 9aab3f7

Please sign in to comment.