3 * @defgroup LockManager Lock management
6 use Psr\Log\LoggerInterface
;
9 * Resource locking handling.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
27 * @ingroup LockManager
28 * @author Aaron Schulz
32 * @brief Class for handling resource locking.
34 * Locks on resource keys can either be shared or exclusive.
36 * Implementations must keep track of what is locked by this proccess
37 * in-memory and support nested locking calls (using reference counting).
38 * At least LOCK_UW and LOCK_EX must be implemented. LOCK_SH can be a no-op.
39 * Locks should either be non-blocking or have low wait timeouts.
41 * Subclasses should avoid throwing exceptions at all costs.
43 * @ingroup LockManager
46 abstract class LockManager
{
47 /** @var LoggerInterface */
50 /** @var array Mapping of lock types to the type actually used */
51 protected $lockTypeMap = [
52 self
::LOCK_SH
=> self
::LOCK_SH
,
53 self
::LOCK_UW
=> self
::LOCK_EX
, // subclasses may use self::LOCK_SH
54 self
::LOCK_EX
=> self
::LOCK_EX
57 /** @var array Map of (resource path => lock type => count) */
58 protected $locksHeld = [];
60 protected $domain; // string; domain (usually wiki ID)
61 protected $lockTTL; // integer; maximum time locks can be held
63 /** @var string Random 32-char hex number */
66 /** Lock types; stronger locks have higher values */
67 const LOCK_SH
= 1; // shared lock (for reads)
68 const LOCK_UW
= 2; // shared lock (for reads used to write elsewhere)
69 const LOCK_EX
= 3; // exclusive lock (for writes)
72 * Construct a new instance from configuration
74 * @param array $config Parameters include:
75 * - domain : Domain (usually wiki ID) that all resources are relative to [optional]
76 * - lockTTL : Age (in seconds) at which resource locks should expire.
77 * This only applies if locks are not tied to a connection/process.
79 public function __construct( array $config ) {
80 $this->domain
= isset( $config['domain'] ) ?
$config['domain'] : 'global';
81 if ( isset( $config['lockTTL'] ) ) {
82 $this->lockTTL
= max( 5, $config['lockTTL'] );
83 } elseif ( PHP_SAPI
=== 'cli' ) {
84 $this->lockTTL
= 3600;
86 $met = ini_get( 'max_execution_time' ); // this is 0 in CLI mode
87 $this->lockTTL
= max( 5 * 60, 2 * (int)$met );
91 for ( $i = 1; $i <= 5; ++
$i ) {
92 $random[] = mt_rand( 0, 0xFFFFFFF );
94 $this->session
= md5( implode( '-', $random ) );
96 $this->logger
= isset( $config['logger'] ) ?
$config['logger'] : new \Psr\Log\
NullLogger();
100 * Lock the resources at the given abstract paths
102 * @param array $paths List of resource names
103 * @param int $type LockManager::LOCK_* constant
104 * @param int $timeout Timeout in seconds (0 means non-blocking) (since 1.21)
105 * @return StatusValue
107 final public function lock( array $paths, $type = self
::LOCK_EX
, $timeout = 0 ) {
108 return $this->lockByType( [ $type => $paths ], $timeout );
112 * Lock the resources at the given abstract paths
114 * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
115 * @param int $timeout Timeout in seconds (0 means non-blocking) (since 1.21)
116 * @return StatusValue
119 final public function lockByType( array $pathsByType, $timeout = 0 ) {
120 $pathsByType = $this->normalizePathsByType( $pathsByType );
123 $loop = new WaitConditionLoop(
124 function () use ( &$status, $pathsByType ) {
125 $status = $this->doLockByType( $pathsByType );
127 return $status->isOK() ?
: WaitConditionLoop
::CONDITION_CONTINUE
;
137 * Unlock the resources at the given abstract paths
139 * @param array $paths List of paths
140 * @param int $type LockManager::LOCK_* constant
141 * @return StatusValue
143 final public function unlock( array $paths, $type = self
::LOCK_EX
) {
144 return $this->unlockByType( [ $type => $paths ] );
148 * Unlock the resources at the given abstract paths
150 * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
151 * @return StatusValue
154 final public function unlockByType( array $pathsByType ) {
155 $pathsByType = $this->normalizePathsByType( $pathsByType );
156 $status = $this->doUnlockByType( $pathsByType );
162 * Get the base 36 SHA-1 of a string, padded to 31 digits.
163 * Before hashing, the path will be prefixed with the domain ID.
164 * This should be used interally for lock key or file names.
166 * @param string $path
169 final protected function sha1Base36Absolute( $path ) {
170 return Wikimedia\base_convert
( sha1( "{$this->domain}:{$path}" ), 16, 36, 31 );
174 * Get the base 16 SHA-1 of a string, padded to 31 digits.
175 * Before hashing, the path will be prefixed with the domain ID.
176 * This should be used interally for lock key or file names.
178 * @param string $path
181 final protected function sha1Base16Absolute( $path ) {
182 return sha1( "{$this->domain}:{$path}" );
186 * Normalize the $paths array by converting LOCK_UW locks into the
187 * appropriate type and removing any duplicated paths for each lock type.
189 * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
193 final protected function normalizePathsByType( array $pathsByType ) {
195 foreach ( $pathsByType as $type => $paths ) {
196 $res[$this->lockTypeMap
[$type]] = array_unique( $paths );
203 * @see LockManager::lockByType()
204 * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
205 * @return StatusValue
208 protected function doLockByType( array $pathsByType ) {
209 $status = StatusValue
::newGood();
210 $lockedByType = []; // map of (type => paths)
211 foreach ( $pathsByType as $type => $paths ) {
212 $status->merge( $this->doLock( $paths, $type ) );
213 if ( $status->isOK() ) {
214 $lockedByType[$type] = $paths;
216 // Release the subset of locks that were acquired
217 foreach ( $lockedByType as $lType => $lPaths ) {
218 $status->merge( $this->doUnlock( $lPaths, $lType ) );
228 * Lock resources with the given keys and lock type
230 * @param array $paths List of paths
231 * @param int $type LockManager::LOCK_* constant
232 * @return StatusValue
234 abstract protected function doLock( array $paths, $type );
237 * @see LockManager::unlockByType()
238 * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
239 * @return StatusValue
242 protected function doUnlockByType( array $pathsByType ) {
243 $status = StatusValue
::newGood();
244 foreach ( $pathsByType as $type => $paths ) {
245 $status->merge( $this->doUnlock( $paths, $type ) );
252 * Unlock resources with the given keys and lock type
254 * @param array $paths List of paths
255 * @param int $type LockManager::LOCK_* constant
256 * @return StatusValue
258 abstract protected function doUnlock( array $paths, $type );