1 / 49

Virtual Actors with Microsoft Orleans

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.

jills
Télécharger la présentation

Virtual Actors with Microsoft Orleans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Virtual Actors with Microsoft Orleans By Daniel D’Agostino 1st August 2017

  2. Introducing: Actors Actor models

  3. Multithreading Shared Resource Objects Objects Thread 1 Thread 2

  4. Actor Model Actor Single Thread Internal State

  5. Actor Model Message Passing

  6. Actor Properties • Single threaded • Not per actor, but thread pool • One message at a time (internal message queue) • No data sharing • Message passing

  7. Actor Models Examples • Erlang • Akka • Akka .NET • Orleans • Azure ServiceFabric • ProtoActor

  8. Introducing: Concepts Behind Microsoft Orleans Virtual Actors

  9. Grains and Silos

  10. Terminology • Grain: a virtual actor • Silo: a server instance, holding many grains • Turn: processing a message • Activation: single living instance of a grain

  11. 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

  12. Handling Failure • No supervision hierarchies • Exceptions are thrown back to the caller • Grains on dead nodes are automatically reactivated elsewhere by the runtime

  13. 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

  14. 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)

  15. Finite State Machines • Not provided out of the box • Trivial to implement

  16. Introducing: Orleans itself Microsoft Orleans

  17. 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

  18. Community and Support • Under active development by Microsoft • No commercial support • Active support on Gitter chat • Very good documentation • Various community-contributed plugins • OrleansContrib

  19. Roadmap • Mainly working towards Orleans 2.0 with .NET Core / Standard support • Technical Preview already available

  20. Deployment • Built with Azure in mind • Supports on-premises deployment • Pluggable components and third party integrations • Cluster membership • Storage • etc.

  21. How To: Create Simple Orleans projects Getting Started

  22. Simple Orleans Projects • Get the Orleans VS Extension • Dev/Test Host

  23. Full Project Structure

  24. Dashboard (Peek)

  25. How To: Do Various Cool Stuff Intermediate concepts

  26. 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

  27. 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)

  28. 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

  29. How To: Save Grain State Grain Persistence

  30. 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

  31. 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

  32. Official Storage Providers • AzureTableStorage • AzureBlobStorage • DynamoDBStorageProvider • ADO .NET Storage Provider • SQL Server • MySQL / MariaDB • MemoryStorage • Volatile, for testing only • ShardedStorageProvider

  33. Contributed Storage Providers • MongoDB • DocumentDB • Redis • Simple SQL Server • RavenDB • CouchBase • Firebase • Arango

  34. Custom Storage Providers • Simple API • Optimistic Concurrency Control / Etag Taken from: https://dotnet.github.io/orleans/Documentation/Getting-Started-With-Orleans/Grain-Persistence.html

  35. How To: Run Tasks Periodically Timers and reminders

  36. 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)

  37. Reminders • Persistent • Stored in database • Runs even if grain not active • Must be explicitly cancelled • Good for higher resolution (minutes, hours, or days)

  38. How To: Run Unit and Integration Tests Against Grains Testing grains

  39. Testing Approaches • Unit Testing • TestCluster

  40. 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

  41. TestCluster • In-Memory Cluster • Package: Microsoft.Orleans.TestingHost • Can test all grain interactions

  42. On: PubSub, Distributed Systems, and More Advanced concepts

  43. 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

  44. Cluster Membership • MembershipTableGrain (testing only) • Databases via ADO .NET • Azure Table Storage • Apache ZooKeeper • Consul • DynamoDB

  45. Design Patterns • From OrleansContrib: • Observer • Cadence • Reduce • Smart Cache • Dispatcher • Hub

  46. 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

  47. How to: find more information Wrapping up

  48. Resources • Orleans Homepage • Documentation & Tutorials • Halo Cloud Services presentation • Technical Report 2014 • Gitter chat • Source code on GitHub • OrleansContrib

  49. Question Time • Q&A

More Related