1 package DBIx::Class::Storage::DBI::Replicated::Pool;
4 use MooseX::AttributeHelpers;
5 use DBIx::Class::Storage::DBI::Replicated::Replicant;
7 use Scalar::Util 'reftype';
8 use Carp::Clan qw/^DBIx::Class/;
9 use MooseX::Types::Moose qw/Num Int ClassName HashRef/;
11 use namespace::clean -except => 'meta';
15 DBIx::Class::Storage::DBI::Replicated::Pool - Manage a pool of replicants
19 This class is used internally by L<DBIx::Class::Storage::DBI::Replicated>. You
20 shouldn't need to create instances of this class.
24 In a replicated storage type, there is at least one replicant to handle the
25 read only traffic. The Pool class manages this replicant, or list of
26 replicants, and gives some methods for querying information about their status.
30 This class defines the following attributes.
32 =head2 maximum_lag ($num)
34 This is a number which defines the maximum allowed lag returned by the
35 L<DBIx::Class::Storage::DBI/lag_behind_master> method. The default is 0. In
36 general, this should return a larger number when the replicant is lagging
37 behind it's master, however the implementation of this is database specific, so
38 don't count on this number having a fixed meaning. For example, MySQL will
39 return a number of seconds that the replicating database is lagging.
43 has 'maximum_lag' => (
53 This is an integer representing a time since the last time the replicants were
54 validated. It's nothing fancy, just an integer provided via the perl time
59 has 'last_validated' => (
62 reader=>'last_validated',
63 writer=>'_last_validated',
68 =head2 replicant_type ($classname)
70 Base class used to instantiate replicants that are in the pool. Unless you
71 need to subclass L<DBIx::Class::Storage::DBI::Replicated::Replicant> you should
72 just leave this alone.
76 has 'replicant_type' => (
80 default=>'DBIx::Class::Storage::DBI',
82 'create_replicant' => 'new',
88 A hashref of replicant, with the key being the dsn and the value returning the
89 actual replicant storage. For example if the $dsn element is something like:
91 "dbi:SQLite:dbname=dbfile"
93 You could access the specific replicant via:
95 $schema->storage->replicants->{'dbname=dbfile'}
97 This attributes also supports the following helper methods:
101 =item set_replicant($key=>$storage)
103 Pushes a replicant onto the HashRef under $key
105 =item get_replicant($key)
107 Retrieves the named replicant
111 Returns true if the Pool defines replicants.
115 The number of replicants in the pool
117 =item delete_replicant ($key)
119 removes the replicant under $key from the pool
125 has 'replicants' => (
127 metaclass => 'Collection::Hash',
128 isa=>HashRef['DBIx::Class::Storage::DBI'],
131 'set' => 'set_replicant',
132 'get' => 'get_replicant',
133 'empty' => 'has_replicants',
134 'count' => 'num_replicants',
135 'delete' => 'delete_replicant',
141 This class defines the following methods.
143 =head2 connect_replicants ($schema, Array[$connect_info])
145 Given an array of $dsn suitable for connected to a database, create an
146 L<DBIx::Class::Storage::DBI::Replicated::Replicant> object and store it in the
147 L</replicants> attribute.
151 sub connect_replicants {
153 my $schema = shift @_;
155 my @newly_created = ();
156 foreach my $connect_info (@_) {
157 $connect_info = [ $connect_info ]
158 if reftype $connect_info ne 'ARRAY';
160 croak "coderef replicant connect_info not supported"
161 if ref $connect_info->[0] && reftype $connect_info->[0] eq 'CODE';
163 my $replicant = $self->connect_replicant($schema, $connect_info);
165 my $key = $connect_info->[0];
166 $key = $key->{dsn} if ref $key && reftype $key eq 'HASH';
167 ($key) = ($key =~ m/^dbi\:.+\:(.+)$/);
169 $self->set_replicant( $key => $replicant);
170 push @newly_created, $replicant;
173 return @newly_created;
176 =head2 connect_replicant ($schema, $connect_info)
178 Given a schema object and a hashref of $connect_info, connect the replicant
183 sub connect_replicant {
184 my ($self, $schema, $connect_info) = @_;
185 my $replicant = $self->create_replicant($schema);
186 $replicant->connect_info($connect_info);
187 $self->_safely_ensure_connected($replicant);
188 DBIx::Class::Storage::DBI::Replicated::Replicant->meta->apply($replicant);
192 =head2 _safely_ensure_connected ($replicant)
194 The standard ensure_connected method with throw an exception should it fail to
195 connect. For the master database this is desirable, but since replicants are
196 allowed to fail, this behavior is not desirable. This method wraps the call
197 to ensure_connected in an eval in order to catch any generated errors. That
198 way a slave to go completely offline (ie, the box itself can die) without
199 bringing down your entire pool of databases.
203 sub _safely_ensure_connected {
204 my ($self, $replicant, @args) = @_;
206 $replicant->ensure_connected(@args);
212 sprintf( "Exception trying to ->ensure_connected for replicant %s, error is %s",
213 $replicant->_dbi_connect_info->[0], $@)
220 =head2 connected_replicants
222 Returns true if there are connected replicants. Actually is overloaded to
223 return the number of replicants. So you can do stuff like:
225 if( my $num_connected = $storage->has_connected_replicants ) {
226 print "I have $num_connected connected replicants";
228 print "Sorry, no replicants.";
231 This method will actually test that each replicant in the L</replicants> hashref
232 is actually connected, try not to hit this 10 times a second.
236 sub connected_replicants {
240 } $self->all_replicants );
243 =head2 active_replicants
245 This is an array of replicants that are considered to be active in the pool.
246 This does not check to see if they are connected, but if they are not, DBIC
247 should automatically reconnect them for us when we hit them with a query.
251 sub active_replicants {
253 return ( grep {$_} map {
255 } $self->all_replicants );
258 =head2 all_replicants
260 Just a simple array of all the replicant storages. No particular order to the
261 array is given, nor should any meaning be derived.
267 return values %{$self->replicants};
270 =head2 validate_replicants
272 This does a check to see if 1) each replicate is connected (or reconnectable),
273 2) that is ->is_replicating, and 3) that it is not exceeding the lag amount
274 defined by L</maximum_lag>. Replicants that fail any of these tests are set to
275 inactive, and thus removed from the replication pool.
277 This tests L<all_replicants>, since a replicant that has been previous marked
278 as inactive can be reactived should it start to pass the validation tests again.
280 See L<DBIx::Class::Storage::DBI> for more about checking if a replicating
281 connection is not following a master or is lagging.
283 Calling this method will generate queries on the replicant databases so it is
284 not recommended that you run them very often.
286 This method requires that your underlying storage engine supports some sort of
287 native replication mechanism. Currently only MySQL native replication is
288 supported. Your patches to make other replication types work are welcomed.
292 sub validate_replicants {
294 foreach my $replicant($self->all_replicants) {
295 if($self->_safely_ensure_connected($replicant)) {
296 my $is_replicating = $replicant->is_replicating;
297 unless(defined $is_replicating) {
298 $replicant->debugobj->print("Storage Driver ".ref($self)." Does not support the 'is_replicating' method. Assuming you are manually managing.\n");
301 if($is_replicating) {
302 my $lag_behind_master = $replicant->lag_behind_master;
303 unless(defined $lag_behind_master) {
304 $replicant->debugobj->print("Storage Driver ".ref($self)." Does not support the 'lag_behind_master' method. Assuming you are manually managing.\n");
307 if($lag_behind_master <= $self->maximum_lag) {
308 $replicant->active(1);
310 $replicant->active(0);
314 $replicant->active(0);
318 $replicant->active(0);
321 ## Mark that we completed this validation.
322 $self->_last_validated(time);
327 John Napiorkowski <john.napiorkowski@takkle.com>
331 You may distribute this code under the same terms as Perl itself.
335 __PACKAGE__->meta->make_immutable;