Top Banner
OTP Anton Fagerberg [email protected]
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),

May 20, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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),

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),

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),

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),

ActorConcurrency primitiveEach actor is a processMessage passing (only interaction)No shared information (memory) with other actors

Page 6: 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),

http://www.brianstorti.com/the-actor-model/

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

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),

The Little Elixir & OTP Guidebook

Benjamin Tan Wei Hao