From c50202a9e435e4260ee4471a73f7abb884b1bb2d Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Sat, 13 Jul 2019 14:52:15 -0700 Subject: [PATCH] objectcache: only process cache non-LB connections in SqlBagOStuff LB should manage any such process caching otherwise as it owns the handles Change-Id: Ic9cd84c7c3cba790eb127e70f6506270e5bec2a1 --- includes/objectcache/SqlBagOStuff.php | 54 ++++++++++++++------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/includes/objectcache/SqlBagOStuff.php b/includes/objectcache/SqlBagOStuff.php index a8c23d6158..7e5a8a46b6 100644 --- a/includes/objectcache/SqlBagOStuff.php +++ b/includes/objectcache/SqlBagOStuff.php @@ -174,19 +174,20 @@ class SqlBagOStuff extends BagOStuff { * @throws MWException */ protected function getDB( $serverIndex ) { - if ( !isset( $this->conns[$serverIndex] ) ) { - if ( $serverIndex >= $this->numServers ) { - throw new MWException( __METHOD__ . ": Invalid server index \"$serverIndex\"" ); - } + if ( $serverIndex >= $this->numServers ) { + throw new MWException( __METHOD__ . ": Invalid server index \"$serverIndex\"" ); + } - # Don't keep timing out trying to connect for each call if the DB is down - if ( isset( $this->connFailureErrors[$serverIndex] ) - && ( time() - $this->connFailureTimes[$serverIndex] ) < 60 - ) { - throw $this->connFailureErrors[$serverIndex]; - } + # Don't keep timing out trying to connect for each call if the DB is down + if ( + isset( $this->connFailureErrors[$serverIndex] ) && + ( time() - $this->connFailureTimes[$serverIndex] ) < 60 + ) { + throw $this->connFailureErrors[$serverIndex]; + } - if ( $this->serverInfos ) { + if ( $this->serverInfos ) { + if ( !isset( $this->conns[$serverIndex] ) ) { // Use custom database defined by server connection info $info = $this->serverInfos[$serverIndex]; $type = $info['type'] ?? 'mysql'; @@ -194,25 +195,26 @@ class SqlBagOStuff extends BagOStuff { $this->logger->debug( __CLASS__ . ": connecting to $host" ); $db = Database::factory( $type, $info ); $db->clearFlag( DBO_TRX ); // auto-commit mode + $this->conns[$serverIndex] = $db; + } + $db = $this->conns[$serverIndex]; + } else { + // Use the main LB database + $lb = MediaWikiServices::getInstance()->getDBLoadBalancer(); + $index = $this->replicaOnly ? DB_REPLICA : DB_MASTER; + if ( $lb->getServerType( $lb->getWriterIndex() ) !== 'sqlite' ) { + // Keep a separate connection to avoid contention and deadlocks + $db = $lb->getConnection( $index, [], false, $lb::CONN_TRX_AUTOCOMMIT ); } else { - // Use the main LB database - $lb = MediaWikiServices::getInstance()->getDBLoadBalancer(); - $index = $this->replicaOnly ? DB_REPLICA : DB_MASTER; - if ( $lb->getServerType( $lb->getWriterIndex() ) !== 'sqlite' ) { - // Keep a separate connection to avoid contention and deadlocks - $db = $lb->getConnection( $index, [], false, $lb::CONN_TRX_AUTOCOMMIT ); - } else { - // However, SQLite has the opposite behavior due to DB-level locking. - // Stock sqlite MediaWiki installs use a separate sqlite cache DB instead. - $db = $lb->getConnection( $index ); - } + // However, SQLite has the opposite behavior due to DB-level locking. + // Stock sqlite MediaWiki installs use a separate sqlite cache DB instead. + $db = $lb->getConnection( $index ); } - - $this->logger->debug( sprintf( "Connection %s will be used for SqlBagOStuff", $db ) ); - $this->conns[$serverIndex] = $db; } - return $this->conns[$serverIndex]; + $this->logger->debug( sprintf( "Connection %s will be used for SqlBagOStuff", $db ) ); + + return $db; } /** -- 2.20.1