summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Marius Venstad <venstad@gmail.com>2020-01-16 14:52:06 +0100
committerJon Marius Venstad <venstad@gmail.com>2020-01-16 14:52:06 +0100
commit5dfe8e2421af5d9eaae31d779d1636180799ddef (patch)
tree2c2a2544209055de1216e442de12d8eca379791c
parent19583fee94481927a82cd703dad561e5dd13fe3a (diff)
Hold a JVM-wide reentrant lock to grab mutex — helps ZK stale reads?
-rw-r--r--zkfacade/src/main/java/com/yahoo/vespa/curator/Lock.java10
1 files changed, 9 insertions, 1 deletions
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 + "'");