Add debug logging for the case that the API goes read only
authorMarius Hoch <hoo@online.de>
Sun, 17 Jan 2016 17:03:25 +0000 (18:03 +0100)
committerMarius Hoch <hoo@online.de>
Wed, 27 Jan 2016 09:16:18 +0000 (10:16 +0100)
Useful for tasks like T123867.

Change-Id: I39a65047eac05b9e0268a9184b9fae4c48e66ce9

includes/api/ApiMain.php

index 6ddc28a..814d14e 100644 (file)
@@ -1180,27 +1180,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)" )
+                       );
+               }
        }
 
        /**