Skip to content

Commit 9d65684

Browse files
committed
MAGETWO-98151: Add support ZooKeeper locks
1 parent e7a723a commit 9d65684

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

lib/internal/Magento/Framework/Lock/Backend/Zookeeper.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,18 @@ class Zookeeper implements LockManagerInterface
8484
*/
8585
public function __construct(string $host, string $path = self::DEFAULT_PATH)
8686
{
87-
if (empty($path)) {
87+
if (!$path) {
8888
throw new RuntimeException(
8989
new Phrase('The path needs to be a non-empty string.')
9090
);
9191
}
9292

93+
if (!$host) {
94+
throw new RuntimeException(
95+
new Phrase('The host needs to be a non-empty string.')
96+
);
97+
}
98+
9399
$this->host = $host;
94100
$this->path = preg_replace('#\/*$#', '', $path) ?: '/';
95101
}
@@ -221,18 +227,18 @@ private function checkAndCreateParentNode(string $path): bool
221227
*/
222228
private function getIndex(string $key)
223229
{
224-
if (!preg_match("/[0-9]+$/", $key, $matches))
230+
if (!preg_match('/' . $this->lockName . '([0-9]+)$/', $key, $matches))
225231
return null;
226232

227-
return intval($matches[0]);
233+
return intval($matches[1]);
228234
}
229235

230236
/**
231237
* Checks if there is any sequence node under parent of $fullKey.
232238
* At first checks that the $fullKey node is present, if not - returns false.
233239
*
234-
* If $indexKey is non-null and there is a smaller index that $indexKey then returns true,
235-
* if all the nodes are larger than $indexKey then returns false.
240+
* If $indexKey is non-null and there is a smaller index than $indexKey then returns true,
241+
* otherwise returns false.
236242
*
237243
* @param string $fullKey The full path without any sequence info
238244
* @param int|null $indexKey The index to compare
@@ -249,12 +255,11 @@ private function isAnyLock(string $fullKey, int $indexKey = null): bool
249255

250256
$children = $this->getProvider()->getChildren($parent);
251257

252-
foreach ($children as $childKey) {
253-
254-
if (is_null($indexKey)) {
255-
return true;
256-
}
258+
if (is_null($indexKey) && !empty($children)) {
259+
return true;
260+
}
257261

262+
foreach ($children as $childKey) {
258263
$childIndex = $this->getIndex($childKey);
259264

260265
if (is_null($childIndex)) {

lib/internal/Magento/Framework/Lock/Test/Unit/Backend/ZookeeperTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,21 @@ protected function setUp()
4343
* @expectedExceptionMessage The path needs to be a non-empty string.
4444
* @return void
4545
*/
46-
public function testConstructionWithException()
46+
public function testConstructionWithPathException()
4747
{
4848
$this->zookeeperProvider = new ZookeeperProvider($this->host, '');
4949
}
5050

51+
/**
52+
* @expectedException \Magento\Framework\Exception\RuntimeException
53+
* @expectedExceptionMessage The host needs to be a non-empty string.
54+
* @return void
55+
*/
56+
public function testConstructionWithHostException()
57+
{
58+
$this->zookeeperProvider = new ZookeeperProvider('', $this->path);
59+
}
60+
5161
/**
5262
* @return void
5363
*/

setup/src/Magento/Setup/Model/ConfigOptionsList/Lock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ private function validateZookeeperConfig(array $options, DeploymentConfig $deplo
241241
*/
242242
private function getLockProvider(array $options, DeploymentConfig $deploymentConfig): string
243243
{
244-
if (empty($options[self::INPUT_KEY_LOCK_PROVIDER])) {
244+
if (!isset($options[self::INPUT_KEY_LOCK_PROVIDER])) {
245245
return (string) $deploymentConfig->get(
246246
self::CONFIG_PATH_LOCK_PROVIDER,
247247
$this->getDefaultValue(self::INPUT_KEY_LOCK_PROVIDER)

0 commit comments

Comments
 (0)