Merge "Add WAN Cache to SiteStats::jobs"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Tue, 25 Jul 2017 18:30:58 +0000 (18:30 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 25 Jul 2017 18:30:58 +0000 (18:30 +0000)
1  2 
includes/SiteStats.php
tests/phpunit/MediaWikiTestCase.php

diff --combined includes/SiteStats.php
@@@ -34,9 -34,6 +34,6 @@@ class SiteStats 
        /** @var bool */
        private static $loaded = false;
  
-       /** @var int */
-       private static $jobs;
        /** @var int[] */
        private static $pageCount = [];
  
        }
  
        /**
+        * Total number of jobs in the job queue.
         * @return int
         */
        static function jobs() {
-               if ( !isset( self::$jobs ) ) {
-                       try{
-                               self::$jobs = array_sum( JobQueueGroup::singleton()->getQueueSizes() );
-                       } catch ( JobQueueError $e ) {
-                               self::$jobs = 0;
-                       }
-               }
-               return self::$jobs;
+               $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
+               return $cache->getWithSetCallback(
+                       $cache->makeKey( 'SiteStats', 'jobscount' ),
+                       $cache::TTL_MINUTE,
+                       function ( $oldValue, &$ttl, array &$setOpts ) {
+                               try{
+                                       $jobs = array_sum( JobQueueGroup::singleton()->getQueueSizes() );
+                               } catch ( JobQueueError $e ) {
+                                       $jobs = 0;
+                               }
+                               return $jobs;
+                       },
+                       [ 'pcTTL' => $cache::TTL_PROC_LONG ]
+               );
        }
  
        /**
@@@ -289,6 -293,7 +293,6 @@@ class SiteStatsInit 
        private $mUsers = null, $mFiles = null;
  
        /**
 -       * Constructor
         * @param bool|IDatabase $database
         * - boolean: Whether to use the master DB
         * - IDatabase: Database connection to use
@@@ -5,8 -5,6 +5,8 @@@ use MediaWiki\Logger\LoggerFactory
  use MediaWiki\Logger\MonologSpi;
  use MediaWiki\MediaWikiServices;
  use Psr\Log\LoggerInterface;
 +use Wikimedia\Rdbms\IMaintainableDatabase;
 +use Wikimedia\Rdbms\Database;
  use Wikimedia\TestingAccessWrapper;
  
  /**
@@@ -217,7 -215,6 +217,7 @@@ abstract class MediaWikiTestCase extend
        protected static function resetGlobalServices( Config $bootstrapConfig = null ) {
                $oldServices = MediaWikiServices::getInstance();
                $oldConfigFactory = $oldServices->getConfigFactory();
 +              $oldLoadBalancerFactory = $oldServices->getDBLoadBalancerFactory();
  
                $testConfig = self::makeTestConfig( $bootstrapConfig );
  
                $serviceLocator = MediaWikiServices::getInstance();
                self::installTestServices(
                        $oldConfigFactory,
 +                      $oldLoadBalancerFactory,
                        $serviceLocator
                );
                return $serviceLocator;
  
        /**
         * @param ConfigFactory $oldConfigFactory
 +       * @param LBFactory $oldLoadBalancerFactory
         * @param MediaWikiServices $newServices
         *
         * @throws MWException
         */
        private static function installTestServices(
                ConfigFactory $oldConfigFactory,
 +              LBFactory $oldLoadBalancerFactory,
                MediaWikiServices $newServices
        ) {
                // Use bootstrap config for all configuration.
                                [ 'main' =>  $bootstrapConfig ]
                        )
                );
 +              $newServices->resetServiceForTesting( 'DBLoadBalancerFactory' );
 +              $newServices->redefineService(
 +                      'DBLoadBalancerFactory',
 +                      function ( MediaWikiServices $services ) use ( $oldLoadBalancerFactory ) {
 +                              return $oldLoadBalancerFactory;
 +                      }
 +              );
        }
  
        /**
                ConfigFactory $oldFactory,
                array $configurations
        ) {
 -              return function( MediaWikiServices $services ) use ( $oldFactory, $configurations ) {
 +              return function ( MediaWikiServices $services ) use ( $oldFactory, $configurations ) {
                        $factory = new ConfigFactory();
  
                        // clone configurations from $oldFactory that are not overwritten by $configurations
         * The key is added to the array of globals that will be reset afterwards
         * in the tearDown().
         *
 -       * @example
 -       * <code>
 +       * @par Example
 +       * @code
         *     protected function setUp() {
         *         $this->setMwGlobals( 'wgRestrictStuff', true );
         *     }
         *     }
         *
         *     function testQuux() {}
 -       * </code>
 +       * @endcode
         *
         * @param array|string $pairs Key to the global variable, or an array
         *  of key/value pairs.
  
                $oldInstance = MediaWikiServices::getInstance();
                $oldConfigFactory = $oldInstance->getConfigFactory();
 +              $oldLoadBalancerFactory = $oldInstance->getDBLoadBalancerFactory();
  
                $testConfig = self::makeTestConfig( null, $configOverrides );
                $newInstance = new MediaWikiServices( $testConfig );
  
                self::installTestServices(
                        $oldConfigFactory,
 +                      $oldLoadBalancerFactory,
                        $newInstance
                );
                MediaWikiServices::forceGlobalInstance( $newInstance );
  
        private function addCoreDBData() {
                if ( $this->db->getType() == 'oracle' ) {
 -
                        # Insert 0 user to prevent FK violations
                        # Anonymous user
                        if ( !$this->db->selectField( 'user', '1', [ 'user_id' => 0 ] ) ) {
                        $page->doEditContent(
                                new WikitextContent( 'UTContent' ),
                                'UTPageSummary',
-                               EDIT_NEW,
+                               EDIT_NEW | EDIT_SUPPRESS_RC,
                                false,
                                $user
                        );
+                       // an edit always attempt to purge backlink links such as history
+                       // pages. That is unneccessary.
+                       JobQueueGroup::singleton()->get( 'htmlCacheUpdate' )->delete();
+                       // WikiPages::doEditUpdates randomly adds RC purges
+                       JobQueueGroup::singleton()->get( 'recentChangesUpdate' )->delete();
  
                        // doEditContent() probably started the session via
                        // User::loadFromSession(). Close it now.
                        return;
                }
  
 +              Hooks::run( 'UnitTestsBeforeDatabaseTeardown' );
 +
                foreach ( $wgJobClasses as $type => $class ) {
                        // Delete any jobs under the clone DB (or old prefix in other stores)
                        JobQueueGroup::singleton()->get( $type )->delete();
                if ( $db->getType() == 'oracle' ) {
                        $db->query( 'BEGIN FILL_WIKI_INFO; END;' );
                }
 +
 +              Hooks::run( 'UnitTestsAfterDatabaseSetup', [ $db, $prefix ] );
        }
  
        /**
                        if ( !isset( $wgNamespaceContentModels[$ns] ) ||
                                $wgNamespaceContentModels[$ns] === CONTENT_MODEL_WIKITEXT
                        ) {
 -
                                $wikitextNS = $ns;
  
                                return $wikitextNS;