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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.curator.stats;
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;
/**
* Information about a lock.
*
* <p>Should be mutated by a single thread, except {@link #fillStackTrace()} which can be
* invoked by any threads. Other threads may see an inconsistent state of this instance.</p>
*
* @author hakon
*/
public class LockInfo {
private final ThreadLockInfo threadLockInfo;
private final String lockPath;
private final Instant callAcquireInstant;
private final Duration timeout;
private volatile Optional<Instant> lockAcquiredInstant = Optional.empty();
private volatile Optional<Instant> terminalStateInstant = Optional.empty();
private volatile Optional<String> stackTrace = Optional.empty();
public static LockInfo invokingAcquire(ThreadLockInfo threadLockInfo, String lockPath, Duration timeout) {
return new LockInfo(threadLockInfo, lockPath, timeout, Instant.now());
}
public enum LockState {
ACQUIRING(false), ACQUIRE_FAILED(true), TIMED_OUT(true), ACQUIRED(false), RELEASED(true);
private final boolean terminal;
LockState(boolean terminal) { this.terminal = terminal; }
public boolean isTerminal() { return terminal; }
}
private volatile LockState lockState = LockState.ACQUIRING;
private LockInfo(ThreadLockInfo threadLockInfo, String lockPath, Duration timeout, Instant callAcquireInstant) {
this.threadLockInfo = threadLockInfo;
this.lockPath = lockPath;
this.callAcquireInstant = callAcquireInstant;
this.timeout = timeout;
}
public String getThreadName() { return threadLockInfo.getThreadName(); }
public String getLockPath() { return lockPath; }
public Instant getTimeAcquiredWasInvoked() { return callAcquireInstant; }
public Duration getAcquireTimeout() { return timeout; }
public LockState getLockState() { return lockState; }
public Optional<Instant> getTimeLockWasAcquired() { return lockAcquiredInstant; }
public Optional<Instant> getTimeTerminalStateWasReached() { return terminalStateInstant; }
public Optional<String> getStackTrace() { return stackTrace; }
public Duration getDurationOfAcquire() {
return Duration.between(callAcquireInstant, lockAcquiredInstant.orElseGet(Instant::now));
}
public Duration getDurationWithLock() {
return lockAcquiredInstant
.map(start -> Duration.between(start, terminalStateInstant.orElseGet(Instant::now)))
.orElse(Duration.ZERO);
}
public Duration getDuration() { return Duration.between(callAcquireInstant, terminalStateInstant.orElseGet(Instant::now)); }
/** Get time from just before trying to acquire lock to the time the terminal state was reached, or ZERO. */
public Duration getStableTotalDuration() {
return terminalStateInstant.map(instant -> Duration.between(callAcquireInstant, instant)).orElse(Duration.ZERO);
}
/** Fill in the stack trace starting at the caller's stack frame. */
public void fillStackTrace() {
// This method is public. If invoked concurrently, the this.stackTrace may be updated twice,
// which is fine.
this.stackTrace = Optional.of(threadLockInfo.getStackTrace());
}
void acquireFailed() { setTerminalState(LockState.ACQUIRE_FAILED); }
void timedOut() { setTerminalState(LockState.TIMED_OUT); }
void released() { setTerminalState(LockState.RELEASED); }
void lockAcquired() {
lockState = LockState.ACQUIRED;
lockAcquiredInstant = Optional.of(Instant.now());
}
void setTerminalState(LockState terminalState) { setTerminalState(terminalState, Instant.now()); }
void setTerminalState(LockState terminalState, Instant instant) {
lockState = terminalState;
terminalStateInstant = Optional.of(instant);
}
}
|