Search in sources :

Example 6 with DocWriteRequest

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

the class TransportShardBulkAction method updateReplicaRequest.

// Visible for unit testing
static Translog.Location updateReplicaRequest(BulkItemResultHolder bulkItemResult, final DocWriteRequest.OpType opType, final Translog.Location originalLocation, BulkShardRequest request) {
    final Engine.Result operationResult = bulkItemResult.operationResult;
    final DocWriteResponse response = bulkItemResult.response;
    final BulkItemRequest replicaRequest = bulkItemResult.replicaRequest;
    if (operationResult == null) {
        // in case of noop update operation
        assert response.getResult() == DocWriteResponse.Result.NOOP : "only noop updates can have a null operation";
        replicaRequest.setPrimaryResponse(new BulkItemResponse(replicaRequest.id(), opType, response));
        return originalLocation;
    } else if (operationResult.hasFailure() == false) {
        BulkItemResponse primaryResponse = new BulkItemResponse(replicaRequest.id(), opType, response);
        replicaRequest.setPrimaryResponse(primaryResponse);
        // set a blank ShardInfo so we can safely send it to the replicas. We won't use it in the real response though.
        primaryResponse.getResponse().setShardInfo(new ShardInfo());
        // The operation was successful, advance the translog
        return locationToSync(originalLocation, operationResult.getTranslogLocation());
    } else {
        DocWriteRequest docWriteRequest = replicaRequest.request();
        Exception failure = operationResult.getFailure();
        if (isConflictException(failure)) {
            logger.trace((Supplier<?>) () -> new ParameterizedMessage("{} failed to execute bulk item ({}) {}", request.shardId(), docWriteRequest.opType().getLowercase(), request), failure);
        } else {
            logger.debug((Supplier<?>) () -> new ParameterizedMessage("{} failed to execute bulk item ({}) {}", request.shardId(), docWriteRequest.opType().getLowercase(), request), failure);
        }
        // then just use the response we got from the failed execution
        if (replicaRequest.getPrimaryResponse() == null || isConflictException(failure) == false) {
            replicaRequest.setPrimaryResponse(new BulkItemResponse(replicaRequest.id(), docWriteRequest.opType(), // concrete index instead of an alias if used!
            new BulkItemResponse.Failure(request.index(), docWriteRequest.type(), docWriteRequest.id(), failure)));
        }
        return originalLocation;
    }
}
Also used : DocWriteResponse(org.elasticsearch.action.DocWriteResponse) LongSupplier(java.util.function.LongSupplier) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) Engine(org.elasticsearch.index.engine.Engine) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) IOException(java.io.IOException) ShardInfo(org.elasticsearch.action.support.replication.ReplicationResponse.ShardInfo)

Example 7 with DocWriteRequest

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

the class TransportSingleItemBulkWriteAction method shardOperationOnPrimary.

@Override
protected WritePrimaryResult<Request, Response> shardOperationOnPrimary(Request request, final IndexShard primary) throws Exception {
    BulkItemRequest[] itemRequests = new BulkItemRequest[1];
    WriteRequest.RefreshPolicy refreshPolicy = request.getRefreshPolicy();
    request.setRefreshPolicy(WriteRequest.RefreshPolicy.NONE);
    itemRequests[0] = new BulkItemRequest(0, ((DocWriteRequest) request));
    BulkShardRequest bulkShardRequest = new BulkShardRequest(request.shardId(), refreshPolicy, itemRequests);
    WritePrimaryResult<BulkShardRequest, BulkShardResponse> bulkResult = shardBulkAction.shardOperationOnPrimary(bulkShardRequest, primary);
    assert bulkResult.finalResponseIfSuccessful.getResponses().length == 1 : "expected only one bulk shard response";
    BulkItemResponse itemResponse = bulkResult.finalResponseIfSuccessful.getResponses()[0];
    final Response response;
    final Exception failure;
    if (itemResponse.isFailed()) {
        failure = itemResponse.getFailure().getCause();
        response = null;
    } else {
        response = (Response) itemResponse.getResponse();
        failure = null;
    }
    return new WritePrimaryResult<>(request, response, bulkResult.location, failure, primary, logger);
}
Also used : WriteResponse(org.elasticsearch.action.support.WriteResponse) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) ReplicationResponse(org.elasticsearch.action.support.replication.ReplicationResponse) ReplicatedWriteRequest(org.elasticsearch.action.support.replication.ReplicatedWriteRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) WriteRequest(org.elasticsearch.action.support.WriteRequest)

Example 8 with DocWriteRequest

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

the class TransportShardBulkActionTests method testShouldExecuteReplicaItem.

public void testShouldExecuteReplicaItem() throws Exception {
    // Successful index request should be replicated
    DocWriteRequest writeRequest = new IndexRequest("index", "type", "id").source(Requests.INDEX_CONTENT_TYPE, "foo", "bar");
    DocWriteResponse response = new IndexResponse(shardId, "type", "id", 1, 1, randomBoolean());
    BulkItemRequest request = new BulkItemRequest(0, writeRequest);
    request.setPrimaryResponse(new BulkItemResponse(0, DocWriteRequest.OpType.INDEX, response));
    assertTrue(TransportShardBulkAction.shouldExecuteReplicaItem(request, 0));
    // Failed index requests should not be replicated (for now!)
    writeRequest = new IndexRequest("index", "type", "id").source(Requests.INDEX_CONTENT_TYPE, "foo", "bar");
    response = new IndexResponse(shardId, "type", "id", 1, 1, randomBoolean());
    request = new BulkItemRequest(0, writeRequest);
    request.setPrimaryResponse(new BulkItemResponse(0, DocWriteRequest.OpType.INDEX, new BulkItemResponse.Failure("test", "type", "id", new IllegalArgumentException("i died"))));
    assertFalse(TransportShardBulkAction.shouldExecuteReplicaItem(request, 0));
    // NOOP requests should not be replicated
    writeRequest = new UpdateRequest("index", "type", "id");
    response = new UpdateResponse(shardId, "type", "id", 1, DocWriteResponse.Result.NOOP);
    request = new BulkItemRequest(0, writeRequest);
    request.setPrimaryResponse(new BulkItemResponse(0, DocWriteRequest.OpType.UPDATE, response));
    assertFalse(TransportShardBulkAction.shouldExecuteReplicaItem(request, 0));
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 9 with DocWriteRequest

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

the class TransportShardBulkActionTests method testExecuteBulkDeleteRequest.

public void testExecuteBulkDeleteRequest() throws Exception {
    IndexMetaData metaData = indexMetaData();
    IndexShard shard = newStartedShard(true);
    BulkItemRequest[] items = new BulkItemRequest[1];
    DocWriteRequest writeRequest = new DeleteRequest("index", "type", "id");
    items[0] = new BulkItemRequest(0, writeRequest);
    BulkShardRequest bulkShardRequest = new BulkShardRequest(shardId, RefreshPolicy.NONE, items);
    Translog.Location location = new Translog.Location(0, 0, 0);
    UpdateHelper updateHelper = null;
    Translog.Location newLocation = TransportShardBulkAction.executeBulkItemRequest(metaData, shard, bulkShardRequest, location, 0, updateHelper, threadPool::absoluteTimeInMillis, new NoopMappingUpdatePerformer());
    // Translog changes, even though the document didn't exist
    assertThat(newLocation, not(location));
    BulkItemRequest replicaRequest = bulkShardRequest.items()[0];
    DocWriteRequest replicaDeleteRequest = replicaRequest.request();
    BulkItemResponse primaryResponse = replicaRequest.getPrimaryResponse();
    DeleteResponse response = primaryResponse.getResponse();
    // Any version can be matched on replica
    assertThat(replicaDeleteRequest.version(), equalTo(Versions.MATCH_ANY));
    assertThat(replicaDeleteRequest.versionType(), equalTo(VersionType.INTERNAL));
    assertThat(primaryResponse.getItemId(), equalTo(0));
    assertThat(primaryResponse.getId(), equalTo("id"));
    assertThat(primaryResponse.getOpType(), equalTo(DocWriteRequest.OpType.DELETE));
    assertFalse(primaryResponse.isFailed());
    assertThat(response.getResult(), equalTo(DocWriteResponse.Result.NOT_FOUND));
    assertThat(response.getShardId(), equalTo(shard.shardId()));
    assertThat(response.getIndex(), equalTo("index"));
    assertThat(response.getType(), equalTo("type"));
    assertThat(response.getId(), equalTo("id"));
    assertThat(response.getVersion(), equalTo(1L));
    assertThat(response.getSeqNo(), equalTo(0L));
    assertThat(response.forcedRefresh(), equalTo(false));
    // Now do the same after indexing the document, it should now find and delete the document
    indexDoc(shard, "type", "id", "{\"foo\": \"bar\"}");
    writeRequest = new DeleteRequest("index", "type", "id");
    items[0] = new BulkItemRequest(0, writeRequest);
    bulkShardRequest = new BulkShardRequest(shardId, RefreshPolicy.NONE, items);
    location = newLocation;
    newLocation = TransportShardBulkAction.executeBulkItemRequest(metaData, shard, bulkShardRequest, location, 0, updateHelper, threadPool::absoluteTimeInMillis, new NoopMappingUpdatePerformer());
    // Translog changes, because the document was deleted
    assertThat(newLocation, not(location));
    replicaRequest = bulkShardRequest.items()[0];
    replicaDeleteRequest = replicaRequest.request();
    primaryResponse = replicaRequest.getPrimaryResponse();
    response = primaryResponse.getResponse();
    // Any version can be matched on replica
    assertThat(replicaDeleteRequest.version(), equalTo(Versions.MATCH_ANY));
    assertThat(replicaDeleteRequest.versionType(), equalTo(VersionType.INTERNAL));
    assertThat(primaryResponse.getItemId(), equalTo(0));
    assertThat(primaryResponse.getId(), equalTo("id"));
    assertThat(primaryResponse.getOpType(), equalTo(DocWriteRequest.OpType.DELETE));
    assertFalse(primaryResponse.isFailed());
    assertThat(response.getResult(), equalTo(DocWriteResponse.Result.DELETED));
    assertThat(response.getShardId(), equalTo(shard.shardId()));
    assertThat(response.getIndex(), equalTo("index"));
    assertThat(response.getType(), equalTo("type"));
    assertThat(response.getId(), equalTo("id"));
    assertThat(response.getVersion(), equalTo(3L));
    assertThat(response.getSeqNo(), equalTo(2L));
    assertThat(response.forcedRefresh(), equalTo(false));
    assertDocCount(shard, 0);
    closeShards(shard);
}
Also used : IndexShard(org.elasticsearch.index.shard.IndexShard) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Translog(org.elasticsearch.index.translog.Translog) UpdateHelper(org.elasticsearch.action.update.UpdateHelper) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 10 with DocWriteRequest

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

the class TransportShardBulkActionTests method testExecuteBulkIndexRequest.

public void testExecuteBulkIndexRequest() throws Exception {
    IndexMetaData metaData = indexMetaData();
    IndexShard shard = newStartedShard(true);
    BulkItemRequest[] items = new BulkItemRequest[1];
    boolean create = randomBoolean();
    DocWriteRequest writeRequest = new IndexRequest("index", "type", "id").source(Requests.INDEX_CONTENT_TYPE, "foo", "bar").create(create);
    BulkItemRequest primaryRequest = new BulkItemRequest(0, writeRequest);
    items[0] = primaryRequest;
    BulkShardRequest bulkShardRequest = new BulkShardRequest(shardId, RefreshPolicy.NONE, items);
    Translog.Location location = new Translog.Location(0, 0, 0);
    UpdateHelper updateHelper = null;
    Translog.Location newLocation = TransportShardBulkAction.executeBulkItemRequest(metaData, shard, bulkShardRequest, location, 0, updateHelper, threadPool::absoluteTimeInMillis, new NoopMappingUpdatePerformer());
    // Translog should change, since there were no problems
    assertThat(newLocation, not(location));
    BulkItemResponse primaryResponse = bulkShardRequest.items()[0].getPrimaryResponse();
    assertThat(primaryResponse.getItemId(), equalTo(0));
    assertThat(primaryResponse.getId(), equalTo("id"));
    assertThat(primaryResponse.getOpType(), equalTo(create ? DocWriteRequest.OpType.CREATE : DocWriteRequest.OpType.INDEX));
    assertFalse(primaryResponse.isFailed());
    // Assert that the document actually made it there
    assertDocCount(shard, 1);
    writeRequest = new IndexRequest("index", "type", "id").source(Requests.INDEX_CONTENT_TYPE, "foo", "bar").create(true);
    primaryRequest = new BulkItemRequest(0, writeRequest);
    items[0] = primaryRequest;
    bulkShardRequest = new BulkShardRequest(shardId, RefreshPolicy.NONE, items);
    Translog.Location secondLocation = TransportShardBulkAction.executeBulkItemRequest(metaData, shard, bulkShardRequest, newLocation, 0, updateHelper, threadPool::absoluteTimeInMillis, new NoopMappingUpdatePerformer());
    // Translog should not change, since the document was not indexed due to a version conflict
    assertThat(secondLocation, equalTo(newLocation));
    BulkItemRequest replicaRequest = bulkShardRequest.items()[0];
    primaryResponse = bulkShardRequest.items()[0].getPrimaryResponse();
    assertThat(primaryResponse.getItemId(), equalTo(0));
    assertThat(primaryResponse.getId(), equalTo("id"));
    assertThat(primaryResponse.getOpType(), equalTo(DocWriteRequest.OpType.CREATE));
    // Should be failed since the document already exists
    assertTrue(primaryResponse.isFailed());
    BulkItemResponse.Failure failure = primaryResponse.getFailure();
    assertThat(failure.getIndex(), equalTo("index"));
    assertThat(failure.getType(), equalTo("type"));
    assertThat(failure.getId(), equalTo("id"));
    assertThat(failure.getCause().getClass(), equalTo(VersionConflictEngineException.class));
    assertThat(failure.getCause().getMessage(), containsString("version conflict, document already exists (current version [1])"));
    assertThat(failure.getStatus(), equalTo(RestStatus.CONFLICT));
    assertThat(replicaRequest, equalTo(primaryRequest));
    // Assert that the document count is still 1
    assertDocCount(shard, 1);
    closeShards(shard);
}
Also used : IndexShard(org.elasticsearch.index.shard.IndexShard) IndexRequest(org.elasticsearch.action.index.IndexRequest) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Translog(org.elasticsearch.index.translog.Translog) UpdateHelper(org.elasticsearch.action.update.UpdateHelper) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) DocWriteRequest(org.elasticsearch.action.DocWriteRequest)

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