MediaWiki master
QuorumLockManager.php
Go to the documentation of this file.
1<?php
6namespace Wikimedia\LockManager;
7
8use StatusValue;
9use Wikimedia\ArrayUtils\ArrayUtils;
10
22abstract class QuorumLockManager extends LockManager {
24 protected $lockServers = [];
26 protected $downServers = [];
28 protected $minVotes = 2;
29
31 final protected function doLockByType( array $pathsByType ) {
32 $status = StatusValue::newGood();
33 $lockedPaths = []; // files locked in this attempt (type => paths)
34 foreach ( $pathsByType as $type => $paths ) {
35 foreach ( $paths as $path ) {
36 if ( isset( $this->locksHeld[$path][$type] ) ) {
37 ++$this->locksHeld[$path][$type];
38 } else {
39 $lockedPaths[$type][] = $path;
40 $status->merge( $this->doLockingRequest( $path, $type ) );
41 if ( !$status->isOK() ) {
42 $status->merge( $this->doUnlockByType( $lockedPaths ) );
43 return $status;
44 } else {
45 $this->locksHeld[$path][$type] = 1; // locked
46 }
47 }
48 }
49 }
50
51 return $status;
52 }
53
61 protected function doUnlockByType( array $pathsByType ) {
62 $status = StatusValue::newGood();
63
64 foreach ( $pathsByType as $type => $paths ) {
65 foreach ( $paths as $path ) {
66 if ( !isset( $this->locksHeld[$path][$type] ) ) {
67 $status->warning( 'lockmanager-notlocked', $path );
68 } else {
69 --$this->locksHeld[$path][$type];
70 // Reference count the locks held and release locks when zero
71 if ( $this->locksHeld[$path][$type] <= 0 ) {
72 $status->merge( $this->doUnlockingRequest( $path, $type ) );
73 unset( $this->locksHeld[$path][$type] );
74 }
75 if ( $this->locksHeld[$path] === [] ) {
76 unset( $this->locksHeld[$path] ); // no SH or EX locks left for key
77 }
78 }
79 }
80 }
81
82 if ( $this->locksHeld === [] ) {
83 $status->merge( $this->releaseAllLocks() );
84 }
85
86 return $status;
87 }
88
97 final protected function doLockingRequest( string $path, string $type ) {
98 $status = StatusValue::newGood();
99 $yesVotes = 0; // locks made on trustable servers
100 $votesLeft = count( $this->lockServers ); // remaining peers
101 $sortedServers = array_keys( $this->lockServers );
102 // shuffle the servers based on hashing of the keys
103 ArrayUtils::consistentHashSort( $sortedServers, $path );
104 $minVotes = min( $this->minVotes, count( $this->lockServers ) );
105 foreach ( $sortedServers as $lockServer ) {
106 if ( !$this->isServerUp( $lockServer ) ) {
107 --$votesLeft;
108 $status->warning( 'lockmanager-fail-svr-acquire', $lockServer );
109 $this->downServers[$lockServer] = time();
110 continue;
111 }
112 $status->merge( $this->getLocksOnServer( $lockServer, [ $type => [ $path ] ] ) );
113 if ( !$status->isOK() ) {
114 return $status; // vetoed; resource locked
115 }
116 ++$yesVotes; // success for this peer
117 if ( $yesVotes >= $minVotes ) {
118 return $status; // lock obtained
119 }
120 --$votesLeft;
121 $votesNeeded = $minVotes - $yesVotes;
122 if ( $votesNeeded > $votesLeft ) {
123 break; // short-circuit
124 }
125 }
126 // At this point, we must not have met the quorum
127 $status->setResult( false );
128
129 return $status;
130 }
131
139 final protected function doUnlockingRequest( string $path, string $type ) {
140 $status = StatusValue::newGood();
141
142 $yesVotes = 0; // locks freed on trustable servers
143 $isDegraded = count( $this->downServers ); // not the normal quorum?
144 $sortedServers = array_keys( $this->lockServers );
145 // shuffle the servers based on hashing of the keys
146 ArrayUtils::consistentHashSort( $sortedServers, $path );
147 $minVotes = min( $this->minVotes, count( $this->lockServers ) );
148 foreach ( $sortedServers as $lockServer ) {
149 if ( !$this->isServerUp( $lockServer ) ) {
150 $status->warning( 'lockmanager-fail-svr-release', $lockServer );
151 } else {
152 // Attempt to release the lock on this peer
153 $status->merge( $this->freeLocksOnServer( $lockServer, [ $type => [ $path ] ] ) );
154 ++$yesVotes; // success for this peer
155 // Normally the first peers form the quorum, and the others are ignored.
156 // Ignore them in this case, but not when an alternative quorum was used.
157 if ( $yesVotes >= $minVotes && !$isDegraded ) {
158 break; // lock released
159 }
160 }
161 }
162
163 // Set a bad StatusValue if the quorum was not met.
164 // Assumes the same "up" servers as during the acquire step.
165 $status->setResult( $yesVotes >= $minVotes );
166
167 return $status;
168 }
169
177 abstract protected function isServerUp( $lockServer );
178
186 abstract protected function getLocksOnServer( $lockServer, array $pathsByType );
187
197 abstract protected function freeLocksOnServer( $lockServer, array $pathsByType );
198
206 abstract protected function releaseAllLocks();
207}
208
209class_alias( QuorumLockManager::class, 'QuorumLockManager' );
Resource locking handling.
Base class for lock managers that use a quorum of peer servers for locks.
freeLocksOnServer( $lockServer, array $pathsByType)
Get a connection to a lock server and release locks on $paths.
isServerUp( $lockServer)
Check if a lock server is up.
array $lockServers
Map server names to data used by subclasses.
releaseAllLocks()
Release all locks that this session is holding.
array $downServers
list of down server names
doLockingRequest(string $path, string $type)
Attempt to acquire locks with the peers.
getLocksOnServer( $lockServer, array $pathsByType)
Get a connection to a lock server and acquire locks.
doLockByType(array $pathsByType)
Acquire a set of locks without blocking.If some locks could not be acquired, then the other locks sho...
int $minVotes
Minimum number of success to consider lock/unlock successful.
doUnlockingRequest(string $path, string $type)
Attempt to release locks with the peers.