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

Changes to Orleans runtime to enable building Indexing as a NuGet package #5674

Merged
merged 16 commits into from Jul 19, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/Azure/Shared/Storage/AzureStorageUtils.cs
Expand Up @@ -8,10 +8,11 @@
using Microsoft.WindowsAzure.Storage.Shared.Protocol;
using Microsoft.WindowsAzure.Storage.Table;
using Orleans.Runtime;
using Orleans.Storage;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;

//
// Number of #ifs can be reduced (or removed), once we separate test projects by feature/area, otherwise we are ending up with ambigous types and build errors.
// Number of #ifs can be reduced (or removed), once we separate test projects by feature/area, otherwise we are ending up with ambiguous types and build errors.
//

#if ORLEANS_CLUSTERING
Expand All @@ -37,10 +38,7 @@ namespace Orleans.Transactions.AzureStorage
/// </summary>
internal static class AzureTableConstants
{
/// <summary>
/// ETag of value "*" to match any etag for conditional table operations (update, nerge, delete).
/// </summary>
public const string ANY_ETAG = "*";
public const string ANY_ETAG = StorageProviderUtils.ANY_ETAG;

public const string PKProperty = nameof(TableEntity.PartitionKey);
public const string RKProperty = nameof(TableEntity.RowKey);
Expand Down
Expand Up @@ -14,7 +14,7 @@ namespace Orleans.Concurrency
/// </para>
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
internal sealed class ReadOnlyAttribute : Attribute
public sealed class ReadOnlyAttribute : Attribute
{
}

Expand Down
12 changes: 12 additions & 0 deletions src/Orleans.Core.Abstractions/Core/GrainExtensions.cs
Expand Up @@ -63,6 +63,18 @@ public static TGrainInterface Cast<TGrainInterface>(this IAddressable grain)
return grain.AsReference<TGrainInterface>();
}

/// <summary>
/// Casts the provided <paramref name="grain"/> to the provided <paramref name="interfaceType"/>.
/// </summary>
/// <param name="grain">The grain.</param>
/// <param name="interfaceType">The resulting interface type.</param>
/// <returns>A reference to <paramref name="grain"/> which implements <paramref name="interfaceType"/>.</returns>
public static object Cast(this IAddressable grain, Type interfaceType)
{

return grain.AsWeaklyTypedReference().Runtime.Convert(grain, interfaceType);
}

/// <summary>
/// Binds the grain reference to the provided <see cref="IGrainFactory"/>.
/// </summary>
Expand Down
131 changes: 130 additions & 1 deletion src/Orleans.Core.Abstractions/Core/IGrainFactory.cs
@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading.Tasks;
using Orleans.Runtime;

Expand Down Expand Up @@ -78,5 +78,134 @@ public interface IGrainFactory
/// </summary>
/// <param name="grain">The grain reference.</param>
void BindGrainReference(IAddressable grain);

/// <summary>
/// A GetGrain overload that returns the runtime type of the grain interface and returns the grain cast to
/// TGrainInterface.
///
/// The main use-case is when you want to get a grain whose type is unknown at compile time (e.g. generic type parameters).
/// </summary>
/// <typeparam name="TGrainInterface">The output type of the grain</typeparam>
/// <param name="grainPrimaryKey">the primary key of the grain</param>
/// <param name="grainInterfaceType">the runtime type of the grain interface</param>
/// <returns>the requested grain with the given grainID and grainInterfaceType</returns>
TGrainInterface GetGrain<TGrainInterface>(Type grainInterfaceType, Guid grainPrimaryKey)
where TGrainInterface : IGrain;

/// <summary>
/// A GetGrain overload that returns the runtime type of the grain interface and returns the grain cast to
/// TGrainInterface.
///
/// The main use-case is when you want to get a grain whose type is unknown at compile time (e.g. generic type parameters).
/// </summary>
/// <typeparam name="TGrainInterface">The output type of the grain</typeparam>
/// <param name="grainPrimaryKey">the primary key of the grain</param>
/// <param name="grainInterfaceType">the runtime type of the grain interface</param>
/// <returns>the requested grain with the given grainID and grainInterfaceType</returns>
TGrainInterface GetGrain<TGrainInterface>(Type grainInterfaceType, long grainPrimaryKey)
where TGrainInterface : IGrain;

/// <summary>
/// A GetGrain overload that returns the runtime type of the grain interface and returns the grain cast to
/// TGrainInterface.
///
/// The main use-case is when you want to get a grain whose type is unknown at compile time (e.g. generic type parameters).
/// </summary>
/// <typeparam name="TGrainInterface">The output type of the grain</typeparam>
/// <param name="grainPrimaryKey">the primary key of the grain</param>
/// <param name="grainInterfaceType">the runtime type of the grain interface</param>
/// <returns>the requested grain with the given grainID and grainInterfaceType</returns>
TGrainInterface GetGrain<TGrainInterface>(Type grainInterfaceType, string grainPrimaryKey)
where TGrainInterface : IGrain;

/// <summary>
/// A GetGrain overload that returns the runtime type of the grain interface and returns the grain cast to
/// TGrainInterface.
///
/// The main use-case is when you want to get a grain whose type is unknown at compile time (e.g. generic type parameters).
/// </summary>
/// <typeparam name="TGrainInterface">The output type of the grain</typeparam>
/// <param name="grainPrimaryKey">the primary key of the grain</param>
/// <param name="keyExtension">The key extension of the grain.</param>
/// <param name="grainInterfaceType">the runtime type of the grain interface</param>
/// <returns>the requested grain with the given grainID and grainInterfaceType</returns>
TGrainInterface GetGrain<TGrainInterface>(Type grainInterfaceType, Guid grainPrimaryKey, string keyExtension)
where TGrainInterface : IGrain;

/// <summary>
/// A GetGrain overload that returns the runtime type of the grain interface and returns the grain cast to
/// TGrainInterface.
///
/// The main use-case is when you want to get a grain whose type is unknown at compile time (e.g. generic type parameters).
/// </summary>
/// <typeparam name="TGrainInterface">The output type of the grain</typeparam>
/// <param name="grainPrimaryKey">the primary key of the grain</param>
/// <param name="keyExtension">The key extension of the grain.</param>
/// <param name="grainInterfaceType">the runtime type of the grain interface</param>
/// <returns>the requested grain with the given grainID and grainInterfaceType</returns>
TGrainInterface GetGrain<TGrainInterface>(Type grainInterfaceType, long grainPrimaryKey, string keyExtension)
where TGrainInterface : IGrain;

/// <summary>
/// A GetGrain overload that returns the runtime type of the grain interface and returns the grain cast to
/// <see paramref="TGrainInterface"/>. It is the caller's responsibility to ensure <see paramref="TGrainInterface"/>
/// extends IGrain, as there is no compile-time checking for this overload.
///
/// The main use-case is when you want to get a grain whose type is unknown at compile time.
/// </summary>
/// <param name="grainPrimaryKey">the primary key of the grain</param>
/// <param name="grainInterfaceType">the runtime type of the grain interface</param>
/// <returns></returns>
IGrain GetGrain(Type grainInterfaceType, Guid grainPrimaryKey);

/// <summary>
/// A GetGrain overload that returns the runtime type of the grain interface and returns the grain cast to
/// <see paramref="TGrainInterface"/>. It is the caller's responsibility to ensure <see paramref="TGrainInterface"/>
/// extends IGrain, as there is no compile-time checking for this overload.
///
/// The main use-case is when you want to get a grain whose type is unknown at compile time.
/// </summary>
/// <param name="grainPrimaryKey">the primary key of the grain</param>
/// <param name="grainInterfaceType">the runtime type of the grain interface</param>
/// <returns></returns>
IGrain GetGrain(Type grainInterfaceType, long grainPrimaryKey);

/// <summary>
/// A GetGrain overload that returns the runtime type of the grain interface and returns the grain cast to
/// <see paramref="TGrainInterface"/>. It is the caller's responsibility to ensure <see paramref="TGrainInterface"/>
/// extends IGrain, as there is no compile-time checking for this overload.
///
/// The main use-case is when you want to get a grain whose type is unknown at compile time.
/// </summary>
/// <param name="grainPrimaryKey">the primary key of the grain</param>
/// <param name="grainInterfaceType">the runtime type of the grain interface</param>
/// <returns></returns>
IGrain GetGrain(Type grainInterfaceType, string grainPrimaryKey);

/// <summary>
/// A GetGrain overload that returns the runtime type of the grain interface and returns the grain cast to
/// <see paramref="TGrainInterface"/>. It is the caller's responsibility to ensure <see paramref="TGrainInterface"/>
/// extends IGrain, as there is no compile-time checking for this overload.
///
/// The main use-case is when you want to get a grain whose type is unknown at compile time.
/// </summary>
/// <param name="grainPrimaryKey">the primary key of the grain</param>
/// <param name="keyExtension">The key extension of the grain.</param>
/// <param name="grainInterfaceType">the runtime type of the grain interface</param>
/// <returns></returns>
IGrain GetGrain(Type grainInterfaceType, Guid grainPrimaryKey, string keyExtension);

/// <summary>
/// A GetGrain overload that returns the runtime type of the grain interface and returns the grain cast to
/// <see paramref="TGrainInterface"/>. It is the caller's responsibility to ensure <see paramref="TGrainInterface"/>
/// extends IGrain, as there is no compile-time checking for this overload.
///
/// The main use-case is when you want to get a grain whose type is unknown at compile time.
/// </summary>
/// <param name="grainPrimaryKey">the primary key of the grain</param>
/// <param name="keyExtension">The key extension of the grain.</param>
/// <param name="grainInterfaceType">the runtime type of the grain interface</param>
/// <returns></returns>
IGrain GetGrain(Type grainInterfaceType, long grainPrimaryKey, string keyExtension);
}
}
6 changes: 6 additions & 0 deletions src/Orleans.Core.Abstractions/IDs/GrainId.cs
Expand Up @@ -94,6 +94,11 @@ internal static GrainId GetGrainServiceGrainId(short id, int typeData)
return FindOrCreateGrainId(UniqueKey.NewGrainServiceKey(id, typeData));
}

internal static GrainId GetGrainServiceGrainId(int typeData, string systemGrainId)
{
return FindOrCreateGrainId(UniqueKey.NewGrainServiceKey(systemGrainId, typeData));
}

public Guid PrimaryKey
{
get { return GetPrimaryKey(); }
Expand Down Expand Up @@ -239,6 +244,7 @@ private string ToStringImpl(bool detailed)
fullString = $"*gcl/{Key.KeyExt}/{idString}";
break;
case UniqueKey.Category.SystemTarget:
case UniqueKey.Category.KeyExtSystemTarget:
fullString = $"*stg/{Key.N1}/{idString}";
break;
case UniqueKey.Category.SystemGrain:
Expand Down
33 changes: 19 additions & 14 deletions src/Orleans.Core.Abstractions/IDs/UniqueKey.cs
Expand Up @@ -25,6 +25,7 @@ public enum Category : byte
Client = 4,
KeyExtGrain = 6,
GeoClient = 7,
KeyExtSystemTarget = 8,
}

public UInt64 N0 { get; private set; }
Expand All @@ -51,18 +52,17 @@ public bool IsLongKey
}

public bool IsSystemTargetKey
{
get { return IdCategory == Category.SystemTarget; }
}
=> IsSystemTarget(IdCategory);

public bool HasKeyExt
{
get {
var category = IdCategory;
return category == Category.KeyExtGrain
|| category == Category.GeoClient; // geo clients use the KeyExt string to specify the cluster id
}
}
private static bool IsSystemTarget(Category category)
=> category == Category.SystemTarget || category == Category.KeyExtSystemTarget;

public bool HasKeyExt => IsKeyExt(IdCategory);

private static bool IsKeyExt(Category category)
=> category == Category.KeyExtGrain
|| category == Category.KeyExtSystemTarget
|| category == Category.GeoClient; // geo clients use the KeyExt string to specify the cluster id

internal static readonly UniqueKey Empty =
new UniqueKey
Expand Down Expand Up @@ -108,7 +108,7 @@ internal static UniqueKey Parse(ReadOnlySpan<char> input)

private static UniqueKey NewKey(ulong n0, ulong n1, Category category, long typeData, string keyExt)
{
if (category != Category.KeyExtGrain && category != Category.GeoClient && keyExt != null)
if (!IsKeyExt(category) && keyExt != null)
throw new ArgumentException("Only key extended grains can specify a non-null key extension.");

var typeCodeData = ((ulong)category << 56) + ((ulong)typeData & 0x00FFFFFFFFFFFFFF);
Expand Down Expand Up @@ -159,6 +159,11 @@ public static UniqueKey NewGrainServiceKey(short key, long typeData)
return NewKey(0, n1, Category.SystemTarget, typeData, null);
}

public static UniqueKey NewGrainServiceKey(string key, long typeData)
{
return NewKey(0, 0, Category.KeyExtSystemTarget, typeData, key);
}

internal static UniqueKey NewKey(ulong n0, ulong n1, ulong typeCodeData, string keyExt)
{
ValidateKeyExt(keyExt, typeCodeData);
Expand All @@ -180,7 +185,7 @@ private void ThrowIfIsNotLong()

private static void ThrowIfIsSystemTargetKey(Category category)
{
if (category == Category.SystemTarget)
if (IsSystemTarget(category))
throw new ArgumentException(
"This overload of NewKey cannot be used to construct an instance of UniqueKey containing a SystemTarget id.");
}
Expand Down Expand Up @@ -351,7 +356,7 @@ internal string ToHexString()
private static void ValidateKeyExt(string keyExt, UInt64 typeCodeData)
{
Category category = GetCategory(typeCodeData);
if (category == Category.KeyExtGrain)
if (category == Category.KeyExtGrain || category == Category.KeyExtSystemTarget)
{
if (string.IsNullOrWhiteSpace(keyExt))
{
Expand Down
7 changes: 7 additions & 0 deletions src/Orleans.Core.Abstractions/Runtime/GrainReference.cs
Expand Up @@ -2,6 +2,7 @@
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Orleans.CodeGeneration;
using Orleans.Core;
using Orleans.Serialization;

namespace Orleans.Runtime
Expand Down Expand Up @@ -45,6 +46,8 @@ public class GrainReference : IAddressable, IEquatable<GrainReference>, ISeriali

internal bool IsSystemTarget { get { return GrainId.IsSystemTarget; } }

public bool IsGrainService => this.IsSystemTarget; // TODO make this distinct
ReubenBond marked this conversation as resolved.
Show resolved Hide resolved

internal bool IsObserverReference { get { return GrainId.IsClient; } }

internal GuidId ObserverId { get { return observerId; } }
Expand All @@ -67,11 +70,15 @@ internal IGrainReferenceRuntime Runtime

internal GrainId GrainId { get; private set; }

public IGrainIdentity GrainIdentity => this.GrainId;

/// <summary>
/// Called from generated code.
/// </summary>
protected internal readonly SiloAddress SystemTargetSilo;

public SiloAddress GrainServiceSiloAddress => this.SystemTargetSilo; // TODO make this distinct

[NonSerialized]
private IGrainReferenceRuntime runtime;

Expand Down
11 changes: 10 additions & 1 deletion src/Orleans.Core.Abstractions/Runtime/IGrainReferenceRuntime.cs
@@ -1,4 +1,5 @@
using Orleans.CodeGeneration;
using Orleans.CodeGeneration;
using System;
using System.Threading.Tasks;

namespace Orleans.Runtime
Expand Down Expand Up @@ -32,5 +33,13 @@ public interface IGrainReferenceRuntime
/// <param name="grain">The grain reference being cast.</param>
/// <returns>A reference to <paramref name="grain"/> which implements <typeparamref name="TGrainInterface"/>.</returns>
TGrainInterface Convert<TGrainInterface>(IAddressable grain);

/// <summary>
/// Converts the provided <paramref name="grain"/> to the provided <paramref name="interfaceType"/>.
/// </summary>
/// <param name="grain">The grain.</param>
/// <param name="interfaceType">The resulting interface type.</param>
/// <returns>A reference to <paramref name="grain"/> which implements <paramref name="interfaceType"/>.</returns>
object Convert(IAddressable grain, Type interfaceType);
}
}
13 changes: 3 additions & 10 deletions src/Orleans.Core/CodeGeneration/IGrainState.cs
Expand Up @@ -20,21 +20,14 @@ public interface IGrainState
/// </summary>
/// <typeparam name="T">The type of application level payload.</typeparam>
[Serializable]
internal class GrainState<T> : IGrainState
public class GrainState<T> : IGrainState
{
public T State;

object IGrainState.State
{
get
{
return State;

}
set
{
State = (T)value;
}
get => State;
set => State = (T)value;
}

/// <inheritdoc />
Expand Down