Release notes for Iabf4873f
[mediawiki.git] / includes / job / aggregator / JobQueueAggregatorRedis.php
blob057a5878e8ce5cdd79f3e01c1e4d455353eac552
1 <?php
2 /**
3 * Job queue aggregator code that uses PhpRedis.
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.
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.
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
20 * @file
21 * @author Aaron Schulz
24 /**
25 * Class to handle tracking information about all queues using PhpRedis
27 * @ingroup JobQueue
28 * @ingroup Redis
29 * @since 1.21
31 class JobQueueAggregatorRedis extends JobQueueAggregator {
32 /** @var RedisConnectionPool */
33 protected $redisPool;
35 /** @var array List of Redis server addresses */
36 protected $servers;
38 /**
39 * @params include:
40 * - redisConfig : An array of parameters to RedisConnectionPool::__construct().
41 * - redisServers : Array of server entries, the first being the primary and the
42 * others being fallback servers. Each entry is either a hostname/port
43 * combination or the absolute path of a UNIX socket.
44 * If a hostname is specified but no port, the standard port number
45 * 6379 will be used. Required.
46 * @param array $params
48 protected function __construct( array $params ) {
49 parent::__construct( $params );
50 $this->servers = isset( $params['redisServers'] )
51 ? $params['redisServers']
52 : array( $params['redisServer'] ); // b/c
53 $this->redisPool = RedisConnectionPool::singleton( $params['redisConfig'] );
56 protected function doNotifyQueueEmpty( $wiki, $type ) {
57 $conn = $this->getConnection();
58 if ( !$conn ) {
59 return false;
61 try {
62 $conn->hDel( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ) );
64 return true;
65 } catch ( RedisException $e ) {
66 $this->handleException( $conn, $e );
68 return false;
72 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
73 $conn = $this->getConnection();
74 if ( !$conn ) {
75 return false;
77 try {
78 $conn->hSet( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ), time() );
80 return true;
81 } catch ( RedisException $e ) {
82 $this->handleException( $conn, $e );
84 return false;
88 protected function doGetAllReadyWikiQueues() {
89 $conn = $this->getConnection();
90 if ( !$conn ) {
91 return array();
93 try {
94 $conn->multi( Redis::PIPELINE );
95 $conn->exists( $this->getReadyQueueKey() );
96 $conn->hGetAll( $this->getReadyQueueKey() );
97 list( $exists, $map ) = $conn->exec();
99 if ( $exists ) { // cache hit
100 $pendingDBs = array(); // (type => list of wikis)
101 foreach ( $map as $key => $time ) {
102 list( $type, $wiki ) = $this->dencQueueName( $key );
103 $pendingDBs[$type][] = $wiki;
105 } else { // cache miss
106 // Avoid duplicated effort
107 $conn->multi( Redis::MULTI );
108 $conn->setnx( $this->getReadyQueueKey() . ":lock", 1 );
109 $conn->expire( $this->getReadyQueueKey() . ":lock", 3600 );
110 if ( $conn->exec() !== array( true, true ) ) { // lock
111 return array(); // already in progress
114 $pendingDBs = $this->findPendingWikiQueues(); // (type => list of wikis)
116 $conn->delete( $this->getReadyQueueKey() . ":lock" ); // unlock
118 $now = time();
119 $map = array();
120 foreach ( $pendingDBs as $type => $wikis ) {
121 foreach ( $wikis as $wiki ) {
122 $map[$this->encQueueName( $type, $wiki )] = $now;
125 $conn->hMSet( $this->getReadyQueueKey(), $map );
128 return $pendingDBs;
129 } catch ( RedisException $e ) {
130 $this->handleException( $conn, $e );
132 return array();
136 protected function doPurge() {
137 $conn = $this->getConnection();
138 if ( !$conn ) {
139 return false;
141 try {
142 $conn->delete( $this->getReadyQueueKey() );
143 } catch ( RedisException $e ) {
144 $this->handleException( $conn, $e );
146 return false;
149 return true;
153 * Get a connection to the server that handles all sub-queues for this queue
155 * @return RedisConnRef|bool Returns false on failure
156 * @throws MWException
158 protected function getConnection() {
159 $conn = false;
160 foreach ( $this->servers as $server ) {
161 $conn = $this->redisPool->getConnection( $server );
162 if ( $conn ) {
163 break;
167 return $conn;
171 * @param RedisConnRef $conn
172 * @param RedisException $e
173 * @return void
175 protected function handleException( RedisConnRef $conn, $e ) {
176 $this->redisPool->handleException( $conn->getServer(), $conn, $e );
180 * @return string
182 private function getReadyQueueKey() {
183 return "jobqueue:aggregator:h-ready-queues:v1"; // global
187 * @param string $type
188 * @param string $wiki
189 * @return string
191 private function encQueueName( $type, $wiki ) {
192 return rawurlencode( $type ) . '/' . rawurlencode( $wiki );
196 * @param string $name
197 * @return string
199 private function dencQueueName( $name ) {
200 list( $type, $wiki ) = explode( '/', $name, 2 );
202 return array( rawurldecode( $type ), rawurldecode( $wiki ) );