21

Click here to load reader

Introduction To Distributed Erlang

Embed Size (px)

Citation preview

Page 1: Introduction To Distributed Erlang

Introduction to Distributed Erlang

Basic principles and a little more

Page 2: Introduction To Distributed Erlang

About us

Developing a new breed of platform for social networking gamesScalability is a must Back-end developed with Erlang/OTP

And lots of other good stuffFront-end developed with Flex 4

Page 3: Introduction To Distributed Erlang

About me

More stickers than a 40 years old RV!

Page 4: Introduction To Distributed Erlang

Agenda and non-agenda

Quick recap of message passingCore principles of Erlang remotingGlobal registry and process groupsNothing on Erlang syntax {<<"sorry">>}Nothing on custom networking nor OTP

OTP is what you'll use in reality

Page 5: Introduction To Distributed Erlang

Why Erlang for concurrency?

Immutability painted all over itDesigned to handle thousands of processes

Spawned (not started nor forked)Processes communicate asynchronouslyPassing messages by value

Page 6: Introduction To Distributed Erlang

Message sending

As easy as:

Pid ! MessageSends a message to a process inboxLike with mail delivery:

Not sure if the letter reached its destinationNeeds a letter back if a response is needed

Page 7: Introduction To Distributed Erlang

RSVP

Add address on the envelope for a response

Page 8: Introduction To Distributed Erlang

RSVP (client)

send(Sid, Message) -> Sid!{self(), Message}, receive {From, Response} -> io:format("Client ~p from ~p~n", [Response, From]) after 1000 -> bail end.

Client is also Server's public

API

Page 9: Introduction To Distributed Erlang

RSVP (server)

start() -> spawn(fun()-> serve() end).serve() -> receive {From, Message} -> io:format("Server ~p from ~p~n", [Message, From]), From!{self(), {ack, Message}}, serve() end.

Page 10: Introduction To Distributed Erlang

Process registration

Allows process naming:

register(my_pid, Pid).Frees application from passing Pids around:

my_pid ! Message.

Page 11: Introduction To Distributed Erlang

RPC strikes back

Call MFA on single or multiple nodesDeclined in zillions of variations:

Blocking or notParallelized, including pmap

Makes code location awareHeterogeneous styles:

Pid ! Message rpc:call(N,M,F,A)

Page 12: Introduction To Distributed Erlang

Node connectivity

Triggered by:

rpc:call ...net_adm:ping(node@host)Nodes shake hands and share information

Processes, registrations...Transitive mechanism

Node 1 ➟ Node 2 and Node 2 ➟ Node3then: Node 1 ➟ Node 3

Page 13: Introduction To Distributed Erlang

Erlang Port Mapper Daemon

Page 14: Introduction To Distributed Erlang

Erlang's magic cookie

Passed on startup:

erl -sname n1 -setcookie=secretProper node and host naming requiredCoarse grained security

Party time or bust!

Page 15: Introduction To Distributed Erlang

Global process registry

Location transparency:

global:register_name(gbs, Sid).global:whereis_name(gbs) ! Message.Wired-in name conflict resolutionStill need to ping nodes

Page 16: Introduction To Distributed Erlang

Process group (1/3)

Distributed named process group Processes join and leave:

pg2:create(mypg2).

pg2:join(mypg2, Sid).

pg2:leave(mypg2, Sid).

Page 17: Introduction To Distributed Erlang

Process group (2/3)

pg2 can be used to send messages to:all processes:

pg2:get_members(mypg2)local processes:

pg2:get_local_members(mypg2)closest / random process:

pg2:get_closest_pid(mypg2)

Page 18: Introduction To Distributed Erlang

Process group (3/3)

Also: pg (experimental)

Page 19: Introduction To Distributed Erlang

Ping pong pang relief

net_adm:world and net_adm:world_listEase node discovery on hostsRequires hosts list

Node discovery with nodefinderUDP multicast S3 list for AWS

Page 20: Introduction To Distributed Erlang

Probing further

Page 21: Introduction To Distributed Erlang

Thank you!