Remove unused variable in ProfilerMwprof
[mediawiki.git] / includes / jobqueue / aggregator / JobQueueAggregatorRedis.php
blobdb9e764c3b1e7afb90356586ac0cf6f9e407e933
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 * @param array $params Possible keys:
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.
47 protected function __construct( array $params ) {
48 parent::__construct( $params );
49 $this->servers = isset( $params['redisServers'] )
50 ? $params['redisServers']
51 : array( $params['redisServer'] ); // b/c
52 $params['redisConfig']['serializer'] = 'none';
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->multi( Redis::PIPELINE );
79 $conn->hSetNx( $this->getQueueTypesKey(), $type, 'enabled' );
80 $conn->hSet( $this->getReadyQueueKey(), $this->encQueueName( $type, $wiki ), time() );
81 $conn->exec();
83 return true;
84 } catch ( RedisException $e ) {
85 $this->handleException( $conn, $e );
87 return false;
91 protected function doGetAllReadyWikiQueues() {
92 $conn = $this->getConnection();
93 if ( !$conn ) {
94 return array();
96 try {
97 $map = $conn->hGetAll( $this->getReadyQueueKey() );
99 if ( is_array( $map ) && isset( $map['_epoch'] ) ) {
100 unset( $map['_epoch'] ); // ignore
101 $pendingDBs = array(); // (type => list of wikis)
102 foreach ( $map as $key => $time ) {
103 list( $type, $wiki ) = $this->dencQueueName( $key );
104 $pendingDBs[$type][] = $wiki;
106 } else {
107 // Avoid duplicated effort
108 $rand = wfRandomString( 32 );
109 $conn->multi( Redis::MULTI );
110 $conn->setex( "{$rand}:lock", 3600, 1 );
111 $conn->renamenx( "{$rand}:lock", $this->getReadyQueueKey() . ":lock" );
112 if ( $conn->exec() !== array( true, true ) ) { // lock
113 $conn->delete( "{$rand}:lock" );
114 return array(); // already in progress
117 $pendingDBs = $this->findPendingWikiQueues(); // (type => list of wikis)
119 $conn->multi( Redis::PIPELINE );
120 $now = time();
121 $map = array( '_epoch' => time() ); // dummy key for empty Redis collections
122 foreach ( $pendingDBs as $type => $wikis ) {
123 $conn->hSetNx( $this->getQueueTypesKey(), $type, 'enabled' );
124 foreach ( $wikis as $wiki ) {
125 $map[$this->encQueueName( $type, $wiki )] = $now;
128 $conn->hMSet( $this->getReadyQueueKey(), $map );
129 $conn->exec();
131 $conn->delete( $this->getReadyQueueKey() . ":lock" ); // unlock
134 return $pendingDBs;
135 } catch ( RedisException $e ) {
136 $this->handleException( $conn, $e );
138 return array();
142 protected function doPurge() {
143 $conn = $this->getConnection();
144 if ( !$conn ) {
145 return false;
147 try {
148 $conn->delete( $this->getReadyQueueKey() );
149 // leave key at getQueueTypesKey() alone
150 } catch ( RedisException $e ) {
151 $this->handleException( $conn, $e );
153 return false;
156 return true;
160 * Get a connection to the server that handles all sub-queues for this queue
162 * @return RedisConnRef|bool Returns false on failure
163 * @throws MWException
165 protected function getConnection() {
166 $conn = false;
167 foreach ( $this->servers as $server ) {
168 $conn = $this->redisPool->getConnection( $server );
169 if ( $conn ) {
170 break;
174 return $conn;
178 * @param RedisConnRef $conn
179 * @param RedisException $e
180 * @return void
182 protected function handleException( RedisConnRef $conn, $e ) {
183 $this->redisPool->handleError( $conn, $e );
187 * @return string
189 private function getReadyQueueKey() {
190 return "jobqueue:aggregator:h-ready-queues:v2"; // global
194 * @return string
196 private function getQueueTypesKey() {
197 return "jobqueue:aggregator:h-queue-types:v2"; // global
201 * @param string $type
202 * @param string $wiki
203 * @return string
205 private function encQueueName( $type, $wiki ) {
206 return rawurlencode( $type ) . '/' . rawurlencode( $wiki );
210 * @param string $name
211 * @return string
213 private function dencQueueName( $name ) {
214 list( $type, $wiki ) = explode( '/', $name, 2 );
216 return array( rawurldecode( $type ), rawurldecode( $wiki ) );