This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 186
/
AsyncRestExecutor.java
174 lines (152 loc) Β· 7.4 KB
/
AsyncRestExecutor.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
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.amazon.opendistroforelasticsearch.sql.legacy.executor;
import com.amazon.opendistroforelasticsearch.sql.legacy.esdomain.LocalClusterState;
import com.amazon.opendistroforelasticsearch.sql.legacy.exception.SqlParseException;
import com.amazon.opendistroforelasticsearch.sql.legacy.metrics.MetricName;
import com.amazon.opendistroforelasticsearch.sql.legacy.metrics.Metrics;
import com.amazon.opendistroforelasticsearch.sql.legacy.query.QueryAction;
import com.amazon.opendistroforelasticsearch.sql.legacy.query.join.BackOffRetryStrategy;
import com.amazon.opendistroforelasticsearch.sql.legacy.utils.LogUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.Transports;
import java.io.IOException;
import java.time.Duration;
import java.util.Map;
import java.util.function.Predicate;
import static com.amazon.opendistroforelasticsearch.sql.legacy.plugin.SqlSettings.QUERY_SLOWLOG;
/**
* A RestExecutor wrapper to execute request asynchronously to avoid blocking transport thread.
*/
public class AsyncRestExecutor implements RestExecutor {
/**
* Custom thread pool name managed by ES
*/
public static final String SQL_WORKER_THREAD_POOL_NAME = "sql-worker";
private static final Logger LOG = LogManager.getLogger(AsyncRestExecutor.class);
/**
* Treat all actions as blocking which means async all actions,
* ex. execute() in csv executor or pretty format executor
*/
private static final Predicate<QueryAction> ALL_ACTION_IS_BLOCKING = anyAction -> true;
/**
* Delegated rest executor to async
*/
private final RestExecutor executor;
/**
* Request type that expect to async to avoid blocking
*/
private final Predicate<QueryAction> isBlocking;
AsyncRestExecutor(RestExecutor executor) {
this(executor, ALL_ACTION_IS_BLOCKING);
}
AsyncRestExecutor(RestExecutor executor, Predicate<QueryAction> isBlocking) {
this.executor = executor;
this.isBlocking = isBlocking;
}
@Override
public void execute(Client client, Map<String, String> params, QueryAction queryAction, RestChannel channel)
throws Exception {
if (isBlockingAction(queryAction) && isRunningInTransportThread()) {
if (LOG.isDebugEnabled()) {
LOG.debug("[{}] Async blocking query action [{}] for executor [{}] in current thread [{}]",
LogUtils.getRequestId(), name(executor), name(queryAction), Thread.currentThread().getName());
}
async(client, params, queryAction, channel);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("[{}] Continue running query action [{}] for executor [{}] in current thread [{}]",
LogUtils.getRequestId(), name(executor), name(queryAction), Thread.currentThread().getName());
}
doExecuteWithTimeMeasured(client, params, queryAction, channel);
}
}
@Override
public String execute(Client client, Map<String, String> params, QueryAction queryAction) throws Exception {
// Result is always required and no easy way to async it here.
return executor.execute(client, params, queryAction);
}
private boolean isBlockingAction(QueryAction queryAction) {
return isBlocking.test(queryAction);
}
private boolean isRunningInTransportThread() {
return Transports.isTransportThread(Thread.currentThread());
}
/**
* Run given task in thread pool asynchronously
*/
private void async(Client client, Map<String, String> params, QueryAction queryAction, RestChannel channel) {
ThreadPool threadPool = client.threadPool();
Runnable runnable = () -> {
try {
doExecuteWithTimeMeasured(client, params, queryAction, channel);
} catch (IOException | SqlParseException | ElasticsearchException e) {
Metrics.getInstance().getNumericalMetric(MetricName.FAILED_REQ_COUNT_SYS).increment();
LOG.warn("[{}] [MCB] async task got an IO/SQL exception: {}", LogUtils.getRequestId(),
e.getMessage());
channel.sendResponse(new BytesRestResponse(RestStatus.INTERNAL_SERVER_ERROR, e.getMessage()));
} catch (IllegalStateException e) {
Metrics.getInstance().getNumericalMetric(MetricName.FAILED_REQ_COUNT_SYS).increment();
LOG.warn("[{}] [MCB] async task got a runtime exception: {}", LogUtils.getRequestId(),
e.getMessage());
channel.sendResponse(new BytesRestResponse(RestStatus.INSUFFICIENT_STORAGE,
"Memory circuit is broken."));
} catch (Throwable t) {
Metrics.getInstance().getNumericalMetric(MetricName.FAILED_REQ_COUNT_SYS).increment();
LOG.warn("[{}] [MCB] async task got an unknown throwable: {}", LogUtils.getRequestId(),
t.getMessage());
channel.sendResponse(new BytesRestResponse(RestStatus.INTERNAL_SERVER_ERROR,
String.valueOf(t.getMessage())));
} finally {
BackOffRetryStrategy.releaseMem(executor);
}
};
// Preserve context of calling thread to ensure headers of requests are forwarded when running blocking actions
threadPool.schedule(
LogUtils.withCurrentContext(runnable),
new TimeValue(0L),
SQL_WORKER_THREAD_POOL_NAME
);
}
/**
* Time the real execution of Executor and log slow query for troubleshooting
*/
private void doExecuteWithTimeMeasured(Client client,
Map<String, String> params,
QueryAction action,
RestChannel channel) throws Exception {
long startTime = System.nanoTime();
try {
executor.execute(client, params, action, channel);
} finally {
Duration elapsed = Duration.ofNanos(System.nanoTime() - startTime);
int slowLogThreshold = LocalClusterState.state().getSettingValue(QUERY_SLOWLOG);
if (elapsed.getSeconds() >= slowLogThreshold) {
LOG.warn("[{}] Slow query: elapsed={} (ms)", LogUtils.getRequestId(), elapsed.toMillis());
}
}
}
private String name(Object object) {
return object.getClass().getSimpleName();
}
}