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

Similar documents
Erlang functional programming in a concurrent world

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

Erlang 101. Google Doc

Time and Space. Indirect communication. Time and space uncoupling. indirect communication

An Erlang primer. Johan Montelius

Erlang: distributed programming

Indirect Communication

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

let s implement a memory Asynchronous vs Synchronous the functional way a first try

Producer Consumer in Ada

Erlang. Types, Abstract Form & Core. Salvador Tamarit Muñoz. Universitat Politècnica de València

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

Programming Paradigms

Distributed Systems. replication Johan Montelius ID2201. Distributed Systems ID2201

Message Passing Part I

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

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

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

concurrent programming XXL

KTH ROYAL INSTITUTE OF TECHNOLOGY. Remote Invocation. Vladimir Vlassov and Johan Montelius

Reminder from last time

LFE - a lisp on the Erlang VM

Parallel Programming in Erlang (PFP Lecture 10) John Hughes

Introduction to Erlang. Franck Petit / Sebastien Tixeuil

Message-passing concurrency in Erlang

Functional Programming In Real Life

Robust Erlang (PFP Lecture 11) John Hughes

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

An Introduction to Erlang

Lecture 8. Linda and Erlang

Advanced Functional Programming, 1DL Lecture 2, Cons T Åhs

Distributed Object-Based Systems The WWW Architecture Web Services Handout 11 Part(a) EECS 591 Farnam Jahanian University of Michigan.

Erlang. Joe Armstrong

3C05 - Advanced Software Engineering Thursday, April 29, 2004

Taking the Derivative

The Go Programming Language. Frank Roberts

Remote Invocation Vladimir Vlassov and Johan Montelius

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

1. Introduction: Key Features of Erlang

Reminder from last ;me

Distributed Erlang and Map-Reduce

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

DISTRIBUTED SYSTEMS Principles and Paradigms Second Edition ANDREW S. TANENBAUM MAARTEN VAN STEEN. Chapter 1. Introduction

Message Passing. Advanced Operating Systems Tutorial 7

Large Scale Computing Infrastructures

Today: Distributed Objects. Distributed Objects

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

An Introduction to Erlang

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

CSCE 314 Programming Languages. Type System

Lecture 5: Erlang 1/31/12

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

FRANCESCO CESARINI. presents ERLANG/OTP. Francesco Cesarini Erlang

Database Management Systems

DISTRIBUTED SYSTEMS [COMP9243] Lecture 8a: Cloud Computing WHAT IS CLOUD COMPUTING? 2. Slide 3. Slide 1. Why is it called Cloud?

Building a video conference (WebRTC) controller with Elixir. Or how Elixir was introduced into VoiSmart

MODELS OF DISTRIBUTED SYSTEMS

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

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

Introduction to Erlang. Franck Petit / Sebastien Tixeuil

An Introduction to Erlang. Richard Carlsson

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

An Introduction to Erlang

MapReduce in Erlang. Tom Van Cutsem

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

Today: Distributed Middleware. Middleware

Erlang and Go (CS262a, Berkeley Fall 2016) Philipp Moritz

Death by Accidental Complexity

Detecting and Visualising Process Relationships in Erlang

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

Programming Distributed Systems

Distributed Middleware. Distributed Objects

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

Programming Language Impact on the Development of Distributed Systems

Overview. This Lecture. Interrupts and exceptions Source: ULK ch 4, ELDD ch1, ch2 & ch4. COSC440 Lecture 3: Interrupts 1

02 - Distributed Systems

Simon Peyton Jones (Microsoft Research) Tokyo Haskell Users Group April 2010

MODELS OF DISTRIBUTED SYSTEMS

Tackling Concurrency With STM. Mark Volkmann 10/22/09

Tackling Concurrency With STM

Architectural Style Requirements for Self-Healing Systems

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

Eventual Consistency Today: Limitations, Extensions and Beyond

JSJS - Project Proposal

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

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

COMMUNICATION IN DISTRIBUTED SYSTEMS

Networked Systems and Services, Fall 2018 Chapter 4. Jussi Kangasharju Markku Kojo Lea Kutvonen

How you can benefit from using. javier

Inter-process communication (IPC)

Before proceeding with this tutorial, you must have a good understanding of Core Java and any of the Linux flavors.

Software Design and Analysis for Engineers

Beautiful Concurrency with Erlang

Distributed KIDS Labs 1

Scripting with Luerl

Introduction. Distributed Systems IT332

CS61B Lecture #5: Arrays and Objects

Introduction to Distributed Systems (DS)

Concurrency: what, why, how

Replication. Feb 10, 2016 CPSC 416

Transcription:

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

Erlang Concurrent Oriented Programming processes have state communicate using message passing access and location transparent asynchronous Functional Programming data structures are immutable 2

History Developed at Ericsson in late eighties, early nineties. Targeting robust applications in the telecom world. Survived despite everything must be Java Growing interest from outside Ericsson. 3

Today Robust server applications: Amazon SimpleDB - database GitHub - code repository RabitMQ - messaging middleware Facebook - chat Why simple to implement fault tolerance scales well in distributed systems multicore performance 4

Erlang background the functional subset concurrency distribution failure detection 5

Data structures Literals atoms: foo, bar,... numbers: 123, 1.23,... bool: true, false Compound tuples: {foo, 12, {bar, zot}} lists: [], [foo, 12, bar, zot] 6

Variables lexically scoped implicit scoping procedure definition case statement untyped assigned a value when introduced Syntax X, Foo, BarZot, _ 7

Assignment and pattern matching Assignment of values to variables is done by pattern matching: <Pattern> = <Expression> A pattern can be a single variable: Foo = 5 Bar = {foo, zot, 42} or a compound pattern {A, B} = {4, 5} {A, [_, B T]} = {41, [foo, bar, zot]} 8

Pattern matching can fail Pattern matching is also used to do test and to guide the execution: {person, Name, Number} = {dog, pluto} 9

No circular structures :-( You can not construct circular data structures in Erlang. Pros makes the implementation easier Cons I like circular structures. 10

Function definitions area(x,y) -> X*Y. 11

If statement fac(n) -> if end. N == 0 -> 1; N > 0 -> N*fac(N-1) 12

Case statement sum(l) -> case L of [] -> 0; [H T] -> H + sum(t) end. 13

Pattern matching member(x,l) -> case L of [] -> no; [X _] -> yes; [_ T] -> member(x, T) end. 14

Higher order F = fun(x) -> X + 1 end. F(5) 15

Higher order map(fun, List) -> case List of [] -> []; [H T] -> [Fun(H) map(fun, T)] end. 16

Modules -module(lst). -export([reverse/1]). reverse(l) -> reverse(l,[]). reverse(l, A) -> case L of [] -> A; [H T] -> reverse(t,[h A]) end. 17

Modules -module(test). -export([palindrome/1]). palindrome(x) -> case lst:reverse(x) of X -> yes; _ -> no end. 18

Concurrency Concurrency is explicitly controlled by creation (spawning) of processes. A process is when created, given a function to evaluate. no one cares about the result Sending and receiving messages is the only way to communicate with a process. no shared state ( well, almost) 19

spawning a process -module(account) start(balance) -> spawn(fun() -> server(balance) end). server(balance) -> : : : 20

Receiving messages server(balance) -> receive {deposit, X} -> server(balance+x); end. {withdraw, X} -> server(balance-x); quit -> ok 21

Sending messages : Account = account:start(40), Account! {deposit, 100}, Account! {withdraw, 50}, : 22

rpc-like communication server(balance) -> receive : : {check, Client} -> Client! {saldo, Balance}, server(balance); : : end. 23

rpc-like communication friday(account) -> Account! {check, self()}, receive {saldo, Balance} -> if Balance > 100 -> party(account); true -> work(account) end end. 24

Process communication P1 receive P2 deliver m1 m2 m2 m1 messages received in FIFO order asynchronous sending, no acknowledgment implicit deferral of message delivery 25

Implicit deferral A process will have an ordered sequence of received messages. The first message that matches one of several program defined patterns will be delivered. Pros and Cons one can select which messages to handle first risk of forgetting messages that are left in a growing queue 26

order of messages src(end, Relay) -> End! a, Relay! b. src a end relay(end) -> receive X -> End! X end. b relay b Distributed Systems ID220U 27

Registration A node register associate names to process identifiers. Knowing the registered name of a process you can look-up the process identifier. The register is a shared data structure! 28

the name is the key : MyAccount = account:start(400), register(johan, MyAccount), : : if end Balance > 100 -> party(account); true -> johan! {withdraw, 100), Account! {deposit, 100}, party(account) 29

Distribution Erlang nodes (an Erlang virtual machine) can be connected in a group. Each node has a unique name. Processes in one node can send messages to and receive messages from processes in other nodes using the same language constructs 30

starting a node moon> erl -sname gold -setcookie jhgsyt : : (gold@moon)> 31

node registry global name {account, 'gold@moon'} a host called moon a node called gold a process registered as account 32

Failure detection A process can monitor another process. if the process dies a messages is placed in the message queue The message will indicate if the termination was normal or abnormal or... if the communication was lost. 33

monitor the account Ref = erlang:monitor(process, Account), Account! {check, self()}, receive {saldo, Balance} -> : end {'DOWN', Ref, process, Account, Reason}-> : 34

Automatic termination A process can link to another process. if the process dies with an exception the linked process will die with the same exception Processes that depend on each other are often linked together, if one dies they all die. 35

linking P = spawn_link(fun()-> server(balance) end), do_something(p), 36

Erlang the functional subset concurrency distribution failure detection 37