Search in sources :

Example 1 with ADResultBulkRequest

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());
}
Also used : ADResultBulkResponse(org.opensearch.ad.transport.ADResultBulkResponse) ArrayList(java.util.ArrayList) ADResultBulkRequest(org.opensearch.ad.transport.ADResultBulkRequest) IndexRequest(org.opensearch.action.index.IndexRequest)

Example 2 with ADResultBulkRequest

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ADResultBulkResponse(org.opensearch.ad.transport.ADResultBulkResponse) ArrayList(java.util.ArrayList) ADResultBulkRequest(org.opensearch.ad.transport.ADResultBulkRequest) IndexRequest(org.opensearch.action.index.IndexRequest) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 3 with ADResultBulkRequest

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());
}
Also used : ADResultBulkResponse(org.opensearch.ad.transport.ADResultBulkResponse) ADResultBulkRequest(org.opensearch.ad.transport.ADResultBulkRequest) ResultWriteRequest(org.opensearch.ad.ratelimit.ResultWriteRequest)

Example 4 with ADResultBulkRequest

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));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ADResultBulkRequest(org.opensearch.ad.transport.ADResultBulkRequest) ArgumentMatchers(org.mockito.ArgumentMatchers) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RequestPriority(org.opensearch.ad.ratelimit.RequestPriority) IOException(java.io.IOException) Test(org.junit.Test) ADResultBulkAction(org.opensearch.ad.transport.ADResultBulkAction) Instant(java.time.Instant) AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ChronoUnit(java.time.temporal.ChronoUnit) ADResultBulkResponse(org.opensearch.ad.transport.ADResultBulkResponse) TestHelpers(org.opensearch.ad.TestHelpers) ResultWriteRequest(org.opensearch.ad.ratelimit.ResultWriteRequest) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ActionListener(org.opensearch.action.ActionListener) AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) ADResultBulkRequest(org.opensearch.ad.transport.ADResultBulkRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

ADResultBulkRequest (org.opensearch.ad.transport.ADResultBulkRequest)4 ADResultBulkResponse (org.opensearch.ad.transport.ADResultBulkResponse)4 ArrayList (java.util.ArrayList)2 IndexRequest (org.opensearch.action.index.IndexRequest)2 ResultWriteRequest (org.opensearch.ad.ratelimit.ResultWriteRequest)2 IOException (java.io.IOException)1 Instant (java.time.Instant)1 ChronoUnit (java.time.temporal.ChronoUnit)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.Test)1 ArgumentMatchers (org.mockito.ArgumentMatchers)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1 ActionListener (org.opensearch.action.ActionListener)1 TestHelpers (org.opensearch.ad.TestHelpers)1 AnomalyDetectionException (org.opensearch.ad.common.exception.AnomalyDetectionException)1 RequestPriority (org.opensearch.ad.ratelimit.RequestPriority)1