610fc4773a0633000de9c6f89dd976b5aa84873b
3 * Version of LockManager based on using memcached servers.
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
21 * @ingroup LockManager
25 * Manage locks using memcached servers.
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.
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.
35 * @ingroup LockManager
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
46 /** @var Array Map server names to MemcachedBagOStuff objects */
47 protected $bagOStuffs = array();
49 protected $serversUp = array(); // (server name => bool)
51 protected $lockExpiry; // integer; maximum time locks can be held
52 protected $session = ''; // string; random SHA-1 UUID
53 protected $wikiId = ''; // string
56 * Construct a new instance from configuration.
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]
66 * @param Array $config
69 public function __construct( array $config ) {
70 parent
::__construct( $config );
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
76 $memcConfig = isset( $config['memcConfig'] )
77 ?
$config['memcConfig']
78 : array( 'class' => 'MemcachedPhpBagOStuff' );
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;
86 throw new MWException(
87 'Only MemcachedBagOStuff classes are supported by MemcLockManager.' );
91 $this->wikiId
= isset( $config['wikiId'] ) ?
$config['wikiId'] : wfWikiID();
93 $met = ini_get( 'max_execution_time' ); // this is 0 in CLI mode
94 $this->lockExpiry
= $met ?
2*(int)$met : 2*3600;
96 $this->session
= wfRandomString( 32 );
100 * @see QuorumLockManager::getLocksOnServer()
103 protected function getLocksOnServer( $lockSrv, array $paths, $type ) {
104 $status = Status
::newGood();
106 $memc = $this->getCache( $lockSrv );
107 $keys = array_map( array( $this, 'recordKeyForPath' ), $paths ); // lock records
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 );
117 // Fetch all the existing lock records...
118 $lockRecords = $memc->getMulti( $keys );
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 );
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 );
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;
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" );
159 // Unlock all of the active lock record keys...
160 $this->releaseMutexes( $memc, $keys );
166 * @see QuorumLockManager::freeLocksOnServer()
169 protected function freeLocksOnServer( $lockSrv, array $paths, $type ) {
170 $status = Status
::newGood();
172 $memc = $this->getCache( $lockSrv );
173 $keys = array_map( array( $this, 'recordKeyForPath' ), $paths ); // lock records
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 );
183 // Fetch all the existing lock records...
184 $lockRecords = $memc->getMulti( $keys );
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
192 $locksHeld = $lockRecords[$locksKey];
193 if ( is_array( $locksHeld ) && isset( $locksHeld[$type] ) ) {
194 unset( $locksHeld[$type][$this->session
] );
195 $ok = $memc->set( $locksKey, $locksHeld );
200 $status->fatal( 'lockmanager-fail-releaselock', $path );
202 wfDebug( __METHOD__
. ": released lock on key $locksKey.\n" );
205 // Unlock all of the active lock record keys...
206 $this->releaseMutexes( $memc, $keys );
212 * @see QuorumLockManager::releaseAllLocks()
215 protected function releaseAllLocks() {
216 return Status
::newGood(); // not supported
220 * @see QuorumLockManager::isServerUp()
223 protected function isServerUp( $lockSrv ) {
224 return (bool)$this->getCache( $lockSrv );
228 * Get the MemcachedBagOStuff object for a $lockSrv
230 * @param $lockSrv string Server name
231 * @return MemcachedBagOStuff|null
233 protected function getCache( $lockSrv ) {
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
);
243 if ( !$this->serversUp
[$lockSrv] ) {
244 return null; // server appears to be down
251 * @param $path string
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 );
261 * @param $memc MemcachedBagOStuff
262 * @param $keys Array List of keys to acquire
265 protected function acquireMutexes( MemcachedBagOStuff
$memc, array $keys ) {
266 $lockedKeys = array();
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.
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.
276 $start = microtime( true );
278 if ( ( ++
$rounds %
4 ) == 0 ) {
279 usleep( 1000*50 ); // 50 ms
281 foreach ( array_diff( $keys, $lockedKeys ) as $key ) {
282 if ( $memc->add( "$key:mutex", 1, 180 ) ) { // lock record
283 $lockedKeys[] = $key;
285 continue; // acquire in order
288 } while ( count( $lockedKeys ) < count( $keys ) && ( microtime( true ) - $start ) <= 6 );
290 if ( count( $lockedKeys ) != count( $keys ) ) {
291 $this->releaseMutexes( $lockedKeys ); // failed; release what was locked
299 * @param $memc MemcachedBagOStuff
300 * @param $keys Array List of acquired keys
303 protected function releaseMutexes( MemcachedBagOStuff
$memc, array $keys ) {
304 foreach ( $keys as $key ) {
305 $memc->delete( "$key:mutex" );
310 * Make sure remaining locks get cleared for sanity
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
);