Merge "Add Special:Users as a synonym for Special:ListUsers"
[lhc/web/wiklou.git] / includes / libs / rdbms / loadbalancer / LoadBalancer.php
index f2e4e3d..bd22aca 100644 (file)
@@ -487,10 +487,8 @@ class LoadBalancer implements ILoadBalancer {
                        $this->waitForPos = $pos;
                        // If a generic reader connection was already established, then wait now
                        $i = $this->readIndex;
-                       if ( $i > 0 ) {
-                               if ( !$this->doWait( $i ) ) {
-                                       $this->laggedReplicaMode = true;
-                               }
+                       if ( ( $i > 0 ) && !$this->doWait( $i ) ) {
+                               $this->laggedReplicaMode = true;
                        }
                } finally {
                        // Restore the older position if it was higher since this is used for lag-protection
@@ -566,15 +564,24 @@ class LoadBalancer implements ILoadBalancer {
        }
 
        public function getAnyOpenConnection( $i, $flags = 0 ) {
+               $i = ( $i === self::DB_MASTER ) ? $this->getWriterIndex() : $i;
                $autocommit = ( ( $flags & self::CONN_TRX_AUTOCOMMIT ) == self::CONN_TRX_AUTOCOMMIT );
+
                foreach ( $this->conns as $connsByServer ) {
-                       if ( !isset( $connsByServer[$i] ) ) {
-                               continue;
+                       if ( $i === self::DB_REPLICA ) {
+                               $indexes = array_keys( $connsByServer );
+                       } else {
+                               $indexes = isset( $connsByServer[$i] ) ? [ $i ] : [];
                        }
 
-                       foreach ( $connsByServer[$i] as $conn ) {
-                               if ( !$autocommit || $conn->getLBInfo( 'autoCommitOnly' ) ) {
-                                       return $conn;
+                       foreach ( $indexes as $index ) {
+                               foreach ( $connsByServer[$index] as $conn ) {
+                                       if ( !$conn->isOpen() ) {
+                                               continue; // some sort of error occured?
+                                       }
+                                       if ( !$autocommit || $conn->getLBInfo( 'autoCommitOnly' ) ) {
+                                               return $conn;
+                                       }
                                }
                        }
                }
@@ -1018,7 +1025,7 @@ class LoadBalancer implements ILoadBalancer {
                                }
                                unset( $this->conns[$connFreeKey][$i][$oldDomain] );
                                // Note that if $domain is an empty string, getDomainID() might not match it
-                               $this->conns[$connInUseKey][$i][$conn->getDomainId()] = $conn;
+                               $this->conns[$connInUseKey][$i][$conn->getDomainID()] = $conn;
                                $this->connLogger->debug( __METHOD__ .
                                        ": reusing free connection from $oldDomain for $domain" );
                                break;
@@ -1842,12 +1849,6 @@ class LoadBalancer implements ILoadBalancer {
                }
        }
 
-       /**
-        * @param IDatabase $conn
-        * @param DBMasterPos|bool $pos
-        * @param int|null $timeout
-        * @return bool
-        */
        public function safeWaitForMasterPos( IDatabase $conn, $pos = false, $timeout = null ) {
                $timeout = max( 1, $timeout ?: $this->waitTimeout );
 
@@ -1862,6 +1863,12 @@ class LoadBalancer implements ILoadBalancer {
                                $pos = $masterConn->getMasterPos();
                        } else {
                                $masterConn = $this->openConnection( $this->getWriterIndex(), self::DOMAIN_ANY );
+                               if ( !$masterConn ) {
+                                       throw new DBReplicationWaitError(
+                                               null,
+                                               "Could not obtain a master database connection to get the position"
+                                       );
+                               }
                                $pos = $masterConn->getMasterPos();
                                $this->closeConnection( $masterConn );
                        }