JobQueueGroup.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php
  2. /**
  3. * Job queue base code.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. use MediaWiki\MediaWikiServices;
  23. /**
  24. * Class to handle enqueueing of background jobs
  25. *
  26. * @ingroup JobQueue
  27. * @since 1.21
  28. */
  29. class JobQueueGroup {
  30. /** @var JobQueueGroup[] */
  31. protected static $instances = [];
  32. /** @var MapCacheLRU */
  33. protected $cache;
  34. /** @var string Wiki domain ID */
  35. protected $domain;
  36. /** @var string|bool Read only rationale (or false if r/w) */
  37. protected $readOnlyReason;
  38. /** @var bool Whether the wiki is not recognized in configuration */
  39. protected $invalidDomain = false;
  40. /** @var array Map of (bucket => (queue => JobQueue, types => list of types) */
  41. protected $coalescedQueues;
  42. const TYPE_DEFAULT = 1; // integer; jobs popped by default
  43. const TYPE_ANY = 2; // integer; any job
  44. const USE_CACHE = 1; // integer; use process or persistent cache
  45. const PROC_CACHE_TTL = 15; // integer; seconds
  46. const CACHE_VERSION = 1; // integer; cache version
  47. /**
  48. * @param string $domain Wiki domain ID
  49. * @param string|bool $readOnlyReason Read-only reason or false
  50. */
  51. protected function __construct( $domain, $readOnlyReason ) {
  52. $this->domain = $domain;
  53. $this->readOnlyReason = $readOnlyReason;
  54. $this->cache = new MapCacheLRU( 10 );
  55. }
  56. /**
  57. * @param bool|string $domain Wiki domain ID
  58. * @return JobQueueGroup
  59. */
  60. public static function singleton( $domain = false ) {
  61. global $wgLocalDatabases;
  62. if ( $domain === false ) {
  63. $domain = WikiMap::getCurrentWikiDbDomain()->getId();
  64. }
  65. if ( !isset( self::$instances[$domain] ) ) {
  66. self::$instances[$domain] = new self( $domain, wfConfiguredReadOnlyReason() );
  67. // Make sure jobs are not getting pushed to bogus wikis. This can confuse
  68. // the job runner system into spawning endless RPC requests that fail (T171371).
  69. $wikiId = WikiMap::getWikiIdFromDbDomain( $domain );
  70. if (
  71. !WikiMap::isCurrentWikiDbDomain( $domain ) &&
  72. !in_array( $wikiId, $wgLocalDatabases )
  73. ) {
  74. self::$instances[$domain]->invalidDomain = true;
  75. }
  76. }
  77. return self::$instances[$domain];
  78. }
  79. /**
  80. * Destroy the singleton instances
  81. *
  82. * @return void
  83. */
  84. public static function destroySingletons() {
  85. self::$instances = [];
  86. }
  87. /**
  88. * Get the job queue object for a given queue type
  89. *
  90. * @param string $type
  91. * @return JobQueue
  92. */
  93. public function get( $type ) {
  94. global $wgJobTypeConf;
  95. $conf = [ 'domain' => $this->domain, 'type' => $type ];
  96. if ( isset( $wgJobTypeConf[$type] ) ) {
  97. $conf = $conf + $wgJobTypeConf[$type];
  98. } else {
  99. $conf = $conf + $wgJobTypeConf['default'];
  100. }
  101. if ( !isset( $conf['readOnlyReason'] ) ) {
  102. $conf['readOnlyReason'] = $this->readOnlyReason;
  103. }
  104. $services = MediaWikiServices::getInstance();
  105. $conf['stats'] = $services->getStatsdDataFactory();
  106. $conf['wanCache'] = $services->getMainWANObjectCache();
  107. return JobQueue::factory( $conf );
  108. }
  109. /**
  110. * Insert jobs into the respective queues of which they belong
  111. *
  112. * This inserts the jobs into the queue specified by $wgJobTypeConf
  113. * and updates the aggregate job queue information cache as needed.
  114. *
  115. * @param IJobSpecification|IJobSpecification[] $jobs A single Job or a list of Jobs
  116. * @throws InvalidArgumentException
  117. * @return void
  118. */
  119. public function push( $jobs ) {
  120. global $wgJobTypesExcludedFromDefaultQueue;
  121. if ( $this->invalidDomain ) {
  122. // Do not enqueue job that cannot be run (T171371)
  123. $e = new LogicException( "Domain '{$this->domain}' is not recognized." );
  124. MWExceptionHandler::logException( $e );
  125. return;
  126. }
  127. $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
  128. if ( $jobs === [] ) {
  129. return;
  130. }
  131. $this->assertValidJobs( $jobs );
  132. $jobsByType = []; // (job type => list of jobs)
  133. foreach ( $jobs as $job ) {
  134. $jobsByType[$job->getType()][] = $job;
  135. }
  136. foreach ( $jobsByType as $type => $jobs ) {
  137. $this->get( $type )->push( $jobs );
  138. }
  139. if ( $this->cache->hasField( 'queues-ready', 'list' ) ) {
  140. $list = $this->cache->getField( 'queues-ready', 'list' );
  141. if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
  142. $this->cache->clear( 'queues-ready' );
  143. }
  144. }
  145. $cache = ObjectCache::getLocalClusterInstance();
  146. $cache->set(
  147. $cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', self::TYPE_ANY ),
  148. 'true',
  149. 15
  150. );
  151. if ( array_diff( array_keys( $jobsByType ), $wgJobTypesExcludedFromDefaultQueue ) ) {
  152. $cache->set(
  153. $cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', self::TYPE_DEFAULT ),
  154. 'true',
  155. 15
  156. );
  157. }
  158. }
  159. /**
  160. * Buffer jobs for insertion via push() or call it now if in CLI mode
  161. *
  162. * @param IJobSpecification|IJobSpecification[] $jobs A single Job or a list of Jobs
  163. * @return void
  164. * @since 1.26
  165. */
  166. public function lazyPush( $jobs ) {
  167. if ( $this->invalidDomain ) {
  168. // Do not enqueue job that cannot be run (T171371)
  169. throw new LogicException( "Domain '{$this->domain}' is not recognized." );
  170. }
  171. if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) {
  172. $this->push( $jobs );
  173. return;
  174. }
  175. $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
  176. // Throw errors now instead of on push(), when other jobs may be buffered
  177. $this->assertValidJobs( $jobs );
  178. DeferredUpdates::addUpdate( new JobQueueEnqueueUpdate( $this->domain, $jobs ) );
  179. }
  180. /**
  181. * Pop a job off one of the job queues
  182. *
  183. * This pops a job off a queue as specified by $wgJobTypeConf and
  184. * updates the aggregate job queue information cache as needed.
  185. *
  186. * @param int|string $qtype JobQueueGroup::TYPE_* constant or job type string
  187. * @param int $flags Bitfield of JobQueueGroup::USE_* constants
  188. * @param array $blacklist List of job types to ignore
  189. * @return RunnableJob|bool Returns false on failure
  190. */
  191. public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0, array $blacklist = [] ) {
  192. global $wgJobClasses;
  193. $job = false;
  194. if ( !WikiMap::isCurrentWikiDbDomain( $this->domain ) ) {
  195. throw new JobQueueError(
  196. "Cannot pop '{$qtype}' job off foreign '{$this->domain}' wiki queue." );
  197. } elseif ( is_string( $qtype ) && !isset( $wgJobClasses[$qtype] ) ) {
  198. // Do not pop jobs if there is no class for the queue type
  199. throw new JobQueueError( "Unrecognized job type '$qtype'." );
  200. }
  201. if ( is_string( $qtype ) ) { // specific job type
  202. if ( !in_array( $qtype, $blacklist ) ) {
  203. $job = $this->get( $qtype )->pop();
  204. }
  205. } else { // any job in the "default" jobs types
  206. if ( $flags & self::USE_CACHE ) {
  207. if ( !$this->cache->hasField( 'queues-ready', 'list', self::PROC_CACHE_TTL ) ) {
  208. $this->cache->setField( 'queues-ready', 'list', $this->getQueuesWithJobs() );
  209. }
  210. $types = $this->cache->getField( 'queues-ready', 'list' );
  211. } else {
  212. $types = $this->getQueuesWithJobs();
  213. }
  214. if ( $qtype == self::TYPE_DEFAULT ) {
  215. $types = array_intersect( $types, $this->getDefaultQueueTypes() );
  216. }
  217. $types = array_diff( $types, $blacklist ); // avoid selected types
  218. shuffle( $types ); // avoid starvation
  219. foreach ( $types as $type ) { // for each queue...
  220. $job = $this->get( $type )->pop();
  221. if ( $job ) { // found
  222. break;
  223. } else { // not found
  224. $this->cache->clear( 'queues-ready' );
  225. }
  226. }
  227. }
  228. return $job;
  229. }
  230. /**
  231. * Acknowledge that a job was completed
  232. *
  233. * @param RunnableJob $job
  234. * @return void
  235. */
  236. public function ack( RunnableJob $job ) {
  237. $this->get( $job->getType() )->ack( $job );
  238. }
  239. /**
  240. * Register the "root job" of a given job into the queue for de-duplication.
  241. * This should only be called right *after* all the new jobs have been inserted.
  242. *
  243. * @param RunnableJob $job
  244. * @return bool
  245. */
  246. public function deduplicateRootJob( RunnableJob $job ) {
  247. return $this->get( $job->getType() )->deduplicateRootJob( $job );
  248. }
  249. /**
  250. * Wait for any replica DBs or backup queue servers to catch up.
  251. *
  252. * This does nothing for certain queue classes.
  253. *
  254. * @return void
  255. */
  256. public function waitForBackups() {
  257. global $wgJobTypeConf;
  258. // Try to avoid doing this more than once per queue storage medium
  259. foreach ( $wgJobTypeConf as $type => $conf ) {
  260. $this->get( $type )->waitForBackups();
  261. }
  262. }
  263. /**
  264. * Get the list of queue types
  265. *
  266. * @return array List of strings
  267. */
  268. public function getQueueTypes() {
  269. return array_keys( $this->getCachedConfigVar( 'wgJobClasses' ) );
  270. }
  271. /**
  272. * Get the list of default queue types
  273. *
  274. * @return array List of strings
  275. */
  276. public function getDefaultQueueTypes() {
  277. global $wgJobTypesExcludedFromDefaultQueue;
  278. return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue );
  279. }
  280. /**
  281. * Check if there are any queues with jobs (this is cached)
  282. *
  283. * @param int $type JobQueueGroup::TYPE_* constant
  284. * @return bool
  285. * @since 1.23
  286. */
  287. public function queuesHaveJobs( $type = self::TYPE_ANY ) {
  288. $cache = ObjectCache::getLocalClusterInstance();
  289. $key = $cache->makeGlobalKey( 'jobqueue', $this->domain, 'hasjobs', $type );
  290. $value = $cache->get( $key );
  291. if ( $value === false ) {
  292. $queues = $this->getQueuesWithJobs();
  293. if ( $type == self::TYPE_DEFAULT ) {
  294. $queues = array_intersect( $queues, $this->getDefaultQueueTypes() );
  295. }
  296. $value = count( $queues ) ? 'true' : 'false';
  297. $cache->add( $key, $value, 15 );
  298. }
  299. return ( $value === 'true' );
  300. }
  301. /**
  302. * Get the list of job types that have non-empty queues
  303. *
  304. * @return string[] List of job types that have non-empty queues
  305. */
  306. public function getQueuesWithJobs() {
  307. $types = [];
  308. foreach ( $this->getCoalescedQueues() as $info ) {
  309. /** @var JobQueue $queue */
  310. $queue = $info['queue'];
  311. $nonEmpty = $queue->getSiblingQueuesWithJobs( $this->getQueueTypes() );
  312. if ( is_array( $nonEmpty ) ) { // batching features supported
  313. $types = array_merge( $types, $nonEmpty );
  314. } else { // we have to go through the queues in the bucket one-by-one
  315. foreach ( $info['types'] as $type ) {
  316. if ( !$this->get( $type )->isEmpty() ) {
  317. $types[] = $type;
  318. }
  319. }
  320. }
  321. }
  322. return $types;
  323. }
  324. /**
  325. * Get the size of the queus for a list of job types
  326. *
  327. * @return int[] Map of (job type => size)
  328. */
  329. public function getQueueSizes() {
  330. $sizeMap = [];
  331. foreach ( $this->getCoalescedQueues() as $info ) {
  332. /** @var JobQueue $queue */
  333. $queue = $info['queue'];
  334. $sizes = $queue->getSiblingQueueSizes( $this->getQueueTypes() );
  335. if ( is_array( $sizes ) ) { // batching features supported
  336. $sizeMap = $sizeMap + $sizes;
  337. } else { // we have to go through the queues in the bucket one-by-one
  338. foreach ( $info['types'] as $type ) {
  339. $sizeMap[$type] = $this->get( $type )->getSize();
  340. }
  341. }
  342. }
  343. return $sizeMap;
  344. }
  345. /**
  346. * @return array[]
  347. * @phan-return array<string,array{queue:JobQueue,types:array<string,class-string>}>
  348. */
  349. protected function getCoalescedQueues() {
  350. global $wgJobTypeConf;
  351. if ( $this->coalescedQueues === null ) {
  352. $this->coalescedQueues = [];
  353. foreach ( $wgJobTypeConf as $type => $conf ) {
  354. $queue = JobQueue::factory(
  355. [ 'domain' => $this->domain, 'type' => 'null' ] + $conf );
  356. $loc = $queue->getCoalesceLocationInternal();
  357. if ( !isset( $this->coalescedQueues[$loc] ) ) {
  358. $this->coalescedQueues[$loc]['queue'] = $queue;
  359. $this->coalescedQueues[$loc]['types'] = [];
  360. }
  361. if ( $type === 'default' ) {
  362. $this->coalescedQueues[$loc]['types'] = array_merge(
  363. $this->coalescedQueues[$loc]['types'],
  364. array_diff( $this->getQueueTypes(), array_keys( $wgJobTypeConf ) )
  365. );
  366. } else {
  367. $this->coalescedQueues[$loc]['types'][] = $type;
  368. }
  369. }
  370. }
  371. return $this->coalescedQueues;
  372. }
  373. /**
  374. * @param string $name
  375. * @return mixed
  376. */
  377. private function getCachedConfigVar( $name ) {
  378. // @TODO: cleanup this whole method with a proper config system
  379. if ( WikiMap::isCurrentWikiDbDomain( $this->domain ) ) {
  380. return $GLOBALS[$name]; // common case
  381. } else {
  382. $wiki = WikiMap::getWikiIdFromDbDomain( $this->domain );
  383. $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
  384. $value = $cache->getWithSetCallback(
  385. $cache->makeGlobalKey( 'jobqueue', 'configvalue', $this->domain, $name ),
  386. $cache::TTL_DAY + mt_rand( 0, $cache::TTL_DAY ),
  387. function () use ( $wiki, $name ) {
  388. global $wgConf;
  389. // @TODO: use the full domain ID here
  390. return [ 'v' => $wgConf->getConfig( $wiki, $name ) ];
  391. },
  392. [ 'pcTTL' => WANObjectCache::TTL_PROC_LONG ]
  393. );
  394. return $value['v'];
  395. }
  396. }
  397. /**
  398. * @param array $jobs
  399. * @throws InvalidArgumentException
  400. */
  401. private function assertValidJobs( array $jobs ) {
  402. foreach ( $jobs as $job ) { // sanity checks
  403. if ( !( $job instanceof IJobSpecification ) ) {
  404. throw new InvalidArgumentException( "Expected IJobSpecification objects" );
  405. }
  406. }
  407. }
  408. }