From 3efcaa231b5150f9cc34b490cd26f2fb3585d8c6 Mon Sep 17 00:00:00 2001 From: Ganesh Chandrasekaran Date: Thu, 4 Oct 2018 20:42:23 +0530 Subject: [PATCH] Create new NotExpirableStoreException for stores that don't support expiration of locks. --- .../Exception/NotExpirableStoreException.php | 21 +++++++++++ src/Symfony/Component/Lock/Lock.php | 3 ++ .../Component/Lock/Store/CombinedStore.php | 4 ++ .../Component/Lock/Store/FlockStore.php | 3 +- .../Component/Lock/Store/SemaphoreStore.php | 3 +- .../Component/Lock/Store/ZookeeperStore.php | 3 +- src/Symfony/Component/Lock/StoreInterface.php | 2 + .../Lock/Tests/Store/CombinedStoreTest.php | 6 ++- .../Lock/Tests/Store/FlockStoreTest.php | 1 + .../Tests/Store/NotExpiringStoreTestTrait.php | 37 +++++++++++++++++++ .../Lock/Tests/Store/SemaphoreStoreTest.php | 1 + .../Lock/Tests/Store/ZookeeperStoreTest.php | 2 + 12 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 src/Symfony/Component/Lock/Exception/NotExpirableStoreException.php create mode 100644 src/Symfony/Component/Lock/Tests/Store/NotExpiringStoreTestTrait.php diff --git a/src/Symfony/Component/Lock/Exception/NotExpirableStoreException.php b/src/Symfony/Component/Lock/Exception/NotExpirableStoreException.php new file mode 100644 index 0000000000000..c7f443e433524 --- /dev/null +++ b/src/Symfony/Component/Lock/Exception/NotExpirableStoreException.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Lock\Exception; + +/** + * NotExpirableStoreException is thrown when a store doesn't support expiration of locks. + * + * @author Ganesh Chandrasekaran + */ +class NotExpirableStoreException extends \LogicException implements ExceptionInterface +{ +} diff --git a/src/Symfony/Component/Lock/Lock.php b/src/Symfony/Component/Lock/Lock.php index 03ac44cd12f3c..c7e58e264234e 100644 --- a/src/Symfony/Component/Lock/Lock.php +++ b/src/Symfony/Component/Lock/Lock.php @@ -19,6 +19,7 @@ use Symfony\Component\Lock\Exception\LockConflictedException; use Symfony\Component\Lock\Exception\LockExpiredException; use Symfony\Component\Lock\Exception\LockReleasingException; +use Symfony\Component\Lock\Exception\NotExpirableStoreException; /** * Lock is the default implementation of the LockInterface. @@ -124,6 +125,8 @@ public function refresh($ttl = null) } $this->logger->info('Expiration defined for "{resource}" lock for "{ttl}" seconds.', array('resource' => $this->key, 'ttl' => $ttl)); + } catch (NotExpirableStoreException $e) { + $this->logger->notice('The store does not support expiration of locks.', array('store' => $this->store)); } catch (LockConflictedException $e) { $this->dirty = false; $this->logger->notice('Failed to define an expiration for the "{resource}" lock, someone else acquired the lock.', array('resource' => $this->key)); diff --git a/src/Symfony/Component/Lock/Store/CombinedStore.php b/src/Symfony/Component/Lock/Store/CombinedStore.php index b0ca2c1454e0b..ece30d4334467 100644 --- a/src/Symfony/Component/Lock/Store/CombinedStore.php +++ b/src/Symfony/Component/Lock/Store/CombinedStore.php @@ -17,6 +17,7 @@ use Symfony\Component\Lock\Exception\InvalidArgumentException; use Symfony\Component\Lock\Exception\LockConflictedException; use Symfony\Component\Lock\Exception\LockExpiredException; +use Symfony\Component\Lock\Exception\NotExpirableStoreException; use Symfony\Component\Lock\Exception\NotSupportedException; use Symfony\Component\Lock\Key; use Symfony\Component\Lock\StoreInterface; @@ -115,6 +116,9 @@ public function putOffExpiration(Key $key, $ttl) $store->putOffExpiration($key, $adjustedTtl); ++$successCount; + } catch (NotExpirableStoreException $e) { + $this->logger->notice('The store does not support expiration of locks.', array('store' => $store)); + ++$successCount; } catch (\Exception $e) { $this->logger->warning('One store failed to put off the expiration of the "{resource}" lock.', array('resource' => $key, 'store' => $store, 'exception' => $e)); ++$failureCount; diff --git a/src/Symfony/Component/Lock/Store/FlockStore.php b/src/Symfony/Component/Lock/Store/FlockStore.php index 5b2732d30ac6f..e134ed9aa992a 100644 --- a/src/Symfony/Component/Lock/Store/FlockStore.php +++ b/src/Symfony/Component/Lock/Store/FlockStore.php @@ -14,6 +14,7 @@ use Symfony\Component\Lock\Exception\InvalidArgumentException; use Symfony\Component\Lock\Exception\LockConflictedException; use Symfony\Component\Lock\Exception\LockStorageException; +use Symfony\Component\Lock\Exception\NotExpirableStoreException; use Symfony\Component\Lock\Key; use Symfony\Component\Lock\StoreInterface; @@ -108,7 +109,7 @@ private function lock(Key $key, $blocking) */ public function putOffExpiration(Key $key, $ttl) { - // do nothing, the flock locks forever. + throw new NotExpirableStoreException(); } /** diff --git a/src/Symfony/Component/Lock/Store/SemaphoreStore.php b/src/Symfony/Component/Lock/Store/SemaphoreStore.php index 47f8616b0a84b..369a2b96357d4 100644 --- a/src/Symfony/Component/Lock/Store/SemaphoreStore.php +++ b/src/Symfony/Component/Lock/Store/SemaphoreStore.php @@ -13,6 +13,7 @@ use Symfony\Component\Lock\Exception\InvalidArgumentException; use Symfony\Component\Lock\Exception\LockConflictedException; +use Symfony\Component\Lock\Exception\NotExpirableStoreException; use Symfony\Component\Lock\Key; use Symfony\Component\Lock\StoreInterface; @@ -102,7 +103,7 @@ public function delete(Key $key) */ public function putOffExpiration(Key $key, $ttl) { - // do nothing, the semaphore locks forever. + throw new NotExpirableStoreException(); } /** diff --git a/src/Symfony/Component/Lock/Store/ZookeeperStore.php b/src/Symfony/Component/Lock/Store/ZookeeperStore.php index 2304dd47a0881..964aab8ff4804 100644 --- a/src/Symfony/Component/Lock/Store/ZookeeperStore.php +++ b/src/Symfony/Component/Lock/Store/ZookeeperStore.php @@ -14,6 +14,7 @@ use Symfony\Component\Lock\Exception\LockAcquiringException; use Symfony\Component\Lock\Exception\LockConflictedException; use Symfony\Component\Lock\Exception\LockReleasingException; +use Symfony\Component\Lock\Exception\NotExpirableStoreException; use Symfony\Component\Lock\Exception\NotSupportedException; use Symfony\Component\Lock\Key; use Symfony\Component\Lock\StoreInterface; @@ -91,7 +92,7 @@ public function waitAndSave(Key $key) */ public function putOffExpiration(Key $key, $ttl) { - throw new NotSupportedException(); + throw new NotExpirableStoreException(); } /** diff --git a/src/Symfony/Component/Lock/StoreInterface.php b/src/Symfony/Component/Lock/StoreInterface.php index d3d446292648d..8ffa0430d44b2 100644 --- a/src/Symfony/Component/Lock/StoreInterface.php +++ b/src/Symfony/Component/Lock/StoreInterface.php @@ -14,6 +14,7 @@ use Symfony\Component\Lock\Exception\LockAcquiringException; use Symfony\Component\Lock\Exception\LockConflictedException; use Symfony\Component\Lock\Exception\LockReleasingException; +use Symfony\Component\Lock\Exception\NotExpirableStoreException; use Symfony\Component\Lock\Exception\NotSupportedException; /** @@ -49,6 +50,7 @@ public function waitAndSave(Key $key); * @param float $ttl amount of second to keep the lock in the store * * @throws LockConflictedException + * @throws NotExpirableStoreException * @throws NotSupportedException */ public function putOffExpiration(Key $key, $ttl); diff --git a/src/Symfony/Component/Lock/Tests/Store/CombinedStoreTest.php b/src/Symfony/Component/Lock/Tests/Store/CombinedStoreTest.php index f2c55b056b324..b8b2810c85c9d 100644 --- a/src/Symfony/Component/Lock/Tests/Store/CombinedStoreTest.php +++ b/src/Symfony/Component/Lock/Tests/Store/CombinedStoreTest.php @@ -15,6 +15,7 @@ use Symfony\Component\Lock\Key; use Symfony\Component\Lock\Store\CombinedStore; use Symfony\Component\Lock\Store\RedisStore; +use Symfony\Component\Lock\Store\ZookeeperStore; use Symfony\Component\Lock\StoreInterface; use Symfony\Component\Lock\Strategy\StrategyInterface; use Symfony\Component\Lock\Strategy\UnanimousStrategy; @@ -46,7 +47,10 @@ public function getStore() self::markTestSkipped($e->getMessage()); } - return new CombinedStore(array(new RedisStore($redis)), new UnanimousStrategy()); + $zookeeper_server = getenv('ZOOKEEPER_HOST').':2181'; + $zookeeper = new \Zookeeper(implode(',', array($zookeeper_server))); + + return new CombinedStore(array(new RedisStore($redis), new ZookeeperStore($zookeeper)), new UnanimousStrategy()); } /** @var \PHPUnit_Framework_MockObject_MockObject */ diff --git a/src/Symfony/Component/Lock/Tests/Store/FlockStoreTest.php b/src/Symfony/Component/Lock/Tests/Store/FlockStoreTest.php index ef3650c3124b5..90c712feb59e7 100644 --- a/src/Symfony/Component/Lock/Tests/Store/FlockStoreTest.php +++ b/src/Symfony/Component/Lock/Tests/Store/FlockStoreTest.php @@ -20,6 +20,7 @@ class FlockStoreTest extends AbstractStoreTest { use BlockingStoreTestTrait; + use NotExpiringStoreTestTrait; /** * {@inheritdoc} diff --git a/src/Symfony/Component/Lock/Tests/Store/NotExpiringStoreTestTrait.php b/src/Symfony/Component/Lock/Tests/Store/NotExpiringStoreTestTrait.php new file mode 100644 index 0000000000000..221c94302592f --- /dev/null +++ b/src/Symfony/Component/Lock/Tests/Store/NotExpiringStoreTestTrait.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Lock\Tests\Store; + +use Symfony\Component\Lock\Key; + +/** + * @author Ganesh Chandrasekaran + */ +trait NotExpiringStoreTestTrait +{ + /** + * @see AbstractStoreTest::getStore() + */ + abstract protected function getStore(); + + /** + * @expectedException \Symfony\Component\Lock\Exception\NotExpirableStoreException + */ + public function testPutOffExpirationThrowsException() + { + $store = $this->getStore(); + $key = new Key(uniqid(__METHOD__, true)); + + $store->save($key); + $store->putOffExpiration($key, 10.0); + } +} diff --git a/src/Symfony/Component/Lock/Tests/Store/SemaphoreStoreTest.php b/src/Symfony/Component/Lock/Tests/Store/SemaphoreStoreTest.php index 6c265b98bdb42..2d4521874b15c 100644 --- a/src/Symfony/Component/Lock/Tests/Store/SemaphoreStoreTest.php +++ b/src/Symfony/Component/Lock/Tests/Store/SemaphoreStoreTest.php @@ -22,6 +22,7 @@ class SemaphoreStoreTest extends AbstractStoreTest { use BlockingStoreTestTrait; + use NotExpiringStoreTestTrait; /** * {@inheritdoc} diff --git a/src/Symfony/Component/Lock/Tests/Store/ZookeeperStoreTest.php b/src/Symfony/Component/Lock/Tests/Store/ZookeeperStoreTest.php index 73be28bcca3ea..0dd00d121aa6b 100644 --- a/src/Symfony/Component/Lock/Tests/Store/ZookeeperStoreTest.php +++ b/src/Symfony/Component/Lock/Tests/Store/ZookeeperStoreTest.php @@ -22,6 +22,8 @@ */ class ZookeeperStoreTest extends AbstractStoreTest { + use NotExpiringStoreTestTrait; + public function getStore(): ZookeeperStore { $zookeeper_server = getenv('ZOOKEEPER_HOST').':2181'; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy