From 9c919dd2cfab267c81519426b567ada6f64377dc Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Thu, 10 Dec 2015 13:56:11 -0800 Subject: [PATCH] Make edit stashing use named DB locks These should be faster by avoiding the inefficient back-off polling in BagOStuff::lock(). Change-Id: I013b2567716eea5e354e13f3e9640573886e596a --- includes/api/ApiStashEdit.php | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/includes/api/ApiStashEdit.php b/includes/api/ApiStashEdit.php index c6d267cc2e..208e9ca880 100644 --- a/includes/api/ApiStashEdit.php +++ b/includes/api/ApiStashEdit.php @@ -108,17 +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 ( $cache->lock( $key, 0, 30 ) ) { - /** @noinspection PhpUnusedLocalVariableInspection */ - $unlocker = new ScopedCallback( function() use ( $cache, $key ) { - $cache->unlock( $key ); - } ); + } elseif ( $dbw->lock( $key, __METHOD__, 1 ) ) { $status = self::parseAndStash( $page, $content, $user ); + $dbw->unlock( $key, __METHOD__ ); } else { $status = 'busy'; } @@ -258,10 +258,14 @@ class ApiStashEdit extends ApiBase { 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 ( $cache->lock( $key, 30, 30 ) ) { + // 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 ); - $cache->unlock( $key ); + $dbw->unlock( $key, __METHOD__ ); } $sec = microtime( true ) - $start; if ( $sec > .01 ) { @@ -430,6 +434,10 @@ class ApiStashEdit extends ApiBase { return true; } + function isWriteMode() { + return true; + } + function isInternal() { return true; } -- 2.20.1