-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
QueryStateTimer.java
285 lines (234 loc) · 7.77 KB
/
QueryStateTimer.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.execution;
import com.google.common.base.Ticker;
import io.airlift.units.Duration;
import org.joda.time.DateTime;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import static io.airlift.units.Duration.succinctNanos;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
class QueryStateTimer
{
private final Ticker ticker;
private final DateTime createTime = DateTime.now();
private final long createNanos;
private final AtomicReference<Long> beginResourceWaitingNanos = new AtomicReference<>();
private final AtomicReference<Long> beginPlanningNanos = new AtomicReference<>();
private final AtomicReference<Long> beginFinishingNanos = new AtomicReference<>();
private final AtomicReference<Long> endNanos = new AtomicReference<>();
private final AtomicReference<Duration> queuedTime = new AtomicReference<>();
private final AtomicReference<Duration> resourceWaitingTime = new AtomicReference<>();
private final AtomicReference<Duration> executionTime = new AtomicReference<>();
private final AtomicReference<Duration> planningTime = new AtomicReference<>();
private final AtomicReference<Duration> finishingTime = new AtomicReference<>();
private final AtomicReference<Long> beginAnalysisNanos = new AtomicReference<>();
private final AtomicReference<Duration> analysisTime = new AtomicReference<>();
private final AtomicReference<Long> beginDistributedPlanningNanos = new AtomicReference<>();
private final AtomicReference<Duration> distributedPlanningTime = new AtomicReference<>();
private final AtomicReference<Long> lastHeartbeatNanos;
public QueryStateTimer(Ticker ticker)
{
this.ticker = requireNonNull(ticker, "ticker is null");
this.createNanos = tickerNanos();
this.lastHeartbeatNanos = new AtomicReference<>(createNanos);
}
//
// State transitions
//
public void beginWaitingForResources()
{
beginWaitingForResources(tickerNanos());
}
private void beginWaitingForResources(long now)
{
queuedTime.compareAndSet(null, nanosSince(createNanos, now));
beginResourceWaitingNanos.compareAndSet(null, now);
}
public void beginPlanning()
{
beginPlanning(tickerNanos());
}
private void beginPlanning(long now)
{
beginWaitingForResources(now);
resourceWaitingTime.compareAndSet(null, nanosSince(beginResourceWaitingNanos, now));
beginPlanningNanos.compareAndSet(null, now);
}
public void beginStarting()
{
beginStarting(tickerNanos());
}
private void beginStarting(long now)
{
beginPlanning(now);
planningTime.compareAndSet(null, nanosSince(beginPlanningNanos, now));
}
public void beginRunning()
{
beginRunning(tickerNanos());
}
private void beginRunning(long now)
{
beginStarting(now);
}
public void beginFinishing()
{
beginFinishing(tickerNanos());
}
private void beginFinishing(long now)
{
beginRunning(now);
beginFinishingNanos.compareAndSet(null, now);
}
public void endQuery()
{
endQuery(tickerNanos());
}
private void endQuery(long now)
{
beginFinishing(now);
finishingTime.compareAndSet(null, nanosSince(beginFinishingNanos, now));
executionTime.compareAndSet(null, nanosSince(beginPlanningNanos, now));
endNanos.compareAndSet(null, now);
}
//
// Additional timings
//
public void beginAnalyzing()
{
beginAnalysisNanos.compareAndSet(null, tickerNanos());
}
public void endAnalysis()
{
analysisTime.compareAndSet(null, nanosSince(beginAnalysisNanos, tickerNanos()));
}
public void beginDistributedPlanning()
{
beginDistributedPlanningNanos.compareAndSet(null, tickerNanos());
}
public void endDistributedPlanning()
{
distributedPlanningTime.compareAndSet(null, nanosSince(beginDistributedPlanningNanos, tickerNanos()));
}
public void recordHeartbeat()
{
lastHeartbeatNanos.set(tickerNanos());
}
//
// Stats
//
public DateTime getCreateTime()
{
return createTime;
}
public Optional<DateTime> getExecutionStartTime()
{
return toDateTime(beginPlanningNanos);
}
public Duration getElapsedTime()
{
if (endNanos.get() != null) {
return succinctNanos(endNanos.get() - createNanos);
}
return nanosSince(createNanos, tickerNanos());
}
public Duration getQueuedTime()
{
Duration queuedTime = this.queuedTime.get();
if (queuedTime != null) {
return queuedTime;
}
// if queue time is not set, the query is still queued
return getElapsedTime();
}
public Duration getResourceWaitingTime()
{
return getDuration(resourceWaitingTime, beginResourceWaitingNanos);
}
public Duration getPlanningTime()
{
return getDuration(planningTime, beginPlanningNanos);
}
public Duration getFinishingTime()
{
return getDuration(finishingTime, beginFinishingNanos);
}
public Duration getExecutionTime()
{
return getDuration(executionTime, beginPlanningNanos);
}
public Optional<DateTime> getEndTime()
{
return toDateTime(endNanos);
}
public Duration getAnalysisTime()
{
return getDuration(analysisTime, beginAnalysisNanos);
}
public Duration getDistributedPlanningTime()
{
return getDuration(distributedPlanningTime, beginDistributedPlanningNanos);
}
public DateTime getLastHeartbeat()
{
return toDateTime(lastHeartbeatNanos.get());
}
//
// Helper methods
//
private long tickerNanos()
{
return ticker.read();
}
private static Duration nanosSince(AtomicReference<Long> start, long end)
{
Long startNanos = start.get();
if (startNanos == null) {
throw new IllegalStateException("Start time not set");
}
return nanosSince(startNanos, end);
}
private static Duration nanosSince(long start, long now)
{
return succinctNanos(now - start);
}
private Duration getDuration(AtomicReference<Duration> finalDuration, AtomicReference<Long> start)
{
Duration duration = finalDuration.get();
if (duration != null) {
return duration;
}
Long startNanos = start.get();
if (startNanos != null) {
return nanosSince(startNanos, tickerNanos());
}
return new Duration(0, MILLISECONDS);
}
private Optional<DateTime> toDateTime(AtomicReference<Long> instantNanos)
{
Long nanos = instantNanos.get();
if (nanos == null) {
return Optional.empty();
}
return Optional.of(toDateTime(nanos));
}
private DateTime toDateTime(long instantNanos)
{
long millisSinceCreate = NANOSECONDS.toMillis(instantNanos - createNanos);
return new DateTime(createTime.getMillis() + millisSinceCreate);
}
}