Search in sources :

Example 1 with Hit

use of org.opensearch.index.reindex.ScrollableHitSource.Hit in project OpenSearch by opensearch-project.

the class AsyncBulkByScrollActionTests method testBuildRequestThrowsException.

/**
 * Mimicks script failures or general wrongness by implementers.
 */
public void testBuildRequestThrowsException() throws Exception {
    DummyAsyncBulkByScrollAction action = new DummyAsyncBulkByScrollAction() {

        @Override
        protected AbstractAsyncBulkByScrollAction.RequestWrapper<?> buildRequest(Hit doc) {
            throw new RuntimeException("surprise");
        }
    };
    ScrollableHitSource.BasicHit hit = new ScrollableHitSource.BasicHit("index", "id", 0);
    hit.setSource(new BytesArray("{}"), XContentType.JSON);
    ScrollableHitSource.Response response = new ScrollableHitSource.Response(false, emptyList(), 1, singletonList(hit), null);
    simulateScrollResponse(action, System.nanoTime(), 0, response);
    ExecutionException e = expectThrows(ExecutionException.class, () -> listener.get());
    assertThat(e.getCause(), instanceOf(RuntimeException.class));
    assertThat(e.getCause().getMessage(), equalTo("surprise"));
}
Also used : IndexResponse(org.opensearch.action.index.IndexResponse) ActionResponse(org.opensearch.action.ActionResponse) DocWriteResponse(org.opensearch.action.DocWriteResponse) UpdateResponse(org.opensearch.action.update.UpdateResponse) DeleteResponse(org.opensearch.action.delete.DeleteResponse) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) ClearScrollResponse(org.opensearch.action.search.ClearScrollResponse) SearchResponse(org.opensearch.action.search.SearchResponse) BulkResponse(org.opensearch.action.bulk.BulkResponse) SearchHit(org.opensearch.search.SearchHit) Hit(org.opensearch.index.reindex.ScrollableHitSource.Hit) BytesArray(org.opensearch.common.bytes.BytesArray) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException)

Example 2 with Hit

use of org.opensearch.index.reindex.ScrollableHitSource.Hit in project OpenSearch by opensearch-project.

the class AsyncBulkByScrollActionTests method testScrollDelay.

public void testScrollDelay() throws Exception {
    /*
         * Replace the thread pool with one that will save the delay sent for the command. We'll use that to check that we used a proper
         * delay for throttling.
         */
    AtomicReference<TimeValue> capturedDelay = new AtomicReference<>();
    AtomicReference<Runnable> capturedCommand = new AtomicReference<>();
    setupClient(new TestThreadPool(getTestName()) {

        @Override
        public ScheduledCancellable schedule(Runnable command, TimeValue delay, String name) {
            capturedDelay.set(delay);
            capturedCommand.set(command);
            return new ScheduledCancellable() {

                private boolean cancelled = false;

                @Override
                public long getDelay(TimeUnit unit) {
                    return unit.convert(delay.millis(), TimeUnit.MILLISECONDS);
                }

                @Override
                public int compareTo(Delayed o) {
                    return 0;
                }

                @Override
                public boolean cancel() {
                    cancelled = true;
                    return true;
                }

                @Override
                public boolean isCancelled() {
                    return cancelled;
                }
            };
        }
    });
    DummyAsyncBulkByScrollAction action = new DummyAsyncBulkByScrollAction() {

        @Override
        protected RequestWrapper<?> buildRequest(Hit doc) {
            return wrap(new IndexRequest().index("test"));
        }
    };
    action.setScroll(scrollId());
    // Set the base for the scroll to wait - this is added to the figure we calculate below
    firstSearchRequest.scroll(timeValueSeconds(10));
    // Set throttle to 1 request per second to make the math simpler
    worker.rethrottle(1f);
    action.start();
    // create a simulated response.
    SearchHit hit = new SearchHit(0, "id", emptyMap(), emptyMap()).sourceRef(new BytesArray("{}"));
    SearchHits hits = new SearchHits(IntStream.range(0, 100).mapToObj(i -> hit).toArray(SearchHit[]::new), new TotalHits(0, TotalHits.Relation.EQUAL_TO), 0);
    InternalSearchResponse internalResponse = new InternalSearchResponse(hits, null, null, null, false, false, 1);
    SearchResponse searchResponse = new SearchResponse(internalResponse, scrollId(), 5, 4, 0, randomLong(), null, SearchResponse.Clusters.EMPTY);
    client.lastSearch.get().listener.onResponse(searchResponse);
    assertEquals(0, capturedDelay.get().seconds());
    capturedCommand.get().run();
    // So the next request is going to have to wait an extra 100 seconds or so (base was 10 seconds, so 110ish)
    assertThat(client.lastScroll.get().request.scroll().keepAlive().seconds(), either(equalTo(110L)).or(equalTo(109L)));
    // Now we can simulate a response and check the delay that we used for the task
    if (randomBoolean()) {
        client.lastScroll.get().listener.onResponse(searchResponse);
        assertEquals(99, capturedDelay.get().seconds());
    } else {
        // Let's rethrottle between the starting the scroll and getting the response
        worker.rethrottle(10f);
        client.lastScroll.get().listener.onResponse(searchResponse);
        // The delay uses the new throttle
        assertEquals(9, capturedDelay.get().seconds());
    }
    // Running the command ought to increment the delay counter on the task.
    capturedCommand.get().run();
    assertEquals(capturedDelay.get(), testTask.getStatus().getThrottled());
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) BytesArray(org.opensearch.common.bytes.BytesArray) Delayed(java.util.concurrent.Delayed) SearchHit(org.opensearch.search.SearchHit) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) TestUtil.randomSimpleString(org.apache.lucene.util.TestUtil.randomSimpleString) TestThreadPool(org.opensearch.threadpool.TestThreadPool) IndexRequest(org.opensearch.action.index.IndexRequest) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) SearchHit(org.opensearch.search.SearchHit) Hit(org.opensearch.index.reindex.ScrollableHitSource.Hit) AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable) TimeUnit(java.util.concurrent.TimeUnit) SearchHits(org.opensearch.search.SearchHits) TimeValue(org.opensearch.common.unit.TimeValue) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Example 3 with Hit

use of org.opensearch.index.reindex.ScrollableHitSource.Hit in project OpenSearch by opensearch-project.

the class AsyncBulkByScrollActionTests method testScrollResponseBatchingBehavior.

/**
 * Tests that each scroll response is a batch and that the batch is launched properly.
 */
public void testScrollResponseBatchingBehavior() throws Exception {
    int maxBatches = randomIntBetween(0, 100);
    for (int batches = 1; batches < maxBatches; batches++) {
        Hit hit = new ScrollableHitSource.BasicHit("index", "id", 0);
        ScrollableHitSource.Response response = new ScrollableHitSource.Response(false, emptyList(), 1, singletonList(hit), null);
        DummyAsyncBulkByScrollAction action = new DummyAsyncBulkByScrollAction();
        simulateScrollResponse(action, System.nanoTime(), 0, response);
        // Use assert busy because the update happens on another thread
        final int expectedBatches = batches;
        assertBusy(() -> assertEquals(expectedBatches, testTask.getStatus().getBatches()));
    }
}
Also used : IndexResponse(org.opensearch.action.index.IndexResponse) ActionResponse(org.opensearch.action.ActionResponse) DocWriteResponse(org.opensearch.action.DocWriteResponse) UpdateResponse(org.opensearch.action.update.UpdateResponse) DeleteResponse(org.opensearch.action.delete.DeleteResponse) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) ClearScrollResponse(org.opensearch.action.search.ClearScrollResponse) SearchResponse(org.opensearch.action.search.SearchResponse) BulkResponse(org.opensearch.action.bulk.BulkResponse) SearchHit(org.opensearch.search.SearchHit) Hit(org.opensearch.index.reindex.ScrollableHitSource.Hit)

Aggregations

SearchResponse (org.opensearch.action.search.SearchResponse)3 Hit (org.opensearch.index.reindex.ScrollableHitSource.Hit)3 SearchHit (org.opensearch.search.SearchHit)3 InternalSearchResponse (org.opensearch.search.internal.InternalSearchResponse)3 ActionResponse (org.opensearch.action.ActionResponse)2 DocWriteResponse (org.opensearch.action.DocWriteResponse)2 BulkItemResponse (org.opensearch.action.bulk.BulkItemResponse)2 BulkResponse (org.opensearch.action.bulk.BulkResponse)2 DeleteResponse (org.opensearch.action.delete.DeleteResponse)2 IndexResponse (org.opensearch.action.index.IndexResponse)2 ClearScrollResponse (org.opensearch.action.search.ClearScrollResponse)2 UpdateResponse (org.opensearch.action.update.UpdateResponse)2 BytesArray (org.opensearch.common.bytes.BytesArray)2 Delayed (java.util.concurrent.Delayed)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 TotalHits (org.apache.lucene.search.TotalHits)1 TestUtil.randomSimpleString (org.apache.lucene.util.TestUtil.randomSimpleString)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1