From fdce245e9fe5da3da5d869561b4bbcf0232e9b5e Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Fri, 14 Oct 2016 02:17:25 -0700 Subject: [PATCH] objectcache: use a default "since" timestamp in getWithSetCallback() * Renamed mDoneWrites to be clearer at what type it is. * Also cleaned up a few callers of this method Change-Id: I45856b210c289c2e2f193cc4328a208e20b4e0a8 --- includes/Block.php | 6 ++++-- includes/Revision.php | 3 ++- includes/actions/InfoAction.php | 1 - includes/libs/objectcache/WANObjectCache.php | 14 +++++++++----- includes/libs/rdbms/database/Database.php | 12 ++++++------ 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/includes/Block.php b/includes/Block.php index 098d51c808..a11ba26484 100644 --- a/includes/Block.php +++ b/includes/Block.php @@ -692,11 +692,13 @@ class Block { public static function isWhitelistedFromAutoblocks( $ip ) { // Try to get the autoblock_whitelist from the cache, as it's faster // than getting the msg raw and explode()'ing it. - $cache = ObjectCache::getMainWANInstance(); + $cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); $lines = $cache->getWithSetCallback( wfMemcKey( 'ipb', 'autoblock', 'whitelist' ), $cache::TTL_DAY, - function () { + function ( $curValue, &$ttl, array &$setOpts ) { + $setOpts += Database::getCacheSetOptions( wfGetDB( DB_REPLICA ) ); + return explode( "\n", wfMessage( 'autoblock_whitelist' )->inContentLanguage()->plain() ); } diff --git a/includes/Revision.php b/includes/Revision.php index 208652fd22..8f337f955c 100644 --- a/includes/Revision.php +++ b/includes/Revision.php @@ -20,6 +20,7 @@ * @file */ use MediaWiki\Linker\LinkTarget; +use MediaWiki\MediaWikiServices; /** * @todo document @@ -1902,7 +1903,7 @@ class Revision implements IDBAccessObject { * @since 1.28 */ public static function newKnownCurrent( IDatabase $db, $pageId, $revId ) { - $cache = ObjectCache::getMainWANInstance(); + $cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); return $cache->getWithSetCallback( // Page/rev IDs passed in from DB to reflect history merges $cache->makeGlobalKey( 'revision', $db->getWikiID(), $pageId, $revId ), diff --git a/includes/actions/InfoAction.php b/includes/actions/InfoAction.php index 4d80a1cd33..c039388783 100644 --- a/includes/actions/InfoAction.php +++ b/includes/actions/InfoAction.php @@ -690,7 +690,6 @@ class InfoAction extends FormlessAction { $dbr = wfGetDB( DB_REPLICA ); $dbrWatchlist = wfGetDB( DB_REPLICA, 'watchlist' ); - $setOpts += Database::getCacheSetOptions( $dbr, $dbrWatchlist ); $watchedItemStore = MediaWikiServices::getInstance()->getWatchedItemStore(); diff --git a/includes/libs/objectcache/WANObjectCache.php b/includes/libs/objectcache/WANObjectCache.php index b0a3109acf..e7c4edbee6 100644 --- a/includes/libs/objectcache/WANObjectCache.php +++ b/includes/libs/objectcache/WANObjectCache.php @@ -939,12 +939,13 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { $cValue = $this->get( $key, $curTTL, $checkKeys, $asOf ); // current value $value = $cValue; // return value - // Determine if a regeneration is desired + $preCallbackTime = microtime( true ); + // Determine if a cached value regeneration is needed or desired if ( $value !== false && $curTTL > 0 && $this->isValid( $value, $versioned, $asOf, $minTime ) && !$this->worthRefreshExpiring( $curTTL, $lowTTL ) - && !$this->worthRefreshPopular( $asOf, $ageNew, $popWindow ) + && !$this->worthRefreshPopular( $asOf, $ageNew, $popWindow, $preCallbackTime ) ) { return $value; } @@ -1013,8 +1014,10 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { } if ( $value !== false && $ttl >= 0 ) { - // Update the cache; this will fail if the key is tombstoned $setOpts['lockTSE'] = $lockTSE; + // Use best known "since" timestamp if not provided + $setOpts += [ 'since' => $preCallbackTime ]; + // Update the cache; this will fail if the key is tombstoned $this->set( $key, $value, $ttl, $setOpts ); } @@ -1336,10 +1339,11 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface { * @param float $asOf UNIX timestamp of the value * @param integer $ageNew Age of key when this might recommend refreshing (seconds) * @param integer $timeTillRefresh Age of key when it should be refreshed if popular (seconds) + * @param float $now The current UNIX timestamp * @return bool */ - protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh ) { - $age = microtime( true ) - $asOf; + protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) { + $age = $now - $asOf; $timeOld = $age - $ageNew; if ( $timeOld <= 0 ) { return false; diff --git a/includes/libs/rdbms/database/Database.php b/includes/libs/rdbms/database/Database.php index fc55671d30..a5a170ba06 100644 --- a/includes/libs/rdbms/database/Database.php +++ b/includes/libs/rdbms/database/Database.php @@ -50,8 +50,8 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware /** @var string SQL query */ protected $mLastQuery = ''; - /** @var bool */ - protected $mDoneWrites = false; + /** @var float|bool UNIX timestamp of last write query */ + protected $mLastWriteTime = false; /** @var string|bool */ protected $mPHPError = false; /** @var string */ @@ -511,11 +511,11 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware } public function doneWrites() { - return (bool)$this->mDoneWrites; + return (bool)$this->mLastWriteTime; } public function lastDoneWrites() { - return $this->mDoneWrites ?: false; + return $this->mLastWriteTime ?: false; } public function writesPending() { @@ -820,7 +820,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware throw new DBReadOnlyError( $this, "Database is read-only: $reason" ); } # Set a flag indicating that writes have been done - $this->mDoneWrites = microtime( true ); + $this->mLastWriteTime = microtime( true ); } // Add trace comment to the begin of the sql string, right after the operator. @@ -2751,7 +2751,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware $writeTime = $this->pendingWriteQueryDuration( self::ESTIMATE_DB_APPLY ); $this->doCommit( $fname ); if ( $this->mTrxDoneWrites ) { - $this->mDoneWrites = microtime( true ); + $this->mLastWriteTime = microtime( true ); $this->trxProfiler->transactionWritingOut( $this->mServer, $this->mDBname, $this->mTrxShortId, $writeTime ); } -- 2.20.1