Slide 1

Slide 1 text

Deep Dive into Gearman Helgi Þormar Þorbjörnsson PHPTek, Chicago, 24th May, 2012

Slide 2

Slide 2 text

Helgi

Slide 3

Slide 3 text

Co-founded Orchestra.io Helgi

Slide 4

Slide 4 text

Co-founded Orchestra.io Work at EngineYard Helgi

Slide 5

Slide 5 text

Co-founded Orchestra.io Work at EngineYard PEAR Developer Helgi

Slide 6

Slide 6 text

Co-founded Orchestra.io Work at EngineYard PEAR Developer From Iceland Helgi

Slide 7

Slide 7 text

Co-founded Orchestra.io Work at EngineYard PEAR Developer From Iceland @h on Twitter Helgi

Slide 8

Slide 8 text

What is Gearman

Slide 9

Slide 9 text

Job Queue

Slide 10

Slide 10 text

Task Processing

Slide 11

Slide 11 text

Your Client Code Gearman Client API (C, PHP, Perl, MySQL UDF, ...) Gearman Job Server gearmand Gearman Worker API (C, PHP, Perl, Python, ...) Your Worker Code Your App Gearman

Slide 12

Slide 12 text

Good for... what?

Slide 13

Slide 13 text

Good for

Slide 14

Slide 14 text

Good for Processing (Images, text)

Slide 15

Slide 15 text

Good for Processing (Images, text) Logs (Web Server, etc)

Slide 16

Slide 16 text

Good for Processing (Images, text) Logs (Web Server, etc) Data Mining

Slide 17

Slide 17 text

Good for Processing (Images, text) Logs (Web Server, etc) Data Mining Mass Emails

Slide 18

Slide 18 text

Good for Processing (Images, text) Logs (Web Server, etc) Data Mining Mass Emails Intensive transformation

Slide 19

Slide 19 text

Good for Processing (Images, text) Logs (Web Server, etc) Data Mining Mass Emails Intensive transformation Search

Slide 20

Slide 20 text

Perception Helps with

Slide 21

Slide 21 text

Perception

Slide 22

Slide 22 text

Perception

Slide 23

Slide 23 text

Perception Defer intensive processes

Slide 24

Slide 24 text

Perception Defer intensive processes Give instant feedback

Slide 25

Slide 25 text

Perception Defer intensive processes Give instant feedback Users keep on browsing

Slide 26

Slide 26 text

Perception Defer intensive processes Give instant feedback Users keep on browsing

Slide 27

Slide 27 text

“It all depends on how we look at things, and not how they are in themselves.” - Carl G. Jung

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Farm out work

Slide 30

Slide 30 text

Language Agnostic

Slide 31

Slide 31 text

Platform Agnostic

Slide 32

Slide 32 text

Your Client Code Gearman Job Server gearmand Your Worker Code Your App Gearman Gearman Client API (C, PHP, Perl, MySQL UDF, ...) Gearman Worker API (C, PHP, Perl, Python, ...)

Slide 33

Slide 33 text

Your Client Code Gearman Job Server gearmand Your Worker Code Your App Gearman Gearman Client API (C, PHP, Perl, MySQL UDF, ...) Gearman Worker API (C, PHP, Perl, Python, ...)

Slide 34

Slide 34 text

Your Client Code Gearman Job Server gearmand Your Worker Code Your App Gearman Gearman Client API (C, PHP, Perl, MySQL UDF, ...) Gearman Worker API (C, PHP, Perl, Python, ...)

Slide 35

Slide 35 text

Userland Code pear.php.net/net_gearman PHP Extension pecl.php.net/gearman

Slide 36

Slide 36 text

PHP Releases

Slide 37

Slide 37 text

PHP Releases In Theory

Slide 38

Slide 38 text

•Linux •RPM •DEB •Windows •OS X •Solaris Multi Platform releases

Slide 39

Slide 39 text

Daily / Hourly Snapshots

Slide 40

Slide 40 text

Push out to mirrors / CDNs

Slide 41

Slide 41 text

A Story!

Slide 42

Slide 42 text

Financial Software

Slide 43

Slide 43 text

3000+ Clients

Slide 44

Slide 44 text

Each one has 5 external data sources

Slide 45

Slide 45 text

Each data source is a web service

Slide 46

Slide 46 text

Original solution was a big PHP script

Slide 47

Slide 47 text

Took over 24 hours

Slide 48

Slide 48 text

New solution is gearman

Slide 49

Slide 49 text

Ran every 6 hours every day

Slide 50

Slide 50 text

Cron

Slide 51

Slide 51 text

Cron Gearman

Slide 52

Slide 52 text

Cron Gearman Update

Slide 53

Slide 53 text

Cron Gearman Update 1 2 3 4 5 Web Services

Slide 54

Slide 54 text

Cron Gearman Update 1 2 3 4 5 Web Services 5 Processing 4 1 2 3

Slide 55

Slide 55 text

But! That wasn’t enough

Slide 56

Slide 56 text

Job kicked off on login

Slide 57

Slide 57 text

1 2 3 4 5 Update Web Services Processing 5 4 3 2 1

Slide 58

Slide 58 text

1 2 3 4 5 Update Web Services Processing 5 4 3 2 1

Slide 59

Slide 59 text

Teach me the magic!

Slide 60

Slide 60 text

Workers

Slide 61

Slide 61 text

Process Reuse

Slide 62

Slide 62 text

Multiple Capabilities

Slide 63

Slide 63 text

WS Update Worker-1 Process

Slide 64

Slide 64 text

WS Update Worker-1 Process

Slide 65

Slide 65 text

WS Update Worker-1 Worker-2 Worker-3 Process

Slide 66

Slide 66 text

addAbility('Update'); $worker->beginWork(); } catch (Net_Gearman_Exception $e) { echo $e->getMessage() . "\n"; exit; }

Slide 67

Slide 67 text

addAbility('Update'); $worker->addAbility('SaveDB'); $worker->addAbility('SaveMemcache'); $worker->beginWork(); } catch (Net_Gearman_Exception $e) { echo $e->getMessage() . "\n"; exit; }

Slide 68

Slide 68 text

Workers pick tasks based on type from top to bottom

Slide 69

Slide 69 text

Pass in -R to Server for capabilities to be assigned in a round-robin order

Slide 70

Slide 70 text

Helps when one worker has many capabilities

Slide 71

Slide 71 text

✓ High Priority ✓ Normal Priority (default) ✓ Low Priority Task Priorities

Slide 72

Slide 72 text

Task Priorities 3 different queues

Slide 73

Slide 73 text

Task Priorities 3 different queues First in - First out within each queue

Slide 74

Slide 74 text

Server Persistence

Slide 75

Slide 75 text

Server Persistence Local memory by default

Slide 76

Slide 76 text

✓ Memcache ✓ Drizzle ✓ MySQL ✓ PostgresSQL ✓ SQLite 3 ✓ TokyoCabinet ✓ Redis (in dev) ✓ MongoDB (in dev) Server Persistence

Slide 77

Slide 77 text

gearmand -d -q libsqlite3 --libsqlite3-db=/tmp/demo/gearman.db --listen=localhost --port=4370

Slide 78

Slide 78 text

Server HA

Slide 79

Slide 79 text

Server HA What if the server dies?

Slide 80

Slide 80 text

Server HA Keep multiple servers

Slide 81

Slide 81 text

gearmand -d -q libsqlite3 --libsqlite3-db=/tmp/demo/gearman.db --listen=localhost --port=4370 gearmand -d -q libsqlite3 --libsqlite3-db=/tmp/demo/gearman.db --listen=localhost --port=4371

Slide 82

Slide 82 text

Server HA Workers cycle through them

Slide 83

Slide 83 text

Worker 1 Client Client Server 1 Server 2 Client Client Worker 2 Worker 3

Slide 84

Slide 84 text

Worker 1 Client Client Server 1 Server 2 Client Client Worker 2 Worker 3

Slide 85

Slide 85 text

Worker 1 Client Client Server 1 Server 2 Client Client Worker 2 Worker 3

Slide 86

Slide 86 text

Code, Code, Code...

Slide 87

Slide 87 text

addAbility('Update'); $worker->addAbility('SaveDB'); $worker->addAbility('SaveMemcache'); $worker->beginWork(); } catch (Net_Gearman_Exception $e) { echo $e->getMessage() . "\n"; exit; }

Slide 88

Slide 88 text

Update($c); echo "Added client $c to the queue with the handler $jobs\n"; } catch (Net_Gearman_Exception $e) { echo 'Could not connect to any server'; }

Slide 89

Slide 89 text

connect(); // Fetch Salary information for this client $this->client->WebserviceSalary($args); // Fetch all the Point of Sale information for this client $this->client->WebservicePos($args); return true; } }

Slide 90

Slide 90 text

Slide 91

Slide 91 text

Foreground vs Background

Slide 92

Slide 92 text

How to keep it all alive and running?

Slide 93

Slide 93 text

Supervisord

Slide 94

Slide 94 text

Manages Processes

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

✓ Makes sure processes are running

Slide 97

Slide 97 text

✓ Makes sure processes are running ✓ Can start more than one of same process

Slide 98

Slide 98 text

✓ Makes sure processes are running ✓ Can start more than one of same process ✓ Restarts crashed processes

Slide 99

Slide 99 text

✓ Makes sure processes are running ✓ Can start more than one of same process ✓ Restarts crashed processes ✓ Simple to configure and manage

Slide 100

Slide 100 text

✓ Makes sure processes are running ✓ Can start more than one of same process ✓ Restarts crashed processes ✓ Simple to configure and manage ✓ Each worker type has a different config

Slide 101

Slide 101 text

Great for long running process!

Slide 102

Slide 102 text

j.mp/supvervisord

Slide 103

Slide 103 text

[program:gearman-worker-save-update] command=php /var/www/gearman/workers/worker1.php numprocs=10 process_name=%(program_name)s-%(process_num)s autorestart=true [program:gearman-worker-ws-1] command=php /var/www/gearman/workers/worker2.php numprocs=5 process_name=%(program_name)s-%(process_num)s autorestart=true [program:gearman-worker-ws-2] command=php /var/www/gearman/workers/worker3.php numprocs=5 process_name=%(program_name)s-%(process_num)s autorestart=true

Slide 104

Slide 104 text

gearman_manager

Slide 105

Slide 105 text

gearman_manager j.mp/gearman_manager

Slide 106

Slide 106 text

✓ Makes sure processes are running ✓ Can start more than one of same process ✓ Restarts crashed processes ✓ Simple to configure and manage ✓ Each worker type has a different config

Slide 107

Slide 107 text

Sounds familiar?

Slide 108

Slide 108 text

Does the worker heavy lifting for you

Slide 109

Slide 109 text

You can focus on writing amazing tasks

Slide 110

Slide 110 text

[GearmanManager] host=localhost:4370,localhost:4371 ; workers can be found in this dir worker_dir=/var/www/gearman/tasks ; Reload workers as new code is available auto_update=1 ; 20 workers will do all jobs count=20 ; Each job will have minimum 1 worker that does only that job dedicated_count=1 [Webservice_Pos] count=10 [Webservice_Salary] count=10

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

gearman_manager + Supervisord = Awesome!

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

Another Story!

Slide 115

Slide 115 text

CloudSplit

Slide 116

Slide 116 text

Near Real Time Cloud Analytics

Slide 117

Slide 117 text

Clients install logging agent locally

Slide 118

Slide 118 text

syslogd

Slide 119

Slide 119 text

Public API

Slide 120

Slide 120 text

Multiple Persistent Gearman Servers

Slide 121

Slide 121 text

Internal DB API

Slide 122

Slide 122 text

Agent

Slide 123

Slide 123 text

Agent syslogd

Slide 124

Slide 124 text

Agent syslogd API

Slide 125

Slide 125 text

Agent syslogd API Load Balanced

Slide 126

Slide 126 text

Agent syslogd API Gearman Gearman Load Balanced

Slide 127

Slide 127 text

Agent syslogd API Gearman Gearman Load Balanced Persistent

Slide 128

Slide 128 text

Agent syslogd API Gearman Gearman Worker Worker Worker Load Balanced Persistent

Slide 129

Slide 129 text

Agent syslogd API Gearman Gearman Worker Worker Worker Internal API Load Balanced Persistent

Slide 130

Slide 130 text

Agent syslogd API Gearman Gearman Worker Worker Worker Internal API Load Balanced Load Balanced Persistent

Slide 131

Slide 131 text

Agent syslogd API Gearman Gearman CouchDB Worker Worker Worker Internal API Load Balanced Load Balanced Persistent

Slide 132

Slide 132 text

engineyardjaunt.eventbrite.com Tomorrow: Tonight: Hackathon!

Slide 133

Slide 133 text

Questions? @h [email protected] Please rate at joind.in/6496