Slide 1

Slide 1 text

The Tony Arcieri RubyConf November 2nd, 2012 Ecosystem

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

We’re hiring!

Slide 4

Slide 4 text

“Threads on Rails!”

Slide 5

Slide 5 text

Why?

Slide 6

Slide 6 text

Multicore

Slide 7

Slide 7 text

Going Faster (The Old Way) •Clock speed grows exponentially •Faster clock speed = faster program! •Threads are expensive!

Slide 8

Slide 8 text

Going Faster (The New Way) •POWER WALL! Clock speed is “stuck” •Number of cores grows exponentially •Threads are increasingly cheap

Slide 9

Slide 9 text

Multicore Is The Future

Slide 10

Slide 10 text

“I’ll just throw multiple VMs at it!”

Slide 11

Slide 11 text

Wastes RAM

Slide 12

Slide 12 text

Serialization Penalty http://aphyr.com/posts/244-context-switches-and-serialization-in-node

Slide 13

Slide 13 text

When we have 100 core CPUs...

Slide 14

Slide 14 text

Will we run 100 VMs? Or one?

Slide 15

Slide 15 text

Open Source Voice Application Framework Projects Using Celluloid

Slide 16

Slide 16 text

OOP + Actor Model

Slide 17

Slide 17 text

“I thought of objects being like biological cells and/or individual computers on a network, only able to communicate with messages” - Alan Kay, creator of Smalltalk, on the meaning of "object oriented programming"

Slide 18

Slide 18 text

OOP Tools Classes Inheritance Messages

Slide 19

Slide 19 text

Concurrency Tools Threads Locks Queues

Slide 20

Slide 20 text

OOP Tools Classes Inheritance Messages Concurrency Tools Threads Locks Queues +

Slide 21

Slide 21 text

Active Objects Based on the Actor Model

Slide 22

Slide 22 text

“Cells”

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Actor Model •Actors are computational entities that can receive messages •Each actor has a unique address •If you know an actor’s address, you can send it messages •Actors can create new actors

Slide 26

Slide 26 text

I’m not the first to do Actor Model + OOP

Slide 27

Slide 27 text

Pythons did it!

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

1997

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

Forgotten approach to concurrency?

Slide 32

Slide 32 text

Example

Slide 33

Slide 33 text

require 'celluloid' class Launcher include Celluloid def launch 3.downto(1) do |count| puts "#{count}..." sleep 1 end puts "BLASTOFF!" end end

Slide 34

Slide 34 text

require 'celluloid' class Launcher include Celluloid def launch 3.downto(1) do |count| puts "#{count}..." sleep 1 end puts "BLASTOFF!" end end

Slide 35

Slide 35 text

>> launcher = Launcher.new => #

Slide 36

Slide 36 text

Synchronous Calls

Slide 37

Slide 37 text

>> launcher = Launcher.new => # >> launcher.launch

Slide 38

Slide 38 text

>> launcher = Launcher.new => # >> launcher.launch 3...

Slide 39

Slide 39 text

>> launcher = Launcher.new => # >> launcher.launch 3... 2...

Slide 40

Slide 40 text

>> launcher = Launcher.new => # >> launcher.launch 3... 2... 1...

Slide 41

Slide 41 text

>> launcher = Launcher.new => # >> launcher.launch 3... 2... 1... BLASTOFF! => nil >>

Slide 42

Slide 42 text

&DOOHU &HOOXORLG $FWRU3UR[\ &$// 5HFHLYHU &HOOXORLG 0DLOER[ 5(63216( &HOOXORLG 0DLOER[ &HOOXORLG$FWRU &HOOXORLG&DOO &HOOXORLG5HVSRQVH Synchronous Calls

Slide 43

Slide 43 text

Asynchronous Calls

Slide 44

Slide 44 text

>> launcher = Launcher.new => # >> launcher.async.launch

Slide 45

Slide 45 text

>> launcher = Launcher.new => # >> launcher.async.launch New syntax!

Slide 46

Slide 46 text

THEY TOOK ‘ER BANG METHERDS!!!!!

Slide 47

Slide 47 text

You’ll get them back in Celluloid 1.0

Slide 48

Slide 48 text

>> launcher = Launcher.new => # >> launcher.async.launch

Slide 49

Slide 49 text

>> launcher = Launcher.new => # >> launcher.async.launch => nil >> Returns immediately

Slide 50

Slide 50 text

>> launcher = Launcher.new => # >> launcher.async.launch => nil >> 3...

Slide 51

Slide 51 text

>> launcher = Launcher.new => # >> launcher.async.launch => nil >> 3... 2...

Slide 52

Slide 52 text

>> launcher = Launcher.new => # >> launcher.async.launch => nil >> 3... 2... 1...

Slide 53

Slide 53 text

>> launcher = Launcher.new => # >> launcher.async.launch => nil >> 3... 2... 1... BLASTOFF!

Slide 54

Slide 54 text

Asynchronous Calls &DOOHU &HOOXORLG $V\QF3UR[\ &$// 5HFHLYHU &HOOXORLG 0DLOER[ &HOOXORLG$FWRU &HOOXORLG&DOO

Slide 55

Slide 55 text

>> launcher1, launcher2 = Launcher.new, Launcher.new => [#, #] Easy Parallelism

Slide 56

Slide 56 text

>> launcher1, launcher2 = Launcher.new, Launcher.new => [#, #] >> launcher1.async.launch; launcher2.async.launch => nil >> Easy Parallelism

Slide 57

Slide 57 text

>> launcher1, launcher2 = Launcher.new, Launcher.new => [#, #] >> launcher1.async.launch; launcher2.async.launch => nil >> 3... 3... Easy Parallelism

Slide 58

Slide 58 text

>> launcher1, launcher2 = Launcher.new, Launcher.new => [#, #] >> launcher1.async.launch; launcher2.async.launch => nil >> 3... 3... 2... 2... Easy Parallelism

Slide 59

Slide 59 text

>> launcher1, launcher2 = Launcher.new, Launcher.new => [#, #] >> launcher1.async.launch; launcher2.async.launch => nil >> 3... 3... 2... 2... 1... 1... Easy Parallelism

Slide 60

Slide 60 text

>> launcher1, launcher2 = Launcher.new, Launcher.new => [#, #] >> launcher1.async.launch; launcher2.async.launch => nil >> 3... 3... 2... 2... 1... 1... BLASTOFF!!! BLASTOFF!!! Easy Parallelism

Slide 61

Slide 61 text

Futures

Slide 62

Slide 62 text

class FibonacciWorker include Celluloid def fib(n) n < 2 ? n : fib(n-1) + fib(n-2) end end

Slide 63

Slide 63 text

>> worker = FibonacciWorker.new => # >> future = worker.future.fib(40) => #

Slide 64

Slide 64 text

>> worker = FibonacciWorker.new => # >> future = worker.future.fib(40) => # Returns immediately

Slide 65

Slide 65 text

>> worker = FibonacciWorker.new => # >> future = worker.future.fib(40) => # >> future.value

Slide 66

Slide 66 text

>> worker = FibonacciWorker.new => # >> future = worker.future.fib(40) => # >> future.value Blocks until complete

Slide 67

Slide 67 text

>> worker = FibonacciWorker.new => # >> future = worker.future.fib(40) => # >> future.value => 102334155

Slide 68

Slide 68 text

&DOOHU &HOOXORLG $FWRU3UR[\ 5HFHLYHU &HOOXORLG 0DLOER[ &HOOXORLG$FWRU &HOOXORLG&DOO &$// &HOOXORLG 0DLOER[ )8785( &HOOXORLG )XWXUH &HOOXORLG5HVSRQVH 9$/8(" 9$/8( Futures

Slide 69

Slide 69 text

Pools

Slide 70

Slide 70 text

>> pool = FibonacciWorker.pool(size: 16) => #

Slide 71

Slide 71 text

>> pool = FibonacciWorker.pool => # 1 actor per CPU (by default)

Slide 72

Slide 72 text

>> pool = FibonacciWorker.pool => # >> (32...40).map { |n| pool.future.fib(n) }.map(&:value)

Slide 73

Slide 73 text

>> pool = FibonacciWorker.pool => # >> (32...40).map { |n| pool.future.fib(n) }.map(&:value) => [2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986]

Slide 74

Slide 74 text

What if an actor crashes?

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

Fault Tolerance •Supervisors & Supervision Trees •“Let it crash!” •Restart in a clean state

Slide 78

Slide 78 text

Ecosystem

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

Distributed Celluloid Evented I/O for Celluloid Celluloid::IO-based web server

Slide 81

Slide 81 text

Distributed Celluloid

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

Bleeding edge :( 0.12.0 prerelease

Slide 84

Slide 84 text

Cells are services

Slide 85

Slide 85 text

DCell exposes them to the network

Slide 86

Slide 86 text

Built on 0MQ •Fast, brokerless message queue •DCell uses push/pull sockets •Built on Celluloid::ZMQ/ffi-rzmq

Slide 87

Slide 87 text

•Distributed process orchestration (e.g. Chef, Puppet) •Multi-tier Web Applications •Asynchronous background jobs Use Cases •Distributed process orchestration (e.g. Chef, Puppet) •Multi-tier Web Applications / SOA •Asynchronous background jobs

Slide 88

Slide 88 text

Why do this? )URQW(QG )URQW(QG )URQW(QG 5(67 &OLHQW 5(67 &OLHQW 5(67 &OLHQW 5(67 6HUYLFH 5(67 6HUYLFH 5(67 6HUYLFH 'RPDLQ 2EMHFW 'RPDLQ 2EMHFW 'RPDLQ 2EMHFW 'RPDLQ 2EMHFW 'RPDLQ 2EMHFW 'RPDLQ 2EMHFW 'RPDLQ 2EMHFW 'RPDLQ 2EMHFW 'RPDLQ 2EMHFW

Slide 89

Slide 89 text

Instead of this? )URQW(QG )URQW(QG )URQW(QG 'RPDLQ 2EMHFW 'RPDLQ 2EMHFW 'RPDLQ 2EMHFW 'RPDLQ 2EMHFW 'RPDLQ 2EMHFW 'RPDLQ 2EMHFW 'RPDLQ 2EMHFW 'RPDLQ 2EMHFW 'RPDLQ 2EMHFW

Slide 90

Slide 90 text

"Objects can message objects transparently that live on other machines over the network, and you don't have to worry about the networking gunk, and you don't have to worry about finding them, and you don't have to worry about anything. It's just as if you messaged an object that's right next door." --Steve Jobs describing NeXT Portable Distributed Objects

Slide 91

Slide 91 text

Distributed objects have mostly been a failure

Slide 92

Slide 92 text

Distributed Object Failures •CORBA •SOAP •PDO •RMI •DRB

Slide 93

Slide 93 text

Why?

Slide 94

Slide 94 text

Not asynchronous Huge problem in distributed systems

Slide 95

Slide 95 text

Not built on the Actor Model

Slide 96

Slide 96 text

Actor Model is AWESOME!!!

Slide 97

Slide 97 text

Unifying abstraction for both concurrency and distribution

Slide 98

Slide 98 text

Success!

Slide 99

Slide 99 text

Example

Slide 100

Slide 100 text

You need Zookeeper :(

Slide 101

Slide 101 text

What is Zookeeper?

Slide 102

Slide 102 text

Zookeeper provides a total ordering of events in a distributed system

Slide 103

Slide 103 text

Zookeeper = Transactions

Slide 104

Slide 104 text

What is Zookeeper good for? •Node registry •Global data registry •Locks •Leader election

Slide 105

Slide 105 text

zk gem •Used by the DCell Zookeeper backend •High level API to Zookeeper •Abstract leader election API https://github.com/slyphon/zk

Slide 106

Slide 106 text

It’s not hard! :D

Slide 107

Slide 107 text

Installing Zookeeper • git clone https://github.com/celluloid/dcell.git • cd dcell • rake zookeeper:install • rake zookeeper:start

Slide 108

Slide 108 text

$ rake zookeeper:start *** Starting Zookeeper cd zookeeper && bin/zkServer.sh start JMX enabled by default Using config: bin/../conf/zoo.cfg Starting zookeeper ... STARTED

Slide 109

Slide 109 text

Two Node Cluster •Node #1 is “itchy” •Node #2 is “scratchy”

Slide 110

Slide 110 text

“itchy” commands >> require 'dcell' => true >> DCell.start :id => 'itchy', :addr => 'tcp:// 127.0.0.1:7777' I, [2012-05-09T10:20:46.999000 #52416] INFO -- : Connected to itchy => # Note itchy is on port 7777

Slide 111

Slide 111 text

“scratchy” commands >> require 'dcell' => true >> DCell.start :id => 'scratchy', :addr => 'tcp:// 127.0.0.1:7778' I, [2012-05-09T10:26:42.322000 #52555] INFO -- : Connected to itchy I, [2012-05-09T10:26:42.331000 #52555] INFO -- : Connected to scratchy => # Note scratchy is on port 7778

Slide 112

Slide 112 text

Finding nodes •DCell::Node[‘itchy’] •DCell::Node.all •DCell.me

Slide 113

Slide 113 text

Finding Remote Cells DCell::Node[‘itchy’][:info]

Slide 114

Slide 114 text

>> info_service = DCell::Node['itchy'][:info] => # >> info_service.uptime => 42 >> info_service.load_averages => [1.08, 0.93, 0.84] >> info_service.distribution => "Mac OS X 10.7.3 (11D50b)" “scratchy” commands

Slide 115

Slide 115 text

Defining DCell services

Slide 116

Slide 116 text

require 'celluloid' class Greeter include Celluloid def hello "Hi" end end Example Cell

Slide 117

Slide 117 text

>> Celluloid::Actor[:greeter] = Greeter.new => # >> Celluloid::Actor[:greeter].hello => "Hi" Naming Cells

Slide 118

Slide 118 text

>> Greeter.supervise_as :greeter => # >> Celluloid::Actor[:greeter].hello => "Hi" Naming Cells

Slide 119

Slide 119 text

>> node = DCell::Node['itchy'] => # >> node[:greeter].hello => "Hi" Calling Cells

Slide 120

Slide 120 text

All Celluloid features supported •Synchronous calls •Asynchronous calls •Futures

Slide 121

Slide 121 text

Evented I/O for Celluloid http://github.com/celluloid/celluloid-io

Slide 122

Slide 122 text

Now that we have threads licked... How about I/O?

Slide 123

Slide 123 text

USE BLOCKING I/O

Slide 124

Slide 124 text

Blocking IO is OK!* No central event loop to block

Slide 125

Slide 125 text

*But be careful Locks in external services = Deadlocks in Celluloid

Slide 126

Slide 126 text

Evented IO •Large numbers of connections (>10,000) •Mostly idle connections •Mostly IO-bound problems •Websockets are an ideal case

Slide 127

Slide 127 text

Actors are event loops

Slide 128

Slide 128 text

&HOOXORLG $FWRU &HOOXORLG 0DLOER[ &RQGLWLRQ 9DULDEOH Normal Actors

Slide 129

Slide 129 text

Celluloid::IO Actors &HOOXORLG $FWRU &HOOXORLG ,20DLOER[ &HOOXORLG ,25HDFWRU

Slide 130

Slide 130 text

nio4r-powered reactor

Slide 131

Slide 131 text

nio4r •Quasi-inspired by Java NIO •Smallest API possible •libev C extension for CRuby/rbx •Java extension for JRuby •Pure Ruby version too! http://github.com/tarcieri/nio4r/

Slide 132

Slide 132 text

Celluloid::IO::TCPSocket •Uses fibered I/O •“Duck-type” of ::TCPSocket •Evented inside Celluloid::IO actors •Blocking IO elsewhere (Ruby Threads, normal Celluloid actors)

Slide 133

Slide 133 text

Evented IO AND Threaded IO You don’t have to choose!

Slide 134

Slide 134 text

class EchoServer include Celluloid::IO def initialize(host, port) puts "*** Starting echo server on #{host}:#{port}" # Since we included Celluloid::IO, we're actually making a # Celluloid::IO::TCPServer here @server = TCPServer.new(host, port) run! end def finalize @server.close if @server end def run loop { handle_connection! @server.accept } end def handle_connection(socket) _, port, host = socket.peeraddr puts "*** Received connection from #{host}:#{port}" loop { socket.write socket.readpartial(4096) } rescue EOFError puts "*** #{host}:#{port} disconnected" socket.close end end supervisor = EchoServer.supervise("127.0.0.1", 1234) trap("INT") { supervisor.terminate; exit } sleep

Slide 135

Slide 135 text

Celluloid::IO-powered web server http://github.com/celluloid/reel

Slide 136

Slide 136 text

Hello World Benchmark # httperf --num-conns=50 --num-calls=1000 Ruby Version Throughput Latency ------------ ---------- ------- JRuby HEAD 5650 reqs/s (0.2 ms/req) Ruby 1.9.3 5263 reqs/s (0.2 ms/req) JRuby 1.6.7 4303 reqs/s (0.2 ms/req) rbx HEAD 2288 reqs/s (0.4 ms/req)

Slide 137

Slide 137 text

Hello World Comparison Web Server Throughput Latency ---------- ---------- ------- Goliath (0.9.4) 2058 reqs/s (0.5 ms/req) Thin (1.2.11) 7502 reqs/s (0.1 ms/req) Node.js (0.6.5) 11735 reqs/s (0.1 ms/req) Ruby servers on 1.9.3

Slide 138

Slide 138 text

It’s got Websockets!

Slide 139

Slide 139 text

Celluloid-powered Web Framework http://github.com/celluloid/lattice

Slide 140

Slide 140 text

Vaporware!

Slide 141

Slide 141 text

Goals •Built out of parts of Rails •Webmachine for resources •Multithreaded development mode •Easy scatter/gather for SOA

Slide 142

Slide 142 text

That’s all, folks!

Slide 143

Slide 143 text

Twitter: @bascule Celluloid: celluloid.io Blog: unlimitednovelty.com