Virtual Actors with Microsoft Orleans By Daniel DAgostino
Virtual Actors with Microsoft Orleans By Daniel D’Agostino 1 st August 2017
Introducing: Actors ACTOR MODELS
Multithreading Shared Resource Objects Thread 1 Objects 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. NET Orleans Azure Service. Fabric Proto. Actor
Introducing: Concepts Behind Microsoft Orleans VIRTUAL ACTORS
Grains and Silos
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 – Orleans. Contrib
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
Full Project Structure Purpose Type Client Console Application Microsoft. Orleans. Client Interfaces Silo Console Application Microsoft. Orleans. Server Interfaces, Grains Class Library Interfaces Class Library Nu. Get Package References Microsoft. Orleans. Core Microsoft. Orleans Interfaces Code. Generator. Build Microsoft. Orleans. Core Microsoft. Orleans Code. Generator. Build
Dashboard (Peek)
How To: Do Various Cool Stuff INTERMEDIATE CONCEPTS
Stateless Workers • By default, grains are singletons • If marked with [Stateless. Worker] 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 Use. Startup. Type<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 – Write. State. Async() – Read. State. Async() – Clear. State. Async() • Storage. Provider attribute • Configure storage provider
Official Storage Providers • • Azure. Table. Storage Azure. Blob. Storage Dynamo. DBStorage. Provider ADO. NET Storage Provider – SQL Server – My. SQL / Maria. DB • Memory. Storage – Volatile, for testing only • Sharded. Storage. Provider
Contributed Storage Providers • • Mongo. DB Document. DB Redis Simple SQL Server Raven. DB Couch. Base Firebase Arango
Custom Storage Providers Taken from: https: //dotnet. github. io/orleans/Documentation/Getting-Started-With-Orleans/Grain-Persistence. html • Simple API • Optimistic Concurrency Control / Etag
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 • Test. Cluster
Unit Testing Grains • Grains are simple classes and can be unit tested as is – No need for Test. Probes or Auto. Pilots! • Exceptions: – Grain. Factory: use DI with IGrain. Factory – Persistence: use DI with IStorage – Get. Primary. Key*(): use DI with workaround • Extension methods, cannot be mocked
Test. Cluster • In-Memory Cluster – Package: Microsoft. Orleans. Testing. Host • Can test all grain interactions
On: Pub. Sub, 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 • Orleans. Contrib – Rabbit. MQ Stream Provider
Cluster Membership • • • Membership. Table. Grain (testing only) Databases via ADO. NET Azure Table Storage Apache Zoo. Keeper Consul Dynamo. DB
Design Patterns • From Orleans. Contrib: – 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 Git. Hub Orleans. Contrib
Question Time • Q&A
- Slides: 49