From 5193c50d8a449206e3651d113a64333879275ac6 Mon Sep 17 00:00:00 2001 From: Anton Komarev Date: Fri, 19 Jul 2024 18:41:10 +0300 Subject: [PATCH] Add human readable value to SQL --- docker-compose.yml | 2 - src/LockId/PostgresLockId.php | 2 +- src/Locker/PostgresAdvisoryLocker.php | 14 ++-- .../Locker/PostgresAdvisoryLockerTest.php | 68 +++++++++---------- 4 files changed, 43 insertions(+), 43 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 30d1374..896664a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,3 @@ -version: "3.9" - services: app: container_name: php-db-locker-app diff --git a/src/LockId/PostgresLockId.php b/src/LockId/PostgresLockId.php index aac0ca4..9a0ab99 100644 --- a/src/LockId/PostgresLockId.php +++ b/src/LockId/PostgresLockId.php @@ -33,7 +33,7 @@ public function __construct( } public static function fromLockId( - LockId $lockId + LockId $lockId, ): self { $humanReadableValue = (string)$lockId; diff --git a/src/Locker/PostgresAdvisoryLocker.php b/src/Locker/PostgresAdvisoryLocker.php index 2de86d0..74f4790 100644 --- a/src/Locker/PostgresAdvisoryLocker.php +++ b/src/Locker/PostgresAdvisoryLocker.php @@ -19,13 +19,14 @@ final class PostgresAdvisoryLocker { - public function acquireLock( + public function tryAcquireLock( PDO $dbConnection, PostgresLockId $postgresLockId, ): bool { + // TODO: Need to cleanup humanReadableValue? $statement = $dbConnection->prepare( <<humanReadableValue SQL ); $statement->execute( @@ -37,7 +38,7 @@ public function acquireLock( return $statement->fetchColumn(0); } - public function acquireLockWithinTransaction( + public function tryAcquireLockWithinTransaction( PDO $dbConnection, PostgresLockId $postgresLockId, ): bool { @@ -49,9 +50,10 @@ public function acquireLockWithinTransaction( ); } + // TODO: Need to cleanup humanReadableValue? $statement = $dbConnection->prepare( <<humanReadableValue SQL ); $statement->execute( @@ -68,7 +70,7 @@ public function releaseLock( PostgresLockId $postgresLockId, ): bool { $statement = $dbConnection->prepare( - <<prepare( - <<initPostgresPdoConnection(); $postgresLockId = $this->initPostgresLockId('test'); - $isLockAcquired = $locker->acquireLock($dbConnection, $postgresLockId); + $isLockAcquired = $locker->tryAcquireLock($dbConnection, $postgresLockId); $this->assertTrue($isLockAcquired); $this->assertPgAdvisoryLockExistsInConnection($dbConnection, $postgresLockId); @@ -43,7 +43,7 @@ public function test_it_can_acquire_lock_with_smallest_lock_id(): void $dbConnection = $this->initPostgresPdoConnection(); $postgresLockId = new PostgresLockId(self::DB_INT64_VALUE_MIN); - $isLockAcquired = $locker->acquireLock($dbConnection, $postgresLockId); + $isLockAcquired = $locker->tryAcquireLock($dbConnection, $postgresLockId); $this->assertTrue($isLockAcquired); $this->assertPgAdvisoryLockExistsInConnection($dbConnection, $postgresLockId); @@ -56,7 +56,7 @@ public function test_it_can_acquire_lock_with_biggest_lock_id(): void $dbConnection = $this->initPostgresPdoConnection(); $postgresLockId = new PostgresLockId(self::DB_INT64_VALUE_MAX); - $isLockAcquired = $locker->acquireLock($dbConnection, $postgresLockId); + $isLockAcquired = $locker->tryAcquireLock($dbConnection, $postgresLockId); $this->assertTrue($isLockAcquired); $this->assertPgAdvisoryLockExistsInConnection($dbConnection, $postgresLockId); @@ -69,8 +69,8 @@ public function test_it_can_acquire_lock_in_same_connection_only_once(): void $dbConnection = $this->initPostgresPdoConnection(); $postgresLockId = $this->initPostgresLockId('test'); - $isLockAcquired1 = $locker->acquireLock($dbConnection, $postgresLockId); - $isLockAcquired2 = $locker->acquireLock($dbConnection, $postgresLockId); + $isLockAcquired1 = $locker->tryAcquireLock($dbConnection, $postgresLockId); + $isLockAcquired2 = $locker->tryAcquireLock($dbConnection, $postgresLockId); $this->assertTrue($isLockAcquired1); $this->assertTrue($isLockAcquired2); @@ -85,8 +85,8 @@ public function test_it_can_acquire_multiple_locks_in_one_connection(): void $postgresLockId1 = $this->initPostgresLockId('test1'); $postgresLockId2 = $this->initPostgresLockId('test2'); - $isLock1Acquired = $locker->acquireLock($dbConnection, $postgresLockId1); - $isLock2Acquired = $locker->acquireLock($dbConnection, $postgresLockId2); + $isLock1Acquired = $locker->tryAcquireLock($dbConnection, $postgresLockId1); + $isLock2Acquired = $locker->tryAcquireLock($dbConnection, $postgresLockId2); $this->assertTrue($isLock1Acquired); $this->assertPgAdvisoryLockExistsInConnection($dbConnection, $postgresLockId1); @@ -101,9 +101,9 @@ public function test_it_cannot_acquire_same_lock_in_two_connections(): void $dbConnection1 = $this->initPostgresPdoConnection(); $dbConnection2 = $this->initPostgresPdoConnection(); $postgresLockId = $this->initPostgresLockId('test'); - $locker->acquireLock($dbConnection1, $postgresLockId); + $locker->tryAcquireLock($dbConnection1, $postgresLockId); - $isLockAcquired = $locker->acquireLock($dbConnection2, $postgresLockId); + $isLockAcquired = $locker->tryAcquireLock($dbConnection2, $postgresLockId); $this->assertFalse($isLockAcquired); $this->assertPgAdvisoryLocksCount(1); @@ -115,7 +115,7 @@ public function test_it_can_release_lock(): void $locker = $this->initLocker(); $dbConnection = $this->initPostgresPdoConnection(); $postgresLockId = $this->initPostgresLockId('test'); - $locker->acquireLock($dbConnection, $postgresLockId); + $locker->tryAcquireLock($dbConnection, $postgresLockId); $isLockReleased = $locker->releaseLock($dbConnection, $postgresLockId); @@ -128,8 +128,8 @@ public function test_it_can_release_lock_twice_if_acquired_twice(): void $locker = $this->initLocker(); $dbConnection = $this->initPostgresPdoConnection(); $postgresLockId = $this->initPostgresLockId('test'); - $locker->acquireLock($dbConnection, $postgresLockId); - $locker->acquireLock($dbConnection, $postgresLockId); + $locker->tryAcquireLock($dbConnection, $postgresLockId); + $locker->tryAcquireLock($dbConnection, $postgresLockId); $isLockReleased1 = $locker->releaseLock($dbConnection, $postgresLockId); $isLockReleased2 = $locker->releaseLock($dbConnection, $postgresLockId); @@ -145,10 +145,10 @@ public function test_it_can_acquire_lock_in_second_connection_after_release(): v $dbConnection1 = $this->initPostgresPdoConnection(); $dbConnection2 = $this->initPostgresPdoConnection(); $postgresLockId = $this->initPostgresLockId('test'); - $locker->acquireLock($dbConnection1, $postgresLockId); + $locker->tryAcquireLock($dbConnection1, $postgresLockId); $locker->releaseLock($dbConnection1, $postgresLockId); - $isLockAcquired = $locker->acquireLock($dbConnection2, $postgresLockId); + $isLockAcquired = $locker->tryAcquireLock($dbConnection2, $postgresLockId); $this->assertTrue($isLockAcquired); $this->assertPgAdvisoryLockExistsInConnection($dbConnection2, $postgresLockId); @@ -161,11 +161,11 @@ public function test_it_cannot_acquire_lock_in_second_connection_after_one_relea $dbConnection1 = $this->initPostgresPdoConnection(); $dbConnection2 = $this->initPostgresPdoConnection(); $postgresLockId = $this->initPostgresLockId('test'); - $locker->acquireLock($dbConnection1, $postgresLockId); - $locker->acquireLock($dbConnection1, $postgresLockId); + $locker->tryAcquireLock($dbConnection1, $postgresLockId); + $locker->tryAcquireLock($dbConnection1, $postgresLockId); $isLockReleased = $locker->releaseLock($dbConnection1, $postgresLockId); - $isLockAcquired = $locker->acquireLock($dbConnection2, $postgresLockId); + $isLockAcquired = $locker->tryAcquireLock($dbConnection2, $postgresLockId); $this->assertTrue($isLockReleased); $this->assertFalse($isLockAcquired); @@ -192,7 +192,7 @@ public function test_it_cannot_release_lock_if_acquired_in_other_connection(): v $dbConnection1 = $this->initPostgresPdoConnection(); $dbConnection2 = $this->initPostgresPdoConnection(); $postgresLockId = $this->initPostgresLockId('test'); - $locker->acquireLock($dbConnection1, $postgresLockId); + $locker->tryAcquireLock($dbConnection1, $postgresLockId); $isLockReleased = $locker->releaseLock($dbConnection2, $postgresLockId); @@ -207,8 +207,8 @@ public function test_it_can_release_all_locks_in_connection(): void $dbConnection = $this->initPostgresPdoConnection(); $postgresLockId1 = $this->initPostgresLockId('test'); $postgresLockId2 = $this->initPostgresLockId('test2'); - $locker->acquireLock($dbConnection, $postgresLockId1); - $locker->acquireLock($dbConnection, $postgresLockId2); + $locker->tryAcquireLock($dbConnection, $postgresLockId1); + $locker->tryAcquireLock($dbConnection, $postgresLockId2); $locker->releaseAllLocks($dbConnection); @@ -234,10 +234,10 @@ public function test_it_can_release_all_locks_in_connection_but_keeps_other_lock $postgresLockId2 = $this->initPostgresLockId('test2'); $postgresLockId3 = $this->initPostgresLockId('test3'); $postgresLockId4 = $this->initPostgresLockId('test4'); - $locker->acquireLock($dbConnection1, $postgresLockId1); - $locker->acquireLock($dbConnection1, $postgresLockId2); - $locker->acquireLock($dbConnection2, $postgresLockId3); - $locker->acquireLock($dbConnection2, $postgresLockId4); + $locker->tryAcquireLock($dbConnection1, $postgresLockId1); + $locker->tryAcquireLock($dbConnection1, $postgresLockId2); + $locker->tryAcquireLock($dbConnection2, $postgresLockId3); + $locker->tryAcquireLock($dbConnection2, $postgresLockId4); $locker->releaseAllLocks($dbConnection1); @@ -253,7 +253,7 @@ public function test_it_can_acquire_lock_within_transaction(): void $postgresLockId = $this->initPostgresLockId('test'); $dbConnection->beginTransaction(); - $isLockAcquired = $locker->acquireLockWithinTransaction($dbConnection, $postgresLockId); + $isLockAcquired = $locker->tryAcquireLockWithinTransaction($dbConnection, $postgresLockId); $this->assertTrue($isLockAcquired); $this->assertPgAdvisoryLocksCount(1); @@ -271,7 +271,7 @@ public function test_it_cannot_acquire_lock_within_transaction_not_in_transactio $dbConnection = $this->initPostgresPdoConnection(); $postgresLockId = $this->initPostgresLockId('test'); - $locker->acquireLockWithinTransaction($dbConnection, $postgresLockId); + $locker->tryAcquireLockWithinTransaction($dbConnection, $postgresLockId); } public function test_it_cannot_acquire_lock_in_second_connection_if_taken_within_transaction(): void @@ -281,9 +281,9 @@ public function test_it_cannot_acquire_lock_in_second_connection_if_taken_within $dbConnection2 = $this->initPostgresPdoConnection(); $postgresLockId = $this->initPostgresLockId('test'); $dbConnection1->beginTransaction(); - $locker->acquireLockWithinTransaction($dbConnection1, $postgresLockId); + $locker->tryAcquireLockWithinTransaction($dbConnection1, $postgresLockId); - $isLockAcquired = $locker->acquireLock($dbConnection2, $postgresLockId); + $isLockAcquired = $locker->tryAcquireLock($dbConnection2, $postgresLockId); $this->assertFalse($isLockAcquired); $this->assertPgAdvisoryLocksCount(1); @@ -296,7 +296,7 @@ public function test_it_can_auto_release_lock_acquired_within_transaction_on_com $dbConnection = $this->initPostgresPdoConnection(); $postgresLockId = $this->initPostgresLockId('test'); $dbConnection->beginTransaction(); - $locker->acquireLockWithinTransaction($dbConnection, $postgresLockId); + $locker->tryAcquireLockWithinTransaction($dbConnection, $postgresLockId); $dbConnection->commit(); @@ -310,7 +310,7 @@ public function test_it_can_auto_release_lock_acquired_within_transaction_on_rol $dbConnection = $this->initPostgresPdoConnection(); $postgresLockId = $this->initPostgresLockId('test'); $dbConnection->beginTransaction(); - $locker->acquireLockWithinTransaction($dbConnection, $postgresLockId); + $locker->tryAcquireLockWithinTransaction($dbConnection, $postgresLockId); $dbConnection->rollBack(); @@ -324,7 +324,7 @@ public function test_it_can_auto_release_lock_acquired_within_transaction_on_con $dbConnection = $this->initPostgresPdoConnection(); $postgresLockId = $this->initPostgresLockId('test'); $dbConnection->beginTransaction(); - $locker->acquireLockWithinTransaction($dbConnection, $postgresLockId); + $locker->tryAcquireLockWithinTransaction($dbConnection, $postgresLockId); $dbConnection = null; @@ -337,7 +337,7 @@ public function test_it_cannot_release_lock_acquired_within_transaction(): void $dbConnection = $this->initPostgresPdoConnection(); $postgresLockId = $this->initPostgresLockId('test'); $dbConnection->beginTransaction(); - $locker->acquireLockWithinTransaction($dbConnection, $postgresLockId); + $locker->tryAcquireLockWithinTransaction($dbConnection, $postgresLockId); $isLockReleased = $locker->releaseLock($dbConnection, $postgresLockId); @@ -352,9 +352,9 @@ public function test_it_cannot_release_all_locks_acquired_within_transaction(): $dbConnection = $this->initPostgresPdoConnection(); $postgresLockId1 = $this->initPostgresLockId('test'); $postgresLockId2 = $this->initPostgresLockId('test2'); - $locker->acquireLock($dbConnection, $postgresLockId1); + $locker->tryAcquireLock($dbConnection, $postgresLockId1); $dbConnection->beginTransaction(); - $locker->acquireLockWithinTransaction($dbConnection, $postgresLockId2); + $locker->tryAcquireLockWithinTransaction($dbConnection, $postgresLockId2); $locker->releaseAllLocks($dbConnection);