Search in sources :

Example 16 with DocWriteRequest

use of org.opensearch.action.DocWriteRequest in project OpenSearch by opensearch-project.

the class TransportShardBulkAction method onComplete.

private static void onComplete(Engine.Result r, BulkPrimaryExecutionContext context, UpdateHelper.Result updateResult) {
    context.markOperationAsExecuted(r);
    final DocWriteRequest<?> docWriteRequest = context.getCurrent();
    final DocWriteRequest.OpType opType = docWriteRequest.opType();
    final boolean isUpdate = opType == DocWriteRequest.OpType.UPDATE;
    final BulkItemResponse executionResult = context.getExecutionResult();
    final boolean isFailed = executionResult.isFailed();
    if (isUpdate && isFailed && isConflictException(executionResult.getFailure().getCause()) && context.getRetryCounter() < ((UpdateRequest) docWriteRequest).retryOnConflict()) {
        context.resetForExecutionForRetry();
        return;
    }
    final BulkItemResponse response;
    if (isUpdate) {
        response = processUpdateResponse((UpdateRequest) docWriteRequest, context.getConcreteIndex(), executionResult, updateResult);
    } else {
        if (isFailed) {
            final Exception failure = executionResult.getFailure().getCause();
            final MessageSupplier messageSupplier = () -> new ParameterizedMessage("{} failed to execute bulk item ({}) {}", context.getPrimary().shardId(), opType.getLowercase(), docWriteRequest);
            if (TransportShardBulkAction.isConflictException(failure)) {
                logger.trace(messageSupplier, failure);
            } else {
                logger.debug(messageSupplier, failure);
            }
        }
        response = executionResult;
    }
    context.markAsCompleted(response);
    assert context.isInitial();
}
Also used : MessageSupplier(org.apache.logging.log4j.util.MessageSupplier) UpdateRequest(org.opensearch.action.update.UpdateRequest) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) DocWriteRequest(org.opensearch.action.DocWriteRequest) NodeClosedException(org.opensearch.node.NodeClosedException) MapperException(org.opensearch.index.mapper.MapperException) VersionConflictEngineException(org.opensearch.index.engine.VersionConflictEngineException) IOException(java.io.IOException)

Example 17 with DocWriteRequest

use of org.opensearch.action.DocWriteRequest in project OpenSearch by opensearch-project.

the class TransportShardBulkActionTests method testExecuteBulkIndexRequestWithErrorWhileUpdatingMapping.

public void testExecuteBulkIndexRequestWithErrorWhileUpdatingMapping() throws Exception {
    IndexShard shard = newStartedShard(true);
    BulkItemRequest[] items = new BulkItemRequest[1];
    DocWriteRequest<IndexRequest> writeRequest = new IndexRequest("index").id("id").source(Requests.INDEX_CONTENT_TYPE, "foo", "bar");
    items[0] = new BulkItemRequest(0, writeRequest);
    BulkShardRequest bulkShardRequest = new BulkShardRequest(shardId, RefreshPolicy.NONE, items);
    // Return an exception when trying to update the mapping, or when waiting for it to come
    RuntimeException err = new RuntimeException("some kind of exception");
    boolean errorOnWait = randomBoolean();
    randomlySetIgnoredPrimaryResponse(items[0]);
    BulkPrimaryExecutionContext context = new BulkPrimaryExecutionContext(bulkShardRequest, shard);
    final CountDownLatch latch = new CountDownLatch(1);
    TransportShardBulkAction.executeBulkItemRequest(context, null, threadPool::absoluteTimeInMillis, errorOnWait == false ? new ThrowingMappingUpdatePerformer(err) : new NoopMappingUpdatePerformer(), errorOnWait ? listener -> listener.onFailure(err) : listener -> listener.onResponse(null), new LatchedActionListener<>(new ActionListener<Void>() {

        @Override
        public void onResponse(Void aVoid) {
        }

        @Override
        public void onFailure(final Exception e) {
            assertEquals(err, e);
        }
    }, latch));
    latch.await();
    assertFalse(context.hasMoreOperationsToExecute());
    // Translog shouldn't be synced, as there were conflicting mappings
    assertThat(context.getLocationToSync(), nullValue());
    BulkItemResponse primaryResponse = bulkShardRequest.items()[0].getPrimaryResponse();
    // Since this was not a conflict failure, the primary response
    // should be filled out with the failure information
    assertThat(primaryResponse.getItemId(), equalTo(0));
    assertThat(primaryResponse.getId(), equalTo("id"));
    assertThat(primaryResponse.getOpType(), equalTo(DocWriteRequest.OpType.INDEX));
    assertTrue(primaryResponse.isFailed());
    assertThat(primaryResponse.getFailureMessage(), containsString("some kind of exception"));
    BulkItemResponse.Failure failure = primaryResponse.getFailure();
    assertThat(failure.getIndex(), equalTo("index"));
    assertThat(failure.getId(), equalTo("id"));
    assertThat(failure.getCause(), equalTo(err));
    closeShards(shard);
}
Also used : OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) IndexResponse(org.opensearch.action.index.IndexResponse) OpenSearchStatusException(org.opensearch.OpenSearchStatusException) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Version(org.opensearch.Version) OpenSearchException(org.opensearch.OpenSearchException) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) IndexShardTestCase(org.opensearch.index.shard.IndexShardTestCase) MapperService(org.opensearch.index.mapper.MapperService) LatchedActionListener(org.opensearch.action.LatchedActionListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Mockito.anyBoolean(org.mockito.Mockito.anyBoolean) Matchers.nullValue(org.hamcrest.Matchers.nullValue) ActionListener(org.opensearch.action.ActionListener) DeleteRequest(org.opensearch.action.delete.DeleteRequest) CyclicBarrier(java.util.concurrent.CyclicBarrier) Settings(org.opensearch.common.settings.Settings) RestStatus(org.opensearch.rest.RestStatus) Engine(org.opensearch.index.engine.Engine) CountDownLatch(java.util.concurrent.CountDownLatch) VersionType(org.opensearch.index.VersionType) ActionTestUtils(org.opensearch.action.support.ActionTestUtils) IndexSettings(org.opensearch.index.IndexSettings) MetadataFieldMapper(org.opensearch.index.mapper.MetadataFieldMapper) DocWriteResponse(org.opensearch.action.DocWriteResponse) UpdateRequest(org.opensearch.action.update.UpdateRequest) Mockito.any(org.mockito.Mockito.any) Matchers.containsString(org.hamcrest.Matchers.containsString) RefreshPolicy(org.opensearch.action.support.WriteRequest.RefreshPolicy) Names(org.opensearch.threadpool.ThreadPool.Names) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) Matchers.arrayWithSize(org.hamcrest.Matchers.arrayWithSize) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) UpdateResponse(org.opensearch.action.update.UpdateResponse) ThreadPool(org.opensearch.threadpool.ThreadPool) CoreMatchers.not(org.hamcrest.CoreMatchers.not) DocWriteRequest(org.opensearch.action.DocWriteRequest) Mockito.spy(org.mockito.Mockito.spy) WritePrimaryResult(org.opensearch.action.support.replication.TransportWriteAction.WritePrimaryResult) Requests(org.opensearch.client.Requests) IndexShard(org.opensearch.index.shard.IndexShard) Mapping(org.opensearch.index.mapper.Mapping) DeleteResponse(org.opensearch.action.delete.DeleteResponse) Translog(org.opensearch.index.translog.Translog) Mockito.anyLong(org.mockito.Mockito.anyLong) Versions(org.opensearch.common.lucene.uid.Versions) UpdateHelper(org.opensearch.action.update.UpdateHelper) VersionConflictEngineException(org.opensearch.index.engine.VersionConflictEngineException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) RootObjectMapper(org.opensearch.index.mapper.RootObjectMapper) Mockito.verify(org.mockito.Mockito.verify) ShardId(org.opensearch.index.shard.ShardId) IndexRequest(org.opensearch.action.index.IndexRequest) Collections(java.util.Collections) IndexShard(org.opensearch.index.shard.IndexShard) IndexRequest(org.opensearch.action.index.IndexRequest) CountDownLatch(java.util.concurrent.CountDownLatch) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) OpenSearchStatusException(org.opensearch.OpenSearchStatusException) OpenSearchException(org.opensearch.OpenSearchException) VersionConflictEngineException(org.opensearch.index.engine.VersionConflictEngineException) IOException(java.io.IOException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener)

Example 18 with DocWriteRequest

use of org.opensearch.action.DocWriteRequest in project OpenSearch by opensearch-project.

the class BulkPrimaryExecutionContextTests method testAbortedSkipped.

public void testAbortedSkipped() {
    BulkShardRequest shardRequest = generateRandomRequest();
    ArrayList<DocWriteRequest<?>> nonAbortedRequests = new ArrayList<>();
    for (BulkItemRequest request : shardRequest.items()) {
        if (randomBoolean()) {
            request.abort("index", new OpenSearchException("bla"));
        } else {
            nonAbortedRequests.add(request.request());
        }
    }
    ArrayList<DocWriteRequest<?>> visitedRequests = new ArrayList<>();
    for (BulkPrimaryExecutionContext context = new BulkPrimaryExecutionContext(shardRequest, null); context.hasMoreOperationsToExecute(); ) {
        visitedRequests.add(context.getCurrent());
        context.setRequestToExecute(context.getCurrent());
        // using failures prevents caring about types
        context.markOperationAsExecuted(new Engine.IndexResult(new OpenSearchException("bla"), 1));
        context.markAsCompleted(context.getExecutionResult());
    }
    assertThat(visitedRequests, equalTo(nonAbortedRequests));
}
Also used : ArrayList(java.util.ArrayList) OpenSearchException(org.opensearch.OpenSearchException) DocWriteRequest(org.opensearch.action.DocWriteRequest) Engine(org.opensearch.index.engine.Engine)

Example 19 with DocWriteRequest

use of org.opensearch.action.DocWriteRequest in project OpenSearch by opensearch-project.

the class IndexNameExpressionResolverTests method testConcreteWriteIndexWithNoWriteIndexWithMultipleIndices.

public void testConcreteWriteIndexWithNoWriteIndexWithMultipleIndices() {
    Metadata.Builder mdBuilder = Metadata.builder().put(indexBuilder("test-0").state(State.OPEN).putAlias(AliasMetadata.builder("test-alias").writeIndex(randomFrom(false, null)))).put(indexBuilder("test-1").state(State.OPEN).putAlias(AliasMetadata.builder("test-alias").writeIndex(randomFrom(false, null))));
    ClusterState state = ClusterState.builder(new ClusterName("_name")).metadata(mdBuilder).build();
    String[] strings = indexNameExpressionResolver.indexAliases(state, "test-0", x -> true, true, new HashSet<>(Arrays.asList("test-0", "test-1", "test-alias")));
    Arrays.sort(strings);
    assertArrayEquals(new String[] { "test-alias" }, strings);
    DocWriteRequest request = randomFrom(new IndexRequest("test-alias"), new UpdateRequest("test-alias", "_id"), new DeleteRequest("test-alias"));
    IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> indexNameExpressionResolver.concreteWriteIndex(state, request.indicesOptions(), request.indices()[0], false, false));
    assertThat(exception.getMessage(), equalTo("no write index is defined for alias [test-alias]." + " The write index may be explicitly disabled using is_write_index=false or the alias points to multiple" + " indices without one being designated as a write index"));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) UpdateRequest(org.opensearch.action.update.UpdateRequest) ClusterName(org.opensearch.cluster.ClusterName) Matchers.containsString(org.hamcrest.Matchers.containsString) DocWriteRequest(org.opensearch.action.DocWriteRequest) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest)

Example 20 with DocWriteRequest

use of org.opensearch.action.DocWriteRequest in project OpenSearch by opensearch-project.

the class IndexNameExpressionResolverTests method testConcreteWriteIndexWithNoWriteIndexWithSingleIndex.

public void testConcreteWriteIndexWithNoWriteIndexWithSingleIndex() {
    Metadata.Builder mdBuilder = Metadata.builder().put(indexBuilder("test-0").state(State.OPEN).putAlias(AliasMetadata.builder("test-alias").writeIndex(false)));
    ClusterState state = ClusterState.builder(new ClusterName("_name")).metadata(mdBuilder).build();
    String[] strings = indexNameExpressionResolver.indexAliases(state, "test-0", x -> true, true, new HashSet<>(Arrays.asList("test-0", "test-alias")));
    Arrays.sort(strings);
    assertArrayEquals(new String[] { "test-alias" }, strings);
    DocWriteRequest request = randomFrom(new IndexRequest("test-alias"), new UpdateRequest("test-alias", "_id"), new DeleteRequest("test-alias"));
    IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> indexNameExpressionResolver.concreteWriteIndex(state, request.indicesOptions(), request.indices()[0], false, false));
    assertThat(exception.getMessage(), equalTo("no write index is defined for alias [test-alias]." + " The write index may be explicitly disabled using is_write_index=false or the alias points to multiple" + " indices without one being designated as a write index"));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) UpdateRequest(org.opensearch.action.update.UpdateRequest) ClusterName(org.opensearch.cluster.ClusterName) Matchers.containsString(org.hamcrest.Matchers.containsString) DocWriteRequest(org.opensearch.action.DocWriteRequest) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest)

Aggregations

DocWriteRequest (org.opensearch.action.DocWriteRequest)22 IndexRequest (org.opensearch.action.index.IndexRequest)16 UpdateRequest (org.opensearch.action.update.UpdateRequest)15 DeleteRequest (org.opensearch.action.delete.DeleteRequest)11 ClusterState (org.opensearch.cluster.ClusterState)7 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)5 ClusterService (org.opensearch.cluster.service.ClusterService)5 ThreadPool (org.opensearch.threadpool.ThreadPool)5 Names (org.opensearch.threadpool.ThreadPool.Names)5 Collections (java.util.Collections)4 Collections.emptyMap (java.util.Collections.emptyMap)4 List (java.util.List)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Mockito.any (org.mockito.Mockito.any)4 Version (org.opensearch.Version)4 ActionListener (org.opensearch.action.ActionListener)4 IndexResponse (org.opensearch.action.index.IndexResponse)4