use of org.opensearch.ad.transport.ADResultBulkRequest 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.ADResultBulkRequest 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.ADResultBulkRequest 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.ADResultBulkRequest in project anomaly-detection by opensearch-project.
the class MultiEntityResultHandlerTests method testNothingToSave.
@Test
public void testNothingToSave() throws IOException, InterruptedException {
setUpSavingAnomalyResultIndex(false);
CountDownLatch verified = new CountDownLatch(1);
handler.flush(new ADResultBulkRequest(), ActionListener.wrap(response -> {
assertTrue("Should not reach here ", false);
verified.countDown();
}, exception -> {
assertTrue(exception instanceof AnomalyDetectionException);
verified.countDown();
}));
assertTrue(verified.await(100, TimeUnit.SECONDS));
}
Aggregations