Commit | Line | Data |
2156bbdd |
1 | package DBIx::Class::Storage::DBI::Replicated; |
f5d3a5de |
2 | |
ecb65397 |
3 | BEGIN { |
4 | use Carp::Clan qw/^DBIx::Class/; |
d4daee7b |
5 | |
ecb65397 |
6 | ## Modules required for Replication support not required for general DBIC |
7 | ## use, so we explicitly test for these. |
d4daee7b |
8 | |
ecb65397 |
9 | my %replication_required = ( |
62eabf11 |
10 | 'Moose' => '0.87', |
450a317d |
11 | 'MooseX::AttributeHelpers' => '0.21', |
62eabf11 |
12 | 'MooseX::Types' => '0.16', |
bd5da369 |
13 | 'namespace::clean' => '0.11', |
14 | 'Hash::Merge' => '0.11' |
ecb65397 |
15 | ); |
d4daee7b |
16 | |
ecb65397 |
17 | my @didnt_load; |
d4daee7b |
18 | |
ecb65397 |
19 | for my $module (keys %replication_required) { |
1a58752c |
20 | eval "use $module $replication_required{$module}"; |
21 | push @didnt_load, "$module $replication_required{$module}" |
22 | if $@; |
ecb65397 |
23 | } |
d4daee7b |
24 | |
ecb65397 |
25 | croak("@{[ join ', ', @didnt_load ]} are missing and are required for Replication") |
d4daee7b |
26 | if @didnt_load; |
ecb65397 |
27 | } |
28 | |
b2e4d522 |
29 | use Moose; |
26ab719a |
30 | use DBIx::Class::Storage::DBI; |
2bf79155 |
31 | use DBIx::Class::Storage::DBI::Replicated::Pool; |
26ab719a |
32 | use DBIx::Class::Storage::DBI::Replicated::Balancer; |
6a151f58 |
33 | use DBIx::Class::Storage::DBI::Replicated::Types qw/BalancerClassNamePart DBICSchema DBICStorageDBI/; |
41916570 |
34 | use MooseX::Types::Moose qw/ClassName HashRef Object/; |
b2e4d522 |
35 | use Scalar::Util 'reftype'; |
b88b85e7 |
36 | use Hash::Merge 'merge'; |
9901aad7 |
37 | |
38 | use namespace::clean -except => 'meta'; |
2bf79155 |
39 | |
40 | =head1 NAME |
41 | |
ecb65397 |
42 | DBIx::Class::Storage::DBI::Replicated - BETA Replicated database support |
2bf79155 |
43 | |
44 | =head1 SYNOPSIS |
45 | |
46 | The Following example shows how to change an existing $schema to a replicated |
47 | storage type, add some replicated (readonly) databases, and perform reporting |
955a6df6 |
48 | tasks. |
2bf79155 |
49 | |
3da4f736 |
50 | You should set the 'storage_type attribute to a replicated type. You should |
d4daee7b |
51 | also define your arguments, such as which balancer you want and any arguments |
3da4f736 |
52 | that the Pool object should get. |
53 | |
64cdad22 |
54 | $schema->storage_type( ['::DBI::Replicated', {balancer=>'::Random'}] ); |
d4daee7b |
55 | |
3da4f736 |
56 | Next, you need to add in the Replicants. Basically this is an array of |
57 | arrayrefs, where each arrayref is database connect information. Think of these |
58 | arguments as what you'd pass to the 'normal' $schema->connect method. |
d4daee7b |
59 | |
64cdad22 |
60 | $schema->storage->connect_replicants( |
61 | [$dsn1, $user, $pass, \%opts], |
62 | [$dsn2, $user, $pass, \%opts], |
63 | [$dsn3, $user, $pass, \%opts], |
64 | ); |
d4daee7b |
65 | |
3da4f736 |
66 | Now, just use the $schema as you normally would. Automatically all reads will |
67 | be delegated to the replicants, while writes to the master. |
68 | |
7e38d850 |
69 | $schema->resultset('Source')->search({name=>'etc'}); |
d4daee7b |
70 | |
3da4f736 |
71 | You can force a given query to use a particular storage using the search |
72 | attribute 'force_pool'. For example: |
d4daee7b |
73 | |
7e38d850 |
74 | my $RS = $schema->resultset('Source')->search(undef, {force_pool=>'master'}); |
3da4f736 |
75 | |
76 | Now $RS will force everything (both reads and writes) to use whatever was setup |
77 | as the master storage. 'master' is hardcoded to always point to the Master, |
78 | but you can also use any Replicant name. Please see: |
212cc5c2 |
79 | L<DBIx::Class::Storage::DBI::Replicated::Pool> and the replicants attribute for more. |
3da4f736 |
80 | |
81 | Also see transactions and L</execute_reliably> for alternative ways to |
82 | force read traffic to the master. In general, you should wrap your statements |
83 | in a transaction when you are reading and writing to the same tables at the |
84 | same time, since your replicants will often lag a bit behind the master. |
212cc5c2 |
85 | |
86 | See L<DBIx::Class::Storage::DBI::Replicated::Instructions> for more help and |
87 | walkthroughs. |
d4daee7b |
88 | |
2bf79155 |
89 | =head1 DESCRIPTION |
90 | |
7e38d850 |
91 | Warning: This class is marked BETA. This has been running a production |
ccb3b897 |
92 | website using MySQL native replication as its backend and we have some decent |
7e38d850 |
93 | test coverage but the code hasn't yet been stressed by a variety of databases. |
94 | Individual DB's may have quirks we are not aware of. Please use this in first |
95 | development and pass along your experiences/bug fixes. |
2bf79155 |
96 | |
97 | This class implements replicated data store for DBI. Currently you can define |
98 | one master and numerous slave database connections. All write-type queries |
99 | (INSERT, UPDATE, DELETE and even LAST_INSERT_ID) are routed to master |
100 | database, all read-type queries (SELECTs) go to the slave database. |
101 | |
102 | Basically, any method request that L<DBIx::Class::Storage::DBI> would normally |
bca099a3 |
103 | handle gets delegated to one of the two attributes: L</read_handler> or to |
104 | L</write_handler>. Additionally, some methods need to be distributed |
2bf79155 |
105 | to all existing storages. This way our storage class is a drop in replacement |
106 | for L<DBIx::Class::Storage::DBI>. |
107 | |
108 | Read traffic is spread across the replicants (slaves) occuring to a user |
109 | selected algorithm. The default algorithm is random weighted. |
110 | |
bca099a3 |
111 | =head1 NOTES |
112 | |
113 | The consistancy betweeen master and replicants is database specific. The Pool |
faaba25f |
114 | gives you a method to validate its replicants, removing and replacing them |
7e38d850 |
115 | when they fail/pass predefined criteria. Please make careful use of the ways |
ecb65397 |
116 | to force a query to run against Master when needed. |
117 | |
118 | =head1 REQUIREMENTS |
119 | |
120 | Replicated Storage has additional requirements not currently part of L<DBIx::Class> |
121 | |
62eabf11 |
122 | Moose => '0.87', |
123 | MooseX::AttributeHelpers => '0.20', |
124 | MooseX::Types => '0.16', |
125 | namespace::clean => '0.11', |
126 | Hash::Merge => '0.11' |
d4daee7b |
127 | |
ecb65397 |
128 | You will need to install these modules manually via CPAN or make them part of the |
129 | Makefile for your distribution. |
2bf79155 |
130 | |
131 | =head1 ATTRIBUTES |
132 | |
133 | This class defines the following attributes. |
134 | |
2ce6e9a6 |
135 | =head2 schema |
136 | |
137 | The underlying L<DBIx::Class::Schema> object this storage is attaching |
138 | |
139 | =cut |
140 | |
141 | has 'schema' => ( |
142 | is=>'rw', |
6a151f58 |
143 | isa=>DBICSchema, |
2ce6e9a6 |
144 | weak_ref=>1, |
145 | required=>1, |
146 | ); |
147 | |
26ab719a |
148 | =head2 pool_type |
2bf79155 |
149 | |
26ab719a |
150 | Contains the classname which will instantiate the L</pool> object. Defaults |
151 | to: L<DBIx::Class::Storage::DBI::Replicated::Pool>. |
2bf79155 |
152 | |
153 | =cut |
154 | |
26ab719a |
155 | has 'pool_type' => ( |
dcdf7b2c |
156 | is=>'rw', |
41916570 |
157 | isa=>ClassName, |
2ce6e9a6 |
158 | default=>'DBIx::Class::Storage::DBI::Replicated::Pool', |
64cdad22 |
159 | handles=>{ |
160 | 'create_pool' => 'new', |
161 | }, |
2bf79155 |
162 | ); |
163 | |
f068a139 |
164 | =head2 pool_args |
165 | |
166 | Contains a hashref of initialized information to pass to the Balancer object. |
212cc5c2 |
167 | See L<DBIx::Class::Storage::DBI::Replicated::Pool> for available arguments. |
f068a139 |
168 | |
169 | =cut |
170 | |
171 | has 'pool_args' => ( |
dcdf7b2c |
172 | is=>'rw', |
41916570 |
173 | isa=>HashRef, |
64cdad22 |
174 | lazy=>1, |
64cdad22 |
175 | default=>sub { {} }, |
f068a139 |
176 | ); |
177 | |
178 | |
26ab719a |
179 | =head2 balancer_type |
2bf79155 |
180 | |
181 | The replication pool requires a balance class to provider the methods for |
182 | choose how to spread the query load across each replicant in the pool. |
183 | |
184 | =cut |
185 | |
26ab719a |
186 | has 'balancer_type' => ( |
dcdf7b2c |
187 | is=>'rw', |
9901aad7 |
188 | isa=>BalancerClassNamePart, |
2ce6e9a6 |
189 | coerce=>1, |
190 | required=>1, |
191 | default=> 'DBIx::Class::Storage::DBI::Replicated::Balancer::First', |
64cdad22 |
192 | handles=>{ |
193 | 'create_balancer' => 'new', |
194 | }, |
2bf79155 |
195 | ); |
196 | |
17b05c13 |
197 | =head2 balancer_args |
198 | |
199 | Contains a hashref of initialized information to pass to the Balancer object. |
212cc5c2 |
200 | See L<DBIx::Class::Storage::DBI::Replicated::Balancer> for available arguments. |
17b05c13 |
201 | |
202 | =cut |
203 | |
204 | has 'balancer_args' => ( |
dcdf7b2c |
205 | is=>'rw', |
41916570 |
206 | isa=>HashRef, |
64cdad22 |
207 | lazy=>1, |
208 | required=>1, |
209 | default=>sub { {} }, |
17b05c13 |
210 | ); |
211 | |
26ab719a |
212 | =head2 pool |
2bf79155 |
213 | |
26ab719a |
214 | Is a <DBIx::Class::Storage::DBI::Replicated::Pool> or derived class. This is a |
215 | container class for one or more replicated databases. |
2bf79155 |
216 | |
217 | =cut |
218 | |
26ab719a |
219 | has 'pool' => ( |
64cdad22 |
220 | is=>'ro', |
221 | isa=>'DBIx::Class::Storage::DBI::Replicated::Pool', |
222 | lazy_build=>1, |
223 | handles=>[qw/ |
6f7344b8 |
224 | connect_replicants |
64cdad22 |
225 | replicants |
226 | has_replicants |
227 | /], |
2bf79155 |
228 | ); |
229 | |
26ab719a |
230 | =head2 balancer |
2bf79155 |
231 | |
26ab719a |
232 | Is a <DBIx::Class::Storage::DBI::Replicated::Balancer> or derived class. This |
233 | is a class that takes a pool (<DBIx::Class::Storage::DBI::Replicated::Pool>) |
2bf79155 |
234 | |
26ab719a |
235 | =cut |
2bf79155 |
236 | |
26ab719a |
237 | has 'balancer' => ( |
dcdf7b2c |
238 | is=>'rw', |
64cdad22 |
239 | isa=>'DBIx::Class::Storage::DBI::Replicated::Balancer', |
240 | lazy_build=>1, |
241 | handles=>[qw/auto_validate_every/], |
26ab719a |
242 | ); |
2bf79155 |
243 | |
cb6ec758 |
244 | =head2 master |
245 | |
246 | The master defines the canonical state for a pool of connected databases. All |
247 | the replicants are expected to match this databases state. Thus, in a classic |
248 | Master / Slaves distributed system, all the slaves are expected to replicate |
249 | the Master's state as quick as possible. This is the only database in the |
250 | pool of databases that is allowed to handle write traffic. |
251 | |
252 | =cut |
253 | |
254 | has 'master' => ( |
64cdad22 |
255 | is=> 'ro', |
6a151f58 |
256 | isa=>DBICStorageDBI, |
64cdad22 |
257 | lazy_build=>1, |
cb6ec758 |
258 | ); |
259 | |
cb6ec758 |
260 | =head1 ATTRIBUTES IMPLEMENTING THE DBIx::Storage::DBI INTERFACE |
261 | |
262 | The following methods are delegated all the methods required for the |
263 | L<DBIx::Class::Storage::DBI> interface. |
264 | |
265 | =head2 read_handler |
266 | |
267 | Defines an object that implements the read side of L<BIx::Class::Storage::DBI>. |
268 | |
269 | =cut |
270 | |
271 | has 'read_handler' => ( |
64cdad22 |
272 | is=>'rw', |
41916570 |
273 | isa=>Object, |
64cdad22 |
274 | lazy_build=>1, |
275 | handles=>[qw/ |
276 | select |
277 | select_single |
278 | columns_info_for |
6f7344b8 |
279 | /], |
cb6ec758 |
280 | ); |
281 | |
cb6ec758 |
282 | =head2 write_handler |
283 | |
284 | Defines an object that implements the write side of L<BIx::Class::Storage::DBI>. |
285 | |
286 | =cut |
287 | |
288 | has 'write_handler' => ( |
64cdad22 |
289 | is=>'ro', |
41916570 |
290 | isa=>Object, |
64cdad22 |
291 | lazy_build=>1, |
6f7344b8 |
292 | handles=>[qw/ |
64cdad22 |
293 | on_connect_do |
6f7344b8 |
294 | on_disconnect_do |
64cdad22 |
295 | connect_info |
296 | throw_exception |
297 | sql_maker |
298 | sqlt_type |
299 | create_ddl_dir |
300 | deployment_statements |
301 | datetime_parser |
6f7344b8 |
302 | datetime_parser_type |
303 | build_datetime_parser |
64cdad22 |
304 | last_insert_id |
305 | insert |
306 | insert_bulk |
307 | update |
308 | delete |
309 | dbh |
2ce6e9a6 |
310 | txn_begin |
64cdad22 |
311 | txn_do |
312 | txn_commit |
313 | txn_rollback |
2ce6e9a6 |
314 | txn_scope_guard |
64cdad22 |
315 | sth |
316 | deploy |
0180bef9 |
317 | with_deferred_fk_checks |
6f7344b8 |
318 | dbh_do |
64cdad22 |
319 | reload_row |
6f7344b8 |
320 | with_deferred_fk_checks |
2ce6e9a6 |
321 | _prep_for_execute |
7fb60fb1 |
322 | |
6f7344b8 |
323 | backup |
324 | is_datatype_numeric |
325 | _count_select |
326 | _subq_count_select |
327 | _subq_update_delete |
d867eeda |
328 | _order_select_columns |
6f7344b8 |
329 | svp_rollback |
330 | svp_begin |
331 | svp_release |
64cdad22 |
332 | /], |
cb6ec758 |
333 | ); |
334 | |
b2e4d522 |
335 | has _master_connect_info_opts => |
336 | (is => 'rw', isa => HashRef, default => sub { {} }); |
337 | |
338 | =head2 around: connect_info |
339 | |
340 | Preserve master's C<connect_info> options (for merging with replicants.) |
dcdf7b2c |
341 | Also set any Replicated related options from connect_info, such as |
342 | C<pool_type>, C<pool_args>, C<balancer_type> and C<balancer_args>. |
b2e4d522 |
343 | |
344 | =cut |
345 | |
346 | around connect_info => sub { |
347 | my ($next, $self, $info, @extra) = @_; |
348 | |
0ce2d0d5 |
349 | my $wantarray = wantarray; |
350 | |
b2e4d522 |
351 | my %opts; |
352 | for my $arg (@$info) { |
353 | next unless (reftype($arg)||'') eq 'HASH'; |
b88b85e7 |
354 | %opts = %{ merge($arg, \%opts) }; |
b2e4d522 |
355 | } |
b2e4d522 |
356 | delete $opts{dsn}; |
357 | |
dcdf7b2c |
358 | if (@opts{qw/pool_type pool_args/}) { |
359 | $self->pool_type(delete $opts{pool_type}) |
360 | if $opts{pool_type}; |
361 | |
b88b85e7 |
362 | $self->pool_args( |
363 | merge((delete $opts{pool_args} || {}), $self->pool_args) |
364 | ); |
dcdf7b2c |
365 | |
67c43863 |
366 | $self->pool($self->_build_pool) |
6f7344b8 |
367 | if $self->pool; |
dcdf7b2c |
368 | } |
369 | |
370 | if (@opts{qw/balancer_type balancer_args/}) { |
371 | $self->balancer_type(delete $opts{balancer_type}) |
372 | if $opts{balancer_type}; |
373 | |
b88b85e7 |
374 | $self->balancer_args( |
375 | merge((delete $opts{balancer_args} || {}), $self->balancer_args) |
376 | ); |
dcdf7b2c |
377 | |
67c43863 |
378 | $self->balancer($self->_build_balancer) |
6f7344b8 |
379 | if $self->balancer; |
dcdf7b2c |
380 | } |
381 | |
b2e4d522 |
382 | $self->_master_connect_info_opts(\%opts); |
383 | |
0ce2d0d5 |
384 | my (@res, $res); |
385 | if ($wantarray) { |
386 | @res = $self->$next($info, @extra); |
387 | } else { |
388 | $res = $self->$next($info, @extra); |
389 | } |
390 | |
fd4eb9c2 |
391 | # Make sure master is blessed into the correct class and apply role to it. |
392 | my $master = $self->master; |
393 | $master->_determine_driver; |
394 | Moose::Meta::Class->initialize(ref $master); |
395 | DBIx::Class::Storage::DBI::Replicated::WithDSN->meta->apply($master); |
0ce2d0d5 |
396 | |
397 | $wantarray ? @res : $res; |
b2e4d522 |
398 | }; |
399 | |
26ab719a |
400 | =head1 METHODS |
2bf79155 |
401 | |
26ab719a |
402 | This class defines the following methods. |
2bf79155 |
403 | |
c354902c |
404 | =head2 BUILDARGS |
2bf79155 |
405 | |
faaba25f |
406 | L<DBIx::Class::Schema> when instantiating its storage passed itself as the |
2ce6e9a6 |
407 | first argument. So we need to massage the arguments a bit so that all the |
408 | bits get put into the correct places. |
2bf79155 |
409 | |
410 | =cut |
411 | |
c354902c |
412 | sub BUILDARGS { |
413 | my ($class, $schema, $storage_type_args, @args) = @_; |
d4daee7b |
414 | |
c354902c |
415 | return { |
6f7344b8 |
416 | schema=>$schema, |
417 | %$storage_type_args, |
418 | @args |
c354902c |
419 | } |
420 | } |
2bf79155 |
421 | |
cb6ec758 |
422 | =head2 _build_master |
2bf79155 |
423 | |
cb6ec758 |
424 | Lazy builder for the L</master> attribute. |
2bf79155 |
425 | |
426 | =cut |
427 | |
cb6ec758 |
428 | sub _build_master { |
2ce6e9a6 |
429 | my $self = shift @_; |
ee356d00 |
430 | my $master = DBIx::Class::Storage::DBI->new($self->schema); |
ee356d00 |
431 | $master |
106d5f3b |
432 | } |
433 | |
26ab719a |
434 | =head2 _build_pool |
2bf79155 |
435 | |
26ab719a |
436 | Lazy builder for the L</pool> attribute. |
2bf79155 |
437 | |
438 | =cut |
439 | |
26ab719a |
440 | sub _build_pool { |
64cdad22 |
441 | my $self = shift @_; |
442 | $self->create_pool(%{$self->pool_args}); |
2bf79155 |
443 | } |
444 | |
26ab719a |
445 | =head2 _build_balancer |
2bf79155 |
446 | |
cb6ec758 |
447 | Lazy builder for the L</balancer> attribute. This takes a Pool object so that |
448 | the balancer knows which pool it's balancing. |
2bf79155 |
449 | |
450 | =cut |
451 | |
26ab719a |
452 | sub _build_balancer { |
64cdad22 |
453 | my $self = shift @_; |
454 | $self->create_balancer( |
6f7344b8 |
455 | pool=>$self->pool, |
64cdad22 |
456 | master=>$self->master, |
457 | %{$self->balancer_args}, |
458 | ); |
2bf79155 |
459 | } |
460 | |
cb6ec758 |
461 | =head2 _build_write_handler |
2bf79155 |
462 | |
cb6ec758 |
463 | Lazy builder for the L</write_handler> attribute. The default is to set this to |
464 | the L</master>. |
50336325 |
465 | |
466 | =cut |
467 | |
cb6ec758 |
468 | sub _build_write_handler { |
64cdad22 |
469 | return shift->master; |
cb6ec758 |
470 | } |
50336325 |
471 | |
cb6ec758 |
472 | =head2 _build_read_handler |
2bf79155 |
473 | |
cb6ec758 |
474 | Lazy builder for the L</read_handler> attribute. The default is to set this to |
475 | the L</balancer>. |
2bf79155 |
476 | |
477 | =cut |
478 | |
cb6ec758 |
479 | sub _build_read_handler { |
64cdad22 |
480 | return shift->balancer; |
cb6ec758 |
481 | } |
50336325 |
482 | |
cb6ec758 |
483 | =head2 around: connect_replicants |
2bf79155 |
484 | |
cb6ec758 |
485 | All calls to connect_replicants needs to have an existing $schema tacked onto |
b2e4d522 |
486 | top of the args, since L<DBIx::Storage::DBI> needs it, and any C<connect_info> |
487 | options merged with the master, with replicant opts having higher priority. |
955a6df6 |
488 | |
cb6ec758 |
489 | =cut |
955a6df6 |
490 | |
b2e4d522 |
491 | around connect_replicants => sub { |
492 | my ($next, $self, @args) = @_; |
493 | |
494 | for my $r (@args) { |
495 | $r = [ $r ] unless reftype $r eq 'ARRAY'; |
496 | |
1a58752c |
497 | $self->throw_exception('coderef replicant connect_info not supported') |
b2e4d522 |
498 | if ref $r->[0] && reftype $r->[0] eq 'CODE'; |
499 | |
500 | # any connect_info options? |
501 | my $i = 0; |
502 | $i++ while $i < @$r && (reftype($r->[$i])||'') ne 'HASH'; |
503 | |
6f7344b8 |
504 | # make one if none |
b2e4d522 |
505 | $r->[$i] = {} unless $r->[$i]; |
506 | |
507 | # merge if two hashes |
b88b85e7 |
508 | my @hashes = @$r[$i .. $#{$r}]; |
509 | |
1a58752c |
510 | $self->throw_exception('invalid connect_info options') |
b88b85e7 |
511 | if (grep { reftype($_) eq 'HASH' } @hashes) != @hashes; |
512 | |
1a58752c |
513 | $self->throw_exception('too many hashrefs in connect_info') |
b88b85e7 |
514 | if @hashes > 2; |
515 | |
516 | my %opts = %{ merge(reverse @hashes) }; |
517 | |
518 | # delete them |
b2e4d522 |
519 | splice @$r, $i+1, ($#{$r} - $i), (); |
520 | |
0bd8e058 |
521 | # make sure master/replicants opts don't clash |
522 | my %master_opts = %{ $self->_master_connect_info_opts }; |
523 | if (exists $opts{dbh_maker}) { |
524 | delete @master_opts{qw/dsn user password/}; |
525 | } |
526 | delete $master_opts{dbh_maker}; |
527 | |
b2e4d522 |
528 | # merge with master |
0bd8e058 |
529 | %opts = %{ merge(\%opts, \%master_opts) }; |
b2e4d522 |
530 | |
531 | # update |
532 | $r->[$i] = \%opts; |
533 | } |
534 | |
535 | $self->$next($self->schema, @args); |
955a6df6 |
536 | }; |
2bf79155 |
537 | |
2bf79155 |
538 | =head2 all_storages |
539 | |
540 | Returns an array of of all the connected storage backends. The first element |
541 | in the returned array is the master, and the remainings are each of the |
542 | replicants. |
543 | |
544 | =cut |
545 | |
546 | sub all_storages { |
64cdad22 |
547 | my $self = shift @_; |
548 | return grep {defined $_ && blessed $_} ( |
549 | $self->master, |
6412a592 |
550 | values %{ $self->replicants }, |
64cdad22 |
551 | ); |
2bf79155 |
552 | } |
553 | |
c4d3fae2 |
554 | =head2 execute_reliably ($coderef, ?@args) |
555 | |
556 | Given a coderef, saves the current state of the L</read_handler>, forces it to |
557 | use reliable storage (ie sets it to the master), executes a coderef and then |
558 | restores the original state. |
559 | |
560 | Example: |
561 | |
64cdad22 |
562 | my $reliably = sub { |
563 | my $name = shift @_; |
564 | $schema->resultset('User')->create({name=>$name}); |
565 | my $user_rs = $schema->resultset('User')->find({name=>$name}); |
566 | return $user_rs; |
567 | }; |
c4d3fae2 |
568 | |
64cdad22 |
569 | my $user_rs = $schema->storage->execute_reliably($reliably, 'John'); |
c4d3fae2 |
570 | |
571 | Use this when you must be certain of your database state, such as when you just |
572 | inserted something and need to get a resultset including it, etc. |
573 | |
574 | =cut |
575 | |
576 | sub execute_reliably { |
64cdad22 |
577 | my ($self, $coderef, @args) = @_; |
d4daee7b |
578 | |
64cdad22 |
579 | unless( ref $coderef eq 'CODE') { |
580 | $self->throw_exception('Second argument must be a coderef'); |
581 | } |
d4daee7b |
582 | |
64cdad22 |
583 | ##Get copy of master storage |
584 | my $master = $self->master; |
d4daee7b |
585 | |
64cdad22 |
586 | ##Get whatever the current read hander is |
587 | my $current = $self->read_handler; |
d4daee7b |
588 | |
64cdad22 |
589 | ##Set the read handler to master |
590 | $self->read_handler($master); |
d4daee7b |
591 | |
64cdad22 |
592 | ## do whatever the caller needs |
593 | my @result; |
594 | my $want_array = wantarray; |
d4daee7b |
595 | |
64cdad22 |
596 | eval { |
597 | if($want_array) { |
598 | @result = $coderef->(@args); |
599 | } elsif(defined $want_array) { |
600 | ($result[0]) = ($coderef->(@args)); |
ed213e85 |
601 | } else { |
64cdad22 |
602 | $coderef->(@args); |
6f7344b8 |
603 | } |
64cdad22 |
604 | }; |
d4daee7b |
605 | |
64cdad22 |
606 | ##Reset to the original state |
6f7344b8 |
607 | $self->read_handler($current); |
d4daee7b |
608 | |
64cdad22 |
609 | ##Exception testing has to come last, otherwise you might leave the |
610 | ##read_handler set to master. |
d4daee7b |
611 | |
64cdad22 |
612 | if($@) { |
613 | $self->throw_exception("coderef returned an error: $@"); |
614 | } else { |
615 | return $want_array ? @result : $result[0]; |
616 | } |
c4d3fae2 |
617 | } |
618 | |
cb6ec758 |
619 | =head2 set_reliable_storage |
620 | |
621 | Sets the current $schema to be 'reliable', that is all queries, both read and |
622 | write are sent to the master |
d4daee7b |
623 | |
cb6ec758 |
624 | =cut |
625 | |
626 | sub set_reliable_storage { |
64cdad22 |
627 | my $self = shift @_; |
628 | my $schema = $self->schema; |
629 | my $write_handler = $self->schema->storage->write_handler; |
d4daee7b |
630 | |
64cdad22 |
631 | $schema->storage->read_handler($write_handler); |
cb6ec758 |
632 | } |
633 | |
634 | =head2 set_balanced_storage |
635 | |
636 | Sets the current $schema to be use the </balancer> for all reads, while all |
637 | writea are sent to the master only |
d4daee7b |
638 | |
cb6ec758 |
639 | =cut |
640 | |
641 | sub set_balanced_storage { |
64cdad22 |
642 | my $self = shift @_; |
643 | my $schema = $self->schema; |
bd5da369 |
644 | my $balanced_handler = $self->schema->storage->balancer; |
d4daee7b |
645 | |
bd5da369 |
646 | $schema->storage->read_handler($balanced_handler); |
cb6ec758 |
647 | } |
2bf79155 |
648 | |
649 | =head2 connected |
650 | |
651 | Check that the master and at least one of the replicants is connected. |
652 | |
653 | =cut |
654 | |
655 | sub connected { |
64cdad22 |
656 | my $self = shift @_; |
657 | return |
658 | $self->master->connected && |
659 | $self->pool->connected_replicants; |
2bf79155 |
660 | } |
661 | |
2bf79155 |
662 | =head2 ensure_connected |
663 | |
664 | Make sure all the storages are connected. |
665 | |
666 | =cut |
667 | |
668 | sub ensure_connected { |
64cdad22 |
669 | my $self = shift @_; |
670 | foreach my $source ($self->all_storages) { |
671 | $source->ensure_connected(@_); |
672 | } |
2bf79155 |
673 | } |
674 | |
2bf79155 |
675 | =head2 limit_dialect |
676 | |
677 | Set the limit_dialect for all existing storages |
678 | |
679 | =cut |
680 | |
681 | sub limit_dialect { |
64cdad22 |
682 | my $self = shift @_; |
683 | foreach my $source ($self->all_storages) { |
684 | $source->limit_dialect(@_); |
685 | } |
3fbe08e3 |
686 | return $self->master->quote_char; |
2bf79155 |
687 | } |
688 | |
2bf79155 |
689 | =head2 quote_char |
690 | |
691 | Set the quote_char for all existing storages |
692 | |
693 | =cut |
694 | |
695 | sub quote_char { |
64cdad22 |
696 | my $self = shift @_; |
697 | foreach my $source ($self->all_storages) { |
698 | $source->quote_char(@_); |
699 | } |
3fbe08e3 |
700 | return $self->master->quote_char; |
2bf79155 |
701 | } |
702 | |
2bf79155 |
703 | =head2 name_sep |
704 | |
705 | Set the name_sep for all existing storages |
706 | |
707 | =cut |
708 | |
709 | sub name_sep { |
64cdad22 |
710 | my $self = shift @_; |
711 | foreach my $source ($self->all_storages) { |
712 | $source->name_sep(@_); |
713 | } |
3fbe08e3 |
714 | return $self->master->name_sep; |
2bf79155 |
715 | } |
716 | |
2bf79155 |
717 | =head2 set_schema |
718 | |
719 | Set the schema object for all existing storages |
720 | |
721 | =cut |
722 | |
723 | sub set_schema { |
64cdad22 |
724 | my $self = shift @_; |
725 | foreach my $source ($self->all_storages) { |
726 | $source->set_schema(@_); |
727 | } |
2bf79155 |
728 | } |
729 | |
2bf79155 |
730 | =head2 debug |
731 | |
732 | set a debug flag across all storages |
733 | |
734 | =cut |
735 | |
736 | sub debug { |
64cdad22 |
737 | my $self = shift @_; |
3fbe08e3 |
738 | if(@_) { |
739 | foreach my $source ($self->all_storages) { |
740 | $source->debug(@_); |
6f7344b8 |
741 | } |
64cdad22 |
742 | } |
3fbe08e3 |
743 | return $self->master->debug; |
2bf79155 |
744 | } |
745 | |
2bf79155 |
746 | =head2 debugobj |
747 | |
748 | set a debug object across all storages |
749 | |
750 | =cut |
751 | |
752 | sub debugobj { |
64cdad22 |
753 | my $self = shift @_; |
3fbe08e3 |
754 | if(@_) { |
755 | foreach my $source ($self->all_storages) { |
756 | $source->debugobj(@_); |
6f7344b8 |
757 | } |
64cdad22 |
758 | } |
3fbe08e3 |
759 | return $self->master->debugobj; |
2bf79155 |
760 | } |
761 | |
2bf79155 |
762 | =head2 debugfh |
763 | |
764 | set a debugfh object across all storages |
765 | |
766 | =cut |
767 | |
768 | sub debugfh { |
64cdad22 |
769 | my $self = shift @_; |
3fbe08e3 |
770 | if(@_) { |
771 | foreach my $source ($self->all_storages) { |
772 | $source->debugfh(@_); |
6f7344b8 |
773 | } |
64cdad22 |
774 | } |
3fbe08e3 |
775 | return $self->master->debugfh; |
2bf79155 |
776 | } |
777 | |
2bf79155 |
778 | =head2 debugcb |
779 | |
780 | set a debug callback across all storages |
781 | |
782 | =cut |
783 | |
784 | sub debugcb { |
64cdad22 |
785 | my $self = shift @_; |
3fbe08e3 |
786 | if(@_) { |
787 | foreach my $source ($self->all_storages) { |
788 | $source->debugcb(@_); |
6f7344b8 |
789 | } |
64cdad22 |
790 | } |
3fbe08e3 |
791 | return $self->master->debugcb; |
2bf79155 |
792 | } |
793 | |
2bf79155 |
794 | =head2 disconnect |
795 | |
796 | disconnect everything |
797 | |
798 | =cut |
799 | |
800 | sub disconnect { |
64cdad22 |
801 | my $self = shift @_; |
802 | foreach my $source ($self->all_storages) { |
803 | $source->disconnect(@_); |
804 | } |
2bf79155 |
805 | } |
806 | |
b2e4d522 |
807 | =head2 cursor_class |
808 | |
809 | set cursor class on all storages, or return master's |
810 | |
811 | =cut |
812 | |
813 | sub cursor_class { |
814 | my ($self, $cursor_class) = @_; |
815 | |
816 | if ($cursor_class) { |
817 | $_->cursor_class($cursor_class) for $self->all_storages; |
818 | } |
819 | $self->master->cursor_class; |
820 | } |
d4daee7b |
821 | |
7e38d850 |
822 | =head1 GOTCHAS |
823 | |
824 | Due to the fact that replicants can lag behind a master, you must take care to |
825 | make sure you use one of the methods to force read queries to a master should |
826 | you need realtime data integrity. For example, if you insert a row, and then |
827 | immediately re-read it from the database (say, by doing $row->discard_changes) |
828 | or you insert a row and then immediately build a query that expects that row |
829 | to be an item, you should force the master to handle reads. Otherwise, due to |
830 | the lag, there is no certainty your data will be in the expected state. |
831 | |
832 | For data integrity, all transactions automatically use the master storage for |
833 | all read and write queries. Using a transaction is the preferred and recommended |
834 | method to force the master to handle all read queries. |
835 | |
836 | Otherwise, you can force a single query to use the master with the 'force_pool' |
837 | attribute: |
838 | |
839 | my $row = $resultset->search(undef, {force_pool=>'master'})->find($pk); |
840 | |
841 | This attribute will safely be ignore by non replicated storages, so you can use |
842 | the same code for both types of systems. |
843 | |
844 | Lastly, you can use the L</execute_reliably> method, which works very much like |
845 | a transaction. |
846 | |
847 | For debugging, you can turn replication on/off with the methods L</set_reliable_storage> |
848 | and L</set_balanced_storage>, however this operates at a global level and is not |
849 | suitable if you have a shared Schema object being used by multiple processes, |
850 | such as on a web application server. You can get around this limitation by |
851 | using the Schema clone method. |
852 | |
853 | my $new_schema = $schema->clone; |
854 | $new_schema->set_reliable_storage; |
d4daee7b |
855 | |
7e38d850 |
856 | ## $new_schema will use only the Master storage for all reads/writes while |
857 | ## the $schema object will use replicated storage. |
858 | |
f5d3a5de |
859 | =head1 AUTHOR |
860 | |
64cdad22 |
861 | John Napiorkowski <john.napiorkowski@takkle.com> |
f5d3a5de |
862 | |
c4d3fae2 |
863 | Based on code originated by: |
f5d3a5de |
864 | |
64cdad22 |
865 | Norbert Csongrádi <bert@cpan.org> |
866 | Peter Siklósi <einon@einon.hu> |
2156bbdd |
867 | |
f5d3a5de |
868 | =head1 LICENSE |
869 | |
870 | You may distribute this code under the same terms as Perl itself. |
871 | |
872 | =cut |
873 | |
c354902c |
874 | __PACKAGE__->meta->make_immutable; |
875 | |
f5d3a5de |
876 | 1; |