Erlang: distributed programming

Similar documents
An Introduction to Erlang

Erlang: An Overview. Part 2 Concurrency and Distribution. Thanks to Richard Carlsson for most of the slides in this part

Erlang 101. Google Doc

Robust Erlang (PFP Lecture 11) John Hughes

Erlang. Functional Concurrent Distributed Soft real-time OTP (fault-tolerance, hot code update ) Open. Check the source code of generic behaviours

Erlang. Functional Concurrent Distributed Soft real-time OTP (fault-tolerance, hot code update ) Open. Check the source code of generic behaviours

An Introduction to Erlang

Introduction to Erlang. Franck Petit / Sebastien Tixeuil

Erlang. Joe Armstrong

FRANCESCO CESARINI. presents ERLANG/OTP. Francesco Cesarini Erlang

Erlang functional programming in a concurrent world

Erlang functional programming in a concurrent world Johan Montelius and Vladimir Vlassov

An Introduction to Erlang

Starting the System & Basic Erlang Exercises

concurrent programming XXL

An Introduction to Erlang

Experiments in OTP-Compliant Dataflow Programming

Message-passing concurrency in Erlang

Lecture 5: Erlang 1/31/12

DISTRIBUTED SYSTEMS [COMP9243] Lecture 1.5: Erlang INTRODUCTION TO ERLANG BASICS: SEQUENTIAL PROGRAMMING 2. Slide 1

Reminder from last time

The Actor Model, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 18 10/23/2014

Erlang. An introduction. Paolo Baldan Linguaggi e Modelli per il Global Computing AA 2016/2017

S AMPLE CHAPTER. Erlang AND OTP IN ACTION. Martin Logan Eric Merritt Richard Carlsson FOREWORD BY ULF WIGER MANNING

Macros, and protocols, and metaprogramming - Oh My!

Maturing, I'll try to provide backwards compatibility for 2 freeswitch releases if I have to make an API change.

Erlang - functional programming in a concurrent world. Johan Montelius KTH

Distributed Erlang and Map-Reduce

Beautiful Concurrency with Erlang

Cliff Moon. Bottleneck Whack-A-Mole. Thursday, March 21, 13

Rudy: a small web server. Johan Montelius. October 2, 2016

Processes. Johan Montelius KTH

A process. the stack

Scenes From the Language Struggle in Toronto, Ontario. Mike Nolta

The Little Elixir & OTP Guidebook by Tan Wei Hao

VALEO: programs robustifier

Erlang. Joe Armstrong.

Kent Academic Repository

The Actor Model applied to the Raspberry Pi and the Embedded Domain. The Erlang Embedded Project. Omer

OTP Design Principles. version 5.6

Distributed Systems Exam 1 Review. Paul Krzyzanowski. Rutgers University. Fall 2016

Principles of Programming Languages (E)

Namy: a distributed name server. Johan Montelius. October 2, 2016

Parallel Programming in Erlang (PFP Lecture 10) John Hughes

The Actor Model applied to the Raspberry Pi and the Embedded Domain. Omer

Producer Consumer in Ada

Erlectricity. Tom Preston-Werner. github.com/mojombo/erlectricity

Programming Paradigms

MODELS OF DISTRIBUTED SYSTEMS

The Actor Model. CSCI 5828: Foundations of Software Engineering Lecture 13 10/04/2016

Introduction to Erlang. Franck Petit / Sebastien Tixeuil

An Erlang primer. Johan Montelius

Chordy: a distributed hash table. Johan Montelius and Vladimir Vlassov. August 29, 2016

Erlang Concepts. Programming for Beginners, Summer 2011

Erlang and Concurrency. André Pang Rising Sun Research

MapReduce in Erlang. Tom Van Cutsem

CS390 Principles of Concurrency and Parallelism. Lecture Notes for Lecture #5 2/2/2012. Author: Jared Hall

Scaling Up from 1000 to 10 Nodes

Typeset in L A TEX from SGML source using the DOCBUILDER 3.0 Document System.

re-exam Concurrent Programming tda383/dit390 Date: Time: 14:00 18:00 Place: Maskinhuset (M)

<Urban.Boquist. com> Rev PA

costransactions Copyright Ericsson AB. All Rights Reserved. costransactions April

An Introduction to Erlang. Richard Carlsson

OSE Copyright Ericsson AB. All Rights Reserved. OSE 1.0 June 23, 2014

Reminder from last ;me

Goldy: a distributed game. Johan Montelius. October 2, 2016

CHAPTER 4: INTERPROCESS COMMUNICATION AND COORDINATION

MODELS OF DISTRIBUTED SYSTEMS

WE ARE ALL BLESSED. Bruce Tate

Lecture 9. Part I. Overview of Message Passing. Communication Coupling. Decoupling Blackboard. Decoupling Broadcast. Linda and Erlang.

Today: Fault Tolerance. Reliable One-One Communication

All you need is fun. Cons T Åhs Keeper of The Code

Concurre Concurr nt n Systems 8L for Part IB Handout 3 Dr. Steven Hand 1

A Small Web Server. Programming II - Elixir Version. Johan Montelius. Spring Term 2018

Lecture 8. Linda and Erlang

DISTRIBUTED COMPUTER SYSTEMS

Test-Driven Development of Concurrent Programs using Concuerror

erlang module Erlang Programming - Lecture 8

EEC-682/782 Computer Networks I

Interprocess Communication

Analysing and visualising callback modules of Erlang generic server behaviours

Erlang: concurrent programming. Johan Montelius. October 2, 2016

Distributed Places. Version 6.3. Kevin Tew. November 20, (require racket/place/distributed)

Actors and Channels in Core λ-calculi. Simon Fowler Joint work with Sam Lindley and Philip Wadler

The do s and don ts of error handling. Joe Armstrong

CS 347: Distributed Databases and Transaction Processing Notes07: Reliable Distributed Database Management

Process Control. Philipp Koehn. 23 April 2018

Two Phase Commit Protocol. Distributed Systems. Remote Procedure Calls (RPC) Network & Distributed Operating Systems. Network OS.

ERLANG BITS AND PIECES

Introduction. version 5.0

Kernel Copyright Ericsson AB. All Rights Reserved. Kernel November

An introduction to Erlang & Elixir. Meetup October 28, Organized by Jean-François Cloutier Held at The Casco Bay Technology Hub, Portland, ME

Verification of Language Based Fault-Tolerance

Last time: introduction. Networks and Operating Systems ( ) Chapter 2: Processes. This time. General OS structure. The kernel is a program!

Today: Fault Tolerance

Concurrency: what, why, how

Author: Paul Zenden. Hot-or-Not Elixir with José Valim

Kernel Copyright Ericsson AB. All Rights Reserved. Kernel 4.0 June 23, 2015

CprE Fault Tolerance. Dr. Yong Guan. Department of Electrical and Computer Engineering & Information Assurance Center Iowa State University

Traditionally, a Web client sends an HTTP

Transcription:

Erlang: distributed May 11, 2012 1 / 21

Fault tolerance in Erlang links, exit signals, system process in Erlang OTP Open Telecom Platform 2 / 21

General idea Links Exit signals System processes Summary of exit signals Idioms for trapping exits Advanced 3 / 21

General idea General idea Links Exit signals System processes Summary of exit signals Idioms for trapping exits Advanced system process handles exit signals from linked processes traps exit messages { EXIT,Pid,Why} default: usual process dies if Why normal link connection between 2 processes symmetric, must be set explicitely exit signal sent to the set of linked processes when process dies { EXIT,B,Why} when process finishes { EXIT,B,normal} A 4 / 21

General idea General idea Links Exit signals System processes Summary of exit signals Idioms for trapping exits Advanced system process handles exit signals from linked processes traps exit messages { EXIT,Pid,Why} default: usual process dies if Why normal link connection between 2 processes symmetric, must be set explicitely exit signal sent to the set of linked processes when process dies { EXIT,B,Why} when process finishes { EXIT,B,normal} A B 4 / 21

General idea General idea Links Exit signals System processes Summary of exit signals Idioms for trapping exits Advanced system process handles exit signals from linked processes traps exit messages { EXIT,Pid,Why} default: usual process dies if Why normal link connection between 2 processes symmetric, must be set explicitely exit signal sent to the set of linked processes when process dies { EXIT,B,Why} when process finishes { EXIT,B,normal} A B 4 / 21

General idea General idea Links Exit signals System processes Summary of exit signals Idioms for trapping exits Advanced system process handles exit signals from linked processes traps exit messages { EXIT,Pid,Why} default: usual process dies if Why normal link connection between 2 processes symmetric, must be set explicitely exit signal sent to the set of linked processes when process dies { EXIT,B,Why} when process finishes { EXIT,B,normal} A { EXIT,B,Why} B 4 / 21

Links General idea Links Exit signals System processes Summary of exit signals Idioms for trapping exits Advanced link defines error propagation path between 2 processes if one dies then another gets exit signal links are established with link(b) or spawn_link(fun) 5 / 21

Exit signals General idea Links Exit signals System processes Summary of exit signals Idioms for trapping exits Advanced exit signal generated when process dies or finishes { EXIT,Pid,Why} Why=normal if a process just finishes (i.e. recursion ends) Why=<exception desc> if there was a problem exit(why) may be called to stop itself sent to all linked processes faking death: exit(pid2, Why) sends { EXIT,Pid,Why} to process Pid2 continues exection 6 / 21

System processes General idea Links Exit signals System processes Summary of exit signals Idioms for trapping exits Advanced usual process dies if receives exit signal from any linked process where Why normal system process set with process_flag(trap_exit,true) traps exit signals from linked processes messages { EXIT,Pid,Why} are added to its mailbox exit signals with Why=kill are not caught at all! process is killed, even system process { EXIT,Pid,killed} broadcasted to all linked processes (notice that kill is propagated as killed) 7 / 21

General idea Links Exit signals System processes Summary of exit signals Idioms for trapping exits Advanced on_exit(pid, Fun) -> spawn(fun() -> process_flag(trap_exit, true), link(pid), receive { EXIT,Pid,Why} -> Fun(Why) end end). 8 / 21

General idea Links Exit signals System processes Summary of exit signals Idioms for trapping exits Advanced F = fun() -> receive X -> list_to_atom(x) end end. Pid = spawn(f). on_exit(pid, fun(why) -> io:format("~p died with ~p~n",[pid,why]) end). create a process that transforms lists to atoms add error handler with on_exit Now sending Pid! hello. results in <0.61.0> died with:{badarg,[{ 9 / 21

Summary of exit signals trap_exit Exit signal Action true kill Die: broadcast the exit signal killed to the link set true X Add { EXIT,Pid,X} to the mailbox false normal Continue: Do nothing, signal vanishes false kill Die: broadcast the exit signal killed to the link set false X Die: broadcast the exit signal X to the link set 10 / 21

Idioms for trapping exits General idea Links Exit signals System processes Summary of exit signals Idioms for trapping exits Advanced 1. Don t care about new process Pid=spawn(fun() ->... end) 2. Want to die if new process dies Pid=spawn_link(fun() ->... end) 3. Want to handle errors if new process dies... process_flag(trap_exit, true), Pid=spawn_link(fun() ->... end), loop(...). loop(state) -> receive { EXIT, SomePid, Reason} -> %% do something with the error loop(state1)... end. 11 / 21

Advanced General idea Links Exit signals System processes Summary of exit signals Idioms for trapping exits Advanced one fails everyone up to system process dies P1 P2 P3 P4 one fails supervisor process restarts one or all Sup P1 P2 P3 12 / 21

Create servers Distribution primitives 13 / 21

Create servers Distribution primitives 1. Erlang spawn process on any Erlang node Erlang VM instance message passing and error handling works without changes run any module function on any node unsafe: should be used in trusted environments, e.g. dedicated LAN 2. Socket based distribution handle and abstract TCP socket communication lib_chan module in examples safe: use in untrusted environments, e.g. internet 14 / 21

Create servers Create servers Distribution primitives $ erl -sname <name[@server]> short name, usable only on the same host $ erl -name <name[@fqdn]> server name, DNS must be accessible, at least /etc/hosts olegus@hebe:~$ erl -name myerlangnode Erlang R15B (erts-5.9) [source] [64-bit] [smp: Eshell V5.9 (abort with ^G) (myerlangnode@hebe.domenis.ut.ee)1> different machines must have the same cookie ~/.erlang.cookie -setcookie parameter 15 / 21

Distribution primitives Create servers Distribution primitives built in rpc module rpc:call(node,mod,function,args) -> Result {badrpc,reason} built in global module extended spawn functions, etc spawn(node,fun) -> Pid spawn(node,mod,func,arglist) -> Pid spawn_link(node,fun) -> Pid spawn_link(node,mod,func,arglist) -> Pid disconnect_node(node) -> bool() ignored node() -> Node node(arg) -> Node nodes() -> [Node] Pid! Msg {RegName,Node}! Msg 16 / 21

Create servers Distribution primitives Run 2 servers with names me and you erl -sname me@localhost erl -sname you@localhost Test with (run from you ) (you@localhost)8> rpc:call(me@localhost, io, format, ["hello~n"]). hello (you@localhost)9> rpc:call(me@localhost, erlang, node, []). me@localhost test remote spawn... 17 / 21

Create servers Distribution primitives Write and compile dist.erl -module(dist). -export([start/0]). loop() -> receive X -> io:format("~p: Im running on ~p~n", [X,node()]) end, loop(). test it 13> RP = spawn(me@localhost, fun dist:loop/0). 14> RP! "Hi". "Hi": Im running on me@localhost 18 / 21

Description 19 / 21

Description provides process restart strategies, event and error logging, etc. declare gen_server module behaviour (checks for undefined functions during compilation) -module(myserver). -behaviour(gen_server). specify 6 callbacks init(startargs) -> {ok, InitState}. handle_call(msg,from,state) -> {reply, ok, State} handle_cast(msg,state) -> {noreply, State}. handle_info(info,state) -> {noreply, State}. terminate(reason, State) -> ok. code_change(oldv, State, Extra) -> {ok, State}. start server with gen_server:start_link(name, CallBackMod, StartArgs, Opts) 20 / 21

Description Description handle_call is a state transform function that is called for each incoming message (synchronous) handle_cast is the same but does not return value (asynchronous) handle_info is for spontaneous messages, e.g. exit signals from linked processes terminate is executed when agent stops code_change is code hot swapping 21 / 21