| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
class JobQueueFederated extends JobQueue { |
| 47 |
|
| 48 |
protected $partitionRing; |
| 49 |
|
| 50 |
protected $partitionQueues = []; |
| 51 |
|
| 52 |
|
| 53 |
protected $maxPartitionsTry; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
protected function __construct( array $params ) { |
| 72 |
parent::__construct( $params ); |
| 73 |
$section = $params['sectionsByWiki'][$this->domain] ?? 'default'; |
| 74 |
if ( !isset( $params['partitionsBySection'][$section] ) ) { |
| 75 |
throw new InvalidArgumentException( "No configuration for section '$section'." ); |
| 76 |
} |
| 77 |
$this->maxPartitionsTry = $params['maxPartitionsTry'] ?? 2; |
| 78 |
|
| 79 |
$partitionMap = $params['partitionsBySection'][$section]; |
| 80 |
arsort( $partitionMap, SORT_NUMERIC ); |
| 81 |
|
| 82 |
$baseConfig = $params; |
| 83 |
foreach ( [ 'class', 'sectionsByWiki', 'maxPartitionsTry', |
| 84 |
'partitionsBySection', 'configByPartition', ] as $o |
| 85 |
) { |
| 86 |
unset( $baseConfig[$o] ); |
| 87 |
} |
| 88 |
|
| 89 |
foreach ( $partitionMap as $partition => $w ) { |
| 90 |
if ( !isset( $params['configByPartition'][$partition] ) ) { |
| 91 |
throw new InvalidArgumentException( "No configuration for partition '$partition'." ); |
| 92 |
} |
| 93 |
$this->partitionQueues[$partition] = JobQueue::factory( |
| 94 |
$baseConfig + $params['configByPartition'][$partition] ); |
| 95 |
} |
| 96 |
|
| 97 |
$this->partitionRing = new HashRing( $partitionMap ); |
| 98 |
} |
| 99 |
|
| 100 |
protected function supportedOrders() { |
| 101 |
|
| 102 |
return [ 'undefined', 'random', 'timestamp' ]; |
| 103 |
} |
| 104 |
|
| 105 |
protected function optimalOrder() { |
| 106 |
return 'undefined'; |
| 107 |
} |
| 108 |
|
| 109 |
protected function supportsDelayedJobs() { |
| 110 |
foreach ( $this->partitionQueues as $queue ) { |
| 111 |
if ( !$queue->supportsDelayedJobs() ) { |
| 112 |
return false; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
protected function doIsEmpty() { |
| 120 |
$empty = true; |
| 121 |
$failed = 0; |
| 122 |
foreach ( $this->partitionQueues as $queue ) { |
| 123 |
try { |
| 124 |
$empty = $empty && $queue->doIsEmpty(); |
| 125 |
} catch ( JobQueueError $e ) { |
| 126 |
++$failed; |
| 127 |
$this->logException( $e ); |
| 128 |
} |
| 129 |
} |
| 130 |
$this->throwErrorIfAllPartitionsDown( $failed ); |
| 131 |
|
| 132 |
return $empty; |
| 133 |
} |
| 134 |
|
| 135 |
protected function doGetSize() { |
| 136 |
return $this->getCrossPartitionSum( 'size', 'doGetSize' ); |
| 137 |
} |
| 138 |
|
| 139 |
protected function doGetAcquiredCount() { |
| 140 |
return $this->getCrossPartitionSum( 'acquiredcount', 'doGetAcquiredCount' ); |
| 141 |
} |
| 142 |
|
| 143 |
protected function doGetDelayedCount() { |
| 144 |
return $this->getCrossPartitionSum( 'delayedcount', 'doGetDelayedCount' ); |
| 145 |
} |
| 146 |
|
| 147 |
protected function doGetAbandonedCount() { |
| 148 |
return $this->getCrossPartitionSum( 'abandonedcount', 'doGetAbandonedCount' ); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
protected function getCrossPartitionSum( $type, $method ) { |
| 157 |
$count = 0; |
| 158 |
$failed = 0; |
| 159 |
foreach ( $this->partitionQueues as $queue ) { |
| 160 |
try { |
| 161 |
$count += $queue->$method(); |
| 162 |
} catch ( JobQueueError $e ) { |
| 163 |
++$failed; |
| 164 |
$this->logException( $e ); |
| 165 |
} |
| 166 |
} |
| 167 |
$this->throwErrorIfAllPartitionsDown( $failed ); |
| 168 |
|
| 169 |
return $count; |
| 170 |
} |
| 171 |
|
| 172 |
protected function doBatchPush( array $jobs, $flags ) { |
| 173 |
|
| 174 |
$partitionRing = $this->partitionRing; |
| 175 |
|
| 176 |
|
| 177 |
$jobsLeft = $jobs; |
| 178 |
for ( $i = $this->maxPartitionsTry; $i > 0 && count( $jobsLeft ); --$i ) { |
| 179 |
try { |
| 180 |
$partitionRing->getLiveLocationWeights(); |
| 181 |
} catch ( UnexpectedValueException $e ) { |
| 182 |
break; |
| 183 |
} |
| 184 |
$jobsLeft = $this->tryJobInsertions( $jobsLeft, $partitionRing, $flags ); |
| 185 |
} |
| 186 |
if ( count( $jobsLeft ) ) { |
| 187 |
throw new JobQueueError( |
| 188 |
"Could not insert job(s), {$this->maxPartitionsTry} partitions tried." ); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
protected function tryJobInsertions( array $jobs, HashRing &$partitionRing, $flags ) { |
| 200 |
$jobsLeft = []; |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
$uJobsByPartition = []; |
| 206 |
|
| 207 |
foreach ( $jobs as $key => $job ) { |
| 208 |
if ( $job->ignoreDuplicates() ) { |
| 209 |
$sha1 = sha1( serialize( $job->getDeduplicationInfo() ) ); |
| 210 |
$uJobsByPartition[$partitionRing->getLiveLocation( $sha1 )][] = $job; |
| 211 |
unset( $jobs[$key] ); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
if ( $flags & self::QOS_ATOMIC ) { |
| 216 |
$nuJobBatches = [ $jobs ]; |
| 217 |
} else { |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
$nuJobBatches = array_chunk( $jobs, 300 ); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
foreach ( $uJobsByPartition as $partition => $jobBatch ) { |
| 226 |
|
| 227 |
$queue = $this->partitionQueues[$partition]; |
| 228 |
try { |
| 229 |
$ok = true; |
| 230 |
$queue->doBatchPush( $jobBatch, $flags | self::QOS_ATOMIC ); |
| 231 |
} catch ( JobQueueError $e ) { |
| 232 |
$ok = false; |
| 233 |
$this->logException( $e ); |
| 234 |
} |
| 235 |
if ( !$ok ) { |
| 236 |
if ( !$partitionRing->ejectFromLiveRing( $partition, 5 ) ) { |
| 237 |
throw new JobQueueError( "Could not insert job(s), no partitions available." ); |
| 238 |
} |
| 239 |
$jobsLeft = array_merge( $jobsLeft, $jobBatch ); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
foreach ( $nuJobBatches as $jobBatch ) { |
| 245 |
$partition = ArrayUtils::pickRandom( $partitionRing->getLiveLocationWeights() ); |
| 246 |
$queue = $this->partitionQueues[$partition]; |
| 247 |
try { |
| 248 |
$ok = true; |
| 249 |
$queue->doBatchPush( $jobBatch, $flags | self::QOS_ATOMIC ); |
| 250 |
} catch ( JobQueueError $e ) { |
| 251 |
$ok = false; |
| 252 |
$this->logException( $e ); |
| 253 |
} |
| 254 |
if ( !$ok ) { |
| 255 |
if ( !$partitionRing->ejectFromLiveRing( $partition, 5 ) ) { |
| 256 |
throw new JobQueueError( "Could not insert job(s), no partitions available." ); |
| 257 |
} |
| 258 |
$jobsLeft = array_merge( $jobsLeft, $jobBatch ); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
return $jobsLeft; |
| 263 |
} |
| 264 |
|
| 265 |
protected function doPop() { |
| 266 |
$partitionsTry = $this->partitionRing->getLiveLocationWeights(); |
| 267 |
|
| 268 |
$failed = 0; |
| 269 |
while ( count( $partitionsTry ) ) { |
| 270 |
$partition = ArrayUtils::pickRandom( $partitionsTry ); |
| 271 |
if ( $partition === false ) { |
| 272 |
break; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
$queue = $this->partitionQueues[$partition]; |
| 277 |
try { |
| 278 |
$job = $queue->pop(); |
| 279 |
} catch ( JobQueueError $e ) { |
| 280 |
++$failed; |
| 281 |
$this->logException( $e ); |
| 282 |
$job = false; |
| 283 |
} |
| 284 |
if ( $job ) { |
| 285 |
$job->setMetadata( 'QueuePartition', $partition ); |
| 286 |
|
| 287 |
return $job; |
| 288 |
} else { |
| 289 |
unset( $partitionsTry[$partition] ); |
| 290 |
} |
| 291 |
} |
| 292 |
$this->throwErrorIfAllPartitionsDown( $failed ); |
| 293 |
|
| 294 |
return false; |
| 295 |
} |
| 296 |
|
| 297 |
protected function doAck( RunnableJob $job ) { |
| 298 |
$partition = $job->getMetadata( 'QueuePartition' ); |
| 299 |
if ( $partition === null ) { |
| 300 |
throw new UnexpectedValueException( "The given job has no defined partition name." ); |
| 301 |
} |
| 302 |
|
| 303 |
$this->partitionQueues[$partition]->ack( $job ); |
| 304 |
} |
| 305 |
|
| 306 |
protected function doIsRootJobOldDuplicate( IJobSpecification $job ) { |
| 307 |
$signature = $job->getRootJobParams()['rootJobSignature']; |
| 308 |
$partition = $this->partitionRing->getLiveLocation( $signature ); |
| 309 |
try { |
| 310 |
return $this->partitionQueues[$partition]->doIsRootJobOldDuplicate( $job ); |
| 311 |
} catch ( JobQueueError $e ) { |
| 312 |
if ( $this->partitionRing->ejectFromLiveRing( $partition, 5 ) ) { |
| 313 |
$partition = $this->partitionRing->getLiveLocation( $signature ); |
| 314 |
return $this->partitionQueues[$partition]->doIsRootJobOldDuplicate( $job ); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
return false; |
| 319 |
} |
| 320 |
|
| 321 |
protected function doDeduplicateRootJob( IJobSpecification $job ) { |
| 322 |
$signature = $job->getRootJobParams()['rootJobSignature']; |
| 323 |
$partition = $this->partitionRing->getLiveLocation( $signature ); |
| 324 |
try { |
| 325 |
return $this->partitionQueues[$partition]->doDeduplicateRootJob( $job ); |
| 326 |
} catch ( JobQueueError $e ) { |
| 327 |
if ( $this->partitionRing->ejectFromLiveRing( $partition, 5 ) ) { |
| 328 |
$partition = $this->partitionRing->getLiveLocation( $signature ); |
| 329 |
return $this->partitionQueues[$partition]->doDeduplicateRootJob( $job ); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
return false; |
| 334 |
} |
| 335 |
|
| 336 |
protected function doDelete() { |
| 337 |
$failed = 0; |
| 338 |
|
| 339 |
foreach ( $this->partitionQueues as $queue ) { |
| 340 |
try { |
| 341 |
$queue->doDelete(); |
| 342 |
} catch ( JobQueueError $e ) { |
| 343 |
++$failed; |
| 344 |
$this->logException( $e ); |
| 345 |
} |
| 346 |
} |
| 347 |
$this->throwErrorIfAllPartitionsDown( $failed ); |
| 348 |
return true; |
| 349 |
} |
| 350 |
|
| 351 |
protected function doWaitForBackups() { |
| 352 |
$failed = 0; |
| 353 |
|
| 354 |
foreach ( $this->partitionQueues as $queue ) { |
| 355 |
try { |
| 356 |
$queue->waitForBackups(); |
| 357 |
} catch ( JobQueueError $e ) { |
| 358 |
++$failed; |
| 359 |
$this->logException( $e ); |
| 360 |
} |
| 361 |
} |
| 362 |
$this->throwErrorIfAllPartitionsDown( $failed ); |
| 363 |
} |
| 364 |
|
| 365 |
protected function doFlushCaches() { |
| 366 |
|
| 367 |
foreach ( $this->partitionQueues as $queue ) { |
| 368 |
$queue->doFlushCaches(); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
public function getAllQueuedJobs() { |
| 373 |
$iterator = new AppendIterator(); |
| 374 |
|
| 375 |
|
| 376 |
foreach ( $this->partitionQueues as $queue ) { |
| 377 |
$iterator->append( $queue->getAllQueuedJobs() ); |
| 378 |
} |
| 379 |
|
| 380 |
return $iterator; |
| 381 |
} |
| 382 |
|
| 383 |
public function getAllDelayedJobs() { |
| 384 |
$iterator = new AppendIterator(); |
| 385 |
|
| 386 |
|
| 387 |
foreach ( $this->partitionQueues as $queue ) { |
| 388 |
$iterator->append( $queue->getAllDelayedJobs() ); |
| 389 |
} |
| 390 |
|
| 391 |
return $iterator; |
| 392 |
} |
| 393 |
|
| 394 |
public function getAllAcquiredJobs() { |
| 395 |
$iterator = new AppendIterator(); |
| 396 |
|
| 397 |
|
| 398 |
foreach ( $this->partitionQueues as $queue ) { |
| 399 |
$iterator->append( $queue->getAllAcquiredJobs() ); |
| 400 |
} |
| 401 |
|
| 402 |
return $iterator; |
| 403 |
} |
| 404 |
|
| 405 |
public function getAllAbandonedJobs() { |
| 406 |
$iterator = new AppendIterator(); |
| 407 |
|
| 408 |
|
| 409 |
foreach ( $this->partitionQueues as $queue ) { |
| 410 |
$iterator->append( $queue->getAllAbandonedJobs() ); |
| 411 |
} |
| 412 |
|
| 413 |
return $iterator; |
| 414 |
} |
| 415 |
|
| 416 |
public function getCoalesceLocationInternal() { |
| 417 |
return "JobQueueFederated:wiki:{$this->domain}" . |
| 418 |
sha1( serialize( array_keys( $this->partitionQueues ) ) ); |
| 419 |
} |
| 420 |
|
| 421 |
protected function doGetSiblingQueuesWithJobs( array $types ) { |
| 422 |
$result = []; |
| 423 |
|
| 424 |
$failed = 0; |
| 425 |
|
| 426 |
foreach ( $this->partitionQueues as $queue ) { |
| 427 |
try { |
| 428 |
$nonEmpty = $queue->doGetSiblingQueuesWithJobs( $types ); |
| 429 |
if ( is_array( $nonEmpty ) ) { |
| 430 |
$result = array_unique( array_merge( $result, $nonEmpty ) ); |
| 431 |
} else { |
| 432 |
return null; |
| 433 |
} |
| 434 |
if ( count( $result ) == count( $types ) ) { |
| 435 |
break; |
| 436 |
} |
| 437 |
} catch ( JobQueueError $e ) { |
| 438 |
++$failed; |
| 439 |
$this->logException( $e ); |
| 440 |
} |
| 441 |
} |
| 442 |
$this->throwErrorIfAllPartitionsDown( $failed ); |
| 443 |
|
| 444 |
return array_values( $result ); |
| 445 |
} |
| 446 |
|
| 447 |
protected function doGetSiblingQueueSizes( array $types ) { |
| 448 |
$result = []; |
| 449 |
$failed = 0; |
| 450 |
|
| 451 |
foreach ( $this->partitionQueues as $queue ) { |
| 452 |
try { |
| 453 |
$sizes = $queue->doGetSiblingQueueSizes( $types ); |
| 454 |
if ( is_array( $sizes ) ) { |
| 455 |
foreach ( $sizes as $type => $size ) { |
| 456 |
$result[$type] = ( $result[$type] ?? 0 ) + $size; |
| 457 |
} |
| 458 |
} else { |
| 459 |
return null; |
| 460 |
} |
| 461 |
} catch ( JobQueueError $e ) { |
| 462 |
++$failed; |
| 463 |
$this->logException( $e ); |
| 464 |
} |
| 465 |
} |
| 466 |
$this->throwErrorIfAllPartitionsDown( $failed ); |
| 467 |
|
| 468 |
return $result; |
| 469 |
} |
| 470 |
|
| 471 |
protected function logException( Exception $e ) { |
| 472 |
wfDebugLog( 'JobQueue', $e->getMessage() . "\n" . $e->getTraceAsString() ); |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
protected function throwErrorIfAllPartitionsDown( $down ) { |
| 483 |
if ( $down >= count( $this->partitionQueues ) ) { |
| 484 |
throw new JobQueueError( 'No queue partitions available.' ); |
| 485 |
} |
| 486 |
} |
| 487 |
} |