revert previous revision
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Replicated / Pool.pm
CommitLineData
26ab719a 1package DBIx::Class::Storage::DBI::Replicated::Pool;
2
3use Moose;
4use MooseX::AttributeHelpers;
5use DBIx::Class::Storage::DBI::Replicated::Replicant;
6use List::Util qw(sum);
7
8=head1 NAME
9
21fc4719 10DBIx::Class::Storage::DBI::Replicated::Pool - Manage a pool of replicants
26ab719a 11
12=head1 SYNOPSIS
13
14This class is used internally by L<DBIx::Class::Storage::DBI::Replicated>. You
15shouldn't need to create instances of this class.
64cdad22 16
26ab719a 17=head1 DESCRIPTION
18
19In a replicated storage type, there is at least one replicant to handle the
20read only traffic. The Pool class manages this replicant, or list of
21replicants, and gives some methods for querying information about their status.
22
23=head1 ATTRIBUTES
24
25This class defines the following attributes.
26
4a607d7a 27=head2 maximum_lag ($num)
28
29This is a number which defines the maximum allowed lag returned by the
30L<DBIx::Class::Storage::DBI/lag_behind_master> method. The default is 0. In
31general, this should return a larger number when the replicant is lagging
32behind it's master, however the implementation of this is database specific, so
33don't count on this number having a fixed meaning. For example, MySQL will
34return a number of seconds that the replicating database is lagging.
35
36=cut
37
38has 'maximum_lag' => (
64cdad22 39 is=>'rw',
40 isa=>'Num',
41 required=>1,
42 lazy=>1,
43 default=>0,
4a607d7a 44);
45
17b05c13 46=head2 last_validated
47
48This is an integer representing a time since the last time the replicants were
49validated. It's nothing fancy, just an integer provided via the perl time
50builtin.
51
52=cut
53
54has 'last_validated' => (
64cdad22 55 is=>'rw',
56 isa=>'Int',
57 reader=>'last_validated',
58 writer=>'_last_validated',
59 lazy=>1,
60 default=>0,
17b05c13 61);
62
4a607d7a 63=head2 replicant_type ($classname)
26ab719a 64
65Base class used to instantiate replicants that are in the pool. Unless you
66need to subclass L<DBIx::Class::Storage::DBI::Replicated::Replicant> you should
67just leave this alone.
68
69=cut
70
71has 'replicant_type' => (
64cdad22 72 is=>'ro',
73 isa=>'ClassName',
74 required=>1,
75 default=>'DBIx::Class::Storage::DBI',
76 handles=>{
77 'create_replicant' => 'new',
78 },
26ab719a 79);
80
26ab719a 81=head2 replicants
82
83A hashref of replicant, with the key being the dsn and the value returning the
84actual replicant storage. For example if the $dsn element is something like:
85
64cdad22 86 "dbi:SQLite:dbname=dbfile"
87
26ab719a 88You could access the specific replicant via:
89
64cdad22 90 $schema->storage->replicants->{'dbname=dbfile'}
91
92This attributes also supports the following helper methods:
26ab719a 93
94=over 4
95
96=item set_replicant($key=>$storage)
97
98Pushes a replicant onto the HashRef under $key
99
100=item get_replicant($key)
101
102Retrieves the named replicant
103
104=item has_replicants
105
106Returns true if the Pool defines replicants.
107
108=item num_replicants
109
110The number of replicants in the pool
111
112=item delete_replicant ($key)
113
114removes the replicant under $key from the pool
115
116=back
117
118=cut
119
120has 'replicants' => (
64cdad22 121 is=>'rw',
122 metaclass => 'Collection::Hash',
123 isa=>'HashRef[DBIx::Class::Storage::DBI]',
124 default=>sub {{}},
125 provides => {
126 'set' => 'set_replicant',
127 'get' => 'get_replicant',
128 'empty' => 'has_replicants',
129 'count' => 'num_replicants',
130 'delete' => 'delete_replicant',
131 },
26ab719a 132);
133
26ab719a 134=head1 METHODS
135
136This class defines the following methods.
137
955a6df6 138=head2 connect_replicants ($schema, Array[$connect_info])
26ab719a 139
140Given an array of $dsn suitable for connected to a database, create an
141L<DBIx::Class::Storage::DBI::Replicated::Replicant> object and store it in the
142L</replicants> attribute.
143
144=cut
145
955a6df6 146sub connect_replicants {
64cdad22 147 my $self = shift @_;
148 my $schema = shift @_;
149
150 my @newly_created = ();
151 foreach my $connect_info (@_) {
bbafcf26 152 my $replicant = $self->connect_replicant($schema, $connect_info);
64cdad22 153 my ($key) = ($connect_info->[0]=~m/^dbi\:.+\:(.+)$/);
154 $self->set_replicant( $key => $replicant);
155 push @newly_created, $replicant;
156 }
157
158 return @newly_created;
26ab719a 159}
160
bbafcf26 161=head2 connect_replicant ($schema, $connect_info)
162
163Given a schema object and a hashref of $connect_info, connect the replicant
164and return it.
165
166=cut
167
168sub connect_replicant {
169 my ($self, $schema, $connect_info) = @_;
170 my $replicant = $self->create_replicant($schema);
f15afa13 171 $replicant->connect_info($connect_info);
172 $self->_safely_ensure_connected($replicant);
173 DBIx::Class::Storage::DBI::Replicated::Replicant->meta->apply($replicant);
bbafcf26 174 return $replicant;
175}
176
f15afa13 177=head2 _safely_ensure_connected ($replicant)
178
179The standard ensure_connected method with throw an exception should it fail to
180connect. For the master database this is desirable, but since replicants are
181allowed to fail, this behavior is not desirable. This method wraps the call
182to ensure_connected in an eval in order to catch any generated errors. That
183way a slave to go completely offline (ie, the box itself can die) without
184bringing down your entire pool of databases.
185
186=cut
187
188sub _safely_ensure_connected {
189 my ($self, $replicant, @args) = @_;
6ffb5be5 190 eval {
191 $replicant->ensure_connected(@args);
192 };
193 if ($@) {
13b9e828 194 $replicant
6ffb5be5 195 ->debugobj
196 ->print(
197 sprintf( "Exception trying to ->ensure_connected for replicant %s, error is %s",
198 $replicant->_dbi_connect_info->[0], $@)
13b9e828 199 );
6ffb5be5 200 return;
13b9e828 201 }
6ffb5be5 202 return 1;
f15afa13 203}
204
26ab719a 205=head2 connected_replicants
206
207Returns true if there are connected replicants. Actually is overloaded to
208return the number of replicants. So you can do stuff like:
209
64cdad22 210 if( my $num_connected = $storage->has_connected_replicants ) {
211 print "I have $num_connected connected replicants";
212 } else {
213 print "Sorry, no replicants.";
214 }
26ab719a 215
216This method will actually test that each replicant in the L</replicants> hashref
217is actually connected, try not to hit this 10 times a second.
218
219=cut
220
221sub connected_replicants {
64cdad22 222 my $self = shift @_;
223 return sum( map {
224 $_->connected ? 1:0
225 } $self->all_replicants );
26ab719a 226}
227
50336325 228=head2 active_replicants
229
230This is an array of replicants that are considered to be active in the pool.
231This does not check to see if they are connected, but if they are not, DBIC
232should automatically reconnect them for us when we hit them with a query.
233
234=cut
235
236sub active_replicants {
64cdad22 237 my $self = shift @_;
238 return ( grep {$_} map {
239 $_->active ? $_:0
240 } $self->all_replicants );
50336325 241}
242
26ab719a 243=head2 all_replicants
244
245Just a simple array of all the replicant storages. No particular order to the
246array is given, nor should any meaning be derived.
247
248=cut
249
250sub all_replicants {
64cdad22 251 my $self = shift @_;
252 return values %{$self->replicants};
26ab719a 253}
254
4a607d7a 255=head2 validate_replicants
256
257This does a check to see if 1) each replicate is connected (or reconnectable),
2582) that is ->is_replicating, and 3) that it is not exceeding the lag amount
259defined by L</maximum_lag>. Replicants that fail any of these tests are set to
260inactive, and thus removed from the replication pool.
261
262This tests L<all_replicants>, since a replicant that has been previous marked
263as inactive can be reactived should it start to pass the validation tests again.
264
265See L<DBIx::Class::Storage::DBI> for more about checking if a replicating
266connection is not following a master or is lagging.
267
268Calling this method will generate queries on the replicant databases so it is
269not recommended that you run them very often.
270
13b9e828 271This method requires that your underlying storage engine supports some sort of
272native replication mechanism. Currently only MySQL native replication is
273supported. Your patches to make other replication types work are welcomed.
274
4a607d7a 275=cut
276
277sub validate_replicants {
64cdad22 278 my $self = shift @_;
279 foreach my $replicant($self->all_replicants) {
13b9e828 280 if($self->_safely_ensure_connected($replicant)) {
281 my $is_replicating = $replicant->is_replicating;
282 unless(defined $is_replicating) {
283 $replicant->debugobj->print("Storage Driver ".ref $self." Does not support the 'is_replicating' method. Assuming you are manually managing.");
284 next;
285 } else {
286 if($is_replicating) {
287 my $lag_behind_master = $replicant->lag_behind_master;
288 unless(defined $lag_behind_master) {
289 $replicant->debugobj->print("Storage Driver ".ref $self." Does not support the 'lag_behind_master' method. Assuming you are manually managing.");
290 next;
291 } else {
292 if($lag_behind_master <= $self->maximum_lag) {
293 $replicant->active(1);
294 } else {
295 $replicant->active(0);
296 }
297 }
298 } else {
299 $replicant->active(0);
300 }
301 }
64cdad22 302 } else {
64cdad22 303 $replicant->active(0);
7edf5f1c 304 }
64cdad22 305 }
306 ## Mark that we completed this validation.
13b9e828 307 $self->_last_validated(time);
4a607d7a 308}
309
26ab719a 310=head1 AUTHOR
311
312John Napiorkowski <john.napiorkowski@takkle.com>
313
314=head1 LICENSE
315
316You may distribute this code under the same terms as Perl itself.
317
318=cut
319
c354902c 320__PACKAGE__->meta->make_immutable;
321
cb6ec758 3221;