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';
10 use DBIx::Class::_Util 'dbic_internal_try';
13 use namespace::clean -except => 'meta';
17 DBIx::Class::Storage::DBI::Replicated::Pool - Manage a pool of replicants
21 This class is used internally by L<DBIx::Class::Storage::DBI::Replicated>. You
22 shouldn't need to create instances of this class.
26 In a replicated storage type, there is at least one replicant to handle the
27 read-only traffic. The Pool class manages this replicant, or list of
28 replicants, and gives some methods for querying information about their status.
32 This class defines the following attributes.
34 =head2 maximum_lag ($num)
36 This is a number which defines the maximum allowed lag returned by the
37 L<DBIx::Class::Storage::DBI/lag_behind_master> method. The default is 0. In
38 general, this should return a larger number when the replicant is lagging
39 behind its master, however the implementation of this is database specific, so
40 don't count on this number having a fixed meaning. For example, MySQL will
41 return a number of seconds that the replicating database is lagging.
45 has 'maximum_lag' => (
55 This is an integer representing a time since the last time the replicants were
56 validated. It's nothing fancy, just an integer provided via the perl L<time|perlfunc/time>
61 has 'last_validated' => (
64 reader=>'last_validated',
65 writer=>'_last_validated',
70 =head2 replicant_type ($classname)
72 Base class used to instantiate replicants that are in the pool. Unless you
73 need to subclass L<DBIx::Class::Storage::DBI::Replicated::Replicant> you should
74 just leave this alone.
78 has 'replicant_type' => (
82 default=>'DBIx::Class::Storage::DBI',
84 'create_replicant' => 'new',
90 A hashref of replicant, with the key being the dsn and the value returning the
91 actual replicant storage. For example, if the $dsn element is something like:
93 "dbi:SQLite:dbname=dbfile"
95 You could access the specific replicant via:
97 $schema->storage->replicants->{'dbname=dbfile'}
99 This attributes also supports the following helper methods:
103 =item set_replicant($key=>$storage)
105 Pushes a replicant onto the HashRef under $key
107 =item get_replicant($key)
109 Retrieves the named replicant
113 Returns true if the Pool defines replicants.
117 The number of replicants in the pool
119 =item delete_replicant ($key)
121 Removes the replicant under $key from the pool
127 has 'replicants' => (
130 isa=>HashRef['Object'],
133 'set_replicant' => 'set',
134 'get_replicant' => 'get',
135 'has_replicants' => 'is_empty',
136 'num_replicants' => 'count',
137 'delete_replicant' => 'delete',
138 'all_replicant_storages' => 'values',
142 around has_replicants => sub {
143 my ($orig, $self) = @_;
144 return !$self->$orig;
147 has next_unknown_replicant_id => (
149 traits => ['Counter'],
153 'inc_unknown_replicant_id' => 'inc',
159 Reference to the master Storage.
163 has master => (is => 'rw', isa => DBICStorageDBI, weak_ref => 1);
167 This class defines the following methods.
169 =head2 connect_replicants ($schema, Array[$connect_info])
171 Given an array of $dsn or connect_info structures suitable for connected to a
172 database, create an L<DBIx::Class::Storage::DBI::Replicated::Replicant> object
173 and store it in the L</replicants> attribute.
177 sub connect_replicants {
179 my $schema = shift @_;
181 my @newly_created = ();
182 foreach my $connect_info (@_) {
183 $connect_info = [ $connect_info ]
184 if reftype $connect_info ne 'ARRAY';
186 my $connect_coderef =
187 (reftype($connect_info->[0])||'') eq 'CODE' ? $connect_info->[0]
188 : (reftype($connect_info->[0])||'') eq 'HASH' &&
189 $connect_info->[0]->{dbh_maker};
193 # yes this is evil, but it only usually happens once (for coderefs)
194 # this will fail if the coderef does not actually DBI::connect
195 no warnings 'redefine';
196 my $connect = \&DBI::connect;
197 local *DBI::connect = sub {
201 $self->connect_replicant($schema, $connect_info);
207 if (!$connect_coderef) {
208 $dsn = $connect_info->[0];
209 $dsn = $dsn->{dsn} if (reftype($dsn)||'') eq 'HASH';
212 # all attempts to get the DSN failed
213 $key = "UNKNOWN_" . $self->next_unknown_replicant_id;
214 $self->inc_unknown_replicant_id;
218 $replicant->dsn($dsn);
219 ($key) = ($dsn =~ m/^dbi\:.+\:(.+)$/i);
222 $replicant->id($key);
223 $self->set_replicant($key => $replicant);
225 push @newly_created, $replicant;
228 return @newly_created;
231 =head2 connect_replicant ($schema, $connect_info)
233 Given a schema object and a hashref of $connect_info, connect the replicant
238 sub connect_replicant {
239 my ($self, $schema, $connect_info) = @_;
240 my $replicant = $self->create_replicant($schema);
241 $replicant->connect_info($connect_info);
243 ## It is undesirable for catalyst to connect at ->conect_replicants time, as
244 ## connections should only happen on the first request that uses the database.
245 ## So we try to set the driver without connecting, however this doesn't always
246 ## work, as a driver may need to connect to determine the DB version, and this
249 ## Why this is necessary at all, is that we need to have the final storage
250 ## class to apply the Replicant role.
252 $self->_safely($replicant, '->_determine_driver', sub {
253 $replicant->_determine_driver
256 Moose::Meta::Class->initialize(ref $replicant);
258 DBIx::Class::Storage::DBI::Replicated::Replicant->meta->apply($replicant);
260 # link back to master
261 $replicant->master($self->master);
266 =head2 _safely_ensure_connected ($replicant)
268 The standard ensure_connected method with throw an exception should it fail to
269 connect. For the master database this is desirable, but since replicants are
270 allowed to fail, this behavior is not desirable. This method wraps the call
271 to ensure_connected in an eval in order to catch any generated errors. That
272 way a slave can go completely offline (e.g. the box itself can die) without
273 bringing down your entire pool of databases.
277 sub _safely_ensure_connected {
278 my ($self, $replicant, @args) = @_;
280 return $self->_safely($replicant, '->ensure_connected', sub {
281 $replicant->ensure_connected(@args)
285 =head2 _safely ($replicant, $name, $code)
287 Execute C<$code> for operation C<$name> catching any exceptions and printing an
288 error message to the C<<$replicant->debugobj>>.
290 Returns 1 on success and undef on failure.
295 my ($self, $replicant, $name, $code) = @_;
297 return dbic_internal_try {
301 $replicant->debugobj->print(sprintf(
302 "Exception trying to $name for replicant %s, error is %s",
303 $replicant->_dbi_connect_info->[0], $_)
309 =head2 connected_replicants
311 Returns true if there are connected replicants. Actually is overloaded to
312 return the number of replicants. So you can do stuff like:
314 if( my $num_connected = $storage->has_connected_replicants ) {
315 print "I have $num_connected connected replicants";
317 print "Sorry, no replicants.";
320 This method will actually test that each replicant in the L</replicants> hashref
321 is actually connected, try not to hit this 10 times a second.
325 sub connected_replicants {
329 } $self->all_replicants );
332 =head2 active_replicants
334 This is an array of replicants that are considered to be active in the pool.
335 This does not check to see if they are connected, but if they are not, DBIC
336 should automatically reconnect them for us when we hit them with a query.
340 sub active_replicants {
342 return ( grep {$_} map {
344 } $self->all_replicants );
347 =head2 all_replicants
349 Just a simple array of all the replicant storages. No particular order to the
350 array is given, nor should any meaning be derived.
356 return values %{$self->replicants};
359 =head2 validate_replicants
361 This does a check to see if 1) each replicate is connected (or reconnectable),
362 2) that is ->is_replicating, and 3) that it is not exceeding the lag amount
363 defined by L</maximum_lag>. Replicants that fail any of these tests are set to
364 inactive, and thus removed from the replication pool.
366 This tests L</all_replicants>, since a replicant that has been previous marked
367 as inactive can be reactivated should it start to pass the validation tests again.
369 See L<DBIx::Class::Storage::DBI> for more about checking if a replicating
370 connection is not following a master or is lagging.
372 Calling this method will generate queries on the replicant databases so it is
373 not recommended that you run them very often.
375 This method requires that your underlying storage engine supports some sort of
376 native replication mechanism. Currently only MySQL native replication is
377 supported. Your patches to make other replication types work are welcomed.
381 sub validate_replicants {
383 foreach my $replicant($self->all_replicants) {
384 if($self->_safely_ensure_connected($replicant)) {
385 my $is_replicating = $replicant->is_replicating;
386 unless(defined $is_replicating) {
387 $replicant->debugobj->print("Storage Driver ".ref($self)." Does not support the 'is_replicating' method. Assuming you are manually managing.\n");
390 if($is_replicating) {
391 my $lag_behind_master = $replicant->lag_behind_master;
392 unless(defined $lag_behind_master) {
393 $replicant->debugobj->print("Storage Driver ".ref($self)." Does not support the 'lag_behind_master' method. Assuming you are manually managing.\n");
396 if($lag_behind_master <= $self->maximum_lag) {
397 $replicant->active(1);
399 $replicant->active(0);
403 $replicant->active(0);
407 $replicant->active(0);
410 ## Mark that we completed this validation.
411 $self->_last_validated(time);
414 =head1 FURTHER QUESTIONS?
416 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
418 =head1 COPYRIGHT AND LICENSE
420 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
421 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
422 redistribute it and/or modify it under the same terms as the
423 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
427 __PACKAGE__->meta->make_immutable;