SlideShare una empresa de Scribd logo
1 de 24
Descargar para leer sin conexión
Concurrency, Robustness and Elixir
SoCraTes 2015
Erlang About Erlang
What is Erlang?
Erlang is ...
●
A programming language (Concurrent, Functional)
●
A virtual machine (lightweight, massively concurrent, asynchronous)
●
An ecosystem (OTP, design patterns and libraries for building robust systems)
●
Developed since 1986, Joe Armstrong et al (Ericsson)
●
Open Source since 1998
●
Initially used for telephone switches
●
Now for many other applications requiring High Availability & distributed
concurrency:
➔
ejabberd
➔
CouchDB
➔
GitHub
Elixir The Elixir project
What is Elixir?
Elixir is a functional, concurrent, general-purpose programming language built atop
the Erlang Virtual Machine.
●
Full compatibility with the Erlang universe, compiles to Erlang bytecode
●
Refurbished syntax (Ruby inspired)
●
Aims for enhanced productivity (better build tools, less boilerplate)
●
Extensibility (Structs, Protocols, DSLs -> Clojure inspired)
Quite young -> First commit Jan 9, 2011
Current version 1.0.5
http://elixir-lang.org/
Erlang Computing challenges of the 21st century
4004
8080
186
286
i386
i486
P5 Pentium
P6
P7
Core
Nehalem
Ivy Bridge
Haswell
0.1
1
10
100
1000
10000
MHz
Clock speed limit Multicore Clustering & Cloud High availability
Concurrent
programming
in Elixir
Elixir The Actor model
Process
Mailbox
Msg 1
Msg 2
Msg 3
defmodule ListenerServer do
use GenServer
def init(args) do
Process.register self, :listener
{:ok, []}
end
def handle_cast(:quit, state) do
{:stop, :normal, state}
end
def handle_cast(message, state) do
IO.puts "Got the message '#{message}'"
{:noreply, state}
end
end
Behaviour
{ state }
Elixir The Actor model
Process
Spawn
Send/
Receive
Mailbox
Msg 1
Msg 2
Msg 3
defmodule ListenerServer do
use GenServer
def init(args) do
Process.register self, :listener
{:ok, []}
end
def handle_cast(:quit, state) do
{:stop, :normal, state}
end
def handle_cast(message, state) do
IO.puts "Got the message'"
{:noreply, state}
end
end
Behaviour
{state}
Elixir Actors in Elixir
Erlang / Elixirs key feature: Packing code into small chunks that can be run
independently and concurrently.
Actor Model:
●
Independent asynchronous processes
●
Share nothing with other processes
●
Communication only via messages
●
Messages are stored in the process mailbox until consumed
●
A process can create new processes
●
Processes have an inner state
Elixir processes ...
●
Aren't system threads or processes.
●
Managed by the Erlang Virtual Machine
●
Extremely light-weight, nearly no overhead to create one.
●
Even a modest computer can run > 100000 Elixir processes.
●
Can be changed at run-time (hot-code swapping)
Elixir OOP versus COP
Object oriented programming Concurrent oriented programming
Class Actor
Instance Process
Instantiation Spawn
Constructor Initializer
Prototype Behaviour
Setter/Getter Message
Interface Protocol
Attribute State
Singleton Registered process
pid = spawn function # Start a new process, return its PID
send pid, message # Send message to process with pid
receive do # Try to match messages in process mailbox
match1 -> result1
match2 -> result2
...
after timeout -> result # Result when no message arrived in timeout
end
self # Process ID of current process
Process.list # List all running processes
flush # (iex) Flush mailbox, print all messages
:pman.start # Start Erlang process manager
Elixir Concurrent programming
defmodule Listener do
def listen do
receive do
:quit -> exit(nil)
message -> IO.puts "Got the message '#{message}'"
end
listen
end
end
listener = spawn fn -> Listener.listen end
send listener, "Hello World"
send listener, :quit
Elixir Process programming
defmodule Listener do
def listen do
receive do
:quit -> exit(nil)
{message, from} -> IO.puts "Got the message '#{message}'"
send from, "Thank you for the message!"
end
listen
end
end
listener = spawn fn -> Listener.listen end
send listener, {"Hello World", self}
flush
send listener, :quit
flush
Elixir Process programming
defmodule Listener do
def start do
Process.register self, :listener
listen
end
def listen do
receive do
:quit -> exit(nil)
message -> IO.puts "Got the message '#{message}'"
end
listen
end
end
spawn fn -> Listener.start end
send :listener, "Hello World"
send :listener, :quit
Elixir Process registering
Elixir Process behaviours
Task Asynchronous calculation
Agent State-carrying process
GenServer Generic OTP Server
Supervisor OTP Supervisor
Application OTP Application
GenEvent Generic Event Handler
:gen_fsm Generic Finite State Machine
Elixir Tasks and Agents
# Start an independent task
task = Task.async(fn -> do_some_work() end)
# Collect the result
result = Task.await(task)
Tasks: Independent asynchronous calculations
# Start a new agent
{:ok, agent} = Agent.start(fn -> initial_state end)
# Update the agent state
Agent.update(agent, fn old_state -> new_state end)
# Get the current agent state
Agent.get(agent, fn state -> value end)
# Terminate the agent
Agent.stop(agent)
Agents: Wrappers around state
line = "Freude schöner Götterfunken"
task = Task.async(fn -> String.upcase(line) end)
IO.puts Task.await(task)
task = Task.async(fn -> :timer.sleep(3000);
String.upcase(line) end)
IO.puts "Waiting for the task to complete..."
IO.puts Task.await(task)
Elixir Task and Agent example
{:ok, agent} = Agent.start(fn -> 0 end, [name: :counter])
IO.puts Agent.get(:counter, fn value -> value end)
Agent.update(:counter, fn value -> value+1 end)
IO.puts Agent.get(:counter, fn value -> value end)
Agent.stop(:counter)
Elixir Generic Server Callbacks
defmodule MyServer do
use GenServer
# Initialize server
def init(args), do: {:ok, state}
# Terminate server
def terminate(reason, state), do: {reason, state}
# Call with reply
def handle_call(msg, from, state), do: {:reply, reply, new_state}
# Casts without reply
def handle_cast( msg, state ), do: {:noreply, new_state}
# All other messages
def handle_info( msg, state ), do: {:noreply, new_state}
# Hot code swap
def code_change( vsn, state, extra ), do: {:ok, new_state}
end
defmodule ListenerServer do
use GenServer
# Public API
def start_link, do: GenServer.start_link ListenerServer, []
# Callback functions
def init(args) do
Process.register self, :listener
{:ok, []}
end
def handle_cast(:quit, state) do
{:stop, :normal, state}
end
def handle_cast(message, state) do
IO.puts "Got the message '#{message}'"
{:noreply, state}
end
end
{:ok, pid} = ListenerServer.start_link
GenServer.cast :listener, "Hello"
GenServer.cast :listener, :quit
Elixir Generic Server
defmodule ListenerServer.Supervisor do
import Supervisor.Spec
def start do
children = [ worker(ListenerServer, [], restart: :transient) ]
Supervisor.start_link(children, strategy: :one_for_one)
end
end
ListenerServer.Supervisor.start
GenServer.cast :listener, "Hello"
GenServer.cast :listener, {1,2,3} # Crashes the listener server
GenServer.cast :listener, "Hello World"
GenServer.cast :listener, :quit
Elixir Supervision
defmodule FirstEventHandler do
use GenEvent
def handle_event(message, state) do
IO.puts "The first event handler was notified: '#{message}'"
{:ok, state}
end
end
defmodule SecondEventHandler do
use GenEvent
def handle_event(message, state) do
IO.puts "The second event handler was notified: '#{message}'"
{:ok, state}
end
end
GenEvent.start_link(name: :eventmanager)
GenEvent.add_handler(:eventmanager, FirstEventHandler, nil)
GenEvent.add_handler(:eventmanager, SecondEventHandler, nil)
GenEvent.notify(:eventmanager, "Hi there!")
Elixir Event Handler
defmodule FiniteStateMachine do
@behaviour :gen_fsm
def start_link(_opts) do
:gen_fsm.start_link({:local, FSM}, __MODULE__, [], [])
end
def next_state() do
:gen_fsm.send_event(FSM, :round)
end
def init(_), do: IO.puts "FSM started"; { :ok, :ping, [] }
def ping(:round, state), do: IO.puts "Ping!"; {:next_state, :pong, state}
def pong(:round, state), do: IO.puts "Pong!"; {:next_state, :ping, state}
end
FiniteStateMachine.start_link [] # "FSM started"
FiniteStateMachine.next_state # "Ping!"
FiniteStateMachine.next_state # "Pong!"
FiniteStateMachine.next_state # "Ping!"
FiniteStateMachine.next_state # "Pong!"
Elixir Finite state machine
Elixir OTP
Super-Supervisor
Supervisor Supervisor
Supervisor
Worker Worker Worker
Worker
Worker Worker WorkerHelper Helper
Application
defmodule ExampleApp do
use Application
def start(_type, _args) do
MainSupervisor.start
end
end
Elixir Elixir application project
Build tool mix:
> mix new exampleapp
mix.exs # Mix project definition file
/config/config.exs # Project specific configuration
/lib/ # Source code
/test/ # Tests suites
# Starts the BEAM Erlang Virtual Machine
# and the EPMD Erlang Port Mapper Daemon (default port 4369)
cluster01> elixir --sname node --no-halt --cookie secret_cookie
Elixir Distributed computing: Nodes
# Elixir master node (on same subnet as cluster nodes)
master> iex --sname masternode --cookie secret_cookie
Node.connect :"node@cluster01" # Establish node connection
pid = Node.spawn :"node@cluster01", func # Spawn a process
Node.self # Name of current node
Node.list # List all connected nodes

Más contenido relacionado

La actualidad más candente

Rethinking the debugger
Rethinking the debuggerRethinking the debugger
Rethinking the debuggerIulian Dragos
 
GenRetry: Simple Exponential Backoff in Elixir
GenRetry: Simple Exponential Backoff in ElixirGenRetry: Simple Exponential Backoff in Elixir
GenRetry: Simple Exponential Backoff in ElixirPete Gamache
 
Introduction to Phoenix Framework (Elixir) 2016-01-07
Introduction to Phoenix Framework (Elixir) 2016-01-07Introduction to Phoenix Framework (Elixir) 2016-01-07
Introduction to Phoenix Framework (Elixir) 2016-01-07Svein Fidjestøl
 
Awesome Concurrency with Elixir Tasks
Awesome Concurrency with Elixir TasksAwesome Concurrency with Elixir Tasks
Awesome Concurrency with Elixir TasksJonathan Magen
 
DDD loves Actor Model and Actor Model loves Elixir
DDD loves Actor Model and Actor Model loves ElixirDDD loves Actor Model and Actor Model loves Elixir
DDD loves Actor Model and Actor Model loves ElixirGianluca Padovani
 
c3ee900a-894b-4680-894c-42f77ffd4446-160308162303
c3ee900a-894b-4680-894c-42f77ffd4446-160308162303c3ee900a-894b-4680-894c-42f77ffd4446-160308162303
c3ee900a-894b-4680-894c-42f77ffd4446-160308162303Robert Warren
 
Ansible, Idempotency, and Jenkins
Ansible, Idempotency, and JenkinsAnsible, Idempotency, and Jenkins
Ansible, Idempotency, and Jenkinstylerturk
 
Voice Interfaces Usergroup Berlin - 05-10-2016 : Kay Lerch on Morse-Coder skill
Voice Interfaces Usergroup Berlin - 05-10-2016 : Kay Lerch on Morse-Coder skillVoice Interfaces Usergroup Berlin - 05-10-2016 : Kay Lerch on Morse-Coder skill
Voice Interfaces Usergroup Berlin - 05-10-2016 : Kay Lerch on Morse-Coder skillKay Lerch
 
Nice performance using Sf2 cache wrapping Sf1 application - Paris
Nice performance using Sf2 cache wrapping Sf1 application - ParisNice performance using Sf2 cache wrapping Sf1 application - Paris
Nice performance using Sf2 cache wrapping Sf1 application - ParisMarc Weistroff
 
How Symfony Changed My Life
How Symfony Changed My LifeHow Symfony Changed My Life
How Symfony Changed My LifeMatthias Noback
 
Asynchronous Programming in C# - Part 1
Asynchronous Programming in C# - Part 1Asynchronous Programming in C# - Part 1
Asynchronous Programming in C# - Part 1Mindfire Solutions
 
Eclipse and Java 8 - Eclipse Day India 2013
Eclipse and Java 8 - Eclipse Day India 2013Eclipse and Java 8 - Eclipse Day India 2013
Eclipse and Java 8 - Eclipse Day India 2013Noopur Gupta
 
C# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programmingC# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programmingPraveen Prajapati
 
JDT Embraces Lambda Expressions - EclipseCon North America 2014
JDT Embraces Lambda Expressions - EclipseCon North America 2014JDT Embraces Lambda Expressions - EclipseCon North America 2014
JDT Embraces Lambda Expressions - EclipseCon North America 2014Noopur Gupta
 
Elm & Elixir: Functional Programming and Web
Elm & Elixir: Functional Programming and WebElm & Elixir: Functional Programming and Web
Elm & Elixir: Functional Programming and WebPublitory
 

La actualidad más candente (20)

Rethinking the debugger
Rethinking the debuggerRethinking the debugger
Rethinking the debugger
 
GenRetry: Simple Exponential Backoff in Elixir
GenRetry: Simple Exponential Backoff in ElixirGenRetry: Simple Exponential Backoff in Elixir
GenRetry: Simple Exponential Backoff in Elixir
 
Introduction to Phoenix Framework (Elixir) 2016-01-07
Introduction to Phoenix Framework (Elixir) 2016-01-07Introduction to Phoenix Framework (Elixir) 2016-01-07
Introduction to Phoenix Framework (Elixir) 2016-01-07
 
Awesome Concurrency with Elixir Tasks
Awesome Concurrency with Elixir TasksAwesome Concurrency with Elixir Tasks
Awesome Concurrency with Elixir Tasks
 
DDD loves Actor Model and Actor Model loves Elixir
DDD loves Actor Model and Actor Model loves ElixirDDD loves Actor Model and Actor Model loves Elixir
DDD loves Actor Model and Actor Model loves Elixir
 
c3ee900a-894b-4680-894c-42f77ffd4446-160308162303
c3ee900a-894b-4680-894c-42f77ffd4446-160308162303c3ee900a-894b-4680-894c-42f77ffd4446-160308162303
c3ee900a-894b-4680-894c-42f77ffd4446-160308162303
 
CPP03 - Repetition
CPP03 - RepetitionCPP03 - Repetition
CPP03 - Repetition
 
Ansible, Idempotency, and Jenkins
Ansible, Idempotency, and JenkinsAnsible, Idempotency, and Jenkins
Ansible, Idempotency, and Jenkins
 
React Testing
React TestingReact Testing
React Testing
 
Voice Interfaces Usergroup Berlin - 05-10-2016 : Kay Lerch on Morse-Coder skill
Voice Interfaces Usergroup Berlin - 05-10-2016 : Kay Lerch on Morse-Coder skillVoice Interfaces Usergroup Berlin - 05-10-2016 : Kay Lerch on Morse-Coder skill
Voice Interfaces Usergroup Berlin - 05-10-2016 : Kay Lerch on Morse-Coder skill
 
Nice performance using Sf2 cache wrapping Sf1 application - Paris
Nice performance using Sf2 cache wrapping Sf1 application - ParisNice performance using Sf2 cache wrapping Sf1 application - Paris
Nice performance using Sf2 cache wrapping Sf1 application - Paris
 
How Symfony Changed My Life
How Symfony Changed My LifeHow Symfony Changed My Life
How Symfony Changed My Life
 
Asynchronous Programming in C# - Part 1
Asynchronous Programming in C# - Part 1Asynchronous Programming in C# - Part 1
Asynchronous Programming in C# - Part 1
 
Eclipse and Java 8 - Eclipse Day India 2013
Eclipse and Java 8 - Eclipse Day India 2013Eclipse and Java 8 - Eclipse Day India 2013
Eclipse and Java 8 - Eclipse Day India 2013
 
C# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programmingC# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programming
 
JDT Embraces Lambda Expressions - EclipseCon North America 2014
JDT Embraces Lambda Expressions - EclipseCon North America 2014JDT Embraces Lambda Expressions - EclipseCon North America 2014
JDT Embraces Lambda Expressions - EclipseCon North America 2014
 
Elixir for Rubyists
Elixir for RubyistsElixir for Rubyists
Elixir for Rubyists
 
2021laravelconftwslides6
2021laravelconftwslides62021laravelconftwslides6
2021laravelconftwslides6
 
Create Your Own Language
Create Your Own LanguageCreate Your Own Language
Create Your Own Language
 
Elm & Elixir: Functional Programming and Web
Elm & Elixir: Functional Programming and WebElm & Elixir: Functional Programming and Web
Elm & Elixir: Functional Programming and Web
 

Similar a Concurrency, Robustness & Elixir SoCraTes 2015

Yurii Bodarev - OTP, Phoenix & Ecto: Three Pillars of Elixir
Yurii Bodarev - OTP, Phoenix & Ecto: Three Pillars of ElixirYurii Bodarev - OTP, Phoenix & Ecto: Three Pillars of Elixir
Yurii Bodarev - OTP, Phoenix & Ecto: Three Pillars of ElixirElixir Club
 
Concurrency and Thread-Safe Data Processing in Background Tasks
Concurrency and Thread-Safe Data Processing in Background TasksConcurrency and Thread-Safe Data Processing in Background Tasks
Concurrency and Thread-Safe Data Processing in Background TasksWO Community
 
Introduction to Erlang/(Elixir) at a Webilea Hands-On Session
Introduction to Erlang/(Elixir) at a Webilea Hands-On SessionIntroduction to Erlang/(Elixir) at a Webilea Hands-On Session
Introduction to Erlang/(Elixir) at a Webilea Hands-On SessionAndré Graf
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)jeffz
 
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had ...
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had ...TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had ...
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had ...tdc-globalcode
 
Concurrency in Elixir with OTP
Concurrency in Elixir with OTPConcurrency in Elixir with OTP
Concurrency in Elixir with OTPJustin Reese
 
Giorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrencyGiorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrencyGiorgio Zoppi
 
Elixir Into Production
Elixir Into ProductionElixir Into Production
Elixir Into ProductionJamie Winsor
 
Game Analytics Cluster Scheduler
Game Analytics Cluster SchedulerGame Analytics Cluster Scheduler
Game Analytics Cluster Schedulercmmdevries
 
Combining the strength of erlang and Ruby
Combining the strength of erlang and RubyCombining the strength of erlang and Ruby
Combining the strength of erlang and RubyMartin Rehfeld
 
Combining the Strengths or Erlang and Ruby
Combining the Strengths or Erlang and RubyCombining the Strengths or Erlang and Ruby
Combining the Strengths or Erlang and RubyWooga
 
Get into Functional Programming with Clojure
Get into Functional Programming with ClojureGet into Functional Programming with Clojure
Get into Functional Programming with ClojureJohn Stevenson
 
Introduction to Elixir
Introduction to ElixirIntroduction to Elixir
Introduction to ElixirDiacode
 
Dealing with Python Reactively - PyCon Korea 2017
Dealing with Python Reactively - PyCon Korea 2017Dealing with Python Reactively - PyCon Korea 2017
Dealing with Python Reactively - PyCon Korea 2017Kenneth Ceyer
 
Introduction To Erlang Final
Introduction To Erlang   FinalIntroduction To Erlang   Final
Introduction To Erlang FinalSinarShebl
 

Similar a Concurrency, Robustness & Elixir SoCraTes 2015 (20)

Elixir concurrency 101
Elixir concurrency 101Elixir concurrency 101
Elixir concurrency 101
 
Yurii Bodarev - OTP, Phoenix & Ecto: Three Pillars of Elixir
Yurii Bodarev - OTP, Phoenix & Ecto: Three Pillars of ElixirYurii Bodarev - OTP, Phoenix & Ecto: Three Pillars of Elixir
Yurii Bodarev - OTP, Phoenix & Ecto: Three Pillars of Elixir
 
Concurrency and Thread-Safe Data Processing in Background Tasks
Concurrency and Thread-Safe Data Processing in Background TasksConcurrency and Thread-Safe Data Processing in Background Tasks
Concurrency and Thread-Safe Data Processing in Background Tasks
 
Introduction to Erlang/(Elixir) at a Webilea Hands-On Session
Introduction to Erlang/(Elixir) at a Webilea Hands-On SessionIntroduction to Erlang/(Elixir) at a Webilea Hands-On Session
Introduction to Erlang/(Elixir) at a Webilea Hands-On Session
 
Node js lecture
Node js lectureNode js lecture
Node js lecture
 
Concurrecny inf sharp
Concurrecny inf sharpConcurrecny inf sharp
Concurrecny inf sharp
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
 
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had ...
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had ...TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had ...
TDC2017 | São Paulo - Trilha Programação Funcional How we figured out we had ...
 
Concurrency in Elixir with OTP
Concurrency in Elixir with OTPConcurrency in Elixir with OTP
Concurrency in Elixir with OTP
 
Giorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrencyGiorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrency
 
Elixir Into Production
Elixir Into ProductionElixir Into Production
Elixir Into Production
 
Celery with python
Celery with pythonCelery with python
Celery with python
 
Game Analytics Cluster Scheduler
Game Analytics Cluster SchedulerGame Analytics Cluster Scheduler
Game Analytics Cluster Scheduler
 
Combining the strength of erlang and Ruby
Combining the strength of erlang and RubyCombining the strength of erlang and Ruby
Combining the strength of erlang and Ruby
 
Combining the Strengths or Erlang and Ruby
Combining the Strengths or Erlang and RubyCombining the Strengths or Erlang and Ruby
Combining the Strengths or Erlang and Ruby
 
Get into Functional Programming with Clojure
Get into Functional Programming with ClojureGet into Functional Programming with Clojure
Get into Functional Programming with Clojure
 
Introduction to Elixir
Introduction to ElixirIntroduction to Elixir
Introduction to Elixir
 
Dealing with Python Reactively - PyCon Korea 2017
Dealing with Python Reactively - PyCon Korea 2017Dealing with Python Reactively - PyCon Korea 2017
Dealing with Python Reactively - PyCon Korea 2017
 
JavaScript for real men
JavaScript for real menJavaScript for real men
JavaScript for real men
 
Introduction To Erlang Final
Introduction To Erlang   FinalIntroduction To Erlang   Final
Introduction To Erlang Final
 

Último

EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 

Último (20)

EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 

Concurrency, Robustness & Elixir SoCraTes 2015

  • 1. Concurrency, Robustness and Elixir SoCraTes 2015
  • 2. Erlang About Erlang What is Erlang? Erlang is ... ● A programming language (Concurrent, Functional) ● A virtual machine (lightweight, massively concurrent, asynchronous) ● An ecosystem (OTP, design patterns and libraries for building robust systems) ● Developed since 1986, Joe Armstrong et al (Ericsson) ● Open Source since 1998 ● Initially used for telephone switches ● Now for many other applications requiring High Availability & distributed concurrency: ➔ ejabberd ➔ CouchDB ➔ GitHub
  • 3. Elixir The Elixir project What is Elixir? Elixir is a functional, concurrent, general-purpose programming language built atop the Erlang Virtual Machine. ● Full compatibility with the Erlang universe, compiles to Erlang bytecode ● Refurbished syntax (Ruby inspired) ● Aims for enhanced productivity (better build tools, less boilerplate) ● Extensibility (Structs, Protocols, DSLs -> Clojure inspired) Quite young -> First commit Jan 9, 2011 Current version 1.0.5 http://elixir-lang.org/
  • 4. Erlang Computing challenges of the 21st century 4004 8080 186 286 i386 i486 P5 Pentium P6 P7 Core Nehalem Ivy Bridge Haswell 0.1 1 10 100 1000 10000 MHz Clock speed limit Multicore Clustering & Cloud High availability
  • 6. Elixir The Actor model Process Mailbox Msg 1 Msg 2 Msg 3 defmodule ListenerServer do use GenServer def init(args) do Process.register self, :listener {:ok, []} end def handle_cast(:quit, state) do {:stop, :normal, state} end def handle_cast(message, state) do IO.puts "Got the message '#{message}'" {:noreply, state} end end Behaviour { state }
  • 7. Elixir The Actor model Process Spawn Send/ Receive Mailbox Msg 1 Msg 2 Msg 3 defmodule ListenerServer do use GenServer def init(args) do Process.register self, :listener {:ok, []} end def handle_cast(:quit, state) do {:stop, :normal, state} end def handle_cast(message, state) do IO.puts "Got the message'" {:noreply, state} end end Behaviour {state}
  • 8. Elixir Actors in Elixir Erlang / Elixirs key feature: Packing code into small chunks that can be run independently and concurrently. Actor Model: ● Independent asynchronous processes ● Share nothing with other processes ● Communication only via messages ● Messages are stored in the process mailbox until consumed ● A process can create new processes ● Processes have an inner state Elixir processes ... ● Aren't system threads or processes. ● Managed by the Erlang Virtual Machine ● Extremely light-weight, nearly no overhead to create one. ● Even a modest computer can run > 100000 Elixir processes. ● Can be changed at run-time (hot-code swapping)
  • 9. Elixir OOP versus COP Object oriented programming Concurrent oriented programming Class Actor Instance Process Instantiation Spawn Constructor Initializer Prototype Behaviour Setter/Getter Message Interface Protocol Attribute State Singleton Registered process
  • 10. pid = spawn function # Start a new process, return its PID send pid, message # Send message to process with pid receive do # Try to match messages in process mailbox match1 -> result1 match2 -> result2 ... after timeout -> result # Result when no message arrived in timeout end self # Process ID of current process Process.list # List all running processes flush # (iex) Flush mailbox, print all messages :pman.start # Start Erlang process manager Elixir Concurrent programming
  • 11. defmodule Listener do def listen do receive do :quit -> exit(nil) message -> IO.puts "Got the message '#{message}'" end listen end end listener = spawn fn -> Listener.listen end send listener, "Hello World" send listener, :quit Elixir Process programming
  • 12. defmodule Listener do def listen do receive do :quit -> exit(nil) {message, from} -> IO.puts "Got the message '#{message}'" send from, "Thank you for the message!" end listen end end listener = spawn fn -> Listener.listen end send listener, {"Hello World", self} flush send listener, :quit flush Elixir Process programming
  • 13. defmodule Listener do def start do Process.register self, :listener listen end def listen do receive do :quit -> exit(nil) message -> IO.puts "Got the message '#{message}'" end listen end end spawn fn -> Listener.start end send :listener, "Hello World" send :listener, :quit Elixir Process registering
  • 14. Elixir Process behaviours Task Asynchronous calculation Agent State-carrying process GenServer Generic OTP Server Supervisor OTP Supervisor Application OTP Application GenEvent Generic Event Handler :gen_fsm Generic Finite State Machine
  • 15. Elixir Tasks and Agents # Start an independent task task = Task.async(fn -> do_some_work() end) # Collect the result result = Task.await(task) Tasks: Independent asynchronous calculations # Start a new agent {:ok, agent} = Agent.start(fn -> initial_state end) # Update the agent state Agent.update(agent, fn old_state -> new_state end) # Get the current agent state Agent.get(agent, fn state -> value end) # Terminate the agent Agent.stop(agent) Agents: Wrappers around state
  • 16. line = "Freude schöner Götterfunken" task = Task.async(fn -> String.upcase(line) end) IO.puts Task.await(task) task = Task.async(fn -> :timer.sleep(3000); String.upcase(line) end) IO.puts "Waiting for the task to complete..." IO.puts Task.await(task) Elixir Task and Agent example {:ok, agent} = Agent.start(fn -> 0 end, [name: :counter]) IO.puts Agent.get(:counter, fn value -> value end) Agent.update(:counter, fn value -> value+1 end) IO.puts Agent.get(:counter, fn value -> value end) Agent.stop(:counter)
  • 17. Elixir Generic Server Callbacks defmodule MyServer do use GenServer # Initialize server def init(args), do: {:ok, state} # Terminate server def terminate(reason, state), do: {reason, state} # Call with reply def handle_call(msg, from, state), do: {:reply, reply, new_state} # Casts without reply def handle_cast( msg, state ), do: {:noreply, new_state} # All other messages def handle_info( msg, state ), do: {:noreply, new_state} # Hot code swap def code_change( vsn, state, extra ), do: {:ok, new_state} end
  • 18. defmodule ListenerServer do use GenServer # Public API def start_link, do: GenServer.start_link ListenerServer, [] # Callback functions def init(args) do Process.register self, :listener {:ok, []} end def handle_cast(:quit, state) do {:stop, :normal, state} end def handle_cast(message, state) do IO.puts "Got the message '#{message}'" {:noreply, state} end end {:ok, pid} = ListenerServer.start_link GenServer.cast :listener, "Hello" GenServer.cast :listener, :quit Elixir Generic Server
  • 19. defmodule ListenerServer.Supervisor do import Supervisor.Spec def start do children = [ worker(ListenerServer, [], restart: :transient) ] Supervisor.start_link(children, strategy: :one_for_one) end end ListenerServer.Supervisor.start GenServer.cast :listener, "Hello" GenServer.cast :listener, {1,2,3} # Crashes the listener server GenServer.cast :listener, "Hello World" GenServer.cast :listener, :quit Elixir Supervision
  • 20. defmodule FirstEventHandler do use GenEvent def handle_event(message, state) do IO.puts "The first event handler was notified: '#{message}'" {:ok, state} end end defmodule SecondEventHandler do use GenEvent def handle_event(message, state) do IO.puts "The second event handler was notified: '#{message}'" {:ok, state} end end GenEvent.start_link(name: :eventmanager) GenEvent.add_handler(:eventmanager, FirstEventHandler, nil) GenEvent.add_handler(:eventmanager, SecondEventHandler, nil) GenEvent.notify(:eventmanager, "Hi there!") Elixir Event Handler
  • 21. defmodule FiniteStateMachine do @behaviour :gen_fsm def start_link(_opts) do :gen_fsm.start_link({:local, FSM}, __MODULE__, [], []) end def next_state() do :gen_fsm.send_event(FSM, :round) end def init(_), do: IO.puts "FSM started"; { :ok, :ping, [] } def ping(:round, state), do: IO.puts "Ping!"; {:next_state, :pong, state} def pong(:round, state), do: IO.puts "Pong!"; {:next_state, :ping, state} end FiniteStateMachine.start_link [] # "FSM started" FiniteStateMachine.next_state # "Ping!" FiniteStateMachine.next_state # "Pong!" FiniteStateMachine.next_state # "Ping!" FiniteStateMachine.next_state # "Pong!" Elixir Finite state machine
  • 22. Elixir OTP Super-Supervisor Supervisor Supervisor Supervisor Worker Worker Worker Worker Worker Worker WorkerHelper Helper Application
  • 23. defmodule ExampleApp do use Application def start(_type, _args) do MainSupervisor.start end end Elixir Elixir application project Build tool mix: > mix new exampleapp mix.exs # Mix project definition file /config/config.exs # Project specific configuration /lib/ # Source code /test/ # Tests suites
  • 24. # Starts the BEAM Erlang Virtual Machine # and the EPMD Erlang Port Mapper Daemon (default port 4369) cluster01> elixir --sname node --no-halt --cookie secret_cookie Elixir Distributed computing: Nodes # Elixir master node (on same subnet as cluster nodes) master> iex --sname masternode --cookie secret_cookie Node.connect :"node@cluster01" # Establish node connection pid = Node.spawn :"node@cluster01", func # Spawn a process Node.self # Name of current node Node.list # List all connected nodes