Merge "Stop doing $that = $this in includes/libs"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Wed, 10 Feb 2016 18:28:31 +0000 (18:28 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 10 Feb 2016 18:28:31 +0000 (18:28 +0000)
includes/api/ApiMain.php
includes/deferred/LinksUpdate.php
includes/jobqueue/JobQueueDB.php
includes/jobqueue/JobQueueMemory.php
includes/jobqueue/JobQueueRedis.php

index f8192e5..eb76024 100644 (file)
@@ -1181,27 +1181,44 @@ class ApiMain extends ApiBase {
                        && in_array( 'bot', $this->getUser()->getGroups() )
                        && wfGetLB()->getServerCount() > 1
                ) {
-                       // Figure out how many servers have passed the lag threshold
-                       $numLagged = 0;
-                       $lagLimit = $this->getConfig()->get( 'APIMaxLagThreshold' );
-                       foreach ( wfGetLB()->getLagTimes() as $lag ) {
-                               if ( $lag > $lagLimit ) {
-                                       ++$numLagged;
-                               }
-                       }
-                       // If a majority of slaves are too lagged then disallow writes
-                       $slaveCount = wfGetLB()->getServerCount() - 1;
-                       if ( $numLagged >= ceil( $slaveCount / 2 ) ) {
-                               $parsed = $this->parseMsg( array( 'readonlytext' ) );
-                               $this->dieUsage(
-                                       $parsed['info'],
-                                       $parsed['code'],
-                                       /* http error */
-                                       0,
-                                       array( 'readonlyreason' => "Waiting for $numLagged lagged database(s)" )
-                               );
+                       $this->checkBotReadOnly();
+               }
+       }
+
+       /**
+        * Check whether we are readonly for bots
+        */
+       private function checkBotReadOnly() {
+               // Figure out how many servers have passed the lag threshold
+               $numLagged = 0;
+               $lagLimit = $this->getConfig()->get( 'APIMaxLagThreshold' );
+               $laggedServers = array();
+               $loadBalancer = wfGetLB();
+               foreach ( $loadBalancer->getLagTimes() as $serverIndex => $lag ) {
+                       if ( $lag > $lagLimit ) {
+                               ++$numLagged;
+                               $laggedServers[] = $loadBalancer->getServerName( $serverIndex ) . " ({$lag}s)";
                        }
                }
+
+               // If a majority of slaves are too lagged then disallow writes
+               $slaveCount = wfGetLB()->getServerCount() - 1;
+               if ( $numLagged >= ceil( $slaveCount / 2 ) ) {
+                       $laggedServers = join( ', ', $laggedServers );
+                       wfDebugLog(
+                               'api-readonly',
+                               "Api request failed as read only because the following DBs are lagged: $laggedServers"
+                       );
+
+                       $parsed = $this->parseMsg( array( 'readonlytext' ) );
+                       $this->dieUsage(
+                               $parsed['info'],
+                               $parsed['code'],
+                               /* http error */
+                               0,
+                               array( 'readonlyreason' => "Waiting for $numLagged lagged database(s)" )
+                       );
+               }
        }
 
        /**
index 3021af1..9cd20fc 100644 (file)
@@ -143,9 +143,8 @@ class LinksUpdate extends SqlDataUpdate implements EnqueueableDataUpdate {
                Hooks::run( 'LinksUpdate', array( &$this ) );
                $this->doIncrementalUpdate();
 
-               $that = $this;
-               $this->mDb->onTransactionIdle( function() use ( $that ) {
-                       Hooks::run( 'LinksUpdateComplete', array( &$that ) );
+               $this->mDb->onTransactionIdle( function() {
+                       Hooks::run( 'LinksUpdateComplete', array( &$this ) );
                } );
        }
 
index f10866e..51dec65 100644 (file)
@@ -181,11 +181,10 @@ class JobQueueDB extends JobQueue {
        protected function doBatchPush( array $jobs, $flags ) {
                $dbw = $this->getMasterDB();
 
-               $that = $this;
                $method = __METHOD__;
                $dbw->onTransactionIdle(
-                       function () use ( $dbw, $that, $jobs, $flags, $method ) {
-                               $that->doBatchPushInternal( $dbw, $jobs, $flags, $method );
+                       function () use ( $dbw, $jobs, $flags, $method ) {
+                               $this->doBatchPushInternal( $dbw, $jobs, $flags, $method );
                        }
                );
        }
index 7dad748..7e9c0c9 100644 (file)
@@ -175,11 +175,10 @@ class JobQueueMemory extends JobQueue {
                        return new ArrayIterator( array() );
                }
 
-               $that = $this;
                return new MappedIterator(
                        $unclaimed,
-                       function ( $value ) use ( $that ) {
-                               $that->jobFromSpecInternal( $value );
+                       function ( $value ) {
+                               $this->jobFromSpecInternal( $value );
                        }
                );
        }
@@ -195,11 +194,10 @@ class JobQueueMemory extends JobQueue {
                        return new ArrayIterator( array() );
                }
 
-               $that = $this;
                return new MappedIterator(
                        $claimed,
-                       function ( $value ) use ( $that ) {
-                               $that->jobFromSpecInternal( $value );
+                       function ( $value ) {
+                               $this->jobFromSpecInternal( $value );
                        }
                );
        }
index eda3e9c..408828d 100644 (file)
@@ -573,12 +573,10 @@ LUA;
         * @return MappedIterator
         */
        protected function getJobIterator( RedisConnRef $conn, array $uids ) {
-               $that = $this;
-
                return new MappedIterator(
                        $uids,
-                       function ( $uid ) use ( $that, $conn ) {
-                               return $that->getJobFromUidInternal( $uid, $conn );
+                       function ( $uid ) use ( $conn ) {
+                               return $this->getJobFromUidInternal( $uid, $conn );
                        },
                        array( 'accept' => function ( $job ) {
                                return is_object( $job );