Finite State Machines - Why the fear?
Upcoming SlideShare
Loading in...5
×
 

Finite State Machines - Why the fear?

on

  • 2,690 views

Finite State Machines are overlooked at best, ignored at worst, and virtually always dismissed. This is tragic since FSMs are not just about Door Locks (the most commonly used example). On the ...

Finite State Machines are overlooked at best, ignored at worst, and virtually always dismissed. This is tragic since FSMs are not just about Door Locks (the most commonly used example). On the contrary, these FSMs are invaluable in clearly defining communication protocols – ranging from low-level web-services through complex telephony application to reliable interactions between loosely-coupled systems. Properly using them can significantly enhance the stability and reliability of your systems.

Join me as I take you through a crash-course in FSMs, using erlang’s gen_fsm behavior as the background, and hopefully leaving you with a better appreciation of both FSM and erlang in the process.

Statistics

Views

Total Views
2,690
Views on SlideShare
2,576
Embed Views
114

Actions

Likes
2
Downloads
20
Comments
0

3 Embeds 114

https://twitter.com 111
http://www.linkedin.com 2
https://twimg0-a.akamaihd.net 1

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • So, I started a phone company. It was a great thing. Phones make people happy, right?
  • Yeah. Kids. Don’t do this at home. Don’t start a phone company.
  • But I did. Hosted phone services for SMBs. Vonage for small businesses. In the cloud. Awesome
  • 3 orders of magnitude
  • 4 orders of magnitude
  • Infinity!
  • Houston – we have a problem
  • Consider the simplest case of handling a call
  • It actually gets a lot more complex
  • And eventually, a *lot* more complex
  • To put it differently,It was a “bet the farm” moment
  • And it basically involved a lot of beer
  • 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.
  • The Six Essential Characteristics of a Fault Tolerant System
  • The Six Essential Characteristics of a Fault Tolerant System
  • The Six Essential Characteristics of a Fault Tolerant System
  • The Six Essential Characteristics of a Fault Tolerant System
  • The Six Essential Characteristics of a Fault Tolerant System
  • The Six Essential Characteristics of a Fault Tolerant System
  • ‘Distributed’ problems mean you spend a huge chunk of your time dealing with theadminstrivia of distribution.With erlang you get that for free!
  • 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!
  • Implement the server part of a client/server relationship
  • Talk to tcp over sockets
  • Event handling
  • Finite state machines
  • And a bunch more (gen_udp, gen_utp!, etc.)
  • But, we’re talking about Finite State Machines
  • Lets talk about FSMs
  • Putting this into practice, here is some code
  • A system has states
  • When certain events show up
  • You do something
  • And transition to the next state
  • Wait, WHUT? Where did the “events” come from?
  • I’m glad you asked  more code
  • And thusly an event is sent
  • Voila, an ErlangFSM
  • 2.0 is anightmare; Stick w/ 1.0
  • A system has states
  • Events and transitions
  • The code
  • The states
  • When you receive an event
  • try to do something
  • If it works go to the next state
  • If it fails, go to a differen state
  • Thusly
  • And now for the events
  • But wait! What if you walk out partway?
  • So, a wee bit triclyhere -
  • We have a timeout
  • And it tells you where to go after the timeout
  • And, you’re done.
  • Lets look at this as a ‘real world” thingy
  • But, you say, BUT, what about the complexity of dealing with FSMs? They’re big! And unweildy!
  • Nah. They aren’t. Not in Erlang
  • Remember this? It was a PITA? Actually, a small part of a PITA?
  • But each one is tightly encapsulated, and clean.It’s the Erlang Way
  • But each one is tightly encapsulated, and clean.It’s the Erlang Way
  • My fanciful slides are actually real. This actually happens.
  • And it’s the same for everything else I showed. FSMs of FSMs.
  • An FSM of FSMs of FSMsBuilding these out “correctly”, in real time, is (was!) trivial
  • “Real World”  Business Rules can help. (VM,syncrhonziation)

Finite State Machines - Why the fear? Finite State Machines - Why the fear? Presentation Transcript

  • { 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