JobQueueRedis.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. <?php
  2. /**
  3. * Redis-backed job queue 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\Logger\LoggerFactory;
  23. use Psr\Log\LoggerInterface;
  24. /**
  25. * Class to handle job queues stored in Redis
  26. *
  27. * This is a faster and less resource-intensive job queue than JobQueueDB.
  28. * All data for a queue using this class is placed into one redis server.
  29. * The mediawiki/services/jobrunner background service must be set up and running.
  30. *
  31. * There are eight main redis keys (per queue) used to track jobs:
  32. * - l-unclaimed : A list of job IDs used for ready unclaimed jobs
  33. * - z-claimed : A sorted set of (job ID, UNIX timestamp as score) used for job retries
  34. * - z-abandoned : A sorted set of (job ID, UNIX timestamp as score) used for broken jobs
  35. * - z-delayed : A sorted set of (job ID, UNIX timestamp as score) used for delayed jobs
  36. * - h-idBySha1 : A hash of (SHA1 => job ID) for unclaimed jobs used for de-duplication
  37. * - h-sha1ById : A hash of (job ID => SHA1) for unclaimed jobs used for de-duplication
  38. * - h-attempts : A hash of (job ID => attempt count) used for job claiming/retries
  39. * - h-data : A hash of (job ID => serialized blobs) for job storage
  40. * A job ID can be in only one of z-delayed, l-unclaimed, z-claimed, and z-abandoned.
  41. * If an ID appears in any of those lists, it should have a h-data entry for its ID.
  42. * If a job has a SHA1 de-duplication value and its ID is in l-unclaimed or z-delayed, then
  43. * there should be no other such jobs with that SHA1. Every h-idBySha1 entry has an h-sha1ById
  44. * entry and every h-sha1ById must refer to an ID that is l-unclaimed. If a job has its
  45. * ID in z-claimed or z-abandoned, then it must also have an h-attempts entry for its ID.
  46. *
  47. * The following keys are used to track queue states:
  48. * - s-queuesWithJobs : A set of all queues with non-abandoned jobs
  49. *
  50. * The background service takes care of undelaying, recycling, and pruning jobs as well as
  51. * removing s-queuesWithJobs entries as queues empty.
  52. *
  53. * Additionally, "rootjob:* keys track "root jobs" used for additional de-duplication.
  54. * Aside from root job keys, all keys have no expiry, and are only removed when jobs are run.
  55. * All the keys are prefixed with the relevant wiki ID information.
  56. *
  57. * This class requires Redis 2.6 as it makes use Lua scripts for fast atomic operations.
  58. * Additionally, it should be noted that redis has different persistence modes, such
  59. * as rdb snapshots, journaling, and no persistence. Appropriate configuration should be
  60. * made on the servers based on what queues are using it and what tolerance they have.
  61. *
  62. * @ingroup JobQueue
  63. * @ingroup Redis
  64. * @since 1.22
  65. */
  66. class JobQueueRedis extends JobQueue {
  67. /** @var RedisConnectionPool */
  68. protected $redisPool;
  69. /** @var LoggerInterface */
  70. protected $logger;
  71. /** @var string Server address */
  72. protected $server;
  73. /** @var string Compression method to use */
  74. protected $compression;
  75. const MAX_PUSH_SIZE = 25; // avoid tying up the server
  76. /**
  77. * @param array $params Possible keys:
  78. * - redisConfig : An array of parameters to RedisConnectionPool::__construct().
  79. * Note that the serializer option is ignored as "none" is always used.
  80. * - redisServer : A hostname/port combination or the absolute path of a UNIX socket.
  81. * If a hostname is specified but no port, the standard port number
  82. * 6379 will be used. Required.
  83. * - compression : The type of compression to use; one of (none,gzip).
  84. * - daemonized : Set to true if the redisJobRunnerService runs in the background.
  85. * This will disable job recycling/undelaying from the MediaWiki side
  86. * to avoid redundance and out-of-sync configuration.
  87. * @throws InvalidArgumentException
  88. */
  89. public function __construct( array $params ) {
  90. parent::__construct( $params );
  91. $params['redisConfig']['serializer'] = 'none'; // make it easy to use Lua
  92. $this->server = $params['redisServer'];
  93. $this->compression = $params['compression'] ?? 'none';
  94. $this->redisPool = RedisConnectionPool::singleton( $params['redisConfig'] );
  95. if ( empty( $params['daemonized'] ) ) {
  96. throw new InvalidArgumentException(
  97. "Non-daemonized mode is no longer supported. Please install the " .
  98. "mediawiki/services/jobrunner service and update \$wgJobTypeConf as needed." );
  99. }
  100. $this->logger = LoggerFactory::getInstance( 'redis' );
  101. }
  102. protected function supportedOrders() {
  103. return [ 'timestamp', 'fifo' ];
  104. }
  105. protected function optimalOrder() {
  106. return 'fifo';
  107. }
  108. protected function supportsDelayedJobs() {
  109. return true;
  110. }
  111. /**
  112. * @see JobQueue::doIsEmpty()
  113. * @return bool
  114. * @throws JobQueueError
  115. */
  116. protected function doIsEmpty() {
  117. return $this->doGetSize() == 0;
  118. }
  119. /**
  120. * @see JobQueue::doGetSize()
  121. * @return int
  122. * @throws JobQueueError
  123. */
  124. protected function doGetSize() {
  125. $conn = $this->getConnection();
  126. try {
  127. return $conn->lLen( $this->getQueueKey( 'l-unclaimed' ) );
  128. } catch ( RedisException $e ) {
  129. throw $this->handleErrorAndMakeException( $conn, $e );
  130. }
  131. }
  132. /**
  133. * @see JobQueue::doGetAcquiredCount()
  134. * @return int
  135. * @throws JobQueueError
  136. */
  137. protected function doGetAcquiredCount() {
  138. $conn = $this->getConnection();
  139. try {
  140. $conn->multi( Redis::PIPELINE );
  141. $conn->zSize( $this->getQueueKey( 'z-claimed' ) );
  142. $conn->zSize( $this->getQueueKey( 'z-abandoned' ) );
  143. return array_sum( $conn->exec() );
  144. } catch ( RedisException $e ) {
  145. throw $this->handleErrorAndMakeException( $conn, $e );
  146. }
  147. }
  148. /**
  149. * @see JobQueue::doGetDelayedCount()
  150. * @return int
  151. * @throws JobQueueError
  152. */
  153. protected function doGetDelayedCount() {
  154. $conn = $this->getConnection();
  155. try {
  156. return $conn->zSize( $this->getQueueKey( 'z-delayed' ) );
  157. } catch ( RedisException $e ) {
  158. throw $this->handleErrorAndMakeException( $conn, $e );
  159. }
  160. }
  161. /**
  162. * @see JobQueue::doGetAbandonedCount()
  163. * @return int
  164. * @throws JobQueueError
  165. */
  166. protected function doGetAbandonedCount() {
  167. $conn = $this->getConnection();
  168. try {
  169. return $conn->zSize( $this->getQueueKey( 'z-abandoned' ) );
  170. } catch ( RedisException $e ) {
  171. throw $this->handleErrorAndMakeException( $conn, $e );
  172. }
  173. }
  174. /**
  175. * @see JobQueue::doBatchPush()
  176. * @param IJobSpecification[] $jobs
  177. * @param int $flags
  178. * @return void
  179. * @throws JobQueueError
  180. */
  181. protected function doBatchPush( array $jobs, $flags ) {
  182. // Convert the jobs into field maps (de-duplicated against each other)
  183. $items = []; // (job ID => job fields map)
  184. foreach ( $jobs as $job ) {
  185. $item = $this->getNewJobFields( $job );
  186. if ( strlen( $item['sha1'] ) ) { // hash identifier => de-duplicate
  187. $items[$item['sha1']] = $item;
  188. } else {
  189. $items[$item['uuid']] = $item;
  190. }
  191. }
  192. if ( $items === [] ) {
  193. return; // nothing to do
  194. }
  195. $conn = $this->getConnection();
  196. try {
  197. // Actually push the non-duplicate jobs into the queue...
  198. if ( $flags & self::QOS_ATOMIC ) {
  199. $batches = [ $items ]; // all or nothing
  200. } else {
  201. $batches = array_chunk( $items, self::MAX_PUSH_SIZE );
  202. }
  203. $failed = 0;
  204. $pushed = 0;
  205. foreach ( $batches as $itemBatch ) {
  206. $added = $this->pushBlobs( $conn, $itemBatch );
  207. if ( is_int( $added ) ) {
  208. $pushed += $added;
  209. } else {
  210. $failed += count( $itemBatch );
  211. }
  212. }
  213. $this->incrStats( 'inserts', $this->type, count( $items ) );
  214. $this->incrStats( 'inserts_actual', $this->type, $pushed );
  215. $this->incrStats( 'dupe_inserts', $this->type,
  216. count( $items ) - $failed - $pushed );
  217. if ( $failed > 0 ) {
  218. $err = "Could not insert {$failed} {$this->type} job(s).";
  219. wfDebugLog( 'JobQueueRedis', $err );
  220. throw new RedisException( $err );
  221. }
  222. } catch ( RedisException $e ) {
  223. throw $this->handleErrorAndMakeException( $conn, $e );
  224. }
  225. }
  226. /**
  227. * @param RedisConnRef $conn
  228. * @param array $items List of results from JobQueueRedis::getNewJobFields()
  229. * @return int Number of jobs inserted (duplicates are ignored)
  230. * @throws RedisException
  231. */
  232. protected function pushBlobs( RedisConnRef $conn, array $items ) {
  233. $args = [ $this->encodeQueueName() ];
  234. // Next args come in 4s ([id, sha1, rtime, blob [, id, sha1, rtime, blob ... ] ] )
  235. foreach ( $items as $item ) {
  236. $args[] = (string)$item['uuid'];
  237. $args[] = (string)$item['sha1'];
  238. $args[] = (string)$item['rtimestamp'];
  239. $args[] = (string)$this->serialize( $item );
  240. }
  241. static $script =
  242. /** @lang Lua */
  243. <<<LUA
  244. local kUnclaimed, kSha1ById, kIdBySha1, kDelayed, kData, kQwJobs = unpack(KEYS)
  245. -- First argument is the queue ID
  246. local queueId = ARGV[1]
  247. -- Next arguments all come in 4s (one per job)
  248. local variadicArgCount = #ARGV - 1
  249. if variadicArgCount % 4 ~= 0 then
  250. return redis.error_reply('Unmatched arguments')
  251. end
  252. -- Insert each job into this queue as needed
  253. local pushed = 0
  254. for i = 2,#ARGV,4 do
  255. local id,sha1,rtimestamp,blob = ARGV[i],ARGV[i+1],ARGV[i+2],ARGV[i+3]
  256. if sha1 == '' or redis.call('hExists',kIdBySha1,sha1) == 0 then
  257. if 1*rtimestamp > 0 then
  258. -- Insert into delayed queue (release time as score)
  259. redis.call('zAdd',kDelayed,rtimestamp,id)
  260. else
  261. -- Insert into unclaimed queue
  262. redis.call('lPush',kUnclaimed,id)
  263. end
  264. if sha1 ~= '' then
  265. redis.call('hSet',kSha1ById,id,sha1)
  266. redis.call('hSet',kIdBySha1,sha1,id)
  267. end
  268. redis.call('hSet',kData,id,blob)
  269. pushed = pushed + 1
  270. end
  271. end
  272. -- Mark this queue as having jobs
  273. redis.call('sAdd',kQwJobs,queueId)
  274. return pushed
  275. LUA;
  276. return $conn->luaEval( $script,
  277. array_merge(
  278. [
  279. $this->getQueueKey( 'l-unclaimed' ), # KEYS[1]
  280. $this->getQueueKey( 'h-sha1ById' ), # KEYS[2]
  281. $this->getQueueKey( 'h-idBySha1' ), # KEYS[3]
  282. $this->getQueueKey( 'z-delayed' ), # KEYS[4]
  283. $this->getQueueKey( 'h-data' ), # KEYS[5]
  284. $this->getGlobalKey( 's-queuesWithJobs' ), # KEYS[6]
  285. ],
  286. $args
  287. ),
  288. 6 # number of first argument(s) that are keys
  289. );
  290. }
  291. /**
  292. * @see JobQueue::doPop()
  293. * @return RunnableJob|bool
  294. * @throws JobQueueError
  295. */
  296. protected function doPop() {
  297. $job = false;
  298. $conn = $this->getConnection();
  299. try {
  300. do {
  301. $blob = $this->popAndAcquireBlob( $conn );
  302. if ( !is_string( $blob ) ) {
  303. break; // no jobs; nothing to do
  304. }
  305. $this->incrStats( 'pops', $this->type );
  306. $item = $this->unserialize( $blob );
  307. if ( $item === false ) {
  308. wfDebugLog( 'JobQueueRedis', "Could not unserialize {$this->type} job." );
  309. continue;
  310. }
  311. // If $item is invalid, the runner loop recyling will cleanup as needed
  312. $job = $this->getJobFromFields( $item ); // may be false
  313. } while ( !$job ); // job may be false if invalid
  314. } catch ( RedisException $e ) {
  315. throw $this->handleErrorAndMakeException( $conn, $e );
  316. }
  317. return $job;
  318. }
  319. /**
  320. * @param RedisConnRef $conn
  321. * @return array Serialized string or false
  322. * @throws RedisException
  323. */
  324. protected function popAndAcquireBlob( RedisConnRef $conn ) {
  325. static $script =
  326. /** @lang Lua */
  327. <<<LUA
  328. local kUnclaimed, kSha1ById, kIdBySha1, kClaimed, kAttempts, kData = unpack(KEYS)
  329. local rTime = unpack(ARGV)
  330. -- Pop an item off the queue
  331. local id = redis.call('rPop',kUnclaimed)
  332. if not id then
  333. return false
  334. end
  335. -- Allow new duplicates of this job
  336. local sha1 = redis.call('hGet',kSha1ById,id)
  337. if sha1 then redis.call('hDel',kIdBySha1,sha1) end
  338. redis.call('hDel',kSha1ById,id)
  339. -- Mark the jobs as claimed and return it
  340. redis.call('zAdd',kClaimed,rTime,id)
  341. redis.call('hIncrBy',kAttempts,id,1)
  342. return redis.call('hGet',kData,id)
  343. LUA;
  344. return $conn->luaEval( $script,
  345. [
  346. $this->getQueueKey( 'l-unclaimed' ), # KEYS[1]
  347. $this->getQueueKey( 'h-sha1ById' ), # KEYS[2]
  348. $this->getQueueKey( 'h-idBySha1' ), # KEYS[3]
  349. $this->getQueueKey( 'z-claimed' ), # KEYS[4]
  350. $this->getQueueKey( 'h-attempts' ), # KEYS[5]
  351. $this->getQueueKey( 'h-data' ), # KEYS[6]
  352. time(), # ARGV[1] (injected to be replication-safe)
  353. ],
  354. 6 # number of first argument(s) that are keys
  355. );
  356. }
  357. /**
  358. * @see JobQueue::doAck()
  359. * @param RunnableJob $job
  360. * @return RunnableJob|bool
  361. * @throws UnexpectedValueException
  362. * @throws JobQueueError
  363. */
  364. protected function doAck( RunnableJob $job ) {
  365. $uuid = $job->getMetadata( 'uuid' );
  366. if ( $uuid === null ) {
  367. throw new UnexpectedValueException( "Job of type '{$job->getType()}' has no UUID." );
  368. }
  369. $conn = $this->getConnection();
  370. try {
  371. static $script =
  372. /** @lang Lua */
  373. <<<LUA
  374. local kClaimed, kAttempts, kData = unpack(KEYS)
  375. local id = unpack(ARGV)
  376. -- Unmark the job as claimed
  377. local removed = redis.call('zRem',kClaimed,id)
  378. -- Check if the job was recycled
  379. if removed == 0 then
  380. return 0
  381. end
  382. -- Delete the retry data
  383. redis.call('hDel',kAttempts,id)
  384. -- Delete the job data itself
  385. return redis.call('hDel',kData,id)
  386. LUA;
  387. $res = $conn->luaEval( $script,
  388. [
  389. $this->getQueueKey( 'z-claimed' ), # KEYS[1]
  390. $this->getQueueKey( 'h-attempts' ), # KEYS[2]
  391. $this->getQueueKey( 'h-data' ), # KEYS[3]
  392. $uuid # ARGV[1]
  393. ],
  394. 3 # number of first argument(s) that are keys
  395. );
  396. if ( !$res ) {
  397. wfDebugLog( 'JobQueueRedis', "Could not acknowledge {$this->type} job $uuid." );
  398. return false;
  399. }
  400. $this->incrStats( 'acks', $this->type );
  401. } catch ( RedisException $e ) {
  402. throw $this->handleErrorAndMakeException( $conn, $e );
  403. }
  404. return true;
  405. }
  406. /**
  407. * @see JobQueue::doDeduplicateRootJob()
  408. * @param IJobSpecification $job
  409. * @return bool
  410. * @throws JobQueueError
  411. * @throws LogicException
  412. */
  413. protected function doDeduplicateRootJob( IJobSpecification $job ) {
  414. if ( !$job->hasRootJobParams() ) {
  415. throw new LogicException( "Cannot register root job; missing parameters." );
  416. }
  417. $params = $job->getRootJobParams();
  418. $key = $this->getRootJobCacheKey( $params['rootJobSignature'] );
  419. $conn = $this->getConnection();
  420. try {
  421. $timestamp = $conn->get( $key ); // last known timestamp of such a root job
  422. if ( $timestamp && $timestamp >= $params['rootJobTimestamp'] ) {
  423. return true; // a newer version of this root job was enqueued
  424. }
  425. // Update the timestamp of the last root job started at the location...
  426. return $conn->set( $key, $params['rootJobTimestamp'], self::ROOTJOB_TTL ); // 2 weeks
  427. } catch ( RedisException $e ) {
  428. throw $this->handleErrorAndMakeException( $conn, $e );
  429. }
  430. }
  431. /**
  432. * @see JobQueue::doIsRootJobOldDuplicate()
  433. * @param IJobSpecification $job
  434. * @return bool
  435. * @throws JobQueueError
  436. */
  437. protected function doIsRootJobOldDuplicate( IJobSpecification $job ) {
  438. if ( !$job->hasRootJobParams() ) {
  439. return false; // job has no de-deplication info
  440. }
  441. $params = $job->getRootJobParams();
  442. $conn = $this->getConnection();
  443. try {
  444. // Get the last time this root job was enqueued
  445. $timestamp = $conn->get( $this->getRootJobCacheKey( $params['rootJobSignature'] ) );
  446. } catch ( RedisException $e ) {
  447. throw $this->handleErrorAndMakeException( $conn, $e );
  448. }
  449. // Check if a new root job was started at the location after this one's...
  450. return ( $timestamp && $timestamp > $params['rootJobTimestamp'] );
  451. }
  452. /**
  453. * @see JobQueue::doDelete()
  454. * @return bool
  455. * @throws JobQueueError
  456. */
  457. protected function doDelete() {
  458. static $props = [ 'l-unclaimed', 'z-claimed', 'z-abandoned',
  459. 'z-delayed', 'h-idBySha1', 'h-sha1ById', 'h-attempts', 'h-data' ];
  460. $conn = $this->getConnection();
  461. try {
  462. $keys = [];
  463. foreach ( $props as $prop ) {
  464. $keys[] = $this->getQueueKey( $prop );
  465. }
  466. $ok = ( $conn->del( $keys ) !== false );
  467. $conn->sRem( $this->getGlobalKey( 's-queuesWithJobs' ), $this->encodeQueueName() );
  468. return $ok;
  469. } catch ( RedisException $e ) {
  470. throw $this->handleErrorAndMakeException( $conn, $e );
  471. }
  472. }
  473. /**
  474. * @see JobQueue::getAllQueuedJobs()
  475. * @return Iterator
  476. * @throws JobQueueError
  477. */
  478. public function getAllQueuedJobs() {
  479. $conn = $this->getConnection();
  480. try {
  481. $uids = $conn->lRange( $this->getQueueKey( 'l-unclaimed' ), 0, -1 );
  482. } catch ( RedisException $e ) {
  483. throw $this->handleErrorAndMakeException( $conn, $e );
  484. }
  485. return $this->getJobIterator( $conn, $uids );
  486. }
  487. /**
  488. * @see JobQueue::getAllDelayedJobs()
  489. * @return Iterator
  490. * @throws JobQueueError
  491. */
  492. public function getAllDelayedJobs() {
  493. $conn = $this->getConnection();
  494. try {
  495. $uids = $conn->zRange( $this->getQueueKey( 'z-delayed' ), 0, -1 );
  496. } catch ( RedisException $e ) {
  497. throw $this->handleErrorAndMakeException( $conn, $e );
  498. }
  499. return $this->getJobIterator( $conn, $uids );
  500. }
  501. /**
  502. * @see JobQueue::getAllAcquiredJobs()
  503. * @return Iterator
  504. * @throws JobQueueError
  505. */
  506. public function getAllAcquiredJobs() {
  507. $conn = $this->getConnection();
  508. try {
  509. $uids = $conn->zRange( $this->getQueueKey( 'z-claimed' ), 0, -1 );
  510. } catch ( RedisException $e ) {
  511. throw $this->handleErrorAndMakeException( $conn, $e );
  512. }
  513. return $this->getJobIterator( $conn, $uids );
  514. }
  515. /**
  516. * @see JobQueue::getAllAbandonedJobs()
  517. * @return Iterator
  518. * @throws JobQueueError
  519. */
  520. public function getAllAbandonedJobs() {
  521. $conn = $this->getConnection();
  522. try {
  523. $uids = $conn->zRange( $this->getQueueKey( 'z-abandoned' ), 0, -1 );
  524. } catch ( RedisException $e ) {
  525. throw $this->handleErrorAndMakeException( $conn, $e );
  526. }
  527. return $this->getJobIterator( $conn, $uids );
  528. }
  529. /**
  530. * @param RedisConnRef $conn
  531. * @param array $uids List of job UUIDs
  532. * @return MappedIterator
  533. */
  534. protected function getJobIterator( RedisConnRef $conn, array $uids ) {
  535. return new MappedIterator(
  536. $uids,
  537. function ( $uid ) use ( $conn ) {
  538. return $this->getJobFromUidInternal( $uid, $conn );
  539. },
  540. [ 'accept' => function ( $job ) {
  541. return is_object( $job );
  542. } ]
  543. );
  544. }
  545. public function getCoalesceLocationInternal() {
  546. return "RedisServer:" . $this->server;
  547. }
  548. protected function doGetSiblingQueuesWithJobs( array $types ) {
  549. return array_keys( array_filter( $this->doGetSiblingQueueSizes( $types ) ) );
  550. }
  551. protected function doGetSiblingQueueSizes( array $types ) {
  552. $sizes = []; // (type => size)
  553. $types = array_values( $types ); // reindex
  554. $conn = $this->getConnection();
  555. try {
  556. $conn->multi( Redis::PIPELINE );
  557. foreach ( $types as $type ) {
  558. $conn->lLen( $this->getQueueKey( 'l-unclaimed', $type ) );
  559. }
  560. $res = $conn->exec();
  561. if ( is_array( $res ) ) {
  562. foreach ( $res as $i => $size ) {
  563. $sizes[$types[$i]] = $size;
  564. }
  565. }
  566. } catch ( RedisException $e ) {
  567. throw $this->handleErrorAndMakeException( $conn, $e );
  568. }
  569. return $sizes;
  570. }
  571. /**
  572. * This function should not be called outside JobQueueRedis
  573. *
  574. * @param string $uid
  575. * @param RedisConnRef|Redis $conn
  576. * @return RunnableJob|bool Returns false if the job does not exist
  577. * @throws JobQueueError
  578. * @throws UnexpectedValueException
  579. */
  580. public function getJobFromUidInternal( $uid, $conn ) {
  581. try {
  582. $data = $conn->hGet( $this->getQueueKey( 'h-data' ), $uid );
  583. if ( $data === false ) {
  584. return false; // not found
  585. }
  586. $item = $this->unserialize( $data );
  587. if ( !is_array( $item ) ) { // this shouldn't happen
  588. throw new UnexpectedValueException( "Could not unserialize job with ID '$uid'." );
  589. }
  590. $params = $item['params'];
  591. $params += [ 'namespace' => $item['namespace'], 'title' => $item['title'] ];
  592. $job = $this->factoryJob( $item['type'], $params );
  593. $job->setMetadata( 'uuid', $item['uuid'] );
  594. $job->setMetadata( 'timestamp', $item['timestamp'] );
  595. // Add in attempt count for debugging at showJobs.php
  596. $job->setMetadata( 'attempts',
  597. $conn->hGet( $this->getQueueKey( 'h-attempts' ), $uid ) );
  598. return $job;
  599. } catch ( RedisException $e ) {
  600. throw $this->handleErrorAndMakeException( $conn, $e );
  601. }
  602. }
  603. /**
  604. * @return array List of (wiki,type) tuples for queues with non-abandoned jobs
  605. * @throws JobQueueConnectionError
  606. * @throws JobQueueError
  607. */
  608. public function getServerQueuesWithJobs() {
  609. $queues = [];
  610. $conn = $this->getConnection();
  611. try {
  612. $set = $conn->sMembers( $this->getGlobalKey( 's-queuesWithJobs' ) );
  613. foreach ( $set as $queue ) {
  614. $queues[] = $this->decodeQueueName( $queue );
  615. }
  616. } catch ( RedisException $e ) {
  617. throw $this->handleErrorAndMakeException( $conn, $e );
  618. }
  619. return $queues;
  620. }
  621. /**
  622. * @param IJobSpecification $job
  623. * @return array
  624. */
  625. protected function getNewJobFields( IJobSpecification $job ) {
  626. return [
  627. // Fields that describe the nature of the job
  628. 'type' => $job->getType(),
  629. 'namespace' => $job->getParams()['namespace'] ?? NS_SPECIAL,
  630. 'title' => $job->getParams()['title'] ?? '',
  631. 'params' => $job->getParams(),
  632. // Some jobs cannot run until a "release timestamp"
  633. 'rtimestamp' => $job->getReleaseTimestamp() ?: 0,
  634. // Additional job metadata
  635. 'uuid' => UIDGenerator::newRawUUIDv4( UIDGenerator::QUICK_RAND ),
  636. 'sha1' => $job->ignoreDuplicates()
  637. ? Wikimedia\base_convert( sha1( serialize( $job->getDeduplicationInfo() ) ), 16, 36, 31 )
  638. : '',
  639. 'timestamp' => time() // UNIX timestamp
  640. ];
  641. }
  642. /**
  643. * @param array $fields
  644. * @return RunnableJob|bool
  645. */
  646. protected function getJobFromFields( array $fields ) {
  647. $params = $fields['params'];
  648. $params += [ 'namespace' => $fields['namespace'], 'title' => $fields['title'] ];
  649. $job = $this->factoryJob( $fields['type'], $params );
  650. $job->setMetadata( 'uuid', $fields['uuid'] );
  651. $job->setMetadata( 'timestamp', $fields['timestamp'] );
  652. return $job;
  653. }
  654. /**
  655. * @param array $fields
  656. * @return string Serialized and possibly compressed version of $fields
  657. */
  658. protected function serialize( array $fields ) {
  659. $blob = serialize( $fields );
  660. if ( $this->compression === 'gzip'
  661. && strlen( $blob ) >= 1024
  662. && function_exists( 'gzdeflate' )
  663. ) {
  664. $object = (object)[ 'blob' => gzdeflate( $blob ), 'enc' => 'gzip' ];
  665. $blobz = serialize( $object );
  666. return ( strlen( $blobz ) < strlen( $blob ) ) ? $blobz : $blob;
  667. } else {
  668. return $blob;
  669. }
  670. }
  671. /**
  672. * @param string $blob
  673. * @return array|bool Unserialized version of $blob or false
  674. */
  675. protected function unserialize( $blob ) {
  676. $fields = unserialize( $blob );
  677. if ( is_object( $fields ) ) {
  678. if ( $fields->enc === 'gzip' && function_exists( 'gzinflate' ) ) {
  679. $fields = unserialize( gzinflate( $fields->blob ) );
  680. } else {
  681. $fields = false;
  682. }
  683. }
  684. return is_array( $fields ) ? $fields : false;
  685. }
  686. /**
  687. * Get a connection to the server that handles all sub-queues for this queue
  688. *
  689. * @return RedisConnRef|Redis
  690. * @throws JobQueueConnectionError
  691. */
  692. protected function getConnection() {
  693. $conn = $this->redisPool->getConnection( $this->server, $this->logger );
  694. if ( !$conn ) {
  695. throw new JobQueueConnectionError(
  696. "Unable to connect to redis server {$this->server}." );
  697. }
  698. return $conn;
  699. }
  700. /**
  701. * @param RedisConnRef $conn
  702. * @param RedisException $e
  703. * @return JobQueueError
  704. */
  705. protected function handleErrorAndMakeException( RedisConnRef $conn, $e ) {
  706. $this->redisPool->handleError( $conn, $e );
  707. return new JobQueueError( "Redis server error: {$e->getMessage()}\n" );
  708. }
  709. /**
  710. * @return string JSON
  711. */
  712. private function encodeQueueName() {
  713. return json_encode( [ $this->type, $this->domain ] );
  714. }
  715. /**
  716. * @param string $name JSON
  717. * @return array (type, wiki)
  718. */
  719. private function decodeQueueName( $name ) {
  720. return json_decode( $name );
  721. }
  722. /**
  723. * @param string $name
  724. * @return string
  725. */
  726. private function getGlobalKey( $name ) {
  727. $parts = [ 'global', 'jobqueue', $name ];
  728. foreach ( $parts as $part ) {
  729. if ( !preg_match( '/[a-zA-Z0-9_-]+/', $part ) ) {
  730. throw new InvalidArgumentException( "Key part characters are out of range." );
  731. }
  732. }
  733. return implode( ':', $parts );
  734. }
  735. /**
  736. * @param string $prop
  737. * @param string|null $type Override this for sibling queues
  738. * @return string
  739. */
  740. private function getQueueKey( $prop, $type = null ) {
  741. $type = is_string( $type ) ? $type : $this->type;
  742. // Use wiki ID for b/c
  743. $keyspace = WikiMap::getWikiIdFromDbDomain( $this->domain );
  744. $parts = [ $keyspace, 'jobqueue', $type, $prop ];
  745. // Parts are typically ASCII, but encode for sanity to escape ":"
  746. return implode( ':', array_map( 'rawurlencode', $parts ) );
  747. }
  748. }