[Bast-commits] r6189 - in DBIx-Class/0.08/branches/storage-tweaks: . lib/DBIx/Class/Storage/DBI lib/DBIx/Class/Storage/DBI/Replicated lib/DBIx/Class/Storage/DBI/Replicated/Balancer t

caelum at dev.catalyst.perl.org caelum at dev.catalyst.perl.org
Sat May 9 03:31:15 GMT 2009


Author: caelum
Date: 2009-05-09 03:31:15 +0000 (Sat, 09 May 2009)
New Revision: 6189

Added:
   DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated/WithDSN.pm
Modified:
   DBIx-Class/0.08/branches/storage-tweaks/Makefile.PL
   DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated.pm
   DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm
   DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated/Replicant.pm
   DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated/Types.pm
   DBIx-Class/0.08/branches/storage-tweaks/t/93storage_replication.t
Log:
::DBI::Replicated - add master_read_weight to ::Random balancer_type

Modified: DBIx-Class/0.08/branches/storage-tweaks/Makefile.PL
===================================================================
--- DBIx-Class/0.08/branches/storage-tweaks/Makefile.PL	2009-05-09 01:59:20 UTC (rev 6188)
+++ DBIx-Class/0.08/branches/storage-tweaks/Makefile.PL	2009-05-09 03:31:15 UTC (rev 6189)
@@ -77,7 +77,7 @@
   ,
 
   # t/93storage_replication.t
-  'Moose',                        => 0.54,
+  'Moose',                        => 0.77,
   'MooseX::AttributeHelpers'      => 0.12,
   'MooseX::Types',                => 0.10,
   'namespace::clean'              => 0.11,

Modified: DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm
===================================================================
--- DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm	2009-05-09 01:59:20 UTC (rev 6188)
+++ DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated/Balancer/Random.pm	2009-05-09 03:31:15 UTC (rev 6189)
@@ -2,6 +2,7 @@
 
 use Moose;
 with 'DBIx::Class::Storage::DBI::Replicated::Balancer';
+use DBIx::Class::Storage::DBI::Replicated::Types 'Weight';
 use namespace::clean -except => 'meta';
 
 =head1 NAME
@@ -27,6 +28,17 @@
 
 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.
@@ -41,13 +53,26 @@
 
 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
 
 John Napiorkowski <john.napiorkowski at takkle.com>

Modified: DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated/Replicant.pm
===================================================================
--- DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated/Replicant.pm	2009-05-09 01:59:20 UTC (rev 6188)
+++ DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated/Replicant.pm	2009-05-09 03:31:15 UTC (rev 6189)
@@ -2,7 +2,8 @@
 
 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';
 
@@ -55,18 +56,6 @@
 
 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.
@@ -79,7 +68,8 @@
 
 =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
 

Modified: DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated/Types.pm
===================================================================
--- DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated/Types.pm	2009-05-09 01:59:20 UTC (rev 6188)
+++ DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated/Types.pm	2009-05-09 03:31:15 UTC (rev 6189)
@@ -9,8 +9,8 @@
 =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';
@@ -29,6 +29,11 @@
     $type;  	
   };
 
+subtype Weight,
+  as Num,
+  where { $_ >= 0 && $_ <= 1 },
+  message { 'weight must be a decimal between 0 and 1' };
+
 =head1 AUTHOR
 
   John Napiorkowski <john.napiorkowski at takkle.com>

Added: DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated/WithDSN.pm
===================================================================
--- DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated/WithDSN.pm	                        (rev 0)
+++ DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated/WithDSN.pm	2009-05-09 03:31:15 UTC (rev 6189)
@@ -0,0 +1,51 @@
+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 at takkle.com>
+
+=head1 LICENSE
+
+You may distribute this code under the same terms as Perl itself.
+
+=cut
+
+1;

Modified: DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated.pm
===================================================================
--- DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated.pm	2009-05-09 01:59:20 UTC (rev 6188)
+++ DBIx-Class/0.08/branches/storage-tweaks/lib/DBIx/Class/Storage/DBI/Replicated.pm	2009-05-09 03:31:15 UTC (rev 6189)
@@ -275,7 +275,6 @@
   is=>'ro',
   isa=>Object,
   lazy_build=>1,
-  lazy_build=>1,
   handles=>[qw/   
     on_connect_do
     on_disconnect_do       
@@ -388,7 +387,9 @@
 
 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

Modified: DBIx-Class/0.08/branches/storage-tweaks/t/93storage_replication.t
===================================================================
--- DBIx-Class/0.08/branches/storage-tweaks/t/93storage_replication.t	2009-05-09 01:59:20 UTC (rev 6188)
+++ DBIx-Class/0.08/branches/storage-tweaks/t/93storage_replication.t	2009-05-09 03:31:15 UTC (rev 6189)
@@ -11,7 +11,7 @@
     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';
@@ -80,6 +80,7 @@
             balancer_type=>'::Random',
             balancer_args=>{
               auto_validate_every=>100,
+	      master_read_weight => 1
             },
           }
         },
@@ -96,6 +97,7 @@
         balancer_type=>'::Random',
         balancer_args=> {
           auto_validate_every=>100,
+	  master_read_weight => 1
         },
         deploy_args=>{
           add_drop_table => 1,
@@ -356,6 +358,28 @@
 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.
@@ -684,3 +708,5 @@
 
 ## Delete the old database files
 $replicated->cleanup;
+
+# vim: sw=4 sts=4 :




More information about the Bast-commits mailing list