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',
65 =head2 replicant_type ($classname)
67 Base class used to instantiate replicants that are in the pool. Unless you
68 need to subclass L<DBIx::Class::Storage::DBI::Replicated::Replicant> you should
69 just leave this alone.
73 has 'replicant_type' => (
77 default=>'DBIx::Class::Storage::DBI',
79 'create_replicant' => 'new',
85 A hashref of replicant, with the key being the dsn and the value returning the
86 actual replicant storage. For example if the $dsn element is something like:
88 "dbi:SQLite:dbname=dbfile"
90 You could access the specific replicant via:
92 $schema->storage->replicants->{'dbname=dbfile'}
94 This attributes also supports the following helper methods
98 =item set_replicant($key=>$storage)
100 Pushes a replicant onto the HashRef under $key
102 =item get_replicant($key)
104 Retrieves the named replicant
108 Returns true if the Pool defines replicants.
112 The number of replicants in the pool
114 =item delete_replicant ($key)
116 removes the replicant under $key from the pool
122 has 'replicants' => (
124 metaclass => 'Collection::Hash',
125 isa=>'HashRef[DBIx::Class::Storage::DBI]',
128 'set' => 'set_replicant',
129 'get' => 'get_replicant',
130 'empty' => 'has_replicants',
131 'count' => 'num_replicants',
132 'delete' => 'delete_replicant',
138 This class defines the following methods.
140 =head2 connect_replicants ($schema, Array[$connect_info])
142 Given an array of $dsn suitable for connected to a database, create an
143 L<DBIx::Class::Storage::DBI::Replicated::Replicant> object and store it in the
144 L</replicants> attribute.
148 sub connect_replicants {
150 my $schema = shift @_;
152 my @newly_created = ();
153 foreach my $connect_info (@_) {
155 my $replicant = $self->create_replicant($schema);
156 $replicant->connect_info($connect_info);
157 $replicant->ensure_connected;
158 DBIx::Class::Storage::DBI::Replicated::Replicant->meta->apply($replicant);
160 my ($key) = ($connect_info->[0]=~m/^dbi\:.+\:(.+)$/);
161 $self->set_replicant( $key => $replicant);
162 push @newly_created, $replicant;
165 return @newly_created;
168 =head2 connected_replicants
170 Returns true if there are connected replicants. Actually is overloaded to
171 return the number of replicants. So you can do stuff like:
173 if( my $num_connected = $storage->has_connected_replicants ) {
174 print "I have $num_connected connected replicants";
176 print "Sorry, no replicants.";
179 This method will actually test that each replicant in the L</replicants> hashref
180 is actually connected, try not to hit this 10 times a second.
184 sub connected_replicants {
188 } $self->all_replicants );
191 =head2 active_replicants
193 This is an array of replicants that are considered to be active in the pool.
194 This does not check to see if they are connected, but if they are not, DBIC
195 should automatically reconnect them for us when we hit them with a query.
199 sub active_replicants {
201 return ( grep {$_} map {
203 } $self->all_replicants );
206 =head2 all_replicants
208 Just a simple array of all the replicant storages. No particular order to the
209 array is given, nor should any meaning be derived.
215 return values %{$self->replicants};
218 =head2 validate_replicants
220 This does a check to see if 1) each replicate is connected (or reconnectable),
221 2) that is ->is_replicating, and 3) that it is not exceeding the lag amount
222 defined by L</maximum_lag>. Replicants that fail any of these tests are set to
223 inactive, and thus removed from the replication pool.
225 This tests L<all_replicants>, since a replicant that has been previous marked
226 as inactive can be reactived should it start to pass the validation tests again.
228 See L<DBIx::Class::Storage::DBI> for more about checking if a replicating
229 connection is not following a master or is lagging.
231 Calling this method will generate queries on the replicant databases so it is
232 not recommended that you run them very often.
236 sub validate_replicants {
238 foreach my $replicant($self->all_replicants) {
240 $replicant->is_replicating &&
241 $replicant->lag_behind_master <= $self->maximum_lag &&
242 $replicant->ensure_connected
244 ## TODO:: Hook debug for this
245 $replicant->active(1)
247 ## TODO:: Hook debug for this
248 $replicant->active(0);
252 ## Mark that we completed this validation.
253 $self->_last_validated(time);
258 John Napiorkowski <john.napiorkowski@takkle.com>
262 You may distribute this code under the same terms as Perl itself.