500 likes | 570 Views
Virtual Actors with Microsoft Orleans. By Daniel D’Agostino 1st August 2017. Introducing: Actors. Actor models. Multithreading. Shared Resource. Objects. Objects. Thread 1. Thread 2. Actor Model. Actor. Single Thread. Internal State. Actor Model. Message Passing.
E N D
Virtual Actors with Microsoft Orleans By Daniel D’Agostino 1st August 2017
Introducing: Actors Actor models
Multithreading Shared Resource Objects Objects Thread 1 Thread 2
Actor Model Actor Single Thread Internal State
Actor Model Message Passing
Actor Properties • Single threaded • Not per actor, but thread pool • One message at a time (internal message queue) • No data sharing • Message passing
Actor Models Examples • Erlang • Akka • Akka .NET • Orleans • Azure ServiceFabric • ProtoActor
Introducing: Concepts Behind Microsoft Orleans Virtual Actors
Terminology • Grain: a virtual actor • Silo: a server instance, holding many grains • Turn: processing a message • Activation: single living instance of a grain
Activation and Life Cycle • Grains always “exist” • The runtime activates them as needed • They are deactivated automatically after an idle period or when requested • Can control what happens on activation/deactivation • Comparison with normal actors: • Control creation, full life cycle, hierarchy
Handling Failure • No supervision hierarchies • Exceptions are thrown back to the caller • Grains on dead nodes are automatically reactivated elsewhere by the runtime
Location Transparency • No actor path • Just a grain ID • Actors are allocated automatically to different nodes • Typically you would not know where an actor lives • Forces you to think distributed first
Asynchrous Messaging • No need to declare a message class • Methods provide an abstraction • Grain methods must be asynchronous • Must return Task or Task<T> • Enforced by the runtime • Invocation patterns • Await: preserves order, affects throughput • Fire and forget: parallel (faster, no ordering)
Finite State Machines • Not provided out of the box • Trivial to implement
Introducing: Orleans itself Microsoft Orleans
What is Orleans? • Created by Microsoft Research • Open sourced in January 2015 • Virtual Actors implementation • Abstraction of actor model • Less control but much easier to work with • Used in Halo 4 and 5 among others
Community and Support • Under active development by Microsoft • No commercial support • Active support on Gitter chat • Very good documentation • Various community-contributed plugins • OrleansContrib
Roadmap • Mainly working towards Orleans 2.0 with .NET Core / Standard support • Technical Preview already available
Deployment • Built with Azure in mind • Supports on-premises deployment • Pluggable components and third party integrations • Cluster membership • Storage • etc.
How To: Create Simple Orleans projects Getting Started
Simple Orleans Projects • Get the Orleans VS Extension • Dev/Test Host
How To: Do Various Cool Stuff Intermediate concepts
Stateless Workers • By default, grains are singletons • If marked with [StatelessWorker] attribute, multiple activations of a grain are allowed • Always local, never remote • Automatic scaling • Similar in concept to Akka routers • Activations are not individually addressable
Messaging Optimisations • Serialization • Default: deep copy • Immutable: pass by reference • Stateless Worker: saves remote call • Re-entrant grains • Possibility of interleaving • Not recommended • Code generation (runtime vs build-time)
Dependency Injection • Simple built-in dependency injection • Based on ASP .NET Core model • How to: • Add startup class • Configure dependencies • Call UseStartupType<T>() • Add constructor dependencies
How To: Save Grain State Grain Persistence
Grain Persistence Overview • Saves current state only • Event sourcing introduced very recently • State is automatically loaded on grain activation • Storage provider name specified in an attribute • Or defaults to “Default” • Configure using code or XML config
Grain Persistence How-To • Create class holding grain’s state • Inherit from Grain<T> • T is the grain state class • API • State property • WriteStateAsync() • ReadStateAsync() • ClearStateAsync() • StorageProvider attribute • Configure storage provider
Official Storage Providers • AzureTableStorage • AzureBlobStorage • DynamoDBStorageProvider • ADO .NET Storage Provider • SQL Server • MySQL / MariaDB • MemoryStorage • Volatile, for testing only • ShardedStorageProvider
Contributed Storage Providers • MongoDB • DocumentDB • Redis • Simple SQL Server • RavenDB • CouchBase • Firebase • Arango
Custom Storage Providers • Simple API • Optimistic Concurrency Control / Etag Taken from: https://dotnet.github.io/orleans/Documentation/Getting-Started-With-Orleans/Grain-Persistence.html
How To: Run Tasks Periodically Timers and reminders
Timers • Bound to grain activation • Will not run if grain not active • Does not count towards keeping grain active • Does not span across multiple activations • Same as System.Threading.Timer • Behaves like reentrant grain • Good for low-res (e.g. seconds/minutes)
Reminders • Persistent • Stored in database • Runs even if grain not active • Must be explicitly cancelled • Good for higher resolution (minutes, hours, or days)
How To: Run Unit and Integration Tests Against Grains Testing grains
Testing Approaches • Unit Testing • TestCluster
Unit Testing Grains • Grains are simple classes and can be unit tested as is • No need for TestProbes or AutoPilots! • Exceptions: • GrainFactory: use DI with IGrainFactory • Persistence: use DI with IStorage • GetPrimaryKey*(): use DI with workaround • Extension methods, cannot be mocked
TestCluster • In-Memory Cluster • Package: Microsoft.Orleans.TestingHost • Can test all grain interactions
On: PubSub, Distributed Systems, and More Advanced concepts
Publish/Subscribe • Virtual Streams Abstraction • Observers and Observables • Explicit and Implicit Subscriptions • Official • Simple Message Stream Provider • Azure Queue Stream Provider • OrleansContrib • RabbitMQ Stream Provider
Cluster Membership • MembershipTableGrain (testing only) • Databases via ADO .NET • Azure Table Storage • Apache ZooKeeper • Consul • DynamoDB
Design Patterns • From OrleansContrib: • Observer • Cadence • Reduce • Smart Cache • Dispatcher • Hub
Delivery and Ordering • Tradeoff • Message Loss • Duplicates • Ordering (Orleans: await) • Delivery Guarantees • At Least Once (Orleans: by configuration) • At Most Once (Orleans: by default) • Exactly Once
How to: find more information Wrapping up
Resources • Orleans Homepage • Documentation & Tutorials • Halo Cloud Services presentation • Technical Report 2014 • Gitter chat • Source code on GitHub • OrleansContrib
Question Time • Q&A