| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
use MediaWiki\MediaWikiServices; |
| 21 |
use Wikimedia\Rdbms\DBConnectionError; |
| 22 |
use Wikimedia\Rdbms\DBError; |
| 23 |
use Wikimedia\Rdbms\IDatabase; |
| 24 |
use Wikimedia\Rdbms\IMaintainableDatabase; |
| 25 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 26 |
use Wikimedia\Rdbms\RawSQLValue; |
| 27 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 28 |
use Wikimedia\Rdbms\ServerInfo; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class JobQueueDB extends JobQueue { |
| 37 |
|
| 38 |
private const CACHE_TTL_SHORT = 30; |
| 39 |
|
| 40 |
private const MAX_AGE_PRUNE = 7 * 24 * 3600; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
private const MAX_JOB_RANDOM = 2_147_483_647; |
| 46 |
|
| 47 |
private const MAX_OFFSET = 255; |
| 48 |
|
| 49 |
|
| 50 |
protected $conn; |
| 51 |
|
| 52 |
|
| 53 |
protected $server; |
| 54 |
|
| 55 |
protected $cluster; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
protected function __construct( array $params ) { |
| 67 |
parent::__construct( $params ); |
| 68 |
|
| 69 |
if ( isset( $params['server'] ) ) { |
| 70 |
$this->server = $params['server']; |
| 71 |
|
| 72 |
$this->server['flags'] ??= 0; |
| 73 |
$this->server['flags'] &= ~( IDatabase::DBO_TRX | IDatabase::DBO_DEFAULT ); |
| 74 |
} elseif ( isset( $params['cluster'] ) && is_string( $params['cluster'] ) ) { |
| 75 |
$this->cluster = $params['cluster']; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
protected function supportedOrders() { |
| 80 |
return [ 'random', 'timestamp', 'fifo' ]; |
| 81 |
} |
| 82 |
|
| 83 |
protected function optimalOrder() { |
| 84 |
return 'random'; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
protected function doIsEmpty() { |
| 92 |
$dbr = $this->getReplicaDB(); |
| 93 |
try { |
| 94 |
|
| 95 |
$found = (bool)$dbr->newSelectQueryBuilder() |
| 96 |
->select( '1' ) |
| 97 |
->from( 'job' ) |
| 98 |
->where( [ 'job_cmd' => $this->type, 'job_token' => '' ] ) |
| 99 |
->caller( __METHOD__ )->fetchField(); |
| 100 |
} catch ( DBError $e ) { |
| 101 |
throw $this->getDBException( $e ); |
| 102 |
} |
| 103 |
|
| 104 |
return !$found; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
protected function doGetSize() { |
| 112 |
$key = $this->getCacheKey( 'size' ); |
| 113 |
|
| 114 |
$size = $this->wanCache->get( $key ); |
| 115 |
if ( is_int( $size ) ) { |
| 116 |
return $size; |
| 117 |
} |
| 118 |
|
| 119 |
$dbr = $this->getReplicaDB(); |
| 120 |
try { |
| 121 |
$size = $dbr->newSelectQueryBuilder() |
| 122 |
->from( 'job' ) |
| 123 |
->where( [ 'job_cmd' => $this->type, 'job_token' => '' ] ) |
| 124 |
->caller( __METHOD__ )->fetchRowCount(); |
| 125 |
} catch ( DBError $e ) { |
| 126 |
throw $this->getDBException( $e ); |
| 127 |
} |
| 128 |
$this->wanCache->set( $key, $size, self::CACHE_TTL_SHORT ); |
| 129 |
|
| 130 |
return $size; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
protected function doGetAcquiredCount() { |
| 138 |
if ( $this->claimTTL <= 0 ) { |
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
$key = $this->getCacheKey( 'acquiredcount' ); |
| 143 |
|
| 144 |
$count = $this->wanCache->get( $key ); |
| 145 |
if ( is_int( $count ) ) { |
| 146 |
return $count; |
| 147 |
} |
| 148 |
|
| 149 |
$dbr = $this->getReplicaDB(); |
| 150 |
try { |
| 151 |
$count = $dbr->newSelectQueryBuilder() |
| 152 |
->from( 'job' ) |
| 153 |
->where( [ |
| 154 |
'job_cmd' => $this->type, |
| 155 |
$dbr->expr( 'job_token', '!=', '' ), |
| 156 |
] ) |
| 157 |
->caller( __METHOD__ )->fetchRowCount(); |
| 158 |
} catch ( DBError $e ) { |
| 159 |
throw $this->getDBException( $e ); |
| 160 |
} |
| 161 |
$this->wanCache->set( $key, $count, self::CACHE_TTL_SHORT ); |
| 162 |
|
| 163 |
return $count; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
protected function doGetAbandonedCount() { |
| 173 |
if ( $this->claimTTL <= 0 ) { |
| 174 |
return 0; |
| 175 |
} |
| 176 |
|
| 177 |
$key = $this->getCacheKey( 'abandonedcount' ); |
| 178 |
|
| 179 |
$count = $this->wanCache->get( $key ); |
| 180 |
if ( is_int( $count ) ) { |
| 181 |
return $count; |
| 182 |
} |
| 183 |
|
| 184 |
$dbr = $this->getReplicaDB(); |
| 185 |
try { |
| 186 |
$count = $dbr->newSelectQueryBuilder() |
| 187 |
->from( 'job' ) |
| 188 |
->where( |
| 189 |
[ |
| 190 |
'job_cmd' => $this->type, |
| 191 |
$dbr->expr( 'job_token', '!=', '' ), |
| 192 |
$dbr->expr( 'job_attempts', '>=', $this->maxTries ), |
| 193 |
] |
| 194 |
) |
| 195 |
->caller( __METHOD__ )->fetchRowCount(); |
| 196 |
} catch ( DBError $e ) { |
| 197 |
throw $this->getDBException( $e ); |
| 198 |
} |
| 199 |
|
| 200 |
$this->wanCache->set( $key, $count, self::CACHE_TTL_SHORT ); |
| 201 |
|
| 202 |
return $count; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
protected function doBatchPush( array $jobs, $flags ) { |
| 213 |
$dbw = $this->getPrimaryDB(); |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
$fname = __METHOD__; |
| 224 |
$dbw->onTransactionPreCommitOrIdle( |
| 225 |
function ( IDatabase $dbw ) use ( $jobs, $flags, $fname ) { |
| 226 |
$this->doBatchPushInternal( $dbw, $jobs, $flags, $fname ); |
| 227 |
}, |
| 228 |
$fname |
| 229 |
); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public function doBatchPushInternal( IDatabase $dbw, array $jobs, $flags, $method ) { |
| 244 |
if ( $jobs === [] ) { |
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
$rowSet = []; |
| 249 |
$rowList = []; |
| 250 |
foreach ( $jobs as $job ) { |
| 251 |
$row = $this->insertFields( $job, $dbw ); |
| 252 |
if ( $job->ignoreDuplicates() ) { |
| 253 |
$rowSet[$row['job_sha1']] = $row; |
| 254 |
} else { |
| 255 |
$rowList[] = $row; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
if ( $flags & self::QOS_ATOMIC ) { |
| 260 |
$dbw->startAtomic( $method ); |
| 261 |
} |
| 262 |
try { |
| 263 |
|
| 264 |
if ( count( $rowSet ) ) { |
| 265 |
$res = $dbw->newSelectQueryBuilder() |
| 266 |
->select( 'job_sha1' ) |
| 267 |
->from( 'job' ) |
| 268 |
->where( |
| 269 |
[ |
| 270 |
|
| 271 |
'job_sha1' => array_map( 'strval', array_keys( $rowSet ) ), |
| 272 |
'job_token' => '' |
| 273 |
] |
| 274 |
) |
| 275 |
->caller( $method )->fetchResultSet(); |
| 276 |
foreach ( $res as $row ) { |
| 277 |
wfDebug( "Job with hash '{$row->job_sha1}' is a duplicate." ); |
| 278 |
unset( $rowSet[$row->job_sha1] ); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
$rows = array_merge( $rowList, array_values( $rowSet ) ); |
| 283 |
|
| 284 |
foreach ( array_chunk( $rows, 50 ) as $rowBatch ) { |
| 285 |
$dbw->newInsertQueryBuilder() |
| 286 |
->insertInto( 'job' ) |
| 287 |
->rows( $rowBatch ) |
| 288 |
->caller( $method )->execute(); |
| 289 |
} |
| 290 |
$this->incrStats( 'inserts', $this->type, count( $rows ) ); |
| 291 |
$this->incrStats( 'dupe_inserts', $this->type, |
| 292 |
count( $rowSet ) + count( $rowList ) - count( $rows ) |
| 293 |
); |
| 294 |
} catch ( DBError $e ) { |
| 295 |
throw $this->getDBException( $e ); |
| 296 |
} |
| 297 |
if ( $flags & self::QOS_ATOMIC ) { |
| 298 |
$dbw->endAtomic( $method ); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
protected function doPop() { |
| 307 |
$job = false; |
| 308 |
try { |
| 309 |
$uuid = wfRandomString( 32 ); |
| 310 |
do { |
| 311 |
|
| 312 |
if ( in_array( $this->order, [ 'fifo', 'timestamp' ] ) ) { |
| 313 |
$row = $this->claimOldest( $uuid ); |
| 314 |
} else { |
| 315 |
$rand = mt_rand( 0, self::MAX_JOB_RANDOM ); |
| 316 |
$gte = (bool)mt_rand( 0, 1 ); |
| 317 |
$row = $this->claimRandom( $uuid, $rand, $gte ); |
| 318 |
} |
| 319 |
|
| 320 |
if ( !$row ) { |
| 321 |
break; |
| 322 |
} |
| 323 |
$this->incrStats( 'pops', $this->type ); |
| 324 |
|
| 325 |
|
| 326 |
$job = $this->jobFromRow( $row ); |
| 327 |
break; |
| 328 |
} while ( true ); |
| 329 |
|
| 330 |
if ( !$job || mt_rand( 0, 9 ) == 0 ) { |
| 331 |
|
| 332 |
|
| 333 |
$this->recycleAndDeleteStaleJobs(); |
| 334 |
} |
| 335 |
} catch ( DBError $e ) { |
| 336 |
throw $this->getDBException( $e ); |
| 337 |
} |
| 338 |
|
| 339 |
return $job; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
protected function claimRandom( $uuid, $rand, $gte ) { |
| 351 |
$dbw = $this->getPrimaryDB(); |
| 352 |
|
| 353 |
$tinyQueue = $this->wanCache->get( $this->getCacheKey( 'small' ) ); |
| 354 |
|
| 355 |
$invertedDirection = false; |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
do { |
| 361 |
if ( $tinyQueue ) { |
| 362 |
|
| 363 |
|
| 364 |
$row = $dbw->newSelectQueryBuilder() |
| 365 |
->select( self::selectFields() ) |
| 366 |
->from( 'job' ) |
| 367 |
->where( |
| 368 |
[ |
| 369 |
'job_cmd' => $this->type, |
| 370 |
'job_token' => '', |
| 371 |
$dbw->expr( 'job_random', $gte ? '>=' : '<=', $rand ) |
| 372 |
] |
| 373 |
) |
| 374 |
->orderBy( |
| 375 |
'job_random', |
| 376 |
$gte ? SelectQueryBuilder::SORT_ASC : SelectQueryBuilder::SORT_DESC |
| 377 |
) |
| 378 |
->caller( __METHOD__ )->fetchRow(); |
| 379 |
if ( !$row && !$invertedDirection ) { |
| 380 |
$gte = !$gte; |
| 381 |
$invertedDirection = true; |
| 382 |
continue; |
| 383 |
} |
| 384 |
} else { |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
$row = $dbw->newSelectQueryBuilder() |
| 389 |
->select( self::selectFields() ) |
| 390 |
->from( 'job' ) |
| 391 |
->where( |
| 392 |
[ |
| 393 |
'job_cmd' => $this->type, |
| 394 |
'job_token' => '', |
| 395 |
] |
| 396 |
) |
| 397 |
->offset( mt_rand( 0, self::MAX_OFFSET ) ) |
| 398 |
->caller( __METHOD__ )->fetchRow(); |
| 399 |
if ( !$row ) { |
| 400 |
$tinyQueue = true; |
| 401 |
$this->wanCache->set( $this->getCacheKey( 'small' ), 1, 30 ); |
| 402 |
continue; |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
if ( !$row ) { |
| 407 |
break; |
| 408 |
} |
| 409 |
|
| 410 |
$dbw->newUpdateQueryBuilder() |
| 411 |
->update( 'job' ) |
| 412 |
->set( [ |
| 413 |
'job_token' => $uuid, |
| 414 |
'job_token_timestamp' => $dbw->timestamp(), |
| 415 |
'job_attempts' => new RawSQLValue( 'job_attempts+1' ), |
| 416 |
] ) |
| 417 |
->where( [ |
| 418 |
'job_cmd' => $this->type, |
| 419 |
'job_id' => $row->job_id, |
| 420 |
'job_token' => '' |
| 421 |
] ) |
| 422 |
->caller( __METHOD__ )->execute(); |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
if ( !$dbw->affectedRows() ) { |
| 427 |
$row = false; |
| 428 |
} |
| 429 |
} while ( !$row ); |
| 430 |
|
| 431 |
return $row; |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
protected function claimOldest( $uuid ) { |
| 441 |
$dbw = $this->getPrimaryDB(); |
| 442 |
|
| 443 |
$row = false; |
| 444 |
do { |
| 445 |
if ( $dbw->getType() === 'mysql' ) { |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
$dbw->query( "UPDATE {$dbw->tableName( 'job' )} " . |
| 451 |
"SET " . |
| 452 |
"job_token = {$dbw->addQuotes( $uuid ) }, " . |
| 453 |
"job_token_timestamp = {$dbw->addQuotes( $dbw->timestamp() )}, " . |
| 454 |
"job_attempts = job_attempts+1 " . |
| 455 |
"WHERE ( " . |
| 456 |
"job_cmd = {$dbw->addQuotes( $this->type )} " . |
| 457 |
"AND job_token = {$dbw->addQuotes( '' )} " . |
| 458 |
") ORDER BY job_id ASC LIMIT 1", |
| 459 |
__METHOD__ |
| 460 |
); |
| 461 |
} else { |
| 462 |
|
| 463 |
|
| 464 |
$qb = $dbw->newSelectQueryBuilder() |
| 465 |
->select( 'job_id' ) |
| 466 |
->from( 'job' ) |
| 467 |
->where( [ 'job_cmd' => $this->type, 'job_token' => '' ] ) |
| 468 |
->orderBy( 'job_id', SelectQueryBuilder::SORT_ASC ) |
| 469 |
->limit( 1 ); |
| 470 |
|
| 471 |
$dbw->newUpdateQueryBuilder() |
| 472 |
->update( 'job' ) |
| 473 |
->set( [ |
| 474 |
'job_token' => $uuid, |
| 475 |
'job_token_timestamp' => $dbw->timestamp(), |
| 476 |
'job_attempts' => new RawSQLValue( 'job_attempts+1' ), |
| 477 |
] ) |
| 478 |
->where( [ 'job_id' => new RawSQLValue( '(' . $qb->getSQL() . ')' ) ] ) |
| 479 |
->caller( __METHOD__ )->execute(); |
| 480 |
} |
| 481 |
|
| 482 |
if ( !$dbw->affectedRows() ) { |
| 483 |
break; |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
$row = $dbw->newSelectQueryBuilder() |
| 488 |
->select( self::selectFields() ) |
| 489 |
->from( 'job' ) |
| 490 |
->where( [ 'job_cmd' => $this->type, 'job_token' => $uuid ] ) |
| 491 |
->caller( __METHOD__ )->fetchRow(); |
| 492 |
if ( !$row ) { |
| 493 |
wfDebug( "Row deleted as duplicate by another process." ); |
| 494 |
} |
| 495 |
} while ( !$row ); |
| 496 |
|
| 497 |
return $row; |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
protected function doAck( RunnableJob $job ) { |
| 507 |
$id = $job->getMetadata( 'id' ); |
| 508 |
if ( $id === null ) { |
| 509 |
throw new UnexpectedValueException( "Job of type '{$job->getType()}' has no ID." ); |
| 510 |
} |
| 511 |
|
| 512 |
$dbw = $this->getPrimaryDB(); |
| 513 |
try { |
| 514 |
|
| 515 |
$dbw->newDeleteQueryBuilder() |
| 516 |
->deleteFrom( 'job' ) |
| 517 |
->where( [ 'job_cmd' => $this->type, 'job_id' => $id ] ) |
| 518 |
->caller( __METHOD__ )->execute(); |
| 519 |
|
| 520 |
$this->incrStats( 'acks', $this->type ); |
| 521 |
} catch ( DBError $e ) { |
| 522 |
throw $this->getDBException( $e ); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
protected function doDeduplicateRootJob( IJobSpecification $job ) { |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
$dbw = $this->getPrimaryDB(); |
| 539 |
$dbw->onTransactionCommitOrIdle( |
| 540 |
function () use ( $job ) { |
| 541 |
parent::doDeduplicateRootJob( $job ); |
| 542 |
}, |
| 543 |
__METHOD__ |
| 544 |
); |
| 545 |
|
| 546 |
return true; |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
protected function doDelete() { |
| 554 |
$dbw = $this->getPrimaryDB(); |
| 555 |
try { |
| 556 |
$dbw->newDeleteQueryBuilder() |
| 557 |
->deleteFrom( 'job' ) |
| 558 |
->where( [ 'job_cmd' => $this->type ] ) |
| 559 |
->caller( __METHOD__ )->execute(); |
| 560 |
} catch ( DBError $e ) { |
| 561 |
throw $this->getDBException( $e ); |
| 562 |
} |
| 563 |
|
| 564 |
return true; |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
protected function doWaitForBackups() { |
| 572 |
if ( $this->server ) { |
| 573 |
return; |
| 574 |
} |
| 575 |
|
| 576 |
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); |
| 577 |
$lbFactory->waitForReplication(); |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
protected function doFlushCaches() { |
| 584 |
foreach ( [ 'size', 'acquiredcount' ] as $type ) { |
| 585 |
$this->wanCache->delete( $this->getCacheKey( $type ) ); |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
public function getAllQueuedJobs() { |
| 594 |
return $this->getJobIterator( [ 'job_cmd' => $this->getType(), 'job_token' => '' ] ); |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
public function getAllAcquiredJobs() { |
| 602 |
$dbr = $this->getReplicaDB(); |
| 603 |
return $this->getJobIterator( [ 'job_cmd' => $this->getType(), $dbr->expr( 'job_token', '>', '' ) ] ); |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
public function getAllAbandonedJobs() { |
| 611 |
$dbr = $this->getReplicaDB(); |
| 612 |
return $this->getJobIterator( [ |
| 613 |
'job_cmd' => $this->getType(), |
| 614 |
$dbr->expr( 'job_token', '>', '' ), |
| 615 |
$dbr->expr( 'job_attempts', '>=', intval( $this->maxTries ) ), |
| 616 |
] ); |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
protected function getJobIterator( array $conds ) { |
| 624 |
$dbr = $this->getReplicaDB(); |
| 625 |
$qb = $dbr->newSelectQueryBuilder() |
| 626 |
->select( self::selectFields() ) |
| 627 |
->from( 'job' ) |
| 628 |
->where( $conds ); |
| 629 |
try { |
| 630 |
return new MappedIterator( |
| 631 |
$qb->caller( __METHOD__ )->fetchResultSet(), |
| 632 |
function ( $row ) { |
| 633 |
return $this->jobFromRow( $row ); |
| 634 |
} |
| 635 |
); |
| 636 |
} catch ( DBError $e ) { |
| 637 |
throw $this->getDBException( $e ); |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
public function getCoalesceLocationInternal() { |
| 642 |
if ( $this->server ) { |
| 643 |
return null; |
| 644 |
} |
| 645 |
|
| 646 |
return is_string( $this->cluster ) |
| 647 |
? "DBCluster:{$this->cluster}:{$this->domain}" |
| 648 |
: "LBFactory:{$this->domain}"; |
| 649 |
} |
| 650 |
|
| 651 |
protected function doGetSiblingQueuesWithJobs( array $types ) { |
| 652 |
$dbr = $this->getReplicaDB(); |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
$res = $dbr->newSelectQueryBuilder() |
| 658 |
->select( 'job_cmd' ) |
| 659 |
->distinct() |
| 660 |
->from( 'job' ) |
| 661 |
->where( [ 'job_cmd' => $types ] ) |
| 662 |
->caller( __METHOD__ )->fetchResultSet(); |
| 663 |
|
| 664 |
$types = []; |
| 665 |
foreach ( $res as $row ) { |
| 666 |
$types[] = $row->job_cmd; |
| 667 |
} |
| 668 |
|
| 669 |
return $types; |
| 670 |
} |
| 671 |
|
| 672 |
protected function doGetSiblingQueueSizes( array $types ) { |
| 673 |
$dbr = $this->getReplicaDB(); |
| 674 |
|
| 675 |
$res = $dbr->newSelectQueryBuilder() |
| 676 |
->select( [ 'job_cmd', 'count' => 'COUNT(*)' ] ) |
| 677 |
->from( 'job' ) |
| 678 |
->where( [ 'job_cmd' => $types ] ) |
| 679 |
->groupBy( 'job_cmd' ) |
| 680 |
->caller( __METHOD__ )->fetchResultSet(); |
| 681 |
|
| 682 |
$sizes = []; |
| 683 |
foreach ( $res as $row ) { |
| 684 |
$sizes[$row->job_cmd] = (int)$row->count; |
| 685 |
} |
| 686 |
|
| 687 |
return $sizes; |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
public function recycleAndDeleteStaleJobs() { |
| 696 |
$now = time(); |
| 697 |
$count = 0; |
| 698 |
$dbw = $this->getPrimaryDB(); |
| 699 |
|
| 700 |
try { |
| 701 |
if ( !$dbw->lock( "jobqueue-recycle-{$this->type}", __METHOD__, 1 ) ) { |
| 702 |
return $count; |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
if ( $this->claimTTL > 0 ) { |
| 707 |
$claimCutoff = $dbw->timestamp( $now - $this->claimTTL ); |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
$res = $dbw->newSelectQueryBuilder() |
| 712 |
->select( 'job_id' ) |
| 713 |
->from( 'job' ) |
| 714 |
->where( |
| 715 |
[ |
| 716 |
'job_cmd' => $this->type, |
| 717 |
$dbw->expr( 'job_token', '!=', '' ), |
| 718 |
$dbw->expr( 'job_token_timestamp', '<', $claimCutoff ), |
| 719 |
$dbw->expr( 'job_attempts', '<', $this->maxTries ), |
| 720 |
] |
| 721 |
) |
| 722 |
->caller( __METHOD__ )->fetchResultSet(); |
| 723 |
$ids = array_map( |
| 724 |
static function ( $o ) { |
| 725 |
return $o->job_id; |
| 726 |
}, iterator_to_array( $res ) |
| 727 |
); |
| 728 |
if ( count( $ids ) ) { |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
$dbw->newUpdateQueryBuilder() |
| 733 |
->update( 'job' ) |
| 734 |
->set( [ |
| 735 |
'job_token' => '', |
| 736 |
'job_token_timestamp' => $dbw->timestamp( $now ) |
| 737 |
] ) |
| 738 |
->where( [ |
| 739 |
'job_id' => $ids, |
| 740 |
$dbw->expr( 'job_token', '!=', '' ), |
| 741 |
] ) |
| 742 |
->caller( __METHOD__ )->execute(); |
| 743 |
|
| 744 |
$affected = $dbw->affectedRows(); |
| 745 |
$count += $affected; |
| 746 |
$this->incrStats( 'recycles', $this->type, $affected ); |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
$pruneCutoff = $dbw->timestamp( $now - self::MAX_AGE_PRUNE ); |
| 752 |
$qb = $dbw->newSelectQueryBuilder() |
| 753 |
->select( 'job_id' ) |
| 754 |
->from( 'job' ) |
| 755 |
->where( |
| 756 |
[ |
| 757 |
'job_cmd' => $this->type, |
| 758 |
$dbw->expr( 'job_token', '!=', '' ), |
| 759 |
$dbw->expr( 'job_token_timestamp', '<', $pruneCutoff ) |
| 760 |
] |
| 761 |
); |
| 762 |
if ( $this->claimTTL > 0 ) { |
| 763 |
$qb->andWhere( $dbw->expr( 'job_attempts', '>=', $this->maxTries ) ); |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
$res = $qb->caller( __METHOD__ )->fetchResultSet(); |
| 768 |
$ids = array_map( |
| 769 |
static function ( $o ) { |
| 770 |
return $o->job_id; |
| 771 |
}, iterator_to_array( $res ) |
| 772 |
); |
| 773 |
if ( count( $ids ) ) { |
| 774 |
$dbw->newDeleteQueryBuilder() |
| 775 |
->deleteFrom( 'job' ) |
| 776 |
->where( [ 'job_id' => $ids ] ) |
| 777 |
->caller( __METHOD__ )->execute(); |
| 778 |
$affected = $dbw->affectedRows(); |
| 779 |
$count += $affected; |
| 780 |
$this->incrStats( 'abandons', $this->type, $affected ); |
| 781 |
} |
| 782 |
|
| 783 |
$dbw->unlock( "jobqueue-recycle-{$this->type}", __METHOD__ ); |
| 784 |
} catch ( DBError $e ) { |
| 785 |
throw $this->getDBException( $e ); |
| 786 |
} |
| 787 |
|
| 788 |
return $count; |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
protected function insertFields( IJobSpecification $job, IReadableDatabase $db ) { |
| 797 |
return [ |
| 798 |
|
| 799 |
'job_cmd' => $job->getType(), |
| 800 |
'job_namespace' => $job->getParams()['namespace'] ?? NS_SPECIAL, |
| 801 |
'job_title' => $job->getParams()['title'] ?? '', |
| 802 |
'job_params' => self::makeBlob( $job->getParams() ), |
| 803 |
|
| 804 |
'job_timestamp' => $db->timestamp(), |
| 805 |
'job_sha1' => Wikimedia\base_convert( |
| 806 |
sha1( serialize( $job->getDeduplicationInfo() ) ), |
| 807 |
16, 36, 31 |
| 808 |
), |
| 809 |
'job_random' => mt_rand( 0, self::MAX_JOB_RANDOM ) |
| 810 |
]; |
| 811 |
} |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
protected function getReplicaDB() { |
| 818 |
try { |
| 819 |
return $this->getDB( DB_REPLICA ); |
| 820 |
} catch ( DBConnectionError $e ) { |
| 821 |
throw new JobQueueConnectionError( "DBConnectionError:" . $e->getMessage() ); |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
protected function getPrimaryDB() { |
| 831 |
try { |
| 832 |
return $this->getDB( DB_PRIMARY ); |
| 833 |
} catch ( DBConnectionError $e ) { |
| 834 |
throw new JobQueueConnectionError( "DBConnectionError:" . $e->getMessage() ); |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
protected function getDB( $index ) { |
| 843 |
if ( $this->server ) { |
| 844 |
if ( $this->conn instanceof IDatabase ) { |
| 845 |
return $this->conn; |
| 846 |
} elseif ( $this->conn instanceof DBError ) { |
| 847 |
throw $this->conn; |
| 848 |
} |
| 849 |
|
| 850 |
try { |
| 851 |
$this->conn = MediaWikiServices::getInstance()->getDatabaseFactory()->create( |
| 852 |
$this->server['type'], |
| 853 |
$this->server |
| 854 |
); |
| 855 |
} catch ( DBError $e ) { |
| 856 |
$this->conn = $e; |
| 857 |
throw $e; |
| 858 |
} |
| 859 |
|
| 860 |
return $this->conn; |
| 861 |
} else { |
| 862 |
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); |
| 863 |
$lb = is_string( $this->cluster ) |
| 864 |
? $lbFactory->getExternalLB( $this->cluster ) |
| 865 |
: $lbFactory->getMainLB( $this->domain ); |
| 866 |
|
| 867 |
if ( $lb->getServerType( ServerInfo::WRITER_INDEX ) !== 'sqlite' ) { |
| 868 |
|
| 869 |
|
| 870 |
$flags = $lb::CONN_TRX_AUTOCOMMIT; |
| 871 |
} else { |
| 872 |
|
| 873 |
$flags = 0; |
| 874 |
} |
| 875 |
|
| 876 |
return $lb->getMaintenanceConnectionRef( $index, [], $this->domain, $flags ); |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
private function getCacheKey( $property ) { |
| 885 |
$cluster = is_string( $this->cluster ) ? $this->cluster : 'main'; |
| 886 |
|
| 887 |
return $this->wanCache->makeGlobalKey( |
| 888 |
'jobqueue', |
| 889 |
$this->domain, |
| 890 |
$cluster, |
| 891 |
$this->type, |
| 892 |
$property |
| 893 |
); |
| 894 |
} |
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
protected static function makeBlob( $params ) { |
| 901 |
if ( $params !== false ) { |
| 902 |
return serialize( $params ); |
| 903 |
} else { |
| 904 |
return ''; |
| 905 |
} |
| 906 |
} |
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
protected function jobFromRow( $row ) { |
| 913 |
$params = ( (string)$row->job_params !== '' ) ? unserialize( $row->job_params ) : []; |
| 914 |
if ( !is_array( $params ) ) { |
| 915 |
throw new UnexpectedValueException( |
| 916 |
"Could not unserialize job with ID '{$row->job_id}'." ); |
| 917 |
} |
| 918 |
|
| 919 |
$params += [ 'namespace' => $row->job_namespace, 'title' => $row->job_title ]; |
| 920 |
$job = $this->factoryJob( $row->job_cmd, $params ); |
| 921 |
$job->setMetadata( 'id', $row->job_id ); |
| 922 |
$job->setMetadata( 'timestamp', $row->job_timestamp ); |
| 923 |
|
| 924 |
return $job; |
| 925 |
} |
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
protected function getDBException( DBError $e ) { |
| 932 |
return new JobQueueError( get_class( $e ) . ": " . $e->getMessage() ); |
| 933 |
} |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
public static function selectFields() { |
| 941 |
return [ |
| 942 |
'job_id', |
| 943 |
'job_cmd', |
| 944 |
'job_namespace', |
| 945 |
'job_title', |
| 946 |
'job_timestamp', |
| 947 |
'job_params', |
| 948 |
'job_random', |
| 949 |
'job_attempts', |
| 950 |
'job_token', |
| 951 |
'job_token_timestamp', |
| 952 |
'job_sha1', |
| 953 |
]; |
| 954 |
} |
| 955 |
} |