From 95e463e1d96a2dc62f5aa59a74402f1af112010a Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Wed, 13 Mar 2013 00:41:22 -0700 Subject: [PATCH] [LockManager] Refactor lock TTL code to base class. * Also made the TTL configurable. Change-Id: I713979267fe7bd92ac6cd5fe4d4168d20e539c75 --- includes/filebackend/lockmanager/LockManager.php | 13 ++++++++++++- .../filebackend/lockmanager/MemcLockManager.php | 8 ++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/includes/filebackend/lockmanager/LockManager.php b/includes/filebackend/lockmanager/LockManager.php index 6d155df418..0512a01b1e 100644 --- a/includes/filebackend/lockmanager/LockManager.php +++ b/includes/filebackend/lockmanager/LockManager.php @@ -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 ); + } } /** diff --git a/includes/filebackend/lockmanager/MemcLockManager.php b/includes/filebackend/lockmanager/MemcLockManager.php index 8131f81782..fafc588a9e 100644 --- a/includes/filebackend/lockmanager/MemcLockManager.php +++ b/includes/filebackend/lockmanager/MemcLockManager.php @@ -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 ); } -- 2.20.1