Merge "Typo fix"
[mediawiki.git] / includes / filebackend / lockmanager / LSLockManager.php
blob97de8dca6a39f9c9af24ab7a2a56691328115720
1 <?php
2 /**
3 * Version of LockManager based on using lock daemon servers.
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.
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.
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
20 * @file
21 * @ingroup LockManager
24 /**
25 * Manage locks using a lock daemon server.
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.
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.
36 * @ingroup LockManager
37 * @since 1.19
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
47 /** @var Array Map of server names to server config */
48 protected $lockServers; // (server name => server config array)
50 /** @var Array Map Server connections (server name => resource) */
51 protected $conns = array();
53 protected $connTimeout; // float number of seconds
54 protected $session = ''; // random SHA-1 string
56 /**
57 * Construct a new instance from configuration.
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]
69 * @param array $config
71 public function __construct( array $config ) {
72 parent::__construct( $config );
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
79 if ( isset( $config['connTimeout'] ) ) {
80 $this->connTimeout = $config['connTimeout'];
81 } else {
82 $this->connTimeout = 3; // use some sane amount
85 $this->session = wfRandomString( 32 ); // 128 bits
88 /**
89 * @see QuorumLockManager::getLocksOnServer()
90 * @return Status
92 protected function getLocksOnServer( $lockSrv, array $paths, $type ) {
93 $status = Status::newGood();
95 // Send out the command and get the response...
96 $type = ( $type == self::LOCK_SH ) ? 'SH' : 'EX';
97 $keys = array_unique( array_map( array( $this, 'sha1Base36Absolute' ), $paths ) );
98 $response = $this->sendCommand( $lockSrv, 'ACQUIRE', $type, $keys );
100 if ( $response !== 'ACQUIRED' ) {
101 foreach ( $paths as $path ) {
102 $status->fatal( 'lockmanager-fail-acquirelock', $path );
106 return $status;
110 * @see QuorumLockManager::freeLocksOnServer()
111 * @return Status
113 protected function freeLocksOnServer( $lockSrv, array $paths, $type ) {
114 $status = Status::newGood();
116 // Send out the command and get the response...
117 $type = ( $type == self::LOCK_SH ) ? 'SH' : 'EX';
118 $keys = array_unique( array_map( array( $this, 'sha1Base36Absolute' ), $paths ) );
119 $response = $this->sendCommand( $lockSrv, 'RELEASE', $type, $keys );
121 if ( $response !== 'RELEASED' ) {
122 foreach ( $paths as $path ) {
123 $status->fatal( 'lockmanager-fail-releaselock', $path );
127 return $status;
131 * @see QuorumLockManager::releaseAllLocks()
132 * @return Status
134 protected function releaseAllLocks() {
135 $status = Status::newGood();
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 );
144 return $status;
148 * @see QuorumLockManager::isServerUp()
149 * @return bool
151 protected function isServerUp( $lockSrv ) {
152 return (bool)$this->getConnection( $lockSrv );
156 * Send a command and get back the response
158 * @param $lockSrv string
159 * @param $action string
160 * @param $type string
161 * @param $values Array
162 * @return string|bool
164 protected function sendCommand( $lockSrv, $action, $type, $values ) {
165 $conn = $this->getConnection( $lockSrv );
166 if ( !$conn ) {
167 return false; // no connection
169 $authKey = $this->lockServers[$lockSrv]['authKey'];
170 // Build of the command as a flat string...
171 $values = implode( '|', $values );
172 $key = hash_hmac( 'sha1', "{$this->session}\n{$action}\n{$type}\n{$values}", $authKey );
173 // Send out the command...
174 if ( fwrite( $conn, "{$this->session}:$key:$action:$type:$values\n" ) === false ) {
175 return false;
177 // Get the response...
178 $response = fgets( $conn );
179 if ( $response === false ) {
180 return false;
182 return trim( $response );
186 * Get (or reuse) a connection to a lock server
188 * @param $lockSrv string
189 * @return resource
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;
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;
206 return $this->conns[$lockSrv];
210 * Make sure remaining locks get cleared for sanity
212 function __destruct() {
213 $this->releaseAllLocks();
214 foreach ( $this->conns as $conn ) {
215 fclose( $conn );