objectcache: use a default "since" timestamp in getWithSetCallback()
authorAaron Schulz <aschulz@wikimedia.org>
Fri, 14 Oct 2016 09:17:25 +0000 (02:17 -0700)
committerOri.livneh <ori@wikimedia.org>
Fri, 14 Oct 2016 23:31:09 +0000 (23:31 +0000)
* 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
includes/Revision.php
includes/actions/InfoAction.php
includes/libs/objectcache/WANObjectCache.php
includes/libs/rdbms/database/Database.php

index 098d51c..a11ba26 100644 (file)
@@ -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() );
                        }
index 208652f..8f337f9 100644 (file)
@@ -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 ),
index 4d80a1c..c039388 100644 (file)
@@ -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();
index b0a3109..e7c4edb 100644 (file)
@@ -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;
index fc55671..a5a170b 100644 (file)
@@ -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 );
                }