1 package DBIx::Class::Storage::DBI::Replicated::Pool;
4 use DBIx::Class::Storage::DBI::Replicated::Replicant;
6 use Scalar::Util 'reftype';
8 use MooseX::Types::Moose qw/Num Int ClassName HashRef/;
9 use DBIx::Class::Storage::DBI::Replicated::Types 'DBICStorageDBI';
12 use namespace::clean -except => 'meta';
16 DBIx::Class::Storage::DBI::Replicated::Pool - Manage a pool of replicants
20 This class is used internally by L<DBIx::Class::Storage::DBI::Replicated>. You
21 shouldn't need to create instances of this class.
25 In a replicated storage type, there is at least one replicant to handle the
26 read-only traffic. The Pool class manages this replicant, or list of
27 replicants, and gives some methods for querying information about their status.
31 This class defines the following attributes.
33 =head2 maximum_lag ($num)
35 This is a number which defines the maximum allowed lag returned by the
36 L<DBIx::Class::Storage::DBI/lag_behind_master> method. The default is 0. In
37 general, this should return a larger number when the replicant is lagging
38 behind its master, however the implementation of this is database specific, so
39 don't count on this number having a fixed meaning. For example, MySQL will
40 return a number of seconds that the replicating database is lagging.
44 has 'maximum_lag' => (
54 This is an integer representing a time since the last time the replicants were
55 validated. It's nothing fancy, just an integer provided via the perl L<time|perlfunc/time>
60 has 'last_validated' => (
63 reader=>'last_validated',
64 writer=>'_last_validated',
69 =head2 replicant_type ($classname)
71 Base class used to instantiate replicants that are in the pool. Unless you
72 need to subclass L<DBIx::Class::Storage::DBI::Replicated::Replicant> you should
73 just leave this alone.
77 has 'replicant_type' => (
81 default=>'DBIx::Class::Storage::DBI',
83 'create_replicant' => 'new',
89 A hashref of replicant, with the key being the dsn and the value returning the
90 actual replicant storage. For example, if the $dsn element is something like:
92 "dbi:SQLite:dbname=dbfile"
94 You could access the specific replicant via:
96 $schema->storage->replicants->{'dbname=dbfile'}
98 This attributes also supports the following helper methods:
102 =item set_replicant($key=>$storage)
104 Pushes a replicant onto the HashRef under $key
106 =item get_replicant($key)
108 Retrieves the named replicant
112 Returns true if the Pool defines replicants.
116 The number of replicants in the pool
118 =item delete_replicant ($key)
120 Removes the replicant under $key from the pool
126 has 'replicants' => (
129 isa=>HashRef['Object'],
132 'set_replicant' => 'set',
133 'get_replicant' => 'get',
134 'has_replicants' => 'is_empty',
135 'num_replicants' => 'count',
136 'delete_replicant' => 'delete',
137 'all_replicant_storages' => 'values',
141 around has_replicants => sub {
142 my ($orig, $self) = @_;
143 return !$self->$orig;
146 has next_unknown_replicant_id => (
148 traits => ['Counter'],
152 'inc_unknown_replicant_id' => 'inc',
158 Reference to the master Storage.
162 has master => (is => 'rw', isa => DBICStorageDBI, weak_ref => 1);
166 This class defines the following methods.
168 =head2 connect_replicants ($schema, Array[$connect_info])
170 Given an array of $dsn or connect_info structures suitable for connected to a
171 database, create an L<DBIx::Class::Storage::DBI::Replicated::Replicant> object
172 and store it in the L</replicants> attribute.
176 sub connect_replicants {
178 my $schema = shift @_;
180 my @newly_created = ();
181 foreach my $connect_info (@_) {
182 $connect_info = [ $connect_info ]
183 if reftype $connect_info ne 'ARRAY';
185 my $connect_coderef =
186 (reftype($connect_info->[0])||'') eq 'CODE' ? $connect_info->[0]
187 : (reftype($connect_info->[0])||'') eq 'HASH' &&
188 $connect_info->[0]->{dbh_maker};
192 # yes this is evil, but it only usually happens once (for coderefs)
193 # this will fail if the coderef does not actually DBI::connect
194 no warnings 'redefine';
195 my $connect = \&DBI::connect;
196 local *DBI::connect = sub {
200 $self->connect_replicant($schema, $connect_info);
206 if (!$connect_coderef) {
207 $dsn = $connect_info->[0];
208 $dsn = $dsn->{dsn} if (reftype($dsn)||'') eq 'HASH';
211 # all attempts to get the DSN failed
212 $key = "UNKNOWN_" . $self->next_unknown_replicant_id;
213 $self->inc_unknown_replicant_id;
217 $replicant->dsn($dsn);
218 ($key) = ($dsn =~ m/^dbi\:.+\:(.+)$/i);
221 $replicant->id($key);
222 $self->set_replicant($key => $replicant);
224 push @newly_created, $replicant;
227 return @newly_created;
230 =head2 connect_replicant ($schema, $connect_info)
232 Given a schema object and a hashref of $connect_info, connect the replicant
237 sub connect_replicant {
238 my ($self, $schema, $connect_info) = @_;
239 my $replicant = $self->create_replicant($schema);
240 $replicant->connect_info($connect_info);
242 ## It is undesirable for catalyst to connect at ->conect_replicants time, as
243 ## connections should only happen on the first request that uses the database.
244 ## So we try to set the driver without connecting, however this doesn't always
245 ## work, as a driver may need to connect to determine the DB version, and this
248 ## Why this is necessary at all, is that we need to have the final storage
249 ## class to apply the Replicant role.
251 $self->_safely($replicant, '->_determine_driver', sub {
252 $replicant->_determine_driver
255 Moose::Meta::Class->initialize(ref $replicant);
257 DBIx::Class::Storage::DBI::Replicated::Replicant->meta->apply($replicant);
259 # link back to master
260 $replicant->master($self->master);
265 =head2 _safely_ensure_connected ($replicant)
267 The standard ensure_connected method with throw an exception should it fail to
268 connect. For the master database this is desirable, but since replicants are
269 allowed to fail, this behavior is not desirable. This method wraps the call
270 to ensure_connected in an eval in order to catch any generated errors. That
271 way a slave can go completely offline (e.g. the box itself can die) without
272 bringing down your entire pool of databases.
276 sub _safely_ensure_connected {
277 my ($self, $replicant, @args) = @_;
279 return $self->_safely($replicant, '->ensure_connected', sub {
280 $replicant->ensure_connected(@args)
284 =head2 _safely ($replicant, $name, $code)
286 Execute C<$code> for operation C<$name> catching any exceptions and printing an
287 error message to the C<<$replicant->debugobj>>.
289 Returns 1 on success and undef on failure.
294 my ($self, $replicant, $name, $code) = @_;
300 $replicant->debugobj->print(sprintf(
301 "Exception trying to $name for replicant %s, error is %s",
302 $replicant->_dbi_connect_info->[0], $_)
308 =head2 connected_replicants
310 Returns true if there are connected replicants. Actually is overloaded to
311 return the number of replicants. So you can do stuff like:
313 if( my $num_connected = $storage->has_connected_replicants ) {
314 print "I have $num_connected connected replicants";
316 print "Sorry, no replicants.";
319 This method will actually test that each replicant in the L</replicants> hashref
320 is actually connected, try not to hit this 10 times a second.
324 sub connected_replicants {
328 } $self->all_replicants );
331 =head2 active_replicants
333 This is an array of replicants that are considered to be active in the pool.
334 This does not check to see if they are connected, but if they are not, DBIC
335 should automatically reconnect them for us when we hit them with a query.
339 sub active_replicants {
341 return ( grep {$_} map {
343 } $self->all_replicants );
346 =head2 all_replicants
348 Just a simple array of all the replicant storages. No particular order to the
349 array is given, nor should any meaning be derived.
355 return values %{$self->replicants};
358 =head2 validate_replicants
360 This does a check to see if 1) each replicate is connected (or reconnectable),
361 2) that is ->is_replicating, and 3) that it is not exceeding the lag amount
362 defined by L</maximum_lag>. Replicants that fail any of these tests are set to
363 inactive, and thus removed from the replication pool.
365 This tests L</all_replicants>, since a replicant that has been previous marked
366 as inactive can be reactivated should it start to pass the validation tests again.
368 See L<DBIx::Class::Storage::DBI> for more about checking if a replicating
369 connection is not following a master or is lagging.
371 Calling this method will generate queries on the replicant databases so it is
372 not recommended that you run them very often.
374 This method requires that your underlying storage engine supports some sort of
375 native replication mechanism. Currently only MySQL native replication is
376 supported. Your patches to make other replication types work are welcomed.
380 sub validate_replicants {
382 foreach my $replicant($self->all_replicants) {
383 if($self->_safely_ensure_connected($replicant)) {
384 my $is_replicating = $replicant->is_replicating;
385 unless(defined $is_replicating) {
386 $replicant->debugobj->print("Storage Driver ".ref($self)." Does not support the 'is_replicating' method. Assuming you are manually managing.\n");
389 if($is_replicating) {
390 my $lag_behind_master = $replicant->lag_behind_master;
391 unless(defined $lag_behind_master) {
392 $replicant->debugobj->print("Storage Driver ".ref($self)." Does not support the 'lag_behind_master' method. Assuming you are manually managing.\n");
395 if($lag_behind_master <= $self->maximum_lag) {
396 $replicant->active(1);
398 $replicant->active(0);
402 $replicant->active(0);
406 $replicant->active(0);
409 ## Mark that we completed this validation.
410 $self->_last_validated(time);
413 =head1 FURTHER QUESTIONS?
415 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
417 =head1 COPYRIGHT AND LICENSE
419 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
420 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
421 redistribute it and/or modify it under the same terms as the
422 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
426 __PACKAGE__->meta->make_immutable;