|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Lock\Store; |
| 13 | + |
| 14 | +use Symfony\Component\Lock\Exception\LockAcquiringException; |
| 15 | +use Symfony\Component\Lock\Exception\LockConflictedException; |
| 16 | +use Symfony\Component\Lock\Exception\LockReleasingException; |
| 17 | +use Symfony\Component\Lock\Exception\NotSupportedException; |
| 18 | +use Symfony\Component\Lock\Key; |
| 19 | +use Symfony\Component\Lock\StoreInterface; |
| 20 | + |
| 21 | +/** |
| 22 | + * ZookeeperStore is a StoreInterface implementation using Zookeeper as store engine. |
| 23 | + * |
| 24 | + * @author Ganesh Chandrasekaran <gchandrasekaran@wayfair.com> |
| 25 | + */ |
| 26 | +class ZookeeperStore implements StoreInterface |
| 27 | +{ |
| 28 | + private $zookeeper; |
| 29 | + |
| 30 | + public function __construct(\Zookeeper $zookeeper) |
| 31 | + { |
| 32 | + $this->zookeeper = $zookeeper; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * {@inheritdoc} |
| 37 | + */ |
| 38 | + public function save(Key $key) |
| 39 | + { |
| 40 | + if ($this->exists($key)) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + $resource = $this->getKeyResource($key); |
| 45 | + $token = $this->getUniqueToken($key); |
| 46 | + |
| 47 | + $this->createNewLock($resource, $token); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * {@inheritdoc} |
| 52 | + */ |
| 53 | + public function delete(Key $key) |
| 54 | + { |
| 55 | + if (!$this->exists($key)) { |
| 56 | + return; |
| 57 | + } |
| 58 | + $resource = $this->getKeyResource($key); |
| 59 | + try { |
| 60 | + $this->zookeeper->delete($resource); |
| 61 | + } catch (\ZookeeperException $exception) { |
| 62 | + // For Zookeeper Ephemeral Nodes, the node will be deleted upon session death. But, if we want to unlock |
| 63 | + // the lock before proceeding further in the session, the client should be aware of this |
| 64 | + throw new LockReleasingException($exception); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * {@inheritdoc} |
| 70 | + */ |
| 71 | + public function exists(Key $key): bool |
| 72 | + { |
| 73 | + $resource = $this->getKeyResource($key); |
| 74 | + try { |
| 75 | + return $this->zookeeper->get($resource) === $this->getUniqueToken($key); |
| 76 | + } catch (\ZookeeperException $ex) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * {@inheritdoc} |
| 83 | + */ |
| 84 | + public function waitAndSave(Key $key) |
| 85 | + { |
| 86 | + throw new NotSupportedException(); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * {@inheritdoc} |
| 91 | + */ |
| 92 | + public function putOffExpiration(Key $key, $ttl) |
| 93 | + { |
| 94 | + throw new NotSupportedException(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Creates a zookeeper node. |
| 99 | + * |
| 100 | + * @param string $node The node which needs to be created |
| 101 | + * @param string $value The value to be assigned to a zookeeper node |
| 102 | + * |
| 103 | + * @throws LockConflictedException |
| 104 | + * @throws LockAcquiringException |
| 105 | + */ |
| 106 | + private function createNewLock(string $node, string $value) |
| 107 | + { |
| 108 | + // Default Node Permissions |
| 109 | + $acl = array(array('perms' => \Zookeeper::PERM_ALL, 'scheme' => 'world', 'id' => 'anyone')); |
| 110 | + // This ensures that the nodes are deleted when the client session to zookeeper server ends. |
| 111 | + $type = \Zookeeper::EPHEMERAL; |
| 112 | + |
| 113 | + try { |
| 114 | + $this->zookeeper->create($node, $value, $acl, $type); |
| 115 | + } catch (\ZookeeperException $ex) { |
| 116 | + if (\Zookeeper::NODEEXISTS === $ex->getCode()) { |
| 117 | + throw new LockConflictedException($ex); |
| 118 | + } |
| 119 | + |
| 120 | + throw new LockAcquiringException($ex); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private function getKeyResource(Key $key): string |
| 125 | + { |
| 126 | + // Since we do not support storing locks as multi-level nodes, we convert them to be stored at root level. |
| 127 | + // For example: foo/bar will become /foo-bar and /foo/bar will become /-foo-bar |
| 128 | + $resource = (string) $key; |
| 129 | + |
| 130 | + if (false !== \strpos($resource, '/')) { |
| 131 | + $resource = \strtr($resource, array('/' => '-')).'-'.sha1($resource); |
| 132 | + } |
| 133 | + |
| 134 | + if ('' === $resource) { |
| 135 | + $resource = sha1($resource); |
| 136 | + } |
| 137 | + |
| 138 | + return '/'.$resource; |
| 139 | + } |
| 140 | + |
| 141 | + private function getUniqueToken(Key $key): string |
| 142 | + { |
| 143 | + if (!$key->hasState(self::class)) { |
| 144 | + $token = base64_encode(random_bytes(32)); |
| 145 | + $key->setState(self::class, $token); |
| 146 | + } |
| 147 | + |
| 148 | + return $key->getState(self::class); |
| 149 | + } |
| 150 | +} |
0 commit comments