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