| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Deferred\DeferredUpdates; |
| 22 |
use MediaWiki\Deferred\JobQueueEnqueueUpdate; |
| 23 |
use MediaWiki\MediaWikiServices; |
| 24 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 25 |
use Wikimedia\Rdbms\ReadOnlyMode; |
| 26 |
use Wikimedia\Stats\IBufferingStatsdDataFactory; |
| 27 |
use Wikimedia\UUID\GlobalIdGenerator; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class JobQueueGroup { |
| 40 |
|
| 41 |
protected $cache; |
| 42 |
|
| 43 |
|
| 44 |
protected $domain; |
| 45 |
|
| 46 |
protected $readOnlyMode; |
| 47 |
|
| 48 |
private $localJobClasses; |
| 49 |
|
| 50 |
private $jobTypeConfiguration; |
| 51 |
|
| 52 |
private $jobTypesExcludedFromDefaultQueue; |
| 53 |
|
| 54 |
private $statsdDataFactory; |
| 55 |
|
| 56 |
private $wanCache; |
| 57 |
|
| 58 |
private $globalIdGenerator; |
| 59 |
|
| 60 |
|
| 61 |
protected $coalescedQueues; |
| 62 |
|
| 63 |
public const TYPE_DEFAULT = 1; |
| 64 |
private const TYPE_ANY = 2; |
| 65 |
|
| 66 |
public const USE_CACHE = 1; |
| 67 |
|
| 68 |
private const PROC_CACHE_TTL = 15; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function __construct( |
| 84 |
$domain, |
| 85 |
ReadOnlyMode $readOnlyMode, |
| 86 |
?array $localJobClasses, |
| 87 |
array $jobTypeConfiguration, |
| 88 |
array $jobTypesExcludedFromDefaultQueue, |
| 89 |
IBufferingStatsdDataFactory $statsdDataFactory, |
| 90 |
WANObjectCache $wanCache, |
| 91 |
GlobalIdGenerator $globalIdGenerator |
| 92 |
) { |
| 93 |
$this->domain = $domain; |
| 94 |
$this->readOnlyMode = $readOnlyMode; |
| 95 |
$this->cache = new MapCacheLRU( 10 ); |
| 96 |
$this->localJobClasses = $localJobClasses; |
| 97 |
$this->jobTypeConfiguration = $jobTypeConfiguration; |
| 98 |
$this->jobTypesExcludedFromDefaultQueue = $jobTypesExcludedFromDefaultQueue; |
| 99 |
$this->statsdDataFactory = $statsdDataFactory; |
| 100 |
$this->wanCache = $wanCache; |
| 101 |
$this->globalIdGenerator = $globalIdGenerator; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function get( $type ) { |
| 111 |
$conf = [ 'domain' => $this->domain, 'type' => $type ]; |
| 112 |
$conf += $this->jobTypeConfiguration[$type] ?? $this->jobTypeConfiguration['default']; |
| 113 |
if ( !isset( $conf['readOnlyReason'] ) ) { |
| 114 |
$conf['readOnlyReason'] = $this->readOnlyMode->getConfiguredReason(); |
| 115 |
} |
| 116 |
|
| 117 |
$conf['stats'] = $this->statsdDataFactory; |
| 118 |
$conf['wanCache'] = $this->wanCache; |
| 119 |
$conf['idGenerator'] = $this->globalIdGenerator; |
| 120 |
|
| 121 |
return JobQueue::factory( $conf ); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public function push( $jobs ) { |
| 134 |
$jobs = is_array( $jobs ) ? $jobs : [ $jobs ]; |
| 135 |
if ( $jobs === [] ) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
$this->assertValidJobs( $jobs ); |
| 140 |
|
| 141 |
$jobsByType = []; |
| 142 |
foreach ( $jobs as $job ) { |
| 143 |
$type = $job->getType(); |
| 144 |
if ( isset( $this->jobTypeConfiguration[$type] ) ) { |
| 145 |
$jobsByType[$type][] = $job; |
| 146 |
} else { |
| 147 |
if ( |
| 148 |
isset( $this->jobTypeConfiguration['default']['typeAgnostic'] ) && |
| 149 |
$this->jobTypeConfiguration['default']['typeAgnostic'] |
| 150 |
) { |
| 151 |
$jobsByType['default'][] = $job; |
| 152 |
} else { |
| 153 |
$jobsByType[$type][] = $job; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
foreach ( $jobsByType as $type => $jobs ) { |
| 159 |
$this->get( $type )->push( $jobs ); |
| 160 |
} |
| 161 |
|
| 162 |
if ( $this->cache->hasField( 'queues-ready', 'list' ) ) { |
| 163 |
$list = $this->cache->getField( 'queues-ready', 'list' ); |
| 164 |
if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) { |
| 165 |
$this->cache->clear( 'queues-ready' ); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
$cache = MediaWikiServices::getInstance()->getObjectCacheFactory()->getLocalClusterInstance(); |
| 170 |
$cache->set( |
| 171 |
$cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', self::TYPE_ANY ), |
| 172 |
'true', |
| 173 |
15 |
| 174 |
); |
| 175 |
if ( array_diff( array_keys( $jobsByType ), $this->jobTypesExcludedFromDefaultQueue ) ) { |
| 176 |
$cache->set( |
| 177 |
$cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', self::TYPE_DEFAULT ), |
| 178 |
'true', |
| 179 |
15 |
| 180 |
); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public function lazyPush( $jobs ) { |
| 192 |
if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) { |
| 193 |
$this->push( $jobs ); |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
$jobs = is_array( $jobs ) ? $jobs : [ $jobs ]; |
| 198 |
|
| 199 |
|
| 200 |
$this->assertValidJobs( $jobs ); |
| 201 |
|
| 202 |
DeferredUpdates::addUpdate( new JobQueueEnqueueUpdate( $this->domain, $jobs ) ); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0, array $ignored = [] ) { |
| 220 |
$job = false; |
| 221 |
|
| 222 |
if ( !$this->localJobClasses ) { |
| 223 |
throw new JobQueueError( |
| 224 |
"Cannot pop '{$qtype}' job off foreign '{$this->domain}' wiki queue." ); |
| 225 |
} |
| 226 |
if ( is_string( $qtype ) && !isset( $this->localJobClasses[$qtype] ) ) { |
| 227 |
|
| 228 |
throw new JobQueueError( "Unrecognized job type '$qtype'." ); |
| 229 |
} |
| 230 |
|
| 231 |
if ( is_string( $qtype ) ) { |
| 232 |
if ( !in_array( $qtype, $ignored ) ) { |
| 233 |
$job = $this->get( $qtype )->pop(); |
| 234 |
} |
| 235 |
} else { |
| 236 |
if ( $flags & self::USE_CACHE ) { |
| 237 |
if ( !$this->cache->hasField( 'queues-ready', 'list', self::PROC_CACHE_TTL ) ) { |
| 238 |
$this->cache->setField( 'queues-ready', 'list', $this->getQueuesWithJobs() ); |
| 239 |
} |
| 240 |
$types = $this->cache->getField( 'queues-ready', 'list' ); |
| 241 |
} else { |
| 242 |
$types = $this->getQueuesWithJobs(); |
| 243 |
} |
| 244 |
|
| 245 |
if ( $qtype == self::TYPE_DEFAULT ) { |
| 246 |
$types = array_intersect( $types, $this->getDefaultQueueTypes() ); |
| 247 |
} |
| 248 |
|
| 249 |
$types = array_diff( $types, $ignored ); |
| 250 |
shuffle( $types ); |
| 251 |
|
| 252 |
foreach ( $types as $type ) { |
| 253 |
$job = $this->get( $type )->pop(); |
| 254 |
if ( $job ) { |
| 255 |
break; |
| 256 |
} else { |
| 257 |
$this->cache->clear( 'queues-ready' ); |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
return $job; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public function ack( RunnableJob $job ) { |
| 272 |
$this->get( $job->getType() )->ack( $job ); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public function deduplicateRootJob( RunnableJob $job ) { |
| 284 |
wfDeprecated( __METHOD__, '1.40' ); |
| 285 |
return true; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public function waitForBackups() { |
| 298 |
wfDeprecated( __METHOD__, '1.41' ); |
| 299 |
|
| 300 |
foreach ( $this->jobTypeConfiguration as $type => $conf ) { |
| 301 |
$this->get( $type )->waitForBackups(); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
public function getQueueTypes() { |
| 313 |
if ( !$this->localJobClasses ) { |
| 314 |
throw new JobQueueError( 'Cannot inspect job queue from foreign wiki' ); |
| 315 |
} |
| 316 |
return array_keys( $this->localJobClasses ); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
public function getDefaultQueueTypes() { |
| 327 |
return array_diff( $this->getQueueTypes(), $this->jobTypesExcludedFromDefaultQueue ); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
public function queuesHaveJobs( $type = self::TYPE_ANY ) { |
| 340 |
$cache = MediaWikiServices::getInstance()->getObjectCacheFactory()->getLocalClusterInstance(); |
| 341 |
$key = $cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', $type ); |
| 342 |
|
| 343 |
$value = $cache->get( $key ); |
| 344 |
if ( $value === false ) { |
| 345 |
$queues = $this->getQueuesWithJobs(); |
| 346 |
if ( $type == self::TYPE_DEFAULT ) { |
| 347 |
$queues = array_intersect( $queues, $this->getDefaultQueueTypes() ); |
| 348 |
} |
| 349 |
$value = count( $queues ) ? 'true' : 'false'; |
| 350 |
$cache->add( $key, $value, 15 ); |
| 351 |
} |
| 352 |
|
| 353 |
return ( $value === 'true' ); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
public function getQueuesWithJobs() { |
| 364 |
$types = []; |
| 365 |
foreach ( $this->getCoalescedQueues() as $info ) { |
| 366 |
|
| 367 |
$queue = $info['queue']; |
| 368 |
$nonEmpty = $queue->getSiblingQueuesWithJobs( $this->getQueueTypes() ); |
| 369 |
if ( is_array( $nonEmpty ) ) { |
| 370 |
$types = array_merge( $types, $nonEmpty ); |
| 371 |
} else { |
| 372 |
foreach ( $info['types'] as $type ) { |
| 373 |
if ( !$this->get( $type )->isEmpty() ) { |
| 374 |
$types[] = $type; |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
return $types; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
public function getQueueSizes() { |
| 391 |
$sizeMap = []; |
| 392 |
foreach ( $this->getCoalescedQueues() as $info ) { |
| 393 |
|
| 394 |
$queue = $info['queue']; |
| 395 |
$sizes = $queue->getSiblingQueueSizes( $this->getQueueTypes() ); |
| 396 |
if ( is_array( $sizes ) ) { |
| 397 |
$sizeMap += $sizes; |
| 398 |
} else { |
| 399 |
foreach ( $info['types'] as $type ) { |
| 400 |
$sizeMap[$type] = $this->get( $type )->getSize(); |
| 401 |
} |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
return $sizeMap; |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
protected function getCoalescedQueues() { |
| 413 |
if ( $this->coalescedQueues === null ) { |
| 414 |
$this->coalescedQueues = []; |
| 415 |
foreach ( $this->jobTypeConfiguration as $type => $conf ) { |
| 416 |
$conf['domain'] = $this->domain; |
| 417 |
$conf['type'] = 'null'; |
| 418 |
$conf['stats'] = $this->statsdDataFactory; |
| 419 |
$conf['wanCache'] = $this->wanCache; |
| 420 |
$conf['idGenerator'] = $this->globalIdGenerator; |
| 421 |
|
| 422 |
$queue = JobQueue::factory( $conf ); |
| 423 |
$loc = $queue->getCoalesceLocationInternal() ?? ''; |
| 424 |
if ( !isset( $this->coalescedQueues[$loc] ) ) { |
| 425 |
$this->coalescedQueues[$loc]['queue'] = $queue; |
| 426 |
$this->coalescedQueues[$loc]['types'] = []; |
| 427 |
} |
| 428 |
if ( $type === 'default' ) { |
| 429 |
$this->coalescedQueues[$loc]['types'] = array_merge( |
| 430 |
$this->coalescedQueues[$loc]['types'], |
| 431 |
array_diff( $this->getQueueTypes(), array_keys( $this->jobTypeConfiguration ) ) |
| 432 |
); |
| 433 |
} else { |
| 434 |
$this->coalescedQueues[$loc]['types'][] = $type; |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
return $this->coalescedQueues; |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
private function assertValidJobs( array $jobs ) { |
| 446 |
foreach ( $jobs as $job ) { |
| 447 |
if ( !( $job instanceof IJobSpecification ) ) { |
| 448 |
$type = get_debug_type( $job ); |
| 449 |
throw new InvalidArgumentException( "Expected IJobSpecification objects, got " . $type ); |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
} |