use of org.opensearch.ad.transport.ADResultBulkResponse in project anomaly-detection by opensearch-project.
the class ResultWriteWorkerTests method testRegular.
public void testRegular() {
List<IndexRequest> retryRequests = new ArrayList<>();
ADResultBulkResponse resp = new ADResultBulkResponse(retryRequests);
ADResultBulkRequest request = new ADResultBulkRequest();
ResultWriteRequest resultWriteRequest = new ResultWriteRequest(Instant.now().plus(10, ChronoUnit.MINUTES).toEpochMilli(), detectorId, RequestPriority.MEDIUM, detectResult, null);
request.add(resultWriteRequest);
doAnswer(invocation -> {
ActionListener<ADResultBulkResponse> listener = invocation.getArgument(1);
listener.onResponse(resp);
return null;
}).when(resultHandler).flush(any(), any());
resultWriteQueue.put(new ResultWriteRequest(Long.MAX_VALUE, detectorId, RequestPriority.MEDIUM, detectResult, null));
// the request results one flush
verify(resultHandler, times(1)).flush(any(), any());
}
use of org.opensearch.ad.transport.ADResultBulkResponse in project anomaly-detection by opensearch-project.
the class ResultWriteWorkerTests method testOverloaded.
public void testOverloaded() {
doAnswer(invocation -> {
ActionListener<ADResultBulkResponse> listener = invocation.getArgument(1);
listener.onFailure(new OpenSearchRejectedExecutionException("blah", true));
return null;
}).when(resultHandler).flush(any(), any());
resultWriteQueue.put(new ResultWriteRequest(Long.MAX_VALUE, detectorId, RequestPriority.MEDIUM, detectResult, null));
// one flush from the original request; and one due to retry
verify(resultHandler, times(1)).flush(any(), any());
verify(nodeStateManager, times(1)).setException(eq(detectorId), any(OpenSearchRejectedExecutionException.class));
}
use of org.opensearch.ad.transport.ADResultBulkResponse in project anomaly-detection by opensearch-project.
the class ResultWriteWorkerTests method testSingleRetryRequest.
public void testSingleRetryRequest() throws IOException {
List<IndexRequest> retryRequests = new ArrayList<>();
try (XContentBuilder builder = jsonBuilder()) {
IndexRequest indexRequest = new IndexRequest(CommonName.ANOMALY_RESULT_INDEX_ALIAS).source(detectResult.toXContent(builder, RestHandlerUtils.XCONTENT_WITH_TYPE));
retryRequests.add(indexRequest);
}
ADResultBulkResponse resp = new ADResultBulkResponse(retryRequests);
ADResultBulkRequest request = new ADResultBulkRequest();
ResultWriteRequest resultWriteRequest = new ResultWriteRequest(Instant.now().plus(10, ChronoUnit.MINUTES).toEpochMilli(), detectorId, RequestPriority.MEDIUM, detectResult, null);
request.add(resultWriteRequest);
final AtomicBoolean retried = new AtomicBoolean();
doAnswer(invocation -> {
ActionListener<ADResultBulkResponse> listener = invocation.getArgument(1);
if (retried.get()) {
listener.onResponse(new ADResultBulkResponse());
} else {
retried.set(true);
listener.onResponse(resp);
}
return null;
}).when(resultHandler).flush(any(), any());
resultWriteQueue.put(new ResultWriteRequest(Long.MAX_VALUE, detectorId, RequestPriority.MEDIUM, detectResult, null));
// one flush from the original request; and one due to retry
verify(resultHandler, times(2)).flush(any(), any());
}
use of org.opensearch.ad.transport.ADResultBulkResponse in project anomaly-detection by opensearch-project.
the class MultiEntityResultHandlerTests method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
handler = new MultiEntityResultHandler(client, settings, threadPool, anomalyDetectionIndices, clientUtil, indexUtil, clusterService);
request = new ADResultBulkRequest();
ResultWriteRequest resultWriteRequest = new ResultWriteRequest(Instant.now().plus(10, ChronoUnit.MINUTES).toEpochMilli(), detectorId, RequestPriority.MEDIUM, TestHelpers.randomAnomalyDetectResult(), null);
request.add(resultWriteRequest);
response = new ADResultBulkResponse();
super.setUpLog4jForJUnit(MultiEntityResultHandler.class);
doAnswer(invocation -> {
ActionListener<ADResultBulkResponse> listener = invocation.getArgument(2);
listener.onResponse(response);
return null;
}).when(client).execute(eq(ADResultBulkAction.INSTANCE), any(), ArgumentMatchers.<ActionListener<ADResultBulkResponse>>any());
}
use of org.opensearch.ad.transport.ADResultBulkResponse in project anomaly-detection by opensearch-project.
the class MultiEntityResultHandlerTests method testSavingFailure.
@Test
public void testSavingFailure() throws IOException, InterruptedException {
setUpSavingAnomalyResultIndex(false);
doAnswer(invocation -> {
ActionListener<ADResultBulkResponse> listener = invocation.getArgument(2);
listener.onFailure(new RuntimeException());
return null;
}).when(client).execute(eq(ADResultBulkAction.INSTANCE), any(), ArgumentMatchers.<ActionListener<ADResultBulkResponse>>any());
CountDownLatch verified = new CountDownLatch(1);
handler.flush(request, ActionListener.wrap(response -> {
assertTrue("Should not reach here ", false);
verified.countDown();
}, exception -> {
assertTrue(exception instanceof RuntimeException);
verified.countDown();
}));
assertTrue(verified.await(100, TimeUnit.SECONDS));
}
Aggregations