Gearman for MySQL

73
Getting started with Gearman for MySQL Giuseppe Maxia Eric Day This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License. Tuesday, 13 April 2010

description

Gearman is a client/server infrastructure for generic tasks, usable on distributed servers, with little worry about the details. No matter what language you speak, Gearman can meet your needs in C, PHP, Perl, Ruby, shell scripting, and several more. Gearman can also work in conjunction with MySQL, either using UDFs, or simply through its basic architecture. This talk will show examples of how to use Gearman for remote installation and how to call functions written in Perl from any other language or from inside MySQL server, with no knowledge of Perl at all.

Transcript of Gearman for MySQL

Page 1: Gearman for MySQL

Getting started with Gearman for

MySQL

Giuseppe MaxiaEric Day

This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License. Tuesday, 13 April 2010

Page 2: Gearman for MySQL

A short (I promise!) history of computing

Tuesday, 13 April 2010

Page 3: Gearman for MySQL

Mainframe

Mainframe

operating system

hardware

application

terminal

client

USER FRIENDLINESS

0 100

terminal

terminal

terminal

Tuesday, 13 April 2010

Page 4: Gearman for MySQL

Mini computers

Mainframe

operating system

hardware

application

client

USER FRIENDLINESS

0 100

Mini terminal

terminal

terminal

terminal

Mini

Tuesday, 13 April 2010

Page 5: Gearman for MySQL

Networks

server

hardware

application

client

USER FRIENDLINESS

0 100

personal computer

personal computer

personal computer

personal computer

hardware

operating system

operating system

hardware

Tuesday, 13 April 2010

Page 6: Gearman for MySQL

Web applications

web server

hardware

application

client

USER FRIENDLINESS

0 100

browser

browser

browser

browser

operating system

hardware

operating system

operating system

hardware

operating system

INT

ERN

ET

Tuesday, 13 April 2010

Page 7: Gearman for MySQL

Cloud applications

web server

hardware

application

client

USER FRIENDLINESS

0 100

browser

browser

browser

browser

operating system

hardware

operating system

operating system

hardware

service provider

applicationapplication

service providerservice

providerservice provider IN

TER

NET

Tuesday, 13 April 2010

Page 8: Gearman for MySQL

Some actors

• memcached

• gearman

Used in production by Yahoo!, LiveJournal and CraigList

Tuesday, 13 April 2010

Page 9: Gearman for MySQL

MAG A NE R

Tuesday, 13 April 2010

Page 10: Gearman for MySQL

MG A NER!=

Tuesday, 13 April 2010

Page 11: Gearman for MySQL

MAG A NE R ?

Tuesday, 13 April 2010

Page 12: Gearman for MySQL

M A GA N E R

Tuesday, 13 April 2010

Page 13: Gearman for MySQL

server worker client

taskjob

request

http://gearman.orgTuesday, 13 April 2010

Page 14: Gearman for MySQL

Gearman: a technology

for distributed computing

Tuesday, 13 April 2010

Page 15: Gearman for MySQL

Distributed

Tuesday, 13 April 2010

Page 16: Gearman for MySQL

Multiple operating systems

Tuesday, 13 April 2010

Page 17: Gearman for MySQL

multiple languages

Tuesday, 13 April 2010

Page 18: Gearman for MySQL

freedom of choice

Tuesday, 13 April 2010

Page 19: Gearman for MySQL

redundancy

Tuesday, 13 April 2010

Page 20: Gearman for MySQL

USING GEARMAN

• Server: gearmand

• Client libraries:

• C/C++

• Java

• Perl

• PHP

• Python

Tuesday, 13 April 2010

Page 21: Gearman for MySQL

Simple usage GEARMAN

• Command line client and worker

Tuesday, 13 April 2010

Page 22: Gearman for MySQL

starting the server/usr/local/sbin/gearmand -d

# started as daemon.# No feedback given on the command line

Tuesday, 13 April 2010

Page 23: Gearman for MySQL

starting the server (2)/usr/local/sbin/gearmand -v -v INFO Starting up INFO Listening on :::4730 (5) INFO Listening on 0.0.0.0:4730 (6)

# started as normal application# verbose output requested

Tuesday, 13 April 2010

Page 24: Gearman for MySQL

starting the workergearman -w -h hostname -p 4730 \ -f conta wc

# -w = act as worker# -f = function# conta = function name# wc = command to execute when function # 'conta' is called

Tuesday, 13 April 2010

Page 25: Gearman for MySQL

what the server says/usr/local/sbin/gearmand -v -v INFO Starting up INFO Listening on :::4730 (5) INFO Listening on 0.0.0.0:4730 (6) …

INFO Accepted connection from 127.0.0.1:4994 INFO [ 0] 127.0.0.1:4994 Connected

Tuesday, 13 April 2010

Page 26: Gearman for MySQL

starting the clientgearman -h hostname -p 4730 \ -f conta < ~/.bashrc 57 135 2149 # <- output # from worker

# -f = function# conta = function name# < ~/.bashrc = input data

Tuesday, 13 April 2010

Page 27: Gearman for MySQL

what the server says/usr/local/sbin/gearmand -v -v INFO Starting up INFO Listening on :::4730 (5) INFO Listening on 0.0.0.0:4730 (6) …

INFO Accepted connection from 127.0.0.1:4994 INFO [ 0] 127.0.0.1:4994 Connected … INFO Accepted connection from 127.0.0.1:5181 INFO [ 0] 127.0.0.1:5181 Connected INFO [ 0] 127.0.0.1:5181 Disconnected

Tuesday, 13 April 2010

Page 28: Gearman for MySQL

What happened

1 server start

listen to port 4730

Tuesday, 13 April 2010

Page 29: Gearman for MySQL

What happened

2 worker starts

registers function 'conta' to server

Tuesday, 13 April 2010

Page 30: Gearman for MySQL

What happened

3 client starts

requires function 'conta' from server

provides input data

Tuesday, 13 April 2010

Page 31: Gearman for MySQL

What happened

4 server sends client request to worker

passes all input data to worker

Tuesday, 13 April 2010

Page 32: Gearman for MySQL

What happened

5 worker receives request and data

processes input

Tuesday, 13 April 2010

Page 33: Gearman for MySQL

What happened

6 worker returns processed data

server passes it to client

Tuesday, 13 April 2010

Page 34: Gearman for MySQL

What happened

7 client receives processed data

client displays result

Tuesday, 13 April 2010

Page 35: Gearman for MySQL

A simple Perl worker

Tuesday, 13 April 2010

Page 36: Gearman for MySQL

A simple worker

1. add server

2. add function

3. loop

4. function definition

Tuesday, 13 April 2010

Page 37: Gearman for MySQL

simple worker (1)use strict;use warnings;use Gearman::XS qw(:constants);use Gearman::XS::Worker;

my $host = '127.0.0.1';my $port = 4730;

my $worker = new Gearman::XS::Worker;

my $ret = $worker->add_server($host, $port);if ($ret != GEARMAN_SUCCESS) { printf(STDERR "%s\n", $worker->error()); exit(1);}

Tuesday, 13 April 2010

Page 38: Gearman for MySQL

simple worker (2)my $options = '';

$ret = $worker->add_function( "reverse", # public function name 0, # timeout \&myreverse, # reference to function $options); # function argumentsif ($ret != GEARMAN_SUCCESS) { printf(STDERR "%s\n", $worker->error());}

Tuesday, 13 April 2010

Page 39: Gearman for MySQL

simple worker (3)while (1) { my $ret = $worker->work(); if ($ret != GEARMAN_SUCCESS) { printf(STDERR "%s\n", $worker->error()); }}

Tuesday, 13 April 2010

Page 40: Gearman for MySQL

simple worker (4)sub myreverse { my ($job) = @_;

my $workload = $job->workload(); my $result = reverse($workload);

printf("Job=%s F_Name=%s Workload=%s Result=%s\n", $job->handle(), $job->function_name(), $job->workload(), $result); return $result;}

Tuesday, 13 April 2010

Page 41: Gearman for MySQL

A simple client

Tuesday, 13 April 2010

Page 42: Gearman for MySQL

A simple perl client

• add server

• run a task

Tuesday, 13 April 2010

Page 43: Gearman for MySQL

simple client (1)use strict;use warnings;use Gearman::XS qw(:constants);use Gearman::XS::Client;

my $client = new Gearman::XS::Client;

my $host = '127.0.0.1';my $port = 4730;my $ret = $client->add_server($host, $port);if ($ret != GEARMAN_SUCCESS) { printf(STDERR "%s\n", $client->error()); exit(1);}

Tuesday, 13 April 2010

Page 44: Gearman for MySQL

simple client (2)my $input = shift || 'teststring';

my ($return, $result) = $client->do("reverse", $input);if ($return == GEARMAN_SUCCESS) { printf("Result=%s\n", $result);}

Tuesday, 13 April 2010

Page 45: Gearman for MySQL

A sample run

Tuesday, 13 April 2010

Page 46: Gearman for MySQL

host 1perl worker.pl

Tuesday, 13 April 2010

Page 47: Gearman for MySQL

host 2perl client.plResult=gnirtstset

Tuesday, 13 April 2010

Page 48: Gearman for MySQL

host 1perl worker.plJob=H:gmac3.local:4 F_Name=reverse Workload=teststring Result=gnirtstset

Tuesday, 13 April 2010

Page 49: Gearman for MySQL

more client functions

• do_background

• add_task

• run_tasks

Tuesday, 13 April 2010

Page 50: Gearman for MySQL

Some advanced usage

• DBIx::SQLCrosstab

• Data cubes

• Perl only

Tuesday, 13 April 2010

Page 51: Gearman for MySQL

See more hacks!

• Gearman hacks with MySQL

• Thursday at 2pm

Tuesday, 13 April 2010

Page 52: Gearman for MySQL

Tuesday, 13 April 2010

Page 53: Gearman for MySQL

Image processing

• CPU intensive

• Large storage needed

• Application is OS specific

Tuesday, 13 April 2010

Page 54: Gearman for MySQL

See more performance!

• Boosting database performance with Gearman

• tomorrow, at 3:05pm

Tuesday, 13 April 2010

Page 55: Gearman for MySQL

Tuesday, 13 April 2010

Page 56: Gearman for MySQL

Gearman for the MySQL server

Tuesday, 13 April 2010

Page 57: Gearman for MySQL

GearmanUDF

Tuesday, 13 April 2010

Page 58: Gearman for MySQL

More power to MySQL

• Perl/PHP/Python functions

• Shell access (you can send and receive email!)

• filesystem access

• advanced monitoring through Gearman features

@

Tuesday, 13 April 2010

Page 59: Gearman for MySQL

WARNING!You can easily shoot yourself in the foot

Tuesday, 13 April 2010

Page 61: Gearman for MySQL

Create a worker (1)my @functions = ( ['reverse', \&myreverse], ['count', \&mycount], ['shell', \&myshell], ['eval', \&myeval], ['store', \&mystore],);

# see the full code here:#http://forge.mysql.com/tools/tool.php?id=235

Tuesday, 13 April 2010

Page 62: Gearman for MySQL

Create a worker (2)for my $func (@functions) { $ret = $worker->add_function( $func->[0], 0, $func->[1], $options); if ($ret != GEARMAN_SUCCESS) { printf(STDERR "error with function %s - %s\n", $func->[0], $worker->error()); }}

Tuesday, 13 April 2010

Page 63: Gearman for MySQL

Create a worker (3)

sub myshell { my $job = shift; my $workload = $job->workload(); my $result = qx($workload); return $result;}

# WARNING!# You can shoot yourself # in the foot!

Tuesday, 13 April 2010

Page 64: Gearman for MySQL

Create a worker (3)

sub myshell { my $job = shift; my $workload = $job->workload(); my $result = qx($workload); return $result;}

# WARNING!# You can shoot yourself # in the foot!

Tuesday, 13 April 2010

Page 65: Gearman for MySQL

Create a worker (4)

sub myeval { my $job = shift; my $workload = $job->workload(); my $result = eval $workload; return $result;}

# WARNING!# You can machine gun yourself # in the foot!

Tuesday, 13 April 2010

Page 66: Gearman for MySQL

Create a worker (4)

sub myeval { my $job = shift; my $workload = $job->workload(); my $result = eval $workload; return $result;}

# WARNING!# You can machine gun yourself # in the foot!

Tuesday, 13 April 2010

Page 67: Gearman for MySQL

Using the UDFmysql> select gman_do('reverse','abcd') as test;+------+| test |+------+| dcba |+------+

Tuesday, 13 April 2010

Page 68: Gearman for MySQL

Using the UDFmysql> SELECT gman_do('shell',concat(' ls -lh ', (select variable_value from information_schema.global_variables where variable_name = "datadir" )))\G

total 40976-rw-rw---- 1 gmax staff 5.0M Nov 11 13:34 ib_logfile0-rw-rw---- 1 gmax staff 5.0M Nov 11 13:34 ib_logfile1-rw-rw---- 1 gmax staff 10M Nov 11 13:34 ibdata1-rw-rw---- 1 gmax staff 1.2K Nov 11 13:34 msandbox.errdrwx------ 2 gmax staff 2.4K Nov 11 13:34 mysql-rw-rw---- 1 gmax staff 6B Nov 11 13:34 mysql_sandbox5140.piddrwx------ 2 gmax staff 68B Nov 11 13:34 test

Tuesday, 13 April 2010

Page 69: Gearman for MySQL

Using the UDFmysql> select gman_do('eval','2 * 3') ;+-------------------------+| gman_do('eval','2 * 3') |+-------------------------+| 6 |+-------------------------+

Tuesday, 13 April 2010

Page 70: Gearman for MySQL

Using the UDFmysql> select gman_do('eval', concat('$_="',host,'";tr/a-z/b-za/; $_')) as test from mysql.user;+-------------+| test |+-------------+| % || mpdbmiptu |+-------------+

Tuesday, 13 April 2010

Page 71: Gearman for MySQL

Replication scenarioMaster

slaveslave

Tuesday, 13 April 2010

Page 72: Gearman for MySQL

Replication scenarioMaster

slaveslave

slaves status

slave 1

slave 2

Tuesday, 13 April 2010

Page 73: Gearman for MySQL

THANKS

This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.

Let's talk!

Tuesday, 13 April 2010