OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries...
Transcript of OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries...
![Page 2: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/2.jpg)
What is OTP?
OTP as a complete development environment for concurrentprogramming.
Benjamin Tan Wei Hao. “The Little Elixir & OTP Guidebook.”
![Page 3: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/3.jpg)
OTP isThe Erlang interpreter and compilerErlang standard librariesDialyzer, a static analysis toolMnesia, a distributed databaseErlang Term Storage (ETS), an in-memory databaseA debuggerAn event tracerA release-management tool
Benjamin Tan Wei Hao. “The Little Elixir & OTP Guidebook.”
![Page 4: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/4.jpg)
OTP BehaviorsDesign patterns for actorsProvide generic pieces
![Page 5: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/5.jpg)
ActorConcurrency primitiveEach actor is a processMessage passing (only interaction)No shared information (memory) with other actors
![Page 7: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/7.jpg)
What actors do
When an actor receives a message, it can do one of these 3 things:
1. Create more actors.2. Send messages to other actors.3. Designates what to do with the next message.
http://www.brianstorti.com/the-actor-model/
![Page 8: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/8.jpg)
Detour into Object Orientation
http://www.codercaste.com/2011/01/12/what-is-object-oriented-programming-and-why-you-need-to-use-it/
![Page 9: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/9.jpg)
Alan Kay
Coined the term "object orientation"
He is best known for his pioneering work on object-orientedprogramming and windowing graphical user interface design.
![Page 10: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/10.jpg)
The Early History of Smalltalk
March 1993
http://stephane.ducasse.free.fr/FreeBooks/SmalltalkHistoryHOPL.pdf
![Page 11: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/11.jpg)
The Early History of Smalltalk
![Page 12: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/12.jpg)
The first three principles are what objects "are about"
1. Everything is an object2. Objects communicate by sending and receiving messages3. Objects have their own memory
![Page 13: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/13.jpg)
Back to OTP
![Page 14: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/14.jpg)
OTP BehaviorsGenServer
Implementing the server of a client-server relationshipSupervisor
Implementing supervision functionalityApplication
Working with applications and defining applicationcallbacks
Elixir is creating more - and you can implement your own!
![Page 15: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/15.jpg)
GenServer (Generic Server)Abstraction of client / server functionality.
![Page 16: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/16.jpg)
GenServer
Provides
Start (spawn) server processMaintain state in serverHandle requests, send responsesStopping server processNaming conventionsHandle unexpected messagesConsistent structure
![Page 17: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/17.jpg)
GenServer
Leaves you to define
State to initializeWhat messages to handle (requests)When to reply (async / sync)What messages to reply withWhat resources to clean-up on termination
![Page 18: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/18.jpg)
def loop(results \\ [], results_expected) do receive do {:ok, result} -> new_results = [result|results] loop(new_results, results_expected) _ -> loop(results, results_expected) end end
Benjamin Tan Wei Hao. “The Little Elixir & OTP Guidebook.”
![Page 19: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/19.jpg)
def handle_call({:location, location}, _from, state) do new_state = update_stats(stats, location) {:reply, "hello!", new_state} end
![Page 20: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/20.jpg)
Sequential programsTypically one main processProgram defensively try & catch if err != nil
![Page 21: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/21.jpg)
Let it crash!
![Page 22: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/22.jpg)
LinkActors can link themselves to other actors(or monitor them)
![Page 23: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/23.jpg)
http://learnyousomeerlang.com/errors-and-processes
![Page 24: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/24.jpg)
SupervisorsObserve other processesTake action when things breakGenServer makes it easy to be supervised
![Page 25: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/25.jpg)
Benjamin Tan Wei Hao. “The Little Elixir & OTP Guidebook.”
![Page 26: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/26.jpg)
Let it crashDelegate error detection and handling to other actorsDo not code defensively
![Page 27: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/27.jpg)
Restart stratergiesone for one
If a process dies, only that process is restarted.one for all
All process in the supervision tree dies with it.rest for one
Processes started after the failing process are terminated.simple one for one
Factor method, many instances of same process.
Also
max restartsmax seconds
![Page 28: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/28.jpg)
Pooly
Benjamin Tan Wei Hao. “The Little Elixir & OTP Guidebook.”
![Page 29: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/29.jpg)
Observer
https://tkowal.wordpress.com/2016/04/23/observer-in-erlangelixir-release/
![Page 30: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/30.jpg)
One of the killer features of the Erlang VM is distribution—thatis, the ability to have multiple Erlang runtimes talking to eachother. Sure, you can probably do it in other languages andplatforms, but most will cause you to lose faith in computersand humanity in general, just because they weren’t built withdistribution in mind.
Benjamin Tan Wei Hao. “The Little Elixir & OTP Guidebook.”
![Page 31: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/31.jpg)
Location transparent clusters!
![Page 32: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/32.jpg)
Node connections are transitive
Benjamin Tan Wei Hao. “The Little Elixir & OTP Guidebook.”
![Page 33: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/33.jpg)
Distribution & fault toleranceFailover - node crashes, another node takes over applicationTakeover - higher priority node takes over application
![Page 34: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/34.jpg)
Chuck Norris
Benjamin Tan Wei Hao. “The Little Elixir & OTP Guidebook.”
![Page 35: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/35.jpg)
GenStageGenStage is a new Elixir behaviour for exchanging events withback-pressure between Elixir processes.
http://elixir-lang.org/blog/2016/07/14/announcing-genstage/
![Page 36: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/36.jpg)
GenStageNot only that, we want to provide developers interested inmanipulating collections with a path to take their code fromeager to lazy, to concurrent and then distributed.
http://elixir-lang.org/blog/2016/07/14/announcing-genstage/
![Page 37: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/37.jpg)
Typespublic void onReceive(Object message) throws Exception { if (message instanceof String) { getSender().tell(message, getSelf()); } else { unhandled(message); } }
![Page 38: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/38.jpg)
Akka Typedhttp://doc.akka.io/docs/akka/current/scala/typed.html
![Page 39: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/39.jpg)
Success typing (Dialyzer)defmodule Cashy.Bug1 do
def convert(:sgd, :usd, amount) do {:ok, amount * 0.70} end
def run do convert(:sgd, :usd, :one_million_dollars) end
end
Benjamin Tan Wei Hao. “The Little Elixir & OTP Guidebook.”
![Page 40: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/40.jpg)
Success typing (Dialyzer)@spec convert(currency, currency, number) :: number def convert(:sgd, :usd, amount) do amount * 0.70 end
Benjamin Tan Wei Hao. “The Little Elixir & OTP Guidebook.”
![Page 41: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/41.jpg)
QuickCheck & Concuerror
![Page 42: OTP - Anton Fagerberg · OTP is The Erlang interpreter and compiler Erlang standard libraries Dialyzer, a static analysis tool Mnesia, a distributed database Erlang Term Storage (ETS),](https://reader030.fdocuments.net/reader030/viewer/2022040608/5ec57884306dd8516847d8a8/html5/thumbnails/42.jpg)
The Little Elixir & OTP Guidebook
Benjamin Tan Wei Hao