Add Cxn.pm. Make MasterSlave, NibbleIterator, and ReplicaLagWaiter use Cxn. Rewrite, simplify Retry.

This commit is contained in:
Daniel Nichter
2011-10-06 12:47:35 -06:00
parent bd900c5ab8
commit e2e40488c5
9 changed files with 310 additions and 203 deletions

View File

@@ -16,6 +16,7 @@ use DSNParser;
use VersionParser;
use OptionParser;
use Quoter;
use Cxn;
use Sandbox;
use PerconaTest;
@@ -50,10 +51,19 @@ SKIP: {
OptionParser => $o,
DSNParser => $dp,
Quoter => $q,
make_cxn => sub {
my $cxn = new Cxn(
@_,
DSNParser => $dp,
OptionParser => $o,
);
$cxn->connect();
return $cxn;
},
);
is_deeply(
$slaves->[0]->{dsn},
$slaves->[0]->dsn(),
{ A => undef,
D => undef,
F => undef,
@@ -63,6 +73,7 @@ SKIP: {
p => 'msandbox',
t => undef,
u => 'msandbox',
n => 'h=127.0.0.1,P=12346',
server_id => 12346,
master_id => 12345,
source => 'hosts',
@@ -70,14 +81,12 @@ SKIP: {
'get_slaves() from recurse_to_slaves()'
);
my ($id) = $slaves->[0]->{dbh}->selectrow_array('SELECT @@SERVER_ID');
my ($id) = $slaves->[0]->dbh()->selectrow_array('SELECT @@SERVER_ID');
is(
$id,
'12346',
'dbh created from get_slaves()'
);
$slaves->[0]->{dbh}->disconnect();
}
# #############################################################################
@@ -170,6 +179,7 @@ is_deeply(
D => undef,
A => undef,
t => undef,
n => 'h=127.0.0.1,P=2900',
},
'Got master DSN',
);
@@ -531,6 +541,15 @@ my $slaves = $ms->get_slaves(
OptionParser => $o,
DSNParser => $dp,
Quoter => $q,
make_cxn => sub {
my $cxn = new Cxn(
@_,
DSNParser => $dp,
OptionParser => $o,
);
$cxn->connect();
return $cxn;
},
);
is_deeply(
@@ -543,20 +562,19 @@ is_deeply(
h => '127.1',
p => 'msandbox',
t => undef,
u => 'msandbox'
u => 'msandbox',
n => 'h=127.1,P=12346',
},
'get_slaves() from DSN table'
);
my ($id) = $slaves->[0]->{dbh}->selectrow_array('SELECT @@SERVER_ID');
my ($id) = $slaves->[0]->dbh()->selectrow_array('SELECT @@SERVER_ID');
is(
$id,
'12346',
'dbh created from DSN table works'
);
$slaves->[0]->{dbh}->disconnect();
# #############################################################################
# Done.
# #############################################################################

View File

@@ -21,6 +21,7 @@ use TableParser;
use TableNibbler;
use RowChecksum;
use NibbleIterator;
use Cxn;
use PerconaTest;
use constant MKDEBUG => $ENV{MKDEBUG} || 0;
@@ -41,11 +42,17 @@ else {
plan tests => 26;
}
my $q = new Quoter();
my $tp = new TableParser(Quoter=>$q);
my $nb = new TableNibbler(TableParser=>$tp, Quoter=>$q);
my $o = new OptionParser(description => 'NibbleIterator');
my $rc = new RowChecksum(OptionParser => $o, Quoter=>$q);
my $q = new Quoter();
my $tp = new TableParser(Quoter=>$q);
my $nb = new TableNibbler(TableParser=>$tp, Quoter=>$q);
my $o = new OptionParser(description => 'NibbleIterator');
my $rc = new RowChecksum(OptionParser => $o, Quoter=>$q);
my $cxn = new Cxn(
dbh => $dbh,
dsn => { h=>'127.1', P=>'12345', n=>'h=127.1,P=12345' },
DSNParser => $dp,
OptionParser => $o,
);
$o->get_specs("$trunk/bin/pt-table-checksum");
@@ -77,7 +84,7 @@ sub make_nibble_iter {
1 while $si->next();
my $ni = new NibbleIterator(
dbh => $dbh,
Cxn => $cxn,
tbl => $schema->get_table($args{db}, $args{tbl}),
chunk_size => $o->get('chunk-size'),
callbacks => $args{callbacks},

View File

@@ -12,6 +12,7 @@ use English qw(-no_match_vars);
use Test::More tests => 5;
use ReplicaLagWaiter;
use Cxn;
use PerconaTest;
my $oktorun = 1;
@@ -36,13 +37,13 @@ sub sleep {
}
my $rll = new ReplicaLagWaiter(
oktorun => \&oktorun,
get_lag => \&get_lag,
sleep => \&sleep,
max_lag => 1,
slaves => [
{ dsn=>{n=>'slave1'}, dbh=>1 },
{ dsn=>{n=>'slave2'}, dbh=>2 },
oktorun => \&oktorun,
get_lag => \&get_lag,
sleep => \&sleep,
max_lag => 1,
slaves => [
new Cxn(dsn=>{n=>'slave1'}, dbh=>1, DSNParser=>1, OptionParser=>1),
new Cxn(dsn=>{n=>'slave2'}, dbh=>2, DSNParser=>1, OptionParser=>1),
],
);

View File

@@ -9,138 +9,103 @@ BEGIN {
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More tests => 14;
use Test::More tests => 6;
use Retry;
use PerconaTest;
my $success;
my $failure;
my $waitno;
my $tryno;
my $tries;
my $die;
my $rt = new Retry();
my @called = ();
my @retry = ();
my @die = ();
my $try = sub {
if ( $die ) {
$die = 0;
die "I die!\n";
}
return $tryno++ == $tries ? "succeed" : undef;
push @called, 'try';
die if shift @die;
};
my $fail = sub {
push @called, 'fail';
return shift @retry;
};
my $wait = sub {
$waitno++;
push @called, 'wait';
};
my $on_success = sub {
$success = "succeed on $tryno";
my $final_fail = sub {
push @called, 'final_fail';
return;
};
my $on_failure = sub {
$failure = "failed on $tryno";
};
sub try_it {
my ( %args ) = @_;
$success = "";
$failure = "";
$waitno = $args{wainot} || 0;
$tryno = $args{tryno} || 1;
$tries = $args{tries} || 3;
sub try_it {
return $rt->retry(
try => $try,
wait => $wait,
on_success => $on_success,
on_failure => $on_failure,
retry_on_die => $args{retry_on_die},
try => $try,
fail => $fail,
wait => $wait,
final_fail => $final_fail,
);
}
my $retval = try_it();
is(
$retval,
"succeed",
"Retry succeeded"
# Success on first try;
@called = ();
@retry = ();
@die = ();
try_it();
is_deeply(
\@called,
['try'],
'Success on first try'
);
is(
$success,
"succeed on 4",
"Called on_success code"
# Success on 2nd try.
@called = ();
@retry = (1),
@die = (1);
try_it();
is_deeply(
\@called,
['try', 'fail', 'wait',
'try'
],
'Success on second try'
);
is(
$waitno,
2,
"Called wait code"
# Success on 3rd, last try.
@called = ();
@retry = (1, 1),
@die = (1, 1);
try_it();
is_deeply(
\@called,
['try', 'fail', 'wait',
'try', 'fail', 'wait',
'try'
],
'Success on third, final try'
);
# Default tries is 3 so allowing ourself 4 tries will cause the retry
# to fail and the on_failure code should be called.
$retval = try_it(tries=>4);
ok(
!defined $retval,
"Returned undef on failure"
# Failure.
@called = ();
@retry = (1, 1, 1);
@die = (1, 1, 1);
try_it();
is_deeply(
\@called,
['try', 'fail', 'wait',
'try', 'fail', 'wait',
'try', 'final_fail',
],
'Failure'
);
is(
$failure,
"failed on 4",
"Called on_failure code"
);
is(
$success,
"",
"Did not call on_success code"
);
# Test what happens if the try code dies. try_it() will reset $die to 0.
$die = 1;
eval { try_it(); };
is(
$EVAL_ERROR,
"I die!\n",
"Dies if code dies without retry_on_die"
);
ok(
!defined $retval,
"Returned undef on try die"
);
is(
$failure,
"",
"Did not call on_failure code on try die without retry_on_die"
);
is(
$success,
"",
"Did not call on_success code"
);
# Test retry_on_die. This should work with tries=2 because the first
# try will die leaving with only 2 more retries.
$die = 1;
$retval = try_it(retry_on_die=>1, tries=>2);
is(
$retval,
"succeed",
"Retry succeeded with retry_on_die"
);
is(
$success,
"succeed on 3",
"Called on_success code with retry_on_die"
);
is(
$waitno,
2,
"Called wait code with retry_on_die"
# Fail and no retry.
@called = ();
@retry = (0);
@die = (1);
try_it();
is_deeply(
\@called,
['try', 'fail', 'final_fail'],
"Fail, don't retry"
);
# #############################################################################