19
19
20
20
package org .elasticsearch .ingest ;
21
21
22
+ import org .elasticsearch .ElasticsearchException ;
22
23
import org .elasticsearch .action .ingest .SimulateProcessorResult ;
23
24
24
25
import java .util .ArrayList ;
25
26
import java .util .List ;
26
- import java .util .Set ;
27
27
28
28
/**
29
29
* Processor to be used within Simulate API to keep track of processors executed in pipeline.
@@ -42,14 +42,46 @@ public final class TrackingResultProcessor implements Processor {
42
42
43
43
@ Override
44
44
public IngestDocument execute (IngestDocument ingestDocument ) throws Exception {
45
+ Processor processor = actualProcessor ;
45
46
try {
46
- actualProcessor .execute (ingestDocument );
47
- processorResultList .add (new SimulateProcessorResult (actualProcessor .getTag (), new IngestDocument (ingestDocument )));
47
+ if (processor instanceof ConditionalProcessor ) {
48
+ ConditionalProcessor conditionalProcessor = (ConditionalProcessor ) processor ;
49
+ if (conditionalProcessor .evaluate (ingestDocument ) == false ) {
50
+ return ingestDocument ;
51
+ }
52
+ if (conditionalProcessor .getProcessor () instanceof PipelineProcessor ) {
53
+ processor = conditionalProcessor .getProcessor ();
54
+ }
55
+ }
56
+ if (processor instanceof PipelineProcessor ) {
57
+ PipelineProcessor pipelineProcessor = ((PipelineProcessor ) processor );
58
+ Pipeline pipeline = pipelineProcessor .getPipeline ();
59
+ //runtime check for cycles against a copy of the document. This is needed to properly handle conditionals around pipelines
60
+ try {
61
+ IngestDocument ingestDocumentCopy = new IngestDocument (ingestDocument );
62
+ ingestDocumentCopy .executePipeline (pipelineProcessor .getPipeline ());
63
+ } catch (ElasticsearchException elasticsearchException ) {
64
+ if (elasticsearchException .getCause ().getCause () instanceof IllegalStateException ) {
65
+ throw elasticsearchException ;
66
+ }
67
+ //else do nothing, let the tracking processors throw the exception while recording the path up to the failure
68
+ } catch (Exception e ) {
69
+ // do nothing, let the tracking processors throw the exception while recording the path up to the failure
70
+ }
71
+ //now that we know that there are no cycles between pipelines, decorate the processors for this pipeline and execute it
72
+ CompoundProcessor verbosePipelineProcessor = decorate (pipeline .getCompoundProcessor (), processorResultList );
73
+ Pipeline verbosePipeline = new Pipeline (pipeline .getId (), pipeline .getDescription (), pipeline .getVersion (),
74
+ verbosePipelineProcessor );
75
+ ingestDocument .executePipeline (verbosePipeline );
76
+ } else {
77
+ processor .execute (ingestDocument );
78
+ processorResultList .add (new SimulateProcessorResult (processor .getTag (), new IngestDocument (ingestDocument )));
79
+ }
48
80
} catch (Exception e ) {
49
81
if (ignoreFailure ) {
50
- processorResultList .add (new SimulateProcessorResult (actualProcessor .getTag (), new IngestDocument (ingestDocument ), e ));
82
+ processorResultList .add (new SimulateProcessorResult (processor .getTag (), new IngestDocument (ingestDocument ), e ));
51
83
} else {
52
- processorResultList .add (new SimulateProcessorResult (actualProcessor .getTag (), e ));
84
+ processorResultList .add (new SimulateProcessorResult (processor .getTag (), e ));
53
85
}
54
86
throw e ;
55
87
}
@@ -66,35 +98,19 @@ public String getTag() {
66
98
return actualProcessor .getTag ();
67
99
}
68
100
69
- public static CompoundProcessor decorate (CompoundProcessor compoundProcessor , List <SimulateProcessorResult > processorResultList ,
70
- Set <PipelineProcessor > pipelinesSeen ) {
101
+ public static CompoundProcessor decorate (CompoundProcessor compoundProcessor , List <SimulateProcessorResult > processorResultList ) {
71
102
List <Processor > processors = new ArrayList <>(compoundProcessor .getProcessors ().size ());
72
103
for (Processor processor : compoundProcessor .getProcessors ()) {
73
- if (processor instanceof PipelineProcessor ) {
74
- PipelineProcessor pipelineProcessor = ((PipelineProcessor ) processor );
75
- if (pipelinesSeen .add (pipelineProcessor ) == false ) {
76
- throw new IllegalStateException ("Cycle detected for pipeline: " + pipelineProcessor .getPipeline ().getId ());
77
- }
78
- processors .add (decorate (pipelineProcessor .getPipeline ().getCompoundProcessor (), processorResultList , pipelinesSeen ));
79
- pipelinesSeen .remove (pipelineProcessor );
80
- } else if (processor instanceof CompoundProcessor ) {
81
- processors .add (decorate ((CompoundProcessor ) processor , processorResultList , pipelinesSeen ));
104
+ if (processor instanceof CompoundProcessor ) {
105
+ processors .add (decorate ((CompoundProcessor ) processor , processorResultList ));
82
106
} else {
83
107
processors .add (new TrackingResultProcessor (compoundProcessor .isIgnoreFailure (), processor , processorResultList ));
84
108
}
85
109
}
86
110
List <Processor > onFailureProcessors = new ArrayList <>(compoundProcessor .getProcessors ().size ());
87
111
for (Processor processor : compoundProcessor .getOnFailureProcessors ()) {
88
- if (processor instanceof PipelineProcessor ) {
89
- PipelineProcessor pipelineProcessor = ((PipelineProcessor ) processor );
90
- if (pipelinesSeen .add (pipelineProcessor ) == false ) {
91
- throw new IllegalStateException ("Cycle detected for pipeline: " + pipelineProcessor .getPipeline ().getId ());
92
- }
93
- onFailureProcessors .add (decorate (pipelineProcessor .getPipeline ().getCompoundProcessor (), processorResultList ,
94
- pipelinesSeen ));
95
- pipelinesSeen .remove (pipelineProcessor );
96
- } else if (processor instanceof CompoundProcessor ) {
97
- onFailureProcessors .add (decorate ((CompoundProcessor ) processor , processorResultList , pipelinesSeen ));
112
+ if (processor instanceof CompoundProcessor ) {
113
+ onFailureProcessors .add (decorate ((CompoundProcessor ) processor , processorResultList ));
98
114
} else {
99
115
onFailureProcessors .add (new TrackingResultProcessor (compoundProcessor .isIgnoreFailure (), processor , processorResultList ));
100
116
}
0 commit comments