private $lastError = 'Unknown error';
/** @var string|bool Reason the LB is read-only or false if not */
private $readOnlyReason = false;
- /** @var int Total connections opened */
- private $connsOpened = 0;
+ /** @var int Total number of new connections ever made with this instance */
+ private $connectionCounter = 0;
/** @var bool */
private $disabled = false;
/** @var bool Whether any connection has been attempted yet */
$this->waitTimeout = $params['waitTimeout'] ?? self::MAX_WAIT_DEFAULT;
- $this->conns = self::newConnsArray();
+ $this->conns = self::newTrackedConnectionsArray();
$this->waitForPos = false;
$this->allowLagged = false;
$this->ownerId = $params['ownerId'] ?? null;
}
- private static function newConnsArray() {
+ private static function newTrackedConnectionsArray() {
return [
// Connection were transaction rounds may be applied
self::KEY_LOCAL => [],
$masterOnly = ( $i === self::DB_MASTER || $i === $this->getWriterIndex() );
// Number of connections made before getting the server index and handle
- $priorConnectionsMade = $this->connsOpened;
-
+ $priorConnectionsMade = $this->connectionCounter;
// Choose a server if $i is DB_MASTER/DB_REPLICA (might trigger new connections)
$serverIndex = $this->getConnectionIndex( $i, $groups, $domain );
// Get an open connection to that server (might trigger a new connection)
}
// Profile any new connections caused by this method
- if ( $this->connsOpened > $priorConnectionsMade ) {
+ if ( $this->connectionCounter > $priorConnectionsMade ) {
$host = $conn->getServer();
$dbname = $conn->getDBname();
$this->trxProfiler->recordConnection( $host, $dbname, $masterOnly );
$masterName = $this->getServerName( $this->getWriterIndex() );
$server['clusterMasterHost'] = $masterName;
- // Log when many connection are made on requests
- if ( ++$this->connsOpened >= self::CONN_HELD_WARN_THRESHOLD ) {
- $this->perfLogger->warning( __METHOD__ . ": " .
- "{$this->connsOpened}+ connections made (master=$masterName)" );
- }
-
$server['srvCache'] = $this->srvCache;
// Set loggers and profilers
$server['connLogger'] = $this->connLogger;
// Create a live connection object
try {
$db = Database::factory( $server['type'], $server );
+ // Log when many connection are made on requests
+ ++$this->connectionCounter;
+ $currentConnCount = $this->getCurrentConnectionCount();
+ if ( $currentConnCount >= self::CONN_HELD_WARN_THRESHOLD ) {
+ $this->perfLogger->warning(
+ __METHOD__ . ": {connections}+ connections made (master={masterdb})",
+ [ 'connections' => $currentConnCount, 'masterdb' => $masterName ]
+ );
+ }
} catch ( DBConnectionError $e ) {
// FIXME: This is probably the ugliest thing I have ever done to
// PHP. I'm half-expecting it to segfault, just out of disgust. -- TS
$conn->close();
} );
- $this->conns = self::newConnsArray();
- $this->connsOpened = 0;
+ $this->conns = self::newTrackedConnectionsArray();
}
public function closeConnection( IDatabase $conn ) {
$this->connLogger->debug(
__METHOD__ . ": closing connection to database $i at '$host'." );
unset( $this->conns[$type][$serverIndex][$i] );
- --$this->connsOpened;
break 2;
}
}
}
}
+ /**
+ * @return int
+ */
+ private function getCurrentConnectionCount() {
+ $count = 0;
+ foreach ( $this->conns as $connsByServer ) {
+ foreach ( $connsByServer as $serverConns ) {
+ $count += count( $serverConns );
+ }
+ }
+
+ return $count;
+ }
+
public function getMaxLag( $domain = false ) {
$host = '';
$maxLag = -1;