/
TimeoutManager.java
192 lines (163 loc) · 5.32 KB
/
TimeoutManager.java
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.engine.search.timeout.spi;
import java.lang.invoke.MethodHandles;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.hibernate.search.engine.common.timing.spi.TimingSource;
import org.hibernate.search.engine.logging.impl.Log;
import org.hibernate.search.engine.common.timing.spi.Deadline;
import org.hibernate.search.util.common.impl.TimeHelper;
import org.hibernate.search.util.common.SearchTimeoutException;
import org.hibernate.search.util.common.logging.impl.LoggerFactory;
/**
* @author Emmanuel Bernard
*/
public class TimeoutManager {
private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );
public static TimeoutManager of(TimingSource timingSource, Long timeout, TimeUnit timeUnit,
boolean exceptionOnTimeout) {
if ( timeout != null && timeUnit != null ) {
if ( exceptionOnTimeout ) {
return TimeoutManager.hardTimeout( timingSource, timeout, timeUnit );
}
else {
return TimeoutManager.softTimeout( timingSource, timeout, timeUnit );
}
}
return TimeoutManager.noTimeout( timingSource );
}
public static TimeoutManager noTimeout(TimingSource timingSource) {
return new TimeoutManager( timingSource, null, null, Type.NONE );
}
public static TimeoutManager softTimeout(TimingSource timingSource, long timeout, TimeUnit timeUnit) {
return new TimeoutManager( timingSource, timeout, timeUnit, Type.LIMIT );
}
public static TimeoutManager hardTimeout(TimingSource timingSource, long timeout, TimeUnit timeUnit) {
return new TimeoutManager( timingSource, timeout, timeUnit, Type.EXCEPTION );
}
public enum Type {
NONE,
EXCEPTION,
LIMIT
}
protected final TimingSource timingSource;
protected final Long timeoutValue;
protected final TimeUnit timeoutUnit;
protected final Long timeoutMs;
protected final Type type;
private final DynamicDeadline deadline;
private Long monotonicTimeEstimateStart;
private Long nanoTimeStart;
public TimeoutManager(TimingSource timingSource, Long timeoutValue, TimeUnit timeoutUnit, Type type) {
this.timingSource = timingSource;
this.timeoutValue = timeoutValue;
this.timeoutUnit = timeoutUnit;
this.timeoutMs = TimeHelper.toMillisecondsRoundedUp( timeoutValue, timeoutUnit );
this.type = type;
this.deadline = timeoutMs == null ? null : new DynamicDeadline();
timingSource.ensureInitialized();
}
/**
* we start counting from this method call (if needed)
*/
public void start() {
this.monotonicTimeEstimateStart = timingSource.monotonicTimeEstimate();
this.nanoTimeStart = timingSource.nanoTime();
}
public void stop() {
this.monotonicTimeEstimateStart = null;
this.nanoTimeStart = null;
}
public TimingSource timingSource() {
return timingSource;
}
/**
* @return The deadline for the timeout defined by this timeout manager,
* {@code null} if no timeout is set.
*/
public Deadline deadlineOrNull() {
return this.deadline;
}
/**
* @return The hard deadline defined by this timeout manager,
* i.e. the deadline beyond which an exception should be thrown.
* {@code null} if no hard timeout is set.
*/
public Deadline hardDeadlineOrNull() {
if ( !hasHardTimeout() ) {
return null;
}
return this.deadline;
}
public long timeoutBaseline() {
return monotonicTimeEstimateStart;
}
/**
* @return {@code true} if the timeout was reached in a previous call to {@link #checkTimedOut()},
* {@code false} otherwise.
*/
public boolean isTimedOut() {
return deadline != null && deadline.timedOut;
}
/**
* @return {@code true} if the timeout was reached, {@code false} otherwise.
* @throws org.hibernate.search.util.common.SearchTimeoutException If the timeout was reached and
* a hard timeout was requested.
*/
public boolean checkTimedOut() {
if ( deadline == null ) {
return false;
}
return deadline.remainingTimeMillis() <= 0;
}
public boolean hasHardTimeout() {
return this.type == Type.EXCEPTION;
}
/**
* Returns the time passed from the start with high precision.
* This method may be performance expensive.
*
* @return high precision duration of took time.
*/
public Duration tookTime() {
return Duration.ofMillis( timingSource.nanoTime() - nanoTimeStart );
}
protected long elapsedTimeInMilliseconds() {
return timingSource.monotonicTimeEstimate() - monotonicTimeEstimateStart;
}
final class DynamicDeadline implements Deadline {
boolean timedOut = false;
@Override
public long remainingTimeMillis() {
final long elapsedTime = elapsedTimeInMilliseconds();
long timeLeft = timeoutMs - elapsedTime;
if ( timeLeft <= 0 ) {
forceTimeout( null );
// Timed out: don't return a negative number.
return 0L;
}
else {
return timeLeft;
}
}
@Override
public void forceTimeout(Exception cause) {
if ( hasHardTimeout() ) {
throw forceTimeoutAndCreateException( cause );
}
else {
this.timedOut = true;
}
}
@Override
public SearchTimeoutException forceTimeoutAndCreateException(Exception cause) {
this.timedOut = true;
return log.timedOut( Duration.ofNanos( timeoutUnit.toNanos( timeoutValue ) ), cause );
}
}
}