Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / t / storage / replicated.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
cb551b07 2use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_replicated';
3
e4dc89b3 4use strict;
5use warnings;
68de9438 6
e4dc89b3 7use Test::More;
cb551b07 8use DBIx::Class::_Util 'modver_gt_or_eq_and_lt';
c0329273 9
4bea1fe7 10use DBICTest;
11
68de9438 12BEGIN {
cb551b07 13 plan skip_all => "A trial version of Moose detected known to break replication - skipping test known to fail" if (
14 DBICTest::RunMode->is_smoker
15 and
16 modver_gt_or_eq_and_lt( 'Moose', '1.99', '1.9903' )
17 )
e4ee5861 18}
19
68de9438 20use Test::Moose;
c4d3fae2 21use Test::Exception;
b2e4d522 22use Scalar::Util 'reftype';
68de9438 23use Moose();
24use MooseX::Types();
25note "Using Moose version $Moose::VERSION and MooseX::Types version $MooseX::Types::VERSION";
8f7986d6 26
aff5e9c1 27my $var_dir_re = qr{ t [\/\\] var [\/\\] }x;
7672675a 28
6287275c 29## Add a connect_info option to test option merging.
617474ab 30use DBIx::Class::Storage::DBI::Replicated;
6287275c 31{
32 package DBIx::Class::Storage::DBI::Replicated;
33
34 use Moose;
35
36 __PACKAGE__->meta->make_mutable;
37
38 around connect_info => sub {
39 my ($next, $self, $info) = @_;
40 $info->[3]{master_option} = 1;
41 $self->$next($info);
42 };
43
44 __PACKAGE__->meta->make_immutable;
45
46 no Moose;
47}
48
0f83441a 49
6a151f58 50
89cf6a70 51=head1 HOW TO USE
52
53 This is a test of the replicated storage system. This will work in one of
54 two ways, either it was try to fake replication with a couple of SQLite DBs
55 and creative use of copy, or if you define a couple of %ENV vars correctly
56 will try to test those. If you do that, it will assume the setup is properly
57 replicating. Your results may vary, but I have demonstrated this to work with
58 mysql native replication.
d59cf2f2 59
89cf6a70 60=cut
61
62
0f83441a 63## ----------------------------------------------------------------------------
64## Build a class to hold all our required testing data and methods.
65## ----------------------------------------------------------------------------
66
857d66ca 67TESTSCHEMACLASSES: {
2bf79155 68
857d66ca 69 ## --------------------------------------------------------------------- ##
70 ## Create an object to contain your replicated stuff.
71 ## --------------------------------------------------------------------- ##
d59cf2f2 72
2bf79155 73 package DBIx::Class::DBI::Replicated::TestReplication;
d59cf2f2 74
2bf79155 75 use DBICTest;
76 use base qw/Class::Accessor::Fast/;
d59cf2f2 77
857d66ca 78 __PACKAGE__->mk_accessors( qw/schema/ );
2bf79155 79
80 ## Initialize the object
d59cf2f2 81
82 sub new {
83 my ($class, $schema_method) = (shift, shift);
84 my $self = $class->SUPER::new(@_);
85
86 $self->schema( $self->init_schema($schema_method) );
87 return $self;
88 }
89
26ab719a 90 ## Get the Schema and set the replication storage type
d59cf2f2 91
2bf79155 92 sub init_schema {
7536c92b 93 #my ($class, $schema_getter) = @_;
94 shift->${\ ( 'get_schema_' . shift ) };
2bf79155 95 }
dcdf7b2c 96
97 sub get_schema_by_storage_type {
98 DBICTest->init_schema(
99 sqlite_use_file => 1,
100 storage_type=>{
101 '::DBI::Replicated' => {
102 balancer_type=>'::Random',
103 balancer_args=>{
104 auto_validate_every=>100,
64ae1667 105 master_read_weight => 1
dcdf7b2c 106 },
107 }
108 },
109 deploy_args=>{
110 add_drop_table => 1,
111 },
112 );
113 }
114
115 sub get_schema_by_connect_info {
116 DBICTest->init_schema(
117 sqlite_use_file => 1,
118 storage_type=> '::DBI::Replicated',
119 balancer_type=>'::Random',
120 balancer_args=> {
64ae1667 121 auto_validate_every=>100,
122 master_read_weight => 1
123 },
124 pool_args=>{
125 maximum_lag=>1,
dcdf7b2c 126 },
127 deploy_args=>{
128 add_drop_table => 1,
129 },
130 );
131 }
132
857d66ca 133 sub generate_replicant_connect_info {}
134 sub replicate {}
135 sub cleanup {}
136
b2e4d522 137 ## --------------------------------------------------------------------- ##
857d66ca 138 ## Subclass for when you are using SQLite for testing, this provides a fake
139 ## replication support.
140 ## --------------------------------------------------------------------- ##
d59cf2f2 141
857d66ca 142 package DBIx::Class::DBI::Replicated::TestReplication::SQLite;
143
144 use DBICTest;
d59cf2f2 145 use File::Copy;
857d66ca 146 use base 'DBIx::Class::DBI::Replicated::TestReplication';
d59cf2f2 147
3da4f736 148 __PACKAGE__->mk_accessors(qw/master_path slave_paths/);
d59cf2f2 149
3da4f736 150 ## Set the master path from DBICTest
d59cf2f2 151
152 sub new {
153 my $class = shift @_;
154 my $self = $class->SUPER::new(@_);
155
156 $self->master_path( DBICTest->_sqlite_dbfilename );
157 $self->slave_paths([
aff5e9c1 158 't/var/DBIxClass_slave1.db',
159 't/var/DBIxClass_slave2.db',
d59cf2f2 160 ]);
161
162 return $self;
163 }
164
26ab719a 165 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
166 ## $storage->connect_info to be used for connecting replicants.
d59cf2f2 167
26ab719a 168 sub generate_replicant_connect_info {
857d66ca 169 my $self = shift @_;
26ab719a 170 my @dsn = map {
171 "dbi:SQLite:${_}";
172 } @{$self->slave_paths};
d59cf2f2 173
9901aad7 174 my @connect_infos = map { [$_,'','',{AutoCommit=>1}] } @dsn;
175
d59cf2f2 176 ## Make sure nothing is left over from a failed test
177 $self->cleanup;
3da4f736 178
d59cf2f2 179 ## try a hashref too
9901aad7 180 my $c = $connect_infos[0];
181 $connect_infos[0] = {
182 dsn => $c->[0],
183 user => $c->[1],
184 password => $c->[2],
185 %{ $c->[3] }
186 };
187
188 @connect_infos
26ab719a 189 }
9901aad7 190
26ab719a 191 ## Do a 'good enough' replication by copying the master dbfile over each of
50336325 192 ## the slave dbfiles. If the master is SQLite we do this, otherwise we
193 ## just do a one second pause to let the slaves catch up.
d59cf2f2 194
26ab719a 195 sub replicate {
196 my $self = shift @_;
197 foreach my $slave (@{$self->slave_paths}) {
198 copy($self->master_path, $slave);
199 }
200 }
d59cf2f2 201
9e75be92 202 ## Cleanup after ourselves. Unlink all the slave paths.
d59cf2f2 203
26ab719a 204 sub cleanup {
205 my $self = shift @_;
e1ff35c4 206 $_->disconnect for values %{ $self->schema->storage->replicants };
26ab719a 207 foreach my $slave (@{$self->slave_paths}) {
d59cf2f2 208 if(-e $slave) {
209 unlink $slave;
210 }
211 }
26ab719a 212 }
d59cf2f2 213
857d66ca 214 ## --------------------------------------------------------------------- ##
215 ## Subclass for when you are setting the databases via custom export vars
216 ## This is for when you have a replicating database setup that you are
217 ## going to test against. You'll need to define the correct $ENV and have
218 ## two slave databases to test against, as well as a replication system
219 ## that will replicate in less than 1 second.
220 ## --------------------------------------------------------------------- ##
d59cf2f2 221
222 package DBIx::Class::DBI::Replicated::TestReplication::Custom;
857d66ca 223 use base 'DBIx::Class::DBI::Replicated::TestReplication';
d59cf2f2 224
857d66ca 225 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
226 ## $storage->connect_info to be used for connecting replicants.
d59cf2f2 227
228 sub generate_replicant_connect_info {
857d66ca 229 return (
230 [$ENV{"DBICTEST_SLAVE0_DSN"}, $ENV{"DBICTEST_SLAVE0_DBUSER"}, $ENV{"DBICTEST_SLAVE0_DBPASS"}, {AutoCommit => 1}],
d59cf2f2 231 [$ENV{"DBICTEST_SLAVE1_DSN"}, $ENV{"DBICTEST_SLAVE1_DBUSER"}, $ENV{"DBICTEST_SLAVE1_DBPASS"}, {AutoCommit => 1}],
857d66ca 232 );
233 }
d59cf2f2 234
235 ## pause a bit to let the replication catch up
236
857d66ca 237 sub replicate {
d59cf2f2 238 sleep 1;
239 }
2bf79155 240}
241
242## ----------------------------------------------------------------------------
243## Create an object and run some tests
244## ----------------------------------------------------------------------------
245
26ab719a 246## Thi first bunch of tests are basic, just make sure all the bits are behaving
2bf79155 247
857d66ca 248my $replicated_class = DBICTest->has_custom_dsn ?
249 'DBIx::Class::DBI::Replicated::TestReplication::Custom' :
250 'DBIx::Class::DBI::Replicated::TestReplication::SQLite';
251
dcdf7b2c 252my $replicated;
253
254for my $method (qw/by_connect_info by_storage_type/) {
a3df35f8 255 undef $replicated;
dcdf7b2c 256 ok $replicated = $replicated_class->new($method)
257 => "Created a replication object $method";
d59cf2f2 258
dcdf7b2c 259 isa_ok $replicated->schema
260 => 'DBIx::Class::Schema';
d59cf2f2 261
dcdf7b2c 262 isa_ok $replicated->schema->storage
263 => 'DBIx::Class::Storage::DBI::Replicated';
264
265 isa_ok $replicated->schema->storage->balancer
266 => 'DBIx::Class::Storage::DBI::Replicated::Balancer::Random'
267 => 'configured balancer_type';
268}
26ab719a 269
f29315ed 270### check that all Storage::DBI methods are handled by ::Replicated
271{
b75235fe 272 my @storage_dbi_methods = Class::MOP::Class
273 ->initialize('DBIx::Class::Storage::DBI')->get_all_method_names;
f29315ed 274
b75235fe 275 my @replicated_methods = DBIx::Class::Storage::DBI::Replicated->meta
276 ->get_all_method_names;
f29315ed 277
b75235fe 278# remove constants and OTHER_CRAP
f29315ed 279 @storage_dbi_methods = grep !/^[A-Z_]+\z/, @storage_dbi_methods;
280
281# remove CAG accessors
282 @storage_dbi_methods = grep !/_accessor\z/, @storage_dbi_methods;
283
284# remove DBIx::Class (the root parent, with CAG and stuff) methods
b75235fe 285 my @root_methods = Class::MOP::Class->initialize('DBIx::Class')
f29315ed 286 ->get_all_method_names;
287 my %count;
b75235fe 288 $count{$_}++ for (@storage_dbi_methods, @root_methods);
f29315ed 289
290 @storage_dbi_methods = grep $count{$_} != 2, @storage_dbi_methods;
291
292# make hashes
293 my %storage_dbi_methods;
294 @storage_dbi_methods{@storage_dbi_methods} = ();
295 my %replicated_methods;
296 @replicated_methods{@replicated_methods} = ();
297
298# remove ::Replicated-specific methods
299 for my $method (@replicated_methods) {
300 delete $replicated_methods{$method}
301 unless exists $storage_dbi_methods{$method};
302 }
e4c5abce 303 @replicated_methods = keys %replicated_methods;
f29315ed 304
305# check that what's left is implemented
306 %count = ();
307 $count{$_}++ for (@storage_dbi_methods, @replicated_methods);
308
309 if ((grep $count{$_} == 2, @storage_dbi_methods) == @storage_dbi_methods) {
310 pass 'all DBIx::Class::Storage::DBI methods implemented';
311 }
312 else {
313 my @unimplemented = grep $count{$_} == 1, @storage_dbi_methods;
314
315 fail 'the following DBIx::Class::Storage::DBI methods are unimplemented: '
316 . "@unimplemented";
317 }
318}
319
26ab719a 320ok $replicated->schema->storage->meta
321 => 'has a meta object';
d59cf2f2 322
26ab719a 323isa_ok $replicated->schema->storage->master
324 => 'DBIx::Class::Storage::DBI';
d59cf2f2 325
26ab719a 326isa_ok $replicated->schema->storage->pool
327 => 'DBIx::Class::Storage::DBI::Replicated::Pool';
d59cf2f2 328
17b05c13 329does_ok $replicated->schema->storage->balancer
d59cf2f2 330 => 'DBIx::Class::Storage::DBI::Replicated::Balancer';
26ab719a 331
332ok my @replicant_connects = $replicated->generate_replicant_connect_info
333 => 'got replication connect information';
334
955a6df6 335ok my @replicated_storages = $replicated->schema->storage->connect_replicants(@replicant_connects)
26ab719a 336 => 'Created some storages suitable for replicants';
6412a592 337
071bbccb 338our %debug;
339$replicated->schema->storage->debug(1);
340$replicated->schema->storage->debugcb(sub {
d59cf2f2 341 my ($op, $info) = @_;
342 ##warn "\n$op, $info\n";
343 %debug = (
344 op => $op,
345 info => $info,
346 dsn => ($info=~m/\[(.+)\]/)[0],
347 storage_type => $info=~m/REPLICANT/ ? 'REPLICANT' : 'MASTER',
348 );
071bbccb 349});
350
6412a592 351ok my @all_storages = $replicated->schema->storage->all_storages
352 => '->all_storages';
353
dcdf7b2c 354is scalar @all_storages,
355 3
6412a592 356 => 'correct number of ->all_storages';
357
dcdf7b2c 358is ((grep $_->isa('DBIx::Class::Storage::DBI'), @all_storages),
359 3
6412a592 360 => '->all_storages are correct type');
b2e4d522 361
dcdf7b2c 362my @all_storage_opts =
363 grep { (reftype($_)||'') eq 'HASH' }
364 map @{ $_->_connect_info }, @all_storages;
365
366is ((grep $_->{master_option}, @all_storage_opts),
367 3
b2e4d522 368 => 'connect_info was merged from master to replicants');
d59cf2f2 369
9901aad7 370my @replicant_names = keys %{ $replicated->schema->storage->replicants };
371
bd5da369 372ok @replicant_names, "found replicant names @replicant_names";
373
9901aad7 374## Silence warning about not supporting the is_replicating method if using the
375## sqlite dbs.
376$replicated->schema->storage->debugobj->silence(1)
aff5e9c1 377 if grep { $_ =~ $var_dir_re } @replicant_names;
d59cf2f2 378
cb6ec758 379isa_ok $replicated->schema->storage->balancer->current_replicant
d59cf2f2 380 => 'DBIx::Class::Storage::DBI';
9901aad7 381
382$replicated->schema->storage->debugobj->silence(0);
383
26ab719a 384ok $replicated->schema->storage->pool->has_replicants
d59cf2f2 385 => 'does have replicants';
26ab719a 386
17b05c13 387is $replicated->schema->storage->pool->num_replicants => 2
26ab719a 388 => 'has two replicants';
d59cf2f2 389
de5dc9ef 390does_ok $replicated_storages[0]
26ab719a 391 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
392
de5dc9ef 393does_ok $replicated_storages[1]
26ab719a 394 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
d59cf2f2 395
de5dc9ef 396does_ok $replicated->schema->storage->replicants->{$replicant_names[0]}
26ab719a 397 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
398
de5dc9ef 399does_ok $replicated->schema->storage->replicants->{$replicant_names[1]}
d59cf2f2 400 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
26ab719a 401
402## Add some info to the database
403
404$replicated
405 ->schema
406 ->populate('Artist', [
407 [ qw/artistid name/ ],
408 [ 4, "Ozric Tentacles"],
409 ]);
071bbccb 410
d59cf2f2 411 is $debug{storage_type}, 'MASTER',
412 "got last query from a master: $debug{dsn}";
413
414 like $debug{info}, qr/INSERT/, 'Last was an insert';
415
26ab719a 416## Make sure all the slaves have the table definitions
417
418$replicated->replicate;
5c1d82d2 419$replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
420$replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
9901aad7 421
422## Silence warning about not supporting the is_replicating method if using the
423## sqlite dbs.
424$replicated->schema->storage->debugobj->silence(1)
aff5e9c1 425 if grep { $_ =~ $var_dir_re } @replicant_names;
d59cf2f2 426
f15afa13 427$replicated->schema->storage->pool->validate_replicants;
26ab719a 428
9901aad7 429$replicated->schema->storage->debugobj->silence(0);
430
26ab719a 431## Make sure we can read the data.
432
433ok my $artist1 = $replicated->schema->resultset('Artist')->find(4)
434 => 'Created Result';
435
071bbccb 436## We removed testing here since master read weight is on, so we can't tell in
437## advance what storage to expect. We turn master read weight off a bit lower
d59cf2f2 438## is $debug{storage_type}, 'REPLICANT'
439## => "got last query from a replicant: $debug{dsn}, $debug{info}";
071bbccb 440
26ab719a 441isa_ok $artist1
442 => 'DBICTest::Artist';
d59cf2f2 443
26ab719a 444is $artist1->name, 'Ozric Tentacles'
445 => 'Found expected name for first result';
446
ee356d00 447## Check that master_read_weight is honored
448{
87974600 449 no warnings qw/once redefine/;
ee356d00 450
451 local
f404f53c 452 *DBIx::Class::Storage::DBI::Replicated::Balancer::Random::_random_number =
d59cf2f2 453 sub { 999 };
ee356d00 454
455 $replicated->schema->storage->balancer->increment_storage;
456
457 is $replicated->schema->storage->balancer->current_replicant,
458 $replicated->schema->storage->master
459 => 'master_read_weight is honored';
460
461 ## turn it off for the duration of the test
462 $replicated->schema->storage->balancer->master_read_weight(0);
463 $replicated->schema->storage->balancer->increment_storage;
464}
465
26ab719a 466## Add some new rows that only the master will have This is because
467## we overload any type of write operation so that is must hit the master
468## database.
469
470$replicated
471 ->schema
472 ->populate('Artist', [
473 [ qw/artistid name/ ],
474 [ 5, "Doom's Children"],
475 [ 6, "Dead On Arrival"],
476 [ 7, "Watergate"],
477 ]);
478
d59cf2f2 479 is $debug{storage_type}, 'MASTER',
480 "got last query from a master: $debug{dsn}";
481
482 like $debug{info}, qr/INSERT/, 'Last was an insert';
071bbccb 483
26ab719a 484## Make sure all the slaves have the table definitions
485$replicated->replicate;
486
487## Should find some data now
488
489ok my $artist2 = $replicated->schema->resultset('Artist')->find(5)
490 => 'Sync succeed';
071bbccb 491
d59cf2f2 492is $debug{storage_type}, 'REPLICANT'
493 => "got last query from a replicant: $debug{dsn}";
494
26ab719a 495isa_ok $artist2
496 => 'DBICTest::Artist';
d59cf2f2 497
26ab719a 498is $artist2->name, "Doom's Children"
499 => 'Found expected name for first result';
500
501## What happens when we disconnect all the replicants?
502
50336325 503is $replicated->schema->storage->pool->connected_replicants => 2
504 => "both replicants are connected";
d59cf2f2 505
89cf6a70 506$replicated->schema->storage->replicants->{$replicant_names[0]}->disconnect;
507$replicated->schema->storage->replicants->{$replicant_names[1]}->disconnect;
26ab719a 508
50336325 509is $replicated->schema->storage->pool->connected_replicants => 0
510 => "both replicants are now disconnected";
511
512## All these should pass, since the database should automatically reconnect
513
26ab719a 514ok my $artist3 = $replicated->schema->resultset('Artist')->find(6)
515 => 'Still finding stuff.';
071bbccb 516
d59cf2f2 517is $debug{storage_type}, 'REPLICANT'
518 => "got last query from a replicant: $debug{dsn}";
519
26ab719a 520isa_ok $artist3
521 => 'DBICTest::Artist';
d59cf2f2 522
26ab719a 523is $artist3->name, "Dead On Arrival"
524 => 'Found expected name for first result';
2bf79155 525
50336325 526is $replicated->schema->storage->pool->connected_replicants => 1
13b9e828 527 => "At Least One replicant reconnected to handle the job";
d59cf2f2 528
6f6fb437 529## What happens when we try to select something that doesn't exist?
530
531ok ! $replicated->schema->resultset('Artist')->find(666)
532 => 'Correctly failed to find something.';
071bbccb 533
d59cf2f2 534is $debug{storage_type}, 'REPLICANT'
535 => "got last query from a replicant: $debug{dsn}";
536
cb6ec758 537## test the reliable option
538
539TESTRELIABLE: {
d59cf2f2 540
541 $replicated->schema->storage->set_reliable_storage;
542
543 ok $replicated->schema->resultset('Artist')->find(2)
544 => 'Read from master 1';
545
546 is $debug{storage_type}, 'MASTER',
547 "got last query from a master: $debug{dsn}";
548
549 ok $replicated->schema->resultset('Artist')->find(5)
550 => 'Read from master 2';
551
552 is $debug{storage_type}, 'MASTER',
553 "got last query from a master: $debug{dsn}";
554
555 $replicated->schema->storage->set_balanced_storage;
556
557 ok $replicated->schema->resultset('Artist')->find(3)
9c748388 558 => 'Read from replicant';
071bbccb 559
d59cf2f2 560 is $debug{storage_type}, 'REPLICANT',
561 "got last query from a replicant: $debug{dsn}";
cb6ec758 562}
563
9c748388 564## Make sure when reliable goes out of scope, we are using replicants again
cb6ec758 565
566ok $replicated->schema->resultset('Artist')->find(1)
567 => 'back to replicant 1.';
071bbccb 568
d59cf2f2 569 is $debug{storage_type}, 'REPLICANT',
570 "got last query from a replicant: $debug{dsn}";
571
cb6ec758 572ok $replicated->schema->resultset('Artist')->find(2)
573 => 'back to replicant 2.';
2156bbdd 574
d59cf2f2 575 is $debug{storage_type}, 'REPLICANT',
576 "got last query from a replicant: $debug{dsn}";
071bbccb 577
106d5f3b 578## set all the replicants to inactive, and make sure the balancer falls back to
579## the master.
580
89cf6a70 581$replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
582$replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
9901aad7 583
f404f53c 584{
585 ## catch the fallback to master warning
586 open my $debugfh, '>', \my $fallback_warning;
587 my $oldfh = $replicated->schema->storage->debugfh;
588 $replicated->schema->storage->debugfh($debugfh);
de5dc9ef 589
f404f53c 590 ok $replicated->schema->resultset('Artist')->find(2)
d59cf2f2 591 => 'Fallback to master';
071bbccb 592
d59cf2f2 593 is $debug{storage_type}, 'MASTER',
594 "got last query from a master: $debug{dsn}";
f404f53c 595
596 like $fallback_warning, qr/falling back to master/
9450f312 597 => 'emits falling back to master debug';
f404f53c 598
599 $replicated->schema->storage->debugfh($oldfh);
600}
9901aad7 601
de5dc9ef 602$replicated->schema->storage->replicants->{$replicant_names[0]}->active(1);
603$replicated->schema->storage->replicants->{$replicant_names[1]}->active(1);
9901aad7 604
605## Silence warning about not supporting the is_replicating method if using the
606## sqlite dbs.
607$replicated->schema->storage->debugobj->silence(1)
aff5e9c1 608 if grep { $_ =~ $var_dir_re } @replicant_names;
d59cf2f2 609
f15afa13 610$replicated->schema->storage->pool->validate_replicants;
de5dc9ef 611
9901aad7 612$replicated->schema->storage->debugobj->silence(0);
613
9450f312 614{
615 ## catch the fallback to master warning
616 open my $debugfh, '>', \my $return_warning;
617 my $oldfh = $replicated->schema->storage->debugfh;
618 $replicated->schema->storage->debugfh($debugfh);
619
620 ok $replicated->schema->resultset('Artist')->find(2)
621 => 'Return to replicants';
622
623 is $debug{storage_type}, 'REPLICANT',
624 "got last query from a replicant: $debug{dsn}";
071bbccb 625
9450f312 626 like $return_warning, qr/Moved back to slave/
627 => 'emits returning to slave debug';
628
629 $replicated->schema->storage->debugfh($oldfh);
630}
d59cf2f2 631
de5dc9ef 632## Getting slave status tests
633
f797e89e 634SKIP: {
635 ## We skip this tests unless you have a custom replicants, since the default
636 ## sqlite based replication tests don't support these functions.
d59cf2f2 637
638 skip 'Cannot Test Replicant Status on Non Replicating Database', 10
f797e89e 639 unless DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"};
640
641 $replicated->replicate; ## Give the slaves a chance to catchup.
642
d59cf2f2 643 ok $replicated->schema->storage->replicants->{$replicant_names[0]}->is_replicating
644 => 'Replicants are replicating';
645
646 is $replicated->schema->storage->replicants->{$replicant_names[0]}->lag_behind_master, 0
647 => 'Replicant is zero seconds behind master';
648
649 ## Test the validate replicants
650
651 $replicated->schema->storage->pool->validate_replicants;
652
653 is $replicated->schema->storage->pool->active_replicants, 2
654 => 'Still have 2 replicants after validation';
655
656 ## Force the replicants to fail the validate test by required their lag to
657 ## be negative (ie ahead of the master!)
658
7edf5f1c 659 $replicated->schema->storage->pool->maximum_lag(-10);
660 $replicated->schema->storage->pool->validate_replicants;
d59cf2f2 661
7edf5f1c 662 is $replicated->schema->storage->pool->active_replicants, 0
663 => 'No way a replicant be be ahead of the master';
d59cf2f2 664
7edf5f1c 665 ## Let's be fair to the replicants again. Let them lag up to 5
d59cf2f2 666
7edf5f1c 667 $replicated->schema->storage->pool->maximum_lag(5);
668 $replicated->schema->storage->pool->validate_replicants;
d59cf2f2 669
7edf5f1c 670 is $replicated->schema->storage->pool->active_replicants, 2
d59cf2f2 671 => 'Both replicants in good standing again';
672
673 ## Check auto validate
674
675 is $replicated->schema->storage->balancer->auto_validate_every, 100
676 => "Got the expected value for auto validate";
677
678 ## This will make sure we auto validatge everytime
679 $replicated->schema->storage->balancer->auto_validate_every(0);
680
681 ## set all the replicants to inactive, and make sure the balancer falls back to
682 ## the master.
683
684 $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0);
685 $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0);
686
687 ## Ok, now when we go to run a query, autovalidate SHOULD reconnect
688
689 is $replicated->schema->storage->pool->active_replicants => 0
690 => "both replicants turned off";
691
692 ok $replicated->schema->resultset('Artist')->find(5)
693 => 'replicant reactivated';
694
695 is $debug{storage_type}, 'REPLICANT',
696 "got last query from a replicant: $debug{dsn}";
697
698 is $replicated->schema->storage->pool->active_replicants => 2
699 => "both replicants reactivated";
f797e89e 700}
701
c4d3fae2 702## Test the reliably callback
703
704ok my $reliably = sub {
d59cf2f2 705
c4d3fae2 706 ok $replicated->schema->resultset('Artist')->find(5)
071bbccb 707 => 'replicant reactivated';
708
d59cf2f2 709 is $debug{storage_type}, 'MASTER',
710 "got last query from a master: $debug{dsn}";
711
67cbc0a8 712 $_[1] = 9;
713
c4d3fae2 714} => 'created coderef properly';
715
67cbc0a8 716my @list_to_mangle = (1, 2, 3);
717
718$replicated->schema->storage->execute_reliably($reliably, @list_to_mangle);
719
720is_deeply
721 \@list_to_mangle,
722 [ 1, 9, 3],
723 'Aliasing of values passed to execute_reliably works'
724;
c4d3fae2 725
726## Try something with an error
727
728ok my $unreliably = sub {
d59cf2f2 729
c4d3fae2 730 ok $replicated->schema->resultset('ArtistXX')->find(5)
d59cf2f2 731 => 'replicant reactivated';
732
c4d3fae2 733} => 'created coderef properly';
734
d59cf2f2 735throws_ok {$replicated->schema->storage->execute_reliably($unreliably)}
ed213e85 736 qr/Can't find source for ArtistXX/
c4d3fae2 737 => 'Bad coderef throws proper error';
d59cf2f2 738
67cbc0a8 739throws_ok {
740 $replicated->schema->storage->execute_reliably(sub{
741 die bless [], 'SomeExceptionThing';
742 });
743} 'SomeExceptionThing', "Blessed exception kept intact";
744
ed213e85 745## Make sure replication came back
746
747ok $replicated->schema->resultset('Artist')->find(3)
748 => 'replicant reactivated';
071bbccb 749
750is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}";
d59cf2f2 751
c4d3fae2 752## make sure transactions are set to execute_reliably
753
754ok my $transaction = sub {
d59cf2f2 755
756 my $id = shift @_;
757
758 $replicated
759 ->schema
760 ->populate('Artist', [
761 [ qw/artistid name/ ],
84f7e8a1 762 [ $id, "Children of the Grave $id"],
d59cf2f2 763 ]);
764
64cdad22 765 ok my $result = $replicated->schema->resultset('Artist')->find($id)
bd5da369 766 => "Found expected artist for $id";
071bbccb 767
d59cf2f2 768 is $debug{storage_type}, 'MASTER',
769 "got last query from a master: $debug{dsn}";
770
64cdad22 771 ok my $more = $replicated->schema->resultset('Artist')->find(1)
bd5da369 772 => 'Found expected artist again for 1';
071bbccb 773
d59cf2f2 774 is $debug{storage_type}, 'MASTER',
775 "got last query from a master: $debug{dsn}";
776
ed213e85 777 return ($result, $more);
d59cf2f2 778
64cdad22 779} => 'Created a coderef properly';
c4d3fae2 780
ed213e85 781## Test the transaction with multi return
782{
d59cf2f2 783 ok my @return = $replicated->schema->txn_do($transaction, 666)
784 => 'did transaction';
785
786 is $return[0]->id, 666
787 => 'first returned value is correct';
071bbccb 788
d59cf2f2 789 is $debug{storage_type}, 'MASTER',
790 "got last query from a master: $debug{dsn}";
071bbccb 791
d59cf2f2 792 is $return[1]->id, 1
793 => 'second returned value is correct';
794
795 is $debug{storage_type}, 'MASTER',
796 "got last query from a master: $debug{dsn}";
071bbccb 797
ed213e85 798}
799
800## Test that asking for single return works
801{
d59cf2f2 802 ok my @return = $replicated->schema->txn_do($transaction, 777)
803 => 'did transaction';
804
805 is $return[0]->id, 777
806 => 'first returned value is correct';
807
808 is $return[1]->id, 1
809 => 'second returned value is correct';
ed213e85 810}
811
812## Test transaction returning a single value
813
814{
d59cf2f2 815 ok my $result = $replicated->schema->txn_do(sub {
816 ok my $more = $replicated->schema->resultset('Artist')->find(1)
817 => 'found inside a transaction';
818 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
819 return $more;
820 }) => 'successfully processed transaction';
821
822 is $result->id, 1
823 => 'Got expected single result from transaction';
ed213e85 824}
c4d3fae2 825
826## Make sure replication came back
827
ed213e85 828ok $replicated->schema->resultset('Artist')->find(1)
c4d3fae2 829 => 'replicant reactivated';
071bbccb 830
831is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}";
d59cf2f2 832
ed213e85 833## Test Discard changes
834
835{
d59cf2f2 836 ok my $artist = $replicated->schema->resultset('Artist')->find(2)
837 => 'got an artist to test discard changes';
071bbccb 838
d59cf2f2 839 is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}";
071bbccb 840
d59cf2f2 841 ok $artist->get_from_storage({force_pool=>'master'})
842 => 'properly discard changes';
071bbccb 843
d59cf2f2 844 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
071bbccb 845
3a443bb0 846 ok $artist->discard_changes({force_pool=>'master'})
847 => 'properly called discard_changes against master (manual attrs)';
9c169362 848
3a443bb0 849 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
9c169362 850
3a443bb0 851 ok $artist->discard_changes()
852 => 'properly called discard_changes against master (default attrs)';
9c169362 853
3a443bb0 854 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
8287e9f8 855
3a443bb0 856 ok $artist->discard_changes({force_pool=>$replicant_names[0]})
857 => 'properly able to override the default attributes';
8287e9f8 858
3a443bb0 859 is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}"
ed213e85 860}
64cdad22 861
862## Test some edge cases, like trying to do a transaction inside a transaction, etc
863
864{
865 ok my $result = $replicated->schema->txn_do(sub {
d59cf2f2 866 return $replicated->schema->txn_do(sub {
867 ok my $more = $replicated->schema->resultset('Artist')->find(1)
868 => 'found inside a transaction inside a transaction';
869 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
870 return $more;
871 });
64cdad22 872 }) => 'successfully processed transaction';
d59cf2f2 873
64cdad22 874 is $result->id, 1
d59cf2f2 875 => 'Got expected single result from transaction';
64cdad22 876}
877
878{
879 ok my $result = $replicated->schema->txn_do(sub {
d59cf2f2 880 return $replicated->schema->storage->execute_reliably(sub {
881 return $replicated->schema->txn_do(sub {
882 return $replicated->schema->storage->execute_reliably(sub {
883 ok my $more = $replicated->schema->resultset('Artist')->find(1)
884 => 'found inside crazy deep transactions and execute_reliably';
885 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
886 return $more;
887 });
888 });
889 });
64cdad22 890 }) => 'successfully processed transaction';
d59cf2f2 891
64cdad22 892 is $result->id, 1
d59cf2f2 893 => 'Got expected single result from transaction';
894}
2ce6e9a6 895
7e38d850 896## Test the force_pool resultset attribute.
bbafcf26 897
898{
d59cf2f2 899 ok my $artist_rs = $replicated->schema->resultset('Artist')
bbafcf26 900 => 'got artist resultset';
d59cf2f2 901
902 ## Turn on Forced Pool Storage
903 ok my $reliable_artist_rs = $artist_rs->search(undef, {force_pool=>'master'})
7e38d850 904 => 'Created a resultset using force_pool storage';
d59cf2f2 905
906 ok my $artist = $reliable_artist_rs->find(2)
7e38d850 907 => 'got an artist result via force_pool storage';
071bbccb 908
d59cf2f2 909 is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}";
bbafcf26 910}
911
bd5da369 912## Test the force_pool resultset attribute part two.
913
914{
d59cf2f2 915 ok my $artist_rs = $replicated->schema->resultset('Artist')
bd5da369 916 => 'got artist resultset';
d59cf2f2 917
918 ## Turn on Forced Pool Storage
919 ok my $reliable_artist_rs = $artist_rs->search(undef, {force_pool=>$replicant_names[0]})
bd5da369 920 => 'Created a resultset using force_pool storage';
d59cf2f2 921
922 ok my $artist = $reliable_artist_rs->find(2)
bd5da369 923 => 'got an artist result via force_pool storage';
071bbccb 924
d59cf2f2 925 is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}";
bd5da369 926}
9e75be92 927
0f83441a 928## Delete the old database files
50336325 929$replicated->cleanup;
ee356d00 930
6988233d 931done_testing;
932
ee356d00 933# vim: sw=4 sts=4 :