Skip to content
Open
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
45 changes: 16 additions & 29 deletions src/Orleans.Core/Core/GrainFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ namespace Orleans
/// <summary>
/// Factory for accessing grains.
/// </summary>
internal class GrainFactory : IInternalGrainFactory
internal class GrainFactory(
IRuntimeClient runtimeClient,
GrainReferenceActivator referenceActivator,
GrainInterfaceTypeResolver interfaceTypeResolver,
GrainInterfaceTypeToGrainTypeResolver interfaceToTypeResolver) : IInternalGrainFactory
{
private GrainReferenceRuntime grainReferenceRuntime;

Expand All @@ -18,24 +22,7 @@ internal class GrainFactory : IInternalGrainFactory
/// </summary>
private readonly Dictionary<(GrainId, Type), ISystemTarget> typedSystemTargetReferenceCache = new Dictionary<(GrainId, Type), ISystemTarget>();

private readonly GrainReferenceActivator referenceActivator;
private readonly GrainInterfaceTypeResolver interfaceTypeResolver;
private readonly GrainInterfaceTypeToGrainTypeResolver interfaceTypeToGrainTypeResolver;
private readonly IRuntimeClient runtimeClient;

public GrainFactory(
IRuntimeClient runtimeClient,
GrainReferenceActivator referenceActivator,
GrainInterfaceTypeResolver interfaceTypeResolver,
GrainInterfaceTypeToGrainTypeResolver interfaceToTypeResolver)
{
this.runtimeClient = runtimeClient;
this.referenceActivator = referenceActivator;
this.interfaceTypeResolver = interfaceTypeResolver;
this.interfaceTypeToGrainTypeResolver = interfaceToTypeResolver;
}

private GrainReferenceRuntime GrainReferenceRuntime => this.grainReferenceRuntime ??= (GrainReferenceRuntime)this.runtimeClient.GrainReferenceRuntime;
private GrainReferenceRuntime GrainReferenceRuntime => this.grainReferenceRuntime ??= (GrainReferenceRuntime)runtimeClient.GrainReferenceRuntime;

/// <inheritdoc />
public TGrainInterface GetGrain<TGrainInterface>(Guid primaryKey, string grainClassNamePrefix = null) where TGrainInterface : IGrainWithGuidKey
Expand Down Expand Up @@ -92,7 +79,7 @@ public TGrainObserverInterface CreateObjectReference<TGrainObserverInterface>(IG
public void DeleteObjectReference<TGrainObserverInterface>(
IGrainObserver obj) where TGrainObserverInterface : IGrainObserver
{
this.runtimeClient.DeleteObjectReference(obj);
runtimeClient.DeleteObjectReference(obj);
}

/// <inheritdoc />
Expand Down Expand Up @@ -146,7 +133,7 @@ public TGrainInterface GetGrain<TGrainInterface>(GrainId grainId) where TGrainIn
}

/// <inheritdoc />
public IAddressable GetGrain(GrainId grainId) => this.referenceActivator.CreateReference(grainId, default);
public IAddressable GetGrain(GrainId grainId) => referenceActivator.CreateReference(grainId, default);

/// <inheritdoc />
public IGrain GetGrain(Type grainInterfaceType, Guid key)
Expand Down Expand Up @@ -187,7 +174,7 @@ public IGrain GetGrain(Type grainInterfaceType, long key, string keyExtension)
/// <inheritdoc />
public IAddressable GetGrain(GrainId grainId, GrainInterfaceType interfaceType)
{
return this.referenceActivator.CreateReference(grainId, interfaceType);
return referenceActivator.CreateReference(grainId, interfaceType);
}

/// <summary>
Expand All @@ -203,20 +190,20 @@ public IAddressable GetGrain(GrainId grainId, GrainInterfaceType interfaceType)
/// <returns>A grain reference which implements the provided interface.</returns>
private IAddressable GetGrain(Type interfaceType, IdSpan grainKey, string grainClassNamePrefix)
{
var grainInterfaceType = this.interfaceTypeResolver.GetGrainInterfaceType(interfaceType);
var grainInterfaceType = interfaceTypeResolver.GetGrainInterfaceType(interfaceType);

GrainType grainType;
if (!string.IsNullOrWhiteSpace(grainClassNamePrefix))
{
grainType = this.interfaceTypeToGrainTypeResolver.GetGrainType(grainInterfaceType, grainClassNamePrefix);
grainType = interfaceToTypeResolver.GetGrainType(grainInterfaceType, grainClassNamePrefix);
}
else
{
grainType = this.interfaceTypeToGrainTypeResolver.GetGrainType(grainInterfaceType);
grainType = interfaceToTypeResolver.GetGrainType(grainInterfaceType);
}

var grainId = GrainId.Create(grainType, grainKey);
var grain = this.referenceActivator.CreateReference(grainId, grainInterfaceType);
var grain = referenceActivator.CreateReference(grainId, grainInterfaceType);
return grain;
}

Expand All @@ -228,8 +215,8 @@ private IAddressable GetGrain(Type interfaceType, IdSpan grainKey, string grainC
/// <returns>A grain reference.</returns>
private object CreateGrainReference(Type interfaceType, GrainId grainId)
{
var grainInterfaceType = this.interfaceTypeResolver.GetGrainInterfaceType(interfaceType);
return this.referenceActivator.CreateReference(grainId, grainInterfaceType);
var grainInterfaceType = interfaceTypeResolver.GetGrainInterfaceType(interfaceType);
return referenceActivator.CreateReference(grainId, grainInterfaceType);
}

/// <summary>
Expand All @@ -251,7 +238,7 @@ private object CreateObjectReference(Type interfaceType, IAddressable obj)
throw new ArgumentException($"The provided object must implement '{interfaceType.FullName}'.", nameof(obj));
}

return this.Cast(this.runtimeClient.CreateObjectReference(obj), interfaceType);
return this.Cast(runtimeClient.CreateObjectReference(obj), interfaceType);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ public class GrainInterfaceTypeToGrainTypeResolver
/// Creates a new instance of the <see cref="GrainInterfaceTypeToGrainTypeResolver"/> class.
/// </summary>
/// <param name="clusterManifestProvider">The cluster manifest provider.</param>
public GrainInterfaceTypeToGrainTypeResolver(IClusterManifestProvider clusterManifestProvider)
{
_clusterManifestProvider = clusterManifestProvider;
}
public GrainInterfaceTypeToGrainTypeResolver(IClusterManifestProvider clusterManifestProvider) => _clusterManifestProvider = clusterManifestProvider;

/// <summary>
/// Returns the <see cref="GrainType"/> which supports the provided <see cref="GrainInterfaceType"/> and which has an implementing type name beginning with the provided prefix string.
Expand Down
56 changes: 20 additions & 36 deletions src/Orleans.Core/Core/GrainMethodInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,25 @@ namespace Orleans.Runtime
/// <summary>
/// Invokes a request on a grain.
/// </summary>
internal sealed class GrainMethodInvoker : IIncomingGrainCallContext
/// <remarks>
/// Initializes a new instance of the <see cref="GrainMethodInvoker"/> class.
/// </remarks>
/// <param name="message">The message.</param>
/// <param name="grainContext">The grain.</param>
/// <param name="request">The request.</param>
/// <param name="filters">The invocation interceptors.</param>
/// <param name="interfaceToImplementationMapping">The implementation map.</param>
/// <param name="responseCopier">The response copier.</param>
internal sealed class GrainMethodInvoker(
Message message,
IGrainContext grainContext,
IInvokable request,
List<IIncomingGrainCallFilter> filters,
InterfaceToImplementationMappingCache interfaceToImplementationMapping,
DeepCopier<Response> responseCopier) : IIncomingGrainCallContext
{
private readonly Message message;
private readonly IInvokable request;
private readonly List<IIncomingGrainCallFilter> filters;
private readonly InterfaceToImplementationMappingCache interfaceToImplementationMapping;
private readonly DeepCopier<Response> responseCopier;
private readonly IGrainContext grainContext;
private int stage;

/// <summary>
/// Initializes a new instance of the <see cref="GrainMethodInvoker"/> class.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="grainContext">The grain.</param>
/// <param name="request">The request.</param>
/// <param name="filters">The invocation interceptors.</param>
/// <param name="interfaceToImplementationMapping">The implementation map.</param>
/// <param name="responseCopier">The response copier.</param>
public GrainMethodInvoker(
Message message,
IGrainContext grainContext,
IInvokable request,
List<IIncomingGrainCallFilter> filters,
InterfaceToImplementationMappingCache interfaceToImplementationMapping,
DeepCopier<Response> responseCopier)
{
this.message = message;
this.request = request;
this.grainContext = grainContext;
this.filters = filters;
this.interfaceToImplementationMapping = interfaceToImplementationMapping;
this.responseCopier = responseCopier;
}

public IInvokable Request => request;

public object Grain => grainContext.GrainInstance;
Expand Down Expand Up @@ -89,7 +73,7 @@ public async Task Invoke()
if (stage < numFilters)
{
// Call each of the specified interceptors.
var systemWideFilter = this.filters[stage];
var systemWideFilter = filters[stage];
stage++;
await systemWideFilter.Invoke(this);

Expand Down Expand Up @@ -132,7 +116,7 @@ public async Task Invoke()
ExceptionDispatchInfo.Capture(exception).Throw();
}

this.Response = this.responseCopier.Copy(this.Response);
this.Response = responseCopier.Copy(this.Response);

return;
}
Expand Down Expand Up @@ -160,8 +144,8 @@ private static void ThrowBrokenCallFilterChain(string filterName)

private (MethodInfo ImplementationMethod, MethodInfo InterfaceMethod) GetMethodEntry()
{
var interfaceType = this.request.GetInterfaceType();
var implementationType = this.request.GetTarget().GetType();
var interfaceType = request.GetInterfaceType();
var implementationType = request.GetTarget().GetType();

// Get or create the implementation map for this object.
var implementationMap = interfaceToImplementationMapping.GetOrCreate(
Expand Down
5 changes: 1 addition & 4 deletions src/Orleans.Core/Diagnostics/MessagingTrace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ private static readonly Action<ILogger, SiloAddress, SiloAddress, Message, Excep

private readonly ILogger log;

public MessagingTrace(ILoggerFactory loggerFactory) : base(Category)
{
this.log = loggerFactory.CreateLogger(Category);
}
public MessagingTrace(ILoggerFactory loggerFactory) : base(Category) => this.log = loggerFactory.CreateLogger(Category);

public void OnSendMessage(Message message)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@

namespace Orleans.Runtime;

internal readonly struct AggregatorKey : IEquatable<AggregatorKey>
internal readonly struct AggregatorKey(string instrumentName, KeyValuePair<string, object>[] tags) : IEquatable<AggregatorKey>
{
public AggregatorKey(string instrumentName, KeyValuePair<string, object>[] tags)
{
InstrumentName = instrumentName;
Tags = tags;
}
public string InstrumentName { get; }
public KeyValuePair<string, object>[] Tags { get; }
public string InstrumentName { get; } = instrumentName;
public KeyValuePair<string, object>[] Tags { get; } = tags;

public override int GetHashCode() => HashCode.Combine(InstrumentName, Tags);
public bool Equals(AggregatorKey other) => InstrumentName == other.InstrumentName && Tags.SequenceEqual(other.Tags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ internal sealed class CounterAggregator
{
private readonly KeyValuePair<string, object>[] _tags;
private long _value = 0;
public CounterAggregator()
{
_tags = Array.Empty<KeyValuePair<string, object>>();
}
public CounterAggregator() => _tags = Array.Empty<KeyValuePair<string, object>>();

public CounterAggregator(in TagList tagList)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@

namespace Orleans.Runtime;

internal class HistogramBucketAggregator
internal class HistogramBucketAggregator(KeyValuePair<string, object>[] tags, long bound, KeyValuePair<string, object> label)
{
private long _value = 0;
private readonly KeyValuePair<string, object>[] _tags;
public long Bound { get; }

public HistogramBucketAggregator(KeyValuePair<string, object>[] tags, long bound, KeyValuePair<string, object> label)
{
_tags = tags.Concat(new[] { label }).ToArray();
Bound = bound;
}
private readonly KeyValuePair<string, object>[] _tags = tags.Concat(new[] { label }).ToArray();
public long Bound { get; } = bound;

public ReadOnlySpan<KeyValuePair<string, object>> Tags => _tags;

Expand Down
26 changes: 10 additions & 16 deletions src/Orleans.Core/GrainReferences/GrainReferenceActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,20 @@ namespace Orleans.GrainReferences
/// <summary>
/// The central point for creating <see cref="GrainReference"/> instances.
/// </summary>
public sealed class GrainReferenceActivator
/// <remarks>
/// Initializes a new instance of the <see cref="GrainReferenceActivator"/> class.
/// </remarks>
/// <param name="serviceProvider">The service provider.</param>
/// <param name="providers">The collection of grain reference activator providers.</param>
public sealed class GrainReferenceActivator(
IServiceProvider serviceProvider,
IEnumerable<IGrainReferenceActivatorProvider> providers)
{
private readonly object _lockObj = new object();
private readonly IServiceProvider _serviceProvider;
private readonly IGrainReferenceActivatorProvider[] _providers;
private readonly IServiceProvider _serviceProvider = serviceProvider;
private readonly IGrainReferenceActivatorProvider[] _providers = providers.ToArray();
private Dictionary<(GrainType, GrainInterfaceType), IGrainReferenceActivator> _activators = new();

/// <summary>
/// Initializes a new instance of the <see cref="GrainReferenceActivator"/> class.
/// </summary>
/// <param name="serviceProvider">The service provider.</param>
/// <param name="providers">The collection of grain reference activator providers.</param>
public GrainReferenceActivator(
IServiceProvider serviceProvider,
IEnumerable<IGrainReferenceActivatorProvider> providers)
{
_serviceProvider = serviceProvider;
_providers = providers.ToArray();
}

/// <summary>
/// Creates a grain reference pointing to the specified grain id and implementing the specified grain interface type.
/// </summary>
Expand Down
5 changes: 1 addition & 4 deletions src/Orleans.Core/Lifecycle/MigrationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ internal sealed class MigrationContext : IDehydrationContext, IRehydrationContex
internal readonly SerializerSessionPool _sessionPool;

[GeneratedActivatorConstructor]
public MigrationContext(SerializerSessionPool sessionPool)
{
_sessionPool = sessionPool;
}
public MigrationContext(SerializerSessionPool sessionPool) => _sessionPool = sessionPool;

[Id(0), Immutable]
private Dictionary<string, (int Offset, int Length)> _indices = new(StringComparer.Ordinal);
Expand Down
21 changes: 8 additions & 13 deletions src/Orleans.Core/Manifest/GrainBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,23 @@ namespace Orleans.Metadata
/// <remarks>
/// Bindings are a way to declaratively connect grains with other resources.
/// </remarks>
public class GrainBindings
/// <remarks>
/// Initializes a new instance of the <see cref="GrainBindings"/> class.
/// </remarks>
/// <param name="grainType">The grain type.</param>
/// <param name="bindings">The bindings for the specified grain type.</param>
public class GrainBindings(GrainType grainType, ImmutableArray<ImmutableDictionary<string, string>> bindings)
{
/// <summary>
/// Initializes a new instance of the <see cref="GrainBindings"/> class.
/// </summary>
/// <param name="grainType">The grain type.</param>
/// <param name="bindings">The bindings for the specified grain type.</param>
public GrainBindings(GrainType grainType, ImmutableArray<ImmutableDictionary<string, string>> bindings)
{
this.GrainType = grainType;
this.Bindings = bindings;
}

/// <summary>
/// Gets the grain type.
/// </summary>
public GrainType GrainType { get; }
public GrainType GrainType { get; } = grainType;

/// <summary>
/// Gets the bindings for the specified grain type.
/// </summary>
public ImmutableArray<ImmutableDictionary<string, string>> Bindings { get; }
public ImmutableArray<ImmutableDictionary<string, string>> Bindings { get; } = bindings;
}

/// <summary>
Expand Down
Loading