From f251c7752c631798691d284d376e2fb1571b5cbb Mon Sep 17 00:00:00 2001 From: Andrey Dyachkov Date: Wed, 12 Jun 2019 09:41:05 +0200 Subject: [PATCH] constant timeout on lock retry --- .../nakadi/repository/zookeeper/ZookeeperLock.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/zalando/nakadi/repository/zookeeper/ZookeeperLock.java b/src/main/java/org/zalando/nakadi/repository/zookeeper/ZookeeperLock.java index f21b2d81a2..b507e32794 100644 --- a/src/main/java/org/zalando/nakadi/repository/zookeeper/ZookeeperLock.java +++ b/src/main/java/org/zalando/nakadi/repository/zookeeper/ZookeeperLock.java @@ -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; @@ -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); @@ -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(); @@ -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(); @@ -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);