{
Erlang and gen_fsm
@dieswaytoofast
(V.P. Ubiquiti Networks)
The Business
The Metrics
 Phone Calls per Second
The Metrics
 Phone Calls per Second
The Metrics
x 1000
 Simultaneous Phone Calls
The Metrics
 Simultaneous Phone Calls
The Metrics
x 10,000
 HTTP API requests
The Metrics
 HTTP API requests
The Metrics
x ∞
 Self-hosted (kinda)
 Voice Response
Call Handling
init
Setup Call
Get Caller
Name
Play Transfer
Message
Got Caller
Name
Dial
 Call Routing
Call Handling
dial_in
Check
Blacklist
get_endpoint conference Play Error
Hangupauto_attendant
dial_in_fml conference
init
Check node
Check
Blacklist
Cancelled Blacklisted Next Endpoint Collections Unknown
Move node
Auto Attendant FML Voicemail FaxConference
 Self-hosted (kinda)
Bet the farm moment
ERLANG!!!
 Concurrency
The Big Six
From http://www.erlang.org/download/armstrong_thesis_2003.pdf
 Concurrency
 Error encapsulation
The Big Six
From http://www.erlang.org/download/armstrong_thesis_2003.pdf
 Concurrency
 Error encapsulation
 Fault detection
The Big Six
From http://www.erlang.org/download/armstrong_thesis_2003.pdf
 Concurrency
 Error encapsulation
 Fault detection
 Fault identification
The Big Six
From http://www.erlang.org/download/armstrong_thesis_2003.pdf
 Concurrency
 Error encapsulation
 Fault detection
 Fault identification
 Code upgrade
The Big Six
From http://www.erlang.org/download/armstrong_thesis_2003.pdf
 Concurrency
 Error encapsulation
 Fault detection
 Fault identification
 Code upgrade
 Stable Storage
The Big Six
From http://www.erlang.org/download/armstrong_thesis_2003.pdf
Erlang
Concurrency Hell
My Blue Heaven My Blue Heaven
Erlang
Concurrency Hell
My Blue Heaven
Deep Problems
My Blue Heaven
Deep Problems
 gen_server
Behaviours
 gen_server
 gen_tcp
Behaviours
 gen_server
 gen_tcp
 gen_event
Behaviours
 gen_server
 gen_tcp
 gen_event
 gen_fsm
Behaviours
 gen_server
 gen_tcp
 gen_event
 gen_fsm
 …
Behaviours
 gen_server
 gen_tcp
 gen_event
 gen_fsm
 …
Behaviours
 A System has States
Finite State Machines
asleepStart
out of
bed
Coffee!!
 A System has States
 States transition
Finite State Machines
asleepStart
out of
bed
Coffee!!
 A System has States
 States transition
 Transitions are Atomic
Finite State Machines
asleepStart
out of
bed
Coffee!!
NOTHING HERE
 A System has States
 States transition
 Transitions are Atomic
 Events at Entry and Exit
Finite State Machines
asleepStart
out of
bed
Riiiiiiiiing Brush Teeth
Coffee!!
asleepStart
out of
bed
Coffee!!
asleepStart
out of
bed
Riiiiiiiiing Brush Teeth
Coffee!!
asleepStart
out of
bed
Riiiiiiiiing Brush Teeth
Coffee!!
asleepStart
out of
bed
Coffee!!
asleepStart
out of
bed
Riiiiiiiiing Brush Teeth
Coffee!!
HUH?
asleepStart
out of
bed
Riiiiiiiiing Brush Teeth
Coffee!!
asleepStart
out of
bed
Riiiiiiiiing Brush Teeth
Coffee!!
W
00t!
Un-
Auth
Start
Token
Req.
Stop
Un-
Auth
Start
Token
Req.
Stop
Get
Request
Token
Get
Access
Token
Request
Token
Access
Token
Un-
Auth
Start
Token
Req.
Stop
Get
Request
Token
Get
Access
Token
Request
Token
Access
Token
Un-
Auth
Start
Token
Req.
Stop
Get
Request
Token
Get
Access
Token
Request
Token
Access
Token
Un-
Auth
Start
Token
Req.
Stop
Get
Request
Token
Get
Access
Token
Request
Token
Access
Token
Un-
Auth
Start
Token
Req.
Stop
Get
Request
Token
Get
Access
Token
Request
Token
Access
Token
Un-
Auth
Start
Token
Req.
Stop
Get
Request
Token
Get
Access
Token
Request
Token
Access
Token
Un-
Auth
Start
Token
Req.
Stop
Get
Request
Token
Get
Access
Token
Request
Token
Access
Token
Un-
Auth
Start
Token
Req.
Stop
Get
Request
Token
Get
Access
Token
Request
Token
Access
Token
 Everything is an FSM
Finite State Machines
 Everything is an FSM
Finite State Machines
COM
PLEXITY?
 Everything is an FSM
Finite State Machines
ENCAPSULATION!
 Voice Response
Finite State Machines
init
Setup Call
Get Caller
Name
Play Transfer
Message
Got Caller
Name
Dial
 Voice Response
Finite State Machines
init
Setup Call
Get Caller
Name
Play Transfer
Message
Got Caller
Name
Dial
 Voice Response
Finite State Machines
init
Setup Call
Get Caller
Name
Play Transfer
Message
Got Caller
Name
Dial
ENCAPSULATION!
Encapsulation
Concurrency Hell
My Blue Heaven
Deep Problems
My Blue Heaven
Deep Problems
 Call Routing
Finite State Machines
dial_in
Check
Blacklist
get_endpoint conference Play Error
Hangupauto_attendant
dial_in_fml conference
init
Check node
Check
Blacklist
Cancelled Blacklisted Next Endpoint Collections Unknown
Move node
Auto Attendant FML Voicemail FaxConference
 Templates are your friend
 So are BIG sheets of paper
 Think “Real World”
The Bottom Line
Finite State Machines - Why the fear?

Finite State Machines - Why the fear?

Editor's Notes

  • #3 So, I started a phone company. It was a great thing. Phones make people happy, right?
  • #4 Yeah. Kids. Don’t do this at home. Don’t start a phone company.
  • #5 But I did. Hosted phone services for SMBs. Vonage for small businesses. In the cloud. Awesome
  • #8 3 orders of magnitude
  • #10 4 orders of magnitude
  • #12 Infinity!
  • #13 Houston – we have a problem
  • #14 Consider the simplest case of handling a call
  • #15 It actually gets a lot more complex
  • #16 And eventually, a *lot* more complex
  • #17 To put it differently,It was a “bet the farm” moment
  • #18 And it basically involved a lot of beer
  • #19 And we rewrote the whole damn thing in Erlang. WhyErlang? Well, Fault Tolerance, basicallyAnd oh, I’m not putting Kirk up there. That’s too obvious.
  • #20 The Six Essential Characteristics of a Fault Tolerant System
  • #21 The Six Essential Characteristics of a Fault Tolerant System
  • #22 The Six Essential Characteristics of a Fault Tolerant System
  • #23 The Six Essential Characteristics of a Fault Tolerant System
  • #24 The Six Essential Characteristics of a Fault Tolerant System
  • #25 The Six Essential Characteristics of a Fault Tolerant System
  • #26 ‘Distributed’ problems mean you spend a huge chunk of your time dealing with theadminstrivia of distribution.With erlang you get that for free!
  • #27 Ok, not really true. You still have to deal with ‘deep problems’ (hard core parallelization issues, etc.)But you’d have to deal with that anyhow!
  • #28 Implement the server part of a client/server relationship
  • #29 Talk to tcp over sockets
  • #30 Event handling
  • #31 Finite state machines
  • #32 And a bunch more (gen_udp, gen_utp!, etc.)
  • #33 But, we’re talking about Finite State Machines
  • #34 Lets talk about FSMs
  • #42 Putting this into practice, here is some code
  • #43 A system has states
  • #44 When certain events show up
  • #45 You do something
  • #46 And transition to the next state
  • #47 Wait, WHUT? Where did the “events” come from?
  • #48 I’m glad you asked  more code
  • #49 And thusly an event is sent
  • #50 Voila, an ErlangFSM
  • #51 2.0 is anightmare; Stick w/ 1.0
  • #52 A system has states
  • #53 Events and transitions
  • #54 The code
  • #55 The states
  • #57 When you receive an event
  • #58 try to do something
  • #59 If it works go to the next state
  • #61 If it fails, go to a differen state
  • #62 Thusly
  • #63 And now for the events
  • #68 But wait! What if you walk out partway?
  • #69 So, a wee bit triclyhere -
  • #70 We have a timeout
  • #71 And it tells you where to go after the timeout
  • #72 And, you’re done.
  • #73 Lets look at this as a ‘real world” thingy
  • #74 But, you say, BUT, what about the complexity of dealing with FSMs? They’re big! And unweildy!
  • #75 Nah. They aren’t. Not in Erlang
  • #76 Remember this? It was a PITA? Actually, a small part of a PITA?
  • #77 But each one is tightly encapsulated, and clean.It’s the Erlang Way
  • #78 But each one is tightly encapsulated, and clean.It’s the Erlang Way
  • #79 My fanciful slides are actually real. This actually happens.
  • #80 And it’s the same for everything else I showed. FSMs of FSMs.
  • #81 An FSM of FSMs of FSMsBuilding these out “correctly”, in real time, is (was!) trivial
  • #82 “Real World”  Business Rules can help. (VM,syncrhonziation)