/
QueryResource.java
255 lines (230 loc) · 8.21 KB
/
QueryResource.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
/*
* 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 io.prestosql.server;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import io.airlift.units.DataSize;
import io.airlift.units.Duration;
import io.prestosql.dispatcher.DispatchManager;
import io.prestosql.dispatcher.DispatchQuery;
import io.prestosql.execution.QueryInfo;
import io.prestosql.execution.QueryManager;
import io.prestosql.execution.QueryState;
import io.prestosql.execution.QueryStats;
import io.prestosql.execution.StageId;
import io.prestosql.spi.PrestoException;
import io.prestosql.spi.QueryId;
import javax.inject.Inject;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import java.util.List;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import static com.google.common.base.Preconditions.checkArgument;
import static io.prestosql.connector.system.KillQueryProcedure.createKillQueryException;
import static io.prestosql.connector.system.KillQueryProcedure.createPreemptQueryException;
import static java.util.Objects.requireNonNull;
/**
* Manage queries scheduled on this node
*/
@Path("/v1/query")
public class QueryResource
{
private static final DataSize ZERO_BYTES = new DataSize(0, DataSize.Unit.BYTE);
private static final Duration ZERO_MILLIS = new Duration(0, TimeUnit.MILLISECONDS);
// TODO There should be a combined interface for this
private final DispatchManager dispatchManager;
private final QueryManager queryManager;
@Inject
public QueryResource(DispatchManager dispatchManager, QueryManager queryManager)
{
this.dispatchManager = requireNonNull(dispatchManager, "dispatchManager is null");
this.queryManager = requireNonNull(queryManager, "queryManager is null");
}
@GET
public List<BasicQueryInfo> getAllQueryInfo(@QueryParam("state") String stateFilter)
{
QueryState expectedState = stateFilter == null ? null : QueryState.valueOf(stateFilter.toUpperCase(Locale.ENGLISH));
ImmutableList.Builder<BasicQueryInfo> builder = new ImmutableList.Builder<>();
for (BasicQueryInfo queryInfo : dispatchManager.getQueries()) {
if (stateFilter == null || queryInfo.getState() == expectedState) {
builder.add(queryInfo);
}
}
return builder.build();
}
@GET
@Path("{queryId}")
public Response getQueryInfo(@PathParam("queryId") QueryId queryId)
{
requireNonNull(queryId, "queryId is null");
try {
QueryInfo queryInfo = queryManager.getFullQueryInfo(queryId);
return Response.ok(queryInfo).build();
}
catch (NoSuchElementException ignored) {
}
try {
DispatchQuery query = dispatchManager.getQuery(queryId);
if (query.isDone()) {
return Response.ok(toFullQueryInfo(query)).build();
}
}
catch (NoSuchElementException ignored) {
}
return Response.status(Status.GONE).build();
}
@DELETE
@Path("{queryId}")
public void cancelQuery(@PathParam("queryId") QueryId queryId)
{
requireNonNull(queryId, "queryId is null");
queryManager.cancelQuery(queryId);
}
@PUT
@Path("{queryId}/killed")
public Response killQuery(@PathParam("queryId") QueryId queryId, String message)
{
return failQuery(queryId, createKillQueryException(message));
}
@PUT
@Path("{queryId}/preempted")
public Response preemptQuery(@PathParam("queryId") QueryId queryId, String message)
{
return failQuery(queryId, createPreemptQueryException(message));
}
private Response failQuery(QueryId queryId, PrestoException queryException)
{
requireNonNull(queryId, "queryId is null");
try {
QueryState state = queryManager.getQueryState(queryId);
// check before killing to provide the proper error code (this is racy)
if (state.isDone()) {
return Response.status(Status.CONFLICT).build();
}
queryManager.failQuery(queryId, queryException);
// verify if the query was failed (if not, we lost the race)
if (!queryException.getErrorCode().equals(queryManager.getQueryInfo(queryId).getErrorCode())) {
return Response.status(Status.CONFLICT).build();
}
return Response.status(Status.OK).build();
}
catch (NoSuchElementException e) {
return Response.status(Status.GONE).build();
}
}
@DELETE
@Path("stage/{stageId}")
public void cancelStage(@PathParam("stageId") StageId stageId)
{
requireNonNull(stageId, "stageId is null");
queryManager.cancelStage(stageId);
}
private static QueryInfo toFullQueryInfo(DispatchQuery query)
{
checkArgument(query.isDone(), "query is not done");
BasicQueryInfo info = query.getBasicQueryInfo();
BasicQueryStats stats = info.getQueryStats();
QueryStats queryStats = new QueryStats(
query.getCreateTime(),
query.getExecutionStartTime().orElse(null),
query.getLastHeartbeat(),
query.getEndTime().orElse(null),
stats.getElapsedTime(),
stats.getQueuedTime(),
ZERO_MILLIS,
ZERO_MILLIS,
ZERO_MILLIS,
ZERO_MILLIS,
ZERO_MILLIS,
ZERO_MILLIS,
ZERO_MILLIS,
0,
0,
0,
0,
0,
0,
0,
0,
0,
ZERO_BYTES,
ZERO_BYTES,
ZERO_BYTES,
ZERO_BYTES,
ZERO_BYTES,
ZERO_BYTES,
ZERO_BYTES,
ZERO_BYTES,
ZERO_BYTES,
info.isScheduled(),
ZERO_MILLIS,
ZERO_MILLIS,
ZERO_MILLIS,
false,
ImmutableSet.of(),
ZERO_BYTES,
0,
ZERO_BYTES,
0,
ZERO_BYTES,
0,
ZERO_BYTES,
0,
ZERO_BYTES,
0,
ZERO_BYTES,
ImmutableList.of(),
ImmutableList.of());
return new QueryInfo(
info.getQueryId(),
info.getSession(),
info.getState(),
info.getMemoryPool(),
info.isScheduled(),
info.getSelf(),
ImmutableList.of(),
info.getQuery(),
info.getPreparedQuery(),
queryStats,
Optional.empty(),
Optional.empty(),
Optional.empty(),
ImmutableMap.of(),
ImmutableSet.of(),
ImmutableMap.of(),
ImmutableMap.of(),
ImmutableSet.of(),
Optional.empty(),
false,
null,
Optional.empty(),
query.getDispatchInfo().getFailureInfo().orElse(null),
info.getErrorCode(),
ImmutableList.of(),
ImmutableSet.of(),
Optional.empty(),
true,
info.getResourceGroupId());
}
}