-
Notifications
You must be signed in to change notification settings - Fork 73
/
AbstractSearchAction.java
210 lines (190 loc) · 8.43 KB
/
AbstractSearchAction.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
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.ad.rest;
import static org.opensearch.ad.util.RestHandlerUtils.getSourceContext;
import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.core.xcontent.ToXContent.EMPTY_PARAMS;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.action.ActionListener;
import org.opensearch.action.ActionType;
import org.opensearch.action.search.SearchRequest;
import org.opensearch.action.search.SearchResponse;
import org.opensearch.ad.constant.CommonErrorMessages;
import org.opensearch.ad.model.AnomalyDetector;
import org.opensearch.ad.rest.handler.AnomalyDetectorFunction;
import org.opensearch.ad.settings.AnomalyDetectorSettings;
import org.opensearch.ad.settings.EnabledSetting;
import org.opensearch.client.node.NodeClient;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.extensions.rest.ExtensionRestResponse;
import org.opensearch.rest.BytesRestResponse;
import org.opensearch.rest.RestChannel;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.RestStatus;
import org.opensearch.sdk.ExtensionsRunner;
import org.opensearch.sdk.SDKClient.SDKRestClient;
import org.opensearch.sdk.rest.BaseExtensionRestHandler;
import org.opensearch.sdk.rest.ReplacedRouteHandler;
import org.opensearch.search.SearchHit;
import org.opensearch.search.builder.SearchSourceBuilder;
/**
* Abstract class to handle search request.
*/
public abstract class AbstractSearchAction<T extends ToXContentObject> extends BaseExtensionRestHandler {
protected final String index;
protected final Class<T> clazz;
protected final List<String> urlPaths;
protected final List<Pair<String, String>> deprecatedPaths;
protected final ActionType<SearchResponse> actionType;
private ExtensionsRunner extensionsRunner;
private SDKRestClient sdkRestClient;
private final Logger logger = LogManager.getLogger(AbstractSearchAction.class);
public AbstractSearchAction(
List<String> urlPaths,
List<Pair<String, String>> deprecatedPaths,
String index,
Class<T> clazz,
ActionType<SearchResponse> actionType,
SDKRestClient sdkRestClient,
ExtensionsRunner extensionsRunner
) {
this.index = index;
this.clazz = clazz;
this.urlPaths = urlPaths;
this.deprecatedPaths = deprecatedPaths;
this.actionType = actionType;
this.sdkRestClient = sdkRestClient;
this.extensionsRunner = extensionsRunner;
}
private Function<RestRequest, ExtensionRestResponse> handleRequest = (request) -> {
try {
return prepareRequest(request);
} catch (Exception e) {
// TODO: handle the AD-specific exceptions separately
return exceptionalRequest(request, e);
}
};
protected ExtensionRestResponse prepareRequest(RestRequest request) throws IOException {
if (!EnabledSetting.isADPluginEnabled()) {
throw new IllegalStateException(CommonErrorMessages.DISABLED_ERR_MSG);
}
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.parseXContent(request.contentOrSourceParamParser());
searchSourceBuilder.fetchSource(getSourceContext(request));
searchSourceBuilder.seqNoAndPrimaryTerm(true).version(true);
SearchRequest searchRequest = new SearchRequest().source(searchSourceBuilder).indices(this.index);
CompletableFuture<SearchResponse> futureResponse = new CompletableFuture<>();
sdkRestClient
.execute(
actionType,
searchRequest,
ActionListener
.wrap(adSearchResponse -> futureResponse.complete(adSearchResponse), ex -> futureResponse.completeExceptionally(ex))
);
SearchResponse searchResponse = futureResponse
.orTimeout(
AnomalyDetectorSettings.REQUEST_TIMEOUT.get(extensionsRunner.getEnvironmentSettings()).getMillis(),
TimeUnit.MILLISECONDS
)
.join();
return search(request, searchResponse);
}
protected void executeWithAdmin(NodeClient client, AnomalyDetectorFunction function, RestChannel channel) {
try {
function.execute();
} catch (Exception e) {
logger.error("Failed to execute with admin", e);
onFailure(channel, e);
}
}
protected void onFailure(RestChannel channel, Exception e) {
try {
channel.sendResponse(new BytesRestResponse(channel, e));
} catch (Exception exception) {
logger.error("Failed to send back failure response for search AD result", exception);
}
}
protected ExtensionRestResponse search(RestRequest request, SearchResponse response) throws IOException {
// return new RestResponseListener<SearchResponse>(channel) {
// @Override
// public RestResponse buildResponse(SearchResponse response) throws Exception {
if (response.isTimedOut()) {
return new ExtensionRestResponse(request, RestStatus.REQUEST_TIMEOUT, response.toString());
}
if (clazz == AnomalyDetector.class) {
for (SearchHit hit : response.getHits()) {
XContentParser parser = XContentType.JSON
.xContent()
.createParser(
extensionsRunner.getNamedXContentRegistry().getRegistry(),
LoggingDeprecationHandler.INSTANCE,
hit.getSourceAsString()
);
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
// write back id and version to anomaly detector object
ToXContentObject xContentObject = AnomalyDetector.parse(parser, hit.getId(), hit.getVersion());
XContentBuilder builder = xContentObject.toXContent(jsonBuilder(), EMPTY_PARAMS);
hit.sourceRef(BytesReference.bytes(builder));
}
}
return new ExtensionRestResponse(request, RestStatus.OK, response.toXContent(JsonXContent.contentBuilder(), EMPTY_PARAMS));
}
public List<RouteHandler> routeHandlers() {
List<RouteHandler> routes = new ArrayList<>();
for (String path : urlPaths) {
routes.add(new RouteHandler(RestRequest.Method.POST, path, handleRequest));
routes.add(new RouteHandler(RestRequest.Method.GET, path, handleRequest));
}
return routes;
}
@Override
public List<ReplacedRouteHandler> replacedRouteHandlers() {
List<ReplacedRouteHandler> replacedRoutes = new ArrayList<>();
for (Pair<String, String> deprecatedPath : deprecatedPaths) {
replacedRoutes
.add(
new ReplacedRouteHandler(
RestRequest.Method.POST,
deprecatedPath.getKey(),
RestRequest.Method.POST,
deprecatedPath.getValue(),
handleRequest
)
);
replacedRoutes
.add(
new ReplacedRouteHandler(
RestRequest.Method.GET,
deprecatedPath.getKey(),
RestRequest.Method.GET,
deprecatedPath.getValue(),
handleRequest
)
);
}
return replacedRoutes;
}
}