1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
/**
* Base class for lock managers that use a quorum of peer servers for locks.
*
* The resource space can also be sharded into separate peer groups.
*
* See MemcLockManager and RedisLockManager.
*
* @stable to extend
* @ingroup LockManager
* @since 1.20
*/
abstract class QuorumLockManager extends LockManager {
/** @var array Map of bucket indexes to peer server lists */
protected $srvsByBucket = []; // (bucket index => (lsrv1, lsrv2, ...))
/** @var array Map of degraded buckets */
protected $degradedBuckets = []; // (bucket index => UNIX timestamp)
final protected function doLockByType( array $pathsByType ) {
$status = StatusValue::newGood();
$pathsByTypeByBucket = []; // (bucket => type => paths)
// Get locks that need to be acquired (buckets => locks)...
foreach ( $pathsByType as $type => $paths ) {
foreach ( $paths as $path ) {
if ( isset( $this->locksHeld[$path][$type] ) ) {
++$this->locksHeld[$path][$type];
} else {
$bucket = $this->getBucketFromPath( $path );
$pathsByTypeByBucket[$bucket][$type][] = $path;
}
}
}
// Acquire locks in each bucket in bucket order to reduce contention. Any blocking
// mutexes during the acquisition step will not involve circular waiting on buckets.
ksort( $pathsByTypeByBucket );
$lockedPaths = []; // files locked in this attempt (type => paths)
// Attempt to acquire these locks...
foreach ( $pathsByTypeByBucket as $bucket => $bucketPathsByType ) {
// Try to acquire the locks for this bucket
$status->merge( $this->doLockingRequestBucket( $bucket, $bucketPathsByType ) );
if ( !$status->isOK() ) {
$status->merge( $this->doUnlockByType( $lockedPaths ) );
return $status;
}
// Record these locks as active
foreach ( $bucketPathsByType as $type => $paths ) {
foreach ( $paths as $path ) {
$this->locksHeld[$path][$type] = 1; // locked
// Keep track of what locks were made in this attempt
$lockedPaths[$type][] = $path;
}
}
}
return $status;
}
/**
* @stable to override
*
* @param array $pathsByType
*
* @return StatusValue
*/
protected function doUnlockByType( array $pathsByType ) {
$status = StatusValue::newGood();
$pathsByTypeByBucket = []; // (bucket => type => paths)
foreach ( $pathsByType as $type => $paths ) {
foreach ( $paths as $path ) {
if ( !isset( $this->locksHeld[$path][$type] ) ) {
$status->warning( 'lockmanager-notlocked', $path );
} else {
--$this->locksHeld[$path][$type];
// Reference count the locks held and release locks when zero
if ( $this->locksHeld[$path][$type] <= 0 ) {
unset( $this->locksHeld[$path][$type] );
$bucket = $this->getBucketFromPath( $path );
$pathsByTypeByBucket[$bucket][$type][] = $path;
}
if ( $this->locksHeld[$path] === [] ) {
unset( $this->locksHeld[$path] ); // no SH or EX locks left for key
}
}
}
}
// Remove these specific locks if possible, or at least release
// all locks once this process is currently not holding any locks.
foreach ( $pathsByTypeByBucket as $bucket => $bucketPathsByType ) {
$status->merge( $this->doUnlockingRequestBucket( $bucket, $bucketPathsByType ) );
}
if ( $this->locksHeld === [] ) {
$status->merge( $this->releaseAllLocks() );
$this->degradedBuckets = []; // safe to retry the normal quorum
}
return $status;
}
/**
* Attempt to acquire locks with the peers for a bucket.
* This is all or nothing; if any key is locked then this totally fails.
*
* @param int $bucket
* @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
* @return StatusValue
*/
final protected function doLockingRequestBucket( $bucket, array $pathsByType ) {
return $this->collectPledgeQuorum(
$bucket,
function ( $lockSrv ) use ( $pathsByType ) {
return $this->getLocksOnServer( $lockSrv, $pathsByType );
}
);
}
/**
* Attempt to release locks with the peers for a bucket
*
* @param int $bucket
* @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
* @return StatusValue
*/
final protected function doUnlockingRequestBucket( $bucket, array $pathsByType ) {
return $this->releasePledges(
$bucket,
function ( $lockSrv ) use ( $pathsByType ) {
return $this->freeLocksOnServer( $lockSrv, $pathsByType );
}
);
}
/**
* Attempt to acquire pledges with the peers for a bucket.
* This is all or nothing; if any key is already pledged then this totally fails.
*
* @param int $bucket
* @param callable $callback Pledge method taking a server name and yielding a StatusValue
* @return StatusValue
*/
final protected function collectPledgeQuorum( $bucket, callable $callback ) {
$status = StatusValue::newGood();
$yesVotes = 0; // locks made on trustable servers
$votesLeft = count( $this->srvsByBucket[$bucket] ); // remaining peers
$quorum = floor( $votesLeft / 2 + 1 ); // simple majority
// Get votes for each peer, in order, until we have enough...
foreach ( $this->srvsByBucket[$bucket] as $lockSrv ) {
if ( !$this->isServerUp( $lockSrv ) ) {
--$votesLeft;
$status->warning( 'lockmanager-fail-svr-acquire', $lockSrv );
$this->degradedBuckets[$bucket] = time();
continue; // server down?
}
// Attempt to acquire the lock on this peer
$status->merge( $callback( $lockSrv ) );
if ( !$status->isOK() ) {
return $status; // vetoed; resource locked
}
++$yesVotes; // success for this peer
if ( $yesVotes >= $quorum ) {
return $status; // lock obtained
}
--$votesLeft;
$votesNeeded = $quorum - $yesVotes;
if ( $votesNeeded > $votesLeft ) {
break; // short-circuit
}
}
// At this point, we must not have met the quorum
$status->setResult( false );
return $status;
}
/**
* Attempt to release pledges with the peers for a bucket
*
* @param int $bucket
* @param callable $callback Pledge method taking a server name and yielding a StatusValue
* @return StatusValue
*/
final protected function releasePledges( $bucket, callable $callback ) {
$status = StatusValue::newGood();
$yesVotes = 0; // locks freed on trustable servers
$votesLeft = count( $this->srvsByBucket[$bucket] ); // remaining peers
$quorum = floor( $votesLeft / 2 + 1 ); // simple majority
$isDegraded = isset( $this->degradedBuckets[$bucket] ); // not the normal quorum?
foreach ( $this->srvsByBucket[$bucket] as $lockSrv ) {
if ( !$this->isServerUp( $lockSrv ) ) {
$status->warning( 'lockmanager-fail-svr-release', $lockSrv );
} else {
// Attempt to release the lock on this peer
$status->merge( $callback( $lockSrv ) );
++$yesVotes; // success for this peer
// Normally the first peers form the quorum, and the others are ignored.
// Ignore them in this case, but not when an alternative quorum was used.
if ( $yesVotes >= $quorum && !$isDegraded ) {
break; // lock released
}
}
}
// Set a bad StatusValue if the quorum was not met.
// Assumes the same "up" servers as during the acquire step.
$status->setResult( $yesVotes >= $quorum );
return $status;
}
/**
* Get the bucket for resource path.
* This should avoid throwing any exceptions.
*
* @param string $path
* @return int
*/
protected function getBucketFromPath( $path ) {
$prefix = substr( sha1( $path ), 0, 2 ); // first 2 hex chars (8 bits)
return (int)base_convert( $prefix, 16, 10 ) % count( $this->srvsByBucket );
}
/**
* Check if a lock server is up.
* This should process cache results to reduce RTT.
*
* @param string $lockSrv
* @return bool
*/
abstract protected function isServerUp( $lockSrv );
/**
* Get a connection to a lock server and acquire locks
*
* @param string $lockSrv
* @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
* @return StatusValue
*/
abstract protected function getLocksOnServer( $lockSrv, array $pathsByType );
/**
* Get a connection to a lock server and release locks on $paths.
*
* Subclasses must effectively implement this or releaseAllLocks().
*
* @param string $lockSrv
* @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
* @return StatusValue
*/
abstract protected function freeLocksOnServer( $lockSrv, array $pathsByType );
/**
* Release all locks that this session is holding.
*
* Subclasses must effectively implement this or freeLocksOnServer().
*
* @return StatusValue
*/
abstract protected function releaseAllLocks();
final protected function doLock( array $paths, $type ) {
// @phan-suppress-previous-line PhanPluginNeverReturnMethod
throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
}
final protected function doUnlock( array $paths, $type ) {
// @phan-suppress-previous-line PhanPluginNeverReturnMethod
throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
}
}
|