Finite State Machines - Why the fear?

2,396
-1

Published on

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.

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,396
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
30
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • 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?

    1. 1. { Erlang and gen_fsm @dieswaytoofast (V.P. Ubiquiti Networks)
    2. 2. The Business
    3. 3. The Metrics
    4. 4.  Phone Calls per Second The Metrics
    5. 5.  Phone Calls per Second The Metrics x 1000
    6. 6.  Simultaneous Phone Calls The Metrics
    7. 7.  Simultaneous Phone Calls The Metrics x 10,000
    8. 8.  HTTP API requests The Metrics
    9. 9.  HTTP API requests The Metrics x ∞
    10. 10.  Self-hosted (kinda)
    11. 11.  Voice Response Call Handling init Setup Call Get Caller Name Play Transfer Message Got Caller Name Dial
    12. 12.  Call Routing Call Handling dial_in Check Blacklist get_endpoint conference Play Error Hangupauto_attendant dial_in_fml conference
    13. 13. init Check node Check Blacklist Cancelled Blacklisted Next Endpoint Collections Unknown Move node Auto Attendant FML Voicemail FaxConference
    14. 14.  Self-hosted (kinda) Bet the farm moment
    15. 15. ERLANG!!!
    16. 16.  Concurrency The Big Six From http://www.erlang.org/download/armstrong_thesis_2003.pdf
    17. 17.  Concurrency  Error encapsulation The Big Six From http://www.erlang.org/download/armstrong_thesis_2003.pdf
    18. 18.  Concurrency  Error encapsulation  Fault detection The Big Six From http://www.erlang.org/download/armstrong_thesis_2003.pdf
    19. 19.  Concurrency  Error encapsulation  Fault detection  Fault identification The Big Six From http://www.erlang.org/download/armstrong_thesis_2003.pdf
    20. 20.  Concurrency  Error encapsulation  Fault detection  Fault identification  Code upgrade The Big Six From http://www.erlang.org/download/armstrong_thesis_2003.pdf
    21. 21.  Concurrency  Error encapsulation  Fault detection  Fault identification  Code upgrade  Stable Storage The Big Six From http://www.erlang.org/download/armstrong_thesis_2003.pdf
    22. 22. Erlang Concurrency Hell My Blue Heaven My Blue Heaven
    23. 23. Erlang Concurrency Hell My Blue Heaven Deep Problems My Blue Heaven Deep Problems
    24. 24.  gen_server Behaviours
    25. 25.  gen_server  gen_tcp Behaviours
    26. 26.  gen_server  gen_tcp  gen_event Behaviours
    27. 27.  gen_server  gen_tcp  gen_event  gen_fsm Behaviours
    28. 28.  gen_server  gen_tcp  gen_event  gen_fsm  … Behaviours
    29. 29.  gen_server  gen_tcp  gen_event  gen_fsm  … Behaviours
    30. 30.  A System has States Finite State Machines
    31. 31. asleepStart out of bed Coffee!!
    32. 32.  A System has States  States transition Finite State Machines
    33. 33. asleepStart out of bed Coffee!!
    34. 34.  A System has States  States transition  Transitions are Atomic Finite State Machines
    35. 35. asleepStart out of bed Coffee!! NOTHING HERE
    36. 36.  A System has States  States transition  Transitions are Atomic  Events at Entry and Exit Finite State Machines
    37. 37. asleepStart out of bed Riiiiiiiiing Brush Teeth Coffee!!
    38. 38. asleepStart out of bed Coffee!!
    39. 39. asleepStart out of bed Riiiiiiiiing Brush Teeth Coffee!!
    40. 40. asleepStart out of bed Riiiiiiiiing Brush Teeth Coffee!!
    41. 41. asleepStart out of bed Coffee!!
    42. 42. asleepStart out of bed Riiiiiiiiing Brush Teeth Coffee!! HUH?
    43. 43. asleepStart out of bed Riiiiiiiiing Brush Teeth Coffee!!
    44. 44. asleepStart out of bed Riiiiiiiiing Brush Teeth Coffee!! W 00t!
    45. 45. Un- Auth Start Token Req. Stop
    46. 46. Un- Auth Start Token Req. Stop Get Request Token Get Access Token Request Token Access Token
    47. 47. Un- Auth Start Token Req. Stop Get Request Token Get Access Token Request Token Access Token
    48. 48. Un- Auth Start Token Req. Stop Get Request Token Get Access Token Request Token Access Token
    49. 49. Un- Auth Start Token Req. Stop Get Request Token Get Access Token Request Token Access Token
    50. 50. Un- Auth Start Token Req. Stop Get Request Token Get Access Token Request Token Access Token
    51. 51. Un- Auth Start Token Req. Stop Get Request Token Get Access Token Request Token Access Token
    52. 52. Un- Auth Start Token Req. Stop Get Request Token Get Access Token Request Token Access Token
    53. 53. Un- Auth Start Token Req. Stop Get Request Token Get Access Token Request Token Access Token
    54. 54.  Everything is an FSM Finite State Machines
    55. 55.  Everything is an FSM Finite State Machines COM PLEXITY?
    56. 56.  Everything is an FSM Finite State Machines ENCAPSULATION!
    57. 57.  Voice Response Finite State Machines init Setup Call Get Caller Name Play Transfer Message Got Caller Name Dial
    58. 58.  Voice Response Finite State Machines init Setup Call Get Caller Name Play Transfer Message Got Caller Name Dial
    59. 59.  Voice Response Finite State Machines init Setup Call Get Caller Name Play Transfer Message Got Caller Name Dial ENCAPSULATION!
    60. 60. Encapsulation Concurrency Hell My Blue Heaven Deep Problems My Blue Heaven Deep Problems
    61. 61.  Call Routing Finite State Machines dial_in Check Blacklist get_endpoint conference Play Error Hangupauto_attendant dial_in_fml conference
    62. 62. init Check node Check Blacklist Cancelled Blacklisted Next Endpoint Collections Unknown Move node Auto Attendant FML Voicemail FaxConference
    63. 63.  Templates are your friend  So are BIG sheets of paper  Think “Real World” The Bottom Line
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×