[LockManager] Refactor lock TTL code to base class.
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 13 Mar 2013 07:41:22 +0000 (00:41 -0700)
committerGerrit Code Review <gerrit@wikimedia.org>
Mon, 18 Mar 2013 05:25:35 +0000 (05:25 +0000)
* Also made the TTL configurable.

Change-Id: I713979267fe7bd92ac6cd5fe4d4168d20e539c75

includes/filebackend/lockmanager/LockManager.php
includes/filebackend/lockmanager/MemcLockManager.php

index 6d155df..0512a01 100644 (file)
@@ -54,6 +54,7 @@ abstract class LockManager {
        protected $locksHeld = array();
 
        protected $domain; // string; domain (usually wiki ID)
+       protected $lockTTL; // integer; maximum time locks can be held
 
        /* Lock types; stronger locks have higher values */
        const LOCK_SH = 1; // shared lock (for reads)
@@ -64,12 +65,22 @@ abstract class LockManager {
         * Construct a new instance from configuration
         *
         * $config paramaters include:
-        *   - domain : Domain (usually wiki ID) that all resources are relative to [optional]
+        *   - domain  : Domain (usually wiki ID) that all resources are relative to [optional]
+        *   - lockTTL : Age (in seconds) at which resource locks should expire.
+        *               This only applies if locks are not tied to a connection/process.
         *
         * @param $config Array
         */
        public function __construct( array $config ) {
                $this->domain = isset( $config['domain'] ) ? $config['domain'] : wfWikiID();
+               if ( isset( $config['lockTTL'] ) ) {
+                       $this->lockTTL = max( 1, $config['lockTTL'] );
+               } elseif ( PHP_SAPI === 'cli' ) {
+                       $this->lockTTL = 2*3600;
+               } else {
+                       $met = ini_get( 'max_execution_time' ); // this is 0 in CLI mode
+                       $this->lockTTL = max( 5*60, 2*(int)$met );
+               }
        }
 
        /**
index 8131f81..fafc588 100644 (file)
@@ -48,7 +48,6 @@ class MemcLockManager extends QuorumLockManager {
        /** @var Array */
        protected $serversUp = array(); // (server name => bool)
 
-       protected $lockExpiry; // integer; maximum time locks can be held
        protected $session = ''; // string; random UUID
 
        /**
@@ -86,9 +85,6 @@ class MemcLockManager extends QuorumLockManager {
                        }
                }
 
-               $met = ini_get( 'max_execution_time' ); // this is 0 in CLI mode
-               $this->lockExpiry = $met ? 2*(int)$met : 2*3600;
-
                $this->session = wfRandomString( 32 );
        }
 
@@ -138,7 +134,7 @@ class MemcLockManager extends QuorumLockManager {
                        }
                        if ( $status->isOK() ) {
                                // Register the session in the lock record array
-                               $locksHeld[$type][$this->session] = $now + $this->lockExpiry;
+                               $locksHeld[$type][$this->session] = $now + $this->lockTTL;
                                // We will update this record if none of the other locks conflict
                                $lockRecords[$locksKey] = $locksHeld;
                        }
@@ -242,7 +238,7 @@ class MemcLockManager extends QuorumLockManager {
                if ( isset( $this->bagOStuffs[$lockSrv] ) ) {
                        $memc = $this->bagOStuffs[$lockSrv];
                        if ( !isset( $this->serversUp[$lockSrv] ) ) {
-                               $this->serversUp[$lockSrv] = $memc->set( 'MemcLockManager:ping', 1, 1 );
+                               $this->serversUp[$lockSrv] = $memc->set( __CLASS__ . ':ping', 1, 1 );
                                if ( !$this->serversUp[$lockSrv] ) {
                                        trigger_error( __METHOD__ . ": Could not contact $lockSrv.", E_USER_WARNING );
                                }