Make edit stashing use named DB locks
[lhc/web/wiklou.git] / includes / api / ApiStashEdit.php
index e87fc97..208e9ca 100644 (file)
@@ -19,6 +19,8 @@
  * @author Aaron Schulz
  */
 
+use MediaWiki\Logger\LoggerFactory;
+
 /**
  * Prepare an edit in shared cache so that it can be reused on edit
  *
@@ -39,7 +41,7 @@ class ApiStashEdit extends ApiBase {
        const ERROR_UNCACHEABLE = 'uncacheable';
 
        public function execute() {
-               global $wgMemc;
+               $cache = ObjectCache::getLocalClusterInstance();
 
                $user = $this->getUser();
                $params = $this->extractRequestParams();
@@ -106,18 +108,17 @@ class ApiStashEdit extends ApiBase {
                // The user will abort the AJAX request by pressing "save", so ignore that
                ignore_user_abort( true );
 
+               // Use the master DB for fast blocking locks
+               $dbw = wfGetDB( DB_MASTER );
+
                // Get a key based on the source text, format, and user preferences
                $key = self::getStashKey( $title, $content, $user );
                // De-duplicate requests on the same key
                if ( $user->pingLimiter( 'stashedit' ) ) {
                        $status = 'ratelimited';
-               } elseif ( $wgMemc->lock( $key, 0, 30 ) ) {
-                       /** @noinspection PhpUnusedLocalVariableInspection */
-                       $unlocker = new ScopedCallback( function() use ( $key ) {
-                               global $wgMemc;
-                               $wgMemc->unlock( $key );
-                       } );
+               } elseif ( $dbw->lock( $key, __METHOD__, 1 ) ) {
                        $status = self::parseAndStash( $page, $content, $user );
+                       $dbw->unlock( $key, __METHOD__ );
                } else {
                        $status = 'busy';
                }
@@ -133,7 +134,8 @@ class ApiStashEdit extends ApiBase {
         * @since 1.25
         */
        public static function parseAndStash( WikiPage $page, Content $content, User $user ) {
-               global $wgMemc;
+               $cache = ObjectCache::getLocalClusterInstance();
+               $logger = LoggerFactory::getInstance( 'StashEdit' );
 
                $format = $content->getDefaultFormat();
                $editInfo = $page->prepareContentForEdit( $content, null, $user, $format, false );
@@ -146,16 +148,16 @@ class ApiStashEdit extends ApiBase {
                        );
 
                        if ( $stashInfo ) {
-                               $ok = $wgMemc->set( $key, $stashInfo, $ttl );
+                               $ok = $cache->set( $key, $stashInfo, $ttl );
                                if ( $ok ) {
-                                       wfDebugLog( 'StashEdit', "Cached parser output for key '$key'." );
+                                       $logger->debug( "Cached parser output for key '$key'." );
                                        return self::ERROR_NONE;
                                } else {
-                                       wfDebugLog( 'StashEdit', "Failed to cache parser output for key '$key'." );
+                                       $logger->error( "Failed to cache parser output for key '$key'." );
                                        return self::ERROR_CACHE;
                                }
                        } else {
-                               wfDebugLog( 'StashEdit', "Uncacheable parser output for key '$key'." );
+                               $logger->info( "Uncacheable parser output for key '$key'." );
                                return self::ERROR_UNCACHEABLE;
                        }
                }
@@ -173,7 +175,7 @@ class ApiStashEdit extends ApiBase {
         * will do nothing. Provided the values are cacheable, they will be stored
         * in memcached so that final edit submission might make use of them.
         *
-        * @param Article|WikiPage $page Page title
+        * @param Page|Article|WikiPage $page Page title
         * @param Content $content Proposed page content
         * @param Content $pstContent The result of preSaveTransform() on $content
         * @param ParserOutput $pOut The result of getParserOutput() on $pstContent
@@ -186,7 +188,8 @@ class ApiStashEdit extends ApiBase {
                Page $page, Content $content, Content $pstContent, ParserOutput $pOut,
                ParserOptions $pstOpts, ParserOptions $pOpts, $timestamp
        ) {
-               global $wgMemc;
+               $cache = ObjectCache::getLocalClusterInstance();
+               $logger = LoggerFactory::getInstance( 'StashEdit' );
 
                // getIsPreview() controls parser function behavior that references things
                // like user/revision that don't exists yet. The user/text should already
@@ -208,22 +211,22 @@ class ApiStashEdit extends ApiBase {
                $canonicalPOpts->setIsPreview( true ); // force match
                $canonicalPOpts->setTimestamp( $pOpts->getTimestamp() ); // force match
                if ( !$pOpts->matches( $canonicalPOpts ) ) {
-                       wfDebugLog( 'StashEdit', "Uncacheable preview output for key '$key' (options)." );
+                       $logger->info( "Uncacheable preview output for key '$key' (options)." );
                        return false;
                }
 
                // Build a value to cache with a proper TTL
                list( $stashInfo, $ttl ) = self::buildStashValue( $pstContent, $pOut, $timestamp );
                if ( !$stashInfo ) {
-                       wfDebugLog( 'StashEdit', "Uncacheable parser output for key '$key' (rev/TTL)." );
+                       $logger->info( "Uncacheable parser output for key '$key' (rev/TTL)." );
                        return false;
                }
 
-               $ok = $wgMemc->set( $key, $stashInfo, $ttl );
+               $ok = $cache->set( $key, $stashInfo, $ttl );
                if ( !$ok ) {
-                       wfDebugLog( 'StashEdit', "Failed to cache preview parser output for key '$key'." );
+                       $logger->error( "Failed to cache preview parser output for key '$key'." );
                } else {
-                       wfDebugLog( 'StashEdit', "Cached preview output for key '$key'." );
+                       $logger->debug( "Cached preview output for key '$key'." );
                }
 
                return $ok;
@@ -247,32 +250,37 @@ class ApiStashEdit extends ApiBase {
         * @return stdClass|bool Returns false on cache miss
         */
        public static function checkCache( Title $title, Content $content, User $user ) {
-               global $wgMemc;
+               $cache = ObjectCache::getLocalClusterInstance();
+               $logger = LoggerFactory::getInstance( 'StashEdit' );
 
                $key = self::getStashKey( $title, $content, $user );
-               $editInfo = $wgMemc->get( $key );
+               $editInfo = $cache->get( $key );
                if ( !is_object( $editInfo ) ) {
                        $start = microtime( true );
                        // We ignore user aborts and keep parsing. Block on any prior parsing
-                       // so as to use it's results and make use of the time spent parsing.
-                       if ( $wgMemc->lock( $key, 30, 30 ) ) {
-                               $editInfo = $wgMemc->get( $key );
-                               $wgMemc->unlock( $key );
+                       // so as to use its results and make use of the time spent parsing.
+                       // Skip this logic if there no master connection in case this method
+                       // is called on an HTTP GET request for some reason.
+                       $lb = wfGetLB();
+                       $dbw = $lb->getAnyOpenConnection( $lb->getWriterIndex() );
+                       if ( $dbw && $dbw->lock( $key, __METHOD__, 30 ) ) {
+                               $editInfo = $cache->get( $key );
+                               $dbw->unlock( $key, __METHOD__ );
                        }
                        $sec = microtime( true ) - $start;
                        if ( $sec > .01 ) {
-                               wfDebugLog( 'StashEdit', "Waited $sec seconds on '$key'." );
+                               $logger->warning( "Waited $sec seconds on '$key'." );
                        }
                }
 
                if ( !is_object( $editInfo ) || !$editInfo->output ) {
-                       wfDebugLog( 'StashEdit', "No cache value for key '$key'." );
+                       $logger->debug( "No cache value for key '$key'." );
                        return false;
                }
 
                $time = wfTimestamp( TS_UNIX, $editInfo->output->getTimestamp() );
                if ( ( time() - $time ) <= 3 ) {
-                       wfDebugLog( 'StashEdit', "Timestamp-based cache hit for key '$key'." );
+                       $logger->debug( "Timestamp-based cache hit for key '$key'." );
                        return $editInfo; // assume nothing changed
                }
 
@@ -300,7 +308,7 @@ class ApiStashEdit extends ApiBase {
                        }
 
                        if ( $changed || $res->numRows() != $templateUses ) {
-                               wfDebugLog( 'StashEdit', "Stale cache for key '$key'; template changed." );
+                               $logger->info( "Stale cache for key '$key'; template changed." );
                                return false;
                        }
                }
@@ -323,12 +331,12 @@ class ApiStashEdit extends ApiBase {
                        }
 
                        if ( $changed || $res->numRows() != count( $files ) ) {
-                               wfDebugLog( 'StashEdit', "Stale cache for key '$key'; file changed." );
+                               $logger->info( "Stale cache for key '$key'; file changed." );
                                return false;
                        }
                }
 
-               wfDebugLog( 'StashEdit', "Cache hit for key '$key'." );
+               $logger->debug( "Cache hit for key '$key'." );
 
                return $editInfo;
        }
@@ -426,6 +434,10 @@ class ApiStashEdit extends ApiBase {
                return true;
        }
 
+       function isWriteMode() {
+               return true;
+       }
+
        function isInternal() {
                return true;
        }