Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1061 from zalando/ARUHA-2306-3
Browse files Browse the repository at this point in the history
constant timeout on lock retry
  • Loading branch information
adyach authored Jun 12, 2019
2 parents b5df168 + f251c77 commit 77add95
Showing 1 changed file with 5 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ZookeeperLock {

private static final Logger LOG = LoggerFactory.getLogger(ZookeeperLock.class);
private static final String NAKADI_LOCKS = "/nakadi/locks";
private static final int RETRY_DELAY_MS = 250;
private static final int RETRY_DELAY_MS = 30;
private static final int RETRY_MAX_COUNT = 10;
private final ZooKeeper zookeeper;

Expand All @@ -34,10 +34,8 @@ public ZookeeperLock(final ZooKeeper zookeeper) {

public Closeable tryLock(final String lockNode, final long timeoutMs) throws RuntimeException {
final String lockPath = NAKADI_LOCKS + "/" + lockNode;
int requestCounter = 0;
final long finishAt = System.currentTimeMillis() + timeoutMs;
while (finishAt > System.currentTimeMillis()) {
requestCounter++;
try {
zookeeper.create(lockPath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
return () -> lockRelease(lockPath);
Expand All @@ -53,7 +51,7 @@ public Closeable tryLock(final String lockNode, final long timeoutMs) throws Run
LOG.warn("Failed to acquire lock for {}, retrying", lockPath, e);
}

retryDelay(requestCounter);
retryDelay();
}

close();
Expand All @@ -80,7 +78,7 @@ private void lockRelease(final String lockPath) throws RuntimeException {
LOG.warn("Failed to release lock for {}, retrying", lockPath, e);
}

retryDelay(requestCounter);
retryDelay();
}
LOG.error("Failed to release lock for {}", lockPath);
close();
Expand All @@ -96,9 +94,9 @@ private void close() throws NakadiRuntimeException {
}
}

private void retryDelay(final int requestCounter) {
private void retryDelay() {
try {
Thread.sleep(requestCounter * RETRY_DELAY_MS);
Thread.sleep(RETRY_DELAY_MS);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new RuntimeException("Interrupted while sleeping in retry", ie);
Expand Down

0 comments on commit 77add95

Please sign in to comment.