Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid loading all types when collecting grain types #430

Merged
merged 3 commits into from
Jul 24, 2024
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
10 changes: 7 additions & 3 deletions OrleansDashboard/Implementation/Grains/DashboardGrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Orleans;
using Orleans.Concurrency;
using Orleans.Runtime;
using Orleans.Serialization.Configuration;
using OrleansDashboard.Implementation.Helpers;
using OrleansDashboard.Metrics;
using OrleansDashboard.Metrics.Details;
Expand All @@ -27,6 +28,7 @@ public class DashboardGrain : Grain, IDashboardGrain
private readonly ISiloGrainClient siloGrainClient;
private readonly DashboardCounters counters;
private readonly GrainProfilerOptions grainProfilerOptions;
private readonly TypeManifestOptions typeManifestOptions;
private readonly TimeSpan updateInterval;
private bool isUpdating;
private DateTime startTime = DateTime.UtcNow;
Expand All @@ -37,6 +39,7 @@ public class DashboardGrain : Grain, IDashboardGrain
public DashboardGrain(
IOptions<DashboardOptions> options,
IOptions<GrainProfilerOptions> grainProfilerOptions,
IOptions<TypeManifestOptions> typeManifestOptions,
ISiloDetailsProvider siloDetailsProvider,
ISiloGrainClient siloGrainClient)
{
Expand All @@ -45,6 +48,7 @@ public DashboardGrain(

// Store the options to bypass the broadcase of the isEnabled flag.
this.grainProfilerOptions = grainProfilerOptions.Value;
this.typeManifestOptions = typeManifestOptions.Value;

// Do not allow smaller timers than 1000ms = 1sec.
updateInterval = TimeSpan.FromMilliseconds(Math.Max(options.Value.CounterUpdateIntervalMs, 1000));
Expand Down Expand Up @@ -268,7 +272,7 @@ public async Task<Immutable<string>> GetGrainState(string id, string grainType)

try
{
var implementationType = GrainStateHelper.GetGrainType(grainType);
var implementationType = GrainStateHelper.GetGrainType(grainType, typeManifestOptions);

var mappedGrainId = GrainStateHelper.GetGrainId(id, implementationType);
object grainId = mappedGrainId.Item1;
Expand Down Expand Up @@ -343,8 +347,8 @@ public async Task<Immutable<string>> GetGrainState(string id, string grainType)

public Task<Immutable<string[]>> GetGrainTypes()
{
return Task.FromResult(GrainStateHelper.GetGrainTypes()
.Select(s => s.Namespace + "." + s.Name)
return Task.FromResult(typeManifestOptions.InterfaceImplementations
.Select(s => s.FullName)
.ToArray()
.AsImmutable());
}
Expand Down
21 changes: 4 additions & 17 deletions OrleansDashboard/Implementation/Helpers/GrainStateHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,12 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Orleans.Serialization.Configuration;

namespace OrleansDashboard.Implementation.Helpers
{
internal static class GrainStateHelper
{
public static IEnumerable<Type> GetGrainTypes()
{
return AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(w => w.IsAssignableTo(typeof(IGrain))
&& !w.Namespace.StartsWith("Orleans")
&& w.IsClass
&& !w.IsGenericType);
}

public static (object, string) GetGrainId(string id, Type implementationType)
{
object grainId = null;
Expand Down Expand Up @@ -102,14 +93,10 @@ public static MethodInfo GenerateGetGrainMethod(IGrainFactory grainFactory, obje
}
}

public static Type GetGrainType(string grainType)
public static Type GetGrainType(string grainType, TypeManifestOptions typeManifestOptions)
{
var _grainType = grainType.Split(".").Last();

return AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(w => w.Name.Equals(_grainType))
.FirstOrDefault();
return typeManifestOptions.InterfaceImplementations
.FirstOrDefault(w => w.FullName.Equals(grainType));
}
}
}
8 changes: 4 additions & 4 deletions Tests/UnitTests/GrainStateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public void Dispose()
}

[Fact]
public void TestGetGrainsTypes()
public async Task TestGetGrainsTypes()
{
var types = GrainStateHelper.GetGrainTypes()
.Select(s => s.Namespace + "." + s.Name);
var dashboardGrain = _cluster.GrainFactory.GetGrain<IDashboardGrain>(1);
var types = await dashboardGrain.GetGrainTypes();

Assert.Contains("TestGrains.TestStateInMemoryGrain", types);
Assert.Contains("TestGrains.TestStateInMemoryGrain", types.Value);
}

[Fact]
Expand Down
Loading