Merge 'trunk' into 'replication_dedux'
[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
10DBIx::Class::Storage::DBI::Replicated::Pool; Manage a pool of replicants
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.
16
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' => (
7edf5f1c 39 is=>'rw',
4a607d7a 40 isa=>'Num',
41 required=>1,
42 lazy=>1,
43 default=>0,
44);
45
46=head2 replicant_type ($classname)
26ab719a 47
48Base class used to instantiate replicants that are in the pool. Unless you
49need to subclass L<DBIx::Class::Storage::DBI::Replicated::Replicant> you should
50just leave this alone.
51
52=cut
53
54has 'replicant_type' => (
55 is=>'ro',
56 isa=>'ClassName',
57 required=>1,
de5dc9ef 58 default=>'DBIx::Class::Storage::DBI',
26ab719a 59 handles=>{
60 'create_replicant' => 'new',
61 },
62);
63
26ab719a 64=head2 replicants
65
66A hashref of replicant, with the key being the dsn and the value returning the
67actual replicant storage. For example if the $dsn element is something like:
68
69 "dbi:SQLite:dbname=dbfile"
70
71You could access the specific replicant via:
72
73 $schema->storage->replicants->{'dbname=dbfile'}
74
75This attributes also supports the following helper methods
76
77=over 4
78
79=item set_replicant($key=>$storage)
80
81Pushes a replicant onto the HashRef under $key
82
83=item get_replicant($key)
84
85Retrieves the named replicant
86
87=item has_replicants
88
89Returns true if the Pool defines replicants.
90
91=item num_replicants
92
93The number of replicants in the pool
94
95=item delete_replicant ($key)
96
97removes the replicant under $key from the pool
98
99=back
100
101=cut
102
103has 'replicants' => (
104 is=>'rw',
105 metaclass => 'Collection::Hash',
de5dc9ef 106 isa=>'HashRef[DBIx::Class::Storage::DBI]',
26ab719a 107 default=>sub {{}},
108 provides => {
109 'set' => 'set_replicant',
110 'get' => 'get_replicant',
111 'empty' => 'has_replicants',
112 'count' => 'num_replicants',
113 'delete' => 'delete_replicant',
114 },
115);
116
26ab719a 117=head1 METHODS
118
119This class defines the following methods.
120
955a6df6 121=head2 connect_replicants ($schema, Array[$connect_info])
26ab719a 122
123Given an array of $dsn suitable for connected to a database, create an
124L<DBIx::Class::Storage::DBI::Replicated::Replicant> object and store it in the
125L</replicants> attribute.
126
127=cut
128
955a6df6 129sub connect_replicants {
26ab719a 130 my $self = shift @_;
50336325 131 my $schema = shift @_;
26ab719a 132
133 my @newly_created = ();
134 foreach my $connect_info (@_) {
de5dc9ef 135
50336325 136 my $replicant = $self->create_replicant($schema);
de5dc9ef 137 $replicant->connect_info($connect_info);
26ab719a 138 $replicant->ensure_connected;
de5dc9ef 139 DBIx::Class::Storage::DBI::Replicated::Replicant->meta->apply($replicant);
140
26ab719a 141 my ($key) = ($connect_info->[0]=~m/^dbi\:.+\:(.+)$/);
142 $self->set_replicant( $key => $replicant);
143 push @newly_created, $replicant;
144 }
145
146 return @newly_created;
147}
148
26ab719a 149=head2 connected_replicants
150
151Returns true if there are connected replicants. Actually is overloaded to
152return the number of replicants. So you can do stuff like:
153
154 if( my $num_connected = $storage->has_connected_replicants ) {
155 print "I have $num_connected connected replicants";
156 } else {
157 print "Sorry, no replicants.";
158 }
159
160This method will actually test that each replicant in the L</replicants> hashref
161is actually connected, try not to hit this 10 times a second.
162
163=cut
164
165sub connected_replicants {
166 my $self = shift @_;
167 return sum( map {
168 $_->connected ? 1:0
169 } $self->all_replicants );
170}
171
50336325 172=head2 active_replicants
173
174This is an array of replicants that are considered to be active in the pool.
175This does not check to see if they are connected, but if they are not, DBIC
176should automatically reconnect them for us when we hit them with a query.
177
178=cut
179
180sub active_replicants {
181 my $self = shift @_;
182 return ( grep {$_} map {
183 $_->active ? $_:0
184 } $self->all_replicants );
185}
186
26ab719a 187=head2 all_replicants
188
189Just a simple array of all the replicant storages. No particular order to the
190array is given, nor should any meaning be derived.
191
192=cut
193
194sub all_replicants {
7edf5f1c 195 my $self = shift @_;
196 return values %{$self->replicants};
26ab719a 197}
198
4a607d7a 199=head2 validate_replicants
200
201This does a check to see if 1) each replicate is connected (or reconnectable),
2022) that is ->is_replicating, and 3) that it is not exceeding the lag amount
203defined by L</maximum_lag>. Replicants that fail any of these tests are set to
204inactive, and thus removed from the replication pool.
205
206This tests L<all_replicants>, since a replicant that has been previous marked
207as inactive can be reactived should it start to pass the validation tests again.
208
209See L<DBIx::Class::Storage::DBI> for more about checking if a replicating
210connection is not following a master or is lagging.
211
212Calling this method will generate queries on the replicant databases so it is
213not recommended that you run them very often.
214
215=cut
216
217sub validate_replicants {
7edf5f1c 218 my $self = shift @_;
219 foreach my $replicant($self->all_replicants) {
220 if(
221 $replicant->is_replicating &&
222 $replicant->lag_behind_master <= $self->maximum_lag &&
223 $replicant->ensure_connected
224 ) {
225 ## TODO:: Hook debug for this
226 $replicant->active(1)
227 } else {
228 ## TODO:: Hook debug for this
229 $replicant->active(0);
230 }
231 }
4a607d7a 232}
233
26ab719a 234=head1 AUTHOR
235
236John Napiorkowski <john.napiorkowski@takkle.com>
237
238=head1 LICENSE
239
240You may distribute this code under the same terms as Perl itself.
241
242=cut
243
cb6ec758 2441;