High gear PHP with Gearman

50
PHPBenelux meeting - 13/10/2009 Felix De Vliegher High gear PHP with Gearman zondag 11 oktober 2009

description

Gearman provides a generic application framework to farm out work to other machines or processes that are better suited to do the work.This presentation goes deeper into using Gearman with PHP.

Transcript of High gear PHP with Gearman

Page 1: High gear PHP with Gearman

PHPBenelux meeting - 13/10/2009Felix De Vliegher

High gear PHP with Gearman

zondag 11 oktober 2009

Page 2: High gear PHP with Gearman

Contents

• What is Gearman?

• Distributed work

• Once upon a time

• Advantages

• Gearman 101

• PHP interface

• Examples!

• Application areas

• Other bits

2

zondag 11 oktober 2009

Page 3: High gear PHP with Gearman

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

3

zondag 11 oktober 2009

Page 4: High gear PHP with Gearman

4

zondag 11 oktober 2009

Page 5: High gear PHP with Gearman

Quote

The way I like to think of Gearmanis as a massively distributed,

massively fault tolerant fork mechanism.

- Joe Stump, Digg

5

“”

zondag 11 oktober 2009

Page 6: High gear PHP with Gearman

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

6

zondag 11 oktober 2009

Page 7: High gear PHP with Gearman

What is Gearman?

It’s an

Application frameworkto

distribute work

7

zondag 11 oktober 2009

Page 8: High gear PHP with Gearman

Some keywords

• Open source• Multi-language• Parallel• Asynchronous• Multi threaded -> blazingly fast• Persistent queues• Decentralized• Fault tolerant• tcp 4730• multiple client api’s (c, php, perl, drizzle, ...)

8

zondag 11 oktober 2009

Page 9: High gear PHP with Gearman

Distributed processing

9

Or: how to make others do all the work

zondag 11 oktober 2009

Page 10: High gear PHP with Gearman

“Distributed computing”

are

“autonomous computational entries”

(computers or nodes)

which are communicating by

message passing

10

zondag 11 oktober 2009

Page 11: High gear PHP with Gearman

Distributed processing

• Actual work is distributed

• Handled by a number of “computational entities”

• Some sort of RPC

• Distributed parallel processing

• Shared nothing

• Decentralized

11

zondag 11 oktober 2009

Page 12: High gear PHP with Gearman

Distributed processing

• Actual work is distributed

• Handled by a number of “computational entities”

• Some sort of RPC

• Distributed parallel processing

• Shared nothing

• Decentralized

11

Gearman!

zondag 11 oktober 2009

Page 13: High gear PHP with Gearman

Once upon a timeGearman history

12

zondag 11 oktober 2009

Page 14: High gear PHP with Gearman

Once upon a time

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

• Originally a Perl implementation

• Created by Danga Interactive

• LiveJournal

• SixApart

• Guys behind Memcache and MogileFS

13

zondag 11 oktober 2009

Page 15: High gear PHP with Gearman

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

• Xing.com

• Maybe Netlog? ;-)

14

zondag 11 oktober 2009

Page 16: High gear PHP with Gearman

Advantages

15

zondag 11 oktober 2009

Page 17: High gear PHP with Gearman

Advantages

• Speed up work

• Load balance work

• Parallel and asynchronous work

• Scales well

• Architecture-based workload distributing

• Call functionality in other programming languages

• Doesn’t block your apache processes!

16

zondag 11 oktober 2009

Page 18: High gear PHP with Gearman

Gearman 101Terminology and architecture

17

zondag 11 oktober 2009

Page 19: High gear PHP with Gearman

Terminology

18

zondag 11 oktober 2009

Page 20: High gear PHP with Gearman

Terminology

18

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

zondag 11 oktober 2009

Page 21: High gear PHP with Gearman

Terminology

18

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

zondag 11 oktober 2009

Page 22: High gear PHP with Gearman

Terminology

18

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

zondag 11 oktober 2009

Page 23: High gear PHP with Gearman

Tasks vs Jobs

• A job is always a task

• A task is not always a job

• Single interface: jobs

• Concurrent interface: tasks

• Clients deal with tasks

• Workers deal with jobs

19

zondag 11 oktober 2009

Page 24: High gear PHP with Gearman

Architecture

20

Client application

Gearman Job server(gearmand)

Gearman Client API

Gearman Worker API

Worker application

Gearman Worker API

Worker application

Gearman Worker API

Worker application

zondag 11 oktober 2009

Page 25: High gear PHP with Gearman

Installing Gearman

• Job Server: gearmand

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

21

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

zondag 11 oktober 2009

Page 26: High gear PHP with Gearman

Gearmand usage

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

22

-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

zondag 11 oktober 2009

Page 27: High gear PHP with Gearman

Commandline gearman

• Client mode• ls | gearman -f function

• gearman -f function < file

• gearman -f function “foo data”

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

• gearman -w -f function ./script.sh

• Example:

23

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

zondag 11 oktober 2009

Page 28: High gear PHP with Gearman

PHP Interface

24

zondag 11 oktober 2009

Page 29: High gear PHP with Gearman

PHP: Worker API

Possible to add multiple servers:

25

zondag 11 oktober 2009

Page 30: High gear PHP with Gearman

PHP: Worker API

Registering functions and any valid callback:

Pass application data to the functions:

26

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

zondag 11 oktober 2009

Page 31: High gear PHP with Gearman

PHP: Worker API

27

Put it all together:

zondag 11 oktober 2009

Page 32: High gear PHP with Gearman

PHP: Job API

Status notifications:

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

GearmanJob::sendWarning(string $warning)

GearmanJob::sendComplete(string $result)

GearmanJob::sendFail(void)

GearmanJob::sendException(string $exception)

28

zondag 11 oktober 2009

Page 34: High gear PHP with Gearman

PHP: Job API

Client receiving the status notifications:

30

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

zondag 11 oktober 2009

Page 35: High gear PHP with Gearman

PHP: Client API

Setting up the client:

31

zondag 11 oktober 2009

Page 36: High gear PHP with Gearman

PHP: Client API

Job priorities and synchronous vs asynchronous:

32

$status returns an array containing status information for the job corresponding to the supplied job handle.

The first array element is a boolean indicating whether the job is even known, the second is a boolean indicating whether the job is still running, and the third and fourth elements correspond to the numerator and denominator of the fractional completion percentage, respectively.

zondag 11 oktober 2009

Page 37: High gear PHP with Gearman

Tasks vs Jobs

33

zondag 11 oktober 2009

Page 38: High gear PHP with Gearman

Application areas

34

zondag 11 oktober 2009

Page 39: High gear PHP with Gearman

Application areas

• Map/Reduce

• Log analysis and aggregation

• Image resizing

• Asynchronous queues

• URL processing

• Cache warm-up

• Database jobs

• Cloud services and applications

35

zondag 11 oktober 2009

Page 40: High gear PHP with Gearman

Map / Reduce

• Client sends request to intermediate job server

• Map / reduce worker splits up job (MAP)

• Each part is processed by workers

• Response is sent back to map / reduce worker

• Results are collected and aggregated (REDUCE)

• Client receives one reduced response

36

zondag 11 oktober 2009

Page 41: High gear PHP with Gearman

Map / Reduce

37

Client

Gearman Job server

Map/Reduce Worker

Client Client Client

Gearman Job server

Worker Worker Worker Worker Worker

zondag 11 oktober 2009

Page 42: High gear PHP with Gearman

Image resizing: worker

38

zondag 11 oktober 2009

Page 43: High gear PHP with Gearman

Image resizing: Client

39

zondag 11 oktober 2009

Page 44: High gear PHP with Gearman

Image resizing: output

$ php -q resize_worker.php &

$ php -q resize_client.php image.jpg

H:dev:39 - creating: small_image.jpg x:200 y:

Status: 1/1

$ ls *.jpg

image.jpg small_image.jpg

40

zondag 11 oktober 2009

Page 45: High gear PHP with Gearman

Other bits

41

zondag 11 oktober 2009

Page 46: High gear PHP with Gearman

Other bits

GearUp: Gearman based monitoring service

• No code yet, but looks promising

• http://launchpad.net/gearup

Apache logging through Gearman:

42

zondag 11 oktober 2009

Page 47: High gear PHP with Gearman

Other bits

Gearman and MySQL with Drizzle

• http://krow.livejournal.com/628025.html• todo

43

zondag 11 oktober 2009

Page 48: High gear PHP with Gearman

Links & sourcesCredits:- Red gears: http://lineymachine.googlepages.com/- Distributed computing: http://www.theleadblog.com/2009/06/lead-distribution-creating-right-sales.html- Old gears: http://decorate.pebblez.com

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

44

zondag 11 oktober 2009

Page 49: High gear PHP with Gearman

Questions ?

45

zondag 11 oktober 2009

Page 50: High gear PHP with Gearman

Thank you!

Contact details:

Email: [email protected]: felixdv

zondag 11 oktober 2009