JobQueueGroup.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. /**
  23. * Class to handle enqueueing of background jobs
  24. *
  25. * @ingroup JobQueue
  26. * @since 1.21
  27. */
  28. class JobQueueGroup {
  29. /** @var JobQueueGroup[] */
  30. protected static $instances = [];
  31. /** @var ProcessCacheLRU */
  32. protected $cache;
  33. /** @var string Wiki ID */
  34. protected $wiki;
  35. /** @var string|bool Read only rationale (or false if r/w) */
  36. protected $readOnlyReason;
  37. /** @var bool Whether the wiki is not recognized in configuration */
  38. protected $invalidWiki = false;
  39. /** @var array Map of (bucket => (queue => JobQueue, types => list of types) */
  40. protected $coalescedQueues;
  41. /** @var Job[] */
  42. protected $bufferedJobs = [];
  43. const TYPE_DEFAULT = 1; // integer; jobs popped by default
  44. const TYPE_ANY = 2; // integer; any job
  45. const USE_CACHE = 1; // integer; use process or persistent cache
  46. const PROC_CACHE_TTL = 15; // integer; seconds
  47. const CACHE_VERSION = 1; // integer; cache version
  48. /**
  49. * @param string $wiki Wiki ID
  50. * @param string|bool $readOnlyReason Read-only reason or false
  51. */
  52. protected function __construct( $wiki, $readOnlyReason ) {
  53. $this->wiki = $wiki;
  54. $this->readOnlyReason = $readOnlyReason;
  55. $this->cache = new ProcessCacheLRU( 10 );
  56. }
  57. /**
  58. * @param bool|string $wiki Wiki ID
  59. * @return JobQueueGroup
  60. */
  61. public static function singleton( $wiki = false ) {
  62. global $wgLocalDatabases;
  63. $wiki = ( $wiki === false ) ? wfWikiID() : $wiki;
  64. if ( !isset( self::$instances[$wiki] ) ) {
  65. self::$instances[$wiki] = new self( $wiki, wfConfiguredReadOnlyReason() );
  66. // Make sure jobs are not getting pushed to bogus wikis. This can confuse
  67. // the job runner system into spawning endless RPC requests that fail (T171371).
  68. if ( $wiki !== wfWikiID() && !in_array( $wiki, $wgLocalDatabases ) ) {
  69. self::$instances[$wiki]->invalidWiki = true;
  70. }
  71. }
  72. return self::$instances[$wiki];
  73. }
  74. /**
  75. * Destroy the singleton instances
  76. *
  77. * @return void
  78. */
  79. public static function destroySingletons() {
  80. self::$instances = [];
  81. }
  82. /**
  83. * Get the job queue object for a given queue type
  84. *
  85. * @param string $type
  86. * @return JobQueue
  87. */
  88. public function get( $type ) {
  89. global $wgJobTypeConf;
  90. $conf = [ 'wiki' => $this->wiki, 'type' => $type ];
  91. if ( isset( $wgJobTypeConf[$type] ) ) {
  92. $conf = $conf + $wgJobTypeConf[$type];
  93. } else {
  94. $conf = $conf + $wgJobTypeConf['default'];
  95. }
  96. $conf['aggregator'] = JobQueueAggregator::singleton();
  97. if ( $this->readOnlyReason !== false ) {
  98. $conf['readOnlyReason'] = $this->readOnlyReason;
  99. }
  100. return JobQueue::factory( $conf );
  101. }
  102. /**
  103. * Insert jobs into the respective queues of which they belong
  104. *
  105. * This inserts the jobs into the queue specified by $wgJobTypeConf
  106. * and updates the aggregate job queue information cache as needed.
  107. *
  108. * @param IJobSpecification|IJobSpecification[] $jobs A single Job or a list of Jobs
  109. * @throws InvalidArgumentException
  110. * @return void
  111. */
  112. public function push( $jobs ) {
  113. global $wgJobTypesExcludedFromDefaultQueue;
  114. if ( $this->invalidWiki ) {
  115. // Do not enqueue job that cannot be run (T171371)
  116. $e = new LogicException( "Domain '{$this->wiki}' is not recognized." );
  117. MWExceptionHandler::logException( $e );
  118. return;
  119. }
  120. $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
  121. if ( !count( $jobs ) ) {
  122. return;
  123. }
  124. $this->assertValidJobs( $jobs );
  125. $jobsByType = []; // (job type => list of jobs)
  126. foreach ( $jobs as $job ) {
  127. $jobsByType[$job->getType()][] = $job;
  128. }
  129. foreach ( $jobsByType as $type => $jobs ) {
  130. $this->get( $type )->push( $jobs );
  131. }
  132. if ( $this->cache->has( 'queues-ready', 'list' ) ) {
  133. $list = $this->cache->get( 'queues-ready', 'list' );
  134. if ( count( array_diff( array_keys( $jobsByType ), $list ) ) ) {
  135. $this->cache->clear( 'queues-ready' );
  136. }
  137. }
  138. $cache = ObjectCache::getLocalClusterInstance();
  139. $cache->set(
  140. $cache->makeGlobalKey( 'jobqueue', $this->wiki, 'hasjobs', self::TYPE_ANY ),
  141. 'true',
  142. 15
  143. );
  144. if ( array_diff( array_keys( $jobsByType ), $wgJobTypesExcludedFromDefaultQueue ) ) {
  145. $cache->set(
  146. $cache->makeGlobalKey( 'jobqueue', $this->wiki, 'hasjobs', self::TYPE_DEFAULT ),
  147. 'true',
  148. 15
  149. );
  150. }
  151. }
  152. /**
  153. * Buffer jobs for insertion via push() or call it now if in CLI mode
  154. *
  155. * Note that pushLazyJobs() is registered as a deferred update just before
  156. * DeferredUpdates::doUpdates() in MediaWiki and JobRunner classes in order
  157. * to be executed as the very last deferred update (T100085, T154425).
  158. *
  159. * @param IJobSpecification|IJobSpecification[] $jobs A single Job or a list of Jobs
  160. * @return void
  161. * @since 1.26
  162. */
  163. public function lazyPush( $jobs ) {
  164. if ( $this->invalidWiki ) {
  165. // Do not enqueue job that cannot be run (T171371)
  166. throw new LogicException( "Domain '{$this->wiki}' is not recognized." );
  167. }
  168. if ( PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg' ) {
  169. $this->push( $jobs );
  170. return;
  171. }
  172. $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
  173. // Throw errors now instead of on push(), when other jobs may be buffered
  174. $this->assertValidJobs( $jobs );
  175. $this->bufferedJobs = array_merge( $this->bufferedJobs, $jobs );
  176. }
  177. /**
  178. * Push all jobs buffered via lazyPush() into their respective queues
  179. *
  180. * @return void
  181. * @since 1.26
  182. */
  183. public static function pushLazyJobs() {
  184. foreach ( self::$instances as $group ) {
  185. try {
  186. $group->push( $group->bufferedJobs );
  187. $group->bufferedJobs = [];
  188. } catch ( Exception $e ) {
  189. // Get in as many jobs as possible and let other post-send updates happen
  190. MWExceptionHandler::logException( $e );
  191. }
  192. }
  193. }
  194. /**
  195. * Pop a job off one of the job queues
  196. *
  197. * This pops a job off a queue as specified by $wgJobTypeConf and
  198. * updates the aggregate job queue information cache as needed.
  199. *
  200. * @param int|string $qtype JobQueueGroup::TYPE_* constant or job type string
  201. * @param int $flags Bitfield of JobQueueGroup::USE_* constants
  202. * @param array $blacklist List of job types to ignore
  203. * @return Job|bool Returns false on failure
  204. */
  205. public function pop( $qtype = self::TYPE_DEFAULT, $flags = 0, array $blacklist = [] ) {
  206. $job = false;
  207. if ( is_string( $qtype ) ) { // specific job type
  208. if ( !in_array( $qtype, $blacklist ) ) {
  209. $job = $this->get( $qtype )->pop();
  210. }
  211. } else { // any job in the "default" jobs types
  212. if ( $flags & self::USE_CACHE ) {
  213. if ( !$this->cache->has( 'queues-ready', 'list', self::PROC_CACHE_TTL ) ) {
  214. $this->cache->set( 'queues-ready', 'list', $this->getQueuesWithJobs() );
  215. }
  216. $types = $this->cache->get( 'queues-ready', 'list' );
  217. } else {
  218. $types = $this->getQueuesWithJobs();
  219. }
  220. if ( $qtype == self::TYPE_DEFAULT ) {
  221. $types = array_intersect( $types, $this->getDefaultQueueTypes() );
  222. }
  223. $types = array_diff( $types, $blacklist ); // avoid selected types
  224. shuffle( $types ); // avoid starvation
  225. foreach ( $types as $type ) { // for each queue...
  226. $job = $this->get( $type )->pop();
  227. if ( $job ) { // found
  228. break;
  229. } else { // not found
  230. $this->cache->clear( 'queues-ready' );
  231. }
  232. }
  233. }
  234. return $job;
  235. }
  236. /**
  237. * Acknowledge that a job was completed
  238. *
  239. * @param Job $job
  240. * @return void
  241. */
  242. public function ack( Job $job ) {
  243. $this->get( $job->getType() )->ack( $job );
  244. }
  245. /**
  246. * Register the "root job" of a given job into the queue for de-duplication.
  247. * This should only be called right *after* all the new jobs have been inserted.
  248. *
  249. * @param Job $job
  250. * @return bool
  251. */
  252. public function deduplicateRootJob( Job $job ) {
  253. return $this->get( $job->getType() )->deduplicateRootJob( $job );
  254. }
  255. /**
  256. * Wait for any replica DBs or backup queue servers to catch up.
  257. *
  258. * This does nothing for certain queue classes.
  259. *
  260. * @return void
  261. */
  262. public function waitForBackups() {
  263. global $wgJobTypeConf;
  264. // Try to avoid doing this more than once per queue storage medium
  265. foreach ( $wgJobTypeConf as $type => $conf ) {
  266. $this->get( $type )->waitForBackups();
  267. }
  268. }
  269. /**
  270. * Get the list of queue types
  271. *
  272. * @return array List of strings
  273. */
  274. public function getQueueTypes() {
  275. return array_keys( $this->getCachedConfigVar( 'wgJobClasses' ) );
  276. }
  277. /**
  278. * Get the list of default queue types
  279. *
  280. * @return array List of strings
  281. */
  282. public function getDefaultQueueTypes() {
  283. global $wgJobTypesExcludedFromDefaultQueue;
  284. return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue );
  285. }
  286. /**
  287. * Check if there are any queues with jobs (this is cached)
  288. *
  289. * @param int $type JobQueueGroup::TYPE_* constant
  290. * @return bool
  291. * @since 1.23
  292. */
  293. public function queuesHaveJobs( $type = self::TYPE_ANY ) {
  294. $cache = ObjectCache::getLocalClusterInstance();
  295. $key = $cache->makeGlobalKey( 'jobqueue', $this->wiki, 'hasjobs', $type );
  296. $value = $cache->get( $key );
  297. if ( $value === false ) {
  298. $queues = $this->getQueuesWithJobs();
  299. if ( $type == self::TYPE_DEFAULT ) {
  300. $queues = array_intersect( $queues, $this->getDefaultQueueTypes() );
  301. }
  302. $value = count( $queues ) ? 'true' : 'false';
  303. $cache->add( $key, $value, 15 );
  304. }
  305. return ( $value === 'true' );
  306. }
  307. /**
  308. * Get the list of job types that have non-empty queues
  309. *
  310. * @return array List of job types that have non-empty queues
  311. */
  312. public function getQueuesWithJobs() {
  313. $types = [];
  314. foreach ( $this->getCoalescedQueues() as $info ) {
  315. $nonEmpty = $info['queue']->getSiblingQueuesWithJobs( $this->getQueueTypes() );
  316. if ( is_array( $nonEmpty ) ) { // batching features supported
  317. $types = array_merge( $types, $nonEmpty );
  318. } else { // we have to go through the queues in the bucket one-by-one
  319. foreach ( $info['types'] as $type ) {
  320. if ( !$this->get( $type )->isEmpty() ) {
  321. $types[] = $type;
  322. }
  323. }
  324. }
  325. }
  326. return $types;
  327. }
  328. /**
  329. * Get the size of the queus for a list of job types
  330. *
  331. * @return array Map of (job type => size)
  332. */
  333. public function getQueueSizes() {
  334. $sizeMap = [];
  335. foreach ( $this->getCoalescedQueues() as $info ) {
  336. $sizes = $info['queue']->getSiblingQueueSizes( $this->getQueueTypes() );
  337. if ( is_array( $sizes ) ) { // batching features supported
  338. $sizeMap = $sizeMap + $sizes;
  339. } else { // we have to go through the queues in the bucket one-by-one
  340. foreach ( $info['types'] as $type ) {
  341. $sizeMap[$type] = $this->get( $type )->getSize();
  342. }
  343. }
  344. }
  345. return $sizeMap;
  346. }
  347. /**
  348. * @return array
  349. */
  350. protected function getCoalescedQueues() {
  351. global $wgJobTypeConf;
  352. if ( $this->coalescedQueues === null ) {
  353. $this->coalescedQueues = [];
  354. foreach ( $wgJobTypeConf as $type => $conf ) {
  355. $queue = JobQueue::factory(
  356. [ 'wiki' => $this->wiki, 'type' => 'null' ] + $conf );
  357. $loc = $queue->getCoalesceLocationInternal();
  358. if ( !isset( $this->coalescedQueues[$loc] ) ) {
  359. $this->coalescedQueues[$loc]['queue'] = $queue;
  360. $this->coalescedQueues[$loc]['types'] = [];
  361. }
  362. if ( $type === 'default' ) {
  363. $this->coalescedQueues[$loc]['types'] = array_merge(
  364. $this->coalescedQueues[$loc]['types'],
  365. array_diff( $this->getQueueTypes(), array_keys( $wgJobTypeConf ) )
  366. );
  367. } else {
  368. $this->coalescedQueues[$loc]['types'][] = $type;
  369. }
  370. }
  371. }
  372. return $this->coalescedQueues;
  373. }
  374. /**
  375. * @param string $name
  376. * @return mixed
  377. */
  378. private function getCachedConfigVar( $name ) {
  379. // @TODO: cleanup this whole method with a proper config system
  380. if ( $this->wiki === wfWikiID() ) {
  381. return $GLOBALS[$name]; // common case
  382. } else {
  383. $wiki = $this->wiki;
  384. $cache = ObjectCache::getMainWANInstance();
  385. $value = $cache->getWithSetCallback(
  386. $cache->makeGlobalKey( 'jobqueue', 'configvalue', $wiki, $name ),
  387. $cache::TTL_DAY + mt_rand( 0, $cache::TTL_DAY ),
  388. function () use ( $wiki, $name ) {
  389. global $wgConf;
  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. function __destruct() {
  409. $n = count( $this->bufferedJobs );
  410. if ( $n > 0 ) {
  411. $type = implode( ', ', array_unique( array_map( 'get_class', $this->bufferedJobs ) ) );
  412. trigger_error( __METHOD__ . ": $n buffered job(s) of type(s) $type never inserted." );
  413. }
  414. }
  415. }