MediaWiki master
DatabaseLockManager.php
Go to the documentation of this file.
1<?php
2
4
5use InvalidArgumentException;
6use LogicException;
7use StatusValue;
9
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}
A simplified Database Lock Manager.
__construct(array $config)
Construct a new instance from configuration.
doUnlockByType(array $pathsByType)
Release a set of locks.StatusValue LockManager\unlockByType()
lockByType(array $pathsByType, $timeout=0)
Lock the resources at the given abstract paths.StatusValue 1.22
doLockByType(array $pathsByType)
Since we are overriding lockByType, this should never be reached.
Resource locking handling.
normalizePathsByType(array $pathsByType)
Normalize the $paths array by converting LOCK_UW locks into the appropriate type and removing any dup...
sha1Base36Absolute( $path)
Get the base 36 SHA-1 of a string, padded to 31 digits.
Provide primary and replica IDatabase connections.