Merge "Force User::__toString() return value to be string"
[lhc/web/wiklou.git] / includes / libs / rdbms / loadbalancer / LoadBalancer.php
index 33f3561..894c0dc 100644 (file)
@@ -21,6 +21,7 @@
  * @ingroup Database
  */
 use Psr\Log\LoggerInterface;
+use Wikimedia\ScopedCallback;
 
 /**
  * Database connection, tracking, load balancing, and transaction manager for a cluster
@@ -667,6 +668,10 @@ class LoadBalancer implements ILoadBalancer {
                } elseif ( isset( $this->mConns['local'][$i][0] ) ) {
                        $conn = $this->mConns['local'][$i][0];
                } else {
+                       if ( !isset( $this->mServers[$i] ) || !is_array( $this->mServers[$i] ) ) {
+                               throw new InvalidArgumentException( "No server with index '$i'." );
+                       }
+                       // Open a new connection
                        $server = $this->mServers[$i];
                        $server['serverIndex'] = $i;
                        $conn = $this->reallyOpenConnection( $server, false );
@@ -747,6 +752,9 @@ class LoadBalancer implements ILoadBalancer {
                                        ": reusing free connection from $oldDomain for $domain" );
                        }
                } else {
+                       if ( !isset( $this->mServers[$i] ) || !is_array( $this->mServers[$i] ) ) {
+                               throw new InvalidArgumentException( "No server with index '$i'." );
+                       }
                        // Open a new connection
                        $server = $this->mServers[$i];
                        $server['serverIndex'] = $i;
@@ -799,17 +807,11 @@ class LoadBalancer implements ILoadBalancer {
         * @throws DBAccessError
         * @throws InvalidArgumentException
         */
-       protected function reallyOpenConnection( $server, $dbNameOverride = false ) {
+       protected function reallyOpenConnection( array $server, $dbNameOverride = false ) {
                if ( $this->disabled ) {
                        throw new DBAccessError();
                }
 
-               if ( !is_array( $server ) ) {
-                       throw new InvalidArgumentException(
-                               'You must update your load-balancing configuration. ' .
-                               'See DefaultSettings.php entry for $wgDBservers.' );
-               }
-
                if ( $dbNameOverride !== false ) {
                        $server['dbname'] = $dbNameOverride;
                }
@@ -885,7 +887,7 @@ class LoadBalancer implements ILoadBalancer {
                        // If all servers were busy, mLastError will contain something sensible
                        throw new DBConnectionError( null, $this->mLastError );
                } else {
-                       $context['db_server'] = $conn->getProperty( 'mServer' );
+                       $context['db_server'] = $conn->getServer();
                        $this->connLogger->warning(
                                "Connection error: {last_error} ({db_server})",
                                $context
@@ -1323,7 +1325,7 @@ class LoadBalancer implements ILoadBalancer {
                        $cache->makeGlobalKey( __CLASS__, 'server-read-only', $masterServer ),
                        self::TTL_CACHE_READONLY,
                        function () use ( $domain, $conn ) {
-                               $this->trxProfiler->setSilenced( true );
+                               $old = $this->trxProfiler->setSilenced( true );
                                try {
                                        $dbw = $conn ?: $this->getConnection( self::DB_MASTER, [], $domain );
                                        $readOnly = (int)$dbw->serverIsReadOnly();
@@ -1333,7 +1335,7 @@ class LoadBalancer implements ILoadBalancer {
                                } catch ( DBError $e ) {
                                        $readOnly = 0;
                                }
-                               $this->trxProfiler->setSilenced( false );
+                               $this->trxProfiler->setSilenced( $old );
                                return $readOnly;
                        },
                        [ 'pcTTL' => $cache::TTL_PROC_LONG, 'busyValue' => 0 ]