,
# t/93storage_replication.t
- 'Moose', => 0.54,
+ 'Moose', => 0.77,
'MooseX::AttributeHelpers' => 0.12,
'MooseX::Types', => 0.10,
'namespace::clean' => 0.11,
is=>'ro',
isa=>Object,
lazy_build=>1,
- lazy_build=>1,
handles=>[qw/
on_connect_do
on_disconnect_do
sub _build_master {
my $self = shift @_;
- DBIx::Class::Storage::DBI->new($self->schema);
+ my $master = DBIx::Class::Storage::DBI->new($self->schema);
+ DBIx::Class::Storage::DBI::Replicated::WithDSN->meta->apply($master);
+ $master
}
=head2 _build_pool
use Moose;
with 'DBIx::Class::Storage::DBI::Replicated::Balancer';
+use DBIx::Class::Storage::DBI::Replicated::Types 'Weight';
use namespace::clean -except => 'meta';
=head1 NAME
This class defines the following attributes.
+=head2 master_read_weight
+
+A number from 0 to 1 that specifies what weight to give the master when choosing
+which backend to execute a read query on. A value of 0, which is the default,
+does no reads from master, while a value of 1 gives it the same priority as any
+single replicant.
+
+=cut
+
+has master_read_weight => (is => 'rw', isa => Weight, default => sub { 0 });
+
=head1 METHODS
This class defines the following methods.
sub next_storage {
my $self = shift @_;
- my @active_replicants = $self->pool->active_replicants;
- my $count_active_replicants = $#active_replicants +1;
- my $random_replicant = int(rand($count_active_replicants));
-
- return $active_replicants[$random_replicant];
+
+ my @replicants = $self->pool->active_replicants;
+ my $master = $self->master;
+
+ my $rnd = $self->random_number(@replicants + $self->master_read_weight);
+
+ return $rnd >= @replicants ? $master : $replicants[int $rnd];
+}
+
+=head2 random_number
+
+Returns a random number from 0 to x, not including x. Uses perl's
+L<perlfunc/rand> by default.
+
+=cut
+
+sub random_number {
+ rand($_[1])
}
=head1 AUTHOR
use Moose::Role;
requires qw/_query_start/;
-use MooseX::Types::Moose qw/Bool/;
+with 'DBIx::Class::Storage::DBI::Replicated::WithDSN';
+use MooseX::Types::Moose 'Bool';
use namespace::clean -except => 'meta';
This class defines the following methods.
-=head2 around: _query_start
-
-advice iof the _query_start method to add more debuggin
-
-=cut
-
-around '_query_start' => sub {
- my ($method, $self, $sql, @bind) = @_;
- my $dsn = $self->_dbi_connect_info->[0];
- $self->$method("DSN: $dsn SQL: $sql", @bind);
-};
-
=head2 debugobj
Override the debugobj method to redirect this method call back to the master.
=head1 ALSO SEE
-L<<a href="http://en.wikipedia.org/wiki/Replicant">http://en.wikipedia.org/wiki/Replicant</a>>
+L<http://en.wikipedia.org/wiki/Replicant>,
+L<DBIx::Class::Storage::DBI::Replicated>
=head1 AUTHOR
=cut
use MooseX::Types
- -declare => [qw/BalancerClassNamePart/];
-use MooseX::Types::Moose qw/ClassName Str/;
+ -declare => [qw/BalancerClassNamePart Weight/];
+use MooseX::Types::Moose qw/ClassName Str Num/;
class_type 'DBIx::Class::Storage::DBI';
class_type 'DBIx::Class::Schema';
$type;
};
+subtype Weight,
+ as Num,
+ where { $_ >= 0 && $_ <= 1 },
+ message { 'weight must be a decimal between 0 and 1' };
+
=head1 AUTHOR
John Napiorkowski <john.napiorkowski@takkle.com>
--- /dev/null
+package DBIx::Class::Storage::DBI::Replicated::WithDSN;
+
+use Moose::Role;
+requires qw/_query_start/;
+
+use namespace::clean -except => 'meta';
+
+=head1 NAME
+
+DBIx::Class::Storage::DBI::Replicated::WithDSN - A DBI Storage Role with DSN
+information in trace output
+
+=head1 SYNOPSIS
+
+This class is used internally by L<DBIx::Class::Storage::DBI::Replicated>.
+
+=head1 DESCRIPTION
+
+This role adds C<DSN: > info to storage debugging output.
+
+=head1 METHODS
+
+This class defines the following methods.
+
+=head2 around: _query_start
+
+Add C<DSN: > to debugging output.
+
+=cut
+
+around '_query_start' => sub {
+ my ($method, $self, $sql, @bind) = @_;
+ my $dsn = $self->_dbi_connect_info->[0];
+ $self->$method("DSN: $dsn SQL: $sql", @bind);
+};
+
+=head1 ALSO SEE
+
+L<DBIx::Class::Storage::DBI>
+
+=head1 AUTHOR
+
+John Napiorkowski <john.napiorkowski@takkle.com>
+
+=head1 LICENSE
+
+You may distribute this code under the same terms as Perl itself.
+
+=cut
+
+1;
eval "use DBIx::Class::Storage::DBI::Replicated; use Test::Moose";
plan $@
? ( skip_all => "Deps not installed: $@" )
- : ( tests => 88 );
+ : ( tests => 89 );
}
use_ok 'DBIx::Class::Storage::DBI::Replicated::Pool';
balancer_type=>'::Random',
balancer_args=>{
auto_validate_every=>100,
+ master_read_weight => 1
},
}
},
balancer_type=>'::Random',
balancer_args=> {
auto_validate_every=>100,
+ master_read_weight => 1
},
deploy_args=>{
add_drop_table => 1,
is $artist1->name, 'Ozric Tentacles'
=> 'Found expected name for first result';
+## Check that master_read_weight is honored
+{
+ no warnings 'once';
+
+ # turn off redefined warning
+ local $SIG{__WARN__} = sub {};
+
+ local
+ *DBIx::Class::Storage::DBI::Replicated::Balancer::Random::random_number =
+ sub { 999 };
+
+ $replicated->schema->storage->balancer->increment_storage;
+
+ is $replicated->schema->storage->balancer->current_replicant,
+ $replicated->schema->storage->master
+ => 'master_read_weight is honored';
+
+ ## turn it off for the duration of the test
+ $replicated->schema->storage->balancer->master_read_weight(0);
+ $replicated->schema->storage->balancer->increment_storage;
+}
+
## Add some new rows that only the master will have This is because
## we overload any type of write operation so that is must hit the master
## database.
## Delete the old database files
$replicated->cleanup;
+
+# vim: sw=4 sts=4 :