Search in sources :

Example 1 with DocWriteResponse

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

the class BulkPrimaryExecutionContext method markOperationAsExecuted.

/**
 * the current operation has been executed on the primary with the specified result
 */
public void markOperationAsExecuted(Engine.Result result) {
    assertInvariants(ItemProcessingState.TRANSLATED);
    final BulkItemRequest current = getCurrentItem();
    DocWriteRequest docWriteRequest = getRequestToExecute();
    switch(result.getResultType()) {
        case SUCCESS:
            final DocWriteResponse response;
            if (result.getOperationType() == Engine.Operation.TYPE.INDEX) {
                Engine.IndexResult indexResult = (Engine.IndexResult) result;
                response = new IndexResponse(primary.shardId(), requestToExecute.id(), result.getSeqNo(), result.getTerm(), indexResult.getVersion(), indexResult.isCreated());
            } else if (result.getOperationType() == Engine.Operation.TYPE.DELETE) {
                Engine.DeleteResult deleteResult = (Engine.DeleteResult) result;
                response = new DeleteResponse(primary.shardId(), requestToExecute.id(), deleteResult.getSeqNo(), result.getTerm(), deleteResult.getVersion(), deleteResult.isFound());
            } else {
                throw new AssertionError("unknown result type :" + result.getResultType());
            }
            executionResult = new BulkItemResponse(current.id(), current.request().opType(), response);
            // set a blank ShardInfo so we can safely send it to the replicas. We won't use it in the real response though.
            executionResult.getResponse().setShardInfo(new ReplicationResponse.ShardInfo());
            locationToSync = TransportWriteAction.locationToSync(locationToSync, result.getTranslogLocation());
            break;
        case FAILURE:
            executionResult = new BulkItemResponse(current.id(), docWriteRequest.opType(), // concrete index instead of an alias if used!
            new BulkItemResponse.Failure(request.index(), docWriteRequest.id(), result.getFailure(), result.getSeqNo(), result.getTerm()));
            break;
        default:
            throw new AssertionError("unknown result type for " + getCurrentItem() + ": " + result.getResultType());
    }
    currentItemState = ItemProcessingState.EXECUTED;
}
Also used : DocWriteResponse(org.opensearch.action.DocWriteResponse) ReplicationResponse(org.opensearch.action.support.replication.ReplicationResponse) DeleteResponse(org.opensearch.action.delete.DeleteResponse) IndexResponse(org.opensearch.action.index.IndexResponse) DocWriteRequest(org.opensearch.action.DocWriteRequest) Engine(org.opensearch.index.engine.Engine)

Example 2 with DocWriteResponse

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

the class TransportShardBulkActionTests method testUpdateRequestWithSuccess.

public void testUpdateRequestWithSuccess() throws Exception {
    IndexSettings indexSettings = new IndexSettings(indexMetadata(), Settings.EMPTY);
    DocWriteRequest<UpdateRequest> writeRequest = new UpdateRequest("index", "id").doc(Requests.INDEX_CONTENT_TYPE, "field", "value");
    BulkItemRequest primaryRequest = new BulkItemRequest(0, writeRequest);
    IndexRequest updateResponse = new IndexRequest("index").id("id").source(Requests.INDEX_CONTENT_TYPE, "field", "value");
    boolean created = randomBoolean();
    Translog.Location resultLocation = new Translog.Location(42, 42, 42);
    Engine.IndexResult indexResult = new FakeIndexResult(1, 1, 13, created, resultLocation);
    IndexShard shard = mock(IndexShard.class);
    when(shard.applyIndexOperationOnPrimary(anyLong(), any(), any(), anyLong(), anyLong(), anyLong(), anyBoolean())).thenReturn(indexResult);
    when(shard.indexSettings()).thenReturn(indexSettings);
    when(shard.shardId()).thenReturn(shardId);
    UpdateHelper updateHelper = mock(UpdateHelper.class);
    when(updateHelper.prepare(any(), eq(shard), any())).thenReturn(new UpdateHelper.Result(updateResponse, created ? DocWriteResponse.Result.CREATED : DocWriteResponse.Result.UPDATED, Collections.singletonMap("field", "value"), Requests.INDEX_CONTENT_TYPE));
    BulkItemRequest[] items = new BulkItemRequest[] { primaryRequest };
    BulkShardRequest bulkShardRequest = new BulkShardRequest(shardId, RefreshPolicy.NONE, items);
    randomlySetIgnoredPrimaryResponse(primaryRequest);
    BulkPrimaryExecutionContext context = new BulkPrimaryExecutionContext(bulkShardRequest, shard);
    TransportShardBulkAction.executeBulkItemRequest(context, updateHelper, threadPool::absoluteTimeInMillis, new NoopMappingUpdatePerformer(), listener -> {
    }, ASSERTING_DONE_LISTENER);
    assertFalse(context.hasMoreOperationsToExecute());
    // Check that the translog is successfully advanced
    assertThat(context.getLocationToSync(), equalTo(resultLocation));
    assertThat(bulkShardRequest.items()[0].request(), equalTo(updateResponse));
    // Since this was not a conflict failure, the primary response
    // should be filled out with the failure information
    BulkItemResponse primaryResponse = bulkShardRequest.items()[0].getPrimaryResponse();
    assertThat(primaryResponse.getItemId(), equalTo(0));
    assertThat(primaryResponse.getId(), equalTo("id"));
    assertThat(primaryResponse.getOpType(), equalTo(DocWriteRequest.OpType.UPDATE));
    DocWriteResponse response = primaryResponse.getResponse();
    assertThat(response.status(), equalTo(created ? RestStatus.CREATED : RestStatus.OK));
    assertThat(response.getSeqNo(), equalTo(13L));
}
Also used : UpdateRequest(org.opensearch.action.update.UpdateRequest) IndexSettings(org.opensearch.index.IndexSettings) IndexShard(org.opensearch.index.shard.IndexShard) DocWriteResponse(org.opensearch.action.DocWriteResponse) IndexRequest(org.opensearch.action.index.IndexRequest) Translog(org.opensearch.index.translog.Translog) UpdateHelper(org.opensearch.action.update.UpdateHelper) Engine(org.opensearch.index.engine.Engine)

Example 3 with DocWriteResponse

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

the class TransportShardBulkActionTests method testRetries.

public void testRetries() throws Exception {
    IndexSettings indexSettings = new IndexSettings(indexMetadata(), Settings.EMPTY);
    UpdateRequest writeRequest = new UpdateRequest("index", "id").doc(Requests.INDEX_CONTENT_TYPE, "field", "value");
    // the beating will continue until success has come.
    writeRequest.retryOnConflict(Integer.MAX_VALUE);
    BulkItemRequest primaryRequest = new BulkItemRequest(0, writeRequest);
    IndexRequest updateResponse = new IndexRequest("index").id("id").source(Requests.INDEX_CONTENT_TYPE, "field", "value");
    Exception err = new VersionConflictEngineException(shardId, "id", "I'm conflicted <(;_;)>");
    Engine.IndexResult conflictedResult = new Engine.IndexResult(err, 0);
    Engine.IndexResult mappingUpdate = new Engine.IndexResult(new Mapping(null, mock(RootObjectMapper.class), new MetadataFieldMapper[0], Collections.emptyMap()));
    Translog.Location resultLocation = new Translog.Location(42, 42, 42);
    Engine.IndexResult success = new FakeIndexResult(1, 1, 13, true, resultLocation);
    IndexShard shard = mock(IndexShard.class);
    when(shard.applyIndexOperationOnPrimary(anyLong(), any(), any(), anyLong(), anyLong(), anyLong(), anyBoolean())).thenAnswer(ir -> {
        if (randomBoolean()) {
            return conflictedResult;
        }
        if (randomBoolean()) {
            return mappingUpdate;
        } else {
            return success;
        }
    });
    when(shard.indexSettings()).thenReturn(indexSettings);
    when(shard.shardId()).thenReturn(shardId);
    when(shard.mapperService()).thenReturn(mock(MapperService.class));
    UpdateHelper updateHelper = mock(UpdateHelper.class);
    when(updateHelper.prepare(any(), eq(shard), any())).thenReturn(new UpdateHelper.Result(updateResponse, randomBoolean() ? DocWriteResponse.Result.CREATED : DocWriteResponse.Result.UPDATED, Collections.singletonMap("field", "value"), Requests.INDEX_CONTENT_TYPE));
    BulkItemRequest[] items = new BulkItemRequest[] { primaryRequest };
    BulkShardRequest bulkShardRequest = new BulkShardRequest(shardId, RefreshPolicy.NONE, items);
    final CountDownLatch latch = new CountDownLatch(1);
    TransportShardBulkAction.performOnPrimary(bulkShardRequest, shard, updateHelper, threadPool::absoluteTimeInMillis, new NoopMappingUpdatePerformer(), listener -> listener.onResponse(null), new LatchedActionListener<>(ActionTestUtils.assertNoFailureListener(result -> {
        assertThat(((WritePrimaryResult<BulkShardRequest, BulkShardResponse>) result).location, equalTo(resultLocation));
        BulkItemResponse primaryResponse = result.replicaRequest().items()[0].getPrimaryResponse();
        assertThat(primaryResponse.getItemId(), equalTo(0));
        assertThat(primaryResponse.getId(), equalTo("id"));
        assertThat(primaryResponse.getOpType(), equalTo(DocWriteRequest.OpType.UPDATE));
        DocWriteResponse response = primaryResponse.getResponse();
        assertThat(response.status(), equalTo(RestStatus.CREATED));
        assertThat(response.getSeqNo(), equalTo(13L));
    }), latch), threadPool, Names.WRITE);
    latch.await();
}
Also used : IndexSettings(org.opensearch.index.IndexSettings) Mapping(org.opensearch.index.mapper.Mapping) IndexRequest(org.opensearch.action.index.IndexRequest) Translog(org.opensearch.index.translog.Translog) UpdateHelper(org.opensearch.action.update.UpdateHelper) VersionConflictEngineException(org.opensearch.index.engine.VersionConflictEngineException) Engine(org.opensearch.index.engine.Engine) UpdateRequest(org.opensearch.action.update.UpdateRequest) IndexShard(org.opensearch.index.shard.IndexShard) DocWriteResponse(org.opensearch.action.DocWriteResponse) 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) MetadataFieldMapper(org.opensearch.index.mapper.MetadataFieldMapper) MapperService(org.opensearch.index.mapper.MapperService)

Example 4 with DocWriteResponse

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

the class TransportShardBulkActionTests method testUpdateWithDelete.

public void testUpdateWithDelete() throws Exception {
    IndexSettings indexSettings = new IndexSettings(indexMetadata(), Settings.EMPTY);
    DocWriteRequest<UpdateRequest> writeRequest = new UpdateRequest("index", "id").doc(Requests.INDEX_CONTENT_TYPE, "field", "value");
    BulkItemRequest primaryRequest = new BulkItemRequest(0, writeRequest);
    DeleteRequest updateResponse = new DeleteRequest("index", "id");
    boolean found = randomBoolean();
    Translog.Location resultLocation = new Translog.Location(42, 42, 42);
    final long resultSeqNo = 13;
    Engine.DeleteResult deleteResult = new FakeDeleteResult(1, 1, resultSeqNo, found, resultLocation);
    IndexShard shard = mock(IndexShard.class);
    when(shard.applyDeleteOperationOnPrimary(anyLong(), any(), any(), any(), anyLong(), anyLong())).thenReturn(deleteResult);
    when(shard.indexSettings()).thenReturn(indexSettings);
    when(shard.shardId()).thenReturn(shardId);
    UpdateHelper updateHelper = mock(UpdateHelper.class);
    when(updateHelper.prepare(any(), eq(shard), any())).thenReturn(new UpdateHelper.Result(updateResponse, DocWriteResponse.Result.DELETED, Collections.singletonMap("field", "value"), Requests.INDEX_CONTENT_TYPE));
    BulkItemRequest[] items = new BulkItemRequest[] { primaryRequest };
    BulkShardRequest bulkShardRequest = new BulkShardRequest(shardId, RefreshPolicy.NONE, items);
    randomlySetIgnoredPrimaryResponse(primaryRequest);
    BulkPrimaryExecutionContext context = new BulkPrimaryExecutionContext(bulkShardRequest, shard);
    TransportShardBulkAction.executeBulkItemRequest(context, updateHelper, threadPool::absoluteTimeInMillis, new NoopMappingUpdatePerformer(), listener -> listener.onResponse(null), ASSERTING_DONE_LISTENER);
    assertFalse(context.hasMoreOperationsToExecute());
    // Check that the translog is successfully advanced
    assertThat(context.getLocationToSync(), equalTo(resultLocation));
    assertThat(bulkShardRequest.items()[0].request(), equalTo(updateResponse));
    BulkItemResponse primaryResponse = bulkShardRequest.items()[0].getPrimaryResponse();
    assertThat(primaryResponse.getItemId(), equalTo(0));
    assertThat(primaryResponse.getId(), equalTo("id"));
    assertThat(primaryResponse.getOpType(), equalTo(DocWriteRequest.OpType.UPDATE));
    DocWriteResponse response = primaryResponse.getResponse();
    assertThat(response.status(), equalTo(RestStatus.OK));
    assertThat(response.getSeqNo(), equalTo(resultSeqNo));
}
Also used : UpdateRequest(org.opensearch.action.update.UpdateRequest) IndexSettings(org.opensearch.index.IndexSettings) IndexShard(org.opensearch.index.shard.IndexShard) DocWriteResponse(org.opensearch.action.DocWriteResponse) Translog(org.opensearch.index.translog.Translog) UpdateHelper(org.opensearch.action.update.UpdateHelper) DeleteRequest(org.opensearch.action.delete.DeleteRequest) Engine(org.opensearch.index.engine.Engine)

Example 5 with DocWriteResponse

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

the class TransportShardBulkActionTests method testNoopUpdateRequest.

public void testNoopUpdateRequest() throws Exception {
    DocWriteRequest<UpdateRequest> writeRequest = new UpdateRequest("index", "id").doc(Requests.INDEX_CONTENT_TYPE, "field", "value");
    BulkItemRequest primaryRequest = new BulkItemRequest(0, writeRequest);
    DocWriteResponse noopUpdateResponse = new UpdateResponse(shardId, "id", 0, 2, 1, DocWriteResponse.Result.NOOP);
    IndexShard shard = mock(IndexShard.class);
    UpdateHelper updateHelper = mock(UpdateHelper.class);
    when(updateHelper.prepare(any(), eq(shard), any())).thenReturn(new UpdateHelper.Result(noopUpdateResponse, DocWriteResponse.Result.NOOP, Collections.singletonMap("field", "value"), Requests.INDEX_CONTENT_TYPE));
    BulkItemRequest[] items = new BulkItemRequest[] { primaryRequest };
    BulkShardRequest bulkShardRequest = new BulkShardRequest(shardId, RefreshPolicy.NONE, items);
    randomlySetIgnoredPrimaryResponse(primaryRequest);
    BulkPrimaryExecutionContext context = new BulkPrimaryExecutionContext(bulkShardRequest, shard);
    TransportShardBulkAction.executeBulkItemRequest(context, updateHelper, threadPool::absoluteTimeInMillis, new NoopMappingUpdatePerformer(), listener -> {
    }, ASSERTING_DONE_LISTENER);
    assertFalse(context.hasMoreOperationsToExecute());
    // Basically nothing changes in the request since it's a noop
    assertThat(context.getLocationToSync(), nullValue());
    BulkItemResponse primaryResponse = bulkShardRequest.items()[0].getPrimaryResponse();
    assertThat(primaryResponse.getItemId(), equalTo(0));
    assertThat(primaryResponse.getId(), equalTo("id"));
    assertThat(primaryResponse.getOpType(), equalTo(DocWriteRequest.OpType.UPDATE));
    assertThat(primaryResponse.getResponse(), equalTo(noopUpdateResponse));
    assertThat(primaryResponse.getResponse().getResult(), equalTo(DocWriteResponse.Result.NOOP));
    assertThat(bulkShardRequest.items().length, equalTo(1));
    // check that bulk item was not mutated
    assertEquals(primaryRequest, bulkShardRequest.items()[0]);
    assertThat(primaryResponse.getResponse().getSeqNo(), equalTo(0L));
}
Also used : UpdateRequest(org.opensearch.action.update.UpdateRequest) DocWriteResponse(org.opensearch.action.DocWriteResponse) IndexShard(org.opensearch.index.shard.IndexShard) UpdateResponse(org.opensearch.action.update.UpdateResponse) UpdateHelper(org.opensearch.action.update.UpdateHelper)

Aggregations

DocWriteResponse (org.opensearch.action.DocWriteResponse)7 UpdateHelper (org.opensearch.action.update.UpdateHelper)4 UpdateRequest (org.opensearch.action.update.UpdateRequest)4 Engine (org.opensearch.index.engine.Engine)4 IndexShard (org.opensearch.index.shard.IndexShard)4 IndexSettings (org.opensearch.index.IndexSettings)3 Translog (org.opensearch.index.translog.Translog)3 IndexRequest (org.opensearch.action.index.IndexRequest)2 IOException (java.io.IOException)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 OpenSearchException (org.opensearch.OpenSearchException)1 OpenSearchStatusException (org.opensearch.OpenSearchStatusException)1 DocWriteRequest (org.opensearch.action.DocWriteRequest)1 Failure (org.opensearch.action.bulk.BulkItemResponse.Failure)1 DeleteRequest (org.opensearch.action.delete.DeleteRequest)1 DeleteResponse (org.opensearch.action.delete.DeleteResponse)1 IndexResponse (org.opensearch.action.index.IndexResponse)1 ReplicationResponse (org.opensearch.action.support.replication.ReplicationResponse)1 UpdateResponse (org.opensearch.action.update.UpdateResponse)1