Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / job / JobQueueAggregatorRedis.php
blobc6a799df3f6134c04457e58dca179b0f951d3a4b
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 /**
36 * @params include:
37 * - redisConfig : An array of parameters to RedisConnectionPool::__construct().
38 * - redisServer : A hostname/port combination or the absolute path of a UNIX socket.
39 * If a hostname is specified but no port, the standard port number
40 * 6379 will be used. Required.
41 * @param array $params
43 protected function __construct( array $params ) {
44 parent::__construct( $params );
45 $this->server = $params['redisServer'];
46 $this->redisPool = RedisConnectionPool::singleton( $params['redisConfig'] );
49 /**
50 * @see JobQueueAggregator::doNotifyQueueEmpty()
52 protected function doNotifyQueueEmpty( $wiki, $type ) {
53 $conn = $this->getConnection();
54 if ( !$conn ) {
55 return false;
57 try {
58 $conn->hDel( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ) );
59 return true;
60 } catch ( RedisException $e ) {
61 $this->handleException( $conn, $e );
62 return false;
66 /**
67 * @see JobQueueAggregator::doNotifyQueueNonEmpty()
69 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
70 $conn = $this->getConnection();
71 if ( !$conn ) {
72 return false;
74 try {
75 $conn->hSet( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ), time() );
76 return true;
77 } catch ( RedisException $e ) {
78 $this->handleException( $conn, $e );
79 return false;
83 /**
84 * @see JobQueueAggregator::doAllGetReadyWikiQueues()
86 protected function doGetAllReadyWikiQueues() {
87 $conn = $this->getConnection();
88 if ( !$conn ) {
89 return array();
91 try {
92 $conn->multi( Redis::PIPELINE );
93 $conn->exists( $this->getReadyQueueKey() );
94 $conn->hGetAll( $this->getReadyQueueKey() );
95 list( $exists, $map ) = $conn->exec();
97 if ( $exists ) { // cache hit
98 $pendingDBs = array(); // (type => list of wikis)
99 foreach ( $map as $key => $time ) {
100 list( $type, $wiki ) = $this->dencQueueName( $key );
101 $pendingDBs[$type][] = $wiki;
103 } else { // cache miss
104 // Avoid duplicated effort
105 $conn->multi( Redis::MULTI );
106 $conn->setnx( $this->getReadyQueueKey() . ":lock", 1 );
107 $conn->expire( $this->getReadyQueueKey() . ":lock", 3600 );
108 if ( $conn->exec() !== array( true, true ) ) { // lock
109 return array(); // already in progress
112 $pendingDBs = $this->findPendingWikiQueues(); // (type => list of wikis)
114 $conn->delete( $this->getReadyQueueKey() . ":lock" ); // unlock
116 $now = time();
117 $map = array();
118 foreach ( $pendingDBs as $type => $wikis ) {
119 foreach ( $wikis as $wiki ) {
120 $map[$this->encQueueName( $type, $wiki )] = $now;
123 $conn->hMSet( $this->getReadyQueueKey(), $map );
126 return $pendingDBs;
127 } catch ( RedisException $e ) {
128 $this->handleException( $conn, $e );
129 return array();
134 * @see JobQueueAggregator::doPurge()
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 );
145 return false;
147 return true;
151 * Get a connection to the server that handles all sub-queues for this queue
153 * @return Array (server name, Redis instance)
154 * @throws MWException
156 protected function getConnection() {
157 return $this->redisPool->getConnection( $this->server );
161 * @param RedisConnRef $conn
162 * @param RedisException $e
163 * @return void
165 protected function handleException( RedisConnRef $conn, $e ) {
166 $this->redisPool->handleException( $this->server, $conn, $e );
170 * @return string
172 private function getReadyQueueKey() {
173 return "jobqueue:aggregator:h-ready-queues:v1"; // global
177 * @param string $type
178 * @param string $wiki
179 * @return string
181 private function encQueueName( $type, $wiki ) {
182 return rawurlencode( $type ) . '/' . rawurlencode( $wiki );
186 * @param string $name
187 * @return string
189 private function dencQueueName( $name ) {
190 list( $type, $wiki ) = explode( '/', $name, 2 );
191 return array( rawurldecode( $type ), rawurldecode( $wiki ) );