Follow-up I0b781c11 (2a55449): use User::getAutomaticGroups().
[lhc/web/wiklou.git] / includes / filerepo / backend / 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 */
68 public function __construct( array $config ) {
69 parent::__construct( $config );
70
71 // Sanitize srvsByBucket config to prevent PHP errors
72 $this->srvsByBucket = array_filter( $config['srvsByBucket'], 'is_array' );
73 $this->srvsByBucket = array_values( $this->srvsByBucket ); // consecutive
74
75 $memcConfig = isset( $config['memcConfig'] )
76 ? $config['memcConfig']
77 : array( 'class' => 'MemcachedPhpBagOStuff' );
78
79 foreach ( $config['lockServers'] as $name => $address ) {
80 $params = array( 'servers' => array( $address ) ) + $memcConfig;
81 $cache = ObjectCache::newFromParams( $params );
82 if ( $cache instanceof MemcachedBagOStuff ) {
83 $this->bagOStuffs[$name] = $cache;
84 } else {
85 throw new MWException(
86 'Only MemcachedBagOStuff classes are supported by MemcLockManager.' );
87 }
88 }
89
90 $this->wikiId = isset( $config['wikiId'] ) ? $config['wikiId'] : wfWikiID();
91
92 $met = ini_get( 'max_execution_time' ); // this is 0 in CLI mode
93 $this->lockExpiry = $met ? 2*(int)$met : 2*3600;
94
95 $this->session = wfRandomString( 32 );
96 }
97
98 /**
99 * @see QuorumLockManager::getLocksOnServer()
100 * @return Status
101 */
102 protected function getLocksOnServer( $lockSrv, array $paths, $type ) {
103 $status = Status::newGood();
104
105 $memc = $this->getCache( $lockSrv );
106 $keys = array_map( array( $this, 'recordKeyForPath' ), $paths ); // lock records
107
108 // Lock all of the active lock record keys...
109 if ( !$this->acquireMutexes( $memc, $keys ) ) {
110 foreach ( $paths as $path ) {
111 $status->fatal( 'lockmanager-fail-acquirelock', $path );
112 }
113 return;
114 }
115
116 // Fetch all the existing lock records...
117 $lockRecords = $memc->getMulti( $keys );
118
119 $now = time();
120 // Check if the requested locks conflict with existing ones...
121 foreach ( $paths as $path ) {
122 $locksKey = $this->recordKeyForPath( $path );
123 $locksHeld = isset( $lockRecords[$locksKey] )
124 ? $lockRecords[$locksKey]
125 : array( self::LOCK_SH => array(), self::LOCK_EX => array() ); // init
126 foreach ( $locksHeld[self::LOCK_EX] as $session => $expiry ) {
127 if ( $expiry < $now ) { // stale?
128 unset( $locksHeld[self::LOCK_EX][$session] );
129 } elseif ( $session !== $this->session ) {
130 $status->fatal( 'lockmanager-fail-acquirelock', $path );
131 }
132 }
133 if ( $type === self::LOCK_EX ) {
134 foreach ( $locksHeld[self::LOCK_SH] as $session => $expiry ) {
135 if ( $expiry < $now ) { // stale?
136 unset( $locksHeld[self::LOCK_SH][$session] );
137 } elseif ( $session !== $this->session ) {
138 $status->fatal( 'lockmanager-fail-acquirelock', $path );
139 }
140 }
141 }
142 if ( $status->isOK() ) {
143 // Register the session in the lock record array
144 $locksHeld[$type][$this->session] = $now + $this->lockExpiry;
145 // We will update this record if none of the other locks conflict
146 $lockRecords[$locksKey] = $locksHeld;
147 }
148 }
149
150 // If there were no lock conflicts, update all the lock records...
151 if ( $status->isOK() ) {
152 foreach ( $lockRecords as $locksKey => $locksHeld ) {
153 $memc->set( $locksKey, $locksHeld );
154 wfDebug( __METHOD__ . ": acquired lock on key $locksKey.\n" );
155 }
156 }
157
158 // Unlock all of the active lock record keys...
159 $this->releaseMutexes( $memc, $keys );
160
161 return $status;
162 }
163
164 /**
165 * @see QuorumLockManager::freeLocksOnServer()
166 * @return Status
167 */
168 protected function freeLocksOnServer( $lockSrv, array $paths, $type ) {
169 $status = Status::newGood();
170
171 $memc = $this->getCache( $lockSrv );
172 $keys = array_map( array( $this, 'recordKeyForPath' ), $paths ); // lock records
173
174 // Lock all of the active lock record keys...
175 if ( !$this->acquireMutexes( $memc, $keys ) ) {
176 foreach ( $paths as $path ) {
177 $status->fatal( 'lockmanager-fail-releaselock', $path );
178 }
179 return;
180 }
181
182 // Fetch all the existing lock records...
183 $lockRecords = $memc->getMulti( $keys );
184
185 // Remove the requested locks from all records...
186 foreach ( $paths as $path ) {
187 $locksKey = $this->recordKeyForPath( $path ); // lock record
188 if ( !isset( $lockRecords[$locksKey] ) ) {
189 continue; // nothing to do
190 }
191 $locksHeld = $lockRecords[$locksKey];
192 if ( is_array( $locksHeld ) && isset( $locksHeld[$type] ) ) {
193 unset( $locksHeld[$type][$this->session] );
194 $ok = $memc->set( $locksKey, $locksHeld );
195 } else {
196 $ok = true;
197 }
198 if ( !$ok ) {
199 $status->fatal( 'lockmanager-fail-releaselock', $path );
200 }
201 wfDebug( __METHOD__ . ": released lock on key $locksKey.\n" );
202 }
203
204 // Unlock all of the active lock record keys...
205 $this->releaseMutexes( $memc, $keys );
206
207 return $status;
208 }
209
210 /**
211 * @see QuorumLockManager::releaseAllLocks()
212 * @return Status
213 */
214 protected function releaseAllLocks() {
215 return Status::newGood(); // not supported
216 }
217
218 /**
219 * @see QuorumLockManager::isServerUp()
220 * @return bool
221 */
222 protected function isServerUp( $lockSrv ) {
223 return (bool)$this->getCache( $lockSrv );
224 }
225
226 /**
227 * Get the MemcachedBagOStuff object for a $lockSrv
228 *
229 * @param $lockSrv string Server name
230 * @return MemcachedBagOStuff|null
231 */
232 protected function getCache( $lockSrv ) {
233 $memc = null;
234 if ( isset( $this->bagOStuffs[$lockSrv] ) ) {
235 $memc = $this->bagOStuffs[$lockSrv];
236 if ( !isset( $this->serversUp[$lockSrv] ) ) {
237 $this->serversUp[$lockSrv] = $memc->set( 'MemcLockManager:ping', 1, 1 );
238 if ( !$this->serversUp[$lockSrv] ) {
239 trigger_error( __METHOD__ . ": Could not contact $lockSrv.", E_USER_WARNING );
240 }
241 }
242 if ( !$this->serversUp[$lockSrv] ) {
243 return null; // server appears to be down
244 }
245 }
246 return $memc;
247 }
248
249 /**
250 * @param $path string
251 * @return string
252 */
253 protected function recordKeyForPath( $path ) {
254 $hash = LockManager::sha1Base36( $path );
255 list( $db, $prefix ) = wfSplitWikiID( $this->wikiId );
256 return wfForeignMemcKey( $db, $prefix, __CLASS__, 'locks', $hash );
257 }
258
259 /**
260 * @param $memc MemcachedBagOStuff
261 * @param $keys Array List of keys to acquire
262 * @return bool
263 */
264 protected function acquireMutexes( MemcachedBagOStuff $memc, array $keys ) {
265 $lockedKeys = array();
266
267 // Acquire the keys in lexicographical order, to avoid deadlock problems.
268 // If P1 is waiting to acquire a key P2 has, P2 can't also be waiting for a key P1 has.
269 sort( $keys );
270
271 // Try to quickly loop to acquire the keys, but back off after a few rounds.
272 // This reduces memcached spam, especially in the rare case where a server acquires
273 // some lock keys and dies without releasing them. Lock keys expire after a few minutes.
274 $rounds = 0;
275 $start = microtime( true );
276 do {
277 if ( ( ++$rounds % 4 ) == 0 ) {
278 usleep( 1000*50 ); // 50 ms
279 }
280 foreach ( array_diff( $keys, $lockedKeys ) as $key ) {
281 if ( $memc->add( "$key:mutex", 1, 180 ) ) { // lock record
282 $lockedKeys[] = $key;
283 } else {
284 continue; // acquire in order
285 }
286 }
287 } while ( count( $lockedKeys ) < count( $keys ) && ( microtime( true ) - $start ) <= 6 );
288
289 if ( count( $lockedKeys ) != count( $keys ) ) {
290 $this->releaseMutexes( $lockedKeys ); // failed; release what was locked
291 return false;
292 }
293
294 return true;
295 }
296
297 /**
298 * @param $memc MemcachedBagOStuff
299 * @param $keys Array List of acquired keys
300 * @return void
301 */
302 protected function releaseMutexes( MemcachedBagOStuff $memc, array $keys ) {
303 foreach ( $keys as $key ) {
304 $memc->delete( "$key:mutex" );
305 }
306 }
307
308 /**
309 * Make sure remaining locks get cleared for sanity
310 */
311 function __destruct() {
312 while ( count( $this->locksHeld ) ) {
313 foreach ( $this->locksHeld as $path => $locks ) {
314 $this->doUnlock( array( $path ), self::LOCK_EX );
315 $this->doUnlock( array( $path ), self::LOCK_SH );
316 }
317 }
318 }
319 }