From c582d477b3f655f2a2ded1e8d5fed25f837e3c21 Mon Sep 17 00:00:00 2001 From: Marius Hoch Date: Sun, 17 Jan 2016 18:03:25 +0100 Subject: [PATCH] Add debug logging for the case that the API goes read only Useful for tasks like T123867. Change-Id: I39a65047eac05b9e0268a9184b9fae4c48e66ce9 --- includes/api/ApiMain.php | 55 ++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 6ddc28af21..814d14e5de 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -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)" ) + ); + } } /** -- 2.20.1