| 1 | # $Id$ |
|---|
| 2 | |
|---|
| 3 | package Mvalve; |
|---|
| 4 | use Mvalve::Const; |
|---|
| 5 | use Mvalve::Types; |
|---|
| 6 | use Mvalve::Message; |
|---|
| 7 | use Mvalve::Reader; |
|---|
| 8 | use Mvalve::Throttler; |
|---|
| 9 | use Mvalve::Writer; |
|---|
| 10 | use Time::HiRes(); |
|---|
| 11 | |
|---|
| 12 | our $VERSION = '0.00009'; |
|---|
| 13 | our $AUTHORITY = "cpan:DMAKI"; |
|---|
| 14 | |
|---|
| 15 | sub trace { print STDERR "MVALVE: @_\n" } |
|---|
| 16 | |
|---|
| 17 | 1; |
|---|
| 18 | |
|---|
| 19 | __END__ |
|---|
| 20 | |
|---|
| 21 | =head1 NAME |
|---|
| 22 | |
|---|
| 23 | Mvalve - Generic Q4M Powered Message Pipe |
|---|
| 24 | |
|---|
| 25 | =head1 SYNOPSIS |
|---|
| 26 | |
|---|
| 27 | my $writer = Mvalve::Writer->new( |
|---|
| 28 | queue => { |
|---|
| 29 | args => { |
|---|
| 30 | connect_info => [ 'dbi:mysql:dbname=...', ..., ... ] |
|---|
| 31 | } |
|---|
| 32 | } |
|---|
| 33 | ); |
|---|
| 34 | |
|---|
| 35 | my $reader = Mvalve::Reader->new( |
|---|
| 36 | state => { |
|---|
| 37 | args => { |
|---|
| 38 | } |
|---|
| 39 | }, |
|---|
| 40 | queue => { |
|---|
| 41 | module => "...", |
|---|
| 42 | args => { |
|---|
| 43 | connect_info => [ ... ] |
|---|
| 44 | } |
|---|
| 45 | }, |
|---|
| 46 | throttler => { |
|---|
| 47 | args => { |
|---|
| 48 | max_items => $max, |
|---|
| 49 | interval => $interval, |
|---|
| 50 | cache => { |
|---|
| 51 | data => [ ... ] |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | } |
|---|
| 55 | ); |
|---|
| 56 | |
|---|
| 57 | $writer->insert( Mvalve::Message->new(...) ); |
|---|
| 58 | |
|---|
| 59 | while ( 1 ) { |
|---|
| 60 | my $message = $reader->next; |
|---|
| 61 | if ($message) { |
|---|
| 62 | # do whatever |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | =head1 DESCRIPTION |
|---|
| 67 | |
|---|
| 68 | Mvalve stands for "Messave Valve". It is a frontend for Q4M powered set of |
|---|
| 69 | queues, acting as a single pipe. |
|---|
| 70 | |
|---|
| 71 | Mvalve contains a reader and a writer. It's constructed like this because |
|---|
| 72 | typically Mvalve operations are done in separate, read-only or write-only |
|---|
| 73 | processes, so you don't need both to do the job. |
|---|
| 74 | |
|---|
| 75 | All throttling is done at the reader side, so the only thing that the writer |
|---|
| 76 | needs is the information about the queue: |
|---|
| 77 | |
|---|
| 78 | Mvalve::Writer->new( queue => $queue_object ); |
|---|
| 79 | # or |
|---|
| 80 | Mvalve::Writer->new( |
|---|
| 81 | queue => { |
|---|
| 82 | args => { |
|---|
| 83 | connect_info => [ 'dbi:mysql:dbname=...', ..., ... ] |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | ); |
|---|
| 87 | |
|---|
| 88 | The reader needs a bit more information: |
|---|
| 89 | |
|---|
| 90 | Mvalve::Reader->new( |
|---|
| 91 | queue => $queue_object, |
|---|
| 92 | throttler => $throttler_object, # optional - default will be provided |
|---|
| 93 | state => $state_object, # optional - default will be provided |
|---|
| 94 | ); |
|---|
| 95 | # or |
|---|
| 96 | Mvalve::Reader->new( |
|---|
| 97 | queue => { |
|---|
| 98 | module => "Q4M", |
|---|
| 99 | args => { |
|---|
| 100 | connect_info => [ 'dbi:mysql:dbname=...', ..., ... ] |
|---|
| 101 | } |
|---|
| 102 | }, |
|---|
| 103 | throttler => { |
|---|
| 104 | module => "Data::Valve", |
|---|
| 105 | args => { |
|---|
| 106 | max_items => 1, |
|---|
| 107 | interval => 10, |
|---|
| 108 | store => { |
|---|
| 109 | module => "Memcached", |
|---|
| 110 | args => { |
|---|
| 111 | servers => [ ... ] |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | }, |
|---|
| 116 | state => { |
|---|
| 117 | module => "Memcached", |
|---|
| 118 | args => { |
|---|
| 119 | servers => [ ... ] |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | ); |
|---|
| 123 | |
|---|
| 124 | =head1 SETUP |
|---|
| 125 | |
|---|
| 126 | You need to have installed mysql 5.1 or later and q4m. You can grab |
|---|
| 127 | them at: |
|---|
| 128 | |
|---|
| 129 | http://dev.mysql.com/ |
|---|
| 130 | http://q4m.31tools.com/ |
|---|
| 131 | |
|---|
| 132 | Once you have a q4m-enabled mysql running, you need to create these q4m |
|---|
| 133 | enabled tables in your mysql database. |
|---|
| 134 | |
|---|
| 135 | CREATE TABLE q_emerg ( |
|---|
| 136 | destination VARCHAR(40) NOT NULL, |
|---|
| 137 | message BLOB NOT NULL |
|---|
| 138 | ) ENGINE=QUEUE DEFAULT CHARSET=utf8 |
|---|
| 139 | |
|---|
| 140 | CREATE TABLE q_timed ( |
|---|
| 141 | destination VARCHAR(40) NOT NULL, |
|---|
| 142 | ready BIGINT NOT NULL, |
|---|
| 143 | message BLOB NOT NULL |
|---|
| 144 | ) ENGINE=QUEUE DEFAULT CHARSET=utf8 |
|---|
| 145 | |
|---|
| 146 | CREATE TABLE q_incoming ( |
|---|
| 147 | destination VARCHAR(40) NOT NULL, |
|---|
| 148 | message BLOB NOT NULL |
|---|
| 149 | ) ENGINE=QUEUE DEFAULT CHARSET=utf8 |
|---|
| 150 | |
|---|
| 151 | You also need to setup a memcached compatible distributed cache/storage. |
|---|
| 152 | This will be used to share certain key data across multiple instances |
|---|
| 153 | of Mvalve. |
|---|
| 154 | |
|---|
| 155 | =head1 METHODS |
|---|
| 156 | |
|---|
| 157 | =head2 trace |
|---|
| 158 | |
|---|
| 159 | This is for debugging only |
|---|
| 160 | |
|---|
| 161 | =head1 AUTHORS |
|---|
| 162 | |
|---|
| 163 | Daisuke Maki C<< <daisuke@endeworks.jp> >> |
|---|
| 164 | |
|---|
| 165 | Taro Funaki C<< <t@33rpm.jp> >> |
|---|
| 166 | |
|---|
| 167 | =head1 LICENSE |
|---|
| 168 | |
|---|
| 169 | This program is free software; you can redistribute it and/or modify it |
|---|
| 170 | under the same terms as Perl itself. |
|---|
| 171 | |
|---|
| 172 | See http://www.perl.com/perl/misc/Artistic.html |
|---|
| 173 | |
|---|
| 174 | =cut |
|---|
| 175 | |
|---|