Skip to content

Commit

Permalink
Add human readable value to SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkomarev committed Jul 19, 2024
1 parent 0930009 commit 5193c50
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3.9"

services:
app:
container_name: php-db-locker-app
Expand Down
2 changes: 1 addition & 1 deletion src/LockId/PostgresLockId.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
}

public static function fromLockId(
LockId $lockId
LockId $lockId,
): self {
$humanReadableValue = (string)$lockId;

Expand Down
14 changes: 8 additions & 6 deletions src/Locker/PostgresAdvisoryLocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<<<SQL
SELECT pg_try_advisory_lock(:lock_id);
SELECT pg_try_advisory_lock(:lock_id); -- $postgresLockId->humanReadableValue
SQL
);
$statement->execute(
Expand All @@ -37,7 +38,7 @@ public function acquireLock(
return $statement->fetchColumn(0);
}

public function acquireLockWithinTransaction(
public function tryAcquireLockWithinTransaction(
PDO $dbConnection,
PostgresLockId $postgresLockId,
): bool {
Expand All @@ -49,9 +50,10 @@ public function acquireLockWithinTransaction(
);
}

// TODO: Need to cleanup humanReadableValue?
$statement = $dbConnection->prepare(
<<<SQL
SELECT pg_try_advisory_xact_lock(:lock_id);
SELECT pg_try_advisory_xact_lock(:lock_id); -- $postgresLockId->humanReadableValue
SQL
);
$statement->execute(
Expand All @@ -68,7 +70,7 @@ public function releaseLock(
PostgresLockId $postgresLockId,
): bool {
$statement = $dbConnection->prepare(
<<<SQL
<<<'SQL'
SELECT pg_advisory_unlock(:lock_id);
SQL
);
Expand All @@ -85,7 +87,7 @@ public function releaseAllLocks(
PDO $dbConnection,
): void {
$statement = $dbConnection->prepare(
<<<SQL
<<<'SQL'
SELECT pg_advisory_unlock_all();
SQL
);
Expand Down
68 changes: 34 additions & 34 deletions test/Integration/Locker/PostgresAdvisoryLockerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function test_it_can_acquire_lock(): void
$dbConnection = $this->initPostgresPdoConnection();
$postgresLockId = $this->initPostgresLockId('test');

$isLockAcquired = $locker->acquireLock($dbConnection, $postgresLockId);
$isLockAcquired = $locker->tryAcquireLock($dbConnection, $postgresLockId);

$this->assertTrue($isLockAcquired);
$this->assertPgAdvisoryLockExistsInConnection($dbConnection, $postgresLockId);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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;

Expand All @@ -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);

Expand All @@ -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);

Expand Down

0 comments on commit 5193c50

Please sign in to comment.