Browse Source

Replace Lock::acquire() with DI::lock()->acquire()

pull/8071/head
nupplaPhil 1 year ago
parent
commit
634e657d23
No known key found for this signature in database GPG Key ID: D8365C3D36B77D90
13 changed files with 38 additions and 52 deletions
  1. +3
    -3
      src/Console/Lock.php
  2. +0
    -15
      src/Core/Lock.php
  3. +1
    -1
      src/Core/Lock/CacheLock.php
  4. +1
    -1
      src/Core/Lock/DatabaseLock.php
  5. +1
    -1
      src/Core/Lock/ILock.php
  6. +1
    -1
      src/Core/Lock/SemaphoreLock.php
  7. +3
    -2
      src/Core/Update.php
  8. +4
    -4
      src/Core/Worker.php
  9. +2
    -2
      src/Model/Item.php
  10. +1
    -1
      src/Protocol/OStatus.php
  11. +1
    -1
      tests/Util/DbaLockMockTrait.php
  12. +19
    -19
      tests/src/Core/Lock/LockTest.php
  13. +1
    -1
      tests/src/Core/Lock/SemaphoreLockTest.php

+ 3
- 3
src/Console/Lock.php View File

@ -156,11 +156,11 @@ HELP;
}
if (!empty($ttl) && !empty($timeout)) {
$result = $this->lock->acquireLock($lock, $timeout, $ttl);
$result = $this->lock->acquire($lock, $timeout, $ttl);
} elseif (!empty($timeout)) {
$result = $this->lock->acquireLock($lock, $timeout);
$result = $this->lock->acquire($lock, $timeout);
} else {
$result = $this->lock->acquireLock($lock);
$result = $this->lock->acquire($lock);
}
if ($result) {


+ 0
- 15
src/Core/Lock.php View File

@ -15,21 +15,6 @@ use Friendica\DI;
*/
class Lock
{
/**
* @brief Acquires a lock for a given name
*
* @param string $key Name of the lock
* @param integer $timeout Seconds until we give up
* @param integer $ttl The Lock lifespan, must be one of the Cache constants
*
* @return boolean Was the lock successful?
* @throws \Exception
*/
public static function acquire($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
{
return DI::lock()->acquireLock($key, $timeout, $ttl);
}
/**
* @brief Releases a lock if it was set by us
*


+ 1
- 1
src/Core/Lock/CacheLock.php View File

@ -30,7 +30,7 @@ class CacheLock extends Lock
/**
* (@inheritdoc)
*/
public function acquireLock($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES)
public function acquire($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES)
{
$got_lock = false;
$start = time();


+ 1
- 1
src/Core/Lock/DatabaseLock.php View File

@ -35,7 +35,7 @@ class DatabaseLock extends Lock
/**
* (@inheritdoc)
*/
public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
public function acquire($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
{
$got_lock = false;
$start = time();


+ 1
- 1
src/Core/Lock/ILock.php View File

@ -30,7 +30,7 @@ interface ILock
*
* @return boolean Was the lock successful?
*/
public function acquireLock($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES);
public function acquire($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES);
/**
* Releases a lock if it was set by us


+ 1
- 1
src/Core/Lock/SemaphoreLock.php View File

@ -36,7 +36,7 @@ class SemaphoreLock extends Lock
/**
* (@inheritdoc)
*/
public function acquireLock($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES)
public function acquire($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES)
{
self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
if (!empty(self::$semaphore[$key])) {


+ 3
- 2
src/Core/Update.php View File

@ -5,6 +5,7 @@ namespace Friendica\Core;
use Friendica\App;
use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
use Friendica\DI;
use Friendica\Util\Strings;
class Update
@ -95,7 +96,7 @@ class Update
// Compare the current structure with the defined structure
// If the Lock is acquired, never release it automatically to avoid double updates
if (Lock::acquire('dbupdate', 120, Cache::INFINITE)) {
if (DI::lock()->acquire('dbupdate', 120, Cache::INFINITE)) {
// Checks if the build changed during Lock acquiring (so no double update occurs)
$retryBuild = Config::get('system', 'build', null, true);
@ -181,7 +182,7 @@ class Update
// If the update fails or times-out completely you may need to
// delete the config entry to try again.
if (Lock::acquire('dbupdate_function', 120,Cache::INFINITE)) {
if (DI::lock()->acquire('dbupdate_function', 120,Cache::INFINITE)) {
// call the specific update
$retval = $funcname();


+ 4
- 4
src/Core/Worker.php View File

@ -115,7 +115,7 @@ class Worker
}
// Trying to fetch new processes - but only once when successful
if (!$refetched && Lock::acquire('worker_process', 0)) {
if (!$refetched && DI::lock()->acquire('worker_process', 0)) {
self::findWorkerProcesses();
Lock::release('worker_process');
self::$state = self::STATE_REFETCH;
@ -129,7 +129,7 @@ class Worker
if (!self::getWaitingJobForPID()) {
self::$state = self::STATE_LONG_LOOP;
if (Lock::acquire('worker', 0)) {
if (DI::lock()->acquire('worker', 0)) {
// Count active workers and compare them with a maximum value that depends on the load
if (self::tooMuchWorkers()) {
Logger::log('Active worker limit reached, quitting.', Logger::DEBUG);
@ -933,7 +933,7 @@ class Worker
}
$stamp = (float)microtime(true);
if (!Lock::acquire('worker_process')) {
if (!DI::lock()->acquire('worker_process')) {
return false;
}
self::$lock_duration += (microtime(true) - $stamp);
@ -1172,7 +1172,7 @@ class Worker
}
// If there is a lock then we don't have to check for too much worker
if (!Lock::acquire('worker', 0)) {
if (!DI::lock()->acquire('worker', 0)) {
return $added;
}


+ 2
- 2
src/Model/Item.php View File

@ -2060,7 +2060,7 @@ class Item
}
// To avoid timing problems, we are using locks.
$locked = Lock::acquire('item_insert_activity');
$locked = DI::lock()->acquire('item_insert_activity');
if (!$locked) {
Logger::log("Couldn't acquire lock for URI " . $item['uri'] . " - proceeding anyway.");
}
@ -2103,7 +2103,7 @@ class Item
}
// To avoid timing problems, we are using locks.
$locked = Lock::acquire('item_insert_content');
$locked = DI::lock()->acquire('item_insert_content');
if (!$locked) {
Logger::log("Couldn't acquire lock for URI " . $item['uri'] . " - proceeding anyway.");
}


+ 1
- 1
src/Protocol/OStatus.php View File

@ -539,7 +539,7 @@ class OStatus
Logger::log("Item with uri ".$item["uri"]." is from a blocked contact.", Logger::DEBUG);
} else {
// We are having duplicated entries. Hopefully this solves it.
if (Lock::acquire('ostatus_process_item_insert')) {
if (DI::lock()->acquire('ostatus_process_item_insert')) {
$ret = Item::insert($item);
Lock::release('ostatus_process_item_insert');
Logger::log("Item with uri ".$item["uri"]." for user ".$importer["uid"].' stored. Return value: '.$ret);


+ 1
- 1
tests/Util/DbaLockMockTrait.php View File

@ -22,7 +22,7 @@ trait DbaLockMockTrait
* @param null $time The current timestamp
* @param null|int $times How often the method will get used
*
*@see DatabaseLock::acquireLock()
*@see DatabaseLock::acquire()
*
*/
public function mockAcquireLock($key, $ttl = Cache::FIVE_MINUTES, $locked = false, $pid = null, $rowExists = true, $time = null, $times = null)


+ 19
- 19
tests/src/Core/Lock/LockTest.php View File

@ -38,7 +38,7 @@ abstract class LockTest extends MockedTest
public function testLock()
{
$this->assertFalse($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->acquire('foo', 1));
$this->assertTrue($this->instance->isLocked('foo'));
$this->assertFalse($this->instance->isLocked('bar'));
}
@ -49,10 +49,10 @@ abstract class LockTest extends MockedTest
public function testDoubleLock()
{
$this->assertFalse($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->acquire('foo', 1));
$this->assertTrue($this->instance->isLocked('foo'));
// We already locked it
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->acquire('foo', 1));
}
/**
@ -61,7 +61,7 @@ abstract class LockTest extends MockedTest
public function testReleaseLock()
{
$this->assertFalse($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->acquire('foo', 1));
$this->assertTrue($this->instance->isLocked('foo'));
$this->instance->releaseLock('foo');
$this->assertFalse($this->instance->isLocked('foo'));
@ -72,9 +72,9 @@ abstract class LockTest extends MockedTest
*/
public function testReleaseAll()
{
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->acquireLock('bar', 1));
$this->assertTrue($this->instance->acquireLock('nice', 1));
$this->assertTrue($this->instance->acquire('foo', 1));
$this->assertTrue($this->instance->acquire('bar', 1));
$this->assertTrue($this->instance->acquire('nice', 1));
$this->assertTrue($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->isLocked('bar'));
@ -95,9 +95,9 @@ abstract class LockTest extends MockedTest
$this->assertFalse($this->instance->isLocked('foo'));
$this->assertFalse($this->instance->isLocked('bar'));
$this->assertFalse($this->instance->isLocked('nice'));
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->acquireLock('bar', 1));
$this->assertTrue($this->instance->acquireLock('nice', 1));
$this->assertTrue($this->instance->acquire('foo', 1));
$this->assertTrue($this->instance->acquire('bar', 1));
$this->assertTrue($this->instance->acquire('nice', 1));
$this->assertTrue($this->instance->releaseLock('foo'));
@ -117,7 +117,7 @@ abstract class LockTest extends MockedTest
public function testReleaseWitTTL()
{
$this->assertFalse($this->instance->isLocked('test'));
$this->assertTrue($this->instance->acquireLock('test', 1, 10));
$this->assertTrue($this->instance->acquire('test', 1, 10));
$this->assertTrue($this->instance->isLocked('test'));
$this->assertTrue($this->instance->releaseLock('test'));
$this->assertFalse($this->instance->isLocked('test'));
@ -128,9 +128,9 @@ abstract class LockTest extends MockedTest
*/
public function testGetLocks()
{
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->acquireLock('bar', 1));
$this->assertTrue($this->instance->acquireLock('nice', 1));
$this->assertTrue($this->instance->acquire('foo', 1));
$this->assertTrue($this->instance->acquire('bar', 1));
$this->assertTrue($this->instance->acquire('nice', 1));
$this->assertTrue($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->isLocked('bar'));
@ -148,9 +148,9 @@ abstract class LockTest extends MockedTest
*/
public function testGetLocksWithPrefix()
{
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->acquireLock('test1', 1));
$this->assertTrue($this->instance->acquireLock('test2', 1));
$this->assertTrue($this->instance->acquire('foo', 1));
$this->assertTrue($this->instance->acquire('test1', 1));
$this->assertTrue($this->instance->acquire('test2', 1));
$this->assertTrue($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->isLocked('test1'));
@ -174,8 +174,8 @@ abstract class LockTest extends MockedTest
$this->assertFalse($this->instance->isLocked('bar'));
// TODO [nupplaphil] - Because of the Datetime-Utils for the database, we have to wait a FULL second between the checks to invalidate the db-locks/cache
$this->assertTrue($this->instance->acquireLock('foo', 2, 1));
$this->assertTrue($this->instance->acquireLock('bar', 2, 3));
$this->assertTrue($this->instance->acquire('foo', 2, 1));
$this->assertTrue($this->instance->acquire('bar', 2, 3));
$this->assertTrue($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->isLocked('bar'));


+ 1
- 1
tests/src/Core/Lock/SemaphoreLockTest.php View File

@ -85,7 +85,7 @@ class SemaphoreLockTest extends LockTest
touch($file);
$this->assertTrue(file_exists($file));
$this->assertTrue($this->instance->acquireLock('test'));
$this->assertTrue($this->instance->acquire('test'));
$this->assertTrue($this->instance->isLocked('test'));
$this->assertTrue($this->instance->releaseLock('test'));
}


Loading…
Cancel
Save