Search in sources :

Example 6 with IndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest in project elasticsearch by elastic.

the class TransportBulkActionIngestTests method testSingleItemBulkActionIngestLocal.

public void testSingleItemBulkActionIngestLocal() throws Exception {
    Exception exception = new Exception("fake exception");
    IndexRequest indexRequest = new IndexRequest("index", "type", "id");
    indexRequest.source(Collections.emptyMap());
    indexRequest.setPipeline("testpipeline");
    AtomicBoolean responseCalled = new AtomicBoolean(false);
    AtomicBoolean failureCalled = new AtomicBoolean(false);
    singleItemBulkWriteAction.execute(null, indexRequest, ActionListener.wrap(response -> {
        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
    // this is done by the real pipeline execution service when processing
    indexRequest.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)

Example 7 with IndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest 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 8 with IndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest 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)

Example 9 with IndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest in project elasticsearch by elastic.

the class BulkProcessorIT method indexDocs.

private static MultiGetRequestBuilder indexDocs(Client client, BulkProcessor processor, int numDocs) throws Exception {
    MultiGetRequestBuilder multiGetRequestBuilder = client.prepareMultiGet();
    for (int i = 1; i <= numDocs; i++) {
        if (randomBoolean()) {
            processor.add(new IndexRequest("test", "test", Integer.toString(i)).source(Requests.INDEX_CONTENT_TYPE, "field", randomRealisticUnicodeOfLengthBetween(1, 30)));
        } else {
            final String source = "{ \"index\":{\"_index\":\"test\",\"_type\":\"test\",\"_id\":\"" + Integer.toString(i) + "\"} }\n" + JsonXContent.contentBuilder().startObject().field("field", randomRealisticUnicodeOfLengthBetween(1, 30)).endObject().string() + "\n";
            processor.add(new BytesArray(source), null, null, XContentType.JSON);
        }
        multiGetRequestBuilder.add("test", "test", Integer.toString(i));
    }
    return multiGetRequestBuilder;
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) MultiGetRequestBuilder(org.elasticsearch.action.get.MultiGetRequestBuilder) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 10 with IndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest in project elasticsearch by elastic.

the class BulkRequestModifierTests method testPipelineFailures.

public void testPipelineFailures() {
    BulkRequest originalBulkRequest = new BulkRequest();
    for (int i = 0; i < 32; i++) {
        originalBulkRequest.add(new IndexRequest("index", "type", String.valueOf(i)));
    }
    TransportBulkAction.BulkRequestModifier modifier = new TransportBulkAction.BulkRequestModifier(originalBulkRequest);
    for (int i = 0; modifier.hasNext(); i++) {
        modifier.next();
        if (i % 2 == 0) {
            modifier.markCurrentItemAsFailed(new RuntimeException());
        }
    }
    // So half of the requests have "failed", so only the successful requests are left:
    BulkRequest bulkRequest = modifier.getBulkRequest();
    assertThat(bulkRequest.requests().size(), Matchers.equalTo(16));
    List<BulkItemResponse> responses = new ArrayList<>();
    ActionListener<BulkResponse> bulkResponseListener = modifier.wrapActionListenerIfNeeded(1L, new ActionListener<BulkResponse>() {

        @Override
        public void onResponse(BulkResponse bulkItemResponses) {
            responses.addAll(Arrays.asList(bulkItemResponses.getItems()));
        }

        @Override
        public void onFailure(Exception e) {
        }
    });
    List<BulkItemResponse> originalResponses = new ArrayList<>();
    for (DocWriteRequest actionRequest : bulkRequest.requests()) {
        IndexRequest indexRequest = (IndexRequest) actionRequest;
        IndexResponse indexResponse = new IndexResponse(new ShardId("index", "_na_", 0), indexRequest.type(), indexRequest.id(), 1, 1, true);
        originalResponses.add(new BulkItemResponse(Integer.parseInt(indexRequest.id()), indexRequest.opType(), indexResponse));
    }
    bulkResponseListener.onResponse(new BulkResponse(originalResponses.toArray(new BulkItemResponse[originalResponses.size()]), 0));
    assertThat(responses.size(), Matchers.equalTo(32));
    for (int i = 0; i < 32; i++) {
        assertThat(responses.get(i).getId(), Matchers.equalTo(String.valueOf(i)));
    }
}
Also used : ArrayList(java.util.ArrayList) IndexRequest(org.elasticsearch.action.index.IndexRequest) ShardId(org.elasticsearch.index.shard.ShardId) IndexResponse(org.elasticsearch.action.index.IndexResponse) DocWriteRequest(org.elasticsearch.action.DocWriteRequest)

Aggregations

IndexRequest (org.elasticsearch.action.index.IndexRequest)170 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)34 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)32 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)31 IOException (java.io.IOException)28 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)25 Test (org.junit.Test)25 ElasticsearchException (org.elasticsearch.ElasticsearchException)20 IndexResponse (org.elasticsearch.action.index.IndexResponse)17 HashMap (java.util.HashMap)14 Map (java.util.Map)14 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)14 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)13 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)12 BytesReference (org.elasticsearch.common.bytes.BytesReference)11 ArrayList (java.util.ArrayList)10 GetRequest (org.elasticsearch.action.get.GetRequest)10 Matchers.anyBoolean (org.mockito.Matchers.anyBoolean)9 Settings (org.elasticsearch.common.settings.Settings)8 XContentType (org.elasticsearch.common.xcontent.XContentType)8