Merge "Apply IP blocks to X-Forwarded-For header"
[lhc/web/wiklou.git] / includes / job / JobQueueAggregatorRedis.php
1 <?php
2 /**
3 * Job queue aggregator code that uses PhpRedis.
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 * @author Aaron Schulz
22 */
23
24 /**
25 * Class to handle tracking information about all queues using PhpRedis
26 *
27 * @ingroup JobQueue
28 * @ingroup Redis
29 * @since 1.21
30 */
31 class JobQueueAggregatorRedis extends JobQueueAggregator {
32 /** @var RedisConnectionPool */
33 protected $redisPool;
34
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
42 */
43 protected function __construct( array $params ) {
44 parent::__construct( $params );
45 $this->server = $params['redisServer'];
46 $this->redisPool = RedisConnectionPool::singleton( $params['redisConfig'] );
47 }
48
49 /**
50 * @see JobQueueAggregator::doNotifyQueueEmpty()
51 */
52 protected function doNotifyQueueEmpty( $wiki, $type ) {
53 $conn = $this->getConnection();
54 if ( !$conn ) {
55 return false;
56 }
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;
63 }
64 }
65
66 /**
67 * @see JobQueueAggregator::doNotifyQueueNonEmpty()
68 */
69 protected function doNotifyQueueNonEmpty( $wiki, $type ) {
70 $conn = $this->getConnection();
71 if ( !$conn ) {
72 return false;
73 }
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;
80 }
81 }
82
83 /**
84 * @see JobQueueAggregator::doAllGetReadyWikiQueues()
85 */
86 protected function doGetAllReadyWikiQueues() {
87 $conn = $this->getConnection();
88 if ( !$conn ) {
89 return array();
90 }
91 try {
92 $conn->multi( Redis::PIPELINE );
93 $conn->exists( $this->getReadyQueueKey() );
94 $conn->hGetAll( $this->getReadyQueueKey() );
95 list( $exists, $map ) = $conn->exec();
96
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;
102 }
103 } else { // cache miss
104 $pendingDBs = $this->findPendingWikiQueues(); // (type => list of wikis)
105
106 $now = time();
107 $map = array();
108 foreach ( $pendingDBs as $type => $wikis ) {
109 foreach ( $wikis as $wiki ) {
110 $map[$this->encQueueName( $type, $wiki )] = $now;
111 }
112 }
113 $conn->hMSet( $this->getReadyQueueKey(), $map );
114 }
115
116 return $pendingDBs;
117 } catch ( RedisException $e ) {
118 $this->handleException( $conn, $e );
119 return array();
120 }
121 }
122
123 /**
124 * @see JobQueueAggregator::doPurge()
125 */
126 protected function doPurge() {
127 $conn = $this->getConnection();
128 if ( !$conn ) {
129 return false;
130 }
131 try {
132 $conn->delete( $this->getReadyQueueKey() );
133 } catch ( RedisException $e ) {
134 $this->handleException( $conn, $e );
135 return false;
136 }
137 return true;
138 }
139
140 /**
141 * Get a connection to the server that handles all sub-queues for this queue
142 *
143 * @return Array (server name, Redis instance)
144 * @throws MWException
145 */
146 protected function getConnection() {
147 return $this->redisPool->getConnection( $this->server );
148 }
149
150 /**
151 * @param RedisConnRef $conn
152 * @param RedisException $e
153 * @return void
154 */
155 protected function handleException( RedisConnRef $conn, $e ) {
156 $this->redisPool->handleException( $this->server, $conn, $e );
157 }
158
159 /**
160 * @return string
161 */
162 private function getReadyQueueKey() {
163 return "jobqueue:aggregator:h-ready-queues:v1"; // global
164 }
165
166 /**
167 * @param string $type
168 * @param string $wiki
169 * @return string
170 */
171 private function encQueueName( $type, $wiki ) {
172 return rawurlencode( $type ) . '/' . rawurlencode( $wiki );
173 }
174
175 /**
176 * @param string $name
177 * @return string
178 */
179 private function dencQueueName( $name ) {
180 list( $type, $wiki ) = explode( '/', $name, 2 );
181 return array( rawurldecode( $type ), rawurldecode( $wiki ) );
182 }
183 }