Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ReubenBond committed Jan 22, 2022
1 parent ff80f31 commit 5485aa0
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 28 deletions.
5 changes: 0 additions & 5 deletions src/Orleans.Core.Abstractions/Activation/IGrainContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@ internal interface ICollectibleGrainContext : IGrainContext
void DelayDeactivation(TimeSpan timeSpan);
}

internal interface IActivationData : ICollectibleGrainContext
{
IGrainRuntime GrainRuntime { get; }
}

internal interface IGrainTimerRegistry
{
void OnTimerCreated(IGrainTimer timer);
Expand Down
22 changes: 10 additions & 12 deletions src/Orleans.Core.Abstractions/Core/Grain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ namespace Orleans
/// </summary>
public abstract class Grain : IGrainBase, IAddressable
{
private readonly IGrainContext _grainContext;

// Do not use this directly because we currently don't provide a way to inject it;
// any interaction with it will result in non unit-testable code. Any behaviour that can be accessed
// from within client code (including subclasses of this class), should be exposed through IGrainRuntime.
// The better solution is to refactor this interface and make it injectable through the constructor.
internal IActivationData Data => _grainContext as IActivationData;
internal IGrainContext GrainContext { get; private set; }

IGrainContext IGrainBase.GrainContext => _grainContext;
IGrainContext IGrainBase.GrainContext => GrainContext;

public GrainReference GrainReference { get { return _grainContext.GrainReference; } }
public GrainReference GrainReference { get { return GrainContext.GrainReference; } }

internal IGrainRuntime Runtime { get; }

Expand All @@ -41,10 +39,10 @@ protected IGrainFactory GrainFactory
/// </summary>
protected internal IServiceProvider ServiceProvider
{
get { return _grainContext?.ActivationServices ?? Runtime?.ServiceProvider; }
get { return GrainContext?.ActivationServices ?? Runtime?.ServiceProvider; }
}

internal GrainId GrainId => _grainContext.GrainId;
internal GrainId GrainId => GrainContext.GrainId;

/// <summary>
/// This constructor should never be invoked. We expose it so that client code (subclasses of Grain) do not have to add a constructor.
Expand All @@ -61,7 +59,7 @@ protected Grain() : this(RuntimeContext.Current, grainRuntime: null)
/// </summary>
protected Grain(IGrainContext grainContext, IGrainRuntime grainRuntime = null)
{
_grainContext = grainContext;
GrainContext = grainContext;
Runtime = grainRuntime ?? grainContext?.ActivationServices.GetRequiredService<IGrainRuntime>();
}

Expand Down Expand Up @@ -113,7 +111,7 @@ protected IDisposable RegisterTimer(Func<object, Task> asyncCallback, object sta
throw new ArgumentNullException(nameof(asyncCallback));

EnsureRuntime();
return Runtime.TimerRegistry.RegisterTimer(_grainContext ?? RuntimeContext.Current, asyncCallback, state, dueTime, period);
return Runtime.TimerRegistry.RegisterTimer(GrainContext ?? RuntimeContext.Current, asyncCallback, state, dueTime, period);
}

/// <summary>
Expand Down Expand Up @@ -184,7 +182,7 @@ protected Task<List<IGrainReminder>> GetReminders()
protected void DeactivateOnIdle()
{
EnsureRuntime();
Runtime.DeactivateOnIdle(_grainContext ?? RuntimeContext.Current);
Runtime.DeactivateOnIdle(GrainContext ?? RuntimeContext.Current);
}

/// <summary>
Expand All @@ -197,7 +195,7 @@ protected void DeactivateOnIdle()
protected void DelayDeactivation(TimeSpan timeSpan)
{
EnsureRuntime();
Runtime.DelayDeactivation(_grainContext ?? RuntimeContext.Current, timeSpan);
Runtime.DelayDeactivation(GrainContext ?? RuntimeContext.Current, timeSpan);
}

/// <summary>
Expand Down Expand Up @@ -287,7 +285,7 @@ private Task OnSetupState(CancellationToken ct)
{
if (ct.IsCancellationRequested)
return Task.CompletedTask;
this.storage = this.Runtime.GetStorage<TGrainState>(Data);
this.storage = this.Runtime.GetStorage<TGrainState>(GrainContext);
return this.ReadStateAsync();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Orleans.Core.Abstractions/Core/GrainExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal static GrainReference AsReference(this IAddressable grain)

var context = grain switch
{
Grain grainBase => grainBase.Data,
Grain grainBase => grainBase.GrainContext,
IGrainBase activation => activation.GrainContext,
ISystemTargetBase systemTarget => systemTarget,
_ => throw new ArgumentException(GetWrongGrainTypeErrorMessage(grain), nameof(grain))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal static void OnDispatcherMessageReceive(Message msg)
{
dispatcherReceivedByContext[0].Increment();
}
else if (context is IActivationData)
else if (context is IGrainContext)
{
dispatcherReceivedByContext[1].Increment();
}
Expand Down Expand Up @@ -101,7 +101,7 @@ internal static void OnImaMessageEnqueued(IGrainContext context)
{
imaEnqueuedByContext[1].Increment();
}
else if (context is IActivationData)
else if (context is IGrainContext)
{
imaEnqueuedByContext[2].Increment();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Orleans.Runtime/Catalog/ActivationData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Orleans.Runtime
/// MUST lock this object for any concurrent access
/// Consider: compartmentalize by usage, e.g., using separate interfaces for data for catalog, etc.
/// </summary>
internal class ActivationData : IActivationData, IGrainExtensionBinder, ICollectibleGrainContext, IAsyncDisposable, IActivationWorkingSetMember, IGrainTimerRegistry, IGrainManagementExtension
internal class ActivationData : IGrainContext, ICollectibleGrainContext, IGrainExtensionBinder, IActivationWorkingSetMember, IGrainTimerRegistry, IGrainManagementExtension, IAsyncDisposable
{
private readonly GrainTypeSharedContext _shared;
private readonly IServiceScope serviceScope;
Expand Down
7 changes: 4 additions & 3 deletions test/Grains/TestInternalGrains/ActivationGCTestGrains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ public Task Nop()
internal class BusyActivationGcTestGrain1: Grain, IBusyActivationGcTestGrain1
{
private readonly string _id = Guid.NewGuid().ToString();

private readonly ActivationCollector activationCollector;
private readonly IGrainContext _grainContext;

private int burstCount = 0;

public BusyActivationGcTestGrain1(ActivationCollector activationCollector)
public BusyActivationGcTestGrain1(ActivationCollector activationCollector, IGrainContext grainContext)
{
this.activationCollector = activationCollector;
_grainContext = grainContext;
}

public Task Nop()
Expand Down Expand Up @@ -66,7 +67,7 @@ public Task EnableBurstOnCollection(int count)
private void OnCollectActivation(GrainId grainId)
{
var other = grainId.Type;
var self = Data.Address.GrainId.Type;
var self = _grainContext.Address.GrainId.Type;
if (other == self)
{
IBusyActivationGcTestGrain1 g = GrainFactory.GetGrain<IBusyActivationGcTestGrain1>(grainId);
Expand Down
10 changes: 6 additions & 4 deletions test/Grains/TestInternalGrains/EchoTaskGrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ public Task<string> EchoError(string data)
internal class EchoTaskGrain : Grain<EchoTaskGrainState>, IEchoTaskGrain, IDebuggerHelperTestGrain
{
private readonly IInternalGrainFactory internalGrainFactory;
private readonly IGrainContext _grainContext;
private ILogger logger;

public EchoTaskGrain(IInternalGrainFactory internalGrainFactory, ILogger<EchoTaskGrain> logger)
public EchoTaskGrain(IInternalGrainFactory internalGrainFactory, ILogger<EchoTaskGrain> logger, IGrainContext grainContext)
{
this.internalGrainFactory = internalGrainFactory;
this.logger = logger;
_grainContext = grainContext;
}

public Task<int> GetMyIdAsync() { return Task.FromResult(State.MyId); }
Expand Down Expand Up @@ -142,7 +144,7 @@ public Task PingAsync()
public Task PingLocalSiloAsync()
{
logger.Info("IEchoGrainAsync.PingLocal");
SiloAddress mySilo = Data.Address.SiloAddress;
SiloAddress mySilo = _grainContext.Address.SiloAddress;
return GetSiloControlReference(mySilo).Ping("PingLocal");
}

Expand All @@ -155,7 +157,7 @@ public Task PingRemoteSiloAsync(SiloAddress siloAddress)
public async Task PingOtherSiloAsync()
{
logger.Info("IEchoGrainAsync.PingOtherSilo");
SiloAddress mySilo = Data.Address.SiloAddress;
SiloAddress mySilo = _grainContext.Address.SiloAddress;

IManagementGrain mgmtGrain = GrainFactory.GetGrain<IManagementGrain>(0);
var silos = await mgmtGrain.GetHosts();
Expand All @@ -170,7 +172,7 @@ public async Task PingOtherSiloAsync()
public async Task PingClusterMemberAsync()
{
logger.Info("IEchoGrainAsync.PingClusterMemberAsync");
SiloAddress mySilo = Data.Address.SiloAddress;
SiloAddress mySilo = _grainContext.Address.SiloAddress;

IManagementGrain mgmtGrain = GrainFactory.GetGrain<IManagementGrain>(0);
var silos = await mgmtGrain.GetHosts();
Expand Down

0 comments on commit 5485aa0

Please sign in to comment.