Skip to content

Commit

Permalink
Align MayInterleave callback signature with 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
yevhen committed Nov 21, 2019
1 parent ca4b1f9 commit 201102e
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 35 deletions.
3 changes: 2 additions & 1 deletion Source/Example.EventSourcing.FSM/Domain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Orleankka.Meta;
using Orleankka.Behaviors;

using Orleans.CodeGeneration;
using Orleans.Concurrency;

namespace Example
Expand All @@ -14,7 +15,7 @@ public interface IInventoryItem : IActor
[MayInterleave(nameof(IsReentrant))]
public class InventoryItem : EventSourcedFsmActor, IInventoryItem
{
public static bool IsReentrant(object msg) => msg is GetDetails;
public static bool IsReentrant(InvokeMethodRequest req) => req.Message(x => x is GetDetails);

string name;
int total;
Expand Down
3 changes: 2 additions & 1 deletion Source/Example.EventSourcing.Idiomatic/Domain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Orleankka;
using Orleankka.Meta;

using Orleans.CodeGeneration;
using Orleans.Concurrency;

namespace Example
Expand All @@ -14,7 +15,7 @@ public interface IInventoryItem : IActor {}
[MayInterleave(nameof(IsReentrant))]
public class InventoryItem : EventSourcedActor, IInventoryItem
{
public static bool IsReentrant(object msg) => msg is GetDetails;
public static bool IsReentrant(InvokeMethodRequest req) => req.Message(x => x is GetDetails);

int total;
string name;
Expand Down
3 changes: 2 additions & 1 deletion Source/Example.EventSourcing.Persistence.GES/Domain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Orleankka;
using Orleankka.Meta;

using Orleans.CodeGeneration;
using Orleans.Concurrency;

namespace Example
Expand All @@ -15,7 +16,7 @@ public interface IInventoryItem : IActor
[MayInterleave(nameof(IsReentrant))]
public class InventoryItem : EventSourcedActor, IInventoryItem
{
public static bool IsReentrant(object msg) => msg is GetDetails;
public static bool IsReentrant(InvokeMethodRequest req) => req.Message(x => x is GetDetails);

int total;
string name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Orleankka;
using Orleankka.Meta;

using Orleans.CodeGeneration;
using Orleans.Concurrency;

namespace Example
Expand All @@ -14,7 +15,7 @@ public interface IInventoryItem : IActor
[MayInterleave(nameof(IsReentrant))]
public class InventoryItem : EventSourcedActor, IInventoryItem
{
public static bool IsReentrant(object msg) => msg is GetDetails;
public static bool IsReentrant(InvokeMethodRequest req) => req.Message(x => x is GetDetails);

int total;
string name;
Expand Down
3 changes: 2 additions & 1 deletion Source/Example.Reentrant/ReaderWriterLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Orleankka;
using Orleankka.Meta;

using Orleans.CodeGeneration;
using Orleans.Concurrency;

namespace Example
Expand All @@ -26,7 +27,7 @@ public interface IReaderWriterLock : IActor
[MayInterleave(nameof(IsReentrant))]
public class ReaderWriterLock : Actor, IReaderWriterLock
{
public static bool IsReentrant(object msg) => msg is Read;
public static bool IsReentrant(InvokeMethodRequest req) => req.Message(x => x is Read);

int value;
ConsolePosition indicator;
Expand Down
17 changes: 9 additions & 8 deletions Source/Orleankka.Runtime/ActorAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq.Expressions;
using System.Reflection;

using Orleans.CodeGeneration;
using Orleans.Internals;
using Orleans.Concurrency;

Expand All @@ -13,7 +14,7 @@ namespace Orleankka

class Interleaving
{
internal static Func<object, bool> MayInterleavePredicate(Type actor)
internal static Func<InvokeMethodRequest, bool> MayInterleavePredicate(Type actor)
{
bool reentrant;
return MayInterleavePredicate(actor, out reentrant);
Expand All @@ -26,7 +27,7 @@ internal static bool IsReentrant(Type actor)
return reentrant;
}

static Func<object, bool> MayInterleavePredicate(Type actor, out bool reentrant)
static Func<InvokeMethodRequest, bool> MayInterleavePredicate(Type actor, out bool reentrant)
{
reentrant = false;

Expand All @@ -53,7 +54,7 @@ static Func<object, bool> MayInterleavePredicate(Type actor, out bool reentrant)
: null;
}

static Func<object, bool> DeterminedByCallbackMethod(Type actor, string callbackMethod)
static Func<InvokeMethodRequest, bool> DeterminedByCallbackMethod(Type actor, string callbackMethod)
{
var method = actor.GetMethod(callbackMethod, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.FlattenHierarchy);
if (method == null)
Expand All @@ -63,15 +64,15 @@ static Func<object, bool> DeterminedByCallbackMethod(Type actor, string callback

if (method.ReturnType != typeof(bool) ||
method.GetParameters().Length != 1 ||
method.GetParameters()[0].ParameterType != typeof(object))
method.GetParameters()[0].ParameterType != typeof(InvokeMethodRequest))
throw new InvalidOperationException(
$"Wrong signature of callback method {callbackMethod} " +
$"specified in Reentrant[] attribute for actor class {actor.FullName}. \n" +
$"Expected: [public] static bool {callbackMethod}(object msg)");
$"specified in MayInterleave[] attribute for actor class {actor.FullName}. \n" +
$"Expected: [public] static bool {callbackMethod}(InvokeMethodRequest req)");

var parameter = Expression.Parameter(typeof(object));
var parameter = Expression.Parameter(typeof(InvokeMethodRequest));
var call = Expression.Call(null, method, parameter);
var predicate = Expression.Lambda<Func<object, bool>>(call, parameter).Compile();
var predicate = Expression.Lambda<Func<InvokeMethodRequest, bool>>(call, parameter).Compile();

return predicate;
}
Expand Down
22 changes: 2 additions & 20 deletions Source/Orleankka.Runtime/Core/ActorType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
using System.Linq;
using System.Reflection;

using Microsoft.Extensions.DependencyInjection;

using Orleans.CodeGeneration;
using Orleans.Internals;
using Orleans.Concurrency;
using Orleans.Runtime;

namespace Orleankka.Core
{
Expand Down Expand Up @@ -96,7 +92,7 @@ Assembly[] GrainAssemblies(IEnumerable<ActorType> interfaces) =>
public readonly int TypeCode;
internal readonly Type Grain;

readonly Func<object, bool> interleavePredicate;
readonly Func<InvokeMethodRequest, bool> interleavePredicate;
readonly string invoker;
internal readonly Dispatcher dispatcher;

Expand Down Expand Up @@ -132,21 +128,7 @@ internal IActorInvoker Invoker(ActorInvocationPipeline pipeline) =>
/// FOR INTERNAL USE ONLY!
/// </summary>
[UsedImplicitly]
public bool MayInterleave(InvokeMethodRequest request)
{
if (request?.Arguments == null)
return false;

var receiveMessage = request.Arguments.Length == 1;
if (receiveMessage)
return interleavePredicate(UnwrapImmutable(request.Arguments[0]));

var streamMessage = request.Arguments.Length == 5;
return streamMessage && interleavePredicate(UnwrapImmutable(request.Arguments[2]));
}

static object UnwrapImmutable(object item) =>
item is Immutable<object> ? ((Immutable<object>)item).Value : item;
public bool MayInterleave(InvokeMethodRequest request) => interleavePredicate(request);

internal IEnumerable<StreamSubscriptionSpecification> Subscriptions() =>
StreamSubscriptionSpecification.From(Class, dispatcher);
Expand Down
29 changes: 29 additions & 0 deletions Source/Orleankka.Runtime/InvokeMethodRequestExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;

using Orleans.CodeGeneration;
using Orleans.Concurrency;

namespace Orleankka
{
public static class InvokeMethodRequestExtensions
{
public static bool Message(this InvokeMethodRequest request, Func<object, bool> predicate) =>
predicate(request.Message());

public static object Message(this InvokeMethodRequest request)
{
if (request?.Arguments == null)
return null;

var receiveMessage = request.Arguments.Length == 1;
if (receiveMessage)
return UnwrapImmutable(request.Arguments[0]);

var streamMessage = request.Arguments.Length == 5;
return streamMessage ? UnwrapImmutable(request.Arguments[2]) : null;
}

static object UnwrapImmutable(object item) =>
item is Immutable<object> immutable ? immutable.Value : item;
}
}
7 changes: 5 additions & 2 deletions Source/Orleankka.Tests/Features/Reentrant_messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Threading.Tasks;

using NUnit.Framework;

using Orleans.CodeGeneration;
using Orleans.Concurrency;

namespace Orleankka.Features
Expand Down Expand Up @@ -39,7 +41,7 @@ interface ITestActor : IActor
[MayInterleave(nameof(IsReentrant))]
class TestActor : Actor, ITestActor
{
public static bool IsReentrant(object msg) => msg is ReentrantMessage;
public static bool IsReentrant(InvokeMethodRequest req) => req.Message(x => x is ReentrantMessage);

readonly ActorState state = new ActorState();

Expand Down Expand Up @@ -73,7 +75,8 @@ interface ITestReentrantStreamConsumerActor : IActor
[MayInterleave(nameof(IsReentrant))]
class TestReentrantStreamConsumerActor : Actor, ITestReentrantStreamConsumerActor
{
public static bool IsReentrant(object msg) => msg is GetStreamMessagesInProgress || msg is int;
public static bool IsReentrant(InvokeMethodRequest req) =>
req.Message(x => x is GetStreamMessagesInProgress || x is int);

readonly List<object> streamMessagesInProgress = new List<object>();
List<object> On(GetStreamMessagesInProgress x) => streamMessagesInProgress;
Expand Down

0 comments on commit 201102e

Please sign in to comment.