Akka.NET Internals: How Akka.Remote Connections Work

16
@petabridge Petabridge.com Internals: How Akka.Remote Connections Work By Aaron Stannard CTO, Petabridge

Transcript of Akka.NET Internals: How Akka.Remote Connections Work

@petabridge Petabridge.com

Internals: How Akka.Remote Connections

WorkBy Aaron Stannard

CTO, Petabridge

@petabridge Petabridge.com

Sample Configuration

akka {actor.provider = "Akka.Remote.RemoteActorRefProvider, Akka.Remote"

remote {helios.tcp {port = 8080

hostname = localhost } }}

akka {actor.provider = "Akka.Remote.RemoteActorRefProvider, Akka.Remote"

remote {helios.tcp {port = 8081

hostname = localhost } }}

System 1

System 2

@petabridge Petabridge.com

Terminologies

Akka.RemoteTerms

Transport

Typically a TCPconnection (Helios)

A system can havemultiple active

transports

Endpoint

A specific addressbinding for a

transport

i.e.localhost:8080

Need to have a validtransport for anendpoint to be

enabled

Association

A connectionbetween two

endpoints

Depends on havinga valid inboundendpoint and an

outbound endpoint

AssociationHandle

Interface between anetwork associationand the actors whoreceive messages

@petabridge Petabridge.com

Initial State

System 1 System 2

ActorSystem Start ActorSystem Start

Open InboundTCP Endpoint on

Port 8080

Open InboundTCP Endpoint on

Port 8081

Transport listening Transport listening

Wait for incomingassociations

Wait for incomingassociations

@petabridge Petabridge.com

Connecting to Remote Systems

akka.tcp://MySystem@localhost:9001/user/actorName1

Protocol

ActorSystem

Address

Path

All parts form an "ActorPath"

@petabridge Petabridge.com

Inbound vs. Outbound Endpoints

System 1 System 2

"I want toconnect to you"(OUTBOUND)

"I accept yourconnection"(INBOUND)

@petabridge Petabridge.com

Associating

System 1(localhost:8080)

System 2(localhost:8081)

Actor wants to send messageto

akka.tcp://Sys@localhost:8081/user/someActorPath

Open new outbound endpoint

Endpoint on freeport # opened

Send"Handshake"

Message

Receive "Handshake"fromlocalhost:{random}

Accept InboundAssociation

Send "Associate" tolocalhost:{random}

CONNECTIONESTABLISHED

@petabridge Petabridge.com

Akka Handshake Protocol

System 1(outbound)

System 2(inbound)

Closed

WaitHandshake

Open

Closed

WaitHandshake

Open

1. User-defined actorwants to send messageto System 2 over TCP

2. No available endpointsto System 2; need to

create new one

Closed

3. Send "Associate"request to System 2

WaitHandshake

Closed

4. Received "Associate"Have we quarantined this node?

WaitHandshake

5. Not quarantined.Send "Associate"back to System 1

Open

WaitHandshake

6. Accept "Associate"from System 2 if not

quarantined

Open

WaitHandshake

@petabridge Petabridge.com

Why do we bother?

Establish connection-oriented behavior overany network transport, including connectionlessones likeUDP.

@petabridge Petabridge.com

Remoting System Actors

/system

/transports(TransportSupervisor)

/akkaprotocolmanager.{scheme}.{uid}

(AkkaProtocolManager)

/akkaProtocol-{remoteAddress}-{uid}

(ProtocolStateActor)

/endpointManager(EndpointManager)

/reliableEndpointWriter-{url}-{uid}

(ReliableDeliverySupervsior)

/endpointWriter-{url}-{uid}

(EndpointWriter)

/endpointReader-{url}-{uid}

(EndpointReader)

/remote(RemoteDaemon)

/remote-watcher(Remote

DeathWatch)

@petabridge Petabridge.com

EndpointManager

EndpointManager

Responsiblefor

Starting alltransports

Applying policiesto each endpoint

Being firstresponder to

inboundassociations

Creates

EndpointWriter

ReliableDeliverySupervisor

@petabridge Petabridge.com

Endpoint Policies

EndpointPolicies

Pass OK!

Gated

Node disassociated.

Block all connections to ituntil "retry" window

elapses

Quarantined

Node failed to connectduring association. Mustbe restarted with a new

UID.

Won't establish outboundconnections, but will

accept inbound ifUID isdifferent

Quarantine expires afterwindow of time

@petabridge Petabridge.com

Writing to Endpoints

EndpointReader

EndpointWriter

AssociationHandle

MessageDispatcher1. message addressed to remote actor

2. Serialize message & write to handle

3. Writes serialize message to transport

@petabridge Petabridge.com

Reading from Endpoints

EndpointReader

EndpointWriter

AssociationHandle

MessageDispatcher

1. Message arrives overnetwork

2. Notifies reader

3.Deserializes &finds recipient

4. Dispatches to recipient

@petabridge Petabridge.com

Disassociation

System 1(localhost:8080)

System 2(localhost:8081)

Association

CRASH!

TERMINATED

Transport disconnects

Receive"Disassociation"

notification

Propagate toEndpointManager

Kill all remote deployedactors from System 2;

send all outboundmessages to dead letters