Download presentation
Presentation is loading. Please wait.
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 No data sharing Message passing
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
Purpose Type NuGet Package References Client Console Application Microsoft.Orleans.Client Interfaces Silo Microsoft.Orleans.Server Interfaces, Grains Grains Class Library Microsoft.Orleans.Core Microsoft.Orleans.OrleansCodeGenerator.Build
24
Full Project Structure (2)
Configure garbage collection Configure silo (endpoints) Configure client (gateway) Write Silo code Write Client code Optional: add Dashboard
25
Dashboard (Peek)
26
Intermediate concepts
How To: Do Various Cool Stuff Intermediate concepts
27
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
28
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)
29
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
30
How To: Save Grain State
Grain Persistence
31
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
32
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
33
Official Storage Providers
AzureTableStorage AzureBlobStorage DynamoDBStorageProvider ADO .NET Storage Provider SQL Server MySQL / MariaDB MemoryStorage Volatile, for testing only ShardedStorageProvider
34
Contributed Storage Providers
MongoDB DocumentDB Redis Simple SQL Server RavenDB CouchBase Firebase Arango
35
Custom Storage Providers
Taken from: Simple API Optimistic Concurrency Control / Etag
36
How To: Run Tasks Periodically
Timers and reminders
37
Timers Bound to grain activation Same as System.Threading.Timer
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)
38
Reminders Persistent Stored in database Runs even if grain not active Must be explicitly cancelled Good for higher resolution (minutes, hours, or days)
39
How To: Run Unit and Integration Tests Against Grains
Testing grains
40
Testing Approaches Unit Testing TestCluster
41
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
42
TestCluster In-Memory Cluster Can test all grain interactions
Package: Microsoft.Orleans.TestingHost Can test all grain interactions
43
On: PubSub, Distributed Systems, and More
Advanced concepts
44
Publish/Subscribe Virtual Streams Abstraction Official OrleansContrib
Observers and Observables Explicit and Implicit Subscriptions Official Simple Message Stream Provider Azure Queue Stream Provider OrleansContrib RabbitMQ Stream Provider
45
Cluster Membership MembershipTableGrain (testing only)
Databases via ADO .NET Azure Table Storage Apache ZooKeeper Consul DynamoDB
46
Design Patterns From OrleansContrib: Observer Cadence Reduce
Smart Cache Dispatcher Hub
47
Delivery and Ordering Tradeoff Delivery Guarantees Message Loss
Duplicates Ordering (Orleans: await) Delivery Guarantees At Least Once (Orleans: by configuration) At Most Once (Orleans: by default) Exactly Once
48
How to: find more information
Wrapping up
49
Resources Orleans Homepage Halo Cloud Services presentation
Documentation & Tutorials Halo Cloud Services presentation Technical Report 2014 Gitter chat Source code on GitHub OrleansContrib
50
Question Time Q&A
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.