[LockManager] Added a RedisLockManager class.
[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 wfProfileIn( __METHOD__ );
96 $msleep = array( 0, 50, 100, 300, 500 ); // retry backoff times
97 $start = microtime( true );
98 do {
99 $status = $this->doLock( array_unique( $paths ), $this->lockTypeMap[$type] );
100 $elapsed = microtime( true ) - $start;
101 if ( $status->isOK() || $elapsed >= $timeout || $elapsed < 0 ) {
102 break; // success, timeout, or clock set back
103 }
104 usleep( 1e3 * ( next( $msleep ) ?: 1000 ) ); // use 1 sec after enough times
105 $elapsed = microtime( true ) - $start;
106 } while ( $elapsed < $timeout && $elapsed >= 0 );
107 wfProfileOut( __METHOD__ );
108 return $status;
109 }
110
111 /**
112 * Unlock the resources at the given abstract paths
113 *
114 * @param array $paths List of storage paths
115 * @param $type integer LockManager::LOCK_* constant
116 * @return Status
117 */
118 final public function unlock( array $paths, $type = self::LOCK_EX ) {
119 wfProfileIn( __METHOD__ );
120 $status = $this->doUnlock( array_unique( $paths ), $this->lockTypeMap[$type] );
121 wfProfileOut( __METHOD__ );
122 return $status;
123 }
124
125 /**
126 * Get the base 36 SHA-1 of a string, padded to 31 digits.
127 * Before hashing, the path will be prefixed with the domain ID.
128 * This should be used interally for lock key or file names.
129 *
130 * @param $path string
131 * @return string
132 */
133 final protected function sha1Base36Absolute( $path ) {
134 return wfBaseConvert( sha1( "{$this->domain}:{$path}" ), 16, 36, 31 );
135 }
136
137 /**
138 * Get the base 16 SHA-1 of a string, padded to 31 digits.
139 * Before hashing, the path will be prefixed with the domain ID.
140 * This should be used interally for lock key or file names.
141 *
142 * @param $path string
143 * @return string
144 */
145 final protected function sha1Base16Absolute( $path ) {
146 return sha1( "{$this->domain}:{$path}" );
147 }
148
149 /**
150 * Lock resources with the given keys and lock type
151 *
152 * @param array $paths List of storage paths
153 * @param $type integer LockManager::LOCK_* constant
154 * @return Status
155 */
156 abstract protected function doLock( array $paths, $type );
157
158 /**
159 * Unlock resources with the given keys and lock type
160 *
161 * @param array $paths List of storage paths
162 * @param $type integer LockManager::LOCK_* constant
163 * @return Status
164 */
165 abstract protected function doUnlock( array $paths, $type );
166 }
167
168 /**
169 * Simple version of LockManager that does nothing
170 * @since 1.19
171 */
172 class NullLockManager extends LockManager {
173 /**
174 * @see LockManager::doLock()
175 * @param $paths array
176 * @param $type int
177 * @return Status
178 */
179 protected function doLock( array $paths, $type ) {
180 return Status::newGood();
181 }
182
183 /**
184 * @see LockManager::doUnlock()
185 * @param $paths array
186 * @param $type int
187 * @return Status
188 */
189 protected function doUnlock( array $paths, $type ) {
190 return Status::newGood();
191 }
192 }