Merge "Don't localize parentheses in version number in parserTests.php"
[lhc/web/wiklou.git] / includes / filebackend / lockmanager / LockManager.php
1 <?php
2 /**
3 * @defgroup LockManager Lock management
4 * @ingroup FileBackend
5 */
6
7 /**
8 * Resource locking handling.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @file
26 * @ingroup LockManager
27 * @author Aaron Schulz
28 */
29
30 /**
31 * @brief Class for handling resource locking.
32 *
33 * Locks on resource keys can either be shared or exclusive.
34 *
35 * Implementations must keep track of what is locked by this proccess
36 * in-memory and support nested locking calls (using reference counting).
37 * At least LOCK_UW and LOCK_EX must be implemented. LOCK_SH can be a no-op.
38 * Locks should either be non-blocking or have low wait timeouts.
39 *
40 * Subclasses should avoid throwing exceptions at all costs.
41 *
42 * @ingroup LockManager
43 * @since 1.19
44 */
45 abstract class LockManager {
46 /** @var array Mapping of lock types to the type actually used */
47 protected $lockTypeMap = array(
48 self::LOCK_SH => self::LOCK_SH,
49 self::LOCK_UW => self::LOCK_EX, // subclasses may use self::LOCK_SH
50 self::LOCK_EX => self::LOCK_EX
51 );
52
53 /** @var array Map of (resource path => lock type => count) */
54 protected $locksHeld = array();
55
56 protected $domain; // string; domain (usually wiki ID)
57 protected $lockTTL; // integer; maximum time locks can be held
58
59 /** Lock types; stronger locks have higher values */
60 const LOCK_SH = 1; // shared lock (for reads)
61 const LOCK_UW = 2; // shared lock (for reads used to write elsewhere)
62 const LOCK_EX = 3; // exclusive lock (for writes)
63
64 /**
65 * Construct a new instance from configuration
66 *
67 * @param array $config Parameters include:
68 * - domain : Domain (usually wiki ID) that all resources are relative to [optional]
69 * - lockTTL : Age (in seconds) at which resource locks should expire.
70 * This only applies if locks are not tied to a connection/process.
71 */
72 public function __construct( array $config ) {
73 $this->domain = isset( $config['domain'] ) ? $config['domain'] : wfWikiID();
74 if ( isset( $config['lockTTL'] ) ) {
75 $this->lockTTL = max( 5, $config['lockTTL'] );
76 } elseif ( PHP_SAPI === 'cli' ) {
77 $this->lockTTL = 3600;
78 } else {
79 $met = ini_get( 'max_execution_time' ); // this is 0 in CLI mode
80 $this->lockTTL = max( 5 * 60, 2 * (int)$met );
81 }
82 }
83
84 /**
85 * Lock the resources at the given abstract paths
86 *
87 * @param array $paths List of resource names
88 * @param int $type LockManager::LOCK_* constant
89 * @param int $timeout Timeout in seconds (0 means non-blocking) (since 1.21)
90 * @return Status
91 */
92 final public function lock( array $paths, $type = self::LOCK_EX, $timeout = 0 ) {
93 return $this->lockByType( array( $type => $paths ), $timeout );
94 }
95
96 /**
97 * Lock the resources at the given abstract paths
98 *
99 * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
100 * @param int $timeout Timeout in seconds (0 means non-blocking) (since 1.21)
101 * @return Status
102 * @since 1.22
103 */
104 final public function lockByType( array $pathsByType, $timeout = 0 ) {
105 $status = Status::newGood();
106 $pathsByType = $this->normalizePathsByType( $pathsByType );
107 $msleep = array( 0, 50, 100, 300, 500 ); // retry backoff times
108 $start = microtime( true );
109 do {
110 $status = $this->doLockByType( $pathsByType );
111 $elapsed = microtime( true ) - $start;
112 if ( $status->isOK() || $elapsed >= $timeout || $elapsed < 0 ) {
113 break; // success, timeout, or clock set back
114 }
115 usleep( 1e3 * ( next( $msleep ) ?: 1000 ) ); // use 1 sec after enough times
116 $elapsed = microtime( true ) - $start;
117 } while ( $elapsed < $timeout && $elapsed >= 0 );
118
119 return $status;
120 }
121
122 /**
123 * Unlock the resources at the given abstract paths
124 *
125 * @param array $paths List of paths
126 * @param int $type LockManager::LOCK_* constant
127 * @return Status
128 */
129 final public function unlock( array $paths, $type = self::LOCK_EX ) {
130 return $this->unlockByType( array( $type => $paths ) );
131 }
132
133 /**
134 * Unlock the resources at the given abstract paths
135 *
136 * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
137 * @return Status
138 * @since 1.22
139 */
140 final public function unlockByType( array $pathsByType ) {
141 $pathsByType = $this->normalizePathsByType( $pathsByType );
142 $status = $this->doUnlockByType( $pathsByType );
143
144 return $status;
145 }
146
147 /**
148 * Get the base 36 SHA-1 of a string, padded to 31 digits.
149 * Before hashing, the path will be prefixed with the domain ID.
150 * This should be used interally for lock key or file names.
151 *
152 * @param string $path
153 * @return string
154 */
155 final protected function sha1Base36Absolute( $path ) {
156 return wfBaseConvert( sha1( "{$this->domain}:{$path}" ), 16, 36, 31 );
157 }
158
159 /**
160 * Get the base 16 SHA-1 of a string, padded to 31 digits.
161 * Before hashing, the path will be prefixed with the domain ID.
162 * This should be used interally for lock key or file names.
163 *
164 * @param string $path
165 * @return string
166 */
167 final protected function sha1Base16Absolute( $path ) {
168 return sha1( "{$this->domain}:{$path}" );
169 }
170
171 /**
172 * Normalize the $paths array by converting LOCK_UW locks into the
173 * appropriate type and removing any duplicated paths for each lock type.
174 *
175 * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
176 * @return array
177 * @since 1.22
178 */
179 final protected function normalizePathsByType( array $pathsByType ) {
180 $res = array();
181 foreach ( $pathsByType as $type => $paths ) {
182 $res[$this->lockTypeMap[$type]] = array_unique( $paths );
183 }
184
185 return $res;
186 }
187
188 /**
189 * @see LockManager::lockByType()
190 * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
191 * @return Status
192 * @since 1.22
193 */
194 protected function doLockByType( array $pathsByType ) {
195 $status = Status::newGood();
196 $lockedByType = array(); // map of (type => paths)
197 foreach ( $pathsByType as $type => $paths ) {
198 $status->merge( $this->doLock( $paths, $type ) );
199 if ( $status->isOK() ) {
200 $lockedByType[$type] = $paths;
201 } else {
202 // Release the subset of locks that were acquired
203 foreach ( $lockedByType as $lType => $lPaths ) {
204 $status->merge( $this->doUnlock( $lPaths, $lType ) );
205 }
206 break;
207 }
208 }
209
210 return $status;
211 }
212
213 /**
214 * Lock resources with the given keys and lock type
215 *
216 * @param array $paths List of paths
217 * @param int $type LockManager::LOCK_* constant
218 * @return Status
219 */
220 abstract protected function doLock( array $paths, $type );
221
222 /**
223 * @see LockManager::unlockByType()
224 * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
225 * @return Status
226 * @since 1.22
227 */
228 protected function doUnlockByType( array $pathsByType ) {
229 $status = Status::newGood();
230 foreach ( $pathsByType as $type => $paths ) {
231 $status->merge( $this->doUnlock( $paths, $type ) );
232 }
233
234 return $status;
235 }
236
237 /**
238 * Unlock resources with the given keys and lock type
239 *
240 * @param array $paths List of paths
241 * @param int $type LockManager::LOCK_* constant
242 * @return Status
243 */
244 abstract protected function doUnlock( array $paths, $type );
245 }
246
247 /**
248 * Simple version of LockManager that does nothing
249 * @since 1.19
250 */
251 class NullLockManager extends LockManager {
252 protected function doLock( array $paths, $type ) {
253 return Status::newGood();
254 }
255
256 protected function doUnlock( array $paths, $type ) {
257 return Status::newGood();
258 }
259 }