},
);
-=head2 pool_args
-
-Contains a hashref of initialized information to pass to the Pool object. See
-L<DBIx::Class::Storage::Replicated::Pool> for available arguments.
-
-=cut
-
-has 'pool_args' => (
- is=>'ro',
- isa=>'HashRef',
-);
-
=head2 balancer_type
The replication pool requires a balance class to provider the methods for
},
);
+=head2 balancer_args
+
+Contains a hashref of initialized information to pass to the Balancer object.
+See L<DBIx::Class::Storage::Replicated::Pool> for available arguments.
+
+=cut
+
+has 'balancer_args' => (
+ is=>'ro',
+ isa=>'HashRef',
+);
+
=head2 pool
Is a <DBIx::Class::Storage::DBI::Replicated::Pool> or derived class. This is a
connect_replicants
replicants
has_replicants
- num_replicants
- delete_replicant
- validate_replicants
/],
);
is=>'ro',
isa=>'DBIx::Class::Storage::DBI::Replicated::Balancer',
lazy_build=>1,
+ handles=>[qw/auto_validate_every/],
);
=head2 master
sub _build_pool {
my $self = shift @_;
- $self->create_pool($self->pool_args);
+ $self->create_pool;
}
=head2 _build_balancer_type
=cut
sub _build_balancer_type {
- return 'DBIx::Class::Storage::DBI::Replicated::Balancer';
+ return 'DBIx::Class::Storage::DBI::Replicated::Balancer::First';
}
=head2 _build_balancer
my $self = shift @_;
$self->create_balancer(
pool=>$self->pool,
- master=>$self->master);
+ master=>$self->master,
+ %{$self->balancer_args},);
}
=head2 _build_write_handler
package DBIx::Class::Storage::DBI::Replicated::Balancer;
-use Moose;
+use Moose::Role;
+requires 'next_storage';
=head1 NAME
=head1 SYNOPSIS
-This class is used internally by L<DBIx::Class::Storage::DBI::Replicated>. You
-shouldn't need to create instances of this class.
+This role is used internally by L<DBIx::Class::Storage::DBI::Replicated>.
=head1 DESCRIPTION
has 'auto_validate_every' => (
is=>'rw',
isa=>'Int',
- predicate=>'had_auto_validate_every',
-);
-
-=head2 last_validated
-
-This is an integer representing a time since the last time the replicants were
-validated. It's nothing fancy, just an integer provided via the perl time
-builtin.
-
-=cut
-
-has 'last_validated' => (
- is=>'rw',
- isa=>'Int',
- reader=>'last_validated',
- writer=>'_last_validated',
- lazy=>1,
- default=>sub {
- time;
- },
+ predicate=>'has_auto_validate_every',
);
=head2 master
=head2 next_storage
+This method should be defined in the class which consumes this role.
+
Given a pool object, return the next replicant that will serve queries. The
default behavior is to grap the first replicant it finds but you can write
your own subclasses of L<DBIx::Class::Storage::DBI::Replicated::Balancer> to
This returns from the pool of active replicants. If there are no active
replicants, then you should have it return the master as an ultimate fallback.
-TODO this needs to wrap for the subclasses better. Maybe good use of INNER?
+=head2 around: next_storage
+
+Advice on next storage to add the autovalidation. We have this broken out so
+that it's easier to break out the auto validation into a role.
+
+This also returns the master in the case that none of the replicants are active
+or just just forgot to create them :)
=cut
-sub next_storage {
- my $self = shift @_;
-
- ## Do we need to validate the replicants?
- if(
- $self->had_auto_validate_every &&
- ($self->auto_validate_every + $self->last_validated) > time
- ) {
- $self->pool->validate_replicants;
- $self->_last_validated(time);
- }
+around 'next_storage' => sub {
+ my ($next_storage, $self, @args) = @_;
+ my $now = time;
- ## Get a replicant, or the master if none
- my $next = ($self->pool->active_replicants)[0];
- return $next ? $next:$self->master;
-}
+ ## Do we need to validate the replicants?
+ if(
+ $self->has_auto_validate_every &&
+ ($self->auto_validate_every + $self->pool->last_validated) <= $now
+ ) {
+ $self->pool->validate_replicants;
+ }
+
+ ## Get a replicant, or the master if none
+ my $next = $self->$next_storage(@args);
+ return $next ? $next:$self->master;
+};
=head2 before: select
--- /dev/null
+package DBIx::Class::Storage::DBI::Replicated::Balancer::First;
+
+use List::Util qw(shuffle);
+use Moose;
+with 'DBIx::Class::Storage::DBI::Replicated::Balancer';
+
+=head1 NAME
+
+DBIx::Class::Storage::DBI::Replicated::Balancer::First; Just get the First Balancer
+
+=head1 SYNOPSIS
+
+This class is used internally by L<DBIx::Class::Storage::DBI::Replicated>. You
+shouldn't need to create instances of this class.
+
+=head1 DESCRIPTION
+
+Given a pool (L<DBIx::Class::Storage::DBI::Replicated::Pool>) of replicated
+database's (L<DBIx::Class::Storage::DBI::Replicated::Replicant>), defines a
+method by which query load can be spread out across each replicant in the pool.
+
+This Balancer just get's whatever is the first replicant in the pool
+
+=head1 ATTRIBUTES
+
+This class defines the following attributes.
+
+=head1 METHODS
+
+This class defines the following methods.
+
+=head2 next_storage
+
+Just get the first storage. Probably only good when you have one replicant.
+
+=cut
+
+sub next_storage {
+ return (shift->pool->active_replicants)[0];
+}
+
+=head1 AUTHOR
+
+John Napiorkowski <john.napiorkowski@takkle.com>
+
+=head1 LICENSE
+
+You may distribute this code under the same terms as Perl itself.
+
+=cut
+
+1;
\ No newline at end of file
use List::Util qw(shuffle);
use Moose;
-extends 'DBIx::Class::Storage::DBI::Replicated::Balancer';
+with 'DBIx::Class::Storage::DBI::Replicated::Balancer';
=head1 NAME
-DBIx::Class::Storage::DBI::Replicated::Balancer; A Software Load Balancer
+DBIx::Class::Storage::DBI::Replicated::Balancer::Random; A 'random' Balancer
=head1 SYNOPSIS
=cut
sub next_storage {
- my $self = shift @_;
- my $next = (shuffle($self->pool->active_replicants))[0];
- return $next ? $next : $self->master;
+ return (shuffle(shift->pool->active_replicants))[0];
}
=head1 AUTHOR
default=>0,
);
+=head2 last_validated
+
+This is an integer representing a time since the last time the replicants were
+validated. It's nothing fancy, just an integer provided via the perl time
+builtin.
+
+=cut
+
+has 'last_validated' => (
+ is=>'rw',
+ isa=>'Int',
+ reader=>'last_validated',
+ writer=>'_last_validated',
+ lazy=>1,
+ default=>sub {
+ time;
+ },
+);
+
=head2 replicant_type ($classname)
Base class used to instantiate replicants that are in the pool. Unless you
$replicant->active(0);
}
}
+
+ ## Mark that we completed this validation.
+ $self->_last_validated(time);
}
=head1 AUTHOR
eval "use Moose; use Test::Moose";
plan $@
? ( skip_all => 'needs Moose for testing' )
- : ( tests => 46 );
+ : ( tests => 50 );
}
use_ok 'DBIx::Class::Storage::DBI::Replicated::Pool';
storage_type=>[
'::DBI::Replicated' => {
balancer_type=>'::Random',
+ balancer_args=>{
+ auto_validate_every=>100,
+ },
}
],
deploy_args=>{
isa_ok $replicated->schema->storage->pool
=> 'DBIx::Class::Storage::DBI::Replicated::Pool';
-isa_ok $replicated->schema->storage->balancer
+does_ok $replicated->schema->storage->balancer
=> 'DBIx::Class::Storage::DBI::Replicated::Balancer';
ok my @replicant_connects = $replicated->generate_replicant_connect_info
ok $replicated->schema->storage->pool->has_replicants
=> 'does have replicants';
-is $replicated->schema->storage->num_replicants => 2
+is $replicated->schema->storage->pool->num_replicants => 2
=> 'has two replicants';
does_ok $replicated_storages[0]
## We skip this tests unless you have a custom replicants, since the default
## sqlite based replication tests don't support these functions.
- skip 'Cannot Test Replicant Status on Non Replicating Database', 3
+ skip 'Cannot Test Replicant Status on Non Replicating Database', 9
unless DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"};
$replicated->replicate; ## Give the slaves a chance to catchup.
is $replicated->schema->storage->pool->active_replicants, 2
=> 'Both replicants in good standing again';
+
+ ## Check auto validate
+
+ is $replicated->schema->storage->balancer->auto_validate_every, 100
+ => "Got the expected value for auto validate";
+
+ ## This will make sure we auto validatge everytime
+ $replicated->schema->storage->balancer->auto_validate_every(0);
+
+ ## set all the replicants to inactive, and make sure the balancer falls back to
+ ## the master.
+
+ $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
+ $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
+
+ ## Ok, now when we go to run a query, autovalidate SHOULD reconnect
+
+ is $replicated->schema->storage->pool->active_replicants => 0
+ => "both replicants turned off";
+
+ ok $replicated->schema->resultset('Artist')->find(5)
+ => 'replicant reactivated';
+
+ is $replicated->schema->storage->pool->active_replicants => 2
+ => "both replicants reactivated";
}
## Delete the old database files