Merge "Don't check namespace in SpecialWantedtemplates"
[lhc/web/wiklou.git] / includes / db / LoadBalancer.php
index 2ea2487..52dca08 100644 (file)
@@ -60,8 +60,6 @@ class LoadBalancer {
        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;
@@ -113,8 +111,6 @@ class LoadBalancer {
                                }
                        }
                }
-
-               $this->mProcCache = new ProcessCacheLRU( 30 );
        }
 
        /**
@@ -291,8 +287,8 @@ class LoadBalancer {
                                return false;
                        }
 
-                       wfDebugLog( 'connect', __METHOD__ .
-                               ": Using reader #$i: {$this->mServers[$i]['host']}..." );
+                       $serverName = $this->getServerName( $i );
+                       wfDebugLog( 'connect', __METHOD__ . ": Using reader #$i: $serverName..." );
 
                        $conn = $this->openConnection( $i, $wiki );
                        if ( !$conn ) {
@@ -462,7 +458,7 @@ class LoadBalancer {
 
                if ( $result == -1 || is_null( $result ) ) {
                        # Timed out waiting for slave, use master instead
-                       $server = $this->mServers[$index]['host'];
+                       $server = $server = $this->getServerName( $index );
                        $msg = __METHOD__ . ": Timed out waiting on $server pos {$this->mWaitForPos}";
                        wfDebug( "$msg\n" );
                        wfDebugLog( 'DBPerformance', "$msg:\n" . wfBacktrace( true ) );
@@ -538,7 +534,6 @@ class LoadBalancer {
                # Now we have an explicit index into the servers array
                $conn = $this->openConnection( $i, $wiki );
                if ( !$conn ) {
-
                        return $this->reportConnectionError();
                }
 
@@ -651,25 +646,32 @@ class LoadBalancer {
        public function openConnection( $i, $wiki = false ) {
                if ( $wiki !== false ) {
                        $conn = $this->openForeignConnection( $i, $wiki );
-
-                       return $conn;
-               }
-               if ( isset( $this->mConns['local'][$i][0] ) ) {
+               } elseif ( isset( $this->mConns['local'][$i][0] ) ) {
                        $conn = $this->mConns['local'][$i][0];
                } else {
                        $server = $this->mServers[$i];
                        $server['serverIndex'] = $i;
                        $conn = $this->reallyOpenConnection( $server, false );
+                       $serverName = $this->getServerName( $i );
                        if ( $conn->isOpen() ) {
-                               wfDebug( "Connected to database $i at {$this->mServers[$i]['host']}\n" );
+                               wfDebug( "Connected to database $i at $serverName\n" );
                                $this->mConns['local'][$i][0] = $conn;
                        } else {
-                               wfDebug( "Failed to connect to database $i at {$this->mServers[$i]['host']}\n" );
+                               wfDebug( "Failed to connect to database $i at $serverName\n" );
                                $this->mErrorConnection = $conn;
                                $conn = false;
                        }
                }
 
+               if ( $conn && !$conn->isOpen() ) {
+                       // Connection was made but later unrecoverably lost for some reason.
+                       // Do not return a handle that will just throw exceptions on use,
+                       // but let the calling code (e.g. getReaderIndex) try another server.
+                       // See DatabaseMyslBase::ping() for how this can happen.
+                       $this->mErrorConnection = $conn;
+                       $conn = false;
+               }
+
                return $conn;
        }
 
@@ -888,12 +890,14 @@ class LoadBalancer {
         */
        public function getServerName( $i ) {
                if ( isset( $this->mServers[$i]['hostName'] ) ) {
-                       return $this->mServers[$i]['hostName'];
+                       $name = $this->mServers[$i]['hostName'];
                } elseif ( isset( $this->mServers[$i]['host'] ) ) {
-                       return $this->mServers[$i]['host'];
+                       $name = $this->mServers[$i]['host'];
                } else {
-                       return '';
+                       $name = '';
                }
+
+               return ( $name != '' ) ? $name : 'localhost';
        }
 
        /**
@@ -1232,16 +1236,8 @@ class LoadBalancer {
                        return array( 0 => 0 ); // no replication = no lag
                }
 
-               if ( $this->mProcCache->has( 'slave_lag', 'times', 1 ) ) {
-                       return $this->mProcCache->get( 'slave_lag', 'times' );
-               }
-
                # Send the request to the load monitor
-               $times = $this->getLoadMonitor()->getLagTimes( array_keys( $this->mServers ), $wiki );
-
-               $this->mProcCache->set( 'slave_lag', 'times', $times );
-
-               return $times;
+               return $this->getLoadMonitor()->getLagTimes( array_keys( $this->mServers ), $wiki );
        }
 
        /**
@@ -1268,8 +1264,10 @@ class LoadBalancer {
 
        /**
         * Clear the cache for slag lag delay times
+        *
+        * This is only used for testing
         */
        public function clearLagTimeCache() {
-               $this->mProcCache->clear( 'slave_lag' );
+               $this->getLoadMonitor()->clearCaches();
        }
 }