Search in sources :

Example 11 with MultiGetRequest

use of org.opensearch.action.get.MultiGetRequest in project OpenSearch by opensearch-project.

the class BulkProcessorRetryIT method executeBulkRejectionLoad.

private void executeBulkRejectionLoad(BackoffPolicy backoffPolicy, boolean rejectedExecutionExpected) throws Exception {
    final CorrelatingBackoffPolicy internalPolicy = new CorrelatingBackoffPolicy(backoffPolicy);
    final int numberOfAsyncOps = randomIntBetween(600, 700);
    final CountDownLatch latch = new CountDownLatch(numberOfAsyncOps);
    final Set<Object> responses = Collections.newSetFromMap(new ConcurrentHashMap<>());
    BulkProcessor bulkProcessor = initBulkProcessorBuilder(new BulkProcessor.Listener() {

        @Override
        public void beforeBulk(long executionId, BulkRequest request) {
        }

        @Override
        public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
            internalPolicy.logResponse(response);
            responses.add(response);
            latch.countDown();
        }

        @Override
        public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
            internalPolicy.logResponse(failure);
            responses.add(failure);
            latch.countDown();
        }
    }).setBulkActions(1).setConcurrentRequests(randomIntBetween(0, 100)).setBackoffPolicy(internalPolicy).build();
    MultiGetRequest multiGetRequest = indexDocs(bulkProcessor, numberOfAsyncOps);
    latch.await(10, TimeUnit.SECONDS);
    bulkProcessor.close();
    assertEquals(responses.size(), numberOfAsyncOps);
    boolean rejectedAfterAllRetries = false;
    for (Object response : responses) {
        if (response instanceof BulkResponse) {
            BulkResponse bulkResponse = (BulkResponse) response;
            for (BulkItemResponse bulkItemResponse : bulkResponse.getItems()) {
                if (bulkItemResponse.isFailed()) {
                    BulkItemResponse.Failure failure = bulkItemResponse.getFailure();
                    if (failure.getStatus() == RestStatus.TOO_MANY_REQUESTS) {
                        if (rejectedExecutionExpected == false) {
                            assertRetriedCorrectly(internalPolicy, bulkResponse, failure.getCause());
                            rejectedAfterAllRetries = true;
                        }
                    } else {
                        throw new AssertionError("Unexpected failure with status: " + failure.getStatus());
                    }
                }
            }
        } else {
            if (response instanceof RemoteTransportException && ((RemoteTransportException) response).status() == RestStatus.TOO_MANY_REQUESTS) {
                if (rejectedExecutionExpected == false) {
                    assertRetriedCorrectly(internalPolicy, response, ((Throwable) response).getCause());
                    rejectedAfterAllRetries = true;
                }
            // ignored, we exceeded the write queue size when dispatching the initial bulk request
            } else {
                Throwable t = (Throwable) response;
                // we're not expecting any other errors
                throw new AssertionError("Unexpected failure", t);
            }
        }
    }
    highLevelClient().indices().refresh(new RefreshRequest(), RequestOptions.DEFAULT);
    int multiGetResponsesCount = highLevelClient().mget(multiGetRequest, RequestOptions.DEFAULT).getResponses().length;
    if (rejectedExecutionExpected) {
        assertThat(multiGetResponsesCount, lessThanOrEqualTo(numberOfAsyncOps));
    } else if (rejectedAfterAllRetries) {
        assertThat(multiGetResponsesCount, lessThan(numberOfAsyncOps));
    } else {
        assertThat(multiGetResponsesCount, equalTo(numberOfAsyncOps));
    }
}
Also used : RefreshRequest(org.opensearch.action.admin.indices.refresh.RefreshRequest) RemoteTransportException(org.opensearch.transport.RemoteTransportException) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) BulkResponse(org.opensearch.action.bulk.BulkResponse) CountDownLatch(java.util.concurrent.CountDownLatch) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) BulkProcessor(org.opensearch.action.bulk.BulkProcessor) BulkRequest(org.opensearch.action.bulk.BulkRequest)

Example 12 with MultiGetRequest

use of org.opensearch.action.get.MultiGetRequest in project OpenSearch by opensearch-project.

the class BulkProcessorIT method indexDocs.

private MultiGetRequest indexDocs(BulkProcessor processor, int numDocs, String localIndex, String globalIndex, String globalPipeline) throws Exception {
    MultiGetRequest multiGetRequest = new MultiGetRequest();
    for (int i = 1; i <= numDocs; i++) {
        if (randomBoolean()) {
            processor.add(new IndexRequest(localIndex).id(Integer.toString(i)).source(XContentType.JSON, "field", randomRealisticUnicodeOfLengthBetween(1, 30)));
        } else {
            BytesArray data = bytesBulkRequest(localIndex, i);
            processor.add(data, globalIndex, globalPipeline, XContentType.JSON);
        }
        multiGetRequest.add(localIndex, Integer.toString(i));
    }
    return multiGetRequest;
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) IndexRequest(org.opensearch.action.index.IndexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest)

Example 13 with MultiGetRequest

use of org.opensearch.action.get.MultiGetRequest in project OpenSearch by opensearch-project.

the class BulkProcessorIT method testBulkProcessorConcurrentRequestsReadOnlyIndex.

public void testBulkProcessorConcurrentRequestsReadOnlyIndex() throws Exception {
    Request request = new Request("PUT", "/test-ro");
    request.setJsonEntity("{\n" + "    \"settings\" : {\n" + "        \"index\" : {\n" + "            \"blocks.write\" : true\n" + "        }\n" + "    }\n" + "    \n" + "}");
    Response response = client().performRequest(request);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
    int bulkActions = randomIntBetween(10, 100);
    int numDocs = randomIntBetween(bulkActions, bulkActions + 100);
    int concurrentRequests = randomIntBetween(0, 10);
    int expectedBulkActions = numDocs / bulkActions;
    final CountDownLatch latch = new CountDownLatch(expectedBulkActions);
    int totalExpectedBulkActions = numDocs % bulkActions == 0 ? expectedBulkActions : expectedBulkActions + 1;
    final CountDownLatch closeLatch = new CountDownLatch(totalExpectedBulkActions);
    int testDocs = 0;
    int testReadOnlyDocs = 0;
    MultiGetRequest multiGetRequest = new MultiGetRequest();
    BulkProcessorTestListener listener = new BulkProcessorTestListener(latch, closeLatch);
    try (BulkProcessor processor = initBulkProcessorBuilder(listener).setConcurrentRequests(concurrentRequests).setBulkActions(bulkActions).setFlushInterval(TimeValue.timeValueHours(24)).setBulkSize(new ByteSizeValue(1, ByteSizeUnit.GB)).build()) {
        for (int i = 1; i <= numDocs; i++) {
            // let's make sure we get at least 1 item in the MultiGetRequest regardless of the randomising roulette
            if (randomBoolean() || multiGetRequest.getItems().size() == 0) {
                testDocs++;
                processor.add(new IndexRequest("test").id(Integer.toString(testDocs)).source(XContentType.JSON, "field", "value"));
                multiGetRequest.add("test", Integer.toString(testDocs));
            } else {
                testReadOnlyDocs++;
                processor.add(new IndexRequest("test-ro").id(Integer.toString(testReadOnlyDocs)).source(XContentType.JSON, "field", "value"));
            }
        }
    }
    closeLatch.await();
    assertThat(listener.beforeCounts.get(), equalTo(totalExpectedBulkActions));
    assertThat(listener.afterCounts.get(), equalTo(totalExpectedBulkActions));
    assertThat(listener.bulkFailures.size(), equalTo(0));
    assertThat(listener.bulkItems.size(), equalTo(testDocs + testReadOnlyDocs));
    Set<String> ids = new HashSet<>();
    Set<String> readOnlyIds = new HashSet<>();
    for (BulkItemResponse bulkItemResponse : listener.bulkItems) {
        assertThat(bulkItemResponse.getIndex(), either(equalTo("test")).or(equalTo("test-ro")));
        if (bulkItemResponse.getIndex().equals("test")) {
            assertThat(bulkItemResponse.isFailed(), equalTo(false));
            // with concurrent requests > 1 we can't rely on the order of the bulk requests
            assertThat(Integer.valueOf(bulkItemResponse.getId()), both(greaterThan(0)).and(lessThanOrEqualTo(testDocs)));
            // we do want to check that we don't get duplicate ids back
            assertThat(ids.add(bulkItemResponse.getId()), equalTo(true));
        } else {
            assertThat(bulkItemResponse.isFailed(), equalTo(true));
            // with concurrent requests > 1 we can't rely on the order of the bulk requests
            assertThat(Integer.valueOf(bulkItemResponse.getId()), both(greaterThan(0)).and(lessThanOrEqualTo(testReadOnlyDocs)));
            // we do want to check that we don't get duplicate ids back
            assertThat(readOnlyIds.add(bulkItemResponse.getId()), equalTo(true));
        }
    }
    assertMultiGetResponse(highLevelClient().mget(multiGetRequest, RequestOptions.DEFAULT), testDocs);
}
Also used : BulkRequest(org.opensearch.action.bulk.BulkRequest) SearchRequest(org.opensearch.action.search.SearchRequest) IndexRequest(org.opensearch.action.index.IndexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) CountDownLatch(java.util.concurrent.CountDownLatch) IndexRequest(org.opensearch.action.index.IndexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) MultiGetResponse(org.opensearch.action.get.MultiGetResponse) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) MultiGetItemResponse(org.opensearch.action.get.MultiGetItemResponse) BulkResponse(org.opensearch.action.bulk.BulkResponse) BulkProcessor(org.opensearch.action.bulk.BulkProcessor) HashSet(java.util.HashSet)

Example 14 with MultiGetRequest

use of org.opensearch.action.get.MultiGetRequest in project OpenSearch by opensearch-project.

the class BulkProcessorIT method testBulkProcessorConcurrentRequests.

public void testBulkProcessorConcurrentRequests() throws Exception {
    int bulkActions = randomIntBetween(10, 100);
    int numDocs = randomIntBetween(bulkActions, bulkActions + 100);
    int concurrentRequests = randomIntBetween(0, 7);
    int expectedBulkActions = numDocs / bulkActions;
    final CountDownLatch latch = new CountDownLatch(expectedBulkActions);
    int totalExpectedBulkActions = numDocs % bulkActions == 0 ? expectedBulkActions : expectedBulkActions + 1;
    final CountDownLatch closeLatch = new CountDownLatch(totalExpectedBulkActions);
    BulkProcessorTestListener listener = new BulkProcessorTestListener(latch, closeLatch);
    MultiGetRequest multiGetRequest;
    try (BulkProcessor processor = initBulkProcessorBuilder(listener).setConcurrentRequests(concurrentRequests).setBulkActions(bulkActions).setFlushInterval(TimeValue.timeValueHours(24)).setBulkSize(new ByteSizeValue(1, ByteSizeUnit.GB)).build()) {
        multiGetRequest = indexDocs(processor, numDocs);
        latch.await();
        assertThat(listener.beforeCounts.get(), equalTo(expectedBulkActions));
        assertThat(listener.afterCounts.get(), equalTo(expectedBulkActions));
        assertThat(listener.bulkFailures.size(), equalTo(0));
        assertThat(listener.bulkItems.size(), equalTo(numDocs - numDocs % bulkActions));
    }
    closeLatch.await();
    assertThat(listener.beforeCounts.get(), equalTo(totalExpectedBulkActions));
    assertThat(listener.afterCounts.get(), equalTo(totalExpectedBulkActions));
    assertThat(listener.bulkFailures.size(), equalTo(0));
    assertThat(listener.bulkItems.size(), equalTo(numDocs));
    Set<String> ids = new HashSet<>();
    for (BulkItemResponse bulkItemResponse : listener.bulkItems) {
        assertThat(bulkItemResponse.getFailureMessage(), bulkItemResponse.isFailed(), equalTo(false));
        assertThat(bulkItemResponse.getIndex(), equalTo("test"));
        // with concurrent requests > 1 we can't rely on the order of the bulk requests
        assertThat(Integer.valueOf(bulkItemResponse.getId()), both(greaterThan(0)).and(lessThanOrEqualTo(numDocs)));
        // we do want to check that we don't get duplicate ids back
        assertThat(ids.add(bulkItemResponse.getId()), equalTo(true));
    }
    assertMultiGetResponse(highLevelClient().mget(multiGetRequest, RequestOptions.DEFAULT), numDocs);
}
Also used : BulkProcessor(org.opensearch.action.bulk.BulkProcessor) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) CountDownLatch(java.util.concurrent.CountDownLatch) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) HashSet(java.util.HashSet)

Example 15 with MultiGetRequest

use of org.opensearch.action.get.MultiGetRequest in project OpenSearch by opensearch-project.

the class RequestConverters method multiGet.

static Request multiGet(MultiGetRequest multiGetRequest) throws IOException {
    Request request = new Request(HttpPost.METHOD_NAME, "/_mget");
    Params parameters = new Params();
    parameters.withPreference(multiGetRequest.preference());
    parameters.withRealtime(multiGetRequest.realtime());
    parameters.withRefresh(multiGetRequest.refresh());
    request.addParameters(parameters.asMap());
    request.setEntity(createEntity(multiGetRequest, REQUEST_BODY_CONTENT_TYPE));
    return request;
}
Also used : BulkRequest(org.opensearch.action.bulk.BulkRequest) WriteRequest(org.opensearch.action.support.WriteRequest) AbstractBulkByScrollRequest(org.opensearch.index.reindex.AbstractBulkByScrollRequest) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) CountRequest(org.opensearch.client.core.CountRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) RankEvalRequest(org.opensearch.index.rankeval.RankEvalRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) ExplainRequest(org.opensearch.action.explain.ExplainRequest) SearchRequest(org.opensearch.action.search.SearchRequest) DeleteStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) GetRequest(org.opensearch.action.get.GetRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ClusterHealthRequest(org.opensearch.action.admin.cluster.health.ClusterHealthRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest)

Aggregations

MultiGetRequest (org.opensearch.action.get.MultiGetRequest)17 IndexRequest (org.opensearch.action.index.IndexRequest)9 BulkRequest (org.opensearch.action.bulk.BulkRequest)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 BulkProcessor (org.opensearch.action.bulk.BulkProcessor)6 GetRequest (org.opensearch.action.get.GetRequest)5 ByteSizeValue (org.opensearch.common.unit.ByteSizeValue)5 DocWriteRequest (org.opensearch.action.DocWriteRequest)4 BulkItemResponse (org.opensearch.action.bulk.BulkItemResponse)4 DeleteRequest (org.opensearch.action.delete.DeleteRequest)4 SearchRequest (org.opensearch.action.search.SearchRequest)4 WriteRequest (org.opensearch.action.support.WriteRequest)4 UpdateRequest (org.opensearch.action.update.UpdateRequest)4 GetSourceRequest (org.opensearch.client.core.GetSourceRequest)4 MultiTermVectorsRequest (org.opensearch.client.core.MultiTermVectorsRequest)4 TermVectorsRequest (org.opensearch.client.core.TermVectorsRequest)4 DeleteByQueryRequest (org.opensearch.index.reindex.DeleteByQueryRequest)4 ReindexRequest (org.opensearch.index.reindex.ReindexRequest)4 UpdateByQueryRequest (org.opensearch.index.reindex.UpdateByQueryRequest)4 DeleteStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest)3