Enhancements and refactorings for the offline locking manager.
Ticket: 225,226
This commit is contained in:
parent
308541322b
commit
799102b280
@ -25,11 +25,11 @@
|
|||||||
* page requests can't be interfered by other users.
|
* page requests can't be interfered by other users.
|
||||||
*
|
*
|
||||||
* @author Roman Borschel <roman@code-factory.org>
|
* @author Roman Borschel <roman@code-factory.org>
|
||||||
|
* @author Pierre Minnieur <pm@pierre-minnieur.de>
|
||||||
* @license LGPL
|
* @license LGPL
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
class Doctrine_Locking_Manager_Pessimistic
|
class Doctrine_Locking_Manager_Pessimistic {
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* The datasource that is used by the locking manager
|
* The datasource that is used by the locking manager
|
||||||
*
|
*
|
||||||
@ -41,7 +41,6 @@ class Doctrine_Locking_Manager_Pessimistic
|
|||||||
*/
|
*/
|
||||||
private $_lockTable = 'doctrine_lock_tracking';
|
private $_lockTable = 'doctrine_lock_tracking';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new locking manager object
|
* Constructs a new locking manager object
|
||||||
*
|
*
|
||||||
@ -50,12 +49,10 @@ class Doctrine_Locking_Manager_Pessimistic
|
|||||||
*
|
*
|
||||||
* @param Doctrine_Connection $dataSource The database connection to use
|
* @param Doctrine_Connection $dataSource The database connection to use
|
||||||
*/
|
*/
|
||||||
public function __construct(Doctrine_Connection $dataSource)
|
public function __construct(Doctrine_Connection $dataSource) {
|
||||||
{
|
|
||||||
$this->_dataSource = $dataSource;
|
$this->_dataSource = $dataSource;
|
||||||
|
|
||||||
if($this->_dataSource->getAttribute(Doctrine::ATTR_CREATE_TABLES) === true)
|
if ($this->_dataSource->getAttribute(Doctrine::ATTR_CREATE_TABLES) === true) {
|
||||||
{
|
|
||||||
$columns = array();
|
$columns = array();
|
||||||
$columns['object_type'] = array('string', 50, array('notnull' => true, 'primary' => true));
|
$columns['object_type'] = array('string', 50, array('notnull' => true, 'primary' => true));
|
||||||
$columns['object_key'] = array('string', 250, array('notnull' => true, 'primary' => true));
|
$columns['object_key'] = array('string', 250, array('notnull' => true, 'primary' => true));
|
||||||
@ -64,8 +61,7 @@ class Doctrine_Locking_Manager_Pessimistic
|
|||||||
|
|
||||||
$dataDict = new Doctrine_DataDict($this->_dataSource->getDBH());
|
$dataDict = new Doctrine_DataDict($this->_dataSource->getDBH());
|
||||||
$dataDict->createTable($this->_lockTable, $columns);
|
$dataDict->createTable($this->_lockTable, $columns);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -77,21 +73,18 @@ class Doctrine_Locking_Manager_Pessimistic
|
|||||||
* holds a lock on this record
|
* holds a lock on this record
|
||||||
* @throws Doctrine_Locking_Exception If the locking failed due to database errors
|
* @throws Doctrine_Locking_Exception If the locking failed due to database errors
|
||||||
*/
|
*/
|
||||||
public function getLock(Doctrine_Record $record, $userIdent)
|
public function getLock(Doctrine_Record $record, $userIdent) {
|
||||||
{
|
|
||||||
$objectType = $record->getTable()->getComponentName();
|
$objectType = $record->getTable()->getComponentName();
|
||||||
$key = $record->obtainIdentifier();
|
$key = $record->obtainIdentifier();
|
||||||
|
|
||||||
$gotLock = false;
|
$gotLock = false;
|
||||||
|
|
||||||
if(is_array($key))
|
if (is_array($key)) {
|
||||||
{
|
|
||||||
// Composite key
|
// Composite key
|
||||||
$key = implode('|', $key);
|
$key = implode('|', $key);
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
$dbh = $this->_dataSource->getDBH();
|
$dbh = $this->_dataSource->getDBH();
|
||||||
$dbh->beginTransaction();
|
$dbh->beginTransaction();
|
||||||
|
|
||||||
@ -112,11 +105,9 @@ class Doctrine_Locking_Manager_Pessimistic
|
|||||||
// PK violation occured => existing lock!
|
// PK violation occured => existing lock!
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!$gotLock)
|
if (!$gotLock) {
|
||||||
{
|
|
||||||
$lockingUserIdent = $this->_getLockingUserIdent($objectType, $key);
|
$lockingUserIdent = $this->_getLockingUserIdent($objectType, $key);
|
||||||
if($lockingUserIdent !== null && $lockingUserIdent == $userIdent)
|
if ($lockingUserIdent !== null && $lockingUserIdent == $userIdent) {
|
||||||
{
|
|
||||||
$gotLock = true; // The requesting user already has a lock
|
$gotLock = true; // The requesting user already has a lock
|
||||||
// Update timestamp
|
// Update timestamp
|
||||||
$stmt = $dbh->prepare("UPDATE $this->_lockTable SET timestamp_obtained = :ts
|
$stmt = $dbh->prepare("UPDATE $this->_lockTable SET timestamp_obtained = :ts
|
||||||
@ -130,12 +121,8 @@ class Doctrine_Locking_Manager_Pessimistic
|
|||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$dbh->commit();
|
||||||
$dbh->commit();
|
} catch (Exception $pdoe) {
|
||||||
|
|
||||||
}
|
|
||||||
catch(Exception $pdoe)
|
|
||||||
{
|
|
||||||
$dbh->rollback();
|
$dbh->rollback();
|
||||||
throw new Doctrine_Locking_Exception($pdoe->getMessage());
|
throw new Doctrine_Locking_Exception($pdoe->getMessage());
|
||||||
}
|
}
|
||||||
@ -151,19 +138,16 @@ class Doctrine_Locking_Manager_Pessimistic
|
|||||||
* @return boolean TRUE if a lock was released, FALSE if no lock was released
|
* @return boolean TRUE if a lock was released, FALSE if no lock was released
|
||||||
* @throws Doctrine_Locking_Exception If the release procedure failed due to database errors
|
* @throws Doctrine_Locking_Exception If the release procedure failed due to database errors
|
||||||
*/
|
*/
|
||||||
public function releaseLock(Doctrine_Record $record, $userIdent)
|
public function releaseLock(Doctrine_Record $record, $userIdent) {
|
||||||
{
|
|
||||||
$objectType = $record->getTable()->getComponentName();
|
$objectType = $record->getTable()->getComponentName();
|
||||||
$key = $record->obtainIdentifier();
|
$key = $record->obtainIdentifier();
|
||||||
|
|
||||||
if(is_array($key))
|
if (is_array($key)) {
|
||||||
{
|
|
||||||
// Composite key
|
// Composite key
|
||||||
$key = implode('|', $key);
|
$key = implode('|', $key);
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
$dbh = $this->_dataSource->getDBH();
|
$dbh = $this->_dataSource->getDBH();
|
||||||
$stmt = $dbh->prepare("DELETE FROM $this->_lockTable WHERE
|
$stmt = $dbh->prepare("DELETE FROM $this->_lockTable WHERE
|
||||||
object_type = :object_type AND
|
object_type = :object_type AND
|
||||||
@ -176,11 +160,8 @@ class Doctrine_Locking_Manager_Pessimistic
|
|||||||
|
|
||||||
$count = $stmt->rowCount();
|
$count = $stmt->rowCount();
|
||||||
|
|
||||||
return ($count > 0);
|
return ($count > 0);
|
||||||
|
} catch (PDOException $pdoe) {
|
||||||
}
|
|
||||||
catch(PDOException $pdoe)
|
|
||||||
{
|
|
||||||
throw new Doctrine_Locking_Exception($pdoe->getMessage());
|
throw new Doctrine_Locking_Exception($pdoe->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -193,16 +174,13 @@ class Doctrine_Locking_Manager_Pessimistic
|
|||||||
* @return mixed The unique user identifier for the specified lock
|
* @return mixed The unique user identifier for the specified lock
|
||||||
* @throws Doctrine_Locking_Exception If the query failed due to database errors
|
* @throws Doctrine_Locking_Exception If the query failed due to database errors
|
||||||
*/
|
*/
|
||||||
private function _getLockingUserIdent($objectType, $key)
|
private function _getLockingUserIdent($objectType, $key) {
|
||||||
{
|
if (is_array($key)) {
|
||||||
if(is_array($key))
|
|
||||||
{
|
|
||||||
// Composite key
|
// Composite key
|
||||||
$key = implode('|', $key);
|
$key = implode('|', $key);
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
$dbh = $this->_dataSource->getDBH();
|
$dbh = $this->_dataSource->getDBH();
|
||||||
$stmt = $dbh->prepare("SELECT user_ident
|
$stmt = $dbh->prepare("SELECT user_ident
|
||||||
FROM $this->_lockTable
|
FROM $this->_lockTable
|
||||||
@ -211,47 +189,70 @@ class Doctrine_Locking_Manager_Pessimistic
|
|||||||
$stmt->bindParam(':object_key', $key);
|
$stmt->bindParam(':object_key', $key);
|
||||||
$success = $stmt->execute();
|
$success = $stmt->execute();
|
||||||
|
|
||||||
if(!$success)
|
if (!$success) {
|
||||||
{
|
|
||||||
throw new Doctrine_Locking_Exception("Failed to determine locking user");
|
throw new Doctrine_Locking_Exception("Failed to determine locking user");
|
||||||
}
|
}
|
||||||
|
|
||||||
$user_ident = $stmt->fetchColumn();
|
$userIdent = $stmt->fetchColumn();
|
||||||
}
|
} catch (PDOException $pdoe) {
|
||||||
catch(PDOException $pdoe)
|
|
||||||
{
|
|
||||||
throw new Doctrine_Locking_Exception($pdoe->getMessage());
|
throw new Doctrine_Locking_Exception($pdoe->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
return $user_ident;
|
return $userIdent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the identifier that identifies the owner of the lock on the given
|
||||||
|
* record.
|
||||||
|
*
|
||||||
|
* @param Doctrine_Record $lockedRecord The record.
|
||||||
|
* @return mixed The unique user identifier that identifies the owner of the lock.
|
||||||
|
*/
|
||||||
|
public function getLockOwner($lockedRecord) {
|
||||||
|
$objectType = $lockedRecord->getTable()->getComponentName();
|
||||||
|
$key = $lockedRecord->obtainIdentifier();
|
||||||
|
return $this->_getLockingUserIdent($objectType, $key);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Releases locks older than a defined amount of seconds
|
* Releases locks older than a defined amount of seconds
|
||||||
*
|
*
|
||||||
* When called without parameters all locks older than 15 minutes are released.
|
* When called without parameters all locks older than 15 minutes are released.
|
||||||
*
|
*
|
||||||
* @param integer $age The maximum valid age of locks in seconds
|
* @param integer $age The maximum valid age of locks in seconds
|
||||||
|
* @param string $objectType The type of the object (component name)
|
||||||
|
* @param mixed $userIdent The unique identifier of the locking user
|
||||||
* @return integer The number of locks that have been released
|
* @return integer The number of locks that have been released
|
||||||
* @throws Doctrine_Locking_Exception If the release process failed due to database errors
|
* @throws Doctrine_Locking_Exception If the release process failed due to database errors
|
||||||
*/
|
*/
|
||||||
public function releaseAgedLocks($age = 900)
|
public function releaseAgedLocks($age = 900, $objectType = null, $userIdent = null) {
|
||||||
{
|
|
||||||
$age = time() - $age;
|
$age = time() - $age;
|
||||||
|
|
||||||
try
|
try {
|
||||||
{
|
$dbh = $this->_dataSource->getDBH();
|
||||||
$dbh = $this->_dataSource->getDBH();
|
$stmt = $dbh->prepare("DELETE FROM $this->_lockTable WHERE timestamp_obtained < :age");
|
||||||
$stmt = $dbh->prepare("DELETE FROM $this->_lockTable WHERE timestamp_obtained < :age");
|
$stmt->bindParam(':age', $age);
|
||||||
$stmt->bindParam(':age', $age);
|
$query = "DELETE FROM $this->_lockTable WHERE timestamp_obtained < :age";
|
||||||
|
if ($objectType) {
|
||||||
|
$query .= " AND object_type = :object_type";
|
||||||
|
}
|
||||||
|
if ($userIdent) {
|
||||||
|
$query .= " AND user_ident = :user_ident";
|
||||||
|
}
|
||||||
|
$stmt = $dbh->prepare($query);
|
||||||
|
$stmt->bindParam(':age', $age);
|
||||||
|
if ($objectType) {
|
||||||
|
$stmt->bindParam(':object_type', $objectType);
|
||||||
|
}
|
||||||
|
if ($userIdent) {
|
||||||
|
$stmt->bindParam(':user_ident', $userIdent);
|
||||||
|
}
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
|
|
||||||
$count = $stmt->rowCount();
|
$count = $stmt->rowCount();
|
||||||
|
|
||||||
return $count;
|
return $count;
|
||||||
}
|
} catch (PDOException $pdoe) {
|
||||||
catch(PDOException $pdoe)
|
|
||||||
{
|
|
||||||
throw new Doctrine_Locking_Exception($pdoe->getMessage());
|
throw new Doctrine_Locking_Exception($pdoe->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,67 +1,86 @@
|
|||||||
<?PHP
|
<?PHP
|
||||||
|
|
||||||
require_once("UnitTestCase.php");
|
require_once("UnitTestCase.php");
|
||||||
|
|
||||||
|
|
||||||
class Doctrine_PessimisticLockingTestCase extends Doctrine_UnitTestCase
|
class Doctrine_PessimisticLockingTestCase extends Doctrine_UnitTestCase {
|
||||||
{
|
private $lockingManager;
|
||||||
private $lockingManager;
|
|
||||||
|
/**
|
||||||
/**
|
* Sets up everything for the lock testing
|
||||||
* Sets up everything for the lock testing
|
*
|
||||||
*
|
* Creates a locking manager and a test record to work with.
|
||||||
* Creates a locking manager and a test record to work with.
|
*/
|
||||||
*/
|
public function setUp() {
|
||||||
public function setUp()
|
parent::setUp();
|
||||||
{
|
$this->lockingManager = new Doctrine_Locking_Manager_Pessimistic($this->connection);
|
||||||
parent::setUp();
|
|
||||||
$this->lockingManager = new Doctrine_Locking_Manager_Pessimistic($this->connection);
|
// Create sample data to test on
|
||||||
|
$entry1 = new Forum_Entry();
|
||||||
// Create sample data to test on
|
$entry1->author = 'Bart Simpson';
|
||||||
$entry1 = new Forum_Entry();
|
$entry1->topic = 'I love donuts!';
|
||||||
$entry1->author = 'Bart Simpson';
|
$entry1->save();
|
||||||
$entry1->topic = 'I love donuts!';
|
}
|
||||||
$entry1->save();
|
|
||||||
}
|
/**
|
||||||
|
* Tests the basic locking mechanism
|
||||||
/**
|
*
|
||||||
* Tests the basic locking mechanism
|
* Currently tested: successful lock, failed lock, release lock
|
||||||
*
|
*/
|
||||||
* Currently tested: successful lock, failed lock, release lock
|
public function testLock() {
|
||||||
*/
|
$entries = $this->connection->query("FROM Forum_Entry WHERE Forum_Entry.author = 'Bart Simpson'");
|
||||||
public function testLock()
|
|
||||||
{
|
// Test successful lock
|
||||||
$entries = $this->connection->query("FROM Forum_Entry WHERE Forum_Entry.author = 'Bart Simpson'");
|
$gotLock = $this->lockingManager->getLock($entries[0], 'romanb');
|
||||||
|
$this->assertTrue($gotLock);
|
||||||
// Test successful lock
|
|
||||||
$gotLock = $this->lockingManager->getLock($entries[0], 'romanb');
|
// Test failed lock (another user already got a lock on the entry)
|
||||||
$this->assertTrue($gotLock);
|
$gotLock = $this->lockingManager->getLock($entries[0], 'konstav');
|
||||||
|
$this->assertFalse($gotLock);
|
||||||
// Test failed lock (another user already got a lock on the entry)
|
|
||||||
$gotLock = $this->lockingManager->getLock($entries[0], 'konstav');
|
// Test release lock
|
||||||
$this->assertFalse($gotLock);
|
$released = $this->lockingManager->releaseLock($entries[0], 'romanb');
|
||||||
|
$this->assertTrue($released);
|
||||||
// Test release lock
|
}
|
||||||
$released = $this->lockingManager->releaseLock($entries[0], 'romanb');
|
|
||||||
$this->assertTrue($released);
|
/**
|
||||||
}
|
* Tests the release mechanism of aged locks
|
||||||
|
* This test implicitly tests getLock().
|
||||||
/**
|
*/
|
||||||
* Tests the release mechanism of aged locks
|
public function testReleaseAgedLocks() {
|
||||||
*/
|
$entries = $this->connection->query("FROM Forum_Entry WHERE Forum_Entry.author = 'Bart Simpson'");
|
||||||
public function testReleaseAgedLocks()
|
$this->lockingManager->getLock($entries[0], 'romanb');
|
||||||
{
|
$released = $this->lockingManager->releaseAgedLocks(-1); // age -1 seconds => release all
|
||||||
$entries = $this->connection->query("FROM Forum_Entry WHERE Forum_Entry.author = 'Bart Simpson'");
|
$this->assertEqual(1, $released);
|
||||||
$this->lockingManager->getLock($entries[0], 'romanb');
|
|
||||||
$released = $this->lockingManager->releaseAgedLocks(-1); // age -1 seconds => release all
|
// A second call should return false (no locks left)
|
||||||
$this->assertTrue($released);
|
$released = $this->lockingManager->releaseAgedLocks(-1);
|
||||||
|
$this->assertEqual(0, $released);
|
||||||
// A second call should return false (no locks left)
|
|
||||||
$released = $this->lockingManager->releaseAgedLocks(-1);
|
// Test with further parameters
|
||||||
$this->assertFalse($released);
|
$this->lockingManager->getLock($entries[0], 'romanb');
|
||||||
}
|
$released = $this->lockingManager->releaseAgedLocks(-1, 'User'); // shouldnt release anything
|
||||||
}
|
$this->assertEqual(0, $released);
|
||||||
|
$released = $this->lockingManager->releaseAgedLocks(-1, 'Forum_Entry'); // should release the lock
|
||||||
|
$this->assertEqual(1, $released);
|
||||||
|
|
||||||
?>
|
$this->lockingManager->getLock($entries[0], 'romanb');
|
||||||
|
$released = $this->lockingManager->releaseAgedLocks(-1, 'Forum_Entry', 'zyne'); // shouldnt release anything
|
||||||
|
$this->assertEqual(0, $released);
|
||||||
|
$released = $this->lockingManager->releaseAgedLocks(-1, 'Forum_Entry', 'romanb'); // should release the lock
|
||||||
|
$this->assertEqual(1, $released);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the retrieving of a lock's owner.
|
||||||
|
* This test implicitly tests getLock().
|
||||||
|
*
|
||||||
|
* @param Doctrine_Record $lockedRecord
|
||||||
|
*/
|
||||||
|
public function testGetLockOwner() {
|
||||||
|
$entries = $this->connection->query("FROM Forum_Entry WHERE Forum_Entry.author = 'Bart Simpson'");
|
||||||
|
$gotLock = $this->lockingManager->getLock($entries[0], 'romanb');
|
||||||
|
$this->assertEqual('romanb', $this->lockingManager->getLockOwner($entries[0]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user