Slide 1

Slide 1 text

Elixir Peeking into Elixir’s Processes, OTP & Supervisors 21st March 2014 /benjamintanweihao /bentanweihao [email protected]

Slide 2

Slide 2 text

What will we learn today? Elixir & Erlang In less than 5 minutes

Slide 3

Slide 3 text

Elixir & Erlang In less than 5 minutes Processes 101 The Basic Concurrency Primitive What will we learn today?

Slide 4

Slide 4 text

Elixir & Erlang In less than 5 minutes OTP Framework and much more Processes 101 The Basic Concurrency Primitive What will we learn today?

Slide 5

Slide 5 text

Elixir & Erlang In less than 5 minutes OTP Framework and much more Supervisors Fault Tolerance & Recovery Processes 101 The Basic Concurrency Primitive What will we learn today?

Slide 6

Slide 6 text

Ohai, Elixir!

Slide 7

Slide 7 text

Elixir & Erlang In less than 5 minutes

Slide 8

Slide 8 text

Ohai, Erlang! Erlang is a general-purpose concurrent, garbage-collected programming language and runtime system. The sequential subset of Erlang is a functional language, with eager evaluation, single assignment, and dynamic typing. It was designed by Ericsson to support distributed, fault-tolerant, soft-real-time, non- stop applications. It supports hot swapping, so that code can be changed without stopping a system.

Slide 9

Slide 9 text

Ohai, Erlang! Erlang is a general-purpose concurrent, garbage-collected programming language and runtime system. The sequential subset of Erlang is a functional language, with eager evaluation, single assignment, and dynamic typing. It was designed by Ericsson to support distributed, fault-tolerant, soft-real-time, non- stop applications. It supports hot swapping, so that code can be changed without stopping a system.

Slide 10

Slide 10 text

• Free lunch is over Why Elixir ?

Slide 11

Slide 11 text

Why Elixir ? • Free lunch is over • Hyper-threading & Multicore • Faster software means using all cores! • But … Concurrency -> Coordination • Functional makes this easier

Slide 12

Slide 12 text

Design Goals of Elixir 1.Productivity 2.Extensibility 3.Compatibility

Slide 13

Slide 13 text

Productivity Complete Elixir Application

Slide 14

Slide 14 text

Productivity Includes Supervisor Chain

Slide 15

Slide 15 text

Productivity Testing built-in

Slide 16

Slide 16 text

Extensibility Macros & Meta-programming Implementing unless using if

Slide 17

Slide 17 text

Extensibility Macros & Meta-programming Implementing unless using if

Slide 18

Slide 18 text

Compatibility Elixir can call Erlang code, without any conversion cost at all.

Slide 19

Slide 19 text

Compatibility Elixir can use Erlang libraries!

Slide 20

Slide 20 text

The Actor Concurrency Model • Actor = Process

Slide 21

Slide 21 text

The Actor Concurrency Model • Actor = Process • A process performs a specific task when it receives a message

Slide 22

Slide 22 text

The Actor Concurrency Model • Actor = Process • A process performs a specific task when it receives a message • In turn, the process can reply to the sender

Slide 23

Slide 23 text

The Actor Concurrency Model • Actor = Process • A process performs a specific task when it receives a message • In turn, the process can reply to the sender • All messages go to a processes’ mailbox – Q of unprocessed messages sent from other processes that are not yet consumed

Slide 24

Slide 24 text

The Actor Concurrency Model • Actor = Process • A process performs a specific task when it receives a message • In turn, the process can reply to the sender • All messages go to a processes’ mailbox – Q of unprocessed messages sent from other processes that are not yet consumed Shared-nothing Async Message-passing

Slide 25

Slide 25 text

Processes 101 The Basic Concurrency Primitive

Slide 26

Slide 26 text

Creating Processes & Sending Messages in Elixir Creating a Process

Slide 27

Slide 27 text

Creating Processes & Sending Messages in Elixir Module, Function, Arguments

Slide 28

Slide 28 text

Creating Processes & Sending Messages in Elixir Process id

Slide 29

Slide 29 text

Creating Processes & Sending Messages in Elixir Sending a Message to w1

Slide 30

Slide 30 text

Creating Processes & Sending Messages in Elixir Process waits for a message …

Slide 31

Slide 31 text

Creating Processes & Sending Messages in Elixir Pattern matches!

Slide 32

Slide 32 text

Creating Processes & Sending Messages in Elixir Result is sent back to the calling process (self)

Slide 33

Slide 33 text

Creating Processes & Sending Messages in Elixir Returns immediately

Slide 34

Slide 34 text

Creating Processes & Sending Messages in Elixir Get result from self

Slide 35

Slide 35 text

Creating Processes & Sending Messages in Elixir Demo

Slide 36

Slide 36 text

OTP Framework and much more

Slide 37

Slide 37 text

What is OTP? • Comes with Elixir/Erlang • Framework to build applications that are fault- tolerant, scalable, distributed • Databases + Profilers + Debuggers

Slide 38

Slide 38 text

OTP Behaviours • GenServer • Supervisor • Application

Slide 39

Slide 39 text

An Example GenServer Implement the GenServer Behaviour

Slide 40

Slide 40 text

An Example GenServer Implement GenServer Callbacks

Slide 41

Slide 41 text

An Example GenServer Callbacks are NOT called explicitly

Slide 42

Slide 42 text

An Example GenServer OTP calls the callbacks.

Slide 43

Slide 43 text

An Example GenServer Synchronous Call: Caller waits for reply

Slide 44

Slide 44 text

An Example GenServer Asynchronous Call: Caller doesn’t wait for reply

Slide 45

Slide 45 text

An Example GenServer Demo

Slide 46

Slide 46 text

Supervisors Fault Tolerance & Recovery

Slide 47

Slide 47 text

Supervisors for Fault Tolerance and Recovery one_for_one restart strategy

Slide 48

Slide 48 text

Supervisors for Fault Tolerance and Recovery rest_for_all restart strategy

Slide 49

Slide 49 text

Supervisors for Fault Tolerance and Recovery rest_for_one restart strategy

Slide 50

Slide 50 text

An Example Supervisor Implement the Supervisor Behaviour

Slide 51

Slide 51 text

An Example Supervisor Declaring the Supervision tree. Both Supervisors and Workers (e.g. GenServers) can be supervised.

Slide 52

Slide 52 text

An Example Supervisor

Slide 53

Slide 53 text

An Example Supervisor Declare the restart strategy

Slide 54

Slide 54 text

Supervisor Demo Supervisor A Supervisor B Supervisor C Supervisor D Server D Server B Worker 1 Worker 1 Worker

Slide 55

Slide 55 text

Supervisor Demo Supervisor A Supervisor B Supervisor C Supervisor D Server D Server B Worker 1 Worker 1 Worker one_for_one

Slide 56

Slide 56 text

Supervisor Demo Supervisor A Supervisor B Supervisor C Supervisor D Server D Server B Worker 1 Worker 1 Worker one_for_all

Slide 57

Slide 57 text

Supervisor Demo Supervisor A Supervisor B Supervisor C Supervisor D Server D Server B Worker 1 Worker 1 Worker simple_one_for_one

Slide 58

Slide 58 text

Supervisor Demo Supervisor A Supervisor B Supervisor C Supervisor D Server D Server B Worker 1 Worker 1 Worker one_for_one

Slide 59

Slide 59 text

Supervisor Demo Supervisor A Supervisor B Supervisor C Supervisor D Server D Server B Worker 1 Worker 1 Worker one_for_one simple_one_for_one one_for_all one_for_one

Slide 60

Slide 60 text

Elixir & Erlang In less than 5 minutes OTP Framework and much more Supervisors Fault Tolerance & Recovery Processes 101 The Basic Concurrency Primitive

Slide 61

Slide 61 text

Resources

Slide 62

Slide 62 text

Work in Progress! ! Sign up at: http://www.exotpbook.com Me.

Slide 63

Slide 63 text

Stickers!* *Very Limited Quantity

Slide 64

Slide 64 text

Thanks! /benjamintanweihao /bentanweihao [email protected]