From: Rafael Kitover Date: Fri, 15 May 2009 02:04:12 +0000 (+0000) Subject: minor replication changes - use a real hash merge, clarify master_read_weight, really... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b88b85e73de8ca03cfd1f7bc2ee43d6403302986;p=dbsrgits%2FDBIx-Class-Historic.git minor replication changes - use a real hash merge, clarify master_read_weight, really done with this now. --- diff --git a/Makefile.PL b/Makefile.PL index 7aaf1dc..715ad7c 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -81,6 +81,7 @@ my %force_requires_if_author = ( 'MooseX::AttributeHelpers' => 0.12, 'MooseX::Types', => 0.10, 'namespace::clean' => 0.11, + 'Hash::Merge', => 0.11, # t/96_is_deteministic_value.t 'DateTime::Format::Strptime' => 0, diff --git a/lib/DBIx/Class/Storage/DBI/Replicated.pm b/lib/DBIx/Class/Storage/DBI/Replicated.pm index 465eec0..0dc50d7 100644 --- a/lib/DBIx/Class/Storage/DBI/Replicated.pm +++ b/lib/DBIx/Class/Storage/DBI/Replicated.pm @@ -11,6 +11,7 @@ BEGIN { MooseX::AttributeHelpers => '0.12', MooseX::Types => '0.10', namespace::clean => '0.11', + Hash::Merge => '0.11' ); my @didnt_load; @@ -33,6 +34,7 @@ use DBIx::Class::Storage::DBI::Replicated::Types 'BalancerClassNamePart'; use MooseX::Types::Moose qw/ClassName HashRef Object/; use Scalar::Util 'reftype'; use Carp::Clan qw/^DBIx::Class/; +use Hash::Merge 'merge'; use namespace::clean -except => 'meta'; @@ -110,6 +112,7 @@ Replicated Storage has additional requirements not currently part of L 0.12 MooseX::Types => 0.10 namespace::clean => 0.11 + Hash::Merge => 0.11 You will need to install these modules manually via CPAN or make them part of the Makefile for your distribution. @@ -324,7 +327,7 @@ around connect_info => sub { my %opts; for my $arg (@$info) { next unless (reftype($arg)||'') eq 'HASH'; - %opts = (%opts, %$arg); + %opts = %{ merge($arg, \%opts) }; } delete $opts{dsn}; @@ -332,10 +335,9 @@ around connect_info => sub { $self->pool_type(delete $opts{pool_type}) if $opts{pool_type}; - $self->pool_args({ - %{ $self->pool_args }, - %{ delete $opts{pool_args} || {} } - }); + $self->pool_args( + merge((delete $opts{pool_args} || {}), $self->pool_args) + ); $self->pool($self->_build_pool) if $self->pool; @@ -345,10 +347,9 @@ around connect_info => sub { $self->balancer_type(delete $opts{balancer_type}) if $opts{balancer_type}; - $self->balancer_args({ - %{ $self->balancer_args }, - %{ delete $opts{balancer_args} || {} } - }); + $self->balancer_args( + merge((delete $opts{balancer_args} || {}), $self->balancer_args) + ); $self->balancer($self->_build_balancer) if $self->balancer; @@ -468,11 +469,21 @@ around connect_replicants => sub { $r->[$i] = {} unless $r->[$i]; # merge if two hashes - my %opts = map %$_, @$r[$i .. $#{$r}]; + my @hashes = @$r[$i .. $#{$r}]; + + croak "invalid connect_info options" + if (grep { reftype($_) eq 'HASH' } @hashes) != @hashes; + + croak "too many hashrefs in connect_info" + if @hashes > 2; + + my %opts = %{ merge(reverse @hashes) }; + +# delete them splice @$r, $i+1, ($#{$r} - $i), (); # merge with master - %opts = (%{ $self->_master_connect_info_opts }, %opts); + %opts = %{ merge(\%opts, $self->_master_connect_info_opts) }; # update $r->[$i] = \%opts; diff --git a/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm b/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm index d3959d0..f23db75 100644 --- a/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm +++ b/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm @@ -30,10 +30,16 @@ 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. +A number greater than 0 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. + +For example: if you have 2 replicants, and a L of C<0.5>, +the chance of reading from master will be C<20%>. + +You can set it to a value higher than 1, making master have higher weight than +any single replicant, if for example you have a very powerful master. =cut diff --git a/lib/DBIx/Class/Storage/DBI/Replicated/Types.pm b/lib/DBIx/Class/Storage/DBI/Replicated/Types.pm index b66748f..c366ea5 100644 --- a/lib/DBIx/Class/Storage/DBI/Replicated/Types.pm +++ b/lib/DBIx/Class/Storage/DBI/Replicated/Types.pm @@ -31,8 +31,8 @@ coerce BalancerClassNamePart, subtype Weight, as Num, - where { $_ >= 0 && $_ <= 1 }, - message { 'weight must be a decimal between 0 and 1' }; + where { $_ >= 0 }, + message { 'weight must be a decimal greater than 0' }; =head1 AUTHOR