|
28 | 28 | import org.elasticsearch.env.Environment;
|
29 | 29 | import org.elasticsearch.env.NodeEnvironment;
|
30 | 30 | import org.elasticsearch.ingest.AbstractProcessor;
|
| 31 | +import org.elasticsearch.ingest.ConfigurationUtils; |
31 | 32 | import org.elasticsearch.ingest.IngestDocument;
|
32 | 33 | import org.elasticsearch.ingest.PipelineConfiguration;
|
33 | 34 | import org.elasticsearch.ingest.Processor;
|
|
48 | 49 | import java.util.Collection;
|
49 | 50 | import java.util.List;
|
50 | 51 | import java.util.Map;
|
| 52 | +import java.util.Objects; |
51 | 53 | import java.util.function.BiConsumer;
|
52 | 54 | import java.util.function.Supplier;
|
53 | 55 |
|
@@ -97,6 +99,26 @@ public void testFinalPipelineCantChangeDestination() {
|
97 | 99 | );
|
98 | 100 | }
|
99 | 101 |
|
| 102 | + public void testFinalPipelineCantRerouteDestination() { |
| 103 | + final Settings settings = Settings.builder().put(IndexSettings.FINAL_PIPELINE.getKey(), "final_pipeline").build(); |
| 104 | + createIndex("index", settings); |
| 105 | + |
| 106 | + final BytesReference finalPipelineBody = new BytesArray(""" |
| 107 | + {"processors": [{"reroute": {}}]}"""); |
| 108 | + client().admin().cluster().putPipeline(new PutPipelineRequest("final_pipeline", finalPipelineBody, XContentType.JSON)).actionGet(); |
| 109 | + |
| 110 | + final IllegalStateException e = expectThrows( |
| 111 | + IllegalStateException.class, |
| 112 | + () -> client().prepareIndex("index").setId("1").setSource(Map.of("field", "value")).get() |
| 113 | + ); |
| 114 | + assertThat( |
| 115 | + e, |
| 116 | + hasToString( |
| 117 | + endsWith("final pipeline [final_pipeline] can't change the target index (from [index] to [target]) for document [1]") |
| 118 | + ) |
| 119 | + ); |
| 120 | + } |
| 121 | + |
100 | 122 | public void testFinalPipelineOfOldDestinationIsNotInvoked() {
|
101 | 123 | Settings settings = Settings.builder()
|
102 | 124 | .put(IndexSettings.DEFAULT_PIPELINE.getKey(), "default_pipeline")
|
@@ -187,6 +209,73 @@ public void testDefaultPipelineOfNewDestinationIsNotInvoked() {
|
187 | 209 | assertFalse(target.getHits().getAt(0).getSourceAsMap().containsKey("final"));
|
188 | 210 | }
|
189 | 211 |
|
| 212 | + public void testDefaultPipelineOfRerouteDestinationIsInvoked() { |
| 213 | + Settings settings = Settings.builder().put(IndexSettings.DEFAULT_PIPELINE.getKey(), "default_pipeline").build(); |
| 214 | + createIndex("index", settings); |
| 215 | + |
| 216 | + settings = Settings.builder().put(IndexSettings.DEFAULT_PIPELINE.getKey(), "target_default_pipeline").build(); |
| 217 | + createIndex("target", settings); |
| 218 | + |
| 219 | + BytesReference defaultPipelineBody = new BytesArray(""" |
| 220 | + {"processors": [{"reroute": {}}]}"""); |
| 221 | + client().admin() |
| 222 | + .cluster() |
| 223 | + .putPipeline(new PutPipelineRequest("default_pipeline", defaultPipelineBody, XContentType.JSON)) |
| 224 | + .actionGet(); |
| 225 | + |
| 226 | + BytesReference targetPipeline = new BytesArray(""" |
| 227 | + {"processors": [{"final": {}}]}"""); |
| 228 | + client().admin() |
| 229 | + .cluster() |
| 230 | + .putPipeline(new PutPipelineRequest("target_default_pipeline", targetPipeline, XContentType.JSON)) |
| 231 | + .actionGet(); |
| 232 | + |
| 233 | + IndexResponse indexResponse = client().prepareIndex("index") |
| 234 | + .setId("1") |
| 235 | + .setSource(Map.of("field", "value")) |
| 236 | + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) |
| 237 | + .get(); |
| 238 | + assertEquals(RestStatus.CREATED, indexResponse.status()); |
| 239 | + SearchResponse target = client().prepareSearch("target").get(); |
| 240 | + assertEquals(1, target.getHits().getTotalHits().value); |
| 241 | + assertTrue(target.getHits().getAt(0).getSourceAsMap().containsKey("final")); |
| 242 | + } |
| 243 | + |
| 244 | + public void testAvoidIndexingLoop() { |
| 245 | + Settings settings = Settings.builder().put(IndexSettings.DEFAULT_PIPELINE.getKey(), "default_pipeline").build(); |
| 246 | + createIndex("index", settings); |
| 247 | + |
| 248 | + settings = Settings.builder().put(IndexSettings.DEFAULT_PIPELINE.getKey(), "target_default_pipeline").build(); |
| 249 | + createIndex("target", settings); |
| 250 | + |
| 251 | + BytesReference defaultPipelineBody = new BytesArray(""" |
| 252 | + {"processors": [{"reroute": {"dest": "target"}}]}"""); |
| 253 | + client().admin() |
| 254 | + .cluster() |
| 255 | + .putPipeline(new PutPipelineRequest("default_pipeline", defaultPipelineBody, XContentType.JSON)) |
| 256 | + .actionGet(); |
| 257 | + |
| 258 | + BytesReference targetPipeline = new BytesArray(""" |
| 259 | + {"processors": [{"reroute": {"dest": "index"}}]}"""); |
| 260 | + client().admin() |
| 261 | + .cluster() |
| 262 | + .putPipeline(new PutPipelineRequest("target_default_pipeline", targetPipeline, XContentType.JSON)) |
| 263 | + .actionGet(); |
| 264 | + |
| 265 | + IllegalStateException exception = expectThrows( |
| 266 | + IllegalStateException.class, |
| 267 | + () -> client().prepareIndex("index") |
| 268 | + .setId("1") |
| 269 | + .setSource(Map.of("dest", "index")) |
| 270 | + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) |
| 271 | + .get() |
| 272 | + ); |
| 273 | + assertThat( |
| 274 | + exception.getMessage(), |
| 275 | + equalTo("index cycle detected while processing pipeline [target_default_pipeline] for document [1]: [index, target, index]") |
| 276 | + ); |
| 277 | + } |
| 278 | + |
190 | 279 | public void testFinalPipeline() {
|
191 | 280 | final Settings settings = Settings.builder().put(IndexSettings.FINAL_PIPELINE.getKey(), "final_pipeline").build();
|
192 | 281 | createIndex("index", settings);
|
@@ -393,6 +482,26 @@ public String getType() {
|
393 | 482 | return "changing_dest";
|
394 | 483 | }
|
395 | 484 |
|
| 485 | + }, |
| 486 | + "reroute", |
| 487 | + (processorFactories, tag, description, config) -> { |
| 488 | + final String dest = Objects.requireNonNullElse( |
| 489 | + ConfigurationUtils.readOptionalStringProperty(description, tag, config, "dest"), |
| 490 | + "target" |
| 491 | + ); |
| 492 | + return new AbstractProcessor(tag, description) { |
| 493 | + @Override |
| 494 | + public IngestDocument execute(final IngestDocument ingestDocument) throws Exception { |
| 495 | + ingestDocument.reroute(dest); |
| 496 | + return ingestDocument; |
| 497 | + } |
| 498 | + |
| 499 | + @Override |
| 500 | + public String getType() { |
| 501 | + return "reroute"; |
| 502 | + } |
| 503 | + |
| 504 | + }; |
396 | 505 | }
|
397 | 506 | );
|
398 | 507 | }
|
|
0 commit comments