Merge 'trunk' into 'replication_dedux'
[dbsrgits/DBIx-Class.git] / t / 93storage_replication.t
1 use strict;
2 use warnings;
3 use lib qw(t/lib);
4 use Test::More;
5 use DBICTest;
6
7 BEGIN {
8     eval "use Moose; use Test::Moose";
9     plan $@
10         ? ( skip_all => 'needs Moose for testing' )
11         : ( tests => 50 );
12 }
13
14 use_ok 'DBIx::Class::Storage::DBI::Replicated::Pool';
15 use_ok 'DBIx::Class::Storage::DBI::Replicated::Balancer';
16 use_ok 'DBIx::Class::Storage::DBI::Replicated::Replicant';
17 use_ok 'DBIx::Class::Storage::DBI::Replicated';
18
19 =head1 HOW TO USE
20
21     This is a test of the replicated storage system.  This will work in one of
22     two ways, either it was try to fake replication with a couple of SQLite DBs
23     and creative use of copy, or if you define a couple of %ENV vars correctly
24     will try to test those.  If you do that, it will assume the setup is properly
25     replicating.  Your results may vary, but I have demonstrated this to work with
26     mysql native replication.
27     
28 =cut
29
30
31 ## ----------------------------------------------------------------------------
32 ## Build a class to hold all our required testing data and methods.
33 ## ----------------------------------------------------------------------------
34
35 TESTSCHEMACLASSES: {
36
37     ## --------------------------------------------------------------------- ##
38     ## Create an object to contain your replicated stuff.
39     ## --------------------------------------------------------------------- ##
40     
41     package DBIx::Class::DBI::Replicated::TestReplication;
42    
43     use DBICTest;
44     use base qw/Class::Accessor::Fast/;
45     
46     __PACKAGE__->mk_accessors( qw/schema/ );
47
48     ## Initialize the object
49     
50         sub new {
51             my $class = shift @_;
52             my $self = $class->SUPER::new(@_);
53         
54             $self->schema( $self->init_schema );
55             return $self;
56         }
57     
58     ## Get the Schema and set the replication storage type
59     
60     sub init_schema {
61         my $class = shift @_;
62         
63         my $schema = DBICTest->init_schema(
64             storage_type=>{
65                 '::DBI::Replicated' => {
66                         balancer_type=>'::Random',
67                     balancer_args=>{
68                         auto_validate_every=>100,
69                     },
70                 }
71             },
72             deploy_args=>{
73                    add_drop_table => 1,
74             },
75         );
76
77         return $schema;
78     }
79     
80     sub generate_replicant_connect_info {}
81     sub replicate {}
82     sub cleanup {}
83
84   
85     ## --------------------------------------------------------------------- ##
86     ## Subclass for when you are using SQLite for testing, this provides a fake
87     ## replication support.
88     ## --------------------------------------------------------------------- ##
89         
90     package DBIx::Class::DBI::Replicated::TestReplication::SQLite;
91
92     use DBICTest;
93     use File::Copy;    
94     use base 'DBIx::Class::DBI::Replicated::TestReplication';
95     
96     __PACKAGE__->mk_accessors( qw/master_path slave_paths/ );
97     
98     ## Set the mastep path from DBICTest
99     
100         sub new {
101             my $class = shift @_;
102             my $self = $class->SUPER::new(@_);
103         
104             $self->master_path( DBICTest->_sqlite_dbfilename );
105             $self->slave_paths([
106             "t/var/DBIxClass_slave1.db",
107             "t/var/DBIxClass_slave2.db",    
108         ]);
109         
110             return $self;
111         }    
112         
113     ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
114     ## $storage->connect_info to be used for connecting replicants.
115     
116     sub generate_replicant_connect_info {
117         my $self = shift @_;
118         my @dsn = map {
119             "dbi:SQLite:${_}";
120         } @{$self->slave_paths};
121         
122         return map { [$_,'','',{AutoCommit=>1}] } @dsn;
123     }
124     
125     ## Do a 'good enough' replication by copying the master dbfile over each of
126     ## the slave dbfiles.  If the master is SQLite we do this, otherwise we
127     ## just do a one second pause to let the slaves catch up.
128     
129     sub replicate {
130         my $self = shift @_;
131         foreach my $slave (@{$self->slave_paths}) {
132             copy($self->master_path, $slave);
133         }
134     }
135     
136     ## Cleanup after ourselves.  Unlink all gthe slave paths.
137     
138     sub cleanup {
139         my $self = shift @_;
140         foreach my $slave (@{$self->slave_paths}) {
141             unlink $slave;
142         }     
143     }
144     
145     ## --------------------------------------------------------------------- ##
146     ## Subclass for when you are setting the databases via custom export vars
147     ## This is for when you have a replicating database setup that you are
148     ## going to test against.  You'll need to define the correct $ENV and have
149     ## two slave databases to test against, as well as a replication system
150     ## that will replicate in less than 1 second.
151     ## --------------------------------------------------------------------- ##
152         
153     package DBIx::Class::DBI::Replicated::TestReplication::Custom; 
154     use base 'DBIx::Class::DBI::Replicated::TestReplication';
155     
156     ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
157     ## $storage->connect_info to be used for connecting replicants.
158     
159     sub generate_replicant_connect_info { 
160         return (
161             [$ENV{"DBICTEST_SLAVE0_DSN"}, $ENV{"DBICTEST_SLAVE0_DBUSER"}, $ENV{"DBICTEST_SLAVE0_DBPASS"}, {AutoCommit => 1}],
162             [$ENV{"DBICTEST_SLAVE1_DSN"}, $ENV{"DBICTEST_SLAVE1_DBUSER"}, $ENV{"DBICTEST_SLAVE1_DBPASS"}, {AutoCommit => 1}],           
163         );
164     }
165     
166     ## pause a bit to let the replication catch up 
167     
168     sub replicate {
169         sleep 1;
170     } 
171 }
172
173 ## ----------------------------------------------------------------------------
174 ## Create an object and run some tests
175 ## ----------------------------------------------------------------------------
176
177 ## Thi first bunch of tests are basic, just make sure all the bits are behaving
178
179 my $replicated_class = DBICTest->has_custom_dsn ?
180     'DBIx::Class::DBI::Replicated::TestReplication::Custom' :
181     'DBIx::Class::DBI::Replicated::TestReplication::SQLite';
182
183 ok my $replicated = $replicated_class->new
184     => 'Created a replication object';
185     
186 isa_ok $replicated->schema
187     => 'DBIx::Class::Schema';
188     
189 isa_ok $replicated->schema->storage
190     => 'DBIx::Class::Storage::DBI::Replicated';
191
192 ok $replicated->schema->storage->meta
193     => 'has a meta object';
194     
195 isa_ok $replicated->schema->storage->master
196     => 'DBIx::Class::Storage::DBI';
197     
198 isa_ok $replicated->schema->storage->pool
199     => 'DBIx::Class::Storage::DBI::Replicated::Pool';
200     
201 does_ok $replicated->schema->storage->balancer
202     => 'DBIx::Class::Storage::DBI::Replicated::Balancer'; 
203
204 ok my @replicant_connects = $replicated->generate_replicant_connect_info
205     => 'got replication connect information';
206
207 ok my @replicated_storages = $replicated->schema->storage->connect_replicants(@replicant_connects)
208     => 'Created some storages suitable for replicants';
209     
210 isa_ok $replicated->schema->storage->balancer->current_replicant
211     => 'DBIx::Class::Storage::DBI';
212     
213 ok $replicated->schema->storage->pool->has_replicants
214     => 'does have replicants';     
215
216 is $replicated->schema->storage->pool->num_replicants => 2
217     => 'has two replicants';
218        
219 does_ok $replicated_storages[0]
220     => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
221
222 does_ok $replicated_storages[1]
223     => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
224     
225 my @replicant_names = keys %{$replicated->schema->storage->replicants};
226
227 does_ok $replicated->schema->storage->replicants->{$replicant_names[0]}
228     => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
229
230 does_ok $replicated->schema->storage->replicants->{$replicant_names[1]}
231     => 'DBIx::Class::Storage::DBI::Replicated::Replicant';  
232
233 ## Add some info to the database
234
235 $replicated
236     ->schema
237     ->populate('Artist', [
238         [ qw/artistid name/ ],
239         [ 4, "Ozric Tentacles"],
240     ]);
241                 
242 ## Make sure all the slaves have the table definitions
243
244 $replicated->replicate;
245
246 ## Make sure we can read the data.
247
248 ok my $artist1 = $replicated->schema->resultset('Artist')->find(4)
249     => 'Created Result';
250
251 isa_ok $artist1
252     => 'DBICTest::Artist';
253     
254 is $artist1->name, 'Ozric Tentacles'
255     => 'Found expected name for first result';
256
257 ## Add some new rows that only the master will have  This is because
258 ## we overload any type of write operation so that is must hit the master
259 ## database.
260
261 $replicated
262     ->schema
263     ->populate('Artist', [
264         [ qw/artistid name/ ],
265         [ 5, "Doom's Children"],
266         [ 6, "Dead On Arrival"],
267         [ 7, "Watergate"],
268     ]);
269
270 SKIP: {
271     ## We can't do this test if we have a custom replicants, since we assume
272     ## if there are custom one that you are trying to test a real replicating
273     ## system.  See docs above for more.
274     
275     skip 'Cannot test inconsistent replication since you have a real replication system', 1
276      if DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"};
277     
278         ## Alright, the database 'cluster' is not in a consistent state.  When we do
279         ## a read now we expect bad news    
280     is $replicated->schema->resultset('Artist')->find(5), undef
281     => 'read after disconnect fails because it uses a replicant which we have neglected to "replicate" yet'; 
282 }
283
284 ## Make sure all the slaves have the table definitions
285 $replicated->replicate;
286
287 ## Should find some data now
288
289 ok my $artist2 = $replicated->schema->resultset('Artist')->find(5)
290     => 'Sync succeed';
291     
292 isa_ok $artist2
293     => 'DBICTest::Artist';
294     
295 is $artist2->name, "Doom's Children"
296     => 'Found expected name for first result';
297
298 ## What happens when we disconnect all the replicants?
299
300 is $replicated->schema->storage->pool->connected_replicants => 2
301     => "both replicants are connected";
302     
303 $replicated->schema->storage->replicants->{$replicant_names[0]}->disconnect;
304 $replicated->schema->storage->replicants->{$replicant_names[1]}->disconnect;
305
306 is $replicated->schema->storage->pool->connected_replicants => 0
307     => "both replicants are now disconnected";
308
309 ## All these should pass, since the database should automatically reconnect
310
311 ok my $artist3 = $replicated->schema->resultset('Artist')->find(6)
312     => 'Still finding stuff.';
313     
314 isa_ok $artist3
315     => 'DBICTest::Artist';
316     
317 is $artist3->name, "Dead On Arrival"
318     => 'Found expected name for first result';
319
320 is $replicated->schema->storage->pool->connected_replicants => 1
321     => "One replicant reconnected to handle the job";
322     
323 ## What happens when we try to select something that doesn't exist?
324
325 ok ! $replicated->schema->resultset('Artist')->find(666)
326     => 'Correctly failed to find something.';
327     
328 ## test the reliable option
329
330 TESTRELIABLE: {
331         
332         $replicated->schema->storage->set_reliable_storage;
333         
334         ok $replicated->schema->resultset('Artist')->find(2)
335             => 'Read from master 1';
336         
337         ok $replicated->schema->resultset('Artist')->find(5)
338             => 'Read from master 2';
339             
340     $replicated->schema->storage->set_balanced_storage;     
341             
342         ok $replicated->schema->resultset('Artist')->find(3)
343         => 'Read from replicant';
344 }
345
346 ## Make sure when reliable goes out of scope, we are using replicants again
347
348 ok $replicated->schema->resultset('Artist')->find(1)
349     => 'back to replicant 1.';
350     
351 ok $replicated->schema->resultset('Artist')->find(2)
352     => 'back to replicant 2.';
353
354 ## set all the replicants to inactive, and make sure the balancer falls back to
355 ## the master.
356
357 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
358 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
359     
360 ok $replicated->schema->resultset('Artist')->find(2)
361     => 'Fallback to master';
362
363 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
364 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
365
366 ok $replicated->schema->resultset('Artist')->find(2)
367     => 'Returned to replicates';
368     
369 ## Getting slave status tests
370
371 SKIP: {
372     ## We skip this tests unless you have a custom replicants, since the default
373     ## sqlite based replication tests don't support these functions.
374     
375     skip 'Cannot Test Replicant Status on Non Replicating Database', 9
376      unless DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"};
377
378     $replicated->replicate; ## Give the slaves a chance to catchup.
379
380         ok $replicated->schema->storage->replicants->{$replicant_names[0]}->is_replicating
381             => 'Replicants are replicating';
382             
383         is $replicated->schema->storage->replicants->{$replicant_names[0]}->lag_behind_master, 0
384             => 'Replicant is zero seconds behind master';
385             
386         ## Test the validate replicants
387         
388         $replicated->schema->storage->pool->validate_replicants;
389         
390         is $replicated->schema->storage->pool->active_replicants, 2
391             => 'Still have 2 replicants after validation';
392             
393         ## Force the replicants to fail the validate test by required their lag to
394         ## be negative (ie ahead of the master!)
395         
396     $replicated->schema->storage->pool->maximum_lag(-10);
397     $replicated->schema->storage->pool->validate_replicants;
398     
399     is $replicated->schema->storage->pool->active_replicants, 0
400         => 'No way a replicant be be ahead of the master';
401         
402     ## Let's be fair to the replicants again.  Let them lag up to 5
403         
404     $replicated->schema->storage->pool->maximum_lag(5);
405     $replicated->schema->storage->pool->validate_replicants;
406     
407     is $replicated->schema->storage->pool->active_replicants, 2
408         => 'Both replicants in good standing again';    
409         
410         ## Check auto validate
411         
412         is $replicated->schema->storage->balancer->auto_validate_every, 100
413             => "Got the expected value for auto validate";
414             
415                 ## This will make sure we auto validatge everytime
416                 $replicated->schema->storage->balancer->auto_validate_every(0);
417                 
418                 ## set all the replicants to inactive, and make sure the balancer falls back to
419                 ## the master.
420                 
421                 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
422                 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
423                 
424                 ## Ok, now when we go to run a query, autovalidate SHOULD reconnect
425         
426         is $replicated->schema->storage->pool->active_replicants => 0
427             => "both replicants turned off";
428                 
429         ok $replicated->schema->resultset('Artist')->find(5)
430             => 'replicant reactivated';
431             
432         is $replicated->schema->storage->pool->active_replicants => 2
433             => "both replicants reactivated";        
434 }
435
436 ## Delete the old database files
437 $replicated->cleanup;
438
439
440
441
442
443