+ All Categories
Home > Technology > Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Date post: 10-May-2015
Category:
Upload: benjamin-tan
View: 1,655 times
Download: 2 times
Share this document with a friend
Popular Tags:
64
El ixir Peeking into Elixir’s Processes, OTP & Supervisors 21st March 2014 /benjamintanweihao /bentanweihao [email protected]
Transcript
Page 1: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Elixir

Peeking into Elixir’s

Processes, OTP & Supervisors 21st March 2014

/benjamintanweihao /bentanweihao

[email protected]

Page 2: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

What will we learn today?

Elixir & Erlang

In less than 5 minutes

Page 3: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Elixir & Erlang

In less than 5 minutesProcesses 101

The Basic Concurrency Primitive

What will we learn today?

Page 4: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Elixir & Erlang

In less than 5 minutes

OTP Framework and much more

Processes 101 The Basic Concurrency

Primitive

What will we learn today?

Page 5: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

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?

Page 6: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Ohai, Elixir!

Page 7: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Elixir & Erlang

In less than 5 minutes

Page 8: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

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.

Page 9: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

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.

Page 10: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

• Free lunch is over

Why Elixir ?

Page 11: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Why Elixir ?• Free lunch is over • Hyper-threading & Multicore • Faster software means using all

cores! • But … Concurrency -> Coordination • Functional makes this easier

Page 12: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Design Goals of Elixir

1.Productivity

2.Extensibility

3.Compatibility

Page 13: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Productivity

Complete Elixir Application

Page 14: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Productivity

Includes Supervisor Chain

Page 15: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Productivity

Testing built-in

Page 16: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

ExtensibilityMacros & Meta-programming

Implementing unless using if

Page 17: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

ExtensibilityMacros & Meta-programming

Implementing unless using if

Page 18: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Compatibility

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

Page 19: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Compatibility

Elixir can use Erlang libraries!

Page 20: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

The Actor Concurrency Model

• Actor = Process

Page 21: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

The Actor Concurrency Model

• Actor = Process • A process performs a specific task

when it receives a message

Page 22: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

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

Page 23: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

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

Page 24: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

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

Page 25: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Processes 101 The Basic Concurrency Primitive

Page 26: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Creating Processes & Sending Messages in Elixir

Creating a Process

Page 27: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Creating Processes & Sending Messages in Elixir

Module, Function, Arguments

Page 28: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Creating Processes & Sending Messages in Elixir

Process id

Page 29: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Creating Processes & Sending Messages in Elixir

Sending a Message to w1

Page 30: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Creating Processes & Sending Messages in Elixir

Process waits for a message …

Page 31: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Creating Processes & Sending Messages in Elixir

Pattern matches!

Page 32: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Creating Processes & Sending Messages in Elixir

Result is sent back to the calling process (self)

Page 33: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Creating Processes & Sending Messages in Elixir

Returns immediately

Page 34: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Creating Processes & Sending Messages in Elixir

Get result from self

Page 35: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Creating Processes & Sending Messages in Elixir Demo

Page 36: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

OTP

Framework and much more

Page 37: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

What is OTP?• Comes with Elixir/Erlang • Framework to build

applications that are fault-tolerant, scalable, distributed

• Databases + Profilers + Debuggers

Page 38: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

OTP Behaviours

•GenServer • Supervisor •Application

Page 39: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

An Example GenServerImplement the GenServer Behaviour

Page 40: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

An Example GenServer

Implement GenServer Callbacks

Page 41: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

An Example GenServer

Callbacks are NOT called explicitly

Page 42: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

An Example GenServer

OTP calls the callbacks.

Page 43: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

An Example GenServer

Synchronous Call: Caller waits for reply

Page 44: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

An Example GenServer

Asynchronous Call: Caller doesn’t wait for reply

Page 45: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

An Example GenServer Demo

Page 46: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Supervisors

Fault Tolerance & Recovery

Page 47: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Supervisors for Fault Tolerance and Recovery

one_for_one restart strategy

Page 48: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Supervisors for Fault Tolerance and Recovery

rest_for_all restart strategy

Page 49: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Supervisors for Fault Tolerance and Recovery

rest_for_one restart strategy

Page 50: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

An Example SupervisorImplement the Supervisor Behaviour

Page 51: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

An Example Supervisor

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

Page 52: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

An Example Supervisor

Page 53: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

An Example Supervisor

Declare the restart strategy

Page 54: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Supervisor DemoSupervisor A

Supervisor B Supervisor C

Supervisor D

Server D

Server B

Worker 1Worker 1Worker

Page 55: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Supervisor DemoSupervisor A

Supervisor B Supervisor C

Supervisor D

Server D

Server B

Worker 1Worker 1Worker

one_for_one

Page 56: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Supervisor DemoSupervisor A

Supervisor B Supervisor C

Supervisor D

Server D

Server B

Worker 1Worker 1Worker

one_for_all

Page 57: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Supervisor DemoSupervisor A

Supervisor B Supervisor C

Supervisor D

Server D

Server B

Worker 1Worker 1Worker

simple_one_for_one

Page 58: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Supervisor DemoSupervisor A

Supervisor B Supervisor C

Supervisor D

Server D

Server B

Worker 1Worker 1Worker

one_for_one

Page 59: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Supervisor DemoSupervisor A

Supervisor B Supervisor C

Supervisor D

Server D

Server B

Worker 1Worker 1Worker

one_for_one

simple_one_for_one

one_for_all

one_for_one

Page 60: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Elixir & Erlang

In less than 5 minutes

OTP Framework and much more

Supervisors Fault Tolerance & Recovery

Processes 101 The Basic Concurrency

Primitive

Page 61: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Resources

Page 62: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

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

Me.

Page 63: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Stickers!**Very Limited Quantity

Page 64: Elixir – Peeking into Elixir's Processes, OTP and Supervisors

Thanks!

/benjamintanweihao /bentanweihao

[email protected]


Recommended