Merge "Removed old HTMLCacheUpdateJob b/c code"
[lhc/web/wiklou.git] / includes / db / LoadBalancer.php
index 07645bd..917d703 100644 (file)
@@ -58,9 +58,14 @@ class LoadBalancer {
        private $mLaggedSlaveMode;
        /** @var string The last DB selection or connection error */
        private $mLastError = 'Unknown error';
+       /** @var integer Total connections opened */
+       private $connsOpened = 0;
        /** @var ProcessCacheLRU */
        private $mProcCache;
 
+       /** @var integer Warn when this many connection are held */
+       const CONN_HELD_WARN_THRESHOLD = 10;
+
        /**
         * @param array $params Array with keys:
         *   servers           Required. Array of server info structures.
@@ -151,17 +156,23 @@ class LoadBalancer {
        /**
         * @param array $loads
         * @param bool|string $wiki Wiki to get non-lagged for
+        * @param float $maxLag Restrict the maximum allowed lag to this many seconds
         * @return bool|int|string
         */
-       private function getRandomNonLagged( array $loads, $wiki = false ) {
-               # Unset excessively lagged servers
+       private function getRandomNonLagged( array $loads, $wiki = false, $maxLag = INF ) {
                $lags = $this->getLagTimes( $wiki );
+
+               # Unset excessively lagged servers
                foreach ( $lags as $i => $lag ) {
                        if ( $i != 0 ) {
+                               $maxServerLag = $maxLag;
+                               if ( isset( $this->mServers[$i]['max lag'] ) ) {
+                                       $maxServerLag = min( $maxServerLag, $this->mServers[$i]['max lag'] );
+                               }
                                if ( $lag === false ) {
                                        wfDebugLog( 'replication', "Server #$i is not replicating" );
                                        unset( $loads[$i] );
-                               } elseif ( isset( $this->mServers[$i]['max lag'] ) && $lag > $this->mServers[$i]['max lag'] ) {
+                               } elseif ( $lag > $maxServerLag ) {
                                        wfDebugLog( 'replication', "Server #$i is excessively lagged ($lag seconds)" );
                                        unset( $loads[$i] );
                                }
@@ -218,8 +229,6 @@ class LoadBalancer {
                        return $this->mReadIndex;
                }
 
-               new ProfileSection( __METHOD__ );
-
                # Find the relevant load array
                if ( $group !== false ) {
                        if ( isset( $this->mGroupLoads[$group] ) ) {
@@ -252,7 +261,19 @@ class LoadBalancer {
                        if ( $wgReadOnly || $this->mAllowLagged || $laggedSlaveMode ) {
                                $i = ArrayUtils::pickRandom( $currentLoads );
                        } else {
-                               $i = $this->getRandomNonLagged( $currentLoads, $wiki );
+                               $i = false;
+                               if ( $this->mWaitForPos && $this->mWaitForPos->asOfTime() ) {
+                                       # ChronologyProtecter causes mWaitForPos to be set via sessions.
+                                       # This triggers doWait() after connect, so it's especially good to
+                                       # avoid lagged servers so as to avoid just blocking in that method.
+                                       $ago = microtime( true ) - $this->mWaitForPos->asOfTime();
+                                       # Aim for <= 1 second of waiting (being too picky can backfire)
+                                       $i = $this->getRandomNonLagged( $currentLoads, $wiki, $ago + 1 );
+                               }
+                               if ( $i === false ) {
+                                       # Any server with less lag than it's 'max lag' param is preferable
+                                       $i = $this->getRandomNonLagged( $currentLoads, $wiki );
+                               }
                                if ( $i === false && count( $currentLoads ) != 0 ) {
                                        # All slaves lagged. Switch to read-only mode
                                        wfDebugLog( 'replication', "All slaves lagged. Switch to read-only mode" );
@@ -307,9 +328,11 @@ class LoadBalancer {
                                        $this->mServers[$i]['slave pos'] = $conn->getSlavePos();
                                }
                        }
-                       if ( $this->mReadIndex <= 0 && $this->mLoads[$i] > 0 && $group !== false ) {
+                       if ( $this->mReadIndex <= 0 && $this->mLoads[$i] > 0 && $group === false ) {
                                $this->mReadIndex = $i;
                        }
+                       $serverName = $this->getServerName( $i );
+                       wfDebug( __METHOD__ . ": using server $serverName for group '$group'\n" );
                }
 
                return $i;
@@ -322,7 +345,6 @@ class LoadBalancer {
         * @param DBMasterPos $pos
         */
        public function waitFor( $pos ) {
-               wfProfileIn( __METHOD__ );
                $this->mWaitForPos = $pos;
                $i = $this->mReadIndex;
 
@@ -332,7 +354,6 @@ class LoadBalancer {
                                $this->mLaggedSlaveMode = true;
                        }
                }
-               wfProfileOut( __METHOD__ );
        }
 
        /**
@@ -342,7 +363,6 @@ class LoadBalancer {
         * @return bool Success (able to connect and no timeouts reached)
         */
        public function waitForAll( $pos, $timeout = null ) {
-               wfProfileIn( __METHOD__ );
                $this->mWaitForPos = $pos;
                $serverCount = count( $this->mServers );
 
@@ -352,7 +372,6 @@ class LoadBalancer {
                                $ok = $this->doWait( $i, true, $timeout ) && $ok;
                        }
                }
-               wfProfileOut( __METHOD__ );
 
                return $ok;
        }
@@ -438,11 +457,8 @@ class LoadBalancer {
         * @throws MWException
         * @return DatabaseBase
         */
-       public function &getConnection( $i, $groups = array(), $wiki = false ) {
-               wfProfileIn( __METHOD__ );
-
+       public function getConnection( $i, $groups = array(), $wiki = false ) {
                if ( $i === null || $i === false ) {
-                       wfProfileOut( __METHOD__ );
                        throw new MWException( 'Attempt to call ' . __METHOD__ .
                                ' with invalid server index' );
                }
@@ -451,22 +467,17 @@ class LoadBalancer {
                        $wiki = false;
                }
 
-               # Query groups
+               $groups = ( $groups === false || $groups === array() )
+                       ? array( false ) // check one "group": the generic pool
+                       : (array)$groups;
+
                if ( $i == DB_MASTER ) {
                        $i = $this->getWriterIndex();
-               } elseif ( !is_array( $groups ) ) {
-                       $groupIndex = $this->getReaderIndex( $groups, $wiki );
-                       if ( $groupIndex !== false ) {
-                               $serverName = $this->getServerName( $groupIndex );
-                               wfDebug( __METHOD__ . ": using server $serverName for group $groups\n" );
-                               $i = $groupIndex;
-                       }
                } else {
+                       # Try to find an available server in any the query groups (in order)
                        foreach ( $groups as $group ) {
                                $groupIndex = $this->getReaderIndex( $group, $wiki );
                                if ( $groupIndex !== false ) {
-                                       $serverName = $this->getServerName( $groupIndex );
-                                       wfDebug( __METHOD__ . ": using server $serverName for group $group\n" );
                                        $i = $groupIndex;
                                        break;
                                }
@@ -476,11 +487,13 @@ class LoadBalancer {
                # Operation-based index
                if ( $i == DB_SLAVE ) {
                        $this->mLastError = 'Unknown error'; // reset error string
-                       $i = $this->getReaderIndex( false, $wiki );
+                       # Try the general server pool if $groups are unavailable.
+                       $i = in_array( false, $groups, true )
+                               ? false // don't bother with this if that is what was tried above
+                               : $this->getReaderIndex( false, $wiki );
                        # Couldn't find a working server in getReaderIndex()?
                        if ( $i === false ) {
                                $this->mLastError = 'No working slave server: ' . $this->mLastError;
-                               wfProfileOut( __METHOD__ );
 
                                return $this->reportConnectionError();
                        }
@@ -489,13 +502,10 @@ class LoadBalancer {
                # Now we have an explicit index into the servers array
                $conn = $this->openConnection( $i, $wiki );
                if ( !$conn ) {
-                       wfProfileOut( __METHOD__ );
 
                        return $this->reportConnectionError();
                }
 
-               wfProfileOut( __METHOD__ );
-
                return $conn;
        }
 
@@ -595,10 +605,8 @@ class LoadBalancer {
         * @access private
         */
        public function openConnection( $i, $wiki = false ) {
-               wfProfileIn( __METHOD__ );
                if ( $wiki !== false ) {
                        $conn = $this->openForeignConnection( $i, $wiki );
-                       wfProfileOut( __METHOD__ );
 
                        return $conn;
                }
@@ -617,7 +625,6 @@ class LoadBalancer {
                                $conn = false;
                        }
                }
-               wfProfileOut( __METHOD__ );
 
                return $conn;
        }
@@ -641,7 +648,6 @@ class LoadBalancer {
         * @return DatabaseBase
         */
        private function openForeignConnection( $i, $wiki ) {
-               wfProfileIn( __METHOD__ );
                list( $dbName, $prefix ) = wfSplitWikiID( $wiki );
                if ( isset( $this->mConns['foreignUsed'][$i][$wiki] ) ) {
                        // Reuse an already-used connection
@@ -694,7 +700,6 @@ class LoadBalancer {
                        $refCount = $conn->getLBInfo( 'foreignPoolRefCount' );
                        $conn->setLBInfo( 'foreignPoolRefCount', $refCount + 1 );
                }
-               wfProfileOut( __METHOD__ );
 
                return $conn;
        }
@@ -734,6 +739,14 @@ class LoadBalancer {
                        $server['dbname'] = $dbNameOverride;
                }
 
+               // Log when many connection are made on requests
+               if ( ++$this->connsOpened >= self::CONN_HELD_WARN_THRESHOLD ) {
+                       $masterAddr = $this->getServerName( 0 );
+                       wfDebugLog( 'DBPerformance', __METHOD__ . ": " .
+                               "{$this->connsOpened}+ connections made (master=$masterAddr)\n" .
+                               wfBacktrace( true ) );
+               }
+
                # Create object
                try {
                        $db = DatabaseBase::factory( $server['type'], $server );
@@ -905,6 +918,7 @@ class LoadBalancer {
                        'foreignFree' => array(),
                        'foreignUsed' => array(),
                );
+               $this->connsOpened = 0;
        }
 
        /**
@@ -921,6 +935,7 @@ class LoadBalancer {
                                        if ( $conn === $candidateConn ) {
                                                $conn->close();
                                                unset( $this->mConns[$i1][$i2][$i3] );
+                                               --$this->connsOpened;
                                                $done = true;
                                                break;
                                        }