[LockManager] Added a RedisLockManager class.
authorAaron Schulz <aschulz@wikimedia.org>
Tue, 19 Feb 2013 02:45:23 +0000 (18:45 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Fri, 19 Apr 2013 03:18:54 +0000 (20:18 -0700)
Change-Id: I7ade74eb307a5075533f36836768af60f106a6b9

includes/AutoLoader.php
includes/filebackend/lockmanager/LockManager.php
includes/filebackend/lockmanager/RedisLockManager.php [new file with mode: 0644]

index 88bb21b..4a8ecaa 100644 (file)
@@ -573,8 +573,9 @@ $wgAutoloadLocalClasses = array(
        'LSLockManager' => 'includes/filebackend/lockmanager/LSLockManager.php',
        'MemcLockManager' => 'includes/filebackend/lockmanager/MemcLockManager.php',
        'QuorumLockManager' => 'includes/filebackend/lockmanager/QuorumLockManager.php',
-       'MySqlLockManager' => 'includes/filebackend/lockmanager/DBLockManager.php',
-       'PostgreSqlLockManager' => 'includes/filebackend/lockmanager/DBLockManager.php',
+       'MySqlLockManager'=> 'includes/filebackend/lockmanager/DBLockManager.php',
+       'PostgreSqlLockManager'=> 'includes/filebackend/lockmanager/DBLockManager.php',
+       'RedisLockManager'=> 'includes/filebackend/lockmanager/RedisLockManager.php',
        'NullLockManager' => 'includes/filebackend/lockmanager/LockManager.php',
        'FileOp' => 'includes/filebackend/FileOp.php',
        'FileOpBatch' => 'includes/filebackend/FileOpBatch.php',
index 9b7a7ff..9c778c7 100644 (file)
@@ -151,7 +151,7 @@ abstract class LockManager {
         *
         * @param array $paths List of storage paths
         * @param $type integer LockManager::LOCK_* constant
-        * @return string
+        * @return Status
         */
        abstract protected function doLock( array $paths, $type );
 
@@ -160,7 +160,7 @@ abstract class LockManager {
         *
         * @param array $paths List of storage paths
         * @param $type integer LockManager::LOCK_* constant
-        * @return string
+        * @return Status
         */
        abstract protected function doUnlock( array $paths, $type );
 }
diff --git a/includes/filebackend/lockmanager/RedisLockManager.php b/includes/filebackend/lockmanager/RedisLockManager.php
new file mode 100644 (file)
index 0000000..e8e5996
--- /dev/null
@@ -0,0 +1,255 @@
+<?php
+/**
+ * Version of LockManager based on using redis servers.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup LockManager
+ */
+
+/**
+ * Manage locks using redis servers.
+ *
+ * Version of LockManager based on using redis servers.
+ * This is meant for multi-wiki systems that may share files.
+ * All locks are non-blocking, which avoids deadlocks.
+ *
+ * All lock requests for a resource, identified by a hash string, will map to one
+ * bucket. Each bucket maps to one or several peer servers, each running redis.
+ * A majority of peers must agree for a lock to be acquired.
+ *
+ * This class requires Redis 2.6 as it makes use Lua scripts for fast atomic operations.
+ *
+ * @ingroup LockManager
+ * @since 1.22
+ */
+class RedisLockManager extends QuorumLockManager {
+       /** @var Array Mapping of lock types to the type actually used */
+       protected $lockTypeMap = array(
+               self::LOCK_SH => self::LOCK_SH,
+               self::LOCK_UW => self::LOCK_SH,
+               self::LOCK_EX => self::LOCK_EX
+       );
+
+       /** @var RedisConnectionPool */
+       protected $redisPool;
+       /** @var Array Map server names to hostname/IP and port numbers */
+       protected $lockServers = array();
+
+       protected $session = ''; // string; random UUID
+
+       /**
+        * Construct a new instance from configuration.
+        *
+        * $config paramaters include:
+        *   - lockServers  : Associative array of server names to "<IP>:<port>" strings.
+        *   - srvsByBucket : Array of 1-16 consecutive integer keys, starting from 0,
+        *                    each having an odd-numbered list of server names (peers) as values.
+        *   - redisConfig  : Configuration for RedisConnectionPool::__construct().
+        *
+        * @param Array $config
+        * @throws MWException
+        */
+       public function __construct( array $config ) {
+               parent::__construct( $config );
+
+               $this->lockServers = $config['lockServers'];
+               // Sanitize srvsByBucket config to prevent PHP errors
+               $this->srvsByBucket = array_filter( $config['srvsByBucket'], 'is_array' );
+               $this->srvsByBucket = array_values( $this->srvsByBucket ); // consecutive
+
+               $config['redisConfig']['serializer'] = 'none';
+               $this->redisPool = RedisConnectionPool::singleton( $config['redisConfig'] );
+
+               $this->session = wfRandomString( 32 );
+       }
+
+       protected function getLocksOnServer( $lockSrv, array $paths, $type ) {
+               $status = Status::newGood();
+
+               $server = $this->lockServers[$lockSrv];
+               $conn = $this->redisPool->getConnection( $server );
+               if ( !$conn ) {
+                       foreach ( $paths as $path ) {
+                               $status->fatal( 'lockmanager-fail-acquirelock', $path );
+                       }
+                       return $status;
+               }
+
+               $keys = array_map( array( $this, 'recordKeyForPath' ), $paths ); // lock records
+
+               try {
+                       static $script =
+<<<LUA
+                       if ARGV[1] ~= 'EX' and ARGV[1] ~= 'SH' then
+                               return redis.error_reply('Unrecognized lock type given (must be EX or SH)')
+                       end
+                       local failed = {}
+                       -- Check that all the locks can be acquired
+                       for i,resourceKey in ipairs(KEYS) do
+                               local keyIsFree = true
+                               local currentLocks = redis.call('hKeys',resourceKey)
+                               for i,lockKey in ipairs(currentLocks) do
+                                       local _, _, type, session = string.find(lockKey,"(%w+):(%w+)")
+                                       -- Check any locks that are not owned by this session
+                                       if session ~= ARGV[2] then
+                                               local lockTimestamp = redis.call('hGet',resourceKey,lockKey)
+                                               if 1*lockTimestamp < ( ARGV[4] - ARGV[3] ) then
+                                                       -- Lock is stale, so just prune it out
+                                                       redis.call('hDel',resourceKey,lockKey)
+                                               elseif ARGV[1] == 'EX' or type == 'EX' then
+                                                       keyIsFree = false
+                                                       break
+                                               end
+                                       end
+                               end
+                               if not keyIsFree then
+                                       failed[#failed+1] = resourceKey
+                               end
+                       end
+                       -- If all locks could be acquired, then do so
+                       if #failed == 0 then
+                               for i,resourceKey in ipairs(KEYS) do
+                                       redis.call('hSet',resourceKey,ARGV[1] .. ':' .. ARGV[2],ARGV[4])
+                                       -- In addition to invalidation logic, be sure to garbage collect
+                                       redis.call('expire',resourceKey,ARGV[3])
+                               end
+                       end
+                       return failed
+LUA;
+                       $res = $conn->luaEval( $script,
+                               array_merge(
+                                       $keys, // KEYS[0], KEYS[1],...KEYS[N]
+                                       array(
+                                               $type === self::LOCK_SH ? 'SH' : 'EX', // ARGV[1]
+                                               $this->session, // ARGV[2]
+                                               $this->lockTTL, // ARGV[3]
+                                               time() // ARGV[4]
+                                       )
+                               ),
+                               count( $keys ) # number of first argument(s) that are keys
+                       );
+               } catch ( RedisException $e ) {
+                       $res = false;
+                       $this->redisPool->handleException( $server, $conn, $e );
+               }
+
+               if ( $res === false ) {
+                       foreach ( $paths as $path ) {
+                               $status->fatal( 'lockmanager-fail-acquirelock', $path );
+                       }
+               } else {
+                       $pathsByKey = array_combine( $keys, $paths );
+                       foreach ( $res as $key ) {
+                               $status->fatal( 'lockmanager-fail-acquirelock', $pathsByKey[$key] );
+                       }
+               }
+
+               return $status;
+       }
+
+       protected function freeLocksOnServer( $lockSrv, array $paths, $type ) {
+               $status = Status::newGood();
+
+               $server = $this->lockServers[$lockSrv];
+               $conn = $this->redisPool->getConnection( $server );
+               if ( !$conn ) {
+                       foreach ( $paths as $path ) {
+                               $status->fatal( 'lockmanager-fail-releaselock', $path );
+                       }
+                       return $status;
+               }
+
+               $keys = array_map( array( $this, 'recordKeyForPath' ), $paths ); // lock records
+
+               try {
+                       static $script =
+<<<LUA
+                       if ARGV[1] ~= 'EX' and ARGV[1] ~= 'SH' then
+                               return redis.error_reply('Unrecognized lock type given (must be EX or SH)')
+                       end
+                       local failed = {}
+                       for i,resourceKey in ipairs(KEYS) do
+                               local released = redis.call('hDel',resourceKey,ARGV[1] .. ':' .. ARGV[2])
+                               if released > 0 then
+                                       -- Remove the whole structure if it is now empty
+                                       if redis.call('hLen',resourceKey) == 0 then
+                                               redis.call('del',resourceKey)
+                                       end
+                               else
+                                       failed[#failed+1] = resourceKey
+                               end
+                       end
+                       return failed
+LUA;
+                       $res = $conn->luaEval( $script,
+                               array_merge(
+                                       $keys, // KEYS[0], KEYS[1],...KEYS[N]
+                                       array(
+                                               $type === self::LOCK_SH ? 'SH' : 'EX', // ARGV[1]
+                                               $this->session // ARGV[2]
+                                       )
+                               ),
+                               count( $keys ) # number of first argument(s) that are keys
+                       );
+               } catch ( RedisException $e ) {
+                       $res = false;
+                       $this->redisPool->handleException( $server, $conn, $e );
+               }
+
+               if ( $res === false ) {
+                       foreach ( $paths as $path ) {
+                               $status->fatal( 'lockmanager-fail-releaselock', $path );
+                       }
+               } else {
+                       $pathsByKey = array_combine( $keys, $paths );
+                       foreach ( $res as $key ) {
+                               $status->fatal( 'lockmanager-fail-releaselock', $pathsByKey[$key] );
+                       }
+               }
+
+               return $status;
+       }
+
+       protected function releaseAllLocks() {
+               return Status::newGood(); // not supported
+       }
+
+       protected function isServerUp( $lockSrv ) {
+               return (bool)$this->redisPool->getConnection( $this->lockServers[$lockSrv] );
+       }
+
+       /**
+        * @param $path string
+        * @return string
+        */
+       protected function recordKeyForPath( $path ) {
+               return implode( ':', array( __CLASS__, 'locks', $this->sha1Base36Absolute( $path ) ) );
+       }
+
+       /**
+        * Make sure remaining locks get cleared for sanity
+        */
+       function __destruct() {
+               while ( count( $this->locksHeld ) ) {
+                       foreach ( $this->locksHeld as $path => $locks ) {
+                               $this->doUnlock( array( $path ), self::LOCK_EX );
+                               $this->doUnlock( array( $path ), self::LOCK_SH );
+                       }
+               }
+       }
+}