use of org.opensearch.action.support.PlainActionFuture in project anomaly-detection by opensearch-project.
the class MultiEntityResultTests method testEndRunNowInModelNode.
public void testEndRunNowInModelNode() throws InterruptedException, IOException {
Pair<NodeStateManager, CountDownLatch> preparedFixture = setUpTestExceptionTestingInModelNode();
NodeStateManager modelNodeStateManager = preparedFixture.getLeft();
CountDownLatch inProgress = preparedFixture.getRight();
when(modelNodeStateManager.fetchExceptionAndClear(anyString())).thenReturn(Optional.of(new EndRunException(detectorId, CommonErrorMessages.INVALID_SEARCH_QUERY_MSG, new NoSuchElementException("No value present"), true)));
setUpEntityResult(1, modelNodeStateManager);
PlainActionFuture<AnomalyResultResponse> listener = new PlainActionFuture<>();
action.doExecute(null, request, listener);
AnomalyResultResponse response = listener.actionGet(10000L);
assertEquals(Double.NaN, response.getAnomalyGrade(), 0.01);
assertTrue(inProgress.await(10000L, TimeUnit.MILLISECONDS));
// since it is end run now, we don't expect any of the normal workflow continues
verify(resultWriteQueue, never()).put(any());
}
use of org.opensearch.action.support.PlainActionFuture in project anomaly-detection by opensearch-project.
the class MultiEntityResultTests method testEmptyFeatures.
public void testEmptyFeatures() throws InterruptedException {
final CountDownLatch inProgressLatch = new CountDownLatch(1);
doAnswer(invocation -> {
ActionListener<SearchResponse> listener = invocation.getArgument(1);
listener.onResponse(createEmptyResponse());
inProgressLatch.countDown();
return null;
}).when(client).search(any(), any());
PlainActionFuture<AnomalyResultResponse> listener = new PlainActionFuture<>();
action.doExecute(null, request, listener);
AnomalyResultResponse response = listener.actionGet(10000L);
assertEquals(Double.NaN, response.getAnomalyGrade(), 0.01);
assertTrue(inProgressLatch.await(10000L, TimeUnit.MILLISECONDS));
PlainActionFuture<AnomalyResultResponse> listener2 = new PlainActionFuture<>();
action.doExecute(null, request, listener2);
AnomalyResultResponse response2 = listener2.actionGet(10000L);
assertEquals(Double.NaN, response2.getAnomalyGrade(), 0.01);
}
use of org.opensearch.action.support.PlainActionFuture in project anomaly-detection by opensearch-project.
the class AnomalyResultTests method nodeNotConnectedExceptionTemplate.
/**
* Test whether we can handle NodeNotConnectedException when sending requests to
* remote nodes.
*
* @param isRCF whether RCF model node throws node connection
* exception or not
* @param temporary whether node has only temporary connection issue. If
* yes, we should not trigger hash ring rebuilding.
* @param numberOfBuildCall the number of expected hash ring build call
*/
private void nodeNotConnectedExceptionTemplate(boolean isRCF, boolean temporary, int numberOfBuildCall) {
ClusterService hackedClusterService = spy(clusterService);
TransportService exceptionTransportService = spy(transportService);
DiscoveryNode rcfNode = clusterService.state().nodes().getLocalNode();
DiscoveryNode thresholdNode = testNodes[1].discoveryNode();
if (isRCF) {
doThrow(new NodeNotConnectedException(rcfNode, "rcf node not connected")).when(exceptionTransportService).getConnection(same(rcfNode));
} else {
when(hashRing.getOwningNodeWithSameLocalAdVersionForRealtimeAD(eq(thresholdModelID))).thenReturn(Optional.of(thresholdNode));
when(hashRing.getNodeByAddress(any())).thenReturn(Optional.of(thresholdNode));
doThrow(new NodeNotConnectedException(rcfNode, "rcf node not connected")).when(exceptionTransportService).getConnection(same(thresholdNode));
}
if (!temporary) {
when(hackedClusterService.state()).thenReturn(ClusterState.builder(new ClusterName("test")).build());
}
// These constructors register handler in transport service
new RCFResultTransportAction(new ActionFilters(Collections.emptySet()), exceptionTransportService, normalModelManager, adCircuitBreakerService, hashRing);
AnomalyResultTransportAction action = new AnomalyResultTransportAction(new ActionFilters(Collections.emptySet()), exceptionTransportService, settings, client, stateManager, featureQuery, normalModelManager, hashRing, hackedClusterService, indexNameResolver, adCircuitBreakerService, adStats, threadPool, NamedXContentRegistry.EMPTY, adTaskManager);
AnomalyResultRequest request = new AnomalyResultRequest(adID, 100, 200);
PlainActionFuture<AnomalyResultResponse> listener = new PlainActionFuture<>();
action.doExecute(null, request, listener);
assertException(listener, AnomalyDetectionException.class);
if (!temporary) {
verify(hashRing, times(numberOfBuildCall)).buildCirclesForRealtimeAD();
verify(stateManager, never()).addPressure(any(String.class), any(String.class));
} else {
verify(hashRing, never()).buildCirclesForRealtimeAD();
verify(stateManager, times(numberOfBuildCall)).addPressure(any(String.class), any(String.class));
}
}
use of org.opensearch.action.support.PlainActionFuture in project anomaly-detection by opensearch-project.
the class AnomalyResultTests method testAllFeaturesDisabled.
@SuppressWarnings("unchecked")
public void testAllFeaturesDisabled() throws IOException {
doAnswer(invocation -> {
ActionListener<Optional<AnomalyDetector>> listener = invocation.getArgument(1);
listener.onFailure(new EndRunException(adID, CommonErrorMessages.ALL_FEATURES_DISABLED_ERR_MSG, true));
return null;
}).when(stateManager).getAnomalyDetector(any(String.class), any(ActionListener.class));
AnomalyResultTransportAction action = new AnomalyResultTransportAction(new ActionFilters(Collections.emptySet()), transportService, settings, client, stateManager, featureQuery, normalModelManager, hashRing, clusterService, indexNameResolver, adCircuitBreakerService, adStats, threadPool, NamedXContentRegistry.EMPTY, adTaskManager);
AnomalyResultRequest request = new AnomalyResultRequest(adID, 100, 200);
PlainActionFuture<AnomalyResultResponse> listener = new PlainActionFuture<>();
action.doExecute(null, request, listener);
assertException(listener, EndRunException.class, CommonErrorMessages.ALL_FEATURES_DISABLED_ERR_MSG);
}
use of org.opensearch.action.support.PlainActionFuture in project anomaly-detection by opensearch-project.
the class RCFPollingTests method setUp.
@Override
@Before
public void setUp() throws Exception {
super.setUp();
clusterService = mock(ClusterService.class);
hashRing = mock(HashRing.class);
transportAddress1 = new TransportAddress(new InetSocketAddress(InetAddress.getByName("1.2.3.4"), 9300));
manager = mock(ModelManager.class);
transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet());
future = new PlainActionFuture<>();
request = new RCFPollingRequest(detectorId);
model0Id = SingleStreamModelIdMapper.getRcfModelId(detectorId, 0);
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
@SuppressWarnings("unchecked") ActionListener<Long> listener = (ActionListener<Long>) args[2];
listener.onResponse(totalUpdates);
return null;
}).when(manager).getTotalUpdates(any(String.class), any(String.class), any());
normalTransportInterceptor = new TransportInterceptor() {
@Override
public AsyncSender interceptSender(AsyncSender sender) {
return new AsyncSender() {
@Override
public <T extends TransportResponse> void sendRequest(Transport.Connection connection, String action, TransportRequest request, TransportRequestOptions options, TransportResponseHandler<T> handler) {
if (RCFPollingAction.NAME.equals(action)) {
sender.sendRequest(connection, action, request, options, rcfRollingHandler(handler));
} else {
sender.sendRequest(connection, action, request, options, handler);
}
}
};
}
};
failureTransportInterceptor = new TransportInterceptor() {
@Override
public AsyncSender interceptSender(AsyncSender sender) {
return new AsyncSender() {
@Override
public <T extends TransportResponse> void sendRequest(Transport.Connection connection, String action, TransportRequest request, TransportRequestOptions options, TransportResponseHandler<T> handler) {
if (RCFPollingAction.NAME.equals(action)) {
sender.sendRequest(connection, action, request, options, rcfFailureRollingHandler(handler));
} else {
sender.sendRequest(connection, action, request, options, handler);
}
}
};
}
};
}
Aggregations