Merge "objectcache: Complete coverage for newAnything()"
[lhc/web/wiklou.git] / includes / api / ApiMain.php
index b5eff70..4068a50 100644 (file)
@@ -1230,6 +1230,35 @@ class ApiMain extends ApiBase {
                return $module;
        }
 
+       /**
+        * @return array
+        */
+       private function getMaxLag() {
+               $dbLag = MediaWikiServices::getInstance()->getDBLoadBalancer()->getMaxLag();
+               $lagInfo = [
+                       'host' => $dbLag[0],
+                       'lag' => $dbLag[1],
+                       'type' => 'db'
+               ];
+
+               $jobQueueLagFactor = $this->getConfig()->get( 'JobQueueIncludeInMaxLagFactor' );
+               if ( $jobQueueLagFactor ) {
+                       // Turn total number of jobs into seconds by using the configured value
+                       $totalJobs = array_sum( JobQueueGroup::singleton()->getQueueSizes() );
+                       $jobQueueLag = $totalJobs / (float)$jobQueueLagFactor;
+                       if ( $jobQueueLag > $lagInfo['lag'] ) {
+                               $lagInfo = [
+                                       'host' => wfHostname(), // XXX: Is there a better value that could be used?
+                                       'lag' => $jobQueueLag,
+                                       'type' => 'jobqueue',
+                                       'jobs' => $totalJobs,
+                               ];
+                       }
+               }
+
+               return $lagInfo;
+       }
+
        /**
         * Check the max lag if necessary
         * @param ApiBase $module Api module being used
@@ -1239,18 +1268,22 @@ class ApiMain extends ApiBase {
        protected function checkMaxLag( $module, $params ) {
                if ( $module->shouldCheckMaxlag() && isset( $params['maxlag'] ) ) {
                        $maxLag = $params['maxlag'];
-                       list( $host, $lag ) = wfGetLB()->getMaxLag();
-                       if ( $lag > $maxLag ) {
+                       $lagInfo = $this->getMaxLag();
+                       if ( $lagInfo['lag'] > $maxLag ) {
                                $response = $this->getRequest()->response();
 
                                $response->header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) );
-                               $response->header( 'X-Database-Lag: ' . intval( $lag ) );
+                               $response->header( 'X-Database-Lag: ' . intval( $lagInfo['lag'] ) );
 
                                if ( $this->getConfig()->get( 'ShowHostnames' ) ) {
-                                       $this->dieWithError( [ 'apierror-maxlag', $lag, $host ] );
+                                       $this->dieWithError(
+                                               [ 'apierror-maxlag', $lagInfo['lag'], $lagInfo['host'] ],
+                                               'maxlag',
+                                               $lagInfo
+                                       );
                                }
 
-                               $this->dieWithError( [ 'apierror-maxlag-generic', $lag ], 'maxlag' );
+                               $this->dieWithError( [ 'apierror-maxlag-generic', $lagInfo['lag'] ], 'maxlag', $lagInfo );
                        }
                }