Search in sources :

Example 11 with DocWriteRequest

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

the class TransportShardBulkActionTests method testExecuteBulkIndexRequestWithRejection.

public void testExecuteBulkIndexRequestWithRejection() throws Exception {
    IndexMetaData metaData = indexMetaData();
    IndexShard shard = newStartedShard(true);
    BulkItemRequest[] items = new BulkItemRequest[1];
    DocWriteRequest writeRequest = new IndexRequest("index", "type", "id").source(Requests.INDEX_CONTENT_TYPE, "foo", "bar");
    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;
    // Pretend the mappings haven't made it to the node yet, and throw  a rejection
    Exception err = new ReplicationOperation.RetryOnPrimaryException(shardId, "rejection");
    try {
        TransportShardBulkAction.executeBulkItemRequest(metaData, shard, bulkShardRequest, location, 0, updateHelper, threadPool::absoluteTimeInMillis, new ThrowingMappingUpdatePerformer(err));
        fail("should have thrown a retry exception");
    } catch (ReplicationOperation.RetryOnPrimaryException e) {
        assertThat(e, equalTo(err));
    }
    closeShards(shard);
}
Also used : IndexShard(org.elasticsearch.index.shard.IndexShard) IndexRequest(org.elasticsearch.action.index.IndexRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) IOException(java.io.IOException) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Translog(org.elasticsearch.index.translog.Translog) UpdateHelper(org.elasticsearch.action.update.UpdateHelper) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) ReplicationOperation(org.elasticsearch.action.support.replication.ReplicationOperation)

Example 12 with DocWriteRequest

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

the class BulkRequestTests method testBulkAddIterable.

public void testBulkAddIterable() {
    BulkRequest bulkRequest = Requests.bulkRequest();
    List<DocWriteRequest> requests = new ArrayList<>();
    requests.add(new IndexRequest("test", "test", "id").source(Requests.INDEX_CONTENT_TYPE, "field", "value"));
    requests.add(new UpdateRequest("test", "test", "id").doc(Requests.INDEX_CONTENT_TYPE, "field", "value"));
    requests.add(new DeleteRequest("test", "test", "id"));
    bulkRequest.add(requests);
    assertThat(bulkRequest.requests().size(), equalTo(3));
    assertThat(bulkRequest.requests().get(0), instanceOf(IndexRequest.class));
    assertThat(bulkRequest.requests().get(1), instanceOf(UpdateRequest.class));
    assertThat(bulkRequest.requests().get(2), instanceOf(DeleteRequest.class));
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) ArrayList(java.util.ArrayList) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 13 with DocWriteRequest

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

the class PipelineExecutionServiceTests method testBulkRequestExecutionWithFailures.

public void testBulkRequestExecutionWithFailures() throws Exception {
    BulkRequest bulkRequest = new BulkRequest();
    String pipelineId = "_id";
    int numRequest = scaledRandomIntBetween(8, 64);
    int numIndexRequests = 0;
    for (int i = 0; i < numRequest; i++) {
        DocWriteRequest request;
        if (randomBoolean()) {
            if (randomBoolean()) {
                request = new DeleteRequest("_index", "_type", "_id");
            } else {
                request = new UpdateRequest("_index", "_type", "_id");
            }
        } else {
            IndexRequest indexRequest = new IndexRequest("_index", "_type", "_id").setPipeline(pipelineId);
            indexRequest.source(Requests.INDEX_CONTENT_TYPE, "field1", "value1");
            request = indexRequest;
            numIndexRequests++;
        }
        bulkRequest.add(request);
    }
    CompoundProcessor processor = mock(CompoundProcessor.class);
    when(processor.getProcessors()).thenReturn(Collections.singletonList(mock(Processor.class)));
    Exception error = new RuntimeException();
    doThrow(error).when(processor).execute(any());
    when(store.get(pipelineId)).thenReturn(new Pipeline(pipelineId, null, version, processor));
    @SuppressWarnings("unchecked") BiConsumer<IndexRequest, Exception> requestItemErrorHandler = mock(BiConsumer.class);
    @SuppressWarnings("unchecked") Consumer<Exception> completionHandler = mock(Consumer.class);
    executionService.executeBulkRequest(bulkRequest.requests(), requestItemErrorHandler, completionHandler);
    verify(requestItemErrorHandler, times(numIndexRequests)).accept(any(IndexRequest.class), eq(error));
    verify(completionHandler, times(1)).accept(null);
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) Matchers.anyString(org.mockito.Matchers.anyString) IndexRequest(org.elasticsearch.action.index.IndexRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 14 with DocWriteRequest

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

the class RequestTests method testBulk.

public void testBulk() throws IOException {
    Map<String, String> expectedParams = new HashMap<>();
    BulkRequest bulkRequest = new BulkRequest();
    if (randomBoolean()) {
        String timeout = randomTimeValue();
        bulkRequest.timeout(timeout);
        expectedParams.put("timeout", timeout);
    } else {
        expectedParams.put("timeout", BulkShardRequest.DEFAULT_TIMEOUT.getStringRep());
    }
    if (randomBoolean()) {
        WriteRequest.RefreshPolicy refreshPolicy = randomFrom(WriteRequest.RefreshPolicy.values());
        bulkRequest.setRefreshPolicy(refreshPolicy);
        if (refreshPolicy != WriteRequest.RefreshPolicy.NONE) {
            expectedParams.put("refresh", refreshPolicy.getValue());
        }
    }
    XContentType xContentType = randomFrom(XContentType.JSON, XContentType.SMILE);
    int nbItems = randomIntBetween(10, 100);
    for (int i = 0; i < nbItems; i++) {
        String index = randomAsciiOfLength(5);
        String type = randomAsciiOfLength(5);
        String id = randomAsciiOfLength(5);
        BytesReference source = RandomObjects.randomSource(random(), xContentType);
        DocWriteRequest.OpType opType = randomFrom(DocWriteRequest.OpType.values());
        DocWriteRequest<?> docWriteRequest = null;
        if (opType == DocWriteRequest.OpType.INDEX) {
            IndexRequest indexRequest = new IndexRequest(index, type, id).source(source, xContentType);
            docWriteRequest = indexRequest;
            if (randomBoolean()) {
                indexRequest.setPipeline(randomAsciiOfLength(5));
            }
            if (randomBoolean()) {
                indexRequest.parent(randomAsciiOfLength(5));
            }
        } else if (opType == DocWriteRequest.OpType.CREATE) {
            IndexRequest createRequest = new IndexRequest(index, type, id).source(source, xContentType).create(true);
            docWriteRequest = createRequest;
            if (randomBoolean()) {
                createRequest.parent(randomAsciiOfLength(5));
            }
        } else if (opType == DocWriteRequest.OpType.UPDATE) {
            final UpdateRequest updateRequest = new UpdateRequest(index, type, id).doc(new IndexRequest().source(source, xContentType));
            docWriteRequest = updateRequest;
            if (randomBoolean()) {
                updateRequest.retryOnConflict(randomIntBetween(1, 5));
            }
            if (randomBoolean()) {
                randomizeFetchSourceContextParams(updateRequest::fetchSource, new HashMap<>());
            }
            if (randomBoolean()) {
                updateRequest.parent(randomAsciiOfLength(5));
            }
        } else if (opType == DocWriteRequest.OpType.DELETE) {
            docWriteRequest = new DeleteRequest(index, type, id);
        }
        if (randomBoolean()) {
            docWriteRequest.routing(randomAsciiOfLength(10));
        }
        if (randomBoolean()) {
            docWriteRequest.version(randomNonNegativeLong());
        }
        if (randomBoolean()) {
            docWriteRequest.versionType(randomFrom(VersionType.values()));
        }
        bulkRequest.add(docWriteRequest);
    }
    Request request = Request.bulk(bulkRequest);
    assertEquals("/_bulk", request.endpoint);
    assertEquals(expectedParams, request.params);
    assertEquals("POST", request.method);
    byte[] content = new byte[(int) request.entity.getContentLength()];
    try (InputStream inputStream = request.entity.getContent()) {
        Streams.readFully(inputStream, content);
    }
    BulkRequest parsedBulkRequest = new BulkRequest();
    parsedBulkRequest.add(content, 0, content.length, xContentType);
    assertEquals(bulkRequest.numberOfActions(), parsedBulkRequest.numberOfActions());
    for (int i = 0; i < bulkRequest.numberOfActions(); i++) {
        DocWriteRequest<?> originalRequest = bulkRequest.requests().get(i);
        DocWriteRequest<?> parsedRequest = parsedBulkRequest.requests().get(i);
        assertEquals(originalRequest.opType(), parsedRequest.opType());
        assertEquals(originalRequest.index(), parsedRequest.index());
        assertEquals(originalRequest.type(), parsedRequest.type());
        assertEquals(originalRequest.id(), parsedRequest.id());
        assertEquals(originalRequest.routing(), parsedRequest.routing());
        assertEquals(originalRequest.parent(), parsedRequest.parent());
        assertEquals(originalRequest.version(), parsedRequest.version());
        assertEquals(originalRequest.versionType(), parsedRequest.versionType());
        DocWriteRequest.OpType opType = originalRequest.opType();
        if (opType == DocWriteRequest.OpType.INDEX) {
            IndexRequest indexRequest = (IndexRequest) originalRequest;
            IndexRequest parsedIndexRequest = (IndexRequest) parsedRequest;
            assertEquals(indexRequest.getPipeline(), parsedIndexRequest.getPipeline());
            assertToXContentEquivalent(indexRequest.source(), parsedIndexRequest.source(), xContentType);
        } else if (opType == DocWriteRequest.OpType.UPDATE) {
            UpdateRequest updateRequest = (UpdateRequest) originalRequest;
            UpdateRequest parsedUpdateRequest = (UpdateRequest) parsedRequest;
            assertEquals(updateRequest.retryOnConflict(), parsedUpdateRequest.retryOnConflict());
            assertEquals(updateRequest.fetchSource(), parsedUpdateRequest.fetchSource());
            if (updateRequest.doc() != null) {
                assertToXContentEquivalent(updateRequest.doc().source(), parsedUpdateRequest.doc().source(), xContentType);
            } else {
                assertNull(parsedUpdateRequest.doc());
            }
        }
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) HashMap(java.util.HashMap) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) WriteRequest(org.elasticsearch.action.support.WriteRequest) ReplicatedWriteRequest(org.elasticsearch.action.support.replication.ReplicatedWriteRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) InputStream(java.io.InputStream) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) WriteRequest(org.elasticsearch.action.support.WriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) BulkShardRequest(org.elasticsearch.action.bulk.BulkShardRequest) GetRequest(org.elasticsearch.action.get.GetRequest) ReplicatedWriteRequest(org.elasticsearch.action.support.replication.ReplicatedWriteRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) ReplicationRequest(org.elasticsearch.action.support.replication.ReplicationRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) XContentType(org.elasticsearch.common.xcontent.XContentType) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 15 with DocWriteRequest

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

the class TransportShardBulkAction method shardOperationOnReplica.

@Override
public WriteReplicaResult<BulkShardRequest> shardOperationOnReplica(BulkShardRequest request, IndexShard replica) throws Exception {
    Translog.Location location = null;
    for (int i = 0; i < request.items().length; i++) {
        BulkItemRequest item = request.items()[i];
        if (shouldExecuteReplicaItem(item, i)) {
            DocWriteRequest docWriteRequest = item.request();
            DocWriteResponse primaryResponse = item.getPrimaryResponse().getResponse();
            final Engine.Result operationResult;
            try {
                switch(docWriteRequest.opType()) {
                    case CREATE:
                    case INDEX:
                        operationResult = executeIndexRequestOnReplica(primaryResponse, (IndexRequest) docWriteRequest, replica);
                        break;
                    case DELETE:
                        operationResult = executeDeleteRequestOnReplica(primaryResponse, (DeleteRequest) docWriteRequest, replica);
                        break;
                    default:
                        throw new IllegalStateException("Unexpected request operation type on replica: " + docWriteRequest.opType().getLowercase());
                }
                if (operationResult.hasFailure()) {
                    // check if any transient write operation failures should be bubbled up
                    Exception failure = operationResult.getFailure();
                    assert failure instanceof VersionConflictEngineException || failure instanceof MapperParsingException : "expected any one of [version conflict, mapper parsing, engine closed, index shard closed]" + " failures. got " + failure;
                    if (!TransportActions.isShardNotAvailableException(failure)) {
                        throw failure;
                    }
                } else {
                    location = locationToSync(location, operationResult.getTranslogLocation());
                }
            } catch (Exception e) {
                // so we will fail the shard
                if (!TransportActions.isShardNotAvailableException(e)) {
                    throw e;
                }
            }
        }
    }
    return new WriteReplicaResult<>(request, location, null, replica, logger);
}
Also used : MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) IOException(java.io.IOException) Translog(org.elasticsearch.index.translog.Translog) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) Engine(org.elasticsearch.index.engine.Engine)

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