From: John Napiorkowski Date: Mon, 7 Jul 2008 18:58:37 +0000 (+0000) Subject: removed some debugging comments, removed transaction from Row->get_from_storage,... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=bbafcf262b74716a37f4265512ba5c1ecd92e15b;p=dbsrgits%2FDBIx-Class-Historic.git removed some debugging comments, removed transaction from Row->get_from_storage, enabled support for new resultset attribute "execute_reliably" which signals the Balancer to send read requests to the Also refactored connect_replicants to break down functionality into two methods and added new Balancer method to roll the replicant to the next in the queque. added tests for all the above. --- diff --git a/lib/DBIx/Class/Row.pm b/lib/DBIx/Class/Row.pm index 8c40b00..71629e3 100644 --- a/lib/DBIx/Class/Row.pm +++ b/lib/DBIx/Class/Row.pm @@ -802,7 +802,7 @@ sub register_column { =head2 get_from_storage Returns a new Row which is whatever the Storage has for the currently created -Row object. You ca use this to see if the storage has become inconsistent with +Row object. You can use this to see if the storage has become inconsistent with whatever your Row object is. =cut @@ -810,9 +810,7 @@ whatever your Row object is. sub get_from_storage { my $self = shift @_; my @primary_columns = map { $self->$_ } $self->primary_columns; - return $self->result_source->schema->txn_do(sub { - return $self->result_source->resultset->find(@primary_columns); - }); + return $self->result_source->resultset->find(@primary_columns); } =head2 throw_exception diff --git a/lib/DBIx/Class/Storage/DBI/Replicated/Balancer.pm b/lib/DBIx/Class/Storage/DBI/Replicated/Balancer.pm index 241d666..186e483 100644 --- a/lib/DBIx/Class/Storage/DBI/Replicated/Balancer.pm +++ b/lib/DBIx/Class/Storage/DBI/Replicated/Balancer.pm @@ -140,21 +140,38 @@ around 'next_storage' => sub { return $next ? $next:$self->master; }; -=head2 before: select +=head2 increment_storage -Advice on the select attribute. Each time we use a replicant -we need to change it via the storage pool algorithm. That way we are spreading -the load evenly (hopefully) across existing capacity. +Rolls the Storage to whatever is next in the queue, as defined by the Balancer. =cut -before 'select' => sub { +sub increment_storage { my $self = shift @_; my $next_replicant = $self->next_storage; $self->current_replicant($next_replicant); +} + +=head2 around: select + +Advice on the select attribute. Each time we use a replicant +we need to change it via the storage pool algorithm. That way we are spreading +the load evenly (hopefully) across existing capacity. + +=cut + +around 'select' => sub { + my ($select, $self, @args) = @_; + + if ($args[-1]->{execute_reliably}) { + return $self->master->select(@args); + } else { + $self->increment_storage; + return $self->$select(@args); + } }; -=head2 before: select_single +=head2 around: select_single Advice on the select_single attribute. Each time we use a replicant we need to change it via the storage pool algorithm. That way we are spreading @@ -162,10 +179,15 @@ the load evenly (hopefully) across existing capacity. =cut -before 'select_single' => sub { - my $self = shift @_; - my $next_replicant = $self->next_storage; - $self->current_replicant($next_replicant); +around 'select_single' => sub { + my ($select_single, $self, @args) = @_; + + if ($args[-1]->{execute_reliably}) { + return $self->master->select_single(@args); + } else { + $self->increment_storage; + return $self->$select_single(@args); + } }; =head2 before: columns_info_for @@ -178,8 +200,7 @@ the load evenly (hopefully) across existing capacity. before 'columns_info_for' => sub { my $self = shift @_; - my $next_replicant = $self->next_storage; - $self->current_replicant($next_replicant); + $self->increment_storage; }; =head1 AUTHOR diff --git a/lib/DBIx/Class/Storage/DBI/Replicated/Pool.pm b/lib/DBIx/Class/Storage/DBI/Replicated/Pool.pm index 07af406..9d314c0 100644 --- a/lib/DBIx/Class/Storage/DBI/Replicated/Pool.pm +++ b/lib/DBIx/Class/Storage/DBI/Replicated/Pool.pm @@ -149,10 +149,7 @@ sub connect_replicants { my @newly_created = (); foreach my $connect_info (@_) { - my $replicant = $self->create_replicant($schema); - $replicant->connect_info($connect_info); - $replicant->ensure_connected; - DBIx::Class::Storage::DBI::Replicated::Replicant->meta->apply($replicant); + my $replicant = $self->connect_replicant($schema, $connect_info); my ($key) = ($connect_info->[0]=~m/^dbi\:.+\:(.+)$/); $self->set_replicant( $key => $replicant); push @newly_created, $replicant; @@ -161,6 +158,24 @@ sub connect_replicants { return @newly_created; } +=head2 connect_replicant ($schema, $connect_info) + +Given a schema object and a hashref of $connect_info, connect the replicant +and return it. + +=cut + +sub connect_replicant { + my ($self, $schema, $connect_info) = @_; + my $replicant = $self->create_replicant($schema); + + $replicant->connect_info($connect_info); + $replicant->ensure_connected; + DBIx::Class::Storage::DBI::Replicated::Replicant->meta->apply($replicant); + + return $replicant; +} + =head2 connected_replicants Returns true if there are connected replicants. Actually is overloaded to @@ -237,10 +252,8 @@ sub validate_replicants { $replicant->lag_behind_master <= $self->maximum_lag && $replicant->ensure_connected ) { - ## TODO:: Hook debug for this $replicant->active(1) } else { - ## TODO:: Hook debug for this $replicant->active(0); } } diff --git a/t/93storage_replication.t b/t/93storage_replication.t index a2a130d..d03f5c6 100644 --- a/t/93storage_replication.t +++ b/t/93storage_replication.t @@ -9,7 +9,7 @@ BEGIN { eval "use Moose; use Test::Moose"; plan $@ ? ( skip_all => 'needs Moose for testing' ) - : ( tests => 77 ); + : ( tests => 80 ); } use_ok 'DBIx::Class::Storage::DBI::Replicated::Pool'; @@ -569,6 +569,20 @@ ok $replicated->schema->resultset('Artist')->find(1) => 'Got expected single result from transaction'; } +## Private attribute tests + +{ + ok my $artist_rs = $replicated->schema->resultset('Artist') + => 'got artist resultset'; + + ## Turn on Reliable Storage + ok my $reliable_artist_rs = $artist_rs->search(undef, {execute_reliably=>1}) + => 'Created a resultset using reliable storage'; + + ok my $artist = $reliable_artist_rs->find(2) + => 'got an artist to test see the attributes'; +} + ## Delete the old database files $replicated->cleanup;