Search in sources :

Example 96 with IndexRequest

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

the class TransportBulkActionIngestTests method testSingleItemBulkActionIngestSkipped.

public void testSingleItemBulkActionIngestSkipped() throws Exception {
    IndexRequest indexRequest = new IndexRequest("index", "type", "id");
    indexRequest.source(Collections.emptyMap());
    singleItemBulkWriteAction.execute(null, indexRequest, ActionListener.wrap(response -> {
    }, exception -> {
        throw new AssertionError(exception);
    }));
    assertTrue(action.isExecuted);
    verifyZeroInteractions(ingestService);
}
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) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 97 with IndexRequest

use of org.elasticsearch.action.index.IndexRequest 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 98 with IndexRequest

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

the class TransportBulkActionIngestTests method testIngestForward.

public void testIngestForward() throws Exception {
    localIngest = false;
    BulkRequest bulkRequest = new BulkRequest();
    IndexRequest indexRequest = new IndexRequest("index", "type", "id");
    indexRequest.source(Collections.emptyMap());
    indexRequest.setPipeline("testpipeline");
    bulkRequest.add(indexRequest);
    BulkResponse bulkResponse = mock(BulkResponse.class);
    AtomicBoolean responseCalled = new AtomicBoolean(false);
    ActionListener<BulkResponse> listener = ActionListener.wrap(response -> {
        responseCalled.set(true);
        assertSame(bulkResponse, response);
    }, e -> {
        throw new AssertionError(e);
    });
    action.execute(null, bulkRequest, listener);
    // should not have executed ingest locally
    verify(executionService, never()).executeBulkRequest(any(), any(), any());
    // but instead should have sent to a remote node with the transport service
    ArgumentCaptor<DiscoveryNode> node = ArgumentCaptor.forClass(DiscoveryNode.class);
    verify(transportService).sendRequest(node.capture(), eq(BulkAction.NAME), any(), remoteResponseHandler.capture());
    // make sure we used one of the nodes
    boolean usedNode1 = node.getValue() == remoteNode1;
    if (usedNode1 == false) {
        assertSame(remoteNode2, node.getValue());
    }
    // no local index execution
    assertFalse(action.isExecuted);
    // listener not called yet
    assertFalse(responseCalled.get());
    // call the listener for the remote node
    remoteResponseHandler.getValue().handleResponse(bulkResponse);
    // now the listener we passed should have been delegated to by the remote listener
    assertTrue(responseCalled.get());
    // still no local index execution
    assertFalse(action.isExecuted);
    // now make sure ingest nodes are rotated through with a subsequent request
    reset(transportService);
    action.execute(null, bulkRequest, listener);
    verify(transportService).sendRequest(node.capture(), eq(BulkAction.NAME), any(), remoteResponseHandler.capture());
    if (usedNode1) {
        assertSame(remoteNode2, node.getValue());
    } else {
        assertSame(remoteNode1, node.getValue());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 99 with IndexRequest

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

the class TransportBulkActionIngestTests method testIngestSkipped.

public void testIngestSkipped() throws Exception {
    BulkRequest bulkRequest = new BulkRequest();
    IndexRequest indexRequest = new IndexRequest("index", "type", "id");
    indexRequest.source(Collections.emptyMap());
    bulkRequest.add(indexRequest);
    action.execute(null, bulkRequest, ActionListener.wrap(response -> {
    }, exception -> {
        throw new AssertionError(exception);
    }));
    assertTrue(action.isExecuted);
    verifyZeroInteractions(ingestService);
}
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) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 100 with IndexRequest

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

the class TransportBulkActionIngestTests method testSingleItemBulkActionIngestForward.

public void testSingleItemBulkActionIngestForward() throws Exception {
    localIngest = false;
    IndexRequest indexRequest = new IndexRequest("index", "type", "id");
    indexRequest.source(Collections.emptyMap());
    indexRequest.setPipeline("testpipeline");
    IndexResponse indexResponse = mock(IndexResponse.class);
    AtomicBoolean responseCalled = new AtomicBoolean(false);
    ActionListener<IndexResponse> listener = ActionListener.wrap(response -> {
        responseCalled.set(true);
        assertSame(indexResponse, response);
    }, e -> {
        throw new AssertionError(e);
    });
    singleItemBulkWriteAction.execute(null, indexRequest, listener);
    // should not have executed ingest locally
    verify(executionService, never()).executeBulkRequest(any(), any(), any());
    // but instead should have sent to a remote node with the transport service
    ArgumentCaptor<DiscoveryNode> node = ArgumentCaptor.forClass(DiscoveryNode.class);
    verify(transportService).sendRequest(node.capture(), eq(BulkAction.NAME), any(), remoteResponseHandler.capture());
    // make sure we used one of the nodes
    boolean usedNode1 = node.getValue() == remoteNode1;
    if (usedNode1 == false) {
        assertSame(remoteNode2, node.getValue());
    }
    // no local index execution
    assertFalse(action.isExecuted);
    // listener not called yet
    assertFalse(responseCalled.get());
    BulkItemResponse itemResponse = new BulkItemResponse(0, DocWriteRequest.OpType.CREATE, indexResponse);
    BulkItemResponse[] bulkItemResponses = new BulkItemResponse[1];
    bulkItemResponses[0] = itemResponse;
    // call the listener for the remote node
    remoteResponseHandler.getValue().handleResponse(new BulkResponse(bulkItemResponses, 0));
    // now the listener we passed should have been delegated to by the remote listener
    assertTrue(responseCalled.get());
    // still no local index execution
    assertFalse(action.isExecuted);
    // now make sure ingest nodes are rotated through with a subsequent request
    reset(transportService);
    singleItemBulkWriteAction.execute(null, indexRequest, listener);
    verify(transportService).sendRequest(node.capture(), eq(BulkAction.NAME), any(), remoteResponseHandler.capture());
    if (usedNode1) {
        assertSame(remoteNode2, node.getValue());
    } else {
        assertSame(remoteNode1, node.getValue());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) IndexResponse(org.elasticsearch.action.index.IndexResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Aggregations

IndexRequest (org.elasticsearch.action.index.IndexRequest)177 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)36 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)34 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)33 IOException (java.io.IOException)28 Test (org.junit.Test)27 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)26 ElasticsearchException (org.elasticsearch.ElasticsearchException)20 IndexResponse (org.elasticsearch.action.index.IndexResponse)17 HashMap (java.util.HashMap)16 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)15 Map (java.util.Map)14 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)14 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)13 GetRequest (org.elasticsearch.action.get.GetRequest)13 BytesReference (org.elasticsearch.common.bytes.BytesReference)12 ArrayList (java.util.ArrayList)10 Matchers.anyBoolean (org.mockito.Matchers.anyBoolean)9 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)8 CreateIndexRequest (org.elasticsearch.client.indices.CreateIndexRequest)8