1 package DBIx::Class::Storage::DBI::Replicated;
4 use DBIx::Class::Storage::DBI;
5 use DBIx::Class::Storage::DBI::Replicated::Pool;
6 use DBIx::Class::Storage::DBI::Replicated::Balancer;
7 use Scalar::Util qw(blessed);
9 extends 'DBIx::Class::Storage::DBI', 'Moose::Object';
13 DBIx::Class::Storage::DBI::Replicated - ALPHA Replicated database support
17 The Following example shows how to change an existing $schema to a replicated
18 storage type, add some replicated (readonly) databases, and perform reporting
21 ## Change storage_type in your schema class
22 $schema->storage_type( ['::DBI::Replicated', {balancer=>'::Random'}] );
24 ## Add some slaves. Basically this is an array of arrayrefs, where each
25 ## arrayref is database connect information
27 $schema->storage->connect_replicants(
28 [$dsn1, $user, $pass, \%opts],
29 [$dsn1, $user, $pass, \%opts],
30 [$dsn1, $user, $pass, \%opts],
35 Warning: This class is marked ALPHA. We are using this in development and have
36 some basic test coverage but the code hasn't yet been stressed by a variety
37 of databases. Individual DB's may have quirks we are not aware of. Please
38 use this in development and pass along your experiences/bug fixes.
40 This class implements replicated data store for DBI. Currently you can define
41 one master and numerous slave database connections. All write-type queries
42 (INSERT, UPDATE, DELETE and even LAST_INSERT_ID) are routed to master
43 database, all read-type queries (SELECTs) go to the slave database.
45 Basically, any method request that L<DBIx::Class::Storage::DBI> would normally
46 handle gets delegated to one of the two attributes: L</master_storage> or to
47 L</current_replicant_storage>. Additionally, some methods need to be distributed
48 to all existing storages. This way our storage class is a drop in replacement
49 for L<DBIx::Class::Storage::DBI>.
51 Read traffic is spread across the replicants (slaves) occuring to a user
52 selected algorithm. The default algorithm is random weighted.
54 TODO more details about the algorithm.
58 This class defines the following attributes.
62 Contains the classname which will instantiate the L</pool> object. Defaults
63 to: L<DBIx::Class::Storage::DBI::Replicated::Pool>.
72 'create_pool' => 'new',
78 The replication pool requires a balance class to provider the methods for
79 choose how to spread the query load across each replicant in the pool.
83 has 'balancer_type' => (
88 'create_balancer' => 'new',
94 Contains a hashref of initialized information to pass to the Balancer object.
95 See L<DBIx::Class::Storage::Replicated::Pool> for available arguments.
99 has 'balancer_args' => (
106 Is a <DBIx::Class::Storage::DBI::Replicated::Pool> or derived class. This is a
107 container class for one or more replicated databases.
113 isa=>'DBIx::Class::Storage::DBI::Replicated::Pool',
124 Is a <DBIx::Class::Storage::DBI::Replicated::Balancer> or derived class. This
125 is a class that takes a pool (<DBIx::Class::Storage::DBI::Replicated::Pool>)
131 isa=>'DBIx::Class::Storage::DBI::Replicated::Balancer',
133 handles=>[qw/auto_validate_every/],
138 The master defines the canonical state for a pool of connected databases. All
139 the replicants are expected to match this databases state. Thus, in a classic
140 Master / Slaves distributed system, all the slaves are expected to replicate
141 the Master's state as quick as possible. This is the only database in the
142 pool of databases that is allowed to handle write traffic.
148 isa=>'DBIx::Class::Storage::DBI',
152 =head1 ATTRIBUTES IMPLEMENTING THE DBIx::Storage::DBI INTERFACE
154 The following methods are delegated all the methods required for the
155 L<DBIx::Class::Storage::DBI> interface.
159 Defines an object that implements the read side of L<BIx::Class::Storage::DBI>.
163 has 'read_handler' => (
176 Defines an object that implements the write side of L<BIx::Class::Storage::DBI>.
180 has 'write_handler' => (
193 deployment_statements
213 This class defines the following methods.
217 L<DBIx::Class::Schema> when instantiating it's storage passed itself as the
218 first argument. We need to invoke L</new> on the underlying parent class, make
219 sure we properly give it a L<Moose> meta class, and then correctly instantiate
220 our attributes. Basically we pass on whatever the schema has in it's class
221 data for 'storage_type_args' to our replicated storage type.
226 my $class = shift @_;
227 my $schema = shift @_;
228 my $storage_type_args = shift @_;
229 my $obj = $class->SUPER::new($schema, $storage_type_args, @_);
231 ## Hate to do it this way, but can't seem to get advice on the attribute working right
232 ## maybe we can do a type and coercion for it.
233 if( $storage_type_args->{balancer_type} && $storage_type_args->{balancer_type}=~m/^::/) {
234 $storage_type_args->{balancer_type} = 'DBIx::Class::Storage::DBI::Replicated::Balancer'.$storage_type_args->{balancer_type};
235 eval "require $storage_type_args->{balancer_type}";
238 return $class->meta->new_object(
239 __INSTANCE__ => $obj,
247 Lazy builder for the L</master> attribute.
252 DBIx::Class::Storage::DBI->new;
255 =head2 _build_pool_type
257 Lazy builder for the L</pool_type> attribute.
261 sub _build_pool_type {
262 return 'DBIx::Class::Storage::DBI::Replicated::Pool';
267 Lazy builder for the L</pool> attribute.
276 =head2 _build_balancer_type
278 Lazy builder for the L</balancer_type> attribute.
282 sub _build_balancer_type {
283 return 'DBIx::Class::Storage::DBI::Replicated::Balancer::First';
286 =head2 _build_balancer
288 Lazy builder for the L</balancer> attribute. This takes a Pool object so that
289 the balancer knows which pool it's balancing.
293 sub _build_balancer {
295 $self->create_balancer(
297 master=>$self->master,
298 %{$self->balancer_args},);
301 =head2 _build_write_handler
303 Lazy builder for the L</write_handler> attribute. The default is to set this to
308 sub _build_write_handler {
309 return shift->master;
312 =head2 _build_read_handler
314 Lazy builder for the L</read_handler> attribute. The default is to set this to
319 sub _build_read_handler {
320 return shift->balancer;
323 =head2 around: connect_replicants
325 All calls to connect_replicants needs to have an existing $schema tacked onto
326 top of the args, since L<DBIx::Storage::DBI> needs it.
330 around 'connect_replicants' => sub {
331 my ($method, $self, @args) = @_;
332 $self->$method($self->schema, @args);
337 Returns an array of of all the connected storage backends. The first element
338 in the returned array is the master, and the remainings are each of the
346 return grep {defined $_ && blessed $_} (
352 =head2 set_reliable_storage
354 Sets the current $schema to be 'reliable', that is all queries, both read and
355 write are sent to the master
359 sub set_reliable_storage {
361 my $schema = $self->schema;
362 my $write_handler = $self->schema->storage->write_handler;
364 $schema->storage->read_handler($write_handler);
367 =head2 set_balanced_storage
369 Sets the current $schema to be use the </balancer> for all reads, while all
370 writea are sent to the master only
374 sub set_balanced_storage {
376 my $schema = $self->schema;
377 my $write_handler = $self->schema->storage->balancer;
379 $schema->storage->read_handler($write_handler);
384 Check that the master and at least one of the replicants is connected.
392 $self->master->connected &&
393 $self->pool->connected_replicants;
396 =head2 ensure_connected
398 Make sure all the storages are connected.
402 sub ensure_connected {
404 foreach my $source ($self->all_storages) {
405 $source->ensure_connected(@_);
411 Set the limit_dialect for all existing storages
417 foreach my $source ($self->all_storages) {
418 $source->limit_dialect(@_);
424 Set the quote_char for all existing storages
430 foreach my $source ($self->all_storages) {
431 $source->quote_char(@_);
437 Set the name_sep for all existing storages
443 foreach my $source ($self->all_storages) {
444 $source->name_sep(@_);
450 Set the schema object for all existing storages
456 foreach my $source ($self->all_storages) {
457 $source->set_schema(@_);
463 set a debug flag across all storages
469 foreach my $source ($self->all_storages) {
476 set a debug object across all storages
482 foreach my $source ($self->all_storages) {
483 $source->debugobj(@_);
489 set a debugfh object across all storages
495 foreach my $source ($self->all_storages) {
496 $source->debugfh(@_);
502 set a debug callback across all storages
508 foreach my $source ($self->all_storages) {
509 $source->debugcb(@_);
515 disconnect everything
521 foreach my $source ($self->all_storages) {
522 $source->disconnect(@_);
528 Norbert Csongrádi <bert@cpan.org>
530 Peter Siklósi <einon@einon.hu>
532 John Napiorkowski <john.napiorkowski@takkle.com>
536 You may distribute this code under the same terms as Perl itself.