From 69c71cc45eba02fb0cbe3a46b2892f4e9dc0cb5d Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Fri, 29 Jan 2016 13:00:08 -0800 Subject: [PATCH] Add LoadBalancer::safeWaitForPos() This is useful for waiting on a specific slave handle to reach a given master position. Change-Id: Ia4569c4fc82c25eb4089cf7198f01e349636e2c7 --- includes/db/loadbalancer/LoadBalancer.php | 36 +++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/includes/db/loadbalancer/LoadBalancer.php b/includes/db/loadbalancer/LoadBalancer.php index b5a79a94fa..fc7fa13223 100644 --- a/includes/db/loadbalancer/LoadBalancer.php +++ b/includes/db/loadbalancer/LoadBalancer.php @@ -1363,10 +1363,10 @@ class LoadBalancer { * function instead of Database::getLag() avoids a fatal error in this * case on many installations. * - * @param DatabaseBase $conn + * @param IDatabase $conn * @return int */ - public function safeGetLag( $conn ) { + public function safeGetLag( IDatabase $conn ) { if ( $this->getServerCount() == 1 ) { return 0; } else { @@ -1374,6 +1374,38 @@ class LoadBalancer { } } + /** + * Wait for a slave DB to reach a specified master position + * + * This will connect to the master to get an accurate position if $pos is not given + * + * @param IDatabase $conn Slave DB + * @param DBMasterPos|bool $pos Master position; default: current position + * @param integer $timeout Timeout in seconds + * @return bool Success + * @since 1.27 + */ + public function safeWaitForPos( IDatabase $conn, $pos = false, $timeout = 10 ) { + if ( $this->getServerCount() == 1 || !$conn->getLBInfo( 'slave' ) ) { + return true; // server is not a slave DB + } + + $pos = $pos ?: $this->getConnection( DB_MASTER )->getMasterPos(); + + $result = $conn->masterPosWait( $pos, $timeout ); + if ( $result == -1 || is_null( $result ) ) { + $msg = __METHOD__ . ": Timed out waiting on {$conn->getServer()} pos {$pos}"; + wfDebugLog( 'replication', "$msg\n" ); + wfDebugLog( 'DBPerformance', "$msg:\n" . wfBacktrace( true ) ); + $ok = false; + } else { + wfDebugLog( 'replication', __METHOD__ . ": Done\n" ); + $ok = true; + } + + return $ok; + } + /** * Clear the cache for slag lag delay times * -- 2.20.1