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);
10 DBIx::Class::Storage::DBI::Replicated::Pool - Manage a pool of replicants
14 This class is used internally by L<DBIx::Class::Storage::DBI::Replicated>. You
15 shouldn't need to create instances of this class.
19 In a replicated storage type, there is at least one replicant to handle the
20 read only traffic. The Pool class manages this replicant, or list of
21 replicants, and gives some methods for querying information about their status.
25 This class defines the following attributes.
27 =head2 maximum_lag ($num)
29 This is a number which defines the maximum allowed lag returned by the
30 L<DBIx::Class::Storage::DBI/lag_behind_master> method. The default is 0. In
31 general, this should return a larger number when the replicant is lagging
32 behind it's master, however the implementation of this is database specific, so
33 don't count on this number having a fixed meaning. For example, MySQL will
34 return a number of seconds that the replicating database is lagging.
38 has 'maximum_lag' => (
48 This is an integer representing a time since the last time the replicants were
49 validated. It's nothing fancy, just an integer provided via the perl time
54 has 'last_validated' => (
57 reader=>'last_validated',
58 writer=>'_last_validated',
63 =head2 replicant_type ($classname)
65 Base class used to instantiate replicants that are in the pool. Unless you
66 need to subclass L<DBIx::Class::Storage::DBI::Replicated::Replicant> you should
67 just leave this alone.
71 has 'replicant_type' => (
75 default=>'DBIx::Class::Storage::DBI',
77 'create_replicant' => 'new',
83 A hashref of replicant, with the key being the dsn and the value returning the
84 actual replicant storage. For example if the $dsn element is something like:
86 "dbi:SQLite:dbname=dbfile"
88 You could access the specific replicant via:
90 $schema->storage->replicants->{'dbname=dbfile'}
92 This attributes also supports the following helper methods:
96 =item set_replicant($key=>$storage)
98 Pushes a replicant onto the HashRef under $key
100 =item get_replicant($key)
102 Retrieves the named replicant
106 Returns true if the Pool defines replicants.
110 The number of replicants in the pool
112 =item delete_replicant ($key)
114 removes the replicant under $key from the pool
120 has 'replicants' => (
122 metaclass => 'Collection::Hash',
123 isa=>'HashRef[DBIx::Class::Storage::DBI]',
126 'set' => 'set_replicant',
127 'get' => 'get_replicant',
128 'empty' => 'has_replicants',
129 'count' => 'num_replicants',
130 'delete' => 'delete_replicant',
136 This class defines the following methods.
138 =head2 connect_replicants ($schema, Array[$connect_info])
140 Given an array of $dsn suitable for connected to a database, create an
141 L<DBIx::Class::Storage::DBI::Replicated::Replicant> object and store it in the
142 L</replicants> attribute.
146 sub connect_replicants {
148 my $schema = shift @_;
150 my @newly_created = ();
151 foreach my $connect_info (@_) {
152 my $replicant = $self->connect_replicant($schema, $connect_info);
153 my ($key) = ($connect_info->[0]=~m/^dbi\:.+\:(.+)$/);
154 $self->set_replicant( $key => $replicant);
155 push @newly_created, $replicant;
158 return @newly_created;
161 =head2 connect_replicant ($schema, $connect_info)
163 Given a schema object and a hashref of $connect_info, connect the replicant
168 sub connect_replicant {
169 my ($self, $schema, $connect_info) = @_;
170 my $replicant = $self->create_replicant($schema);
171 $replicant->connect_info($connect_info);
172 $self->_safely_ensure_connected($replicant);
173 DBIx::Class::Storage::DBI::Replicated::Replicant->meta->apply($replicant);
177 =head2 _safely_ensure_connected ($replicant)
179 The standard ensure_connected method with throw an exception should it fail to
180 connect. For the master database this is desirable, but since replicants are
181 allowed to fail, this behavior is not desirable. This method wraps the call
182 to ensure_connected in an eval in order to catch any generated errors. That
183 way a slave to go completely offline (ie, the box itself can die) without
184 bringing down your entire pool of databases.
188 sub _safely_ensure_connected {
189 my ($self, $replicant, @args) = @_;
191 $return = $replicant->ensure_connected(@args);
196 sprintf( "Exception trying to ->ensure_connected for replicant %s, error is %s",
197 $self->_dbi_connect_info->[0], $@)
203 =head2 connected_replicants
205 Returns true if there are connected replicants. Actually is overloaded to
206 return the number of replicants. So you can do stuff like:
208 if( my $num_connected = $storage->has_connected_replicants ) {
209 print "I have $num_connected connected replicants";
211 print "Sorry, no replicants.";
214 This method will actually test that each replicant in the L</replicants> hashref
215 is actually connected, try not to hit this 10 times a second.
219 sub connected_replicants {
223 } $self->all_replicants );
226 =head2 active_replicants
228 This is an array of replicants that are considered to be active in the pool.
229 This does not check to see if they are connected, but if they are not, DBIC
230 should automatically reconnect them for us when we hit them with a query.
234 sub active_replicants {
236 return ( grep {$_} map {
238 } $self->all_replicants );
241 =head2 all_replicants
243 Just a simple array of all the replicant storages. No particular order to the
244 array is given, nor should any meaning be derived.
250 return values %{$self->replicants};
253 =head2 validate_replicants
255 This does a check to see if 1) each replicate is connected (or reconnectable),
256 2) that is ->is_replicating, and 3) that it is not exceeding the lag amount
257 defined by L</maximum_lag>. Replicants that fail any of these tests are set to
258 inactive, and thus removed from the replication pool.
260 This tests L<all_replicants>, since a replicant that has been previous marked
261 as inactive can be reactived should it start to pass the validation tests again.
263 See L<DBIx::Class::Storage::DBI> for more about checking if a replicating
264 connection is not following a master or is lagging.
266 Calling this method will generate queries on the replicant databases so it is
267 not recommended that you run them very often.
269 This method requires that your underlying storage engine supports some sort of
270 native replication mechanism. Currently only MySQL native replication is
271 supported. Your patches to make other replication types work are welcomed.
275 sub validate_replicants {
277 foreach my $replicant($self->all_replicants) {
278 if($self->_safely_ensure_connected($replicant)) {
279 my $is_replicating = $replicant->is_replicating;
280 unless(defined $is_replicating) {
281 $replicant->debugobj->print("Storage Driver ".ref $self." Does not support the 'is_replicating' method. Assuming you are manually managing.");
284 if($is_replicating) {
285 my $lag_behind_master = $replicant->lag_behind_master;
286 unless(defined $lag_behind_master) {
287 $replicant->debugobj->print("Storage Driver ".ref $self." Does not support the 'lag_behind_master' method. Assuming you are manually managing.");
290 if($lag_behind_master <= $self->maximum_lag) {
291 $replicant->active(1);
293 $replicant->active(0);
297 $replicant->active(0);
301 $replicant->active(0);
304 ## Mark that we completed this validation.
305 $self->_last_validated(time);
310 John Napiorkowski <john.napiorkowski@takkle.com>
314 You may distribute this code under the same terms as Perl itself.
318 __PACKAGE__->meta->make_immutable;