Convert Special:DeletedContributions to use OOUI.
[lhc/web/wiklou.git] / includes / db / loadbalancer / LoadMonitorMySQL.php
index f49e965..444c4b4 100644 (file)
@@ -36,7 +36,7 @@ class LoadMonitorMySQL implements LoadMonitor {
        public function __construct( $parent ) {
                $this->parent = $parent;
 
-               $this->srvCache = ObjectCache::newAccelerator( 'hash' );
+               $this->srvCache = ObjectCache::getLocalServerInstance( 'hash' );
                $this->mainCache = ObjectCache::getLocalClusterInstance();
        }
 
@@ -46,7 +46,7 @@ class LoadMonitorMySQL implements LoadMonitor {
        public function getLagTimes( $serverIndexes, $wiki ) {
                if ( count( $serverIndexes ) == 1 && reset( $serverIndexes ) == 0 ) {
                        # Single server only, just return zero without caching
-                       return array( 0 => 0 );
+                       return [ 0 => 0 ];
                }
 
                $key = $this->getLagTimeCacheKey();
@@ -58,7 +58,7 @@ class LoadMonitorMySQL implements LoadMonitor {
                # (a) Check the local APC cache
                $value = $this->srvCache->get( $key );
                if ( $value && $value['timestamp'] > ( microtime( true ) - $ttl ) ) {
-                       wfDebugLog( 'replication', __FUNCTION__ . ": got lag times ($key) from local cache" );
+                       wfDebugLog( 'replication', __METHOD__ . ": got lag times ($key) from local cache" );
                        return $value['lagTimes']; // cache hit
                }
                $staleValue = $value ?: false;
@@ -67,7 +67,7 @@ class LoadMonitorMySQL implements LoadMonitor {
                $value = $this->mainCache->get( $key );
                if ( $value && $value['timestamp'] > ( microtime( true ) - $ttl ) ) {
                        $this->srvCache->set( $key, $value, $staleTTL );
-                       wfDebugLog( 'replication', __FUNCTION__ . ": got lag times ($key) from main cache" );
+                       wfDebugLog( 'replication', __METHOD__ . ": got lag times ($key) from main cache" );
 
                        return $value['lagTimes']; // cache hit
                }
@@ -86,14 +86,35 @@ class LoadMonitorMySQL implements LoadMonitor {
                        return $staleValue['lagTimes'];
                }
 
-               $lagTimes = array();
+               $lagTimes = [];
                foreach ( $serverIndexes as $i ) {
-                       if ( $i == 0 ) { # Master
-                               $lagTimes[$i] = 0;
-                       } elseif ( false !== ( $conn = $this->parent->getAnyOpenConnection( $i ) ) ) {
-                               $lagTimes[$i] = $conn->getLag();
-                       } elseif ( false !== ( $conn = $this->parent->openConnection( $i, $wiki ) ) ) {
-                               $lagTimes[$i] = $conn->getLag();
+                       if ( $i == $this->parent->getWriterIndex() ) {
+                               $lagTimes[$i] = 0; // master always has no lag
+                               continue;
+                       }
+
+                       $conn = $this->parent->getAnyOpenConnection( $i );
+                       if ( $conn ) {
+                               $close = false; // already open
+                       } else {
+                               $conn = $this->parent->openConnection( $i, $wiki );
+                               $close = true; // new connection
+                       }
+
+                       if ( !$conn ) {
+                               $lagTimes[$i] = false;
+                               $host = $this->parent->getServerName( $i );
+                               wfDebugLog( 'replication', __METHOD__ . ": host $host (#$i) is unreachable" );
+                               continue;
+                       }
+
+                       $lagTimes[$i] = $conn->getLag();
+                       if ( $lagTimes[$i] === false ) {
+                               $host = $this->parent->getServerName( $i );
+                               wfDebugLog( 'replication', __METHOD__ . ": host $host (#$i) is not replicating?" );
+                       }
+
+                       if ( $close ) {
                                # Close the connection to avoid sleeper connections piling up.
                                # Note that the caller will pick one of these DBs and reconnect,
                                # which is slightly inefficient, but this only matters for the lag
@@ -103,10 +124,10 @@ class LoadMonitorMySQL implements LoadMonitor {
                }
 
                # Add a timestamp key so we know when it was cached
-               $value = array( 'lagTimes' => $lagTimes, 'timestamp' => microtime( true ) );
+               $value = [ 'lagTimes' => $lagTimes, 'timestamp' => microtime( true ) ];
                $this->mainCache->set( $key, $value, $staleTTL );
                $this->srvCache->set( $key, $value, $staleTTL );
-               wfDebugLog( 'replication', __FUNCTION__ . ": re-calculated lag times ($key)" );
+               wfDebugLog( 'replication', __METHOD__ . ": re-calculated lag times ($key)" );
 
                return $value['lagTimes'];
        }
@@ -118,7 +139,10 @@ class LoadMonitorMySQL implements LoadMonitor {
        }
 
        private function getLagTimeCacheKey() {
-               # Lag is per-server, not per-DB, so key on the master DB name
-               return wfGlobalCacheKey( 'lag-times', $this->parent->getServerName( 0 ) );
+               $writerIndex = $this->parent->getWriterIndex();
+               // Lag is per-server, not per-DB, so key on the master DB name
+               return $this->srvCache->makeGlobalKey(
+                       'lag-times', $this->parent->getServerName( $writerIndex )
+               );
        }
 }