From 5dfe8e2421af5d9eaae31d779d1636180799ddef Mon Sep 17 00:00:00 2001 From: Jon Marius Venstad Date: Thu, 16 Jan 2020 14:52:06 +0100 Subject: Hold a JVM-wide reentrant lock to grab mutex — helps ZK stale reads? MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zkfacade/src/main/java/com/yahoo/vespa/curator/Lock.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/zkfacade/src/main/java/com/yahoo/vespa/curator/Lock.java b/zkfacade/src/main/java/com/yahoo/vespa/curator/Lock.java index 30af89d0ea8..404c8be3326 100644 --- a/zkfacade/src/main/java/com/yahoo/vespa/curator/Lock.java +++ b/zkfacade/src/main/java/com/yahoo/vespa/curator/Lock.java @@ -7,6 +7,7 @@ import org.apache.curator.framework.recipes.locks.InterProcessLock; import java.time.Duration; import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantLock; /** * A cluster-wide re-entrant mutex which is released on (the last symmetric) close @@ -15,11 +16,13 @@ import java.util.concurrent.TimeUnit; */ public class Lock implements Mutex { + private final ReentrantLock lock; private final InterProcessLock mutex; private final String lockPath; public Lock(String lockPath, Curator curator) { this.lockPath = lockPath; + this.lock = new ReentrantLock(true); mutex = curator.createMutex(lockPath); } @@ -27,21 +30,26 @@ public class Lock implements Mutex { public void acquire(Duration timeout) throws UncheckedTimeoutException { boolean acquired; try { + lock.tryLock(timeout.toMillis(), TimeUnit.MILLISECONDS); acquired = mutex.acquire(timeout.toMillis(), TimeUnit.MILLISECONDS); } catch (Exception e) { + if (lock.isHeldByCurrentThread()) lock.unlock(); throw new RuntimeException("Exception acquiring lock '" + lockPath + "'", e); } - if (! acquired) + if ( ! acquired) { + if (lock.isHeldByCurrentThread()) lock.unlock(); throw new UncheckedTimeoutException("Timed out after waiting " + timeout + " to acquire lock '" + lockPath + "'"); + } } @Override public void close() { try { mutex.release(); + lock.unlock(); } catch (Exception e) { throw new RuntimeException("Exception releasing lock '" + lockPath + "'"); -- cgit v1.2.3