Merge "Provide correct type as input for LoadMonitor::postConnectionBackoff()"
[lhc/web/wiklou.git] / includes / db / LoadBalancer.php
index 9967cc0..c88ad05 100644 (file)
  */
 class LoadBalancer {
        private $mServers, $mConns, $mLoads, $mGroupLoads;
+
+       /** @var bool|DatabaseBase Database connection that caused a problem */
        private $mErrorConnection;
        private $mReadIndex, $mAllowLagged;
-       private $mWaitForPos, $mWaitTimeout;
+
+       /** @var bool|DBMasterPos False if not set */
+       private $mWaitForPos;
+
+       private $mWaitTimeout;
        private $mLaggedSlaveMode, $mLastError = 'Unknown error';
        private $mParentInfo, $mLagTimes;
        private $mLoadMonitorClass, $mLoadMonitor;
 
        /**
         * @param array $params with keys:
-        *    servers           Required. Array of server info structures.
-        *    masterWaitTimeout Replication lag wait timeout
-        *    loadMonitor       Name of a class used to fetch server lag and load.
+        *   servers           Required. Array of server info structures.
+        *   masterWaitTimeout Replication lag wait timeout
+        *   loadMonitor       Name of a class used to fetch server lag and load.
         * @throws MWException
         */
        function __construct( $params ) {
@@ -72,9 +78,9 @@ class LoadBalancer {
                } else {
                        $master = reset( $params['servers'] );
                        if ( isset( $master['type'] ) && $master['type'] === 'mysql' ) {
-                               $this->mLoadMonitorClass = 'LoadMonitor_MySQL';
+                               $this->mLoadMonitorClass = 'LoadMonitorMySQL';
                        } else {
-                               $this->mLoadMonitorClass = 'LoadMonitor_Null';
+                               $this->mLoadMonitorClass = 'LoadMonitorNull';
                        }
                }
 
@@ -108,7 +114,7 @@ class LoadBalancer {
        /**
         * Get or set arbitrary data used by the parent object, usually an LBFactory
         * @param $x
-        * @return Mixed
+        * @return mixed
         */
        function parentInfo( $x = null ) {
                return wfSetVar( $this->mParentInfo, $x );
@@ -120,8 +126,7 @@ class LoadBalancer {
         *
         * @deprecated since 1.21, use ArrayUtils::pickRandom()
         *
-        * @param $weights array
-        *
+        * @param array $weights
         * @return bool|int|string
         */
        function pickRandom( $weights ) {
@@ -129,8 +134,8 @@ class LoadBalancer {
        }
 
        /**
-        * @param $loads array
-        * @param $wiki bool
+        * @param array $loads
+        * @param bool|string $wiki Wiki to get non-lagged for
         * @return bool|int|string
         */
        function getRandomNonLagged( $loads, $wiki = false ) {
@@ -177,8 +182,8 @@ class LoadBalancer {
         * always return a consistent index during a given invocation
         *
         * Side effect: opens connections to databases
-        * @param $group bool
-        * @param $wiki bool
+        * @param bool|string $group
+        * @param bool|string $wiki
         * @throws MWException
         * @return bool|int|string
         */
@@ -273,7 +278,7 @@ class LoadBalancer {
 
                                // Perform post-connection backoff
                                $threshold = isset( $this->mServers[$i]['max threads'] )
-                                       ? $this->mServers[$i]['max threads'] : false;
+                                       ? $this->mServers[$i]['max threads'] : 0;
                                $backoff = $this->getLoadMonitor()->postConnectionBackoff( $conn, $threshold );
 
                                // Decrement reference counter, we are finished with this connection.
@@ -338,7 +343,7 @@ class LoadBalancer {
 
        /**
         * Wait for a specified number of microseconds, and return the period waited
-        * @param $t int
+        * @param int $t
         * @return int
         */
        function sleep( $t ) {
@@ -354,7 +359,7 @@ class LoadBalancer {
         * Set the master wait position
         * If a DB_SLAVE connection has been opened already, waits
         * Otherwise sets a variable telling it to wait if such a connection is opened
-        * @param $pos DBMasterPos
+        * @param DBMasterPos $pos
         */
        public function waitFor( $pos ) {
                wfProfileIn( __METHOD__ );
@@ -372,7 +377,7 @@ class LoadBalancer {
 
        /**
         * Set the master wait position and wait for ALL slaves to catch up to it
-        * @param $pos DBMasterPos
+        * @param DBMasterPos $pos
         */
        public function waitForAll( $pos ) {
                wfProfileIn( __METHOD__ );
@@ -390,7 +395,7 @@ class LoadBalancer {
         * Get any open connection to a given server index, local or foreign
         * Returns false if there is no connection open
         *
-        * @param $i int
+        * @param int $i
         * @return DatabaseBase|bool False on failure
         */
        function getAnyOpenConnection( $i ) {
@@ -575,7 +580,7 @@ class LoadBalancer {
         *
         * @param integer $db
         * @param mixed $groups
-        * @param string $wiki
+        * @param bool|string $wiki
         * @return DBConnRef
         */
        public function getConnectionRef( $db, $groups = array(), $wiki = false ) {
@@ -591,7 +596,7 @@ class LoadBalancer {
         *
         * @param integer $db
         * @param mixed $groups
-        * @param string $wiki
+        * @param bool|string $wiki
         * @return DBConnRef
         */
        public function getLazyConnectionRef( $db, $groups = array(), $wiki = false ) {
@@ -607,7 +612,7 @@ class LoadBalancer {
         * error will be available via $this->mErrorConnection.
         *
         * @param $i Integer server index
-        * @param string $wiki wiki ID to open
+        * @param bool|string $wiki wiki ID to open
         * @return DatabaseBase
         *
         * @access private
@@ -802,7 +807,7 @@ class LoadBalancer {
        /**
         * Returns true if the specified index is a valid server index
         *
-        * @param $i
+        * @param string $i
         * @return bool
         */
        function haveIndex( $i ) {
@@ -812,7 +817,7 @@ class LoadBalancer {
        /**
         * Returns true if the specified index is valid and has non-zero load
         *
-        * @param $i
+        * @param string $i
         * @return bool
         */
        function isNonZeroLoad( $i ) {
@@ -831,7 +836,7 @@ class LoadBalancer {
        /**
         * Get the host name or IP address of the server with the specified index
         * Prefer a readable name if available.
-        * @param $i
+        * @param string $i
         * @return string
         */
        function getServerName( $i ) {
@@ -900,6 +905,7 @@ class LoadBalancer {
        function closeAll() {
                foreach ( $this->mConns as $conns2 ) {
                        foreach ( $conns2 as $conns3 ) {
+                               /** @var DatabaseBase $conn */
                                foreach ( $conns3 as $conn ) {
                                        $conn->close();
                                }
@@ -916,7 +922,7 @@ class LoadBalancer {
         * Deprecated function, typo in function name
         *
         * @deprecated in 1.18
-        * @param $conn
+        * @param DatabaseBase $conn
         */
        function closeConnecton( $conn ) {
                wfDeprecated( __METHOD__, '1.18' );
@@ -927,7 +933,7 @@ class LoadBalancer {
         * Close a connection
         * Using this function makes sure the LoadBalancer knows the connection is closed.
         * If you use $conn->close() directly, the load balancer won't update its state.
-        * @param $conn DatabaseBase
+        * @param DatabaseBase $conn
         */
        function closeConnection( $conn ) {
                $done = false;
@@ -954,6 +960,7 @@ class LoadBalancer {
        function commitAll() {
                foreach ( $this->mConns as $conns2 ) {
                        foreach ( $conns2 as $conns3 ) {
+                               /** @var DatabaseBase[] $conns3 */
                                foreach ( $conns3 as $conn ) {
                                        if ( $conn->trxLevel() ) {
                                                $conn->commit( __METHOD__, 'flush' );
@@ -973,6 +980,7 @@ class LoadBalancer {
                        if ( empty( $conns2[$masterIndex] ) ) {
                                continue;
                        }
+                       /** @var DatabaseBase $conn */
                        foreach ( $conns2[$masterIndex] as $conn ) {
                                if ( $conn->trxLevel() && $conn->writesOrCallbacksPending() ) {
                                        $conn->commit( __METHOD__, 'flush' );
@@ -998,7 +1006,7 @@ class LoadBalancer {
 
        /**
         * Disables/enables lag checks
-        * @param $mode null
+        * @param null|bool $mode
         * @return bool
         */
        function allowLagged( $mode = null ) {
@@ -1017,6 +1025,7 @@ class LoadBalancer {
                $success = true;
                foreach ( $this->mConns as $conns2 ) {
                        foreach ( $conns2 as $conns3 ) {
+                               /** @var DatabaseBase[] $conns3 */
                                foreach ( $conns3 as $conn ) {
                                        if ( !$conn->ping() ) {
                                                $success = false;
@@ -1030,7 +1039,7 @@ class LoadBalancer {
 
        /**
         * Call a function with each open connection object
-        * @param $callback
+        * @param callable $callback
         * @param array $params
         */
        function forEachOpenConnection( $callback, $params = array() ) {
@@ -1050,8 +1059,7 @@ class LoadBalancer {
         * May attempt to open connections to slaves on the default DB. If there is
         * no lag, the maximum lag will be reported as -1.
         *
-        * @param string $wiki Wiki ID, or false for the default database
-        *
+        * @param bool|string $wiki Wiki ID, or false for the default database
         * @return array ( host, max lag, index of max lagged host )
         */
        function getMaxLag( $wiki = false ) {
@@ -1086,8 +1094,7 @@ class LoadBalancer {
         * Get lag time for each server
         * Results are cached for a short time in memcached, and indefinitely in the process cache
         *
-        * @param $wiki
-        *
+        * @param string|bool $wiki
         * @return array
         */
        function getLagTimes( $wiki = false ) {
@@ -1118,8 +1125,7 @@ class LoadBalancer {
         * function instead of Database::getLag() avoids a fatal error in this
         * case on many installations.
         *
-        * @param $conn DatabaseBase
-        *
+        * @param DatabaseBase $conn
         * @return int
         */
        function safeGetLag( $conn ) {
@@ -1148,14 +1154,16 @@ class LoadBalancer {
 class DBConnRef implements IDatabase {
        /** @var LoadBalancer */
        protected $lb;
+
        /** @var DatabaseBase|null */
        protected $conn;
-       /** @var Array|null */
+
+       /** @var array|null */
        protected $params;
 
        /**
-        * @param $lb LoadBalancer
-        * @param $conn DatabaseBase|array Connection or (server index, group, wiki ID) array
+        * @param LoadBalancer $lb
+        * @param DatabaseBase|array $conn Connection or (server index, group, wiki ID) array
         */
        public function __construct( LoadBalancer $lb, $conn ) {
                $this->lb = $lb;