Skip to content

Commit

Permalink
Merge pull request #46 from gregorg/handle_pg_replication_delay
Browse files Browse the repository at this point in the history
handle postgresql replication delay
  • Loading branch information
gregorg authored Feb 7, 2018
2 parents c37aa22 + 470597f commit 597303a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion features/retry-master-slave.feature
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Feature: Retry Master/Slaves
And "conn" retry limit should be 1
And "conn" should have 2 slaves

@skip-travis @skip-mysqli @skip-pdo-pgsql
@skip-travis @skip-mysqli
Scenario: Replication is stopped on slave and query restart on another slave
Given a retry master/slaves connection "conn" with 2 slaves limited to 1 retry
And slave replication is stopped on "conn"
Expand Down
30 changes: 21 additions & 9 deletions src/Driver/Connection/MasterSlavesConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,28 @@ public function setSlaveStatus(bool $running, ?int $delay) {
}

private function getSlaveStatus() {
try {
$sss = $this->wrappedConnection()->query("SHOW SLAVE STATUS")->fetch();
if ($sss['Slave_IO_Running'] === 'No' || $sss['Slave_SQL_Running'] === 'No') {
// slave is STOPPED
return $this->setSlaveStatus(false, INF);
} else {
return $this->setSlaveStatus(true, $sss['Seconds_Behind_Master']);
if (stripos($this->wrappedDriver->getName(), 'pgsql') !== false) {
try {
$sss = $this->wrappedConnection()->query("SELECT CASE WHEN pg_last_xlog_receive_location() IS NULL THEN NULL WHEN pg_last_xlog_receive_location() = pg_last_xlog_replay_location() THEN '00:00:00' ELSE now() - pg_last_xact_replay_timestamp() END AS replication_lag")->fetch();
return $this->setSlaveStatus(true, $sss['replication_lag']);
} catch (\Exception $e) {
return $this->setSlaveStatus(false, null);
}
} else {
try {
$sss = $this->wrappedConnection()->query("SHOW SLAVE STATUS")->fetch();
if ($sss['Slave_IO_Running'] === 'No' || $sss['Slave_SQL_Running'] === 'No') {
// slave is STOPPED
return $this->setSlaveStatus(false, null);
} else {
return $this->setSlaveStatus(true, $sss['Seconds_Behind_Master']);
}
} catch (\Exception $e) {
if (stripos($e->getMessage(), "Access denied") !== false) {
return $this->setSlaveStatus(true, 0);
}
return $this->setSlaveStatus(false, null);
}
} catch (\Exception $e) {
return $this->setSlaveStatus(true, 0);
}
}

Expand Down

0 comments on commit 597303a

Please sign in to comment.