azure service fabric and the actor model: when did we forget object orientation?

41
Azure Service Fabric and the Actor Model: when did we forget Object Orientation? João Pedro “jota” Martins 03.September.2016

Upload: joao-pedro-martins

Post on 08-Jan-2017

366 views

Category:

Technology


7 download

TRANSCRIPT

Page 1: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Azure Service Fabric and the Actor Model: when did we forget Object Orientation?João Pedro “jota” Martins03.September.2016

Page 2: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

João Pedro “jota” MartinsCloud Solution Architect @ Microsoft UKCo-founder - GASP (architecture UG) + APPU (usability prof. assoc.)TechEd 2006 – “Iron Architect” competition winnerBizTalk Server MVP 2006-2011Former CTO @ |create|it| - Premium S.I./MSFT Partner @ Lisbon, Portugal

Page 3: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Ground Rules and ExpectationsMurphy’s law:there will [probably] be problems in the

demos

This is a different way of architecting software: there will be disagreements

There is a lot to say: there won’t be enough time

Page 4: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

What’s this session about?Microsoft Azure Service Fabric= A platform for reliable, hyperscale, microservice-based applications= “PaaS v2” + Stateless+Stateful Services/Actors= Platform for applications BORN FOR THE CLOUD, not Lift&Shift’ed

Actor Model @ Service Fabric= model of concurrent computation that treats "actors" as the primitives of concurrent computation. Actors can implement logic, create more actors, send messages, and determine how to respond to a message received. Actors only modify private state.= use Service Fabric for hosting~= Object Orientation with state persistence + communication through messaging passing

Page 5: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

the platform

Page 6: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Public Cloud Other CloudsOn PremisesPrivate cloud

LifecycleMgmt

Independent Scaling

Independent Updates

Always On

Availability

ResourceEfficient

Stateless/Stateful

Your laptop

Microsoft Azure Service FabricA platform for reliable, hyperscale, microservice-based applications

Microservices

Service Fabric

Page 7: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Battle-hardened for over 5 yearsAzure Core

Infrastructure

thousands of machines

Power BI

Intune

800k devices

Azure SQL Database

1.4 million databases

Bing Cortana

500m evals/sec

Azure Document

DB

billions transactions/wee

k

Skype for Business

Hybrid Ops

Event Hubs

20bn events/day

IoT Suit

e

Halo 4 MP

as “Project Orleans”

Page 8: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Windows OS

Windows OS Windows OS

Windows OS

Windows OS

Windows OS

FabricNode

FabricNode

FabricNode

FabricNode

FabricNode

FabricNode

Set of OS instances grouped to form a pool of resourcesCluster can scale to 1000s of machines, is self repairing, and scales-up or downActs as environment-independent abstraction layer [note: can spread geographic regions]

Service Fabric Cluster

= VM scale set running special services in it

Page 9: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Azure Service Fabric CapabilitiesHigh density service hostingPartitioning supportLoad balancing Placement constraintsConsistent state replication frameworkReliable distributed key/value store, collections and queuesActor Model

Application deployment services: Rolling update with rollback Strong versioning Side-by-side support

Leadership electionName service for discovery

= enterprise-ready computational environment to run things at scale

Page 10: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Service Fabric MicroservicesA microservice is whatever you want it to be:

– ASP.NET, node.js, Java VMs, an arbitrary .exe (* Linux version coming soon)– What we deploy to Service Fabric

Stateless microservice– Has either no state or it can be retrieved from an external store – There can be N instances in multiple nodes– e.g. web frontends, protocol gateways, load balancers, etc.– Most of what we’ve been doing in the last 10 years

Stateful microservice– Maintain hard, authoritative state– N consistent /reliablecopies achieved through replication and local persistence– Reduces the complexity and number of components in traditional three-tier architecture – e.g. web user session , documents, workflow, user profile, shopping cart, IoT devices,

multiplayer games, etc.

Page 11: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

App1 App2

Handling Machine Failures

App Type Packages Service Fabric Cluster VMs

#FAIL

Page 12: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Upgrading Services with zero downtime

Application Package FD0/UD0

FD0/UD1

FD1/UD6

FD1/UD5

FD2/UD4

FD2/UD3

FD – Failure Domain; UD – Upgrade Domain

Page 13: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Node 5Node 4Node 3 Node 6Node 2Node 1

Service partitioning

P2

S

SS

P4SP1

SP3SS

S

• Services can be partitioned for scale-out.• You can choose your own partitioning scheme.• Service partitions are striped across machines in the

cluster.• Replicas automatically scale out & in on cluster changes

Page 14: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Basics for devsDownload from WebPI

Sets up local dev cluster with 5 nodes by default

Includes service fabric explorer + powershell cmdlets

Same code that runs on Azure

Page 15: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

the programming model

Page 16: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

What is a microservice in Service Fabric?Is (logic + state) that is independently versioned, deployed, and scaledHas a unique name that can be resolved (e.g. fabric:/myapplication/myservice)Interacts with other microservices over well defined interfaces and protocols like RESTRemains always logically consistent in the presence of failuresHosted inside a “container” nodeCan be written in any language/frameworkDeveloped by a small engineering team

Page 17: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Reliable Services APIBuild stateless services using existing technologies such as ASP.NET WebAPIBuild stateful services using reliable collections• ReliableDictionary<T>, ReliableQueue<T>• Data Contract SerializerManage the concurrency and granularity of state changes using transactionsCommunicate with services using the technology of your choice • e.g. WebAPI , WCF

Collections• Single machine• Single threaded

Concurrent Collections• Single machine• Multi threaded

Reliable Collections• Multi machine• Multi threaded• Replicated (HA)• Persistence• Asynchronous• Transactional

Page 18: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Queues Storage

“Classical” 3-Tier service patternFront End(StatelessWeb)

StatelessMiddle-tierCompute

Cache

Scale with partitioned storage

Increase reliability with queues

Reduce read latency with caches

Manage your own transactions for state consistency

… Many moving parts each managed differently

Load Balancer

Page 19: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

StatefulMiddle-tierCompute

Stateful services: Simplify design, reduce latency

Front End(StatelessWeb)

data stores used for analytics and disaster recovery

Application state lives in the compute tier

Low Latency reads and writes

Partitions are first class for scale-out

Built in transactions

Fewer moving parts

Load Balancer

Page 20: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Stateful service + Automatic Failoverdemo

Page 21: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Reliable Actors

Page 22: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Public Cloud Other CloudsOn PremisesPrivate cloud

LifecycleMgmt

Independent Scaling

Independent Updates

Always On

Availability

ResourceEfficient

Stateless/Stateful

Guest Executables Reliable Services API

Reliable Actors API

Service Fabric Programming Models

Web Apps - ASP.NET Core

- OWIN

Your laptop

Page 23: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

What is the actor model?Independent entities… holding their own attributes… with their own behaviour… making decisions based on their knowledge… sending messages… and responding to messages. can be modelled with actor pattern/model.

Page 24: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Service Fabric Reliable Actors• Asynchronous and single-threaded programming

model• API to build stateless and stateful objects through the

virtual actor programming model• Support polymorphism/inheritance• Proxy for actor-to-actor and client-to-actor

communication• Runtime automatically maintains lifecycle of actors, and

handles failovers, scales, upgrades, partitioning, etc.

Page 25: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Stateful Actors: creating an actor servicedemo

Page 26: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Stateful Actors: managing a conferencedemo

Page 27: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Demo: “class diagram”

Page 28: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Defining an actor class

Page 29: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Creating an actor instance / Sending messages

ActorProxy.Create – creates an Actor with a given Id and returns a proxy to it, or returns a proxy to existing actorAs there are no constructors, initializing or sending messages is the same

Page 30: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Actor initializationJust create a method and call it via a message (there are no constructors), for example:

Page 31: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Actor/Client-to-Actor messagingRemember this requires network communication, including serialization and deserialization of payloads.

ActorProxy retries in case of communication failure or actor failover…Þ At-least-once message delivery

Þ … use idempotenceÞ Order is not guaranteed

Only one method of an actor instance is executed at any given time.

Page 32: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Turn-based concurrency

Only applies to each specific actor instance . Multiple actor instances run in parallel in the same host .

Page 33: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

ReentrancyBy default actors allow re-entrancein the same logical call-chain context.

This can be disabled for specific actor types with:ReentrancyMode = ActorReentrancyMode.Disallowedin ActorConcurrencySettings when the actor type is registered with Service Fabric

Page 34: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Reading and updating state inside na actorStateManager instance, inherited from Actor superclassReads and writes that actor instance’s private data:- SetStateAsync(“name”, object) // name = object;- GetStateAsync<T>(“name”) || TryGetState<T> // return

name;- AddStateAsync(“name”, object) || TryAddStateAsync<T>- AddOrUpdateStateAsync(“name”, object, delegate)- RemoveStateAsync(“name”) || TryRemoveStateAsync

Values are persisted at the end of the actor method (all-or-nothing).

Page 35: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Lifetime and garbage collectionActor activation- When a call comes for an actor and one is not already active, a new actor is created- The actor's state is loaded if it's maintaining state.- The OnActivateAsync

method is called.

Activation = instantiate a CLR object in memory + loading its State if it exits

Actor deactivation- When an actor is not used for some period of time (60min), it is removed from the Active Actors table- The OnDeactivateAsync

method is called

Being used = method call or reminder method executed

Garbage CollectionWhen an actor is deactivated, references to the actor object are released and it can be garbage collected normally by the CLR GC (1x/min)This only cleans up the actor object; it does not remove state stored in the actor's State Manager. The next time the actor is activated, a new actor object is created and its state is restored.

«ACTORS ARE MEANT TO ABSTRACT AWAY THE LIFETIME OF AN OBJECT'S INSTANCE. JUST CALL ONE WHEN YOU NEED IT, AND IT WILL BE THERE.»

Page 36: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

Timers- Schedule regular execution of a callback on an actor.- Time doesn’t count during execution.- Doesn’t prevent GC.- Respects turn-based concurrency.- Use UnregisterTimer to remove.

João Pedro Martins
Page 37: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

RemindersSimilar to Timers, but:- Are persisted with Actor state- Execute even if actor is deactivated/failover- Can prevent GC- Only one callback method- Use UnregisterReminder to remove

João Pedro Martins
Page 38: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

EventsDesigned for Actor to Client communication

Recreated in case of failovers

1 - Define an interface that describes the events published by the actor.

4 - On the client, create a proxy to the actor that publishes the event and subscribe to its events.

2 - Declare the events published by the actor in the actor interface.

3 - On the client side, implement the event handler

5 - On the actor, publish the events

Page 39: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

In conclusion…Service Fabric Actors…• Have behaviour + state• Bring back the Object Orientation + statefulnes concepts we

had lost• Run in a performant enterprise-ready scalable environment• Especially suited for web session state, shopping cart, iot devices,

or any scenarios with independent objects with own lifetime/state/behaviour

But…• Requires re-architecting existing apps• API can still be improved

Page 40: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

ReferencesAzure Service Fabric - http://aka.ms/ServiceFabric Azure Service Fabric Documentation - http://aka.ms/ServiceFabricdocsOrleans: Distributed Virtual Actors for Programmability and Scalability - https://www.microsoft.com/en-us/research/publication/orleans-distributed-virtual-actors-for-programmability-and-scalability/Book: Programming Microsoft Azure Service Fabric by Haishi Bai (Microsoft Press)Azure Service Fabric Samples - http://aka.ms/ServiceFabricSamples Akka.net (alternative .Net implementation of the Actor Model) - http://getakka.net/ Actor Model (1973) - https://en.wikipedia.org/wiki/Actor_modelMartin Fowler on Microservices - http://martinfowler.com/articles/microservices.htmlSignup for Service Fabric on Linux - http://aka.ms/SFlinuxpreview

Try a Party Cluster (it’s free!):

http://aka.ms/TryServiceFabric

Page 41: Azure Service Fabric and the Actor Model: when did we forget Object Orientation?

João Pedro “jota” [email protected]@lokijotapt.linkedin.com/in/joaopedromartins/

thanks! questions?