INT-4127: Fix ZkLock.tryLock() timing issue

JIRA: https://jira.spring.io/browse/INT-4127
Fixes GH-1913 (https://github.com/spring-projects/spring-integration/issues/1913)

The current `tryLock()` implementation with the `0` timeout to wait for underlying `InterProcessMutex` answer makes this function almost fully impossible

* Fix `ZkLock.tryLock()` to delegate with the `tryLock(1, TimeUnit.SECONDS)` to give some time window chance for network communication.
It isn't ideal for the `tryLock()` contract, but at least it is better then don't let the feature to work at all.

**Cherry-pick to 4.3.x & 4.2.x**
This commit is contained in:
Artem Bilan
2016-09-28 18:12:26 -04:00
committed by Gary Russell
parent 86a079c1cc
commit a7171c4db9
2 changed files with 13 additions and 1 deletions

View File

@@ -271,7 +271,7 @@ public class ZookeeperLockRegistry implements ExpirableLockRegistry, DisposableB
@Override
public boolean tryLock() {
try {
return tryLock(0, TimeUnit.MICROSECONDS);
return tryLock(1, TimeUnit.SECONDS);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();

View File

@@ -352,4 +352,16 @@ public class ZkLockRegistryTests extends ZookeeperTestSupport {
registry.destroy();
}
@Test
public void testTryLock() throws Exception {
ZookeeperLockRegistry registry = new ZookeeperLockRegistry(this.client);
for (int i = 0; i < 10; i++) {
Lock lock = registry.obtain("foo");
assertTrue(lock.tryLock());
lock.unlock();
}
registry.destroy();
}
}