Skip to content

Commit

Permalink
Reduce use of statics in SiloProviderRuntime
Browse files Browse the repository at this point in the history
  • Loading branch information
ReubenBond committed Dec 7, 2016
1 parent 3867d5a commit afe16a5
Show file tree
Hide file tree
Showing 26 changed files with 354 additions and 301 deletions.
9 changes: 1 addition & 8 deletions src/Orleans/Providers/ClientProviderRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public async Task<Tuple<TExtension, TExtensionInterface>> BindExtension<TExtensi
var obj = ((GrainFactory)this.GrainFactory).CreateObjectReference<TExtensionInterface>(extension);

addressable = obj;

if (null == addressable)
{
throw new NullReferenceException("addressable");
Expand Down Expand Up @@ -174,12 +174,5 @@ public IConsistentRingProviderForGrains GetConsistentRingProvider(int mySubRange
{
throw new NotImplementedException("GetConsistentRingProvider");
}

public bool InSilo { get { return false; } }

public object GetCurrentSchedulingContext()
{
return null;
}
}
}
11 changes: 5 additions & 6 deletions src/Orleans/Streams/Providers/IStreamProviderRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ internal interface IStreamProviderRuntime : IProviderRuntime
{
/// <summary>
/// Retrieves the opaque identity of currently executing grain or client object.
/// Just for logging purposes.
/// </summary>
/// <remarks>Exposed for logging purposes.</remarks>
string ExecutingEntityIdentity();

SiloAddress ExecutingSiloAddress { get; }

/// <summary>
/// Returns the stream directory.
/// </summary>
/// <returns>The stream directory.</returns>
StreamDirectory GetStreamDirectory();

void RegisterSystemTarget(ISystemTarget target);
Expand Down Expand Up @@ -48,11 +52,6 @@ Task<Tuple<TExtension, TExtensionInterface>> BindExtension<TExtension, TExtensio
/// <param name="numSubRanges">Total number of sub ranges within this silo range.</param>
/// <returns></returns>
IConsistentRingProviderForGrains GetConsistentRingProvider(int mySubRangeIndex, int numSubRanges);

/// <summary>Return true if this runtime executes inside silo, false otherwise (on the client).</summary>
bool InSilo { get; }

object GetCurrentSchedulingContext();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class ImplicitStreamSubscriberTable
{
private readonly Dictionary<string, HashSet<int>> table;

internal ImplicitStreamSubscriberTable()
public ImplicitStreamSubscriberTable()
{
table = new Dictionary<string, HashSet<int>>();
}
Expand Down
11 changes: 8 additions & 3 deletions src/OrleansRuntime/Cancellation/CancellationSourcesExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ public Task CancelRemoteToken(GrainCancellationToken token)
/// <param name="target"></param>
/// <param name="request"></param>
/// <param name="logger"></param>
internal static void RegisterCancellationTokens(IAddressable target, InvokeMethodRequest request, Logger logger)
/// <param name="siloRuntimeClient"></param>
internal static void RegisterCancellationTokens(
IAddressable target,
InvokeMethodRequest request,
Logger logger,
ISiloRuntimeClient siloRuntimeClient)
{
for (var i = 0; i < request.Arguments.Length; i++)
{
Expand All @@ -54,10 +59,10 @@ internal static void RegisterCancellationTokens(IAddressable target, InvokeMetho
var grainToken = ((GrainCancellationToken) request.Arguments[i]);

CancellationSourcesExtension cancellationExtension;
if (!SiloProviderRuntime.Instance.TryGetExtensionHandler(out cancellationExtension))
if (!siloRuntimeClient.TryGetExtensionHandler(out cancellationExtension))
{
cancellationExtension = new CancellationSourcesExtension();
if (!SiloProviderRuntime.Instance.TryAddExtension(cancellationExtension))
if (!siloRuntimeClient.TryAddExtension(cancellationExtension))
{
logger.Error(
ErrorCode.CancellationExtensionCreationFailed,
Expand Down
9 changes: 1 addition & 8 deletions src/OrleansRuntime/Catalog/GrainCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ public GrainCreator(IServiceProvider services, Func<IGrainRuntime> getGrainRunti
{
this.services = services;
this.grainRuntime = new Lazy<IGrainRuntime>(getGrainRuntime);
if (services != null)
{
this.createFactory = type => ActivatorUtilities.CreateFactory(type, Type.EmptyTypes);
}
else
{
this.createFactory = type => (sp, args) => Activator.CreateInstance(type);
}
this.createFactory = type => ActivatorUtilities.CreateFactory(type, Type.EmptyTypes);
}

/// <summary>
Expand Down
12 changes: 7 additions & 5 deletions src/OrleansRuntime/Core/BootstrapProviderManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Threading.Tasks;
using Orleans.Providers;
using Orleans.Runtime.Configuration;
using Orleans.Runtime.Providers;

namespace Orleans.Runtime
{
Expand All @@ -29,9 +28,10 @@ public IList<IBootstrapProvider> GetProviders()

// Explicitly typed, for backward compat
public async Task LoadAppBootstrapProviders(
IProviderRuntime providerRuntime,
IDictionary<string, ProviderCategoryConfiguration> configs)
{
await pluginManager.LoadAndInitPluginProviders(configCategoryName, configs);
await pluginManager.LoadAndInitPluginProviders(providerRuntime, configCategoryName, configs);
}


Expand All @@ -57,7 +57,7 @@ internal PluginManager(Logger logger)

public IProvider GetProvider(string name)
{
return providerLoader != null ? providerLoader.GetProvider(name) : null;
return providerLoader?.GetProvider(name);
}

public IList<T> GetProviders()
Expand All @@ -66,7 +66,9 @@ public IList<T> GetProviders()
}

internal async Task LoadAndInitPluginProviders(
string configCategoryName, IDictionary<string, ProviderCategoryConfiguration> configs)
IProviderRuntime providerRuntime,
string configCategoryName,
IDictionary<string, ProviderCategoryConfiguration> configs)
{
ProviderCategoryConfiguration categoryConfig;
if (!configs.TryGetValue(configCategoryName, out categoryConfig)) return;
Expand All @@ -76,7 +78,7 @@ internal async Task LoadAndInitPluginProviders(
logger.Info(ErrorCode.SiloCallingProviderInit, "Calling Init for {0} classes", typeof(T).Name);

// Await here to force any errors to show this method name in stack trace, for better diagnostics
await providerLoader.InitProviders(SiloProviderRuntime.Instance);
await providerLoader.InitProviders(providerRuntime);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/OrleansRuntime/Core/Dispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ private async Task AddressMessage(Message message)
// message.
var strategy = targetAddress.Grain.IsGrain ? catalog.GetGrainPlacementStrategy(targetAddress.Grain) : null;
var placementResult = await this.placementDirectorsManager.SelectOrAddActivation(
message.SendingAddress, message.TargetGrain, InsideRuntimeClient.Current.Catalog, strategy);
message.SendingAddress, message.TargetGrain, this.catalog, strategy);

if (placementResult.IsNewPlacement && targetAddress.Grain.IsClient)
{
Expand Down
57 changes: 57 additions & 0 deletions src/OrleansRuntime/Core/ISiloRuntimeClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Threading.Tasks;
using Orleans.Streams;

namespace Orleans.Runtime
{
/// <summary>
/// Runtime client methods accessible on silos.
/// </summary>
internal interface ISiloRuntimeClient : IRuntimeClient
{
/// <summary>
/// Gets the stream directory.
/// </summary>
/// <returns>The stream directory.</returns>
StreamDirectory GetStreamDirectory();

/// <summary>
/// Retrieves the opaque identity of currently executing grain or client object.
/// </summary>
/// <remarks>Exposed for logging purposes.</remarks>
string ExecutingEntityIdentity();

/// <summary>
/// Attempts to add the provided extension handler to the currently executing grain.
/// </summary>
/// <param name="handler">The extension handler.</param>
/// <returns><see langword="true"/> if the operation succeeded; <see langword="false" /> otherwise.</returns>
bool TryAddExtension(IGrainExtension handler);

/// <summary>
/// Attempts to retrieve the specified extension type from the currently executing grain.
/// </summary>
/// <typeparam name="TExtension">The type of the extension.</typeparam>
/// <param name="result">The extension, or <see langword="null" /> if it was not available.</param>
/// <returns><see langword="true"/> if the operation succeeded; <see langword="false" /> otherwise.</returns>
bool TryGetExtensionHandler<TExtension>(out TExtension result) where TExtension : IGrainExtension;

/// <summary>
/// Removes the provided extension handler from the currently executing grain.
/// </summary>
/// <param name="handler">The extension handler to remove.</param>
void RemoveExtension(IGrainExtension handler);

/// <summary>
/// Binds an extension to the currently executing grain if it does not already have an extension of the specified
/// <typeparamref name="TExtensionInterface"/>.
/// </summary>
/// <typeparam name="TExtension">The type of the extension (e.g. StreamConsumerExtension).</typeparam>
/// <typeparam name="TExtensionInterface">The public interface type of the implementation.</typeparam>
/// <param name="newExtensionFunc">A factory function that constructs a new extension object.</param>
/// <returns>A tuple, containing first the extension and second an addressable reference to the extension's interface.</returns>
Task<Tuple<TExtension, TExtensionInterface>> BindExtension<TExtension, TExtensionInterface>(Func<TExtension> newExtensionFunc)
where TExtension : IGrainExtension
where TExtensionInterface : IGrainExtension;
}
}
Loading

0 comments on commit afe16a5

Please sign in to comment.