-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Add Zookeeper data store for Lock Component #27920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nicolas-grekas
merged 1 commit into
symfony:master
from
wayfair-archive:gchandrasekaran_addZookeeperDataStore
Sep 21, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Lock\Store; | ||
|
||
use Symfony\Component\Lock\Exception\LockAcquiringException; | ||
use Symfony\Component\Lock\Exception\LockConflictedException; | ||
use Symfony\Component\Lock\Exception\LockReleasingException; | ||
use Symfony\Component\Lock\Exception\NotSupportedException; | ||
use Symfony\Component\Lock\Key; | ||
use Symfony\Component\Lock\StoreInterface; | ||
|
||
/** | ||
* ZookeeperStore is a StoreInterface implementation using Zookeeper as store engine. | ||
* | ||
* @author Ganesh Chandrasekaran <gchandrasekaran@wayfair.com> | ||
*/ | ||
class ZookeeperStore implements StoreInterface | ||
GaneshChandrasekaran-zz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
private $zookeeper; | ||
|
||
public function __construct(\Zookeeper $zookeeper) | ||
{ | ||
$this->zookeeper = $zookeeper; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function save(Key $key) | ||
{ | ||
if ($this->exists($key)) { | ||
return; | ||
} | ||
|
||
$resource = $this->getKeyResource($key); | ||
$token = $this->getUniqueToken($key); | ||
|
||
$this->createNewLock($resource, $token); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function delete(Key $key) | ||
{ | ||
if (!$this->exists($key)) { | ||
return; | ||
} | ||
$resource = $this->getKeyResource($key); | ||
try { | ||
$this->zookeeper->delete($resource); | ||
} catch (\ZookeeperException $exception) { | ||
// For Zookeeper Ephemeral Nodes, the node will be deleted upon session death. But, if we want to unlock | ||
// the lock before proceeding further in the session, the client should be aware of this | ||
throw new LockReleasingException($exception); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function exists(Key $key): bool | ||
{ | ||
$resource = $this->getKeyResource($key); | ||
try { | ||
return $this->zookeeper->get($resource) === $this->getUniqueToken($key); | ||
} catch (\ZookeeperException $ex) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function waitAndSave(Key $key) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function putOffExpiration(Key $key, $ttl) | ||
{ | ||
throw new NotSupportedException(); | ||
} | ||
|
||
/** | ||
* Creates a zookeeper node. | ||
* | ||
* @param string $node The node which needs to be created | ||
* @param string $value The value to be assigned to a zookeeper node | ||
* | ||
* @throws LockConflictedException | ||
* @throws LockAcquiringException | ||
*/ | ||
private function createNewLock(string $node, string $value) | ||
{ | ||
// Default Node Permissions | ||
$acl = array(array('perms' => \Zookeeper::PERM_ALL, 'scheme' => 'world', 'id' => 'anyone')); | ||
// This ensures that the nodes are deleted when the client session to zookeeper server ends. | ||
$type = \Zookeeper::EPHEMERAL; | ||
|
||
try { | ||
$this->zookeeper->create($node, $value, $acl, $type); | ||
} catch (\ZookeeperException $ex) { | ||
if (\Zookeeper::NODEEXISTS === $ex->getCode()) { | ||
throw new LockConflictedException($ex); | ||
} | ||
|
||
throw new LockAcquiringException($ex); | ||
} | ||
} | ||
|
||
private function getKeyResource(Key $key): string | ||
{ | ||
// Since we do not support storing locks as multi-level nodes, we convert them to be stored at root level. | ||
// For example: foo/bar will become /foo-bar and /foo/bar will become /-foo-bar | ||
$resource = (string) $key; | ||
|
||
if (false !== \strpos($resource, '/')) { | ||
$resource = \strtr($resource, array('/' => '-')).'-'.sha1($resource); | ||
} | ||
|
||
if ('' === $resource) { | ||
$resource = sha1($resource); | ||
} | ||
|
||
return '/'.$resource; | ||
} | ||
|
||
private function getUniqueToken(Key $key): string | ||
{ | ||
if (!$key->hasState(self::class)) { | ||
$token = base64_encode(random_bytes(32)); | ||
$key->setState(self::class, $token); | ||
} | ||
|
||
return $key->getState(self::class); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/Symfony/Component/Lock/Tests/Store/ZookeeperStoreTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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; | ||
use Symfony\Component\Lock\Store\StoreFactory; | ||
use Symfony\Component\Lock\Store\ZookeeperStore; | ||
|
||
/** | ||
* @author Ganesh Chandrasekaran <gchandrasekaran@wayfair.com> | ||
* | ||
* @requires extension zookeeper | ||
*/ | ||
class ZookeeperStoreTest extends AbstractStoreTest | ||
{ | ||
public function getStore(): ZookeeperStore | ||
{ | ||
$zookeeper_server = getenv('ZOOKEEPER_HOST').':2181'; | ||
|
||
$zookeeper = new \Zookeeper(implode(',', array($zookeeper_server))); | ||
|
||
return StoreFactory::createStore($zookeeper); | ||
} | ||
|
||
public function testSaveSucceedsWhenPathContainsMoreThanOneNode() | ||
{ | ||
$store = $this->getStore(); | ||
$resource = '/baseNode/lockNode'; | ||
GaneshChandrasekaran-zz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$key = new Key($resource); | ||
|
||
$store->save($key); | ||
$this->assertTrue($store->exists($key)); | ||
|
||
$store->delete($key); | ||
$this->assertFalse($store->exists($key)); | ||
} | ||
|
||
public function testSaveSucceedsWhenPathContainsOneNode() | ||
{ | ||
$store = $this->getStore(); | ||
$resource = '/baseNode'; | ||
$key = new Key($resource); | ||
|
||
$store->save($key); | ||
$this->assertTrue($store->exists($key)); | ||
|
||
$store->delete($key); | ||
$this->assertFalse($store->exists($key)); | ||
} | ||
|
||
public function testSaveSucceedsWhenPathsContainSameFirstNode() | ||
{ | ||
$store = $this->getStore(); | ||
$resource = 'foo/bar'; | ||
$key = new Key($resource); | ||
|
||
$store->save($key); | ||
$this->assertTrue($store->exists($key)); | ||
|
||
$resource2 = 'foo'; | ||
$key2 = new Ke DFC0 y($resource2); | ||
|
||
$this->assertFalse($store->exists($key2)); | ||
$store->save($key2); | ||
$this->assertTrue($store->exists($key2)); | ||
|
||
$store->delete($key2); | ||
$this->assertFalse($store->exists($key2)); | ||
|
||
$store->delete($key); | ||
$this->assertFalse($store->exists($key)); | ||
} | ||
|
||
public function testRootPathIsLockable() | ||
{ | ||
$store = $this->getStore(); | ||
$resource = '/'; | ||
$key = new Key($resource); | ||
|
||
$store->save($key); | ||
$this->assertTrue($store->exists($key)); | ||
|
||
$store->delete($key); | ||
$this->assertFalse($store->exists($key)); | ||
} | ||
|
||
public function testEmptyStringIsLockable() | ||
{ | ||
$store = $this->getStore(); | ||
$resource = ''; | ||
$key = new Key($resource); | ||
|
||
$store->save($key); | ||
$this->assertTrue($store->exists($key)); | ||
|
||
$store->delete($key); | ||
$this->assertFalse($store->exists($key)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.