MediaWiki
master
Toggle main menu visibility
DatabaseLockManager.php
Go to the documentation of this file.
1
<?php
2
3
namespace
Wikimedia\LockManager
;
4
5
use InvalidArgumentException;
6
use LogicException;
7
use StatusValue;
8
use
Wikimedia\Rdbms\IConnectionProvider
;
9
13
class
DatabaseLockManager
extends
LockManager
{
14
16
private
IConnectionProvider
$dbProvider;
17
22
public
function
__construct
( array $config ) {
23
parent::__construct( $config );
24
25
// Inject the connection provider
26
$this->dbProvider = $config[
'dbProvider'
];
27
}
28
32
public
function
lockByType
( array $pathsByType, $timeout = 0 ) {
33
$status = StatusValue::newGood();
34
$pathsByType = $this->
normalizePathsByType
( $pathsByType );
35
36
foreach
( $pathsByType as $type => $keys ) {
37
if
( $type == self::LOCK_UW ) {
38
throw
new
InvalidArgumentException(
39
'DatabaseLockManager does not support LOCK_UW'
40
);
41
}
42
if
( $type == self::LOCK_SH ) {
43
// LOCK_SH is no-op
44
continue
;
45
}
46
foreach
( $keys as $key ) {
47
$res = $this->dbProvider->getPrimaryDatabase()->lock(
48
$this->
sha1Base36Absolute
( $key ),
49
// It's not really __METHOD__ but maybe better than nothing specially
50
// given the fact that the locked key is a hash
51
substr(
"{$this->domain}:{$key}"
, 0, 15 ),
52
$timeout
53
);
54
55
if
( !$res ) {
56
// If one lock fails, fail the batch.
57
$status->fatal(
'lockmanager-fail-acquirelock'
, $key );
58
}
59
}
60
}
61
62
return
$status;
63
}
64
68
protected
function
doUnlockByType
( array $pathsByType ) {
69
$status = StatusValue::newGood();
70
foreach
( $pathsByType as $type => $keys ) {
71
if
( $type == self::LOCK_UW ) {
72
throw
new
InvalidArgumentException(
73
'DatabaseLockManager does not support LOCK_UW'
74
);
75
}
76
if
( $type == self::LOCK_SH ) {
77
// LOCK_SH is no-op
78
continue
;
79
}
80
foreach
( $keys as $key ) {
81
$res = $this->dbProvider->getPrimaryDatabase()->unlock(
82
$this->
sha1Base36Absolute
( $key ),
83
// It's not really __METHOD__ but maybe better than nothing specially
84
// given the fact that the locked key is a hash
85
substr(
"{$this->domain}:{$key}"
, 0, 15 )
86
);
87
88
if
( !$res ) {
89
// If one lock fails, fail the batch.
90
$status->fatal(
'lockmanager-fail-unlock'
, $key );
91
}
92
}
93
}
94
95
return
$status;
96
}
97
103
protected
function
doLockByType
( array $pathsByType ) {
104
throw
new
LogicException(
105
'DatabaseLockManager does not implement ::doLockByType'
106
);
107
}
108
}
Wikimedia\LockManager\DatabaseLockManager
A simplified Database Lock Manager.
Definition
DatabaseLockManager.php:13
Wikimedia\LockManager\DatabaseLockManager\__construct
__construct(array $config)
Construct a new instance from configuration.
Definition
DatabaseLockManager.php:22
Wikimedia\LockManager\DatabaseLockManager\doUnlockByType
doUnlockByType(array $pathsByType)
Release a set of locks.StatusValue LockManager\unlockByType()
Definition
DatabaseLockManager.php:68
Wikimedia\LockManager\DatabaseLockManager\lockByType
lockByType(array $pathsByType, $timeout=0)
Lock the resources at the given abstract paths.StatusValue 1.22
Definition
DatabaseLockManager.php:32
Wikimedia\LockManager\DatabaseLockManager\doLockByType
doLockByType(array $pathsByType)
Since we are overriding lockByType, this should never be reached.
Definition
DatabaseLockManager.php:103
Wikimedia\LockManager\LockManager
Resource locking handling.
Definition
LockManager.php:37
Wikimedia\LockManager\LockManager\normalizePathsByType
normalizePathsByType(array $pathsByType)
Normalize the $paths array by converting LOCK_UW locks into the appropriate type and removing any dup...
Definition
LockManager.php:194
Wikimedia\LockManager\LockManager\sha1Base36Absolute
sha1Base36Absolute( $path)
Get the base 36 SHA-1 of a string, padded to 31 digits.
Definition
LockManager.php:182
Wikimedia\Rdbms\IConnectionProvider
Provide primary and replica IDatabase connections.
Definition
IConnectionProvider.php:21
Wikimedia\LockManager
Definition
DatabaseLockManager.php:3
includes
libs
LockManager
DatabaseLockManager.php
Generated on
for MediaWiki by
1.18.0