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
21 * @author Aaron Schulz
25 * Class to handle tracking information about all queues using PhpRedis
31 class JobQueueAggregatorRedis
extends JobQueueAggregator
{
32 /** @var RedisConnectionPool */
34 /** @var array List of Redis server addresses */
37 protected $registeredQueue = false;
40 * @param array $params Possible keys:
41 * - redisConfig : An array of parameters to RedisConnectionPool::__construct().
42 * - redisServers : Array of server entries, the first being the primary and the
43 * others being fallback servers. Each entry is either a hostname/port
44 * combination or the absolute path of a UNIX socket.
45 * If a hostname is specified but no port, the standard port number
46 * 6379 will be used. Required.
48 public function __construct( array $params ) {
49 parent
::__construct( $params );
50 $this->servers
= isset( $params['redisServers'] )
51 ?
$params['redisServers']
52 : array( $params['redisServer'] ); // b/c
53 $params['redisConfig']['serializer'] = 'none';
54 $this->redisPool
= RedisConnectionPool
::singleton( $params['redisConfig'] );
57 protected function doNotifyQueueEmpty( $wiki, $type ) {
58 $conn = $this->getConnection();
63 $conn->hDel( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ) );
66 } catch ( RedisException
$e ) {
67 $this->handleException( $conn, $e );
73 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
74 $conn = $this->getConnection();
79 $conn->multi( Redis
::PIPELINE
);
80 if ( !$this->registeredQueue
) {
81 // Make sure the queue is registered as existing
82 $conn->hSetNx( $this->getQueueTypesKey(), $type, 'enabled' );
83 $conn->sAdd( $this->getWikiSetKey(), $wiki );
85 $conn->hSet( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ), time() );
88 $this->registeredQueue
= true;
91 } catch ( RedisException
$e ) {
92 $this->handleException( $conn, $e );
98 protected function doGetAllReadyWikiQueues() {
99 $conn = $this->getConnection();
104 $map = $conn->hGetAll( $this->getReadyQueueKey() );
106 if ( is_array( $map ) && isset( $map['_epoch'] ) ) {
107 unset( $map['_epoch'] ); // ignore
108 $pendingDBs = array(); // (type => list of wikis)
109 foreach ( $map as $key => $time ) {
110 list( $type, $wiki ) = $this->dencQueueName( $key );
111 $pendingDBs[$type][] = $wiki;
114 // Avoid duplicated effort
115 $rand = wfRandomString( 32 );
116 $conn->multi( Redis
::MULTI
);
117 $conn->setex( "{$rand}:lock", 3600, 1 );
118 $conn->renamenx( "{$rand}:lock", $this->getReadyQueueKey() . ":lock" );
119 if ( $conn->exec() !== array( true, true ) ) { // lock
120 $conn->delete( "{$rand}:lock" );
121 return array(); // already in progress
124 $pendingDBs = $this->findPendingWikiQueues(); // (type => list of wikis)
126 $conn->multi( Redis
::PIPELINE
);
128 $map = array( '_epoch' => time() ); // dummy key for empty Redis collections
129 foreach ( $pendingDBs as $type => $wikis ) {
130 $conn->hSetNx( $this->getQueueTypesKey(), $type, 'enabled' );
131 foreach ( $wikis as $wiki ) {
132 $map[$this->encQueueName( $type, $wiki )] = $now;
135 $conn->hMSet( $this->getReadyQueueKey(), $map );
138 $conn->delete( $this->getReadyQueueKey() . ":lock" ); // unlock
142 } catch ( RedisException
$e ) {
143 $this->handleException( $conn, $e );
149 protected function doPurge() {
150 $conn = $this->getConnection();
155 $conn->delete( $this->getReadyQueueKey() );
156 // leave key at getQueueTypesKey() alone
157 } catch ( RedisException
$e ) {
158 $this->handleException( $conn, $e );
167 * Get a connection to the server that handles all sub-queues for this queue
169 * @return RedisConnRef|bool Returns false on failure
170 * @throws MWException
172 protected function getConnection() {
174 foreach ( $this->servers
as $server ) {
175 $conn = $this->redisPool
->getConnection( $server );
185 * @param RedisConnRef $conn
186 * @param RedisException $e
189 protected function handleException( RedisConnRef
$conn, $e ) {
190 $this->redisPool
->handleError( $conn, $e );
196 private function getReadyQueueKey() {
197 return "jobqueue:aggregator:h-ready-queues:v2"; // global
203 private function getQueueTypesKey() {
204 return "jobqueue:aggregator:h-queue-types:v2"; // global
210 private function getWikiSetKey() {
211 return "jobqueue:aggregator:s-wikis:v2"; // global
215 * @param string $type
216 * @param string $wiki
219 private function encQueueName( $type, $wiki ) {
220 return rawurlencode( $type ) . '/' . rawurlencode( $wiki );
224 * @param string $name
227 private function dencQueueName( $name ) {
228 list( $type, $wiki ) = explode( '/', $name, 2 );
230 return array( rawurldecode( $type ), rawurldecode( $wiki ) );