workaround for Moose bug affecting Replicated storage
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Replicated / Pool.pm
CommitLineData
26ab719a 1package DBIx::Class::Storage::DBI::Replicated::Pool;
2
3use Moose;
26ab719a 4use DBIx::Class::Storage::DBI::Replicated::Replicant;
9901aad7 5use List::Util 'sum';
6use Scalar::Util 'reftype';
0bd8e058 7use DBI ();
9901aad7 8use Carp::Clan qw/^DBIx::Class/;
41916570 9use MooseX::Types::Moose qw/Num Int ClassName HashRef/;
cea43436 10use DBIx::Class::Storage::DBI::Replicated::Types 'DBICStorageDBI';
9901aad7 11
12use namespace::clean -except => 'meta';
26ab719a 13
14=head1 NAME
15
21fc4719 16DBIx::Class::Storage::DBI::Replicated::Pool - Manage a pool of replicants
26ab719a 17
18=head1 SYNOPSIS
19
20This class is used internally by L<DBIx::Class::Storage::DBI::Replicated>. You
21shouldn't need to create instances of this class.
d4daee7b 22
26ab719a 23=head1 DESCRIPTION
24
25In a replicated storage type, there is at least one replicant to handle the
26read only traffic. The Pool class manages this replicant, or list of
27replicants, and gives some methods for querying information about their status.
28
29=head1 ATTRIBUTES
30
31This class defines the following attributes.
32
4a607d7a 33=head2 maximum_lag ($num)
34
35This is a number which defines the maximum allowed lag returned by the
36L<DBIx::Class::Storage::DBI/lag_behind_master> method. The default is 0. In
37general, this should return a larger number when the replicant is lagging
faaba25f 38behind its master, however the implementation of this is database specific, so
4a607d7a 39don't count on this number having a fixed meaning. For example, MySQL will
40return a number of seconds that the replicating database is lagging.
41
42=cut
43
44has 'maximum_lag' => (
64cdad22 45 is=>'rw',
41916570 46 isa=>Num,
64cdad22 47 required=>1,
48 lazy=>1,
49 default=>0,
4a607d7a 50);
51
17b05c13 52=head2 last_validated
53
54This is an integer representing a time since the last time the replicants were
faaba25f 55validated. It's nothing fancy, just an integer provided via the perl L<time|perlfunc/time>
17b05c13 56builtin.
57
58=cut
59
60has 'last_validated' => (
64cdad22 61 is=>'rw',
41916570 62 isa=>Int,
64cdad22 63 reader=>'last_validated',
64 writer=>'_last_validated',
65 lazy=>1,
66 default=>0,
17b05c13 67);
68
4a607d7a 69=head2 replicant_type ($classname)
26ab719a 70
71Base class used to instantiate replicants that are in the pool. Unless you
72need to subclass L<DBIx::Class::Storage::DBI::Replicated::Replicant> you should
73just leave this alone.
74
75=cut
76
77has 'replicant_type' => (
64cdad22 78 is=>'ro',
41916570 79 isa=>ClassName,
64cdad22 80 required=>1,
81 default=>'DBIx::Class::Storage::DBI',
82 handles=>{
83 'create_replicant' => 'new',
84 },
26ab719a 85);
86
26ab719a 87=head2 replicants
88
89A hashref of replicant, with the key being the dsn and the value returning the
90actual replicant storage. For example if the $dsn element is something like:
91
64cdad22 92 "dbi:SQLite:dbname=dbfile"
d4daee7b 93
26ab719a 94You could access the specific replicant via:
95
64cdad22 96 $schema->storage->replicants->{'dbname=dbfile'}
d4daee7b 97
64cdad22 98This attributes also supports the following helper methods:
26ab719a 99
100=over 4
101
102=item set_replicant($key=>$storage)
103
104Pushes a replicant onto the HashRef under $key
105
106=item get_replicant($key)
107
108Retrieves the named replicant
109
110=item has_replicants
111
112Returns true if the Pool defines replicants.
113
114=item num_replicants
115
116The number of replicants in the pool
117
118=item delete_replicant ($key)
119
120removes the replicant under $key from the pool
121
122=back
123
124=cut
125
126has 'replicants' => (
64cdad22 127 is=>'rw',
c4d78acb 128 traits => ['Hash'],
071bbccb 129 isa=>HashRef['Object'],
64cdad22 130 default=>sub {{}},
c4d78acb 131 handles => {
132 'set_replicant' => 'set',
133 'get_replicant' => 'get',
134 'has_replicants' => 'is_empty',
135 'num_replicants' => 'count',
136 'delete_replicant' => 'delete',
137 'all_replicant_storages' => 'values',
64cdad22 138 },
26ab719a 139);
140
c4d78acb 141around has_replicants => sub {
142 my ($orig, $self) = @_;
143 return !$self->$orig;
144};
145
ede99b9f 146has next_unknown_replicant_id => (
147 is => 'rw',
c4d78acb 148 traits => ['Counter'],
ede99b9f 149 isa => Int,
150 default => 1,
c4d78acb 151 handles => {
152 'inc_unknown_replicant_id' => 'inc',
ede99b9f 153 },
154);
155
cea43436 156=head2 master
157
158Reference to the master Storage.
159
160=cut
161
162has master => (is => 'rw', isa => DBICStorageDBI, weak_ref => 1);
163
26ab719a 164=head1 METHODS
165
166This class defines the following methods.
167
955a6df6 168=head2 connect_replicants ($schema, Array[$connect_info])
26ab719a 169
d40080c3 170Given an array of $dsn or connect_info structures suitable for connected to a
171database, create an L<DBIx::Class::Storage::DBI::Replicated::Replicant> object
172and store it in the L</replicants> attribute.
26ab719a 173
174=cut
175
955a6df6 176sub connect_replicants {
64cdad22 177 my $self = shift @_;
178 my $schema = shift @_;
d4daee7b 179
64cdad22 180 my @newly_created = ();
181 foreach my $connect_info (@_) {
2cd3ccc4 182 $connect_info = [ $connect_info ]
9901aad7 183 if reftype $connect_info ne 'ARRAY';
184
0bd8e058 185 my $connect_coderef =
186 (reftype($connect_info->[0])||'') eq 'CODE' ? $connect_info->[0]
187 : (reftype($connect_info->[0])||'') eq 'HASH' &&
188 $connect_info->[0]->{dbh_maker};
189
190 my $dsn;
4c91f824 191 my $replicant = do {
ede99b9f 192# yes this is evil, but it only usually happens once (for coderefs)
193# this will fail if the coderef does not actually DBI::connect
0bd8e058 194 no warnings 'redefine';
195 my $connect = \&DBI::connect;
196 local *DBI::connect = sub {
197 $dsn = $_[1];
198 goto $connect;
199 };
4c91f824 200 $self->connect_replicant($schema, $connect_info);
201 };
202
ede99b9f 203 my $key;
204
205 if (!$dsn) {
206 if (!$connect_coderef) {
207 $dsn = $connect_info->[0];
208 $dsn = $dsn->{dsn} if (reftype($dsn)||'') eq 'HASH';
209 }
210 else {
211 # all attempts to get the DSN failed
212 $key = "UNKNOWN_" . $self->next_unknown_replicant_id;
213 $self->inc_unknown_replicant_id;
214 }
215 }
216 if ($dsn) {
217 $replicant->dsn($dsn);
218 ($key) = ($dsn =~ m/^dbi\:.+\:(.+)$/i);
0bd8e058 219 }
0bd8e058 220
ede99b9f 221 $replicant->id($key);
0bd8e058 222 $self->set_replicant($key => $replicant);
ede99b9f 223
64cdad22 224 push @newly_created, $replicant;
225 }
d4daee7b 226
64cdad22 227 return @newly_created;
26ab719a 228}
229
bbafcf26 230=head2 connect_replicant ($schema, $connect_info)
231
232Given a schema object and a hashref of $connect_info, connect the replicant
233and return it.
234
235=cut
236
237sub connect_replicant {
238 my ($self, $schema, $connect_info) = @_;
239 my $replicant = $self->create_replicant($schema);
f15afa13 240 $replicant->connect_info($connect_info);
d40080c3 241
242## It is undesirable for catalyst to connect at ->conect_replicants time, as
243## connections should only happen on the first request that uses the database.
244## So we try to set the driver without connecting, however this doesn't always
245## work, as a driver may need to connect to determine the DB version, and this
246## may fail.
d6e80959 247##
248## Why this is necessary at all, is that we need to have the final storage
249## class to apply the Replicant role.
d40080c3 250
251 $self->_safely($replicant, '->_determine_driver', sub {
252 $replicant->_determine_driver
253 });
254
cea43436 255 Moose::Meta::Class->initialize(ref $replicant);
256
257 my $class = Moose::Meta::Class->create_anon_class(
258 superclasses => [ ref $replicant ],
259 roles => [ 'DBIx::Class::Storage::DBI::Replicated::Replicant' ],
260 cache => 1,
261 );
262 $class->rebless_instance($replicant);
263
264 # link back to master
265 $replicant->master($self->master);
266
bbafcf26 267 return $replicant;
268}
269
f15afa13 270=head2 _safely_ensure_connected ($replicant)
271
272The standard ensure_connected method with throw an exception should it fail to
273connect. For the master database this is desirable, but since replicants are
274allowed to fail, this behavior is not desirable. This method wraps the call
275to ensure_connected in an eval in order to catch any generated errors. That
d40080c3 276way a slave can go completely offline (ie, the box itself can die) without
f15afa13 277bringing down your entire pool of databases.
278
279=cut
280
281sub _safely_ensure_connected {
282 my ($self, $replicant, @args) = @_;
d40080c3 283
284 return $self->_safely($replicant, '->ensure_connected', sub {
285 $replicant->ensure_connected(@args)
286 });
287}
288
289=head2 _safely ($replicant, $name, $code)
290
291Execute C<$code> for operation C<$name> catching any exceptions and printing an
292error message to the C<<$replicant->debugobj>>.
293
294Returns 1 on success and undef on failure.
295
296=cut
297
298sub _safely {
299 my ($self, $replicant, $name, $code) = @_;
300
6ffb5be5 301 eval {
d40080c3 302 $code->()
d7a58a29 303 };
6ffb5be5 304 if ($@) {
d7a58a29 305 $replicant->debugobj->print(sprintf(
306 "Exception trying to $name for replicant %s, error is %s",
307 $replicant->_dbi_connect_info->[0], $@)
308 );
309 return undef;
13b9e828 310 }
d7a58a29 311
6ffb5be5 312 return 1;
f15afa13 313}
314
26ab719a 315=head2 connected_replicants
316
317Returns true if there are connected replicants. Actually is overloaded to
318return the number of replicants. So you can do stuff like:
319
64cdad22 320 if( my $num_connected = $storage->has_connected_replicants ) {
321 print "I have $num_connected connected replicants";
322 } else {
323 print "Sorry, no replicants.";
324 }
26ab719a 325
326This method will actually test that each replicant in the L</replicants> hashref
327is actually connected, try not to hit this 10 times a second.
328
329=cut
330
331sub connected_replicants {
64cdad22 332 my $self = shift @_;
333 return sum( map {
334 $_->connected ? 1:0
335 } $self->all_replicants );
26ab719a 336}
337
50336325 338=head2 active_replicants
339
340This is an array of replicants that are considered to be active in the pool.
341This does not check to see if they are connected, but if they are not, DBIC
342should automatically reconnect them for us when we hit them with a query.
343
344=cut
345
346sub active_replicants {
64cdad22 347 my $self = shift @_;
348 return ( grep {$_} map {
349 $_->active ? $_:0
350 } $self->all_replicants );
50336325 351}
352
26ab719a 353=head2 all_replicants
354
355Just a simple array of all the replicant storages. No particular order to the
356array is given, nor should any meaning be derived.
357
358=cut
359
360sub all_replicants {
64cdad22 361 my $self = shift @_;
362 return values %{$self->replicants};
26ab719a 363}
364
4a607d7a 365=head2 validate_replicants
366
367This does a check to see if 1) each replicate is connected (or reconnectable),
3682) that is ->is_replicating, and 3) that it is not exceeding the lag amount
369defined by L</maximum_lag>. Replicants that fail any of these tests are set to
370inactive, and thus removed from the replication pool.
371
372This tests L<all_replicants>, since a replicant that has been previous marked
373as inactive can be reactived should it start to pass the validation tests again.
374
375See L<DBIx::Class::Storage::DBI> for more about checking if a replicating
376connection is not following a master or is lagging.
377
378Calling this method will generate queries on the replicant databases so it is
379not recommended that you run them very often.
380
13b9e828 381This method requires that your underlying storage engine supports some sort of
382native replication mechanism. Currently only MySQL native replication is
383supported. Your patches to make other replication types work are welcomed.
384
4a607d7a 385=cut
386
387sub validate_replicants {
64cdad22 388 my $self = shift @_;
389 foreach my $replicant($self->all_replicants) {
13b9e828 390 if($self->_safely_ensure_connected($replicant)) {
391 my $is_replicating = $replicant->is_replicating;
392 unless(defined $is_replicating) {
9901aad7 393 $replicant->debugobj->print("Storage Driver ".ref($self)." Does not support the 'is_replicating' method. Assuming you are manually managing.\n");
13b9e828 394 next;
395 } else {
396 if($is_replicating) {
397 my $lag_behind_master = $replicant->lag_behind_master;
398 unless(defined $lag_behind_master) {
9901aad7 399 $replicant->debugobj->print("Storage Driver ".ref($self)." Does not support the 'lag_behind_master' method. Assuming you are manually managing.\n");
13b9e828 400 next;
401 } else {
402 if($lag_behind_master <= $self->maximum_lag) {
403 $replicant->active(1);
404 } else {
405 $replicant->active(0);
406 }
407 }
408 } else {
409 $replicant->active(0);
410 }
411 }
64cdad22 412 } else {
64cdad22 413 $replicant->active(0);
7edf5f1c 414 }
64cdad22 415 }
416 ## Mark that we completed this validation.
13b9e828 417 $self->_last_validated(time);
4a607d7a 418}
419
26ab719a 420=head1 AUTHOR
421
422John Napiorkowski <john.napiorkowski@takkle.com>
423
424=head1 LICENSE
425
426You may distribute this code under the same terms as Perl itself.
427
428=cut
429
c354902c 430__PACKAGE__->meta->make_immutable;
431
cb6ec758 4321;