Merge "Update formatting of file backend classes"
[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 * $config paramaters 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 * @param $config Array
73 */
74 public function __construct( array $config ) {
75 $this->domain = isset( $config['domain'] ) ? $config['domain'] : wfWikiID();
76 if ( isset( $config['lockTTL'] ) ) {
77 $this->lockTTL = max( 1, $config['lockTTL'] );
78 } elseif ( PHP_SAPI === 'cli' ) {
79 $this->lockTTL = 2 * 3600;
80 } else {
81 $met = ini_get( 'max_execution_time' ); // this is 0 in CLI mode
82 $this->lockTTL = max( 5 * 60, 2 * (int)$met );
83 }
84 }
85
86 /**
87 * Lock the resources at the given abstract paths
88 *
89 * @param array $paths List of resource names
90 * @param $type integer LockManager::LOCK_* constant
91 * @param integer $timeout Timeout in seconds (0 means non-blocking) (since 1.21)
92 * @return Status
93 */
94 final public function lock( array $paths, $type = self::LOCK_EX, $timeout = 0 ) {
95 return $this->lockByType( array( $type => $paths ), $timeout );
96 }
97
98 /**
99 * Lock the resources at the given abstract paths
100 *
101 * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
102 * @param integer $timeout Timeout in seconds (0 means non-blocking) (since 1.21)
103 * @return Status
104 * @since 1.22
105 */
106 final public function lockByType( array $pathsByType, $timeout = 0 ) {
107 wfProfileIn( __METHOD__ );
108 $status = Status::newGood();
109 $pathsByType = $this->normalizePathsByType( $pathsByType );
110 $msleep = array( 0, 50, 100, 300, 500 ); // retry backoff times
111 $start = microtime( true );
112 do {
113 $status = $this->doLockByType( $pathsByType );
114 $elapsed = microtime( true ) - $start;
115 if ( $status->isOK() || $elapsed >= $timeout || $elapsed < 0 ) {
116 break; // success, timeout, or clock set back
117 }
118 usleep( 1e3 * ( next( $msleep ) ?: 1000 ) ); // use 1 sec after enough times
119 $elapsed = microtime( true ) - $start;
120 } while ( $elapsed < $timeout && $elapsed >= 0 );
121 wfProfileOut( __METHOD__ );
122
123 return $status;
124 }
125
126 /**
127 * Unlock the resources at the given abstract paths
128 *
129 * @param array $paths List of paths
130 * @param $type integer LockManager::LOCK_* constant
131 * @return Status
132 */
133 final public function unlock( array $paths, $type = self::LOCK_EX ) {
134 return $this->unlockByType( array( $type => $paths ) );
135 }
136
137 /**
138 * Unlock the resources at the given abstract paths
139 *
140 * @param array $pathsByType Map of LockManager::LOCK_* constants to lists of paths
141 * @return Status
142 * @since 1.22
143 */
144 final public function unlockByType( array $pathsByType ) {
145 wfProfileIn( __METHOD__ );
146 $pathsByType = $this->normalizePathsByType( $pathsByType );
147 $status = $this->doUnlockByType( $pathsByType );
148 wfProfileOut( __METHOD__ );
149
150 return $status;
151 }
152
153 /**
154 * Get the base 36 SHA-1 of a string, padded to 31 digits.
155 * Before hashing, the path will be prefixed with the domain ID.
156 * This should be used interally for lock key or file names.
157 *
158 * @param $path string
159 * @return string
160 */
161 final protected function sha1Base36Absolute( $path ) {
162 return wfBaseConvert( sha1( "{$this->domain}:{$path}" ), 16, 36, 31 );
163 }
164
165 /**
166 * Get the base 16 SHA-1 of a string, padded to 31 digits.
167 * Before hashing, the path will be prefixed with the domain ID.
168 * This should be used interally for lock key or file names.
169 *
170 * @param $path string
171 * @return string
172 */
173 final protected function sha1Base16Absolute( $path ) {
174 return sha1( "{$this->domain}:{$path}" );
175 }
176
177 /**
178 * Normalize the $paths array by converting LOCK_UW locks into the
179 * appropriate type and removing any duplicated paths for each lock type.
180 *
181 * @param array $paths Map of LockManager::LOCK_* constants to lists of paths
182 * @return Array
183 * @since 1.22
184 */
185 final protected function normalizePathsByType( array $pathsByType ) {
186 $res = array();
187 foreach ( $pathsByType as $type => $paths ) {
188 $res[$this->lockTypeMap[$type]] = array_unique( $paths );
189 }
190
191 return $res;
192 }
193
194 /**
195 * @see LockManager::lockByType()
196 * @param array $paths Map of LockManager::LOCK_* constants to lists of paths
197 * @return Status
198 * @since 1.22
199 */
200 protected function doLockByType( array $pathsByType ) {
201 $status = Status::newGood();
202 $lockedByType = array(); // map of (type => paths)
203 foreach ( $pathsByType as $type => $paths ) {
204 $status->merge( $this->doLock( $paths, $type ) );
205 if ( $status->isOK() ) {
206 $lockedByType[$type] = $paths;
207 } else {
208 // Release the subset of locks that were acquired
209 foreach ( $lockedByType as $type => $paths ) {
210 $status->merge( $this->doUnlock( $paths, $type ) );
211 }
212 break;
213 }
214 }
215
216 return $status;
217 }
218
219 /**
220 * Lock resources with the given keys and lock type
221 *
222 * @param array $paths List of paths
223 * @param $type integer LockManager::LOCK_* constant
224 * @return Status
225 */
226 abstract protected function doLock( array $paths, $type );
227
228 /**
229 * @see LockManager::unlockByType()
230 * @param array $paths Map of LockManager::LOCK_* constants to lists of paths
231 * @return Status
232 * @since 1.22
233 */
234 protected function doUnlockByType( array $pathsByType ) {
235 $status = Status::newGood();
236 foreach ( $pathsByType as $type => $paths ) {
237 $status->merge( $this->doUnlock( $paths, $type ) );
238 }
239
240 return $status;
241 }
242
243 /**
244 * Unlock resources with the given keys and lock type
245 *
246 * @param array $paths List of paths
247 * @param $type integer LockManager::LOCK_* constant
248 * @return Status
249 */
250 abstract protected function doUnlock( array $paths, $type );
251 }
252
253 /**
254 * Simple version of LockManager that does nothing
255 * @since 1.19
256 */
257 class NullLockManager extends LockManager {
258 protected function doLock( array $paths, $type ) {
259 return Status::newGood();
260 }
261
262 protected function doUnlock( array $paths, $type ) {
263 return Status::newGood();
264 }
265 }