[LockManager] Generallized wiki field to a domain field.
authorAaron Schulz <aschulz@wikimedia.org>
Fri, 1 Feb 2013 18:29:10 +0000 (10:29 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Fri, 1 Feb 2013 18:40:45 +0000 (10:40 -0800)
* In some cases (like certain qualified file backends), a domain
  may be desired that is not simple a wiki ID.
* Also added a LockManangerGroup::config() function to export
  the configuration of a lock manager.

Change-Id: I5a75c55be3cc9017ed8a717d6d30e466fd00340f

includes/filebackend/FileBackend.php
includes/filebackend/lockmanager/DBLockManager.php
includes/filebackend/lockmanager/LockManager.php
includes/filebackend/lockmanager/LockManagerGroup.php

index 8c0ce88..0e61d0c 100644 (file)
@@ -101,7 +101,7 @@ abstract class FileBackend {
                        : wfWikiID(); // e.g. "my_wiki-en_"
                $this->lockManager = ( $config['lockManager'] instanceof LockManager )
                        ? $config['lockManager']
-                       : LockManagerGroup::singleton()->get( $config['lockManager'] );
+                       : LockManagerGroup::singleton( $this->wikiId )->get( $config['lockManager'] );
                $this->fileJournal = isset( $config['fileJournal'] )
                        ? ( ( $config['fileJournal'] instanceof FileJournal )
                                ? $config['fileJournal']
index 8196d5b..7b365c1 100644 (file)
@@ -22,7 +22,8 @@
  */
 
 /**
- * Version of LockManager based on using DB table locks.
+ * Version of LockManager based on using DB table row locks.
+ *
  * This is meant for multi-wiki systems that may share files.
  * All locks are blocking, so it might be useful to set a small
  * lock-wait timeout via server config to curtail deadlocks.
@@ -67,6 +68,7 @@ class DBLockManager extends QuorumLockManager {
         *                   each having an odd-numbered list of DB names (peers) as values.
         *                   Any DB named 'localDBMaster' will automatically use the DB master
         *                   settings for this wiki (without the need for a dbServers entry).
+        *                   Only use 'localDBMaster' if the domain is a valid wiki ID.
         *   - lockExpiry  : Lock timeout (seconds) for dropped connections. [optional]
         *                   This tells the DB server how long to wait before assuming
         *                   connection failure and releasing all the locks for a session.
@@ -197,8 +199,8 @@ class DBLockManager extends QuorumLockManager {
                if ( !isset( $this->conns[$lockDb] ) ) {
                        $db = null;
                        if ( $lockDb === 'localDBMaster' ) {
-                               $lb = wfGetLBFactory()->getMainLB( $this->wiki );
-                               $db = $lb->getConnection( DB_MASTER, array(), $this->wiki );
+                               $lb = wfGetLBFactory()->getMainLB( $this->domain );
+                               $db = $lb->getConnection( DB_MASTER, array(), $this->domain );
                        } elseif ( isset( $this->dbServers[$lockDb] ) ) {
                                $config = $this->dbServers[$lockDb];
                                $db = DatabaseBase::factory( $config['type'], $config );
index 119006a..f988ff4 100644 (file)
@@ -53,7 +53,7 @@ abstract class LockManager {
        /** @var Array Map of (resource path => lock type => count) */
        protected $locksHeld = array();
 
-       protected $wiki; // string; wiki ID
+       protected $domain; // string; domain (usually wiki ID)
 
        /* Lock types; stronger locks have higher values */
        const LOCK_SH = 1; // shared lock (for reads)
@@ -64,12 +64,12 @@ abstract class LockManager {
         * Construct a new instance from configuration
         *
         * $config paramaters include:
-        *   - wiki : Wiki ID string that all resources are relative to [optional]
+        *   - domain : Domain (usually wiki ID) that all resources are relative to [optional]
         *
         * @param $config Array
         */
        public function __construct( array $config ) {
-               $this->wiki = isset( $config['wiki'] ) ? $config['wiki'] : wfWikiID();
+               $this->domain = isset( $config['domain'] ) ? $config['domain'] : wfWikiID();
        }
 
        /**
@@ -102,14 +102,14 @@ abstract class LockManager {
 
        /**
         * Get the base 36 SHA-1 of a string, padded to 31 digits.
-        * Before hashing, the path will be prefixed with the wiki ID.
+        * Before hashing, the path will be prefixed with the domain ID.
         * This should be used interally for lock key or file names.
         *
         * @param $path string
         * @return string
         */
        final protected function sha1Base36Absolute( $path ) {
-               return wfBaseConvert( sha1( "{$this->wiki}:{$path}" ), 16, 36, 31 );
+               return wfBaseConvert( sha1( "{$this->domain}:{$path}" ), 16, 36, 31 );
        }
 
        /**
index 267d391..f0d5818 100644 (file)
  * @since 1.19
  */
 class LockManagerGroup {
-       /** @var Array (wiki => LockManager) */
+       /** @var Array (domain => LockManager) */
        protected static $instances = array();
 
-       protected $wiki; // string; wiki ID
+       protected $domain; // string; domain (usually wiki ID)
 
-       /** @var Array of (name => ('class' =>, 'config' =>, 'instance' =>)) */
+       /** @var Array of (name => ('class' => ..., 'config' => ..., 'instance' => ...)) */
        protected $managers = array();
 
        /**
-        * @param $wiki string Wiki ID
+        * @param $domain string Domain (usually wiki ID)
         */
-       protected function __construct( $wiki ) {
-               $this->wiki = $wiki;
+       protected function __construct( $domain ) {
+               $this->domain = $domain;
        }
 
        /**
-        * @param $wiki string Wiki ID
+        * @param $domain string Domain (usually wiki ID)
         * @return LockManagerGroup
         */
-       public static function singleton( $wiki = false ) {
-               $wiki = ( $wiki === false ) ? wfWikiID() : $wiki;
-               if ( !isset( self::$instances[$wiki] ) ) {
-                       self::$instances[$wiki] = new self( $wiki );
-                       self::$instances[$wiki]->initFromGlobals();
+       public static function singleton( $domain = false ) {
+               $domain = ( $domain === false ) ? wfWikiID() : $domain;
+               if ( !isset( self::$instances[$domain] ) ) {
+                       self::$instances[$domain] = new self( $domain );
+                       self::$instances[$domain]->initFromGlobals();
                }
-               return self::$instances[$wiki];
+               return self::$instances[$domain];
        }
 
        /**
@@ -86,7 +86,7 @@ class LockManagerGroup {
         */
        protected function register( array $configs ) {
                foreach ( $configs as $config ) {
-                       $config['wiki'] = $this->wiki;
+                       $config['domain'] = $this->domain;
                        if ( !isset( $config['name'] ) ) {
                                throw new MWException( "Cannot register a lock manager with no name." );
                        }
@@ -124,6 +124,21 @@ class LockManagerGroup {
                return $this->managers[$name]['instance'];
        }
 
+       /**
+        * Get the config array for a lock manager object with a given name
+        *
+        * @param $name string
+        * @return Array
+        * @throws MWException
+        */
+       public function config( $name ) {
+               if ( !isset( $this->managers[$name] ) ) {
+                       throw new MWException( "No lock manager defined with the name `$name`." );
+               }
+               $class = $this->managers[$name]['class'];
+               return array( 'class' => $class ) + $this->managers[$name]['config'];
+       }
+
        /**
         * Get the default lock manager configured for the site.
         * Returns NullLockManager if no lock manager could be found.