DBIx-HA
view release on metacpan or search on metacpan
lib/DBIx/HA.pm view on Meta::CPAN
warn "$prefix Connecting to $dbname on init_child\n" if (DBIx_HA_DEBUG);
DBIx::HA->connect($dbname);
}
};
}
sub _readsharedfile {
# reads from file-based shared memory to get active database under Apache
my $dbname = shift;
if ($DATABASE::conf{$dbname}->{'failoverlevel'} eq 'application') {
# do this only if we're doing application failover and not process failover
if ($loaded_Apache && (-f "$logdir/DBIxHA_activedb_$dbname")) {
open IN, "$logdir/DBIxHA_activedb_$dbname";
my $dbidx = <IN>;
chomp $dbidx;
close IN;
if ($dbidx == -1) { # we're told that we're at the end of the stack. No db server is available.
$DATABASE::conf{$dbname}->{'end_of_stack'} = 1;
return 0;
}
$DATABASE::conf{$dbname}->{'end_of_stack'} = 0;
if (($dbidx =~ /^\d+$/o) && $DATABASE::conf{$dbname}->{'db_stack'}->[$dbidx]) {
$DATABASE::conf{$dbname}->{'active_db'} = $DATABASE::conf{$dbname}->{'db_stack'}->[$dbidx];
$DBIx::HA::activeserver{$dbname} = $dbidx;
unless ($Apache::ServerStarting == 1) {
my $r = Apache->request;
$r->notes("activedb_$dbname", $dbidx) if (ref $r);
}
} else {
warn "$prefix in _readsharedfile: $dbname shared file has erroneous content, overwriting.\n";
_writesharedfile($dbname, $DBIx::HA::activeserver{$dbname});
return 0;
}
}
}
return 1;
}
sub _writesharedfile {
my $dbname = shift;
my $dbidx = shift;
# updates the active handle
# and writes to file-based shared memory for active database under Apache
warn "$prefix in _writesharedfile: activating index $dbidx for database $dbname\n" if (DBIx_HA_DEBUG);
$DATABASE::conf{$dbname}->{'active_db'} = $DATABASE::conf{$dbname}->{'db_stack'}->[$dbidx];
$DBIx::HA::activeserver{$dbname} = $dbidx;
if ($DATABASE::conf{$dbname}->{'failoverlevel'} eq 'application') {
# do this only if we're doing application failover and not process failover
if ($loaded_Apache) {
unless ($Apache::ServerStarting == 1) {
my $r = Apache->request;
$r->notes("activedb_$dbname", $dbidx) if (ref $r);
}
$logdir = Apache::server_root_relative(undef,'logs');
open IN, ">/$logdir/DBIxHA_activedb_$dbname" || return 0;
print IN $DBIx::HA::activeserver{$dbname};
close IN;
if ($Apache::ServerStarting == 1) {
chmod 0666, "$logdir/DBIxHA_activedb_$dbname";
}
}
}
return 1;
}
sub _getdbname {
# returns the db server name when given the dsn string
my $dsn = shift;
warn "$prefix in _getdbname: $DBIx::HA::finddbserver{$dsn} \n" if (DBIx_HA_DEBUG > 2);
return $DBIx::HA::finddbserver{$dsn};
}
sub _isactivedb {
# returns true if the db server in use is the one that should be active
my $dsn = shift;
my $dbname = _getdbname ($dsn);
_readsharedfile($dbname);
if ($DATABASE::conf{$dbname}->{'end_of_stack'}) {
# we're not in the active db, because there is no active database, the end of stack is reached.
return 0;
}
if ($dsn eq $DATABASE::conf{$dbname}->{'active_db'}->[0]) {
warn "$prefix in _isactivedb: ".$dsn." is the active one \n" if (DBIx_HA_DEBUG > 2);
return 1;
}
warn "$prefix in _isactivedb: ".$dsn." is NOT active \n" if (DBIx_HA_DEBUG > 2);
$DATABASE::retries{$DATABASE::conf{$dbname}->{'active_db'}->[0]} = 0; # reset the active db's retries for this process
return 0;
}
sub _getnextdb {
# returns the proper db server arrayref to use if the current one is dead
my $dsn = shift;
my $dbname = _getdbname ($dsn);
if (_isactivedb ($dsn)) {
# do this only if we are the first to look for a good db server
# otherwise just return the active db server
my $foundmatch = 0;
my $idxnextdb = 0;
my $stackcount = scalar(@{$DATABASE::conf{$dbname}->{'db_stack'}});
foreach (@{$DATABASE::conf{$dbname}->{'db_stack'}}) {
$idxnextdb++;
if ($dsn eq $_->[0]) {
# we got to the current db server in the stack
# next db server in the stack is the correct one
$foundmatch = 1;
last;
}
}
if (! $foundmatch) { # didn't find a match, current dsn is invalid
warn "$prefix in _getnextdb: current dsn is invalid for $dbname: $dsn \n" if (DBIx_HA_DEBUG);
$idxnextdb = 0;
} elsif ($idxnextdb > ($stackcount - 1)) {
warn "$prefix in _getnextdb: Reached end of db server stack for $dbname. Staying there.\n" if (DBIx_HA_DEBUG);
$DATABASE::conf{$dbname}->{'end_of_stack'} = 1;
_writesharedfile($dbname, -1);
return undef;
}
_writesharedfile($dbname, $idxnextdb);
( run in 0.745 second using v1.01-cache-2.11-cpan-98e64b0badf )