7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
16 * @package Zend_Search_Lucene
17 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
19 * @version $Id: LockManager.php 16971 2009-07-22 18:05:45Z mikaelkael $
22 /** Zend_Search_Lucene_Storage_Directory */
23 require_once 'Zend/Search/Lucene/Storage/Directory.php';
25 /** Zend_Search_Lucene_Storage_File */
26 require_once 'Zend/Search/Lucene/Storage/File.php';
29 * This is an utility class which provides index locks processing functionality
32 * @package Zend_Search_Lucene
33 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
34 * @license http://framework.zend.com/license/new-bsd New BSD License
36 class Zend_Search_Lucene_LockManager
39 * consts for name of file to show lock status
41 const WRITE_LOCK_FILE
= 'write.lock.file';
42 const READ_LOCK_FILE
= 'read.lock.file';
43 const READ_LOCK_PROCESSING_LOCK_FILE
= 'read-lock-processing.lock.file';
44 const OPTIMIZATION_LOCK_FILE
= 'optimization.lock.file';
47 * Obtain exclusive write lock on the index
49 * @param Zend_Search_Lucene_Storage_Directory $lockDirectory
50 * @return Zend_Search_Lucene_Storage_File
51 * @throws Zend_Search_Lucene_Exception
53 public static function obtainWriteLock(Zend_Search_Lucene_Storage_Directory
$lockDirectory)
55 $lock = $lockDirectory->createFile(self
::WRITE_LOCK_FILE
);
56 if (!$lock->lock(LOCK_EX
)) {
57 require_once 'Zend/Search/Lucene/Exception.php';
58 throw new Zend_Search_Lucene_Exception('Can\'t obtain exclusive index lock');
64 * Release exclusive write lock
66 * @param Zend_Search_Lucene_Storage_Directory $lockDirectory
68 public static function releaseWriteLock(Zend_Search_Lucene_Storage_Directory
$lockDirectory)
70 $lock = $lockDirectory->getFileObject(self
::WRITE_LOCK_FILE
);
75 * Obtain the exclusive "read escalation/de-escalation" lock
77 * Required to protect the escalate/de-escalate read lock process
78 * on GFS (and potentially other) mounted filesystems.
81 * While GFS supports cluster-wide locking via flock(), it's
82 * implementation isn't quite what it should be. The locking
83 * semantics that work consistently on a local filesystem tend to
84 * fail on GFS mounted filesystems. This appears to be a design defect
85 * in the implementation of GFS. How this manifests itself is that
86 * conditional promotion of a shared lock to exclusive will always
87 * fail, lock release requests are honored but not immediately
88 * processed (causing erratic failures of subsequent conditional
89 * requests) and the releasing of the exclusive lock before the
90 * shared lock is set when a lock is demoted (which can open a window
91 * of opportunity for another process to gain an exclusive lock when
92 * it shoudln't be allowed to).
94 * @param Zend_Search_Lucene_Storage_Directory $lockDirectory
95 * @return Zend_Search_Lucene_Storage_File
96 * @throws Zend_Search_Lucene_Exception
98 private static function _startReadLockProcessing(Zend_Search_Lucene_Storage_Directory
$lockDirectory)
100 $lock = $lockDirectory->createFile(self
::READ_LOCK_PROCESSING_LOCK_FILE
);
101 if (!$lock->lock(LOCK_EX
)) {
102 require_once 'Zend/Search/Lucene/Exception.php';
103 throw new Zend_Search_Lucene_Exception('Can\'t obtain exclusive lock for the read lock processing file');
109 * Release the exclusive "read escalation/de-escalation" lock
111 * Required to protect the escalate/de-escalate read lock process
112 * on GFS (and potentially other) mounted filesystems.
114 * @param Zend_Search_Lucene_Storage_Directory $lockDirectory
116 private static function _stopReadLockProcessing(Zend_Search_Lucene_Storage_Directory
$lockDirectory)
118 $lock = $lockDirectory->getFileObject(self
::READ_LOCK_PROCESSING_LOCK_FILE
);
124 * Obtain shared read lock on the index
126 * It doesn't block other read or update processes, but prevent index from the premature cleaning-up
128 * @param Zend_Search_Lucene_Storage_Directory $defaultLockDirectory
129 * @return Zend_Search_Lucene_Storage_File
130 * @throws Zend_Search_Lucene_Exception
132 public static function obtainReadLock(Zend_Search_Lucene_Storage_Directory
$lockDirectory)
134 $lock = $lockDirectory->createFile(self
::READ_LOCK_FILE
);
135 if (!$lock->lock(LOCK_SH
)) {
136 require_once 'Zend/Search/Lucene/Exception.php';
137 throw new Zend_Search_Lucene_Exception('Can\'t obtain shared reading index lock');
143 * Release shared read lock
145 * @param Zend_Search_Lucene_Storage_Directory $lockDirectory
147 public static function releaseReadLock(Zend_Search_Lucene_Storage_Directory
$lockDirectory)
149 $lock = $lockDirectory->getFileObject(self
::READ_LOCK_FILE
);
154 * Escalate Read lock to exclusive level
156 * @param Zend_Search_Lucene_Storage_Directory $lockDirectory
159 public static function escalateReadLock(Zend_Search_Lucene_Storage_Directory
$lockDirectory)
161 self
::_startReadLockProcessing($lockDirectory);
163 $lock = $lockDirectory->getFileObject(self
::READ_LOCK_FILE
);
165 // First, release the shared lock for the benefit of GFS since
166 // it will fail the conditional request to promote the lock to
167 // "exclusive" while the shared lock is held (even when we are
171 // GFS is really poor. While the above "unlock" returns, GFS
172 // doesn't clean up it's tables right away (which will potentially
173 // cause the conditional locking for the "exclusive" lock to fail.
174 // We will retry the conditional lock request several times on a
175 // failure to get past this. The performance hit is negligible
176 // in the grand scheme of things and only will occur with GFS
177 // filesystems or if another local process has the shared lock
178 // on local filesystems.
179 for ($retries = 0; $retries < 10; $retries++
) {
180 if ($lock->lock(LOCK_EX
, true)) {
181 // Exclusive lock is obtained!
182 self
::_stopReadLockProcessing($lockDirectory);
186 // wait 1 microsecond
190 // Restore lock state
191 $lock->lock(LOCK_SH
);
193 self
::_stopReadLockProcessing($lockDirectory);
198 * De-escalate Read lock to shared level
200 * @param Zend_Search_Lucene_Storage_Directory $lockDirectory
202 public static function deEscalateReadLock(Zend_Search_Lucene_Storage_Directory
$lockDirectory)
204 $lock = $lockDirectory->getFileObject(self
::READ_LOCK_FILE
);
205 $lock->lock(LOCK_SH
);
209 * Obtain exclusive optimization lock on the index
211 * Returns lock object on success and false otherwise (doesn't block execution)
213 * @param Zend_Search_Lucene_Storage_Directory $lockDirectory
216 public static function obtainOptimizationLock(Zend_Search_Lucene_Storage_Directory
$lockDirectory)
218 $lock = $lockDirectory->createFile(self
::OPTIMIZATION_LOCK_FILE
);
219 if (!$lock->lock(LOCK_EX
, true)) {
226 * Release exclusive optimization lock
228 * @param Zend_Search_Lucene_Storage_Directory $lockDirectory
230 public static function releaseOptimizationLock(Zend_Search_Lucene_Storage_Directory
$lockDirectory)
232 $lock = $lockDirectory->getFileObject(self
::OPTIMIZATION_LOCK_FILE
);