| 1 |
<?php |
| 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 |
abstract class QuorumLockManager extends LockManager { |
| 33 |
|
| 34 |
protected $srvsByBucket = []; |
| 35 |
|
| 36 |
|
| 37 |
protected $degradedBuckets = []; |
| 38 |
|
| 39 |
final protected function doLockByType( array $pathsByType ) { |
| 40 |
$status = StatusValue::newGood(); |
| 41 |
|
| 42 |
$pathsByTypeByBucket = []; |
| 43 |
|
| 44 |
foreach ( $pathsByType as $type => $paths ) { |
| 45 |
foreach ( $paths as $path ) { |
| 46 |
if ( isset( $this->locksHeld[$path][$type] ) ) { |
| 47 |
++$this->locksHeld[$path][$type]; |
| 48 |
} else { |
| 49 |
$bucket = $this->getBucketFromPath( $path ); |
| 50 |
$pathsByTypeByBucket[$bucket][$type][] = $path; |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
ksort( $pathsByTypeByBucket ); |
| 58 |
|
| 59 |
$lockedPaths = []; |
| 60 |
|
| 61 |
foreach ( $pathsByTypeByBucket as $bucket => $bucketPathsByType ) { |
| 62 |
|
| 63 |
$status->merge( $this->doLockingRequestBucket( $bucket, $bucketPathsByType ) ); |
| 64 |
if ( !$status->isOK() ) { |
| 65 |
$status->merge( $this->doUnlockByType( $lockedPaths ) ); |
| 66 |
|
| 67 |
return $status; |
| 68 |
} |
| 69 |
|
| 70 |
foreach ( $bucketPathsByType as $type => $paths ) { |
| 71 |
foreach ( $paths as $path ) { |
| 72 |
$this->locksHeld[$path][$type] = 1; |
| 73 |
|
| 74 |
$lockedPaths[$type][] = $path; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return $status; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
protected function doUnlockByType( array $pathsByType ) { |
| 90 |
$status = StatusValue::newGood(); |
| 91 |
|
| 92 |
$pathsByTypeByBucket = []; |
| 93 |
foreach ( $pathsByType as $type => $paths ) { |
| 94 |
foreach ( $paths as $path ) { |
| 95 |
if ( !isset( $this->locksHeld[$path][$type] ) ) { |
| 96 |
$status->warning( 'lockmanager-notlocked', $path ); |
| 97 |
} else { |
| 98 |
--$this->locksHeld[$path][$type]; |
| 99 |
|
| 100 |
if ( $this->locksHeld[$path][$type] <= 0 ) { |
| 101 |
unset( $this->locksHeld[$path][$type] ); |
| 102 |
$bucket = $this->getBucketFromPath( $path ); |
| 103 |
$pathsByTypeByBucket[$bucket][$type][] = $path; |
| 104 |
} |
| 105 |
if ( $this->locksHeld[$path] === [] ) { |
| 106 |
unset( $this->locksHeld[$path] ); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
foreach ( $pathsByTypeByBucket as $bucket => $bucketPathsByType ) { |
| 115 |
$status->merge( $this->doUnlockingRequestBucket( $bucket, $bucketPathsByType ) ); |
| 116 |
} |
| 117 |
if ( $this->locksHeld === [] ) { |
| 118 |
$status->merge( $this->releaseAllLocks() ); |
| 119 |
$this->degradedBuckets = []; |
| 120 |
} |
| 121 |
|
| 122 |
return $status; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
final protected function doLockingRequestBucket( $bucket, array $pathsByType ) { |
| 134 |
return $this->collectPledgeQuorum( |
| 135 |
$bucket, |
| 136 |
function ( $lockSrv ) use ( $pathsByType ) { |
| 137 |
return $this->getLocksOnServer( $lockSrv, $pathsByType ); |
| 138 |
} |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
final protected function doUnlockingRequestBucket( $bucket, array $pathsByType ) { |
| 150 |
return $this->releasePledges( |
| 151 |
$bucket, |
| 152 |
function ( $lockSrv ) use ( $pathsByType ) { |
| 153 |
return $this->freeLocksOnServer( $lockSrv, $pathsByType ); |
| 154 |
} |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
final protected function collectPledgeQuorum( $bucket, callable $callback ) { |
| 167 |
$status = StatusValue::newGood(); |
| 168 |
|
| 169 |
$yesVotes = 0; |
| 170 |
$votesLeft = count( $this->srvsByBucket[$bucket] ); |
| 171 |
$quorum = floor( $votesLeft / 2 + 1 ); |
| 172 |
|
| 173 |
foreach ( $this->srvsByBucket[$bucket] as $lockSrv ) { |
| 174 |
if ( !$this->isServerUp( $lockSrv ) ) { |
| 175 |
--$votesLeft; |
| 176 |
$status->warning( 'lockmanager-fail-svr-acquire', $lockSrv ); |
| 177 |
$this->degradedBuckets[$bucket] = time(); |
| 178 |
continue; |
| 179 |
} |
| 180 |
|
| 181 |
$status->merge( $callback( $lockSrv ) ); |
| 182 |
if ( !$status->isOK() ) { |
| 183 |
return $status; |
| 184 |
} |
| 185 |
++$yesVotes; |
| 186 |
if ( $yesVotes >= $quorum ) { |
| 187 |
return $status; |
| 188 |
} |
| 189 |
--$votesLeft; |
| 190 |
$votesNeeded = $quorum - $yesVotes; |
| 191 |
if ( $votesNeeded > $votesLeft ) { |
| 192 |
break; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
$status->setResult( false ); |
| 197 |
|
| 198 |
return $status; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
final protected function releasePledges( $bucket, callable $callback ) { |
| 209 |
$status = StatusValue::newGood(); |
| 210 |
|
| 211 |
$yesVotes = 0; |
| 212 |
$votesLeft = count( $this->srvsByBucket[$bucket] ); |
| 213 |
$quorum = floor( $votesLeft / 2 + 1 ); |
| 214 |
$isDegraded = isset( $this->degradedBuckets[$bucket] ); |
| 215 |
foreach ( $this->srvsByBucket[$bucket] as $lockSrv ) { |
| 216 |
if ( !$this->isServerUp( $lockSrv ) ) { |
| 217 |
$status->warning( 'lockmanager-fail-svr-release', $lockSrv ); |
| 218 |
} else { |
| 219 |
|
| 220 |
$status->merge( $callback( $lockSrv ) ); |
| 221 |
++$yesVotes; |
| 222 |
|
| 223 |
|
| 224 |
if ( $yesVotes >= $quorum && !$isDegraded ) { |
| 225 |
break; |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
$status->setResult( $yesVotes >= $quorum ); |
| 232 |
|
| 233 |
return $status; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
protected function getBucketFromPath( $path ) { |
| 244 |
$prefix = substr( sha1( $path ), 0, 2 ); |
| 245 |
return (int)base_convert( $prefix, 16, 10 ) % count( $this->srvsByBucket ); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
abstract protected function isServerUp( $lockSrv ); |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
abstract protected function getLocksOnServer( $lockSrv, array $pathsByType ); |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
abstract protected function freeLocksOnServer( $lockSrv, array $pathsByType ); |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
abstract protected function releaseAllLocks(); |
| 285 |
|
| 286 |
final protected function doLock( array $paths, $type ) { |
| 287 |
|
| 288 |
throw new LogicException( __METHOD__ . ': proxy class does not need this method.' ); |
| 289 |
} |
| 290 |
|
| 291 |
final protected function doUnlock( array $paths, $type ) { |
| 292 |
|
| 293 |
throw new LogicException( __METHOD__ . ': proxy class does not need this method.' ); |
| 294 |
} |
| 295 |
} |