Merge "Reset $mConvRuleTitle before every language conversion run"
[lhc/web/wiklou.git] / includes / filebackend / lockmanager / MemcLockManager.php
1 <?php
2 /**
3 * Version of LockManager based on using memcached servers.
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 * @ingroup LockManager
22 */
23
24 /**
25 * Manage locks using memcached servers.
26 *
27 * Version of LockManager based on using memcached servers.
28 * This is meant for multi-wiki systems that may share files.
29 * All locks are non-blocking, which avoids deadlocks.
30 *
31 * All lock requests for a resource, identified by a hash string, will map
32 * to one bucket. Each bucket maps to one or several peer servers, each running memcached.
33 * A majority of peers must agree for a lock to be acquired.
34 *
35 * @ingroup LockManager
36 * @since 1.20
37 */
38 class MemcLockManager extends QuorumLockManager {
39 /** @var Array Mapping of lock types to the type actually used */
40 protected $lockTypeMap = array(
41 self::LOCK_SH => self::LOCK_SH,
42 self::LOCK_UW => self::LOCK_SH,
43 self::LOCK_EX => self::LOCK_EX
44 );
45
46 /** @var Array Map server names to MemcachedBagOStuff objects */
47 protected $bagOStuffs = array();
48 /** @var Array */
49 protected $serversUp = array(); // (server name => bool)
50
51 protected $lockExpiry; // integer; maximum time locks can be held
52 protected $session = ''; // string; random SHA-1 UUID
53 protected $wikiId = ''; // string
54
55 /**
56 * Construct a new instance from configuration.
57 *
58 * $config paramaters include:
59 * - lockServers : Associative array of server names to "<IP>:<port>" strings.
60 * - srvsByBucket : Array of 1-16 consecutive integer keys, starting from 0,
61 * each having an odd-numbered list of server names (peers) as values.
62 * - memcConfig : Configuration array for ObjectCache::newFromParams. [optional]
63 * If set, this must use one of the memcached classes.
64 * - wikiId : Wiki ID string that all resources are relative to. [optional]
65 *
66 * @param Array $config
67 * @throws MWException
68 */
69 public function __construct( array $config ) {
70 parent::__construct( $config );
71
72 // Sanitize srvsByBucket config to prevent PHP errors
73 $this->srvsByBucket = array_filter( $config['srvsByBucket'], 'is_array' );
74 $this->srvsByBucket = array_values( $this->srvsByBucket ); // consecutive
75
76 $memcConfig = isset( $config['memcConfig'] )
77 ? $config['memcConfig']
78 : array( 'class' => 'MemcachedPhpBagOStuff' );
79
80 foreach ( $config['lockServers'] as $name => $address ) {
81 $params = array( 'servers' => array( $address ) ) + $memcConfig;
82 $cache = ObjectCache::newFromParams( $params );
83 if ( $cache instanceof MemcachedBagOStuff ) {
84 $this->bagOStuffs[$name] = $cache;
85 } else {
86 throw new MWException(
87 'Only MemcachedBagOStuff classes are supported by MemcLockManager.' );
88 }
89 }
90
91 $this->wikiId = isset( $config['wikiId'] ) ? $config['wikiId'] : wfWikiID();
92
93 $met = ini_get( 'max_execution_time' ); // this is 0 in CLI mode
94 $this->lockExpiry = $met ? 2*(int)$met : 2*3600;
95
96 $this->session = wfRandomString( 32 );
97 }
98
99 /**
100 * @see QuorumLockManager::getLocksOnServer()
101 * @return Status
102 */
103 protected function getLocksOnServer( $lockSrv, array $paths, $type ) {
104 $status = Status::newGood();
105
106 $memc = $this->getCache( $lockSrv );
107 $keys = array_map( array( $this, 'recordKeyForPath' ), $paths ); // lock records
108
109 // Lock all of the active lock record keys...
110 if ( !$this->acquireMutexes( $memc, $keys ) ) {
111 foreach ( $paths as $path ) {
112 $status->fatal( 'lockmanager-fail-acquirelock', $path );
113 }
114 return;
115 }
116
117 // Fetch all the existing lock records...
118 $lockRecords = $memc->getMulti( $keys );
119
120 $now = time();
121 // Check if the requested locks conflict with existing ones...
122 foreach ( $paths as $path ) {
123 $locksKey = $this->recordKeyForPath( $path );
124 $locksHeld = isset( $lockRecords[$locksKey] )
125 ? $lockRecords[$locksKey]
126 : array( self::LOCK_SH => array(), self::LOCK_EX => array() ); // init
127 foreach ( $locksHeld[self::LOCK_EX] as $session => $expiry ) {
128 if ( $expiry < $now ) { // stale?
129 unset( $locksHeld[self::LOCK_EX][$session] );
130 } elseif ( $session !== $this->session ) {
131 $status->fatal( 'lockmanager-fail-acquirelock', $path );
132 }
133 }
134 if ( $type === self::LOCK_EX ) {
135 foreach ( $locksHeld[self::LOCK_SH] as $session => $expiry ) {
136 if ( $expiry < $now ) { // stale?
137 unset( $locksHeld[self::LOCK_SH][$session] );
138 } elseif ( $session !== $this->session ) {
139 $status->fatal( 'lockmanager-fail-acquirelock', $path );
140 }
141 }
142 }
143 if ( $status->isOK() ) {
144 // Register the session in the lock record array
145 $locksHeld[$type][$this->session] = $now + $this->lockExpiry;
146 // We will update this record if none of the other locks conflict
147 $lockRecords[$locksKey] = $locksHeld;
148 }
149 }
150
151 // If there were no lock conflicts, update all the lock records...
152 if ( $status->isOK() ) {
153 foreach ( $lockRecords as $locksKey => $locksHeld ) {
154 $memc->set( $locksKey, $locksHeld );
155 wfDebug( __METHOD__ . ": acquired lock on key $locksKey.\n" );
156 }
157 }
158
159 // Unlock all of the active lock record keys...
160 $this->releaseMutexes( $memc, $keys );
161
162 return $status;
163 }
164
165 /**
166 * @see QuorumLockManager::freeLocksOnServer()
167 * @return Status
168 */
169 protected function freeLocksOnServer( $lockSrv, array $paths, $type ) {
170 $status = Status::newGood();
171
172 $memc = $this->getCache( $lockSrv );
173 $keys = array_map( array( $this, 'recordKeyForPath' ), $paths ); // lock records
174
175 // Lock all of the active lock record keys...
176 if ( !$this->acquireMutexes( $memc, $keys ) ) {
177 foreach ( $paths as $path ) {
178 $status->fatal( 'lockmanager-fail-releaselock', $path );
179 }
180 return;
181 }
182
183 // Fetch all the existing lock records...
184 $lockRecords = $memc->getMulti( $keys );
185
186 // Remove the requested locks from all records...
187 foreach ( $paths as $path ) {
188 $locksKey = $this->recordKeyForPath( $path ); // lock record
189 if ( !isset( $lockRecords[$locksKey] ) ) {
190 continue; // nothing to do
191 }
192 $locksHeld = $lockRecords[$locksKey];
193 if ( is_array( $locksHeld ) && isset( $locksHeld[$type] ) ) {
194 unset( $locksHeld[$type][$this->session] );
195 $ok = $memc->set( $locksKey, $locksHeld );
196 } else {
197 $ok = true;
198 }
199 if ( !$ok ) {
200 $status->fatal( 'lockmanager-fail-releaselock', $path );
201 }
202 wfDebug( __METHOD__ . ": released lock on key $locksKey.\n" );
203 }
204
205 // Unlock all of the active lock record keys...
206 $this->releaseMutexes( $memc, $keys );
207
208 return $status;
209 }
210
211 /**
212 * @see QuorumLockManager::releaseAllLocks()
213 * @return Status
214 */
215 protected function releaseAllLocks() {
216 return Status::newGood(); // not supported
217 }
218
219 /**
220 * @see QuorumLockManager::isServerUp()
221 * @return bool
222 */
223 protected function isServerUp( $lockSrv ) {
224 return (bool)$this->getCache( $lockSrv );
225 }
226
227 /**
228 * Get the MemcachedBagOStuff object for a $lockSrv
229 *
230 * @param $lockSrv string Server name
231 * @return MemcachedBagOStuff|null
232 */
233 protected function getCache( $lockSrv ) {
234 $memc = null;
235 if ( isset( $this->bagOStuffs[$lockSrv] ) ) {
236 $memc = $this->bagOStuffs[$lockSrv];
237 if ( !isset( $this->serversUp[$lockSrv] ) ) {
238 $this->serversUp[$lockSrv] = $memc->set( 'MemcLockManager:ping', 1, 1 );
239 if ( !$this->serversUp[$lockSrv] ) {
240 trigger_error( __METHOD__ . ": Could not contact $lockSrv.", E_USER_WARNING );
241 }
242 }
243 if ( !$this->serversUp[$lockSrv] ) {
244 return null; // server appears to be down
245 }
246 }
247 return $memc;
248 }
249
250 /**
251 * @param $path string
252 * @return string
253 */
254 protected function recordKeyForPath( $path ) {
255 $hash = LockManager::sha1Base36( $path );
256 list( $db, $prefix ) = wfSplitWikiID( $this->wikiId );
257 return wfForeignMemcKey( $db, $prefix, __CLASS__, 'locks', $hash );
258 }
259
260 /**
261 * @param $memc MemcachedBagOStuff
262 * @param $keys Array List of keys to acquire
263 * @return bool
264 */
265 protected function acquireMutexes( MemcachedBagOStuff $memc, array $keys ) {
266 $lockedKeys = array();
267
268 // Acquire the keys in lexicographical order, to avoid deadlock problems.
269 // If P1 is waiting to acquire a key P2 has, P2 can't also be waiting for a key P1 has.
270 sort( $keys );
271
272 // Try to quickly loop to acquire the keys, but back off after a few rounds.
273 // This reduces memcached spam, especially in the rare case where a server acquires
274 // some lock keys and dies without releasing them. Lock keys expire after a few minutes.
275 $rounds = 0;
276 $start = microtime( true );
277 do {
278 if ( ( ++$rounds % 4 ) == 0 ) {
279 usleep( 1000*50 ); // 50 ms
280 }
281 foreach ( array_diff( $keys, $lockedKeys ) as $key ) {
282 if ( $memc->add( "$key:mutex", 1, 180 ) ) { // lock record
283 $lockedKeys[] = $key;
284 } else {
285 continue; // acquire in order
286 }
287 }
288 } while ( count( $lockedKeys ) < count( $keys ) && ( microtime( true ) - $start ) <= 6 );
289
290 if ( count( $lockedKeys ) != count( $keys ) ) {
291 $this->releaseMutexes( $lockedKeys ); // failed; release what was locked
292 return false;
293 }
294
295 return true;
296 }
297
298 /**
299 * @param $memc MemcachedBagOStuff
300 * @param $keys Array List of acquired keys
301 * @return void
302 */
303 protected function releaseMutexes( MemcachedBagOStuff $memc, array $keys ) {
304 foreach ( $keys as $key ) {
305 $memc->delete( "$key:mutex" );
306 }
307 }
308
309 /**
310 * Make sure remaining locks get cleared for sanity
311 */
312 function __destruct() {
313 while ( count( $this->locksHeld ) ) {
314 foreach ( $this->locksHeld as $path => $locks ) {
315 $this->doUnlock( array( $path ), self::LOCK_EX );
316 $this->doUnlock( array( $path ), self::LOCK_SH );
317 }
318 }
319 }
320 }