high gear php with gearman (phpday 2010)

75
14/05/2010 - phpDay Italia 2010 Felix De Vliegher High gear PHP with Gearman

Upload: felix-de-vliegher

Post on 29-Jan-2018

12.794 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: High gear PHP with Gearman (phpDay 2010)

14/05/2010 - phpDay Italia 2010Felix De Vliegher

High gear PHP with Gearman

Page 2: High gear PHP with Gearman (phpDay 2010)

• Consultant and software engineer at Ibuildings NL

• Long time PHP developer

• PHPBenelux

• Interest in PHP QA, high performance & scalability

• Belgium

Whoami?

2

Page 3: High gear PHP with Gearman (phpDay 2010)

• Consultant and software engineer at Ibuildings NL

• Long time PHP developer

• PHPBenelux

• Interest in PHP QA, high performance & scalability

• Belgium (beer)!

Whoami?

3

Page 4: High gear PHP with Gearman (phpDay 2010)

• Consultant and software engineer at Ibuildings NL

• Long time PHP developer

• PHPBenelux

• Interest in PHP QA, high performance & scalability

• Belgium

Whoami?

4

Page 5: High gear PHP with Gearman (phpDay 2010)

What is Gearman?And why should I care about it?

5

Page 6: High gear PHP with Gearman (phpDay 2010)

6

GEARMAN!

Page 7: High gear PHP with Gearman (phpDay 2010)

A grain of truth

The name is an anagram for “Manager,” since it dispatches jobs to be done, but does not do anything useful itself.

- From Gearman website

7

Page 8: High gear PHP with Gearman (phpDay 2010)

What is Gearman?

It’s an

Application frameworkto

distribute work

8

Page 9: High gear PHP with Gearman (phpDay 2010)

Distribute work?

• Actual work is farmed out

• Handled by a number of nodes

• RPC

• Distributed parallel processing

• Shared nothing

9

Page 10: High gear PHP with Gearman (phpDay 2010)

f

Traditional architecture

10

Model

Controller

View

Application

Page 11: High gear PHP with Gearman (phpDay 2010)

Model

New architecture

11

Client application

Gearman Job server(gearmand)

Gearman Client API

Gearman Worker API

Worker application

Gearman Worker API

Worker application

Gearman Worker API

Worker application

Page 12: High gear PHP with Gearman (phpDay 2010)

Terminology

12

Page 13: High gear PHP with Gearman (phpDay 2010)

Terminology

12

Client Create jobs to be run and send them to a job server

Worker Register with a job server and grab jobs to run

Job Server Coordinates assignment from clients to workers, handles restarts

Page 14: High gear PHP with Gearman (phpDay 2010)

Terminology

12

Client Create jobs to be run and send them to a job server

Worker Register with a job server and grab jobs to run

Job Server Coordinates assignment from clients to workers, handles restarts

Page 15: High gear PHP with Gearman (phpDay 2010)

Terminology

12

Client Create jobs to be run and send them to a job server

Worker Register with a job server and grab jobs to run

Job Server Coordinates assignment from clients to workers, handles restarts

Page 16: High gear PHP with Gearman (phpDay 2010)

Application areas

• Image resizing / generating

• Log analysis and aggregation

• Asynchronous queues

• Map/Reduce

• URL processing

• Cache warm-up

13

Page 17: High gear PHP with Gearman (phpDay 2010)

Multiple client & worker API’s

14

Page 18: High gear PHP with Gearman (phpDay 2010)

Multiple client & worker API’s

14

Page 19: High gear PHP with Gearman (phpDay 2010)

Advantages

• Speed up work

• Parallel and asynchronous work

• Doesn’t block your apache processes

• Scales well

• Architecture-based workload distributing

• Legacy code

15

Page 20: High gear PHP with Gearman (phpDay 2010)

Once upon a timeGearman history

16

Page 21: High gear PHP with Gearman (phpDay 2010)

Once upon a time

• 2005: http://brad.livejournal.com/2106943.html

• Originally a Perl implementation

• Created by Danga Interactive

• Guys behind Memcache and MogileFS

17

Page 22: High gear PHP with Gearman (phpDay 2010)

Once upon a time

• 2008: Rewrite in C by Brian Aker

• PHP Extension by James Luedke

• Gearman powers some of the largest sites around:

• Digg: 45+ servers, 400K jobs/day

• Yahoo: 60+ servers, 6M jobs/day

• Netlog.com

• Xing.com

18

Page 23: High gear PHP with Gearman (phpDay 2010)

Installing Gearman

• Job Server: gearmand

• Get it from https://launchpad.net/gearmand/• extract, configure, make, make install:

19

dev:/usr/local/src# wget http://launchpad.net/gearmand/trunk/0.10/+download/gearmand-0.10.tar.gz

dev:/usr/local/src# tar -xzvf gearmand-0.10.tar.gz

dev:/usr/local/src# cd gearmand-0.10/

dev:/usr/local/src/gearmand-0.10# ./configure --prefix=/usr/local/

dev:/usr/local/src/gearmand-0.10# make

dev:/usr/local/src/gearmand-0.10# make install

dev:/usr/local/src/gearmand-0.10# /usr/local/sbin/gearmand --help

Page 24: High gear PHP with Gearman (phpDay 2010)

Gearmand usage

/usr/local/sbin/gearmand -d -u <user> -L 127.0.0.1 -p 7003

20

-d Start as daemon in background

-u <user> Run as the specified user

-L <host> Only listen on the specified host or IP

-p <port> Listen on the specified port

-t <threads> Number of threads to use

-v(vv) Verbose (useful for debugging)

Page 25: High gear PHP with Gearman (phpDay 2010)

Commandline gearman

• Client mode• ls | gearman -f processFiles

• gearman -f processFiles < file

• gearman -f processFiles “foo data”

• Worker mode• gearman -w -f lineCount -- wc -l

• gearman -w -c 100 -f doStuff ./script.sh

• Example:

21

dev:~/gearman# gearman -w -f foo -- grep GearmanClient &dev:~/gearman# cat demo.php | gearman -f foo$client= new GearmanClient();

Page 26: High gear PHP with Gearman (phpDay 2010)

PHP Interface

22

Page 27: High gear PHP with Gearman (phpDay 2010)

PHP: 2 options

• Pecl extension:

$ pecl install channel://pecl.php.net/gearman-0.6.0

$ php -i | grep "gearman support"

gearman support => enabled

• Net_Gearman PEAR Library:

$ pear install Net_Gearman

• Net_Gearman_Job

• Net_Gearman_Worker

• Net_Gearman_Task

• Net_Gearman_Set

• Net_Gearman_Client

23

Page 28: High gear PHP with Gearman (phpDay 2010)

Worker:

Client:

Simplest example

24

Page 29: High gear PHP with Gearman (phpDay 2010)

Client API

Setting up the client:

25

Page 30: High gear PHP with Gearman (phpDay 2010)

Client API

Job priorities and synchronous vs asynchronous:

26

Page 31: High gear PHP with Gearman (phpDay 2010)

GearmanClient::jobStatus()

27

array(4) { [0]=> bool(true) [1]=> bool(true) [2]=> int(2) [3]=> int(5)}

Job is known?

Job still running?

Numerator

Denominator

Page 32: High gear PHP with Gearman (phpDay 2010)

Notifying the client

Client receiving the status notifications:

28

dev:~/gearman# php -q client.php Running: true, numerator: 0, denomintor: 2Running: true, numerator: 1, denomintor: 2Running: false, numerator: 0, denomintor: 0

Page 33: High gear PHP with Gearman (phpDay 2010)

Worker API

Possible to add multiple servers:

29

Page 34: High gear PHP with Gearman (phpDay 2010)

Worker API

Registering functions using a callback:

Pass application data to the functions:

30

# php -q client.php Sending jobCount: 1: HELLO!Count: 2: WORLD!

Page 35: High gear PHP with Gearman (phpDay 2010)

Worker API

31

Put it all together:

Page 36: High gear PHP with Gearman (phpDay 2010)

Notifying the client

Status notifications:

GearmanJob::sendStatus(int $numerator, int $denominator)

GearmanJob::sendWarning(string $warning)

GearmanJob::sendComplete(string $result)

GearmanJob::sendFail(void)

GearmanJob::sendException(string $exception)

32

Page 37: High gear PHP with Gearman (phpDay 2010)

Jobs vs Tasks

33

Page 39: High gear PHP with Gearman (phpDay 2010)

What about persistence?

By default, jobs are stored in memory (fast)

gearmand --queue-type (-q)

• libdrizzle

• postgresql

• libmemcached

• libsqlite3

Only for background jobs

35

Page 40: High gear PHP with Gearman (phpDay 2010)

Sqlite3 example

gearmand --queue-type=libsqlite3 --libsqlite3-db=/tmp/jobs.sqlite --libsqlite3-table=gearman_jobs

Table structure:

36

sqlite> CREATE TABLE gearman_jobs ( ...> unique_key TEXT PRIMARY_KEY, ...> function_name TEXT, ...> priority INTEGER, ...> data BLOB ...> );

Page 41: High gear PHP with Gearman (phpDay 2010)

How to do storage?

37

Page 42: High gear PHP with Gearman (phpDay 2010)

How to do storage?

Distributed

37

Page 43: High gear PHP with Gearman (phpDay 2010)

How to do storage?

Distributed

Need to share storage (most of the time)

37

Page 44: High gear PHP with Gearman (phpDay 2010)

How to do storage?

Distributed

Need to share storage (most of the time)

Some options:

37

Page 45: High gear PHP with Gearman (phpDay 2010)

How to do storage?

Distributed

Need to share storage (most of the time)

Some options:

• NFS

37

Page 46: High gear PHP with Gearman (phpDay 2010)

How to do storage?

Distributed

Need to share storage (most of the time)

Some options:

• NFS

• MogileFS

37

Page 47: High gear PHP with Gearman (phpDay 2010)

How to do storage?

Distributed

Need to share storage (most of the time)

Some options:

• NFS

• MogileFS

• DR:BD

37

Page 48: High gear PHP with Gearman (phpDay 2010)

So, can I kick out my crons?

38

Page 49: High gear PHP with Gearman (phpDay 2010)

So, can I kick out my crons?

Not quite :)

38

Page 50: High gear PHP with Gearman (phpDay 2010)

So, can I kick out my crons?

Not quite :)

Scheduled execution (cron)

38

Page 51: High gear PHP with Gearman (phpDay 2010)

So, can I kick out my crons?

Not quite :)

Scheduled execution (cron)

delayed execution (at)

38

Page 52: High gear PHP with Gearman (phpDay 2010)

So, can I kick out my crons?

Not quite :)

Scheduled execution (cron)

delayed execution (at)

38

*/15 * * * * /usr/local/bin/gearman -f cronTask < /tmp/input.txt

Page 53: High gear PHP with Gearman (phpDay 2010)

So, can I kick out my crons?

Not quite :)

Scheduled execution (cron)

delayed execution (at)

at functionality is considered for inclusion

38

*/15 * * * * /usr/local/bin/gearman -f cronTask < /tmp/input.txt

Page 54: High gear PHP with Gearman (phpDay 2010)

So, can I kick out my crons?

Not quite :)

Scheduled execution (cron)

delayed execution (at)

at functionality is considered for inclusion

See http://groups.google.com/group/gearman/browse_thread/thread/b9891649fb08d16b#

38

*/15 * * * * /usr/local/bin/gearman -f cronTask < /tmp/input.txt

Page 55: High gear PHP with Gearman (phpDay 2010)

HTTP protocol

Start gearmand with -r http

Use X-Gearman-* headers to modify job

39

POST /reverse HTTP/1.1Content-Length: 13

Hello phpDay!

HTTP/1.0 200 OKX-Gearman-Job-Handle: H:gman:4Content-Length: 13Server: Gearman/0.9

!yaDphp olleH

Page 56: High gear PHP with Gearman (phpDay 2010)

Application areas

40

Page 57: High gear PHP with Gearman (phpDay 2010)

Image resizing: Client

41

Page 58: High gear PHP with Gearman (phpDay 2010)

Image resizing: worker

42

Page 59: High gear PHP with Gearman (phpDay 2010)

Map / Reduce

43

Client

Gearman Job server

Map/Reduce Worker

Client Client Client

Gearman Job server

Worker Worker Worker Worker Worker

Page 60: High gear PHP with Gearman (phpDay 2010)

Apache logging

44

Page 61: High gear PHP with Gearman (phpDay 2010)

Apache logging

44

Page 62: High gear PHP with Gearman (phpDay 2010)

Monitoring

45

Page 63: High gear PHP with Gearman (phpDay 2010)

Monitoring

GearUp: Monitoring service for gearman‣ No code yet, but looks promising

‣ http://launchpad.net/gearup

46

Page 64: High gear PHP with Gearman (phpDay 2010)

Monitoring

GearUp: Monitoring service for gearman‣ No code yet, but looks promising

‣ http://launchpad.net/gearup

46

Page 65: High gear PHP with Gearman (phpDay 2010)

Monitoring

GearUp: Monitoring service for gearman‣ No code yet, but looks promising

‣ http://launchpad.net/gearup

Telnet monitoring:

46

Page 66: High gear PHP with Gearman (phpDay 2010)

Monitoring

Supervisord: can manage workers

Combine with get_memory_usage() to restart workers

http://supervisord.org/

Alternative: http://github.com/brianlmoon/GearmanManager

47

[program:gearman-foobar-worker]command=/usr/local/bin/php -q /home/foo/workers/foobar.phpprocess_name=%(program_name)s_%(process_num)02dautostart=trueautorestart=truenumprocs=100redirect_stderr=truestdout_logfile=/var/log/gearman-foobar-worker.logstdout_logfile_maxbytes=5MBstdout_logfile_backups=10

Page 67: High gear PHP with Gearman (phpDay 2010)

Alternatives

Most similar: Beanstalkd

• PHP Client: pheanstalk

• Web interface in Django

• http://kr.github.com/beanstalkd/

Zend Server job queue

• Has job scheduling and dependencies built in

• Not free

• http://www.zend.com/en/products/server/zend-server-job-queue

48

Page 68: High gear PHP with Gearman (phpDay 2010)

Questions ?

49

Feedback: http://joind.in/1470

Page 69: High gear PHP with Gearman (phpDay 2010)

50

Ibuildings challenge!

Page 70: High gear PHP with Gearman (phpDay 2010)

50

Ibuildings challenge!

http://www.ibuildings.com/challenge

Page 71: High gear PHP with Gearman (phpDay 2010)

50

Ibuildings challenge!

http://www.ibuildings.com/challenge

“The Test Driven Challenge”

Page 72: High gear PHP with Gearman (phpDay 2010)

50

Ibuildings challenge!

http://www.ibuildings.com/challenge

“The Test Driven Challenge”

Win an iPad! (when they’re available)

Page 73: High gear PHP with Gearman (phpDay 2010)

50

Ibuildings challenge!

http://www.ibuildings.com/challenge

“The Test Driven Challenge”

Win an iPad! (when they’re available)

We’re also hiring!

Page 74: High gear PHP with Gearman (phpDay 2010)

Links & sourcesCredits:- Gear man: http://agearman.com/- Distributed computing: http://www.theleadblog.com/2009/06/lead-distribution-creating-right-sales.html- Old gears: http://decorate.pebblez.com- Army of elephpants: http://www.flickr.com/people/dragonbe/

Gearman online:- http://www.danga.com/gearman/- http://gearman.org/- http://pecl.php.net/package/gearman

- IRC: #gearman on irc.freenode.net- ML: http://groups.google.com/group/gearman

51

Page 75: High gear PHP with Gearman (phpDay 2010)

Thank you!

Contact details:

Felix De Vliegher Email: [email protected]

Twitter: @felixdv