Search in sources :

Example 1 with ADResultBulkResponse

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());
}
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 ADResultBulkResponse

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));
}
Also used : ADResultBulkResponse(org.opensearch.ad.transport.ADResultBulkResponse) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException)

Example 3 with ADResultBulkResponse

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());
}
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 4 with ADResultBulkResponse

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

Example 5 with ADResultBulkResponse

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));
}
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) ADResultBulkResponse(org.opensearch.ad.transport.ADResultBulkResponse) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

ADResultBulkResponse (org.opensearch.ad.transport.ADResultBulkResponse)6 ADResultBulkRequest (org.opensearch.ad.transport.ADResultBulkRequest)4 ArrayList (java.util.ArrayList)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)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 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 OpenSearchStatusException (org.opensearch.OpenSearchStatusException)1 ActionListener (org.opensearch.action.ActionListener)1 TestHelpers (org.opensearch.ad.TestHelpers)1 AnomalyDetectionException (org.opensearch.ad.common.exception.AnomalyDetectionException)1