Merge "Add TitleQuickPermissions hook to Title::checkQuickPermissions"
[lhc/web/wiklou.git] / includes / job / aggregator / 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 // 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
110 }
111
112 $pendingDBs = $this->findPendingWikiQueues(); // (type => list of wikis)
113
114 $conn->delete( $this->getReadyQueueKey() . ":lock" ); // unlock
115
116 $now = time();
117 $map = array();
118 foreach ( $pendingDBs as $type => $wikis ) {
119 foreach ( $wikis as $wiki ) {
120 $map[$this->encQueueName( $type, $wiki )] = $now;
121 }
122 }
123 $conn->hMSet( $this->getReadyQueueKey(), $map );
124 }
125
126 return $pendingDBs;
127 } catch ( RedisException $e ) {
128 $this->handleException( $conn, $e );
129 return array();
130 }
131 }
132
133 /**
134 * @see JobQueueAggregator::doPurge()
135 */
136 protected function doPurge() {
137 $conn = $this->getConnection();
138 if ( !$conn ) {
139 return false;
140 }
141 try {
142 $conn->delete( $this->getReadyQueueKey() );
143 } catch ( RedisException $e ) {
144 $this->handleException( $conn, $e );
145 return false;
146 }
147 return true;
148 }
149
150 /**
151 * Get a connection to the server that handles all sub-queues for this queue
152 *
153 * @return Array (server name, Redis instance)
154 * @throws MWException
155 */
156 protected function getConnection() {
157 return $this->redisPool->getConnection( $this->server );
158 }
159
160 /**
161 * @param RedisConnRef $conn
162 * @param RedisException $e
163 * @return void
164 */
165 protected function handleException( RedisConnRef $conn, $e ) {
166 $this->redisPool->handleException( $this->server, $conn, $e );
167 }
168
169 /**
170 * @return string
171 */
172 private function getReadyQueueKey() {
173 return "jobqueue:aggregator:h-ready-queues:v1"; // global
174 }
175
176 /**
177 * @param string $type
178 * @param string $wiki
179 * @return string
180 */
181 private function encQueueName( $type, $wiki ) {
182 return rawurlencode( $type ) . '/' . rawurlencode( $wiki );
183 }
184
185 /**
186 * @param string $name
187 * @return string
188 */
189 private function dencQueueName( $name ) {
190 list( $type, $wiki ) = explode( '/', $name, 2 );
191 return array( rawurldecode( $type ), rawurldecode( $wiki ) );
192 }
193 }