Merge "Avoid some unused parameter IDEA errors in User.php"
[lhc/web/wiklou.git] / includes / cache / MessageCache.php
index 9aac37a..f22c860 100644 (file)
  */
 define( 'MSG_CACHE_VERSION', 2 );
 
-/**
- * Memcached timeout when loading a key.
- * See MessageCache::load()
- */
-define( 'MSG_LOAD_TIMEOUT', 60 );
-
 /**
  * Message cache
  * Performs various MediaWiki namespace-related functions
@@ -41,9 +35,9 @@ define( 'MSG_LOAD_TIMEOUT', 60 );
 class MessageCache {
        const FOR_UPDATE = 1; // force message reload
 
-       /** How long memcached locks last */
-       const WAIT_SEC = 30;
        /** How long to wait for memcached locks */
+       const WAIT_SEC = 15;
+       /** How long memcached locks last */
        const LOCK_TTL = 30;
 
        /**
@@ -190,6 +184,7 @@ class MessageCache {
         */
        protected function getLocalCache( $code ) {
                $cacheKey = wfMemcKey( __CLASS__, $code );
+
                return $this->localCache->get( $cacheKey );
        }
 
@@ -226,8 +221,6 @@ class MessageCache {
         * @return bool
         */
        function load( $code = false, $mode = null ) {
-               global $wgUseLocalMessageCache;
-
                if ( !is_string( $code ) ) {
                        # This isn't really nice, so at least make a note about it and try to
                        # fall back
@@ -260,25 +253,23 @@ class MessageCache {
                # or local cache goes out of date (e.g. due to replace() on some other server)
                list( $hash, $hashVolatile ) = $this->getValidationHash( $code );
 
-               if ( $wgUseLocalMessageCache && $hash ) {
-                       # Try the local cache and check against the cluster hash key...
-                       $cache = $this->getLocalCache( $code );
-                       if ( !$cache ) {
-                               $where[] = 'local cache is empty';
-                       } elseif ( !isset( $cache['HASH'] ) || $cache['HASH'] !== $hash ) {
-                               $where[] = 'local cache has the wrong hash';
-                               $staleCache = $cache;
-                       } elseif ( $this->isCacheExpired( $cache ) ) {
-                               $where[] = 'local cache is expired';
-                               $staleCache = $cache;
-                       } elseif ( $hashVolatile ) {
-                               $where[] = 'local cache validation key is expired/volatile';
-                               $staleCache = $cache;
-                       } else {
-                               $where[] = 'got from local cache';
-                               $success = true;
-                               $this->mCache[$code] = $cache;
-                       }
+               # Try the local cache and check against the cluster hash key...
+               $cache = $this->getLocalCache( $code );
+               if ( !$cache ) {
+                       $where[] = 'local cache is empty';
+               } elseif ( !isset( $cache['HASH'] ) || $cache['HASH'] !== $hash ) {
+                       $where[] = 'local cache has the wrong hash';
+                       $staleCache = $cache;
+               } elseif ( $this->isCacheExpired( $cache ) ) {
+                       $where[] = 'local cache is expired';
+                       $staleCache = $cache;
+               } elseif ( $hashVolatile ) {
+                       $where[] = 'local cache validation key is expired/volatile';
+                       $staleCache = $cache;
+               } else {
+                       $where[] = 'got from local cache';
+                       $success = true;
+                       $this->mCache[$code] = $cache;
                }
 
                if ( !$success ) {
@@ -286,7 +277,7 @@ class MessageCache {
                        # Try the global cache. If it is empty, try to acquire a lock. If
                        # the lock can't be acquired, wait for the other thread to finish
                        # and then try the global cache a second time.
-                       for ( $failedAttempts = 0; $failedAttempts < 2; $failedAttempts++ ) {
+                       for ( $failedAttempts = 0; $failedAttempts <= 1; $failedAttempts++ ) {
                                if ( $hashVolatile && $staleCache ) {
                                        # Do not bother fetching the whole cache blob to avoid I/O.
                                        # Instead, just try to get the non-blocking $statusKey lock
@@ -320,8 +311,9 @@ class MessageCache {
 
                                # We need to call loadFromDB. Limit the concurrency to one process.
                                # This prevents the site from going down when the cache expires.
-                               # Note that the slam-protection lock here is non-blocking.
-                               if ( $this->loadFromDBWithLock( $code, $where ) ) {
+                               # Note that the DB slam protection lock here is non-blocking.
+                               $loadStatus = $this->loadFromDBWithLock( $code, $where, $mode );
+                               if ( $loadStatus === true ) {
                                        $success = true;
                                        break;
                                } elseif ( $staleCache ) {
@@ -331,23 +323,19 @@ class MessageCache {
                                        $success = true;
                                        break;
                                } elseif ( $failedAttempts > 0 ) {
-                                       # Already retried once, still failed, so don't do another lock/unlock cycle
+                                       # Already blocked once, so avoid another lock/unlock cycle.
                                        # This case will typically be hit if memcached is down, or if
-                                       # loadFromDB() takes longer than MSG_WAIT_TIMEOUT
+                                       # loadFromDB() takes longer than LOCK_WAIT.
                                        $where[] = "could not acquire status key.";
                                        break;
+                               } elseif ( $loadStatus === 'cantacquire' ) {
+                                       # Wait for the other thread to finish, then retry. Normally,
+                                       # the memcached get() will then yeild the other thread's result.
+                                       $where[] = 'waited for other thread to complete';
+                                       $this->getReentrantScopedLock( $cacheKey );
                                } else {
-                                       $statusKey = wfMemcKey( 'messages', $code, 'status' );
-                                       $status = $this->mMemc->get( $statusKey );
-                                       if ( $status === 'error' ) {
-                                               # Disable cache
-                                               break;
-                                       } else {
-                                               # Wait for the other thread to finish, then retry. Normally,
-                                               # the memcached get() will then yeild the other thread's result.
-                                               $where[] = 'waited for other thread to complete';
-                                               $this->getReentrantScopedLock( $cacheKey );
-                                       }
+                                       # Disable cache; $loadStatus is 'disabled'
+                                       break;
                                }
                        }
                }
@@ -372,47 +360,39 @@ class MessageCache {
        /**
         * @param string $code
         * @param array $where List of wfDebug() comments
-        * @return bool Lock acquired and loadFromDB() called
+        * @param integer $mode Use MessageCache::FOR_UPDATE to use DB_MASTER
+        * @return bool|string True on success or one of ("cantacquire", "disabled")
         */
-       protected function loadFromDBWithLock( $code, array &$where ) {
+       protected function loadFromDBWithLock( $code, array &$where, $mode = null ) {
                global $wgUseLocalMessageCache;
 
-               $memCache = $this->mMemc;
-
-               # Get the non-blocking status key lock. This lets the caller quickly know
-               # to use any stale cache lying around. Otherwise, it may do a blocking
-               # lock to try to obtain the messages.
+               # If cache updates on all levels fail, give up on message overrides.
+               # This is to avoid easy site outages; see $saveSuccess comments below.
                $statusKey = wfMemcKey( 'messages', $code, 'status' );
-               if ( !$memCache->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT ) ) {
-                       return false; // could not acquire lock
+               $status = $this->mMemc->get( $statusKey );
+               if ( $status === 'error' ) {
+                       $where[] = "could not load; method is still globally disabled";
+                       return 'disabled';
                }
 
-               # Unlock the status key if there is an exception
-               $statusUnlocker = new ScopedCallback( function () use ( $memCache, $statusKey ) {
-                       $memCache->delete( $statusKey );
-               } );
-
                # Now let's regenerate
                $where[] = 'loading from database';
 
+               # Lock the cache to prevent conflicting writes.
+               # This lock is non-blocking so stale cache can quickly be used.
+               # Note that load() will call a blocking getReentrantScopedLock()
+               # after this if it really need to wait for any current thread.
                $cacheKey = wfMemcKey( 'messages', $code );
-               # Lock the cache to prevent conflicting writes
-               # If this lock fails, it doesn't really matter, it just means the
-               # write is potentially non-atomic, e.g. the results of a replace()
-               # may be discarded.
-               $mainUnlocker = $this->getReentrantScopedLock( $cacheKey );
-               if ( !$mainUnlocker ) {
+               $scopedLock = $this->getReentrantScopedLock( $cacheKey, 0 );
+               if ( !$scopedLock ) {
                        $where[] = 'could not acquire main lock';
+                       return 'cantacquire';
                }
 
-               $cache = $this->loadFromDB( $code );
+               $cache = $this->loadFromDB( $code, $mode );
                $this->mCache[$code] = $cache;
                $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
 
-               # Unlock
-               ScopedCallback::consume( $mainUnlocker );
-               ScopedCallback::consume( $statusUnlocker );
-
                if ( !$saveSuccess ) {
                        # Cache save has failed.
                        # There are two main scenarios where this could be a problem:
@@ -430,7 +410,7 @@ class MessageCache {
                        # overhead on every request, and thus saves the wiki from
                        # complete downtime under moderate traffic conditions.
                        if ( !$wgUseLocalMessageCache ) {
-                               $memCache->set( $statusKey, 'error', 60 * 5 );
+                               $this->mMemc->set( $statusKey, 'error', 60 * 5 );
                                $where[] = 'could not save cache, disabled globally for 5 minutes';
                        } else {
                                $where[] = "could not save global cache";
@@ -445,13 +425,15 @@ class MessageCache {
         * $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
         * on-demand from the database later.
         *
-        * @param string $code Language code.
-        * @return array Loaded messages for storing in caches.
+        * @param string $code Language code
+        * @param integer $mode Use MessageCache::FOR_UPDATE to skip process cache
+        * @return array Loaded messages for storing in caches
         */
-       function loadFromDB( $code ) {
+       function loadFromDB( $code, $mode = null ) {
                global $wgMaxMsgCacheEntrySize, $wgLanguageCode, $wgAdaptiveMessageCache;
 
-               $dbr = wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( ( $mode == self::FOR_UPDATE ) ? DB_MASTER : DB_SLAVE );
+
                $cache = array();
 
                # Common conditions
@@ -532,7 +514,7 @@ class MessageCache {
        /**
         * Updates cache as necessary when message page is changed
         *
-        * @param string $title Name of the page changed.
+        * @param string|bool $title Name of the page changed (false if deleted)
         * @param mixed $text New contents of the page.
         */
        public function replace( $title, $text ) {
@@ -544,37 +526,44 @@ class MessageCache {
 
                list( $msg, $code ) = $this->figureMessage( $title );
                if ( strpos( $title, '/' ) !== false && $code === $wgLanguageCode ) {
-                       # Content language overrides do not use the /<code> suffix
+                       // Content language overrides do not use the /<code> suffix
                        return;
                }
 
-               # Note that if the cache is volatile, load() may trigger a DB fetch.
-               # In that case we reenter/reuse the existing cache key lock to avoid
-               # a self-deadlock. This is safe as no reads happen *directly* in this
-               # method between getReentrantScopedLock() and load() below. There is
-               # no risk of data "changing under our feet" for replace().
+               // Note that if the cache is volatile, load() may trigger a DB fetch.
+               // In that case we reenter/reuse the existing cache key lock to avoid
+               // a self-deadlock. This is safe as no reads happen *directly* in this
+               // method between getReentrantScopedLock() and load() below. There is
+               // no risk of data "changing under our feet" for replace().
                $cacheKey = wfMemcKey( 'messages', $code );
                $scopedLock = $this->getReentrantScopedLock( $cacheKey );
                $this->load( $code, self::FOR_UPDATE );
 
                $titleKey = wfMemcKey( 'messages', 'individual', $title );
-
                if ( $text === false ) {
-                       # Article was deleted
+                       // Article was deleted
                        $this->mCache[$code][$title] = '!NONEXISTENT';
-                       $this->mMemc->delete( $titleKey );
+                       $this->wanCache->delete( $titleKey );
                } elseif ( strlen( $text ) > $wgMaxMsgCacheEntrySize ) {
-                       # Check for size
+                       // Check for size
                        $this->mCache[$code][$title] = '!TOO BIG';
-                       $this->mMemc->set( $titleKey, ' ' . $text, $this->mExpiry );
+                       $this->wanCache->set( $titleKey, ' ' . $text, $this->mExpiry );
                } else {
                        $this->mCache[$code][$title] = ' ' . $text;
-                       $this->mMemc->delete( $titleKey );
+                       $this->wanCache->delete( $titleKey );
+               }
+
+               // Mark this cache as definitely "latest" (non-volatile) so
+               // load() calls do try to refresh the cache with slave data
+               $this->mCache[$code]['LATEST'] = time();
+
+               // Update caches if the lock was acquired
+               if ( $scopedLock ) {
+                       $this->saveToCaches( $this->mCache[$code], 'all', $code );
                }
 
-               # Update caches
-               $this->saveToCaches( $this->mCache[$code], 'all', $code );
                ScopedCallback::consume( $scopedLock );
+               // Relay the purge to APC and other DCs
                $this->wanCache->touchCheckKey( wfMemcKey( 'messages', $code ) );
 
                // Also delete cached sidebar... just in case it is affected
@@ -626,9 +615,7 @@ class MessageCache {
         * @param string|bool $code Language code (default: false)
         * @return bool
         */
-       protected function saveToCaches( $cache, $dest, $code = false ) {
-               global $wgUseLocalMessageCache;
-
+       protected function saveToCaches( array $cache, $dest, $code = false ) {
                if ( $dest === 'all' ) {
                        $cacheKey = wfMemcKey( 'messages', $code );
                        $success = $this->mMemc->set( $cacheKey, $cache );
@@ -636,18 +623,14 @@ class MessageCache {
                        $success = true;
                }
 
-               $this->setValidationHash( $code, $cache['HASH'] );
-
-               # Save to local cache
-               if ( $wgUseLocalMessageCache ) {
-                       $this->saveToLocalCache( $code, $cache );
-               }
+               $this->setValidationHash( $code, $cache );
+               $this->saveToLocalCache( $code, $cache );
 
                return $success;
        }
 
        /**
-        * Get the md5 used to validate the local disk cache
+        * Get the md5 used to validate the local APC cache
         *
         * @param string $code
         * @return array (hash or false, bool expiry/volatility status)
@@ -655,35 +638,56 @@ class MessageCache {
        protected function getValidationHash( $code ) {
                $curTTL = null;
                $value = $this->wanCache->get(
-                       wfMemcKey( 'messages', $code, 'hash' ),
+                       wfMemcKey( 'messages', $code, 'hash', 'v1' ),
                        $curTTL,
                        array( wfMemcKey( 'messages', $code ) )
                );
-               $expired = ( $curTTL === null || $curTTL < 0 );
 
-               return array( $value, $expired );
+               if ( !$value ) {
+                       // No hash found at all; cache must regenerate to be safe
+                       $hash = false;
+                       $expired = true;
+               } else {
+                       $hash = $value['hash'];
+                       if ( ( time() - $value['latest'] ) < WANObjectCache::HOLDOFF_TTL ) {
+                               // Cache was recently updated via replace() and should be up-to-date
+                               $expired = false;
+                       } else {
+                               // See if the "check" key was bumped after the hash was generated
+                               $expired = ( $curTTL < 0 );
+                       }
+               }
+
+               return array( $hash, $expired );
        }
 
        /**
         * Set the md5 used to validate the local disk cache
         *
+        * If $cache has a 'LATEST' UNIX timestamp key, then the hash will not
+        * be treated as "volatile" by getValidationHash() for the next few seconds
+        *
         * @param string $code
-        * @param string $hash
+        * @param array $cache Cached messages with a version
         */
-       protected function setValidationHash( $code, $hash ) {
+       protected function setValidationHash( $code, array $cache ) {
                $this->wanCache->set(
-                       wfMemcKey( 'messages', $code, 'hash' ),
-                       $hash,
+                       wfMemcKey( 'messages', $code, 'hash', 'v1' ),
+                       array(
+                               'hash' => $cache['HASH'],
+                               'latest' => isset( $cache['LATEST'] ) ? $cache['LATEST'] : 0
+                       ),
                        WANObjectCache::TTL_NONE
                );
        }
 
        /**
         * @param string $key A language message cache key that stores blobs
+        * @param integer $timeout Wait timeout in seconds
         * @return null|ScopedCallback
         */
-       protected function getReentrantScopedLock( $key ) {
-               return $this->mMemc->getScopedLock( $key, self::WAIT_SEC, self::LOCK_TTL, __METHOD__ );
+       protected function getReentrantScopedLock( $key, $timeout = self::WAIT_SEC ) {
+               return $this->mMemc->getScopedLock( $key, $timeout, self::LOCK_TTL, __METHOD__ );
        }
 
        /**
@@ -927,7 +931,7 @@ class MessageCache {
 
                # Try the individual message cache
                $titleKey = wfMemcKey( 'messages', 'individual', $title );
-               $entry = $this->mMemc->get( $titleKey );
+               $entry = $this->wanCache->get( $titleKey );
                if ( $entry ) {
                        if ( substr( $entry, 0, 1 ) === ' ' ) {
                                $this->mCache[$code][$title] = $entry;
@@ -941,14 +945,12 @@ class MessageCache {
                                return false;
                        } else {
                                # Corrupt/obsolete entry, delete it
-                               $this->mMemc->delete( $titleKey );
+                               $this->wanCache->delete( $titleKey );
                        }
                }
 
                # Try loading it from the database
-               $revision = Revision::newFromTitle(
-                       Title::makeTitle( NS_MEDIAWIKI, $title ), false, Revision::READ_LATEST
-               );
+               $revision = Revision::newFromTitle( Title::makeTitle( NS_MEDIAWIKI, $title ) );
                if ( $revision ) {
                        $content = $revision->getContent();
                        if ( !$content ) {
@@ -969,13 +971,13 @@ class MessageCache {
                                        wfDebugLog(
                                                'MessageCache',
                                                __METHOD__ . ": message content doesn't provide wikitext "
-                                                       . "(content model: " . $content->getContentHandler() . ")"
+                                                       . "(content model: " . $content->getModel() . ")"
                                        );
 
                                        $message = false; // negative caching
                                } else {
                                        $this->mCache[$code][$title] = ' ' . $message;
-                                       $this->mMemc->set( $titleKey, ' ' . $message, $this->mExpiry );
+                                       $this->wanCache->set( $titleKey, ' ' . $message, $this->mExpiry );
                                }
                        }
                } else {
@@ -984,7 +986,7 @@ class MessageCache {
 
                if ( $message === false ) { // negative caching
                        $this->mCache[$code][$title] = '!NONEXISTENT';
-                       $this->mMemc->set( $titleKey, '!NONEXISTENT', $this->mExpiry );
+                       $this->wanCache->set( $titleKey, '!NONEXISTENT', $this->mExpiry );
                }
 
                return $message;