-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
INGEST: Add Pipeline Processor #32473
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e140735
INGEST: Add Pipeline Processor
original-brownbear ed9880c
Merge remote-tracking branch 'elastic/master' into 31842
original-brownbear 6d6475d
add recursion detection + its
original-brownbear 34772e1
Merge remote-tracking branch 'elastic/master' into 31842
original-brownbear 018cab5
CR: remove deadcode and move exception throwing
original-brownbear c6c0bf4
cleanup random finals
original-brownbear 4ef99ea
CR: Add simple test
original-brownbear 90957f4
CR: add tests
original-brownbear cac9f27
Merge remote-tracking branch 'elastic/master' into 31842
original-brownbear 3b27a82
Merge remote-tracking branch 'elastic/master' into 31842
original-brownbear f35dd6f
Merge remote-tracking branch 'elastic/master' into 31842
original-brownbear d9ac299
Merge remote-tracking branch 'elastic/master' into 31842
original-brownbear 742ab8f
Merge remote-tracking branch 'elastic/master' into 31842
original-brownbear File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/PipelineProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.ingest.common; | ||
|
||
import java.util.Map; | ||
import org.elasticsearch.ingest.AbstractProcessor; | ||
import org.elasticsearch.ingest.ConfigurationUtils; | ||
import org.elasticsearch.ingest.IngestDocument; | ||
import org.elasticsearch.ingest.IngestService; | ||
import org.elasticsearch.ingest.Pipeline; | ||
import org.elasticsearch.ingest.Processor; | ||
|
||
public class PipelineProcessor extends AbstractProcessor { | ||
|
||
public static final String TYPE = "pipeline"; | ||
|
||
private final String pipelineName; | ||
|
||
private final IngestService ingestService; | ||
|
||
private PipelineProcessor(String tag, String pipelineName, IngestService ingestService) { | ||
super(tag); | ||
this.pipelineName = pipelineName; | ||
this.ingestService = ingestService; | ||
} | ||
|
||
@Override | ||
public void execute(IngestDocument ingestDocument) throws Exception { | ||
Pipeline pipeline = ingestService.getPipeline(pipelineName); | ||
if (pipeline == null) { | ||
throw new IllegalStateException("Pipeline processor configured for non-existent pipeline [" + pipelineName + ']'); | ||
} | ||
ingestDocument.executePipeline(pipeline); | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return TYPE; | ||
} | ||
|
||
public static final class Factory implements Processor.Factory { | ||
|
||
private final IngestService ingestService; | ||
|
||
public Factory(IngestService ingestService) { | ||
this.ingestService = ingestService; | ||
} | ||
|
||
@Override | ||
public PipelineProcessor create(Map<String, Processor.Factory> registry, String processorTag, | ||
Map<String, Object> config) throws Exception { | ||
String pipeline = | ||
ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "pipeline"); | ||
return new PipelineProcessor(processorTag, pipeline, ingestService); | ||
} | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
...s/ingest-common/src/test/java/org/elasticsearch/ingest/common/PipelineProcessorTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.ingest.common; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
import org.elasticsearch.ElasticsearchException; | ||
import org.elasticsearch.ingest.CompoundProcessor; | ||
import org.elasticsearch.ingest.IngestDocument; | ||
import org.elasticsearch.ingest.IngestService; | ||
import org.elasticsearch.ingest.Pipeline; | ||
import org.elasticsearch.ingest.Processor; | ||
import org.elasticsearch.ingest.RandomDocumentPicks; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class PipelineProcessorTests extends ESTestCase { | ||
|
||
public void testExecutesPipeline() throws Exception { | ||
String pipelineId = "pipeline"; | ||
IngestService ingestService = mock(IngestService.class); | ||
CompletableFuture<IngestDocument> invoked = new CompletableFuture<>(); | ||
IngestDocument testIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); | ||
Pipeline pipeline = new Pipeline( | ||
pipelineId, null, null, | ||
new CompoundProcessor(new Processor() { | ||
@Override | ||
public void execute(final IngestDocument ingestDocument) throws Exception { | ||
invoked.complete(ingestDocument); | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getTag() { | ||
return null; | ||
} | ||
}) | ||
); | ||
when(ingestService.getPipeline(pipelineId)).thenReturn(pipeline); | ||
PipelineProcessor.Factory factory = new PipelineProcessor.Factory(ingestService); | ||
Map<String, Object> config = new HashMap<>(); | ||
config.put("pipeline", pipelineId); | ||
factory.create(Collections.emptyMap(), null, config).execute(testIngestDocument); | ||
assertEquals(testIngestDocument, invoked.get()); | ||
} | ||
|
||
public void testThrowsOnMissingPipeline() throws Exception { | ||
IngestService ingestService = mock(IngestService.class); | ||
IngestDocument testIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); | ||
PipelineProcessor.Factory factory = new PipelineProcessor.Factory(ingestService); | ||
Map<String, Object> config = new HashMap<>(); | ||
config.put("pipeline", "missingPipelineId"); | ||
IllegalStateException e = expectThrows( | ||
IllegalStateException.class, | ||
() -> factory.create(Collections.emptyMap(), null, config).execute(testIngestDocument) | ||
); | ||
assertEquals( | ||
"Pipeline processor configured for non-existent pipeline [missingPipelineId]", e.getMessage() | ||
); | ||
} | ||
|
||
public void testThrowsOnRecursivePipelineInvocations() throws Exception { | ||
String innerPipelineId = "inner"; | ||
String outerPipelineId = "outer"; | ||
IngestService ingestService = mock(IngestService.class); | ||
IngestDocument testIngestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); | ||
Map<String, Object> outerConfig = new HashMap<>(); | ||
outerConfig.put("pipeline", innerPipelineId); | ||
PipelineProcessor.Factory factory = new PipelineProcessor.Factory(ingestService); | ||
Pipeline outer = new Pipeline( | ||
outerPipelineId, null, null, | ||
new CompoundProcessor(factory.create(Collections.emptyMap(), null, outerConfig)) | ||
); | ||
Map<String, Object> innerConfig = new HashMap<>(); | ||
innerConfig.put("pipeline", outerPipelineId); | ||
Pipeline inner = new Pipeline( | ||
innerPipelineId, null, null, | ||
new CompoundProcessor(factory.create(Collections.emptyMap(), null, innerConfig)) | ||
); | ||
when(ingestService.getPipeline(outerPipelineId)).thenReturn(outer); | ||
when(ingestService.getPipeline(innerPipelineId)).thenReturn(inner); | ||
outerConfig.put("pipeline", innerPipelineId); | ||
ElasticsearchException e = expectThrows( | ||
ElasticsearchException.class, | ||
() -> factory.create(Collections.emptyMap(), null, outerConfig).execute(testIngestDocument) | ||
); | ||
assertEquals( | ||
"Recursive invocation of pipeline [inner] detected.", e.getRootCause().getMessage() | ||
); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
...les/ingest-common/src/test/resources/rest-api-spec/test/ingest/210_pipeline_processor.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
--- | ||
teardown: | ||
- do: | ||
ingest.delete_pipeline: | ||
id: "inner" | ||
ignore: 404 | ||
|
||
- do: | ||
ingest.delete_pipeline: | ||
id: "outer" | ||
ignore: 404 | ||
|
||
--- | ||
"Test Pipeline Processor with Simple Inner Pipeline": | ||
- do: | ||
ingest.put_pipeline: | ||
id: "inner" | ||
body: > | ||
{ | ||
"description" : "inner pipeline", | ||
"processors" : [ | ||
{ | ||
"set" : { | ||
"field": "foo", | ||
"value": "bar" | ||
} | ||
}, | ||
{ | ||
"set" : { | ||
"field": "baz", | ||
"value": "blub" | ||
} | ||
} | ||
] | ||
} | ||
- match: { acknowledged: true } | ||
|
||
- do: | ||
ingest.put_pipeline: | ||
id: "outer" | ||
body: > | ||
{ | ||
"description" : "outer pipeline", | ||
"processors" : [ | ||
{ | ||
"pipeline" : { | ||
"pipeline": "inner" | ||
} | ||
} | ||
] | ||
} | ||
- match: { acknowledged: true } | ||
|
||
- do: | ||
index: | ||
index: test | ||
type: test | ||
id: 1 | ||
pipeline: "outer" | ||
body: {} | ||
|
||
- do: | ||
get: | ||
index: test | ||
type: test | ||
id: 1 | ||
- match: { _source.foo: "bar" } | ||
- match: { _source.baz: "blub" } | ||
|
||
--- | ||
"Test Pipeline Processor with Circular Pipelines": | ||
- do: | ||
ingest.put_pipeline: | ||
id: "outer" | ||
body: > | ||
{ | ||
"description" : "outer pipeline", | ||
"processors" : [ | ||
{ | ||
"pipeline" : { | ||
"pipeline": "inner" | ||
} | ||
} | ||
] | ||
} | ||
- match: { acknowledged: true } | ||
|
||
- do: | ||
ingest.put_pipeline: | ||
id: "inner" | ||
body: > | ||
{ | ||
"description" : "inner pipeline", | ||
"processors" : [ | ||
{ | ||
"pipeline" : { | ||
"pipeline": "outer" | ||
} | ||
} | ||
] | ||
} | ||
- match: { acknowledged: true } | ||
|
||
- do: | ||
catch: /illegal_state_exception/ | ||
index: | ||
index: test | ||
type: test | ||
id: 1 | ||
pipeline: "outer" | ||
body: {} | ||
- match: { error.root_cause.0.type: "exception" } | ||
- match: { error.root_cause.0.reason: "java.lang.IllegalArgumentException: java.lang.IllegalStateException: Recursive invocation of pipeline [inner] detected." } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using a set, I think there a call stack could be passed through, that doesn't need to be a member variable? I don't know ingest document coming more mutable than it already is. This method signature is also odd, as I would expect this to be an exception, but it looks like you are avoiding that because it would collide with exceptions that could be thrown from the pipeline itself? But this should fail the pipeline anyways, so I think it is ok to use an exception?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rjernst I can't really pass the callstack with the document can I? I only have the
execute
method available to pass things (because any called pipeline itself could contain additional pipeline processors).The reason I made this return
boolean
instead of throwing right away was more of a style thing to make it clear that the exception was triggered by the pipeline processor. But in hindsight this may be a little needlessly complex :) Moving it in here.