Upload
bill-tulloch
View
1.733
Download
9
Embed Size (px)
Citation preview
Actor Model in .NET:Akka.NET vs Microsoft Orleans
for the curious
William TullochLead Consultant – Readify@wtulloch
PreambleConcurrency and distributed applications
What is the Actor Model?
The Model of Actor Model
A way of reasoning about concurrent computation
What is the Actor Model
A way of reasoning about concurrent computationIs inherently concurrent
The Model of Actor Model
Is a conceptual model for reasoning about concurrent computationIs inherently concurrentManages concurrency through message passing
The Model of Actor Model
Is a conceptual model for reasoning about concurrent computation
Adopts the philosophy that everything is an actor
Is inherently concurrentManages concurrency through message passing
What is an Actor? Lightweight
Never shares state
Communicates through asynchronous messagesHas a mailbox to buffer messages
Processes one message at a time
Is a single-thread object
An Actor Can not exist on its own
Microsoft Orleans&
Akka.NET
The overview
Akka.NET
• A port of Java/Scala Akka
• Open source
• Task Parallel Library
• Reactive methodology• Can be run within an
application, on-prem or in the cloud(?)
The overview
Orleans
• Started by Microsoft Research
• Open source
• Task Parallel Library
• Reactive methodology
• Cloud-native
Akka.NET
• A port of Java/Scala Akka
• Open source
• Task Parallel Library
• Reactive methodology• Can be run within an
application, on-prem or in the cloud(?)
Orleans versus Akka.NET
Hosting Actors
Image from Halo Orleans at build 2104
Orleans - Silo Akka - ActorSystem
Hosting Actors
Image from Halo Orleans at build 2104
Orleans - Silo Akka - ActorSystem
Actors versus Grains
Orleans: An Actor is Grain
Grain instances always exist virtually
Orleans: An Actor is Grain
Grain instances always exist virtuallyAre created on demand
Orleans: An Actor is Grain
Grain instances always exist virtuallyAre created on demand
Are location transparent
Orleans: An Actor is Grain
Grain instances always exist virtuallyAre created on demand
Are location transparent
Every grain must have an Id
Akka.NET: An Actor is Actor
Must be explicitly created and stopped
Akka.NET: An Actor is Actor
Must be explicitly created and stoppedAre created in the context of their parent
Akka.NET: An Actor is Actor
Must be explicitly created and stoppedAre created in the context of their parentExposes a set of life-cycle hooks
Akka.NET: An Actor is Actor
Must be explicitly created and stoppedAre created in the context of their parentExposes a set of life-cycle hooks Are location transparentakka.tcp://[email protected]:12345/user/HelloWorld/$c
Creating grains and actors
Creating a grain
Orleans – creating a grain
• In Visual Studio create two projects•One for your grain interfaces•One for your grain implementations
• In both projects install the NuGet package Microsoft.Orleans.OrleansCodeGenerator.build
Create a Grain interface
public interface IHelloWorld : IGrainWithIntegerKey{ Task Greeting(string name);
Task<string> ReturnGreeting(string name);}
Must implement one of the following:• IGrainWithIntegerKey• IGrainWithGuidKey• IGrainWithStringKey• IGrainWithIntegerCompou
ndKey• IGrainWithGuidCompound
Key
Create an implementation of the interface
public class HelloWorldGrain : Grain, IHelloWorld{ public Task Greeting(string name) { Console.WriteLine($"Hi {name} from Orleans"); return TaskDone.Done; } public Task<string> ReturnGreeting(string name) { return Task.FromResult($"Hi {name} from Orleans"); }}
Interacting with Grains
static async Task SendMessage(User user ){ var helloWorld = GrainClient.GrainFactory.GetGrain<IHelloWorld>(0); var response = await helloWorld.Greeting(user); WriteLine(response);}
Creating an Actor
Akka.Net – creating an actor
• In Visual Studio:• Create a new class library • Create a console application
• Import the core Akka.NET Nuget package: Akka
Create message
public class HelloUserMessage{ public User User { get; } public HelloUserMessage(User user) { User = user; }}
Create an actorpublic class HelloWorldActor : ReceiveActor{ private TimeSpan _waitPeriod; public HelloWorldActor(TimeSpan waitPeriod) { _waitPeriod = waitPeriod;
Receive<HelloUserMessage>(m => { var user = m.User; Thread.Sleep(_waitPeriod); WriteLine($"Hi {user.Id} {user.FirstName} nice to meet you [on thread "); }); Receive<DataCompleted>(m => WriteLine("Data is completed"));}
Using the actor
var system = ActorSystem.Create("demo")
var helloWorld = system.ActorOf(Props.Create(() => new HelloWorldActor()));
helloWorld.Tell(new HelloUserMessage(new User());
await system.Terminate();
Demo Hello World
Messages and Serialisation
Messages and Serialisation
public class UserMessage { public string FirstName { get;} public string LastName { get;} public UserMessage(string firstName, string lastName) { FirstName = firstName; LastName = lastName; }}
Messages and serialisation
Task<string> Greeting(string firstName, string lastName);
Task<Immutable<byte[]>> ProcessRequest(Immutable<byte[]> request);
Messages and Serialisation
[Immutable] public class User { public string FirstName { get;} public string LastName { get;}}
Bits and Pieces
“let it crash” and exception handlingPersisting stateChanging behaviourGrains for everyoneRouting in Akka.NETClustering in Akka.NET
Summing up
Resources
Akka.NETAkka.NET home: http://getakka.net/Bootcamp: https://github.com/petabridge/akka-bootcamp
OrleansOrleans Home: http://dotnet.github.io/orleans/Halo-Orleans: https://channel9.msdn.com/Events/Build/2014/3-
641
Comparing Akka.NET and Orleans: https://github.com/akka/akka-meta/blob/master/ComparisonWithOrleans.md