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