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