|
19 | 19 |
|
20 | 20 | package org.elasticsearch.action.bulk;
|
21 | 21 |
|
| 22 | +import org.elasticsearch.Version; |
22 | 23 | import org.elasticsearch.action.ActionListener;
|
23 | 24 | import org.elasticsearch.action.DocWriteRequest;
|
24 | 25 | import org.elasticsearch.action.index.IndexAction;
|
|
28 | 29 | import org.elasticsearch.cluster.ClusterChangedEvent;
|
29 | 30 | import org.elasticsearch.cluster.ClusterState;
|
30 | 31 | import org.elasticsearch.cluster.ClusterStateApplier;
|
| 32 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 33 | +import org.elasticsearch.cluster.metadata.MetaData; |
31 | 34 | import org.elasticsearch.cluster.node.DiscoveryNode;
|
32 | 35 | import org.elasticsearch.cluster.node.DiscoveryNodes;
|
33 | 36 | import org.elasticsearch.cluster.service.ClusterService;
|
34 | 37 | import org.elasticsearch.common.collect.ImmutableOpenMap;
|
35 | 38 | import org.elasticsearch.common.settings.Settings;
|
36 | 39 | import org.elasticsearch.common.util.concurrent.AtomicArray;
|
37 | 40 | import org.elasticsearch.index.IndexNotFoundException;
|
| 41 | +import org.elasticsearch.index.IndexSettings; |
38 | 42 | import org.elasticsearch.ingest.IngestService;
|
39 | 43 | import org.elasticsearch.ingest.PipelineExecutionService;
|
40 | 44 | import org.elasticsearch.tasks.Task;
|
|
68 | 72 |
|
69 | 73 | public class TransportBulkActionIngestTests extends ESTestCase {
|
70 | 74 |
|
| 75 | + /** |
| 76 | + * Index for which mock settings contain a default pipeline. |
| 77 | + */ |
| 78 | + private static final String WITH_DEFAULT_PIPELINE = "index_with_default_pipeline"; |
| 79 | + |
71 | 80 | /** Services needed by bulk action */
|
72 | 81 | TransportService transportService;
|
73 | 82 | ClusterService clusterService;
|
@@ -153,6 +162,15 @@ public void setupAction() {
|
153 | 162 | when(nodes.getIngestNodes()).thenReturn(ingestNodes);
|
154 | 163 | ClusterState state = mock(ClusterState.class);
|
155 | 164 | when(state.getNodes()).thenReturn(nodes);
|
| 165 | + when(state.getMetaData()).thenReturn(MetaData.builder().indices(ImmutableOpenMap.<String, IndexMetaData>builder() |
| 166 | + .putAll( |
| 167 | + Collections.singletonMap( |
| 168 | + WITH_DEFAULT_PIPELINE, |
| 169 | + IndexMetaData.builder(WITH_DEFAULT_PIPELINE).settings( |
| 170 | + settings(Version.CURRENT).put(IndexSettings.DEFAULT_PIPELINE.getKey(), "default_pipeline") |
| 171 | + .build() |
| 172 | + ).numberOfShards(1).numberOfReplicas(1).build())) |
| 173 | + .build()).build()); |
156 | 174 | when(clusterService.state()).thenReturn(state);
|
157 | 175 | doAnswer(invocation -> {
|
158 | 176 | ClusterChangedEvent event = mock(ClusterChangedEvent.class);
|
@@ -227,7 +245,7 @@ public void testIngestLocal() throws Exception {
|
227 | 245 | // now check success
|
228 | 246 | Iterator<DocWriteRequest> req = bulkDocsItr.getValue().iterator();
|
229 | 247 | failureHandler.getValue().accept((IndexRequest)req.next(), exception); // have an exception for our one index request
|
230 |
| - indexRequest2.setPipeline(null); // this is done by the real pipeline execution service when processing |
| 248 | + indexRequest2.setPipeline(IngestService.NOOP_PIPELINE_NAME); // this is done by the real pipeline execution service when processing |
231 | 249 | completionHandler.getValue().accept(null);
|
232 | 250 | assertTrue(action.isExecuted);
|
233 | 251 | assertFalse(responseCalled.get()); // listener would only be called by real index action, not our mocked one
|
@@ -259,7 +277,7 @@ public void testSingleItemBulkActionIngestLocal() throws Exception {
|
259 | 277 | assertTrue(failureCalled.get());
|
260 | 278 |
|
261 | 279 | // now check success
|
262 |
| - indexRequest.setPipeline(null); // this is done by the real pipeline execution service when processing |
| 280 | + indexRequest.setPipeline(IngestService.NOOP_PIPELINE_NAME); // this is done by the real pipeline execution service when processing |
263 | 281 | completionHandler.getValue().accept(null);
|
264 | 282 | assertTrue(action.isExecuted);
|
265 | 283 | assertFalse(responseCalled.get()); // listener would only be called by real index action, not our mocked one
|
@@ -359,4 +377,35 @@ public void testSingleItemBulkActionIngestForward() throws Exception {
|
359 | 377 | }
|
360 | 378 | }
|
361 | 379 |
|
| 380 | + public void testUseDefaultPipeline() throws Exception { |
| 381 | + Exception exception = new Exception("fake exception"); |
| 382 | + IndexRequest indexRequest = new IndexRequest(WITH_DEFAULT_PIPELINE, "type", "id"); |
| 383 | + indexRequest.source(Collections.emptyMap()); |
| 384 | + AtomicBoolean responseCalled = new AtomicBoolean(false); |
| 385 | + AtomicBoolean failureCalled = new AtomicBoolean(false); |
| 386 | + singleItemBulkWriteAction.execute(null, indexRequest, ActionListener.wrap( |
| 387 | + response -> { |
| 388 | + responseCalled.set(true); |
| 389 | + }, |
| 390 | + e -> { |
| 391 | + assertThat(e, sameInstance(exception)); |
| 392 | + failureCalled.set(true); |
| 393 | + })); |
| 394 | + |
| 395 | + // check failure works, and passes through to the listener |
| 396 | + assertFalse(action.isExecuted); // haven't executed yet |
| 397 | + assertFalse(responseCalled.get()); |
| 398 | + assertFalse(failureCalled.get()); |
| 399 | + verify(executionService).executeBulkRequest(bulkDocsItr.capture(), failureHandler.capture(), completionHandler.capture()); |
| 400 | + completionHandler.getValue().accept(exception); |
| 401 | + assertTrue(failureCalled.get()); |
| 402 | + |
| 403 | + // now check success |
| 404 | + indexRequest.setPipeline(IngestService.NOOP_PIPELINE_NAME); // this is done by the real pipeline execution service when processing |
| 405 | + completionHandler.getValue().accept(null); |
| 406 | + assertTrue(action.isExecuted); |
| 407 | + assertFalse(responseCalled.get()); // listener would only be called by real index action, not our mocked one |
| 408 | + verifyZeroInteractions(transportService); |
| 409 | + } |
| 410 | + |
362 | 411 | }
|
0 commit comments