workaround for Moose bug affecting Replicated storage
[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 = (
c4d78acb 10 'Moose' => '0.90',
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
c4d78acb 123 Moose => '0.90',
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
64cdad22 331 /],
cb6ec758 332);
333
b2e4d522 334has _master_connect_info_opts =>
335 (is => 'rw', isa => HashRef, default => sub { {} });
336
337=head2 around: connect_info
338
339Preserve master's C<connect_info> options (for merging with replicants.)
dcdf7b2c 340Also set any Replicated related options from connect_info, such as
341C<pool_type>, C<pool_args>, C<balancer_type> and C<balancer_args>.
b2e4d522 342
343=cut
344
345around connect_info => sub {
346 my ($next, $self, $info, @extra) = @_;
347
0ce2d0d5 348 my $wantarray = wantarray;
349
b2e4d522 350 my %opts;
351 for my $arg (@$info) {
352 next unless (reftype($arg)||'') eq 'HASH';
b88b85e7 353 %opts = %{ merge($arg, \%opts) };
b2e4d522 354 }
b2e4d522 355 delete $opts{dsn};
356
dcdf7b2c 357 if (@opts{qw/pool_type pool_args/}) {
358 $self->pool_type(delete $opts{pool_type})
359 if $opts{pool_type};
360
b88b85e7 361 $self->pool_args(
362 merge((delete $opts{pool_args} || {}), $self->pool_args)
363 );
dcdf7b2c 364
67c43863 365 $self->pool($self->_build_pool)
6f7344b8 366 if $self->pool;
dcdf7b2c 367 }
368
369 if (@opts{qw/balancer_type balancer_args/}) {
370 $self->balancer_type(delete $opts{balancer_type})
371 if $opts{balancer_type};
372
b88b85e7 373 $self->balancer_args(
374 merge((delete $opts{balancer_args} || {}), $self->balancer_args)
375 );
dcdf7b2c 376
67c43863 377 $self->balancer($self->_build_balancer)
6f7344b8 378 if $self->balancer;
dcdf7b2c 379 }
380
b2e4d522 381 $self->_master_connect_info_opts(\%opts);
382
0ce2d0d5 383 my (@res, $res);
384 if ($wantarray) {
385 @res = $self->$next($info, @extra);
386 } else {
387 $res = $self->$next($info, @extra);
388 }
389
fd4eb9c2 390 # Make sure master is blessed into the correct class and apply role to it.
391 my $master = $self->master;
392 $master->_determine_driver;
393 Moose::Meta::Class->initialize(ref $master);
cea43436 394
395 my $class = Moose::Meta::Class->create_anon_class(
396 superclasses => [ ref $master ],
397 roles => [ 'DBIx::Class::Storage::DBI::Replicated::WithDSN' ],
398 cache => 1,
399 );
400 $class->rebless_instance($master);
401
402 # link pool back to master
403 $self->pool->master($master);
0ce2d0d5 404
405 $wantarray ? @res : $res;
b2e4d522 406};
407
26ab719a 408=head1 METHODS
2bf79155 409
26ab719a 410This class defines the following methods.
2bf79155 411
c354902c 412=head2 BUILDARGS
2bf79155 413
faaba25f 414L<DBIx::Class::Schema> when instantiating its storage passed itself as the
2ce6e9a6 415first argument. So we need to massage the arguments a bit so that all the
416bits get put into the correct places.
2bf79155 417
418=cut
419
c354902c 420sub BUILDARGS {
d7a58a29 421 my ($class, $schema, $storage_type_args, @args) = @_;
d4daee7b 422
c354902c 423 return {
6f7344b8 424 schema=>$schema,
425 %$storage_type_args,
426 @args
c354902c 427 }
428}
2bf79155 429
cb6ec758 430=head2 _build_master
2bf79155 431
cb6ec758 432Lazy builder for the L</master> attribute.
2bf79155 433
434=cut
435
cb6ec758 436sub _build_master {
2ce6e9a6 437 my $self = shift @_;
ee356d00 438 my $master = DBIx::Class::Storage::DBI->new($self->schema);
ee356d00 439 $master
106d5f3b 440}
441
26ab719a 442=head2 _build_pool
2bf79155 443
26ab719a 444Lazy builder for the L</pool> attribute.
2bf79155 445
446=cut
447
26ab719a 448sub _build_pool {
64cdad22 449 my $self = shift @_;
450 $self->create_pool(%{$self->pool_args});
2bf79155 451}
452
26ab719a 453=head2 _build_balancer
2bf79155 454
cb6ec758 455Lazy builder for the L</balancer> attribute. This takes a Pool object so that
456the balancer knows which pool it's balancing.
2bf79155 457
458=cut
459
26ab719a 460sub _build_balancer {
64cdad22 461 my $self = shift @_;
462 $self->create_balancer(
6f7344b8 463 pool=>$self->pool,
64cdad22 464 master=>$self->master,
465 %{$self->balancer_args},
466 );
2bf79155 467}
468
cb6ec758 469=head2 _build_write_handler
2bf79155 470
cb6ec758 471Lazy builder for the L</write_handler> attribute. The default is to set this to
472the L</master>.
50336325 473
474=cut
475
cb6ec758 476sub _build_write_handler {
64cdad22 477 return shift->master;
cb6ec758 478}
50336325 479
cb6ec758 480=head2 _build_read_handler
2bf79155 481
cb6ec758 482Lazy builder for the L</read_handler> attribute. The default is to set this to
483the L</balancer>.
2bf79155 484
485=cut
486
cb6ec758 487sub _build_read_handler {
64cdad22 488 return shift->balancer;
cb6ec758 489}
50336325 490
cb6ec758 491=head2 around: connect_replicants
2bf79155 492
cb6ec758 493All calls to connect_replicants needs to have an existing $schema tacked onto
b2e4d522 494top of the args, since L<DBIx::Storage::DBI> needs it, and any C<connect_info>
495options merged with the master, with replicant opts having higher priority.
955a6df6 496
cb6ec758 497=cut
955a6df6 498
b2e4d522 499around connect_replicants => sub {
500 my ($next, $self, @args) = @_;
501
502 for my $r (@args) {
503 $r = [ $r ] unless reftype $r eq 'ARRAY';
504
1a58752c 505 $self->throw_exception('coderef replicant connect_info not supported')
b2e4d522 506 if ref $r->[0] && reftype $r->[0] eq 'CODE';
507
508# any connect_info options?
509 my $i = 0;
510 $i++ while $i < @$r && (reftype($r->[$i])||'') ne 'HASH';
511
6f7344b8 512# make one if none
b2e4d522 513 $r->[$i] = {} unless $r->[$i];
514
515# merge if two hashes
b88b85e7 516 my @hashes = @$r[$i .. $#{$r}];
517
1a58752c 518 $self->throw_exception('invalid connect_info options')
b88b85e7 519 if (grep { reftype($_) eq 'HASH' } @hashes) != @hashes;
520
1a58752c 521 $self->throw_exception('too many hashrefs in connect_info')
b88b85e7 522 if @hashes > 2;
523
524 my %opts = %{ merge(reverse @hashes) };
525
526# delete them
b2e4d522 527 splice @$r, $i+1, ($#{$r} - $i), ();
528
0bd8e058 529# make sure master/replicants opts don't clash
530 my %master_opts = %{ $self->_master_connect_info_opts };
531 if (exists $opts{dbh_maker}) {
532 delete @master_opts{qw/dsn user password/};
533 }
534 delete $master_opts{dbh_maker};
535
b2e4d522 536# merge with master
0bd8e058 537 %opts = %{ merge(\%opts, \%master_opts) };
b2e4d522 538
539# update
540 $r->[$i] = \%opts;
541 }
542
543 $self->$next($self->schema, @args);
955a6df6 544};
2bf79155 545
2bf79155 546=head2 all_storages
547
548Returns an array of of all the connected storage backends. The first element
549in the returned array is the master, and the remainings are each of the
550replicants.
551
552=cut
553
554sub all_storages {
64cdad22 555 my $self = shift @_;
556 return grep {defined $_ && blessed $_} (
557 $self->master,
6412a592 558 values %{ $self->replicants },
64cdad22 559 );
2bf79155 560}
561
c4d3fae2 562=head2 execute_reliably ($coderef, ?@args)
563
564Given a coderef, saves the current state of the L</read_handler>, forces it to
565use reliable storage (ie sets it to the master), executes a coderef and then
566restores the original state.
567
568Example:
569
64cdad22 570 my $reliably = sub {
571 my $name = shift @_;
572 $schema->resultset('User')->create({name=>$name});
573 my $user_rs = $schema->resultset('User')->find({name=>$name});
574 return $user_rs;
575 };
c4d3fae2 576
64cdad22 577 my $user_rs = $schema->storage->execute_reliably($reliably, 'John');
c4d3fae2 578
579Use this when you must be certain of your database state, such as when you just
580inserted something and need to get a resultset including it, etc.
581
582=cut
583
584sub execute_reliably {
64cdad22 585 my ($self, $coderef, @args) = @_;
d4daee7b 586
64cdad22 587 unless( ref $coderef eq 'CODE') {
588 $self->throw_exception('Second argument must be a coderef');
589 }
d4daee7b 590
64cdad22 591 ##Get copy of master storage
592 my $master = $self->master;
d4daee7b 593
64cdad22 594 ##Get whatever the current read hander is
595 my $current = $self->read_handler;
d4daee7b 596
64cdad22 597 ##Set the read handler to master
598 $self->read_handler($master);
d4daee7b 599
64cdad22 600 ## do whatever the caller needs
601 my @result;
602 my $want_array = wantarray;
d4daee7b 603
64cdad22 604 eval {
605 if($want_array) {
606 @result = $coderef->(@args);
607 } elsif(defined $want_array) {
608 ($result[0]) = ($coderef->(@args));
ed213e85 609 } else {
64cdad22 610 $coderef->(@args);
6f7344b8 611 }
64cdad22 612 };
d4daee7b 613
64cdad22 614 ##Reset to the original state
6f7344b8 615 $self->read_handler($current);
d4daee7b 616
64cdad22 617 ##Exception testing has to come last, otherwise you might leave the
618 ##read_handler set to master.
d4daee7b 619
64cdad22 620 if($@) {
621 $self->throw_exception("coderef returned an error: $@");
622 } else {
623 return $want_array ? @result : $result[0];
624 }
c4d3fae2 625}
626
cb6ec758 627=head2 set_reliable_storage
628
629Sets the current $schema to be 'reliable', that is all queries, both read and
630write are sent to the master
d4daee7b 631
cb6ec758 632=cut
633
634sub set_reliable_storage {
64cdad22 635 my $self = shift @_;
636 my $schema = $self->schema;
637 my $write_handler = $self->schema->storage->write_handler;
d4daee7b 638
64cdad22 639 $schema->storage->read_handler($write_handler);
cb6ec758 640}
641
642=head2 set_balanced_storage
643
644Sets the current $schema to be use the </balancer> for all reads, while all
645writea are sent to the master only
d4daee7b 646
cb6ec758 647=cut
648
649sub set_balanced_storage {
64cdad22 650 my $self = shift @_;
651 my $schema = $self->schema;
bd5da369 652 my $balanced_handler = $self->schema->storage->balancer;
d4daee7b 653
bd5da369 654 $schema->storage->read_handler($balanced_handler);
cb6ec758 655}
2bf79155 656
657=head2 connected
658
659Check that the master and at least one of the replicants is connected.
660
661=cut
662
663sub connected {
64cdad22 664 my $self = shift @_;
665 return
666 $self->master->connected &&
667 $self->pool->connected_replicants;
2bf79155 668}
669
2bf79155 670=head2 ensure_connected
671
672Make sure all the storages are connected.
673
674=cut
675
676sub ensure_connected {
64cdad22 677 my $self = shift @_;
678 foreach my $source ($self->all_storages) {
679 $source->ensure_connected(@_);
680 }
2bf79155 681}
682
2bf79155 683=head2 limit_dialect
684
685Set the limit_dialect for all existing storages
686
687=cut
688
689sub limit_dialect {
64cdad22 690 my $self = shift @_;
691 foreach my $source ($self->all_storages) {
692 $source->limit_dialect(@_);
693 }
3fbe08e3 694 return $self->master->quote_char;
2bf79155 695}
696
2bf79155 697=head2 quote_char
698
699Set the quote_char for all existing storages
700
701=cut
702
703sub quote_char {
64cdad22 704 my $self = shift @_;
705 foreach my $source ($self->all_storages) {
706 $source->quote_char(@_);
707 }
3fbe08e3 708 return $self->master->quote_char;
2bf79155 709}
710
2bf79155 711=head2 name_sep
712
713Set the name_sep for all existing storages
714
715=cut
716
717sub name_sep {
64cdad22 718 my $self = shift @_;
719 foreach my $source ($self->all_storages) {
720 $source->name_sep(@_);
721 }
3fbe08e3 722 return $self->master->name_sep;
2bf79155 723}
724
2bf79155 725=head2 set_schema
726
727Set the schema object for all existing storages
728
729=cut
730
731sub set_schema {
64cdad22 732 my $self = shift @_;
733 foreach my $source ($self->all_storages) {
734 $source->set_schema(@_);
735 }
2bf79155 736}
737
2bf79155 738=head2 debug
739
740set a debug flag across all storages
741
742=cut
743
744sub debug {
64cdad22 745 my $self = shift @_;
3fbe08e3 746 if(@_) {
747 foreach my $source ($self->all_storages) {
748 $source->debug(@_);
6f7344b8 749 }
64cdad22 750 }
3fbe08e3 751 return $self->master->debug;
2bf79155 752}
753
2bf79155 754=head2 debugobj
755
cea43436 756set a debug object
2bf79155 757
758=cut
759
760sub debugobj {
64cdad22 761 my $self = shift @_;
cea43436 762 return $self->master->debugobj(@_);
2bf79155 763}
764
2bf79155 765=head2 debugfh
766
cea43436 767set a debugfh object
2bf79155 768
769=cut
770
771sub debugfh {
64cdad22 772 my $self = shift @_;
cea43436 773 return $self->master->debugfh(@_);
2bf79155 774}
775
2bf79155 776=head2 debugcb
777
cea43436 778set a debug callback
2bf79155 779
780=cut
781
782sub debugcb {
64cdad22 783 my $self = shift @_;
cea43436 784 return $self->master->debugcb(@_);
2bf79155 785}
786
2bf79155 787=head2 disconnect
788
789disconnect everything
790
791=cut
792
793sub disconnect {
64cdad22 794 my $self = shift @_;
795 foreach my $source ($self->all_storages) {
796 $source->disconnect(@_);
797 }
2bf79155 798}
799
b2e4d522 800=head2 cursor_class
801
802set cursor class on all storages, or return master's
803
804=cut
805
806sub cursor_class {
807 my ($self, $cursor_class) = @_;
808
809 if ($cursor_class) {
810 $_->cursor_class($cursor_class) for $self->all_storages;
811 }
812 $self->master->cursor_class;
813}
d4daee7b 814
7e38d850 815=head1 GOTCHAS
816
817Due to the fact that replicants can lag behind a master, you must take care to
818make sure you use one of the methods to force read queries to a master should
819you need realtime data integrity. For example, if you insert a row, and then
820immediately re-read it from the database (say, by doing $row->discard_changes)
821or you insert a row and then immediately build a query that expects that row
822to be an item, you should force the master to handle reads. Otherwise, due to
823the lag, there is no certainty your data will be in the expected state.
824
825For data integrity, all transactions automatically use the master storage for
826all read and write queries. Using a transaction is the preferred and recommended
827method to force the master to handle all read queries.
828
829Otherwise, you can force a single query to use the master with the 'force_pool'
830attribute:
831
832 my $row = $resultset->search(undef, {force_pool=>'master'})->find($pk);
833
834This attribute will safely be ignore by non replicated storages, so you can use
835the same code for both types of systems.
836
837Lastly, you can use the L</execute_reliably> method, which works very much like
838a transaction.
839
840For debugging, you can turn replication on/off with the methods L</set_reliable_storage>
841and L</set_balanced_storage>, however this operates at a global level and is not
842suitable if you have a shared Schema object being used by multiple processes,
843such as on a web application server. You can get around this limitation by
844using the Schema clone method.
845
846 my $new_schema = $schema->clone;
847 $new_schema->set_reliable_storage;
d4daee7b 848
7e38d850 849 ## $new_schema will use only the Master storage for all reads/writes while
850 ## the $schema object will use replicated storage.
851
f5d3a5de 852=head1 AUTHOR
853
64cdad22 854 John Napiorkowski <john.napiorkowski@takkle.com>
f5d3a5de 855
c4d3fae2 856Based on code originated by:
f5d3a5de 857
64cdad22 858 Norbert Csongrádi <bert@cpan.org>
859 Peter Siklósi <einon@einon.hu>
2156bbdd 860
f5d3a5de 861=head1 LICENSE
862
863You may distribute this code under the same terms as Perl itself.
864
865=cut
866
c354902c 867__PACKAGE__->meta->make_immutable;
868
f5d3a5de 8691;