Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Adding RestActions support for AD Search Rest API's #234

Merged
merged 5 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@
import com.amazon.opendistroforelasticsearch.ad.transport.RCFPollingTransportAction;
import com.amazon.opendistroforelasticsearch.ad.transport.RCFResultAction;
import com.amazon.opendistroforelasticsearch.ad.transport.RCFResultTransportAction;
import com.amazon.opendistroforelasticsearch.ad.transport.SearchAnomalyDetectorAction;
import com.amazon.opendistroforelasticsearch.ad.transport.SearchAnomalyDetectorTransportAction;
import com.amazon.opendistroforelasticsearch.ad.transport.SearchAnomalyResultAction;
import com.amazon.opendistroforelasticsearch.ad.transport.SearchAnomalyResultTransportAction;
import com.amazon.opendistroforelasticsearch.ad.transport.StopDetectorAction;
import com.amazon.opendistroforelasticsearch.ad.transport.StopDetectorTransportAction;
import com.amazon.opendistroforelasticsearch.ad.transport.ThresholdResultAction;
Expand Down Expand Up @@ -460,7 +464,9 @@ public List<NamedXContentRegistry.Entry> getNamedXContent() {
new ActionHandler<>(CronAction.INSTANCE, CronTransportAction.class),
new ActionHandler<>(ADStatsNodesAction.INSTANCE, ADStatsNodesTransportAction.class),
new ActionHandler<>(ProfileAction.INSTANCE, ProfileTransportAction.class),
new ActionHandler<>(RCFPollingAction.INSTANCE, RCFPollingTransportAction.class)
new ActionHandler<>(RCFPollingAction.INSTANCE, RCFPollingTransportAction.class),
new ActionHandler<>(SearchAnomalyDetectorAction.INSTANCE, SearchAnomalyDetectorTransportAction.class),
new ActionHandler<>(SearchAnomalyResultAction.INSTANCE, SearchAnomalyResultTransportAction.class)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.ActionType;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.node.NodeClient;
Expand Down Expand Up @@ -57,13 +58,15 @@ public abstract class AbstractSearchAction<T extends ToXContentObject> extends B
private final String index;
private final Class<T> clazz;
private final String urlPath;
private final ActionType<SearchResponse> actionType;

private final Logger logger = LogManager.getLogger(AbstractSearchAction.class);

public AbstractSearchAction(String urlPath, String index, Class<T> clazz) {
public AbstractSearchAction(String urlPath, String index, Class<T> clazz, ActionType<SearchResponse> actionType) {
this.index = index;
this.clazz = clazz;
this.urlPath = urlPath;
this.actionType = actionType;
}

@Override
Expand All @@ -76,10 +79,10 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
searchSourceBuilder.fetchSource(getSourceContext(request));
searchSourceBuilder.seqNoAndPrimaryTerm(true).version(true);
SearchRequest searchRequest = new SearchRequest().source(searchSourceBuilder).indices(this.index);
return channel -> client.search(searchRequest, search(channel, this.clazz));
return channel -> client.execute(actionType, searchRequest, search(channel));
}

private RestResponseListener<SearchResponse> search(RestChannel channel, Class<T> clazz) {
private RestResponseListener<SearchResponse> search(RestChannel channel) {
return new RestResponseListener<SearchResponse>(channel) {
@Override
public RestResponse buildResponse(SearchResponse response) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.amazon.opendistroforelasticsearch.ad.AnomalyDetectorPlugin;
import com.amazon.opendistroforelasticsearch.ad.model.AnomalyDetector;
import com.amazon.opendistroforelasticsearch.ad.transport.SearchAnomalyDetectorAction;

/**
* This class consists of the REST handler to search anomaly detectors.
Expand All @@ -29,7 +30,7 @@ public class RestSearchAnomalyDetectorAction extends AbstractSearchAction<Anomal
private final String SEARCH_ANOMALY_DETECTOR_ACTION = "search_anomaly_detector";

public RestSearchAnomalyDetectorAction() {
super(URL_PATH, ANOMALY_DETECTORS_INDEX, AnomalyDetector.class);
super(URL_PATH, ANOMALY_DETECTORS_INDEX, AnomalyDetector.class, SearchAnomalyDetectorAction.INSTANCE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import com.amazon.opendistroforelasticsearch.ad.AnomalyDetectorPlugin;
import com.amazon.opendistroforelasticsearch.ad.model.AnomalyResult;
import com.amazon.opendistroforelasticsearch.ad.transport.SearchAnomalyResultAction;


/**
* This class consists of the REST handler to search anomaly results.
Expand All @@ -29,7 +31,7 @@ public class RestSearchAnomalyResultAction extends AbstractSearchAction<AnomalyR
private final String SEARCH_ANOMALY_DETECTOR_ACTION = "search_anomaly_result";

public RestSearchAnomalyResultAction() {
super(URL_PATH, ALL_AD_RESULTS_INDEX_PATTERN, AnomalyResult.class);
super(URL_PATH, ALL_AD_RESULTS_INDEX_PATTERN, AnomalyResult.class, SearchAnomalyResultAction.INSTANCE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2020 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.ad.transport;

import org.elasticsearch.action.ActionType;
import org.elasticsearch.action.search.SearchResponse;

public class SearchAnomalyDetectorAction extends ActionType<SearchResponse> {
public static final SearchAnomalyDetectorAction INSTANCE = new SearchAnomalyDetectorAction();
public static final String NAME = "cluster:admin/ad/search/detector";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets use opendistro in the action name. For example: cluster:admin/opendistro/alerting/destinations/write

Also you might want to end with verb like: cluster:admin/opendistro/ad/detectors/search

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure that makes sense.
Will make changes.


private SearchAnomalyDetectorAction() {
super(NAME, SearchResponse::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2020 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.ad.transport;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.HandledTransportAction;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.transport.TransportService;


public class SearchAnomalyDetectorTransportAction extends HandledTransportAction<SearchRequest, SearchResponse> {

private final Client client;

@Inject
public SearchAnomalyDetectorTransportAction(
TransportService transportService,
ActionFilters actionFilters,
Client client
) {
super(SearchAnomalyDetectorAction.NAME, transportService, actionFilters, SearchRequest::new);
this.client = client;
}

@Override
protected void doExecute(Task task, SearchRequest request, ActionListener<SearchResponse> listener) {
client.search(request, new ActionListener<SearchResponse>() {
@Override
public void onResponse(SearchResponse searchResponse) {
listener.onResponse(searchResponse);
}

@Override
public void onFailure(Exception e) {
listener.onFailure(e);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2020 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.ad.transport;

import org.elasticsearch.action.ActionType;
import org.elasticsearch.action.search.SearchResponse;

public class SearchAnomalyResultAction extends ActionType<SearchResponse> {
public static final SearchAnomalyResultAction INSTANCE = new SearchAnomalyResultAction();
public static final String NAME = "cluster:admin/ad/search/result";

private SearchAnomalyResultAction() {
super(NAME, SearchResponse::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2020 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.ad.transport;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.HandledTransportAction;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.transport.TransportService;


public class SearchAnomalyResultTransportAction extends HandledTransportAction<SearchRequest, SearchResponse> {
Copy link

@skkosuri-amzn skkosuri-amzn Sep 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NP: your rest handler is called : RestSearchAnomalyDetectorAction, Would it be better to call transport handler as TransportSearchAnomalyDetectorAction

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats a good point. Existing Transport Actions were named that way, so I just followed them to be consistent.
Here are some examples:https://github.com/opendistro-for-elasticsearch/anomaly-detection/tree/master/src/main/java/com/amazon/opendistroforelasticsearch/ad/transport


private final Client client;

@Inject
public SearchAnomalyResultTransportAction(
TransportService transportService,
ActionFilters actionFilters,
Client client
) {
super(SearchAnomalyResultAction.NAME, transportService, actionFilters, SearchRequest::new);
this.client = client;
}

@Override
protected void doExecute(Task task, SearchRequest request, ActionListener<SearchResponse> listener) {
client.search(request, new ActionListener<SearchResponse>() {
@Override
public void onResponse(SearchResponse searchResponse) {
listener.onResponse(searchResponse);
}

@Override
public void onFailure(Exception e) {
listener.onFailure(e);
}
});
}
}