Search in sources :

Example 16 with DocWriteRequest

use of org.elasticsearch.action.DocWriteRequest in project elasticsearch by elastic.

the class TransportShardBulkAction method executeBulkItemRequest.

/** Executes bulk item requests and handles request execution exceptions */
static Translog.Location executeBulkItemRequest(IndexMetaData metaData, IndexShard primary, BulkShardRequest request, Translog.Location location, int requestIndex, UpdateHelper updateHelper, LongSupplier nowInMillisSupplier, final MappingUpdatePerformer mappingUpdater) throws Exception {
    final DocWriteRequest itemRequest = request.items()[requestIndex].request();
    final DocWriteRequest.OpType opType = itemRequest.opType();
    final BulkItemResultHolder responseHolder;
    switch(itemRequest.opType()) {
        case CREATE:
        case INDEX:
            responseHolder = executeIndexRequest((IndexRequest) itemRequest, request.items()[requestIndex], primary, mappingUpdater);
            break;
        case UPDATE:
            responseHolder = executeUpdateRequest((UpdateRequest) itemRequest, primary, metaData, request, requestIndex, updateHelper, nowInMillisSupplier, mappingUpdater);
            break;
        case DELETE:
            responseHolder = executeDeleteRequest((DeleteRequest) itemRequest, request.items()[requestIndex], primary);
            break;
        default:
            throw new IllegalStateException("unexpected opType [" + itemRequest.opType() + "] found");
    }
    final BulkItemRequest replicaRequest = responseHolder.replicaRequest;
    // update the bulk item request because update request execution can mutate the bulk item request
    request.items()[requestIndex] = replicaRequest;
    // Modify the replica request, if needed, and return a new translog location
    location = updateReplicaRequest(responseHolder, opType, location, request);
    assert replicaRequest.getPrimaryResponse() != null : "replica request must have a primary response";
    return location;
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 17 with DocWriteRequest

use of org.elasticsearch.action.DocWriteRequest in project elasticsearch by elastic.

the class TransportBulkActionIngestTests method testIngestLocal.

public void testIngestLocal() throws Exception {
    Exception exception = new Exception("fake exception");
    BulkRequest bulkRequest = new BulkRequest();
    IndexRequest indexRequest1 = new IndexRequest("index", "type", "id");
    indexRequest1.source(Collections.emptyMap());
    indexRequest1.setPipeline("testpipeline");
    IndexRequest indexRequest2 = new IndexRequest("index", "type", "id");
    indexRequest2.source(Collections.emptyMap());
    indexRequest2.setPipeline("testpipeline");
    bulkRequest.add(indexRequest1);
    bulkRequest.add(indexRequest2);
    AtomicBoolean responseCalled = new AtomicBoolean(false);
    AtomicBoolean failureCalled = new AtomicBoolean(false);
    action.execute(null, bulkRequest, ActionListener.wrap(response -> {
        BulkItemResponse itemResponse = response.iterator().next();
        assertThat(itemResponse.getFailure().getMessage(), containsString("fake exception"));
        responseCalled.set(true);
    }, e -> {
        assertThat(e, sameInstance(exception));
        failureCalled.set(true);
    }));
    // check failure works, and passes through to the listener
    // haven't executed yet
    assertFalse(action.isExecuted);
    assertFalse(responseCalled.get());
    assertFalse(failureCalled.get());
    verify(executionService).executeBulkRequest(bulkDocsItr.capture(), failureHandler.capture(), completionHandler.capture());
    completionHandler.getValue().accept(exception);
    assertTrue(failureCalled.get());
    // now check success
    Iterator<DocWriteRequest> req = bulkDocsItr.getValue().iterator();
    // have an exception for our one index request
    failureHandler.getValue().accept((IndexRequest) req.next(), exception);
    // this is done by the real pipeline execution service when processing
    indexRequest2.setPipeline(null);
    completionHandler.getValue().accept(null);
    assertTrue(action.isExecuted);
    // listener would only be called by real index action, not our mocked one
    assertFalse(responseCalled.get());
    verifyZeroInteractions(transportService);
}
Also used : IngestService(org.elasticsearch.ingest.IngestService) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) PipelineExecutionService(org.elasticsearch.ingest.PipelineExecutionService) ClusterService(org.elasticsearch.cluster.service.ClusterService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Captor(org.mockito.Captor) Supplier(java.util.function.Supplier) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) MockitoAnnotations(org.mockito.MockitoAnnotations) IndexRequest(org.elasticsearch.action.index.IndexRequest) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Settings(org.elasticsearch.common.settings.Settings) ArgumentCaptor(org.mockito.ArgumentCaptor) Matchers.eq(org.mockito.Matchers.eq) Mockito.doAnswer(org.mockito.Mockito.doAnswer) BiConsumer(java.util.function.BiConsumer) IndexResponse(org.elasticsearch.action.index.IndexResponse) ThreadPool(org.elasticsearch.threadpool.ThreadPool) IndicesService(org.elasticsearch.indices.IndicesService) ESTestCase(org.elasticsearch.test.ESTestCase) TransportService(org.elasticsearch.transport.TransportService) ClusterStateApplier(org.elasticsearch.cluster.ClusterStateApplier) Before(org.junit.Before) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) ActionFilters(org.elasticsearch.action.support.ActionFilters) Iterator(java.util.Iterator) AtomicArray(org.elasticsearch.common.util.concurrent.AtomicArray) Mockito.when(org.mockito.Mockito.when) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Matchers.any(org.mockito.Matchers.any) IndexAction(org.elasticsearch.action.index.IndexAction) Mockito.never(org.mockito.Mockito.never) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) ShardStateAction(org.elasticsearch.cluster.action.shard.ShardStateAction) TransportResponseHandler(org.elasticsearch.transport.TransportResponseHandler) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Task(org.elasticsearch.tasks.Task) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.reset(org.mockito.Mockito.reset) ActionListener(org.elasticsearch.action.ActionListener) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IndexRequest(org.elasticsearch.action.index.IndexRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest)

Example 18 with DocWriteRequest

use of org.elasticsearch.action.DocWriteRequest in project elasticsearch by elastic.

the class TransportShardBulkActionTests method testUpdateReplicaRequestWithConflictFailure.

public void testUpdateReplicaRequestWithConflictFailure() throws Exception {
    DocWriteRequest writeRequest = new IndexRequest("index", "type", "id").source(Requests.INDEX_CONTENT_TYPE, "field", "value");
    BulkItemRequest replicaRequest = new BulkItemRequest(0, writeRequest);
    Exception err = new VersionConflictEngineException(shardId, "type", "id", "I'm conflicted <(;_;)>");
    Engine.IndexResult indexResult = new Engine.IndexResult(err, 0, 0);
    BulkItemResultHolder failedResults = new BulkItemResultHolder(null, indexResult, replicaRequest);
    Translog.Location location = new Translog.Location(0, 0, 0);
    BulkItemRequest[] items = new BulkItemRequest[0];
    BulkShardRequest bulkShardRequest = new BulkShardRequest(shardId, RefreshPolicy.NONE, items);
    Translog.Location newLocation = TransportShardBulkAction.updateReplicaRequest(failedResults, DocWriteRequest.OpType.UPDATE, location, bulkShardRequest);
    BulkItemResponse primaryResponse = replicaRequest.getPrimaryResponse();
    // Since this was not a conflict failure, the primary response
    // should be filled out with the failure information
    assertThat(newLocation, equalTo(location));
    assertThat(primaryResponse.getItemId(), equalTo(0));
    assertThat(primaryResponse.getId(), equalTo("id"));
    assertThat(primaryResponse.getOpType(), equalTo(DocWriteRequest.OpType.INDEX));
    assertTrue(primaryResponse.isFailed());
    assertThat(primaryResponse.getFailureMessage(), containsString("I'm conflicted <(;_;)>"));
    BulkItemResponse.Failure failure = primaryResponse.getFailure();
    assertThat(failure.getIndex(), equalTo("index"));
    assertThat(failure.getType(), equalTo("type"));
    assertThat(failure.getId(), equalTo("id"));
    assertThat(failure.getCause(), equalTo(err));
    assertThat(failure.getStatus(), equalTo(RestStatus.CONFLICT));
}
Also used : IndexRequest(org.elasticsearch.action.index.IndexRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) IOException(java.io.IOException) Translog(org.elasticsearch.index.translog.Translog) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) Engine(org.elasticsearch.index.engine.Engine)

Example 19 with DocWriteRequest

use of org.elasticsearch.action.DocWriteRequest in project elasticsearch by elastic.

the class TransportShardBulkActionTests method testUpdateReplicaRequestWithFailure.

public void testUpdateReplicaRequestWithFailure() throws Exception {
    DocWriteRequest writeRequest = new IndexRequest("index", "type", "id").source(Requests.INDEX_CONTENT_TYPE, "field", "value");
    BulkItemRequest replicaRequest = new BulkItemRequest(0, writeRequest);
    Exception err = new ElasticsearchException("I'm dead <(x.x)>");
    Engine.IndexResult indexResult = new Engine.IndexResult(err, 0, 0);
    BulkItemResultHolder failedResults = new BulkItemResultHolder(null, indexResult, replicaRequest);
    Translog.Location location = new Translog.Location(0, 0, 0);
    BulkItemRequest[] items = new BulkItemRequest[0];
    BulkShardRequest bulkShardRequest = new BulkShardRequest(shardId, RefreshPolicy.NONE, items);
    Translog.Location newLocation = TransportShardBulkAction.updateReplicaRequest(failedResults, DocWriteRequest.OpType.UPDATE, location, bulkShardRequest);
    BulkItemResponse primaryResponse = replicaRequest.getPrimaryResponse();
    // Since this was not a conflict failure, the primary response
    // should be filled out with the failure information
    assertThat(newLocation, equalTo(location));
    assertThat(primaryResponse.getItemId(), equalTo(0));
    assertThat(primaryResponse.getId(), equalTo("id"));
    assertThat(primaryResponse.getOpType(), equalTo(DocWriteRequest.OpType.INDEX));
    assertTrue(primaryResponse.isFailed());
    assertThat(primaryResponse.getFailureMessage(), containsString("I'm dead <(x.x)>"));
    BulkItemResponse.Failure failure = primaryResponse.getFailure();
    assertThat(failure.getIndex(), equalTo("index"));
    assertThat(failure.getType(), equalTo("type"));
    assertThat(failure.getId(), equalTo("id"));
    assertThat(failure.getCause(), equalTo(err));
    assertThat(failure.getStatus(), equalTo(RestStatus.INTERNAL_SERVER_ERROR));
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexRequest(org.elasticsearch.action.index.IndexRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) IOException(java.io.IOException) Translog(org.elasticsearch.index.translog.Translog) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) Engine(org.elasticsearch.index.engine.Engine)

Example 20 with DocWriteRequest

use of org.elasticsearch.action.DocWriteRequest in project elasticsearch by elastic.

the class TransportShardBulkActionTests method testNoopUpdateReplicaRequest.

public void testNoopUpdateReplicaRequest() throws Exception {
    DocWriteRequest writeRequest = new IndexRequest("index", "type", "id").source(Requests.INDEX_CONTENT_TYPE, "field", "value");
    BulkItemRequest replicaRequest = new BulkItemRequest(0, writeRequest);
    DocWriteResponse noopUpdateResponse = new UpdateResponse(shardId, "index", "id", 0, DocWriteResponse.Result.NOOP);
    BulkItemResultHolder noopResults = new BulkItemResultHolder(noopUpdateResponse, null, replicaRequest);
    Translog.Location location = new Translog.Location(0, 0, 0);
    BulkItemRequest[] items = new BulkItemRequest[0];
    BulkShardRequest bulkShardRequest = new BulkShardRequest(shardId, RefreshPolicy.NONE, items);
    Translog.Location newLocation = TransportShardBulkAction.updateReplicaRequest(noopResults, DocWriteRequest.OpType.UPDATE, location, bulkShardRequest);
    BulkItemResponse primaryResponse = replicaRequest.getPrimaryResponse();
    // Basically nothing changes in the request since it's a noop
    assertThat(newLocation, equalTo(location));
    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));
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) Translog(org.elasticsearch.index.translog.Translog)

Aggregations

DocWriteRequest (org.elasticsearch.action.DocWriteRequest)23 IndexRequest (org.elasticsearch.action.index.IndexRequest)17 Translog (org.elasticsearch.index.translog.Translog)9 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)7 VersionConflictEngineException (org.elasticsearch.index.engine.VersionConflictEngineException)7 IOException (java.io.IOException)6 DocWriteResponse (org.elasticsearch.action.DocWriteResponse)6 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)6 ElasticsearchException (org.elasticsearch.ElasticsearchException)5 IndexResponse (org.elasticsearch.action.index.IndexResponse)4 UpdateHelper (org.elasticsearch.action.update.UpdateHelper)4 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)4 Engine (org.elasticsearch.index.engine.Engine)4 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)3 WriteRequest (org.elasticsearch.action.support.WriteRequest)3 BytesReference (org.elasticsearch.common.bytes.BytesReference)3 XContentType (org.elasticsearch.common.xcontent.XContentType)3 IndexShard (org.elasticsearch.index.shard.IndexShard)3 ArrayList (java.util.ArrayList)2 GetRequest (org.elasticsearch.action.get.GetRequest)2