MediaWiki REL1_37
JobRunner.php
Go to the documentation of this file.
1<?php
24use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
28use Psr\Log\LoggerAwareInterface;
29use Psr\Log\LoggerInterface;
33use Wikimedia\ScopedCallback;
34
41class JobRunner implements LoggerAwareInterface {
42
46 public const CONSTRUCTOR_OPTIONS = [
47 'JobBackoffThrottling',
48 'JobClasses',
49 'JobSerialCommitThreshold',
50 'MaxJobDBWriteDuration',
51 'TrxProfilerLimits'
52 ];
53
55 private $options;
56
58 private $lbFactory;
59
62
65
67 private $linkCache;
68
70 private $stats;
71
73 private $debug;
74
76 private $logger;
77
79 private const MAX_ALLOWED_LAG = 3;
81 private const SYNC_TIMEOUT = self::MAX_ALLOWED_LAG;
83 private const LAG_CHECK_PERIOD = 1.0;
85 private const ERROR_BACKOFF_TTL = 1;
87 private const READONLY_BACKOFF_TTL = 30;
88
92 public function setDebugHandler( $debug ) {
93 $this->debug = $debug;
94 }
95
101 public function setLogger( LoggerInterface $logger ) {
102 wfDeprecated( __METHOD__, '1.35' );
103 $this->logger = $logger;
104 }
105
117 public function __construct(
118 $serviceOptions = null,
119 ILBFactory $lbFactory = null,
120 JobQueueGroup $jobQueueGroup = null,
121 ReadOnlyMode $readOnlyMode = null,
122 LinkCache $linkCache = null,
123 StatsdDataFactoryInterface $statsdDataFactory = null,
124 LoggerInterface $logger = null
125 ) {
126 if ( !$serviceOptions || $serviceOptions instanceof LoggerInterface ) {
127 // TODO: wfDeprecated( __METHOD__ . ' called directly. Use MediaWikiServices instead', '1.35' );
128 $logger = $serviceOptions;
129 $serviceOptions = new ServiceOptions(
130 static::CONSTRUCTOR_OPTIONS,
131 MediaWikiServices::getInstance()->getMainConfig()
132 );
133 }
134
135 $this->options = $serviceOptions;
136 $this->lbFactory = $lbFactory ?? MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
137 $this->jobQueueGroup = $jobQueueGroup ?? JobQueueGroup::singleton();
138 $this->readOnlyMode = $readOnlyMode ?: MediaWikiServices::getInstance()->getReadOnlyMode();
139 $this->linkCache = $linkCache ?? MediaWikiServices::getInstance()->getLinkCache();
140 $this->stats = $statsdDataFactory ?? MediaWikiServices::getInstance()->getStatsdDataFactory();
141 $this->logger = $logger ?? LoggerFactory::getInstance( 'runJobs' );
142 }
143
169 public function run( array $options ) {
170 $type = $options['type'] ?? false;
171 $maxJobs = $options['maxJobs'] ?? false;
172 $maxTime = $options['maxTime'] ?? false;
173 $throttle = $options['throttle'] ?? true;
174
175 $jobClasses = $this->options->get( 'JobClasses' );
176 $profilerLimits = $this->options->get( 'TrxProfilerLimits' );
177
178 $response = [ 'jobs' => [], 'reached' => 'none-ready' ];
179
180 if ( $type !== false && !isset( $jobClasses[$type] ) ) {
181 // Invalid job type specified
182 $response['reached'] = 'none-possible';
183 return $response;
184 }
185
186 if ( $this->readOnlyMode->isReadOnly() ) {
187 // Any jobs popped off the queue might fail to run and thus might end up lost
188 $response['reached'] = 'read-only';
189 return $response;
190 }
191
192 list( , $maxLag ) = $this->lbFactory->getMainLB()->getMaxLag();
193 if ( $maxLag >= self::MAX_ALLOWED_LAG ) {
194 // DB lag is already too high; caller can immediately try other wikis if applicable
195 $response['reached'] = 'replica-lag-limit';
196 return $response;
197 }
198
199 // Narrow DB query expectations for this HTTP request
200 $this->lbFactory->getTransactionProfiler()
201 ->setExpectations( $profilerLimits['JobRunner'], __METHOD__ );
202
203 // Error out if an explicit DB transaction round is somehow active
204 if ( $this->lbFactory->hasTransactionRound() ) {
205 throw new LogicException( __METHOD__ . ' called with an active transaction round.' );
206 }
207
208 // Some jobs types should not run until a certain timestamp
209 $backoffs = []; // map of (type => UNIX expiry)
210 $backoffDeltas = []; // map of (type => seconds)
211 $wait = 'wait'; // block to read backoffs the first time
212
213 $loopStartTime = microtime( true );
214 $jobsPopped = 0;
215 $timeMsTotal = 0;
216 $lastSyncTime = 1; // initialize "last sync check timestamp" to "ages ago"
217 // Keep popping and running jobs until there are no more...
218 do {
219 // Sync the persistent backoffs with concurrent runners
220 $backoffs = $this->syncBackoffDeltas( $backoffs, $backoffDeltas, $wait );
221 $backoffKeys = $throttle ? array_keys( $backoffs ) : [];
222 $wait = 'nowait'; // less important now
223
224 if ( $type === false ) {
225 // Treat the default job type queues as a single queue and pop off a job
226 $job = $this->jobQueueGroup
227 ->pop( JobQueueGroup::TYPE_DEFAULT, JobQueueGroup::USE_CACHE, $backoffKeys );
228 } else {
229 // Pop off a job from the specified job type queue unless the execution of
230 // that type of job is currently rate-limited by the back-off list
231 $job = in_array( $type, $backoffKeys ) ? false : $this->jobQueueGroup->pop( $type );
232 }
233
234 if ( $job ) {
235 ++$jobsPopped;
236 $jType = $job->getType();
237
238 // Back off of certain jobs for a while (for throttling and for errors)
239 $ttw = $this->getBackoffTimeToWait( $job );
240 if ( $ttw > 0 ) {
241 // Always add the delta for other runners in case the time running the
242 // job negated the backoff for each individually but not collectively.
243 $backoffDeltas[$jType] = ( $backoffDeltas[$jType] ?? 0 ) + $ttw;
244 $backoffs = $this->syncBackoffDeltas( $backoffs, $backoffDeltas, $wait );
245 }
246
247 $info = $this->executeJob( $job );
248
249 // Mark completed or "one shot only" jobs as resolved
250 if ( $info['status'] !== false || !$job->allowRetries() ) {
251 $this->jobQueueGroup->ack( $job );
252 }
253
254 // Back off of certain jobs for a while (for throttling and for errors)
255 if ( $info['status'] === false && mt_rand( 0, 49 ) == 0 ) {
256 $ttw = max( $ttw, $this->getErrorBackoffTTL( $info['caught'] ) );
257 $backoffDeltas[$jType] = ( $backoffDeltas[$jType] ?? 0 ) + $ttw;
258 }
259
260 $response['jobs'][] = [
261 'type' => $jType,
262 'status' => ( $info['status'] === false ) ? 'failed' : 'ok',
263 'error' => $info['error'],
264 'time' => $info['timeMs']
265 ];
266 $timeMsTotal += $info['timeMs'];
267
268 // Break out if we hit the job count or wall time limits
269 if ( $maxJobs && $jobsPopped >= $maxJobs ) {
270 $response['reached'] = 'job-limit';
271 break;
272 } elseif ( $maxTime && ( microtime( true ) - $loopStartTime ) > $maxTime ) {
273 $response['reached'] = 'time-limit';
274 break;
275 }
276
277 // Don't let any of the main DB replica DBs get backed up.
278 // This only waits for so long before exiting and letting
279 // other wikis in the farm (on different masters) get a chance.
280 $timePassed = microtime( true ) - $lastSyncTime;
281 if ( $timePassed >= self::LAG_CHECK_PERIOD || $timePassed < 0 ) {
282 $opts = [ 'ifWritesSince' => $lastSyncTime, 'timeout' => self::SYNC_TIMEOUT ];
283 if ( !$this->lbFactory->waitForReplication( $opts ) ) {
284 $response['reached'] = 'replica-lag-limit';
285 break;
286 }
287 $lastSyncTime = microtime( true );
288 }
289
290 // Abort if nearing OOM to avoid erroring out in the middle of a job
291 if ( !$this->checkMemoryOK() ) {
292 $response['reached'] = 'memory-limit';
293 break;
294 }
295 }
296 } while ( $job );
297
298 // Sync the persistent backoffs for the next runJobs.php pass
299 if ( $backoffDeltas ) {
300 $this->syncBackoffDeltas( $backoffs, $backoffDeltas, 'wait' );
301 }
302
303 $response['backoffs'] = $backoffs;
304 $response['elapsed'] = $timeMsTotal;
305
306 return $response;
307 }
308
327 public function executeJob( RunnableJob $job ) {
328 $oldRequestId = WebRequest::getRequestId();
329 // Temporarily inherit the original ID of the web request that spawned this job
330 WebRequest::overrideRequestId( $job->getRequestId() );
331 // Use an appropriate timeout to balance lag avoidance and job progress
332 $oldTimeout = $this->lbFactory->setDefaultReplicationWaitTimeout( self::SYNC_TIMEOUT );
333 try {
334 return $this->doExecuteJob( $job );
335 } finally {
336 $this->lbFactory->setDefaultReplicationWaitTimeout( $oldTimeout );
337 WebRequest::overrideRequestId( $oldRequestId );
338 }
339 }
340
349 private function doExecuteJob( RunnableJob $job ) {
350 $jType = $job->getType();
351 $msg = $job->toString() . " STARTING";
352 $this->logger->debug( $msg, [ 'job_type' => $job->getType() ] );
353 $this->debugCallback( $msg );
354
355 // Clear out title cache data from prior snapshots
356 // (e.g. from before JobRunner was invoked in this process)
357 $this->linkCache->clear();
358
359 // Run the job...
360 $caught = [];
361 $rssStart = $this->getMaxRssKb();
362 $jobStartTime = microtime( true );
363 try {
364 $fnameTrxOwner = get_class( $job ) . '::run'; // give run() outer scope
365 // Flush any pending changes left over from an implicit transaction round
366 if ( $job->hasExecutionFlag( $job::JOB_NO_EXPLICIT_TRX_ROUND ) ) {
367 $this->lbFactory->commitPrimaryChanges( $fnameTrxOwner ); // new implicit round
368 } else {
369 $this->lbFactory->beginPrimaryChanges( $fnameTrxOwner ); // new explicit round
370 }
371 // Clear any stale REPEATABLE-READ snapshots from replica DB connections
372 $this->lbFactory->flushReplicaSnapshots( $fnameTrxOwner );
373 $status = $job->run();
374 $error = $job->getLastError();
375 // Commit all pending changes from this job
376 $this->commitPrimaryChanges( $job, $fnameTrxOwner );
377 // Run any deferred update tasks; doUpdates() manages transactions itself
378 DeferredUpdates::doUpdates();
379 } catch ( Throwable $e ) {
380 MWExceptionHandler::rollbackPrimaryChangesAndLog( $e );
381 $status = false;
382 $error = get_class( $e ) . ': ' . $e->getMessage();
383 $caught[] = get_class( $e );
384 }
385 // Always attempt to call teardown(), even if Job throws exception
386 try {
387 $job->tearDown( $status );
388 } catch ( Throwable $e ) {
389 MWExceptionHandler::logException( $e );
390 }
391
392 $timeMs = intval( ( microtime( true ) - $jobStartTime ) * 1000 );
393 $rssEnd = $this->getMaxRssKb();
394
395 // Record how long jobs wait before getting popped
396 $readyTs = $job->getReadyTimestamp();
397 if ( $readyTs ) {
398 $pickupDelay = max( 0, $jobStartTime - $readyTs );
399 $this->stats->timing( 'jobqueue.pickup_delay.all', 1000 * $pickupDelay );
400 $this->stats->timing( "jobqueue.pickup_delay.$jType", 1000 * $pickupDelay );
401 }
402 // Record root job age for jobs being run
403 $rootTimestamp = $job->getRootJobParams()['rootJobTimestamp'];
404 if ( $rootTimestamp ) {
405 $age = max( 0, $jobStartTime - wfTimestamp( TS_UNIX, $rootTimestamp ) );
406 $this->stats->timing( "jobqueue.pickup_root_age.$jType", 1000 * $age );
407 }
408 // Track the execution time for jobs
409 $this->stats->timing( "jobqueue.run.$jType", $timeMs );
410 // Track RSS increases for jobs (in case of memory leaks)
411 if ( $rssStart && $rssEnd ) {
412 $this->stats->updateCount( "jobqueue.rss_delta.$jType", $rssEnd - $rssStart );
413 }
414
415 if ( $status === false ) {
416 $msg = $job->toString() . " t={job_duration} error={job_error}";
417 $this->logger->error( $msg, [
418 'job_type' => $job->getType(),
419 'job_duration' => $timeMs,
420 'job_error' => $error,
421 ] );
422
423 $msg = $job->toString() . " t=$timeMs error={$error}";
424 $this->debugCallback( $msg );
425 } else {
426 $msg = $job->toString() . " t={job_duration} good";
427 $this->logger->info( $msg, [
428 'job_type' => $job->getType(),
429 'job_duration' => $timeMs,
430 ] );
431
432 $msg = $job->toString() . " t=$timeMs good";
433 $this->debugCallback( $msg );
434 }
435
436 return [
437 'status' => $status,
438 'error' => $error,
439 'caught' => $caught,
440 'timeMs' => $timeMs
441 ];
442 }
443
448 private function getErrorBackoffTTL( array $caught ) {
449 return in_array( DBReadOnlyError::class, $caught )
450 ? self::READONLY_BACKOFF_TTL
451 : self::ERROR_BACKOFF_TTL;
452 }
453
457 private function getMaxRssKb() {
458 $info = getrusage( 0 /* RUSAGE_SELF */ );
459 // see https://linux.die.net/man/2/getrusage
460 return isset( $info['ru_maxrss'] ) ? (int)$info['ru_maxrss'] : null;
461 }
462
469 $throttling = $this->options->get( 'JobBackoffThrottling' );
470
471 if ( !isset( $throttling[$job->getType()] ) || $job instanceof DuplicateJob ) {
472 return 0; // not throttled
473 }
474
475 $itemsPerSecond = $throttling[$job->getType()];
476 if ( $itemsPerSecond <= 0 ) {
477 return 0; // not throttled
478 }
479
480 $seconds = 0;
481 if ( $job->workItemCount() > 0 ) {
482 $exactSeconds = $job->workItemCount() / $itemsPerSecond;
483 // use randomized rounding
484 $seconds = floor( $exactSeconds );
485 $remainder = $exactSeconds - $seconds;
486 $seconds += ( mt_rand() / mt_getrandmax() < $remainder ) ? 1 : 0;
487 }
488
489 return (int)$seconds;
490 }
491
500 private function loadBackoffs( array $backoffs, $mode = 'wait' ) {
501 $file = wfTempDir() . '/mw-runJobs-backoffs.json';
502 if ( is_file( $file ) ) {
503 $noblock = ( $mode === 'nowait' ) ? LOCK_NB : 0;
504 $handle = fopen( $file, 'rb' );
505 if ( !flock( $handle, LOCK_SH | $noblock ) ) {
506 fclose( $handle );
507 return $backoffs; // don't wait on lock
508 }
509 $content = stream_get_contents( $handle );
510 flock( $handle, LOCK_UN );
511 fclose( $handle );
512 $ctime = microtime( true );
513 $cBackoffs = json_decode( $content, true ) ?: [];
514 foreach ( $cBackoffs as $type => $timestamp ) {
515 if ( $timestamp < $ctime ) {
516 unset( $cBackoffs[$type] );
517 }
518 }
519 } else {
520 $cBackoffs = [];
521 }
522
523 return $cBackoffs;
524 }
525
537 private function syncBackoffDeltas( array $backoffs, array &$deltas, $mode = 'wait' ) {
538 if ( !$deltas ) {
539 return $this->loadBackoffs( $backoffs, $mode );
540 }
541
542 $noblock = ( $mode === 'nowait' ) ? LOCK_NB : 0;
543 $file = wfTempDir() . '/mw-runJobs-backoffs.json';
544 $handle = fopen( $file, 'wb+' );
545 if ( !flock( $handle, LOCK_EX | $noblock ) ) {
546 fclose( $handle );
547 return $backoffs; // don't wait on lock
548 }
549 $ctime = microtime( true );
550 $content = stream_get_contents( $handle );
551 $cBackoffs = json_decode( $content, true ) ?: [];
552 foreach ( $deltas as $type => $seconds ) {
553 $cBackoffs[$type] = isset( $cBackoffs[$type] ) && $cBackoffs[$type] >= $ctime
554 ? $cBackoffs[$type] + $seconds
555 : $ctime + $seconds;
556 }
557 foreach ( $cBackoffs as $type => $timestamp ) {
558 if ( $timestamp < $ctime ) {
559 unset( $cBackoffs[$type] );
560 }
561 }
562 ftruncate( $handle, 0 );
563 fwrite( $handle, json_encode( $cBackoffs ) );
564 flock( $handle, LOCK_UN );
565 fclose( $handle );
566
567 $deltas = [];
568
569 return $cBackoffs;
570 }
571
577 private function checkMemoryOK() {
578 static $maxBytes = null;
579 if ( $maxBytes === null ) {
580 $m = [];
581 if ( preg_match( '!^(\d+)(k|m|g|)$!i', ini_get( 'memory_limit' ), $m ) ) {
582 list( , $num, $unit ) = $m;
583 $conv = [ 'g' => 1073741824, 'm' => 1048576, 'k' => 1024, '' => 1 ];
584 $maxBytes = $num * $conv[strtolower( $unit )];
585 } else {
586 $maxBytes = 0;
587 }
588 }
589 $usedBytes = memory_get_usage();
590 if ( $maxBytes && $usedBytes >= 0.95 * $maxBytes ) {
591 $msg = "Detected excessive memory usage ({used_bytes}/{max_bytes}).";
592 $this->logger->error( $msg, [
593 'used_bytes' => $usedBytes,
594 'max_bytes' => $maxBytes,
595 ] );
596
597 $msg = "Detected excessive memory usage ($usedBytes/$maxBytes).";
598 $this->debugCallback( $msg );
599
600 return false;
601 }
602
603 return true;
604 }
605
610 private function debugCallback( $msg ) {
611 if ( $this->debug ) {
612 call_user_func_array( $this->debug, [ wfTimestamp( TS_DB ) . " $msg\n" ] );
613 }
614 }
615
626 private function commitPrimaryChanges( RunnableJob $job, $fnameTrxOwner ) {
627 $syncThreshold = $this->options->get( 'JobSerialCommitThreshold' );
628
629 $time = false;
630 $lb = $this->lbFactory->getMainLB();
631 if ( $syncThreshold !== false && $lb->hasStreamingReplicaServers() ) {
632 // Generally, there is one primary connection to the local DB
633 $dbwSerial = $lb->getAnyOpenConnection( $lb->getWriterIndex() );
634 // We need natively blocking fast locks
635 if ( $dbwSerial && $dbwSerial->namedLocksEnqueue() ) {
636 $time = $dbwSerial->pendingWriteQueryDuration( $dbwSerial::ESTIMATE_DB_APPLY );
637 if ( $time < $syncThreshold ) {
638 $dbwSerial = false;
639 }
640 } else {
641 $dbwSerial = false;
642 }
643 } else {
644 // There are no replica DBs or writes are all to foreign DB (we don't handle that)
645 $dbwSerial = false;
646 }
647
648 if ( !$dbwSerial ) {
649 $this->lbFactory->commitPrimaryChanges(
650 $fnameTrxOwner,
651 // Abort if any transaction was too big
652 [ 'maxWriteDuration' => $this->options->get( 'MaxJobDBWriteDuration' ) ]
653 );
654
655 return;
656 }
657
658 $ms = intval( 1000 * $time );
659
660 $msg = $job->toString() . " COMMIT ENQUEUED [{job_commit_write_ms}ms of writes]";
661 $this->logger->info( $msg, [
662 'job_type' => $job->getType(),
663 'job_commit_write_ms' => $ms,
664 ] );
665
666 $msg = $job->toString() . " COMMIT ENQUEUED [{$ms}ms of writes]";
667 $this->debugCallback( $msg );
668
669 // Wait for an exclusive lock to commit
670 if ( !$dbwSerial->lock( 'jobrunner-serial-commit', $fnameTrxOwner, 30 ) ) {
671 // This will trigger a rollback in the main loop
672 throw new DBError( $dbwSerial, "Timed out waiting on commit queue." );
673 }
674 $unlocker = new ScopedCallback( static function () use ( $dbwSerial, $fnameTrxOwner ) {
675 $dbwSerial->unlock( 'jobrunner-serial-commit', $fnameTrxOwner );
676 } );
677
678 // Wait for the replica DBs to catch up
679 $pos = $lb->getPrimaryPos();
680 if ( $pos ) {
681 $lb->waitForAll( $pos );
682 }
683
684 // Actually commit the DB primary changes
685 $this->lbFactory->commitPrimaryChanges(
686 $fnameTrxOwner,
687 // Abort if any transaction was too big
688 [ 'maxWriteDuration' => $this->options->get( 'MaxJobDBWriteDuration' ) ]
689 );
690 ScopedCallback::consume( $unlocker );
691 }
692}
wfTempDir()
Tries to get the system directory for temporary files.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
No-op job that does nothing.
Class to handle enqueueing of background jobs.
Job queue runner utility methods.
Definition JobRunner.php:41
ILBFactory $lbFactory
Definition JobRunner.php:58
setDebugHandler( $debug)
Definition JobRunner.php:92
ServiceOptions $options
Definition JobRunner.php:55
LinkCache $linkCache
Definition JobRunner.php:67
run(array $options)
Run jobs of the specified number/type for the specified time.
getErrorBackoffTTL(array $caught)
ReadOnlyMode $readOnlyMode
Definition JobRunner.php:64
const CONSTRUCTOR_OPTIONS
Definition JobRunner.php:46
callable null $debug
Debug output handler.
Definition JobRunner.php:73
setLogger(LoggerInterface $logger)
executeJob(RunnableJob $job)
Run a specific job in a manner appropriate for mass use by job dispatchers.
syncBackoffDeltas(array $backoffs, array &$deltas, $mode='wait')
Merge the current backoff expiries from persistent storage.
commitPrimaryChanges(RunnableJob $job, $fnameTrxOwner)
Issue a commit on all masters who are currently in a transaction and have made changes to the databas...
JobQueueGroup $jobQueueGroup
Definition JobRunner.php:61
StatsdDataFactoryInterface $stats
Definition JobRunner.php:70
doExecuteJob(RunnableJob $job)
debugCallback( $msg)
Log the job message.
getBackoffTimeToWait(RunnableJob $job)
checkMemoryOK()
Make sure that this script is not too close to the memory usage limit.
LoggerInterface $logger
Definition JobRunner.php:76
__construct( $serviceOptions=null, ILBFactory $lbFactory=null, JobQueueGroup $jobQueueGroup=null, ReadOnlyMode $readOnlyMode=null, LinkCache $linkCache=null, StatsdDataFactoryInterface $statsdDataFactory=null, LoggerInterface $logger=null)
Calling this directly is deprecated.
loadBackoffs(array $backoffs, $mode='wait')
Get the previous backoff expiries from persistent storage On I/O or lock acquisition failure this ret...
Cache for article titles (prefixed DB keys) and ids linked from one source.
Definition LinkCache.php:41
A class for passing options to services.
PSR-3 logger instance factory.
MediaWikiServices is the service locator for the application scope of MediaWiki.
A service class for fetching the wiki's current read-only mode.
Database error base class @newable.
Definition DBError.php:32
Job that has a run() method and metadata accessors for JobQueue::pop() and JobQueue::ack()
An interface for generating database load balancers.
$debug
Definition mcc.php:31
if(count( $args)< 1) $job
$content
Definition router.php:76
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42