summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Marius Venstad <venstad@gmail.com>2020-01-16 15:08:59 +0100
committerJon Marius Venstad <venstad@gmail.com>2020-01-16 15:08:59 +0100
commit9775572038f4d8c883a94e1fee1b957b37d0e2f5 (patch)
tree93abbdd895bb5c96f8ee0337e303587b5930a8ac
parent5dfe8e2421af5d9eaae31d779d1636180799ddef (diff)
Swap order of locks, to avoid doubling timeout duration
-rw-r--r--zkfacade/src/main/java/com/yahoo/vespa/curator/Lock.java20
1 files changed, 14 insertions, 6 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 404c8be3326..9a3c7095bbe 100644
--- a/zkfacade/src/main/java/com/yahoo/vespa/curator/Lock.java
+++ b/zkfacade/src/main/java/com/yahoo/vespa/curator/Lock.java
@@ -28,18 +28,18 @@ public class Lock implements Mutex {
/** Take the lock with the given timeout. This may be called multiple times from the same thread - each matched by a close */
public void acquire(Duration timeout) throws UncheckedTimeoutException {
- boolean acquired;
+ boolean acquired = false;
try {
- lock.tryLock(timeout.toMillis(), TimeUnit.MILLISECONDS);
acquired = mutex.acquire(timeout.toMillis(), TimeUnit.MILLISECONDS);
+ lock.tryLock(); // Should be available to only this thread, while holding the above mutex.
}
catch (Exception e) {
- if (lock.isHeldByCurrentThread()) lock.unlock();
+ if (acquired) release();
throw new RuntimeException("Exception acquiring lock '" + lockPath + "'", e);
}
- if ( ! acquired) {
- if (lock.isHeldByCurrentThread()) lock.unlock();
+ if ( ! lock.isHeldByCurrentThread()) {
+ if (acquired) release();
throw new UncheckedTimeoutException("Timed out after waiting " + timeout +
" to acquire lock '" + lockPath + "'");
}
@@ -48,9 +48,17 @@ public class Lock implements Mutex {
@Override
public void close() {
try {
- mutex.release();
lock.unlock();
}
+ finally {
+ release();
+ }
+ }
+
+ private void release() {
+ try {
+ mutex.release();
+ }
catch (Exception e) {
throw new RuntimeException("Exception releasing lock '" + lockPath + "'");
}