From bfdde8b2ffc250c8d64099ba82f132e668e0a681 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Tue, 14 Aug 2018 21:19:12 -0700 Subject: [PATCH] rdbms: split out private LoadBalancer::openLocalConnection method Change-Id: I2138b3d28f8f1a8ea706165fbe3a7e3f0d858447 --- .../libs/rdbms/loadbalancer/LoadBalancer.php | 76 ++++++++++++------- 1 file changed, 48 insertions(+), 28 deletions(-) diff --git a/includes/libs/rdbms/loadbalancer/LoadBalancer.php b/includes/libs/rdbms/loadbalancer/LoadBalancer.php index 92cabca52f..5e08094242 100644 --- a/includes/libs/rdbms/loadbalancer/LoadBalancer.php +++ b/includes/libs/rdbms/loadbalancer/LoadBalancer.php @@ -886,34 +886,7 @@ class LoadBalancer implements ILoadBalancer { $conn = $this->openForeignConnection( $i, $domain, $flags ); } else { // Connection is to the local domain - $connKey = $autoCommit ? self::KEY_LOCAL_NOROUND : self::KEY_LOCAL; - if ( isset( $this->conns[$connKey][$i][0] ) ) { - $conn = $this->conns[$connKey][$i][0]; - } else { - if ( !isset( $this->servers[$i] ) || !is_array( $this->servers[$i] ) ) { - throw new InvalidArgumentException( "No server with index '$i'." ); - } - // Open a new connection - $server = $this->servers[$i]; - $server['serverIndex'] = $i; - $server['autoCommitOnly'] = $autoCommit; - if ( $this->localDomain->getDatabase() !== null ) { - // Use the local domain table prefix if the local domain is specified - $server['tablePrefix'] = $this->localDomain->getTablePrefix(); - } - $conn = $this->reallyOpenConnection( $server, $this->localDomain ); - $host = $this->getServerName( $i ); - if ( $conn->isOpen() ) { - $this->connLogger->debug( - __METHOD__ . ": connected to database $i at '$host'." ); - $this->conns[$connKey][$i][0] = $conn; - } else { - $this->connLogger->warning( - __METHOD__ . ": failed to connect to database $i at '$host'." ); - $this->errorConnection = $conn; - $conn = false; - } - } + $conn = $this->openLocalConnection( $i, $flags ); } if ( $conn instanceof IDatabase && !$conn->isOpen() ) { @@ -939,6 +912,53 @@ class LoadBalancer implements ILoadBalancer { return $conn; } + /** + * Open a connection to a local DB, or return one if it is already open. + * + * On error, returns false, and the connection which caused the + * error will be available via $this->errorConnection. + * + * @note If disable() was called on this LoadBalancer, this method will throw a DBAccessError. + * + * @param int $i Server index + * @param int $flags Class CONN_* constant bitfield + * @return Database + */ + private function openLocalConnection( $i, $flags = 0 ) { + $autoCommit = ( ( $flags & self::CONN_TRX_AUTOCOMMIT ) == self::CONN_TRX_AUTOCOMMIT ); + + $connKey = $autoCommit ? self::KEY_LOCAL_NOROUND : self::KEY_LOCAL; + if ( isset( $this->conns[$connKey][$i][0] ) ) { + $conn = $this->conns[$connKey][$i][0]; + } else { + if ( !isset( $this->servers[$i] ) || !is_array( $this->servers[$i] ) ) { + throw new InvalidArgumentException( "No server with index '$i'." ); + } + // Open a new connection + $server = $this->servers[$i]; + $server['serverIndex'] = $i; + $server['autoCommitOnly'] = $autoCommit; + if ( $this->localDomain->getDatabase() !== null ) { + // Use the local domain table prefix if the local domain is specified + $server['tablePrefix'] = $this->localDomain->getTablePrefix(); + } + $conn = $this->reallyOpenConnection( $server, $this->localDomain ); + $host = $this->getServerName( $i ); + if ( $conn->isOpen() ) { + $this->connLogger->debug( + __METHOD__ . ": connected to database $i at '$host'." ); + $this->conns[$connKey][$i][0] = $conn; + } else { + $this->connLogger->warning( + __METHOD__ . ": failed to connect to database $i at '$host'." ); + $this->errorConnection = $conn; + $conn = false; + } + } + + return $conn; + } + /** * Open a connection to a foreign DB, or return one if it is already open. * -- 2.20.1