Break long lines and formatting updates for includes/db/
[lhc/web/wiklou.git] / includes / db / LoadBalancer.php
index 5c4443d..fa3dbc0 100644 (file)
@@ -101,6 +101,7 @@ class LoadBalancer {
                        $class = $this->mLoadMonitorClass;
                        $this->mLoadMonitor = new $class( $this );
                }
+
                return $this->mLoadMonitor;
        }
 
@@ -167,7 +168,7 @@ class LoadBalancer {
                #wfDebugLog( 'connect', var_export( $loads, true ) );
 
                # Return a random representative of the remainder
-               return $this->pickRandom( $loads );
+               return ArrayUtils::pickRandom( $loads );
        }
 
        /**
@@ -212,6 +213,7 @@ class LoadBalancer {
                                # No loads for this group, return false and the caller can use some other group
                                wfDebug( __METHOD__ . ": no loads for group $group\n" );
                                wfProfileOut( __METHOD__ );
+
                                return false;
                        }
                } else {
@@ -236,7 +238,7 @@ class LoadBalancer {
                        $currentLoads = $nonErrorLoads;
                        while ( count( $currentLoads ) ) {
                                if ( $wgReadOnly || $this->mAllowLagged || $laggedSlaveMode ) {
-                                       $i = $this->pickRandom( $currentLoads );
+                                       $i = ArrayUtils::pickRandom( $currentLoads );
                                } else {
                                        $i = $this->getRandomNonLagged( $currentLoads, $wiki );
                                        if ( $i === false && count( $currentLoads ) != 0 ) {
@@ -244,7 +246,7 @@ class LoadBalancer {
                                                wfDebugLog( 'replication', "All slaves lagged. Switch to read-only mode\n" );
                                                $wgReadOnly = 'The database has been automatically locked ' .
                                                        'while the slave database servers catch up to the master';
-                                               $i = $this->pickRandom( $currentLoads );
+                                               $i = ArrayUtils::pickRandom( $currentLoads );
                                                $laggedSlaveMode = true;
                                        }
                                }
@@ -255,6 +257,7 @@ class LoadBalancer {
                                        # wants us to return false.
                                        wfDebugLog( 'connect', __METHOD__ . ": pickRandom() returned false\n" );
                                        wfProfileOut( __METHOD__ );
+
                                        return false;
                                }
 
@@ -329,6 +332,7 @@ class LoadBalancer {
                        }
                }
                wfProfileOut( __METHOD__ );
+
                return $i;
        }
 
@@ -342,6 +346,7 @@ class LoadBalancer {
                wfDebug( __METHOD__ . ": waiting $t us\n" );
                usleep( $t );
                wfProfileOut( __METHOD__ );
+
                return $t;
        }
 
@@ -373,7 +378,9 @@ class LoadBalancer {
                wfProfileIn( __METHOD__ );
                $this->mWaitForPos = $pos;
                for ( $i = 1; $i < count( $this->mServers ); $i++ ) {
-                       $this->doWait( $i, true );
+                       if ( $this->mLoads[$i] > 0 ) {
+                               $this->doWait( $i, true );
+                       }
                }
                wfProfileOut( __METHOD__ );
        }
@@ -391,6 +398,7 @@ class LoadBalancer {
                                return reset( $conns[$i] );
                        }
                }
+
                return false;
        }
 
@@ -406,11 +414,13 @@ class LoadBalancer {
                if ( !$conn ) {
                        if ( !$open ) {
                                wfDebug( __METHOD__ . ": no connection open\n" );
+
                                return false;
                        } else {
                                $conn = $this->openConnection( $index, '' );
                                if ( !$conn ) {
                                        wfDebug( __METHOD__ . ": failed to open connection\n" );
+
                                        return false;
                                }
                        }
@@ -422,9 +432,11 @@ class LoadBalancer {
                if ( $result == -1 || is_null( $result ) ) {
                        # Timed out waiting for slave, use master instead
                        wfDebug( __METHOD__ . ": Timed out waiting for slave #$index pos {$this->mWaitForPos}\n" );
+
                        return false;
                } else {
                        wfDebug( __METHOD__ . ": Done\n" );
+
                        return true;
                }
        }
@@ -445,10 +457,12 @@ class LoadBalancer {
 
                if ( $i == DB_LAST ) {
                        wfProfileOut( __METHOD__ );
-                       throw new MWException( 'Attempt to call ' . __METHOD__ . ' with deprecated server index DB_LAST' );
+                       throw new MWException( 'Attempt to call ' . __METHOD__ .
+                               ' with deprecated server index DB_LAST' );
                } elseif ( $i === null || $i === false ) {
                        wfProfileOut( __METHOD__ );
-                       throw new MWException( 'Attempt to call ' . __METHOD__ . ' with invalid server index' );
+                       throw new MWException( 'Attempt to call ' . __METHOD__ .
+                               ' with invalid server index' );
                }
 
                if ( $wiki === wfWikiID() ) {
@@ -485,6 +499,7 @@ class LoadBalancer {
                        if ( $i === false ) {
                                $this->mLastError = 'No working slave server: ' . $this->mLastError;
                                wfProfileOut( __METHOD__ );
+
                                return $this->reportConnectionError();
                        }
                }
@@ -493,10 +508,12 @@ class LoadBalancer {
                $conn = $this->openConnection( $i, $wiki );
                if ( !$conn ) {
                        wfProfileOut( __METHOD__ );
+
                        return $this->reportConnectionError();
                }
 
                wfProfileOut( __METHOD__ );
+
                return $conn;
        }
 
@@ -520,6 +537,7 @@ class LoadBalancer {
                }
                if ( $serverIndex === null || $refCount === null ) {
                        wfDebug( __METHOD__ . ": this connection was not opened as a foreign connection\n" );
+
                        /**
                         * This can happen in code like:
                         *   foreach ( $dbs as $db ) {
@@ -530,10 +548,12 @@ class LoadBalancer {
                         * When a connection to the local DB is opened in this way, reuseConnection()
                         * should be ignored
                         */
+
                        return;
                }
                if ( $this->mConns['foreignUsed'][$serverIndex][$wiki] !== $conn ) {
-                       throw new MWException( __METHOD__ . ": connection not found, has the connection been freed already?" );
+                       throw new MWException( __METHOD__ . ": connection not found, has " .
+                               "the connection been freed already?" );
                }
                $conn->setLBInfo( 'foreignPoolRefCount', --$refCount );
                if ( $refCount <= 0 ) {
@@ -561,6 +581,22 @@ class LoadBalancer {
                return new DBConnRef( $this, $this->getConnection( $db, $groups, $wiki ) );
        }
 
+       /**
+        * Get a database connection handle reference without connecting yet
+        *
+        * The handle's methods wrap simply wrap those of a DatabaseBase handle
+        *
+        * @see LoadBalancer::getConnection() for parameter information
+        *
+        * @param integer $db
+        * @param mixed $groups
+        * @param string $wiki
+        * @return DBConnRef
+        */
+       public function getLazyConnectionRef( $db, $groups = array(), $wiki = false ) {
+               return new DBConnRef( $this, array( $db, $groups, $wiki ) );
+       }
+
        /**
         * Open a connection to the server given by the specified index
         * Index must be an actual index into the array.
@@ -580,6 +616,7 @@ class LoadBalancer {
                if ( $wiki !== false ) {
                        $conn = $this->openForeignConnection( $i, $wiki );
                        wfProfileOut( __METHOD__ );
+
                        return $conn;
                }
                if ( isset( $this->mConns['local'][$i][0] ) ) {
@@ -598,6 +635,7 @@ class LoadBalancer {
                        }
                }
                wfProfileOut( __METHOD__ );
+
                return $conn;
        }
 
@@ -653,6 +691,7 @@ class LoadBalancer {
                        $server = $this->mServers[$i];
                        $server['serverIndex'] = $i;
                        $server['foreignPoolRefCount'] = 0;
+                       $server['foreign'] = true;
                        $conn = $this->reallyOpenConnection( $server, $dbName );
                        if ( !$conn->isOpen() ) {
                                wfDebug( __METHOD__ . ": error opening connection for $i/$wiki\n" );
@@ -671,6 +710,7 @@ class LoadBalancer {
                        $conn->setLBInfo( 'foreignPoolRefCount', $refCount + 1 );
                }
                wfProfileOut( __METHOD__ );
+
                return $conn;
        }
 
@@ -685,6 +725,7 @@ class LoadBalancer {
                if ( !is_integer( $index ) ) {
                        return false;
                }
+
                return (bool)$this->getAnyOpenConnection( $index );
        }
 
@@ -724,6 +765,7 @@ class LoadBalancer {
                if ( isset( $server['fakeMaster'] ) ) {
                        $db->setFakeMaster( true );
                }
+
                return $db;
        }
 
@@ -745,6 +787,7 @@ class LoadBalancer {
                        wfLogDBError( "Connection error: {$this->mLastError} ({$server})\n" );
                        $conn->reportConnectionError( "{$this->mLastError} ({$server})" ); // throws DBConnectionError
                }
+
                return false; /* not reached */
        }
 
@@ -814,7 +857,8 @@ class LoadBalancer {
        }
 
        /**
-        * Sets the server info structure for the given index. Entry at index $i is created if it doesn't exist
+        * Sets the server info structure for the given index. Entry at index $i
+        * is created if it doesn't exist
         * @param $i
         * @param $serverInfo
         */
@@ -835,13 +879,16 @@ class LoadBalancer {
                                $conn = $this->getAnyOpenConnection( $i );
                                if ( $conn ) {
                                        wfDebug( "Master pos fetched from slave\n" );
+
                                        return $conn->getSlavePos();
                                }
                        }
                } else {
                        wfDebug( "Master pos fetched from master\n" );
+
                        return $masterConn->getMasterPos();
                }
+
                return false;
        }
 
@@ -957,6 +1004,7 @@ class LoadBalancer {
                        return $this->mAllowLagged;
                }
                $this->mAllowLagged = $mode;
+
                return $this->mAllowLagged;
        }
 
@@ -974,6 +1022,7 @@ class LoadBalancer {
                                }
                        }
                }
+
                return $success;
        }
 
@@ -1027,6 +1076,7 @@ class LoadBalancer {
                                }
                        }
                }
+
                return array( $host, $maxLag, $maxIndex );
        }
 
@@ -1051,6 +1101,7 @@ class LoadBalancer {
                        $this->mLagTimes = $this->getLoadMonitor()->getLagTimes(
                                array_keys( $this->mServers ), $wiki );
                }
+
                return $this->mLagTimes;
        }
 
@@ -1087,6 +1138,7 @@ class LoadBalancer {
 
 /**
  * Helper class to handle automatically marking connectons as reusable (via RAII pattern)
+ * as well handling deferring the actual network connection until the handle is used
  *
  * @ingroup Database
  * @since 1.22
@@ -1094,23 +1146,36 @@ class LoadBalancer {
 class DBConnRef implements IDatabase {
        /** @var LoadBalancer */
        protected $lb;
-       /** @var DatabaseBase */
+       /** @var DatabaseBase|null */
        protected $conn;
+       /** @var Array|null */
+       protected $params;
 
        /**
         * @param $lb LoadBalancer
-        * @param $conn DatabaseBase
+        * @param $conn DatabaseBase|array Connection or (server index, group, wiki ID) array
         */
-       public function __construct( LoadBalancer $lb, DatabaseBase $conn ) {
+       public function __construct( LoadBalancer $lb, $conn ) {
                $this->lb = $lb;
-               $this->conn = $conn;
+               if ( $conn instanceof DatabaseBase ) {
+                       $this->conn = $conn;
+               } else {
+                       $this->params = $conn;
+               }
        }
 
        public function __call( $name, $arguments ) {
+               if ( $this->conn === null ) {
+                       list( $db, $groups, $wiki ) = $this->params;
+                       $this->conn = $this->lb->getConnection( $db, $groups, $wiki );
+               }
+
                return call_user_func_array( array( $this->conn, $name ), $arguments );
        }
 
        function __destruct() {
-               $this->lb->reuseConnection( $this->conn );
+               if ( $this->conn !== null ) {
+                       $this->lb->reuseConnection( $this->conn );
+               }
        }
 }