use of org.opensearch.ad.rest.handler.IndexAnomalyDetectorActionHandler in project anomaly-detection by opensearch-project.
the class IndexAnomalyDetectorTransportAction method adExecute.
protected void adExecute(IndexAnomalyDetectorRequest request, User user, AnomalyDetector currentDetector, ThreadContext.StoredContext storedContext, ActionListener<IndexAnomalyDetectorResponse> listener) {
anomalyDetectionIndices.update();
String detectorId = request.getDetectorID();
long seqNo = request.getSeqNo();
long primaryTerm = request.getPrimaryTerm();
WriteRequest.RefreshPolicy refreshPolicy = request.getRefreshPolicy();
AnomalyDetector detector = request.getDetector();
RestRequest.Method method = request.getMethod();
TimeValue requestTimeout = request.getRequestTimeout();
Integer maxSingleEntityAnomalyDetectors = request.getMaxSingleEntityAnomalyDetectors();
Integer maxMultiEntityAnomalyDetectors = request.getMaxMultiEntityAnomalyDetectors();
Integer maxAnomalyFeatures = request.getMaxAnomalyFeatures();
storedContext.restore();
checkIndicesAndExecute(detector.getIndices(), () -> {
// Don't replace detector's user when update detector
// Github issue: https://github.com/opensearch-project/anomaly-detection/issues/124
User detectorUser = currentDetector == null ? user : currentDetector.getUser();
IndexAnomalyDetectorActionHandler indexAnomalyDetectorActionHandler = new IndexAnomalyDetectorActionHandler(clusterService, client, transportService, listener, anomalyDetectionIndices, detectorId, seqNo, primaryTerm, refreshPolicy, detector, requestTimeout, maxSingleEntityAnomalyDetectors, maxMultiEntityAnomalyDetectors, maxAnomalyFeatures, method, xContentRegistry, detectorUser, adTaskManager, searchFeatureDao);
indexAnomalyDetectorActionHandler.start();
}, listener);
}
use of org.opensearch.ad.rest.handler.IndexAnomalyDetectorActionHandler in project anomaly-detection by opensearch-project.
the class IndexAnomalyDetectorActionHandlerTests method testMoreThanTenMultiEntityDetectors.
@SuppressWarnings("unchecked")
public void testMoreThanTenMultiEntityDetectors() throws IOException {
String field = "a";
AnomalyDetector detector = TestHelpers.randomAnomalyDetectorUsingCategoryFields(detectorId, Arrays.asList(field));
SearchResponse detectorResponse = mock(SearchResponse.class);
int totalHits = 11;
when(detectorResponse.getHits()).thenReturn(TestHelpers.createSearchHits(totalHits));
SearchResponse userIndexResponse = mock(SearchResponse.class);
int userIndexHits = 0;
when(userIndexResponse.getHits()).thenReturn(TestHelpers.createSearchHits(userIndexHits));
// extend NodeClient since its execute method is final and mockito does not allow to mock final methods
// we can also use spy to overstep the final methods
NodeClient client = getCustomNodeClient(detectorResponse, userIndexResponse, detector, threadPool);
NodeClient clientSpy = spy(client);
handler = new IndexAnomalyDetectorActionHandler(clusterService, clientSpy, transportService, channel, anomalyDetectionIndices, detectorId, seqNo, primaryTerm, refreshPolicy, detector, requestTimeout, maxSingleEntityAnomalyDetectors, maxMultiEntityAnomalyDetectors, maxAnomalyFeatures, method, xContentRegistry(), null, adTaskManager, searchFeatureDao);
handler.start();
ArgumentCaptor<Exception> response = ArgumentCaptor.forClass(Exception.class);
verify(clientSpy, times(1)).search(any(SearchRequest.class), any());
verify(channel).onFailure(response.capture());
Exception value = response.getValue();
assertTrue(value instanceof IllegalArgumentException);
String errorMsg = String.format(Locale.ROOT, IndexAnomalyDetectorActionHandler.EXCEEDED_MAX_MULTI_ENTITY_DETECTORS_PREFIX_MSG, maxMultiEntityAnomalyDetectors);
assertTrue(value.getMessage().contains(errorMsg));
}
use of org.opensearch.ad.rest.handler.IndexAnomalyDetectorActionHandler in project anomaly-detection by opensearch-project.
the class IndexAnomalyDetectorActionHandlerTests method testTextField.
@SuppressWarnings("unchecked")
public void testTextField() throws IOException {
String field = "a";
AnomalyDetector detector = TestHelpers.randomAnomalyDetectorUsingCategoryFields(detectorId, Arrays.asList(field));
SearchResponse detectorResponse = mock(SearchResponse.class);
int totalHits = 9;
when(detectorResponse.getHits()).thenReturn(TestHelpers.createSearchHits(totalHits));
// extend NodeClient since its execute method is final and mockito does not allow to mock final methods
// we can also use spy to overstep the final methods
NodeClient client = new NodeClient(Settings.EMPTY, threadPool) {
@Override
public <Request extends ActionRequest, Response extends ActionResponse> void doExecute(ActionType<Response> action, Request request, ActionListener<Response> listener) {
try {
if (action.equals(SearchAction.INSTANCE)) {
listener.onResponse((Response) detectorResponse);
} else {
// passes first get field mapping call where timestamp has to be of type date
// fails on second call where categorical field is checked to be type keyword or IP
// we need to put the test in the same package of GetFieldMappingsResponse since its constructor is package private
GetFieldMappingsResponse response = new GetFieldMappingsResponse(TestHelpers.createFieldMappings(detector.getIndices().get(0), field, "date"));
listener.onResponse((Response) response);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
handler = new IndexAnomalyDetectorActionHandler(clusterService, client, transportService, channel, anomalyDetectionIndices, detectorId, seqNo, primaryTerm, refreshPolicy, detector, requestTimeout, maxSingleEntityAnomalyDetectors, maxMultiEntityAnomalyDetectors, maxAnomalyFeatures, method, xContentRegistry(), null, adTaskManager, searchFeatureDao);
ArgumentCaptor<Exception> response = ArgumentCaptor.forClass(Exception.class);
handler.start();
verify(channel).onFailure(response.capture());
Exception value = response.getValue();
assertTrue(value instanceof Exception);
assertTrue(value.getMessage().contains(IndexAnomalyDetectorActionHandler.CATEGORICAL_FIELD_TYPE_ERR_MSG));
}
use of org.opensearch.ad.rest.handler.IndexAnomalyDetectorActionHandler in project anomaly-detection by opensearch-project.
the class IndexAnomalyDetectorActionHandlerTests method setUp.
@SuppressWarnings("unchecked")
@Override
@Before
public void setUp() throws Exception {
super.setUp();
settings = Settings.EMPTY;
clusterService = mock(ClusterService.class);
clientMock = spy(new NodeClient(settings, threadPool));
transportService = mock(TransportService.class);
channel = mock(ActionListener.class);
anomalyDetectionIndices = mock(AnomalyDetectionIndices.class);
when(anomalyDetectionIndices.doesAnomalyDetectorIndexExist()).thenReturn(true);
detectorId = "123";
seqNo = 0L;
primaryTerm = 0L;
WriteRequest.RefreshPolicy refreshPolicy = WriteRequest.RefreshPolicy.IMMEDIATE;
String field = "a";
detector = TestHelpers.randomAnomalyDetectorUsingCategoryFields(detectorId, Arrays.asList(field));
requestTimeout = new TimeValue(1000L);
maxSingleEntityAnomalyDetectors = 1000;
maxMultiEntityAnomalyDetectors = 10;
maxAnomalyFeatures = 5;
method = RestRequest.Method.POST;
adTaskManager = mock(ADTaskManager.class);
searchFeatureDao = mock(SearchFeatureDao.class);
handler = new IndexAnomalyDetectorActionHandler(clusterService, clientMock, transportService, channel, anomalyDetectionIndices, detectorId, seqNo, primaryTerm, refreshPolicy, detector, requestTimeout, maxSingleEntityAnomalyDetectors, maxMultiEntityAnomalyDetectors, maxAnomalyFeatures, method, xContentRegistry(), null, adTaskManager, searchFeatureDao);
}
use of org.opensearch.ad.rest.handler.IndexAnomalyDetectorActionHandler in project anomaly-detection by opensearch-project.
the class IndexAnomalyDetectorActionHandlerTests method testUpdateTemplate.
@SuppressWarnings("unchecked")
private void testUpdateTemplate(String fieldTypeName) throws IOException {
String field = "a";
AnomalyDetector detector = TestHelpers.randomAnomalyDetectorUsingCategoryFields(detectorId, Arrays.asList(field));
SearchResponse detectorResponse = mock(SearchResponse.class);
int totalHits = 9;
when(detectorResponse.getHits()).thenReturn(TestHelpers.createSearchHits(totalHits));
GetResponse getDetectorResponse = TestHelpers.createGetResponse(detector, detector.getDetectorId(), AnomalyDetector.ANOMALY_DETECTORS_INDEX);
SearchResponse userIndexResponse = mock(SearchResponse.class);
int userIndexHits = 0;
when(userIndexResponse.getHits()).thenReturn(TestHelpers.createSearchHits(userIndexHits));
// extend NodeClient since its execute method is final and mockito does not allow to mock final methods
// we can also use spy to overstep the final methods
NodeClient client = new NodeClient(Settings.EMPTY, threadPool) {
@Override
public <Request extends ActionRequest, Response extends ActionResponse> void doExecute(ActionType<Response> action, Request request, ActionListener<Response> listener) {
try {
if (action.equals(SearchAction.INSTANCE)) {
assertTrue(request instanceof SearchRequest);
SearchRequest searchRequest = (SearchRequest) request;
if (searchRequest.indices()[0].equals(ANOMALY_DETECTORS_INDEX)) {
listener.onResponse((Response) detectorResponse);
} else {
listener.onResponse((Response) userIndexResponse);
}
} else if (action.equals(GetAction.INSTANCE)) {
assertTrue(request instanceof GetRequest);
listener.onResponse((Response) getDetectorResponse);
} else {
GetFieldMappingsResponse response = new GetFieldMappingsResponse(TestHelpers.createFieldMappings(detector.getIndices().get(0), field, fieldTypeName));
listener.onResponse((Response) response);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
NodeClient clientSpy = spy(client);
ClusterName clusterName = new ClusterName("test");
ClusterState clusterState = ClusterState.builder(clusterName).metadata(Metadata.builder().build()).build();
when(clusterService.state()).thenReturn(clusterState);
handler = new IndexAnomalyDetectorActionHandler(clusterService, clientSpy, transportService, channel, anomalyDetectionIndices, detectorId, seqNo, primaryTerm, refreshPolicy, detector, requestTimeout, maxSingleEntityAnomalyDetectors, maxMultiEntityAnomalyDetectors, maxAnomalyFeatures, RestRequest.Method.PUT, xContentRegistry(), null, adTaskManager, searchFeatureDao);
ArgumentCaptor<Exception> response = ArgumentCaptor.forClass(Exception.class);
handler.start();
verify(clientSpy, times(1)).execute(eq(GetFieldMappingsAction.INSTANCE), any(), any());
verify(channel).onFailure(response.capture());
Exception value = response.getValue();
if (fieldTypeName.equals(CommonName.IP_TYPE) || fieldTypeName.equals(CommonName.KEYWORD_TYPE)) {
assertTrue(value.getMessage().contains(IndexAnomalyDetectorActionHandler.NO_DOCS_IN_USER_INDEX_MSG));
} else {
assertTrue(value.getMessage().contains(IndexAnomalyDetectorActionHandler.CATEGORICAL_FIELD_TYPE_ERR_MSG));
}
}
Aggregations