Skip to content

Commit 251ce3c

Browse files
committed
MAGETWO-98151: Add support ZooKeeper locks
1 parent 022827d commit 251ce3c

File tree

5 files changed

+36
-22
lines changed

5 files changed

+36
-22
lines changed

dev/tests/integration/testsuite/Magento/Framework/Lock/Backend/ZookeeperTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,3 @@ public function testUnlockWithoutExistingLock()
8888
$this->assertFalse($this->model->unlock($name));
8989
}
9090
}
91-

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ public function __construct(string $host, string $path = self::DEFAULT_PATH)
101101
}
102102

103103
/**
104-
* {@inheritdoc}
104+
* @inheritdoc
105+
*
105106
* You can see the lock algorithm by the link
106107
* @link https://zookeeper.apache.org/doc/r3.1.2/recipes.html#sc_recipes_Locks
107108
*
@@ -124,7 +125,7 @@ public function lock(string $name, int $timeout = -1): bool
124125
throw new RuntimeException(new Phrase('Failed creating lock %1', [$lockPath]));
125126
}
126127

127-
while($this->isAnyLock($lockKey, $this->getIndex($lockKey))) {
128+
while ($this->isAnyLock($lockKey, $this->getIndex($lockKey))) {
128129
if (!$skipDeadline && $deadline <= microtime(true)) {
129130
$this->getProvider()->delete($lockKey);
130131
return false;
@@ -139,7 +140,8 @@ public function lock(string $name, int $timeout = -1): bool
139140
}
140141

141142
/**
142-
* {@inheritdoc}
143+
* @inheritdoc
144+
*
143145
* @throws RuntimeException
144146
*/
145147
public function unlock(string $name): bool
@@ -152,7 +154,8 @@ public function unlock(string $name): bool
152154
}
153155

154156
/**
155-
* {@inheritdoc}
157+
* @inheritdoc
158+
*
156159
* @throws RuntimeException
157160
*/
158161
public function isLocked(string $name): bool
@@ -184,7 +187,7 @@ private function getProvider(): \Zookeeper
184187
}
185188

186189
$deadline = microtime(true) + $this->connectionTimeout;
187-
while($this->zookeeper->getState() != \Zookeeper::CONNECTED_STATE) {
190+
while ($this->zookeeper->getState() != \Zookeeper::CONNECTED_STATE) {
188191
if ($deadline <= microtime(true)) {
189192
throw new RuntimeException(new Phrase('Zookeeper connection timed out!'));
190193
}
@@ -227,16 +230,17 @@ private function checkAndCreateParentNode(string $path): bool
227230
*/
228231
private function getIndex(string $key)
229232
{
230-
if (!preg_match('/' . $this->lockName . '([0-9]+)$/', $key, $matches))
233+
if (!preg_match('/' . $this->lockName . '([0-9]+)$/', $key, $matches)) {
231234
return null;
235+
}
232236

233237
return intval($matches[1]);
234238
}
235239

236240
/**
237241
* Checks if there is any sequence node under parent of $fullKey.
238-
* At first checks that the $fullKey node is present, if not - returns false.
239242
*
243+
* At first checks that the $fullKey node is present, if not - returns false.
240244
* If $indexKey is non-null and there is a smaller index than $indexKey then returns true,
241245
* otherwise returns false.
242246
*
@@ -255,14 +259,14 @@ private function isAnyLock(string $fullKey, int $indexKey = null): bool
255259

256260
$children = $this->getProvider()->getChildren($parent);
257261

258-
if (is_null($indexKey) && !empty($children)) {
262+
if (null === $indexKey && !empty($children)) {
259263
return true;
260264
}
261265

262266
foreach ($children as $childKey) {
263267
$childIndex = $this->getIndex($childKey);
264268

265-
if (is_null($childIndex)) {
269+
if (null === $childIndex) {
266270
continue;
267271
}
268272

lib/internal/Magento/Framework/Lock/Proxy.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public function __construct(LockBackendFactory $factory)
3737
}
3838

3939
/**
40-
* {@inheritdoc}
40+
* @inheritdoc
41+
*
4142
* @throws RuntimeException
4243
*/
4344
public function isLocked(string $name): bool
@@ -46,7 +47,8 @@ public function isLocked(string $name): bool
4647
}
4748

4849
/**
49-
* {@inheritdoc}
50+
* @inheritdoc
51+
*
5052
* @throws RuntimeException
5153
*/
5254
public function lock(string $name, int $timeout = -1): bool
@@ -55,7 +57,8 @@ public function lock(string $name, int $timeout = -1): bool
5557
}
5658

5759
/**
58-
* {@inheritdoc}
60+
* @inheritdoc
61+
*
5962
* @throws RuntimeException
6063
*/
6164
public function unlock(string $name): bool

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,9 @@ private function validateZookeeperConfig(array $options, DeploymentConfig $deplo
212212

213213
$host = $options[self::INPUT_KEY_LOCK_ZOOKEEPER_HOST]
214214
?? $deploymentConfig->get(
215-
self::CONFIG_PATH_LOCK_ZOOKEEPER_HOST,
216-
$this->getDefaultValue(self::INPUT_KEY_LOCK_ZOOKEEPER_HOST)
217-
);
215+
self::CONFIG_PATH_LOCK_ZOOKEEPER_HOST,
216+
$this->getDefaultValue(self::INPUT_KEY_LOCK_ZOOKEEPER_HOST)
217+
);
218218
$path = $options[self::INPUT_KEY_LOCK_ZOOKEEPER_PATH]
219219
?? $deploymentConfig->get(
220220
self::CONFIG_PATH_LOCK_ZOOKEEPER_PATH,
@@ -251,7 +251,6 @@ private function getLockProvider(array $options, DeploymentConfig $deploymentCon
251251
return (string) $options[self::INPUT_KEY_LOCK_PROVIDER];
252252
}
253253

254-
255254
/**
256255
* Sets default configuration for locks
257256
*

setup/src/Magento/Setup/Test/Unit/Model/ConfigOptionsList/LockTest.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ public function testValidate(array $options, array $expectedResult)
163163
$this->deploymentConfigMock->expects($this->any())
164164
->method('get')
165165
->willReturnArgument(1);
166-
$this->assertSame($expectedResult, $this->lockConfigOptionsList->validate($options, $this->deploymentConfigMock));
166+
$this->assertSame(
167+
$expectedResult,
168+
$this->lockConfigOptionsList->validate($options, $this->deploymentConfigMock)
169+
);
167170
}
168171

169172
/**
@@ -186,10 +189,16 @@ public function validateDataProvider(): array
186189
LockConfigOptionsList::INPUT_KEY_LOCK_ZOOKEEPER_HOST => '',
187190
LockConfigOptionsList::INPUT_KEY_LOCK_ZOOKEEPER_PATH => '',
188191
],
189-
'expectedResult' => [
190-
'Zookeeper path needs to be a non-empty string.',
191-
'Zookeeper host is should be set.',
192-
],
192+
'expectedResult' => extension_loaded('zookeeper')
193+
? [
194+
'Zookeeper path needs to be a non-empty string.',
195+
'Zookeeper host is should be set.',
196+
]
197+
: [
198+
'php extension Zookeeper is not installed.',
199+
'Zookeeper path needs to be a non-empty string.',
200+
'Zookeeper host is should be set.',
201+
],
193202
],
194203
];
195204
}

0 commit comments

Comments
 (0)