blob: 9634d2220c0acc5ffbf6cc0825806ab8a8a0dbbe (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.curator;
import com.yahoo.concurrent.UncheckedTimeoutException;
import com.yahoo.path.Path;
import com.yahoo.transaction.Mutex;
import com.yahoo.vespa.curator.stats.LockStats;
import com.yahoo.vespa.curator.stats.ThreadLockStats;
import org.apache.curator.framework.recipes.locks.InterProcessLock;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
/**
* A cluster-wide re-entrant mutex which is released on (the last symmetric) close.
*
* Re-entrancy is limited to the instance of this. To ensure re-entrancy callers should access the lock through
* {@link Curator#lock(Path, Duration)} instead of constructing this directly.
*
* @author bratseth
*/
public class Lock implements Mutex {
private final InterProcessLock mutex;
private final String lockPath;
public Lock(String lockPath, Curator curator) {
this(lockPath, curator.createMutex(lockPath));
}
/** Public for testing only */
public Lock(String lockPath, InterProcessLock mutex) {
this.lockPath = lockPath;
this.mutex = 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 {
ThreadLockStats threadLockStats = LockStats.getForCurrentThread();
threadLockStats.invokingAcquire(lockPath, timeout);
final boolean acquired;
try {
acquired = mutex.acquire(timeout.toMillis(), TimeUnit.MILLISECONDS);
} catch (Exception e) {
threadLockStats.acquireFailed();
throw new RuntimeException("Exception acquiring lock '" + lockPath + "'", e);
}
if (!acquired) {
threadLockStats.acquireTimedOut();
throw new UncheckedTimeoutException("Timed out after waiting " + timeout +
" to acquire lock '" + lockPath + "'");
}
threadLockStats.lockAcquired();
}
@Override
public void close() {
ThreadLockStats threadLockStats = LockStats.getForCurrentThread();
// Update metrics now before release() to avoid double-counting time in locked state.
// The lockPath must be sent down as close() may be invoked in an order not necessarily
// equal to the reverse order of acquires.
threadLockStats.preRelease(lockPath);
try {
mutex.release();
threadLockStats.postRelease(lockPath);
}
catch (Exception e) {
threadLockStats.releaseFailed(lockPath);
throw new RuntimeException("Exception releasing lock '" + lockPath + "'", e);
}
}
protected String lockPath() { return lockPath; }
@Override
public String toString() {
return "Lock{" + lockPath + "}";
}
}
|