Merge "Added another parser test for headings."
[lhc/web/wiklou.git] / includes / filerepo / backend / lockmanager / LSLockManager.php
1 <?php
2 /**
3 * Version of LockManager based on using lock daemon servers.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup LockManager
22 */
23
24 /**
25 * Manage locks using a lock daemon server.
26 *
27 * Version of LockManager based on using lock daemon servers.
28 * This is meant for multi-wiki systems that may share files.
29 * All locks are non-blocking, which avoids deadlocks.
30 *
31 * All lock requests for a resource, identified by a hash string, will map
32 * to one bucket. Each bucket maps to one or several peer servers, each
33 * running LockServerDaemon.php, listening on a designated TCP port.
34 * A majority of peers must agree for a lock to be acquired.
35 *
36 * @ingroup LockManager
37 * @since 1.19
38 */
39 class LSLockManager extends QuorumLockManager {
40 /** @var Array Mapping of lock types to the type actually used */
41 protected $lockTypeMap = array(
42 self::LOCK_SH => self::LOCK_SH,
43 self::LOCK_UW => self::LOCK_SH,
44 self::LOCK_EX => self::LOCK_EX
45 );
46
47 /** @var Array Map of server names to server config */
48 protected $lockServers; // (server name => server config array)
49
50 /** @var Array Map Server connections (server name => resource) */
51 protected $conns = array();
52
53 protected $connTimeout; // float number of seconds
54 protected $session = ''; // random SHA-1 string
55
56 /**
57 * Construct a new instance from configuration.
58 *
59 * $config paramaters include:
60 * - lockServers : Associative array of server names to configuration.
61 * Configuration is an associative array that includes:
62 * - host : IP address/hostname
63 * - port : TCP port
64 * - authKey : Secret string the lock server uses
65 * - srvsByBucket : Array of 1-16 consecutive integer keys, starting from 0,
66 * each having an odd-numbered list of server names (peers) as values.
67 * - connTimeout : Lock server connection attempt timeout. [optional]
68 *
69 * @param Array $config
70 */
71 public function __construct( array $config ) {
72 parent::__construct( $config );
73
74 $this->lockServers = $config['lockServers'];
75 // Sanitize srvsByBucket config to prevent PHP errors
76 $this->srvsByBucket = array_filter( $config['srvsByBucket'], 'is_array' );
77 $this->srvsByBucket = array_values( $this->srvsByBucket ); // consecutive
78
79 if ( isset( $config['connTimeout'] ) ) {
80 $this->connTimeout = $config['connTimeout'];
81 } else {
82 $this->connTimeout = 3; // use some sane amount
83 }
84
85 $this->session = wfRandomString( 32 ); // 128 bits
86 }
87
88 /**
89 * @see QuorumLockManager::getLocksOnServer()
90 * @return Status
91 */
92 protected function getLocksOnServer( $lockSrv, array $paths, $type ) {
93 $status = Status::newGood();
94
95 // Send out the command and get the response...
96 $type = ( $type == self::LOCK_SH ) ? 'SH' : 'EX';
97 $keys = array_unique( array_map( 'LockManager::sha1Base36', $paths ) );
98 $response = $this->sendCommand( $lockSrv, 'ACQUIRE', $type, $keys );
99
100 if ( $response !== 'ACQUIRED' ) {
101 foreach ( $paths as $path ) {
102 $status->fatal( 'lockmanager-fail-acquirelock', $path );
103 }
104 }
105
106 return $status;
107 }
108
109 /**
110 * @see QuorumLockManager::freeLocksOnServer()
111 * @return Status
112 */
113 protected function freeLocksOnServer( $lockSrv, array $paths, $type ) {
114 $status = Status::newGood();
115
116 // Send out the command and get the response...
117 $type = ( $type == self::LOCK_SH ) ? 'SH' : 'EX';
118 $keys = array_unique( array_map( 'LockManager::sha1Base36', $paths ) );
119 $response = $this->sendCommand( $lockSrv, 'RELEASE', $type, $keys );
120
121 if ( $response !== 'RELEASED' ) {
122 foreach ( $paths as $path ) {
123 $status->fatal( 'lockmanager-fail-releaselock', $path );
124 }
125 }
126
127 return $status;
128 }
129
130 /**
131 * @see QuorumLockManager::releaseAllLocks()
132 * @return Status
133 */
134 protected function releaseAllLocks() {
135 $status = Status::newGood();
136
137 foreach ( $this->conns as $lockSrv => $conn ) {
138 $response = $this->sendCommand( $lockSrv, 'RELEASE_ALL', '', array() );
139 if ( $response !== 'RELEASED_ALL' ) {
140 $status->fatal( 'lockmanager-fail-svr-release', $lockSrv );
141 }
142 }
143
144 return $status;
145 }
146
147 /**
148 * @see QuorumLockManager::isServerUp()
149 * @return bool
150 */
151 protected function isServerUp( $lockSrv ) {
152 return (bool)$this->getConnection( $lockSrv );
153 }
154
155 /**
156 * Send a command and get back the response
157 *
158 * @param $lockSrv string
159 * @param $action string
160 * @param $type string
161 * @param $values Array
162 * @return string|bool
163 */
164 protected function sendCommand( $lockSrv, $action, $type, $values ) {
165 $conn = $this->getConnection( $lockSrv );
166 if ( !$conn ) {
167 return false; // no connection
168 }
169 $authKey = $this->lockServers[$lockSrv]['authKey'];
170 // Build of the command as a flat string...
171 $values = implode( '|', $values );
172 $key = sha1( $this->session . $action . $type . $values . $authKey );
173 // Send out the command...
174 if ( fwrite( $conn, "{$this->session}:$key:$action:$type:$values\n" ) === false ) {
175 return false;
176 }
177 // Get the response...
178 $response = fgets( $conn );
179 if ( $response === false ) {
180 return false;
181 }
182 return trim( $response );
183 }
184
185 /**
186 * Get (or reuse) a connection to a lock server
187 *
188 * @param $lockSrv string
189 * @return resource
190 */
191 protected function getConnection( $lockSrv ) {
192 if ( !isset( $this->conns[$lockSrv] ) ) {
193 $cfg = $this->lockServers[$lockSrv];
194 wfSuppressWarnings();
195 $errno = $errstr = '';
196 $conn = fsockopen( $cfg['host'], $cfg['port'], $errno, $errstr, $this->connTimeout );
197 wfRestoreWarnings();
198 if ( $conn === false ) {
199 return null;
200 }
201 $sec = floor( $this->connTimeout );
202 $usec = floor( ( $this->connTimeout - floor( $this->connTimeout ) ) * 1e6 );
203 stream_set_timeout( $conn, $sec, $usec );
204 $this->conns[$lockSrv] = $conn;
205 }
206 return $this->conns[$lockSrv];
207 }
208
209 /**
210 * Make sure remaining locks get cleared for sanity
211 */
212 function __destruct() {
213 $this->releaseAllLocks();
214 foreach ( $this->conns as $conn ) {
215 fclose( $conn );
216 }
217 }
218 }