1 package DBIx::Class::Storage::DBI::Replicated::Pool;
4 use MooseX::AttributeHelpers;
5 use DBIx::Class::Storage::DBI::Replicated::Replicant;
6 use List::Util qw(sum);
11 DBIx::Class::Storage::DBI::Replicated::Pool - Manage a pool of replicants
15 This class is used internally by L<DBIx::Class::Storage::DBI::Replicated>. You
16 shouldn't need to create instances of this class.
20 In a replicated storage type, there is at least one replicant to handle the
21 read only traffic. The Pool class manages this replicant, or list of
22 replicants, and gives some methods for querying information about their status.
26 This class defines the following attributes.
28 =head2 maximum_lag ($num)
30 This is a number which defines the maximum allowed lag returned by the
31 L<DBIx::Class::Storage::DBI/lag_behind_master> method. The default is 0. In
32 general, this should return a larger number when the replicant is lagging
33 behind it's master, however the implementation of this is database specific, so
34 don't count on this number having a fixed meaning. For example, MySQL will
35 return a number of seconds that the replicating database is lagging.
39 has 'maximum_lag' => (
49 This is an integer representing a time since the last time the replicants were
50 validated. It's nothing fancy, just an integer provided via the perl time
55 has 'last_validated' => (
58 reader=>'last_validated',
59 writer=>'_last_validated',
64 =head2 replicant_type ($classname)
66 Base class used to instantiate replicants that are in the pool. Unless you
67 need to subclass L<DBIx::Class::Storage::DBI::Replicated::Replicant> you should
68 just leave this alone.
72 has 'replicant_type' => (
76 default=>'DBIx::Class::Storage::DBI',
78 'create_replicant' => 'new',
84 A hashref of replicant, with the key being the dsn and the value returning the
85 actual replicant storage. For example if the $dsn element is something like:
87 "dbi:SQLite:dbname=dbfile"
89 You could access the specific replicant via:
91 $schema->storage->replicants->{'dbname=dbfile'}
93 This attributes also supports the following helper methods:
97 =item set_replicant($key=>$storage)
99 Pushes a replicant onto the HashRef under $key
101 =item get_replicant($key)
103 Retrieves the named replicant
107 Returns true if the Pool defines replicants.
111 The number of replicants in the pool
113 =item delete_replicant ($key)
115 removes the replicant under $key from the pool
121 has 'replicants' => (
123 metaclass => 'Collection::Hash',
124 isa=>'HashRef[DBIx::Class::Storage::DBI]',
127 'set' => 'set_replicant',
128 'get' => 'get_replicant',
129 'empty' => 'has_replicants',
130 'count' => 'num_replicants',
131 'delete' => 'delete_replicant',
137 This class defines the following methods.
139 =head2 connect_replicants ($schema, Array[$connect_info])
141 Given an array of $dsn suitable for connected to a database, create an
142 L<DBIx::Class::Storage::DBI::Replicated::Replicant> object and store it in the
143 L</replicants> attribute.
147 sub connect_replicants {
149 my $schema = shift @_;
151 my @newly_created = ();
152 foreach my $connect_info (@_) {
153 $connect_info = [ $connect_info ]
154 if Scalar::Util::reftype($connect_info) ne 'ARRAY';
156 my $replicant = $self->connect_replicant($schema, $connect_info);
158 my $key = $connect_info->[0];
159 $key = $key->{dsn} if Scalar::Util::reftype($key) eq 'HASH';
160 ($key) = ($key =~ m/^dbi\:.+\:(.+)$/);
162 $self->set_replicant( $key => $replicant);
163 push @newly_created, $replicant;
166 return @newly_created;
169 =head2 connect_replicant ($schema, $connect_info)
171 Given a schema object and a hashref of $connect_info, connect the replicant
176 sub connect_replicant {
177 my ($self, $schema, $connect_info) = @_;
178 my $replicant = $self->create_replicant($schema);
179 $replicant->connect_info($connect_info);
180 $self->_safely_ensure_connected($replicant);
181 DBIx::Class::Storage::DBI::Replicated::Replicant->meta->apply($replicant);
185 =head2 _safely_ensure_connected ($replicant)
187 The standard ensure_connected method with throw an exception should it fail to
188 connect. For the master database this is desirable, but since replicants are
189 allowed to fail, this behavior is not desirable. This method wraps the call
190 to ensure_connected in an eval in order to catch any generated errors. That
191 way a slave to go completely offline (ie, the box itself can die) without
192 bringing down your entire pool of databases.
196 sub _safely_ensure_connected {
197 my ($self, $replicant, @args) = @_;
199 $replicant->ensure_connected(@args);
205 sprintf( "Exception trying to ->ensure_connected for replicant %s, error is %s",
206 $replicant->_dbi_connect_info->[0], $@)
213 =head2 connected_replicants
215 Returns true if there are connected replicants. Actually is overloaded to
216 return the number of replicants. So you can do stuff like:
218 if( my $num_connected = $storage->has_connected_replicants ) {
219 print "I have $num_connected connected replicants";
221 print "Sorry, no replicants.";
224 This method will actually test that each replicant in the L</replicants> hashref
225 is actually connected, try not to hit this 10 times a second.
229 sub connected_replicants {
233 } $self->all_replicants );
236 =head2 active_replicants
238 This is an array of replicants that are considered to be active in the pool.
239 This does not check to see if they are connected, but if they are not, DBIC
240 should automatically reconnect them for us when we hit them with a query.
244 sub active_replicants {
246 return ( grep {$_} map {
248 } $self->all_replicants );
251 =head2 all_replicants
253 Just a simple array of all the replicant storages. No particular order to the
254 array is given, nor should any meaning be derived.
260 return values %{$self->replicants};
263 =head2 validate_replicants
265 This does a check to see if 1) each replicate is connected (or reconnectable),
266 2) that is ->is_replicating, and 3) that it is not exceeding the lag amount
267 defined by L</maximum_lag>. Replicants that fail any of these tests are set to
268 inactive, and thus removed from the replication pool.
270 This tests L<all_replicants>, since a replicant that has been previous marked
271 as inactive can be reactived should it start to pass the validation tests again.
273 See L<DBIx::Class::Storage::DBI> for more about checking if a replicating
274 connection is not following a master or is lagging.
276 Calling this method will generate queries on the replicant databases so it is
277 not recommended that you run them very often.
279 This method requires that your underlying storage engine supports some sort of
280 native replication mechanism. Currently only MySQL native replication is
281 supported. Your patches to make other replication types work are welcomed.
285 sub validate_replicants {
287 foreach my $replicant($self->all_replicants) {
288 if($self->_safely_ensure_connected($replicant)) {
289 my $is_replicating = $replicant->is_replicating;
290 unless(defined $is_replicating) {
291 $replicant->debugobj->print("Storage Driver ".ref $self." Does not support the 'is_replicating' method. Assuming you are manually managing.");
294 if($is_replicating) {
295 my $lag_behind_master = $replicant->lag_behind_master;
296 unless(defined $lag_behind_master) {
297 $replicant->debugobj->print("Storage Driver ".ref $self." Does not support the 'lag_behind_master' method. Assuming you are manually managing.");
300 if($lag_behind_master <= $self->maximum_lag) {
301 $replicant->active(1);
303 $replicant->active(0);
307 $replicant->active(0);
311 $replicant->active(0);
314 ## Mark that we completed this validation.
315 $self->_last_validated(time);
320 John Napiorkowski <john.napiorkowski@takkle.com>
324 You may distribute this code under the same terms as Perl itself.
328 __PACKAGE__->meta->make_immutable;