private $mLastError = 'Unknown error';
/** @var integer Total connections opened */
private $connsOpened = 0;
- /** @var ProcessCacheLRU */
- private $mProcCache;
/** @var integer Warn when this many connection are held */
const CONN_HELD_WARN_THRESHOLD = 10;
}
}
}
-
- $this->mProcCache = new ProcessCacheLRU( 30 );
}
/**
return false;
}
- wfDebugLog( 'connect', __METHOD__ .
- ": Using reader #$i: {$this->mServers[$i]['host']}..." );
+ $serverName = $this->getServerName( $i );
+ wfDebugLog( 'connect', __METHOD__ . ": Using reader #$i: $serverName..." );
$conn = $this->openConnection( $i, $wiki );
if ( !$conn ) {
if ( $result == -1 || is_null( $result ) ) {
# Timed out waiting for slave, use master instead
- $server = $this->mServers[$index]['host'];
+ $server = $server = $this->getServerName( $index );
$msg = __METHOD__ . ": Timed out waiting on $server pos {$this->mWaitForPos}";
wfDebug( "$msg\n" );
wfDebugLog( 'DBPerformance', "$msg:\n" . wfBacktrace( true ) );
# Now we have an explicit index into the servers array
$conn = $this->openConnection( $i, $wiki );
if ( !$conn ) {
-
return $this->reportConnectionError();
}
public function openConnection( $i, $wiki = false ) {
if ( $wiki !== false ) {
$conn = $this->openForeignConnection( $i, $wiki );
-
- return $conn;
- }
- if ( isset( $this->mConns['local'][$i][0] ) ) {
+ } elseif ( isset( $this->mConns['local'][$i][0] ) ) {
$conn = $this->mConns['local'][$i][0];
} else {
$server = $this->mServers[$i];
$server['serverIndex'] = $i;
$conn = $this->reallyOpenConnection( $server, false );
+ $serverName = $this->getServerName( $i );
if ( $conn->isOpen() ) {
- wfDebug( "Connected to database $i at {$this->mServers[$i]['host']}\n" );
+ wfDebug( "Connected to database $i at $serverName\n" );
$this->mConns['local'][$i][0] = $conn;
} else {
- wfDebug( "Failed to connect to database $i at {$this->mServers[$i]['host']}\n" );
+ wfDebug( "Failed to connect to database $i at $serverName\n" );
$this->mErrorConnection = $conn;
$conn = false;
}
}
+ if ( $conn && !$conn->isOpen() ) {
+ // Connection was made but later unrecoverably lost for some reason.
+ // Do not return a handle that will just throw exceptions on use,
+ // but let the calling code (e.g. getReaderIndex) try another server.
+ // See DatabaseMyslBase::ping() for how this can happen.
+ $this->mErrorConnection = $conn;
+ $conn = false;
+ }
+
return $conn;
}
/**
* @return int
+ * @since 1.26
*/
- private function getWriterIndex() {
+ public function getWriterIndex() {
return 0;
}
*/
public function getServerName( $i ) {
if ( isset( $this->mServers[$i]['hostName'] ) ) {
- return $this->mServers[$i]['hostName'];
+ $name = $this->mServers[$i]['hostName'];
} elseif ( isset( $this->mServers[$i]['host'] ) ) {
- return $this->mServers[$i]['host'];
+ $name = $this->mServers[$i]['host'];
} else {
- return '';
+ $name = '';
}
+
+ return ( $name != '' ) ? $name : 'localhost';
}
/**
return array( 0 => 0 ); // no replication = no lag
}
- if ( $this->mProcCache->has( 'slave_lag', 'times', 1 ) ) {
- return $this->mProcCache->get( 'slave_lag', 'times' );
- }
-
# Send the request to the load monitor
- $times = $this->getLoadMonitor()->getLagTimes( array_keys( $this->mServers ), $wiki );
-
- $this->mProcCache->set( 'slave_lag', 'times', $times );
-
- return $times;
+ return $this->getLoadMonitor()->getLagTimes( array_keys( $this->mServers ), $wiki );
}
/**
/**
* Clear the cache for slag lag delay times
+ *
+ * This is only used for testing
*/
public function clearLagTimeCache() {
- $this->mProcCache->clear( 'slave_lag' );
- }
-}
-
-/**
- * Helper class to handle automatically marking connections as reusable (via RAII pattern)
- * as well handling deferring the actual network connection until the handle is used
- *
- * @ingroup Database
- * @since 1.22
- */
-class DBConnRef implements IDatabase {
- /** @var LoadBalancer */
- private $lb;
-
- /** @var DatabaseBase|null */
- private $conn;
-
- /** @var array|null */
- private $params;
-
- /**
- * @param LoadBalancer $lb
- * @param DatabaseBase|array $conn Connection or (server index, group, wiki ID) array
- */
- public function __construct( LoadBalancer $lb, $conn ) {
- $this->lb = $lb;
- if ( $conn instanceof DatabaseBase ) {
- $this->conn = $conn;
- } else {
- $this->params = $conn;
- }
- }
-
- public function __call( $name, $arguments ) {
- if ( $this->conn === null ) {
- list( $db, $groups, $wiki ) = $this->params;
- $this->conn = $this->lb->getConnection( $db, $groups, $wiki );
- }
-
- return call_user_func_array( array( $this->conn, $name ), $arguments );
- }
-
- public function __destruct() {
- if ( $this->conn !== null ) {
- $this->lb->reuseConnection( $this->conn );
- }
+ $this->getLoadMonitor()->clearCaches();
}
}