Merge 'replication_dedux' into 'trunk'
[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 Test::Exception;
6 use DBICTest;
7
8 BEGIN {
9     eval "use Moose; use Test::Moose";
10     plan $@
11         ? ( skip_all => 'needs Moose for testing' )
12         : ( tests => 79 );
13 }
14
15 use_ok 'DBIx::Class::Storage::DBI::Replicated::Pool';
16 use_ok 'DBIx::Class::Storage::DBI::Replicated::Balancer';
17 use_ok 'DBIx::Class::Storage::DBI::Replicated::Replicant';
18 use_ok 'DBIx::Class::Storage::DBI::Replicated';
19
20 =head1 HOW TO USE
21
22     This is a test of the replicated storage system.  This will work in one of
23     two ways, either it was try to fake replication with a couple of SQLite DBs
24     and creative use of copy, or if you define a couple of %ENV vars correctly
25     will try to test those.  If you do that, it will assume the setup is properly
26     replicating.  Your results may vary, but I have demonstrated this to work with
27     mysql native replication.
28     
29 =cut
30
31
32 ## ----------------------------------------------------------------------------
33 ## Build a class to hold all our required testing data and methods.
34 ## ----------------------------------------------------------------------------
35
36 TESTSCHEMACLASSES: {
37
38     ## --------------------------------------------------------------------- ##
39     ## Create an object to contain your replicated stuff.
40     ## --------------------------------------------------------------------- ##
41     
42     package DBIx::Class::DBI::Replicated::TestReplication;
43    
44     use DBICTest;
45     use base qw/Class::Accessor::Fast/;
46     
47     __PACKAGE__->mk_accessors( qw/schema/ );
48
49     ## Initialize the object
50     
51         sub new {
52             my $class = shift @_;
53             my $self = $class->SUPER::new(@_);
54         
55             $self->schema( $self->init_schema );
56             return $self;
57         }
58     
59     ## Get the Schema and set the replication storage type
60     
61     sub init_schema {
62         my $class = shift @_;
63         
64         my $schema = DBICTest->init_schema(
65             storage_type=>{
66                 '::DBI::Replicated' => {
67                         balancer_type=>'::Random',
68                     balancer_args=>{
69                         auto_validate_every=>100,
70                     },
71                 }
72             },
73             deploy_args=>{
74                    add_drop_table => 1,
75             },
76         );
77
78         return $schema;
79     }
80     
81     sub generate_replicant_connect_info {}
82     sub replicate {}
83     sub cleanup {}
84
85   
86     ## --------------------------------------------------------------------- ##
87     ## Subclass for when you are using SQLite for testing, this provides a fake
88     ## replication support.
89     ## --------------------------------------------------------------------- ##
90         
91     package DBIx::Class::DBI::Replicated::TestReplication::SQLite;
92
93     use DBICTest;
94     use File::Copy;    
95     use base 'DBIx::Class::DBI::Replicated::TestReplication';
96     
97     __PACKAGE__->mk_accessors( qw/master_path slave_paths/ );
98     
99     ## Set the mastep path from DBICTest
100     
101         sub new {
102             my $class = shift @_;
103             my $self = $class->SUPER::new(@_);
104         
105             $self->master_path( DBICTest->_sqlite_dbfilename );
106             $self->slave_paths([
107             "t/var/DBIxClass_slave1.db",
108             "t/var/DBIxClass_slave2.db",    
109         ]);
110         
111             return $self;
112         }    
113         
114     ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
115     ## $storage->connect_info to be used for connecting replicants.
116     
117     sub generate_replicant_connect_info {
118         my $self = shift @_;
119         my @dsn = map {
120             "dbi:SQLite:${_}";
121         } @{$self->slave_paths};
122         
123         return map { [$_,'','',{AutoCommit=>1}] } @dsn;
124     }
125     
126     ## Do a 'good enough' replication by copying the master dbfile over each of
127     ## the slave dbfiles.  If the master is SQLite we do this, otherwise we
128     ## just do a one second pause to let the slaves catch up.
129     
130     sub replicate {
131         my $self = shift @_;
132         foreach my $slave (@{$self->slave_paths}) {
133             copy($self->master_path, $slave);
134         }
135     }
136     
137     ## Cleanup after ourselves.  Unlink all gthe slave paths.
138     
139     sub cleanup {
140         my $self = shift @_;
141         foreach my $slave (@{$self->slave_paths}) {
142             unlink $slave;
143         }     
144     }
145     
146     ## --------------------------------------------------------------------- ##
147     ## Subclass for when you are setting the databases via custom export vars
148     ## This is for when you have a replicating database setup that you are
149     ## going to test against.  You'll need to define the correct $ENV and have
150     ## two slave databases to test against, as well as a replication system
151     ## that will replicate in less than 1 second.
152     ## --------------------------------------------------------------------- ##
153         
154     package DBIx::Class::DBI::Replicated::TestReplication::Custom; 
155     use base 'DBIx::Class::DBI::Replicated::TestReplication';
156     
157     ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
158     ## $storage->connect_info to be used for connecting replicants.
159     
160     sub generate_replicant_connect_info { 
161         return (
162             [$ENV{"DBICTEST_SLAVE0_DSN"}, $ENV{"DBICTEST_SLAVE0_DBUSER"}, $ENV{"DBICTEST_SLAVE0_DBPASS"}, {AutoCommit => 1}],
163             [$ENV{"DBICTEST_SLAVE1_DSN"}, $ENV{"DBICTEST_SLAVE1_DBUSER"}, $ENV{"DBICTEST_SLAVE1_DBPASS"}, {AutoCommit => 1}],           
164         );
165     }
166     
167     ## pause a bit to let the replication catch up 
168     
169     sub replicate {
170         sleep 1;
171     } 
172 }
173
174 ## ----------------------------------------------------------------------------
175 ## Create an object and run some tests
176 ## ----------------------------------------------------------------------------
177
178 ## Thi first bunch of tests are basic, just make sure all the bits are behaving
179
180 my $replicated_class = DBICTest->has_custom_dsn ?
181     'DBIx::Class::DBI::Replicated::TestReplication::Custom' :
182     'DBIx::Class::DBI::Replicated::TestReplication::SQLite';
183
184 ok my $replicated = $replicated_class->new
185     => 'Created a replication object';
186     
187 isa_ok $replicated->schema
188     => 'DBIx::Class::Schema';
189     
190 isa_ok $replicated->schema->storage
191     => 'DBIx::Class::Storage::DBI::Replicated';
192
193 ok $replicated->schema->storage->meta
194     => 'has a meta object';
195     
196 isa_ok $replicated->schema->storage->master
197     => 'DBIx::Class::Storage::DBI';
198     
199 isa_ok $replicated->schema->storage->pool
200     => 'DBIx::Class::Storage::DBI::Replicated::Pool';
201     
202 does_ok $replicated->schema->storage->balancer
203     => 'DBIx::Class::Storage::DBI::Replicated::Balancer'; 
204
205 ok my @replicant_connects = $replicated->generate_replicant_connect_info
206     => 'got replication connect information';
207
208 ok my @replicated_storages = $replicated->schema->storage->connect_replicants(@replicant_connects)
209     => 'Created some storages suitable for replicants';
210     
211 isa_ok $replicated->schema->storage->balancer->current_replicant
212     => 'DBIx::Class::Storage::DBI';
213     
214 ok $replicated->schema->storage->pool->has_replicants
215     => 'does have replicants';     
216
217 is $replicated->schema->storage->pool->num_replicants => 2
218     => 'has two replicants';
219        
220 does_ok $replicated_storages[0]
221     => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
222
223 does_ok $replicated_storages[1]
224     => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
225     
226 my @replicant_names = keys %{$replicated->schema->storage->replicants};
227
228 does_ok $replicated->schema->storage->replicants->{$replicant_names[0]}
229     => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
230
231 does_ok $replicated->schema->storage->replicants->{$replicant_names[1]}
232     => 'DBIx::Class::Storage::DBI::Replicated::Replicant';  
233
234 ## Add some info to the database
235
236 $replicated
237     ->schema
238     ->populate('Artist', [
239         [ qw/artistid name/ ],
240         [ 4, "Ozric Tentacles"],
241     ]);
242                 
243 ## Make sure all the slaves have the table definitions
244
245 $replicated->replicate;
246 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
247 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
248
249 ## Make sure we can read the data.
250
251 ok my $artist1 = $replicated->schema->resultset('Artist')->find(4)
252     => 'Created Result';
253
254 isa_ok $artist1
255     => 'DBICTest::Artist';
256     
257 is $artist1->name, 'Ozric Tentacles'
258     => 'Found expected name for first result';
259
260 ## Add some new rows that only the master will have  This is because
261 ## we overload any type of write operation so that is must hit the master
262 ## database.
263
264 $replicated
265     ->schema
266     ->populate('Artist', [
267         [ qw/artistid name/ ],
268         [ 5, "Doom's Children"],
269         [ 6, "Dead On Arrival"],
270         [ 7, "Watergate"],
271     ]);
272
273 ## Make sure all the slaves have the table definitions
274 $replicated->replicate;
275
276 ## Should find some data now
277
278 ok my $artist2 = $replicated->schema->resultset('Artist')->find(5)
279     => 'Sync succeed';
280     
281 isa_ok $artist2
282     => 'DBICTest::Artist';
283     
284 is $artist2->name, "Doom's Children"
285     => 'Found expected name for first result';
286
287 ## What happens when we disconnect all the replicants?
288
289 is $replicated->schema->storage->pool->connected_replicants => 2
290     => "both replicants are connected";
291     
292 $replicated->schema->storage->replicants->{$replicant_names[0]}->disconnect;
293 $replicated->schema->storage->replicants->{$replicant_names[1]}->disconnect;
294
295 is $replicated->schema->storage->pool->connected_replicants => 0
296     => "both replicants are now disconnected";
297
298 ## All these should pass, since the database should automatically reconnect
299
300 ok my $artist3 = $replicated->schema->resultset('Artist')->find(6)
301     => 'Still finding stuff.';
302     
303 isa_ok $artist3
304     => 'DBICTest::Artist';
305     
306 is $artist3->name, "Dead On Arrival"
307     => 'Found expected name for first result';
308
309 is $replicated->schema->storage->pool->connected_replicants => 1
310     => "One replicant reconnected to handle the job";
311     
312 ## What happens when we try to select something that doesn't exist?
313
314 ok ! $replicated->schema->resultset('Artist')->find(666)
315     => 'Correctly failed to find something.';
316     
317 ## test the reliable option
318
319 TESTRELIABLE: {
320         
321         $replicated->schema->storage->set_reliable_storage;
322         
323         ok $replicated->schema->resultset('Artist')->find(2)
324             => 'Read from master 1';
325         
326         ok $replicated->schema->resultset('Artist')->find(5)
327             => 'Read from master 2';
328             
329     $replicated->schema->storage->set_balanced_storage;     
330             
331         ok $replicated->schema->resultset('Artist')->find(3)
332         => 'Read from replicant';
333 }
334
335 ## Make sure when reliable goes out of scope, we are using replicants again
336
337 ok $replicated->schema->resultset('Artist')->find(1)
338     => 'back to replicant 1.';
339     
340 ok $replicated->schema->resultset('Artist')->find(2)
341     => 'back to replicant 2.';
342
343 ## set all the replicants to inactive, and make sure the balancer falls back to
344 ## the master.
345
346 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
347 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
348     
349 ok $replicated->schema->resultset('Artist')->find(2)
350     => 'Fallback to master';
351
352 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
353 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
354
355 ok $replicated->schema->resultset('Artist')->find(2)
356     => 'Returned to replicates';
357     
358 ## Getting slave status tests
359
360 SKIP: {
361     ## We skip this tests unless you have a custom replicants, since the default
362     ## sqlite based replication tests don't support these functions.
363     
364     skip 'Cannot Test Replicant Status on Non Replicating Database', 9
365      unless DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"};
366
367     $replicated->replicate; ## Give the slaves a chance to catchup.
368
369         ok $replicated->schema->storage->replicants->{$replicant_names[0]}->is_replicating
370             => 'Replicants are replicating';
371             
372         is $replicated->schema->storage->replicants->{$replicant_names[0]}->lag_behind_master, 0
373             => 'Replicant is zero seconds behind master';
374             
375         ## Test the validate replicants
376         
377         $replicated->schema->storage->pool->validate_replicants;
378         
379         is $replicated->schema->storage->pool->active_replicants, 2
380             => 'Still have 2 replicants after validation';
381             
382         ## Force the replicants to fail the validate test by required their lag to
383         ## be negative (ie ahead of the master!)
384         
385     $replicated->schema->storage->pool->maximum_lag(-10);
386     $replicated->schema->storage->pool->validate_replicants;
387     
388     is $replicated->schema->storage->pool->active_replicants, 0
389         => 'No way a replicant be be ahead of the master';
390         
391     ## Let's be fair to the replicants again.  Let them lag up to 5
392         
393     $replicated->schema->storage->pool->maximum_lag(5);
394     $replicated->schema->storage->pool->validate_replicants;
395     
396     is $replicated->schema->storage->pool->active_replicants, 2
397         => 'Both replicants in good standing again';    
398         
399         ## Check auto validate
400         
401         is $replicated->schema->storage->balancer->auto_validate_every, 100
402             => "Got the expected value for auto validate";
403             
404                 ## This will make sure we auto validatge everytime
405                 $replicated->schema->storage->balancer->auto_validate_every(0);
406                 
407                 ## set all the replicants to inactive, and make sure the balancer falls back to
408                 ## the master.
409                 
410                 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
411                 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
412                 
413                 ## Ok, now when we go to run a query, autovalidate SHOULD reconnect
414         
415         is $replicated->schema->storage->pool->active_replicants => 0
416             => "both replicants turned off";
417                 
418         ok $replicated->schema->resultset('Artist')->find(5)
419             => 'replicant reactivated';
420             
421         is $replicated->schema->storage->pool->active_replicants => 2
422             => "both replicants reactivated";        
423 }
424
425 ## Test the reliably callback
426
427 ok my $reliably = sub {
428         
429     ok $replicated->schema->resultset('Artist')->find(5)
430         => 'replicant reactivated';     
431         
432 } => 'created coderef properly';
433
434 $replicated->schema->storage->execute_reliably($reliably);
435
436 ## Try something with an error
437
438 ok my $unreliably = sub {
439     
440     ok $replicated->schema->resultset('ArtistXX')->find(5)
441         => 'replicant reactivated'; 
442     
443 } => 'created coderef properly';
444
445 throws_ok {$replicated->schema->storage->execute_reliably($unreliably)} 
446     qr/Can't find source for ArtistXX/
447     => 'Bad coderef throws proper error';
448     
449 ## Make sure replication came back
450
451 ok $replicated->schema->resultset('Artist')->find(3)
452     => 'replicant reactivated';
453     
454 ## make sure transactions are set to execute_reliably
455
456 ok my $transaction = sub {
457         
458         my $id = shift @_;
459         
460         $replicated
461             ->schema
462             ->populate('Artist', [
463                 [ qw/artistid name/ ],
464                 [ $id, "Children of the Grave"],
465             ]);
466             
467     ok my $result = $replicated->schema->resultset('Artist')->find($id)
468         => 'Found expected artist';
469         
470     ok my $more = $replicated->schema->resultset('Artist')->find(1)
471         => 'Found expected artist again';
472         
473    return ($result, $more);
474    
475 } => 'Created a coderef properly';
476
477 ## Test the transaction with multi return
478 {
479         ok my @return = $replicated->schema->txn_do($transaction, 666)
480             => 'did transaction';
481             
482             is $return[0]->id, 666
483                 => 'first returned value is correct';
484                 
485             is $return[1]->id, 1
486                 => 'second returned value is correct';
487 }
488
489 ## Test that asking for single return works
490 {
491         ok my $return = $replicated->schema->txn_do($transaction, 777)
492             => 'did transaction';
493             
494             is $return->id, 777
495                 => 'first returned value is correct';
496 }
497
498 ## Test transaction returning a single value
499
500 {
501         ok my $result = $replicated->schema->txn_do(sub {
502                 ok my $more = $replicated->schema->resultset('Artist')->find(1)
503                 => 'found inside a transaction';
504                 return $more;
505         }) => 'successfully processed transaction';
506         
507         is $result->id, 1
508            => 'Got expected single result from transaction';
509 }
510
511 ## Make sure replication came back
512
513 ok $replicated->schema->resultset('Artist')->find(1)
514     => 'replicant reactivated';
515     
516 ## Test Discard changes
517
518 {
519         ok my $artist = $replicated->schema->resultset('Artist')->find(2)
520             => 'got an artist to test discard changes';
521             
522         ok $artist->discard_changes
523            => 'properly discard changes';
524 }
525
526 ## Test some edge cases, like trying to do a transaction inside a transaction, etc
527
528 {
529     ok my $result = $replicated->schema->txn_do(sub {
530         return $replicated->schema->txn_do(sub {
531                 ok my $more = $replicated->schema->resultset('Artist')->find(1)
532                 => 'found inside a transaction inside a transaction';
533                 return $more;                   
534         });
535     }) => 'successfully processed transaction';
536     
537     is $result->id, 1
538        => 'Got expected single result from transaction';          
539 }
540
541 {
542     ok my $result = $replicated->schema->txn_do(sub {
543         return $replicated->schema->storage->execute_reliably(sub {
544                 return $replicated->schema->txn_do(sub {
545                         return $replicated->schema->storage->execute_reliably(sub {
546                                 ok my $more = $replicated->schema->resultset('Artist')->find(1)
547                                 => 'found inside crazy deep transactions and execute_reliably';
548                                 return $more;                           
549                         });
550                 });     
551         });
552     }) => 'successfully processed transaction';
553     
554     is $result->id, 1
555        => 'Got expected single result from transaction';          
556 }     
557
558 ## Test the force_pool resultset attribute.
559
560 {
561         ok my $artist_rs = $replicated->schema->resultset('Artist')
562         => 'got artist resultset';
563            
564         ## Turn on Forced Pool Storage
565         ok my $reliable_artist_rs = $artist_rs->search(undef, {force_pool=>'master'})
566         => 'Created a resultset using force_pool storage';
567            
568     ok my $artist = $reliable_artist_rs->find(2) 
569         => 'got an artist result via force_pool storage';
570 }
571
572 ## Delete the old database files
573 $replicated->cleanup;
574
575
576
577
578
579