14
14
import org .elasticsearch .action .ActionListener ;
15
15
import org .elasticsearch .client .internal .Client ;
16
16
import org .elasticsearch .cluster .ClusterState ;
17
- import org .elasticsearch .cluster .metadata .Metadata ;
18
17
import org .elasticsearch .cluster .service .ClusterService ;
19
18
import org .elasticsearch .common .settings .Setting ;
20
19
import org .elasticsearch .common .settings .Settings ;
21
20
import org .elasticsearch .common .xcontent .LoggingDeprecationHandler ;
22
21
import org .elasticsearch .ingest .AbstractProcessor ;
23
22
import org .elasticsearch .ingest .ConfigurationUtils ;
24
23
import org .elasticsearch .ingest .IngestDocument ;
25
- import org .elasticsearch .ingest .IngestMetadata ;
26
- import org .elasticsearch .ingest .Pipeline ;
27
- import org .elasticsearch .ingest .PipelineConfiguration ;
28
24
import org .elasticsearch .ingest .Processor ;
29
25
import org .elasticsearch .rest .RestStatus ;
30
26
import org .elasticsearch .xpack .core .ml .action .InferModelAction ;
55
51
import org .elasticsearch .xpack .core .ml .utils .ExceptionsHelper ;
56
52
import org .elasticsearch .xpack .ml .inference .loadingservice .LocalModel ;
57
53
import org .elasticsearch .xpack .ml .notifications .InferenceAuditor ;
54
+ import org .elasticsearch .xpack .ml .utils .InferenceProcessorInfoExtractor ;
58
55
59
56
import java .util .Collections ;
60
57
import java .util .HashMap ;
65
62
import java .util .function .Consumer ;
66
63
67
64
import static org .elasticsearch .ingest .IngestDocument .INGEST_KEY ;
68
- import static org .elasticsearch .ingest .Pipeline .PROCESSORS_KEY ;
69
65
import static org .elasticsearch .xpack .core .ClientHelper .ML_ORIGIN ;
70
66
import static org .elasticsearch .xpack .core .ClientHelper .executeAsyncWithOrigin ;
71
67
import static org .elasticsearch .xpack .core .ml .inference .results .InferenceResults .MODEL_ID_RESULTS_FIELD ;
@@ -192,9 +188,6 @@ public String getType() {
192
188
193
189
public static final class Factory implements Processor .Factory , Consumer <ClusterState > {
194
190
195
- private static final String FOREACH_PROCESSOR_NAME = "foreach" ;
196
- // Any more than 10 nestings of processors, we stop searching for inference processor definitions
197
- private static final int MAX_INFERENCE_PROCESSOR_SEARCH_RECURSIONS = 10 ;
198
191
private static final Logger logger = LogManager .getLogger (Factory .class );
199
192
200
193
private final Client client ;
@@ -213,86 +206,12 @@ public Factory(Client client, ClusterService clusterService, Settings settings)
213
206
@ Override
214
207
public void accept (ClusterState state ) {
215
208
minNodeVersion = state .nodes ().getMinNodeVersion ();
216
- currentInferenceProcessors = countNumberInferenceProcessors (state );
217
- }
218
-
219
- public static int countNumberInferenceProcessors (ClusterState state ) {
220
- Metadata metadata = state .getMetadata ();
221
- if (metadata == null ) {
222
- return 0 ;
223
- }
224
- IngestMetadata ingestMetadata = metadata .custom (IngestMetadata .TYPE );
225
- if (ingestMetadata == null ) {
226
- return 0 ;
227
- }
228
-
229
- int count = 0 ;
230
- for (PipelineConfiguration configuration : ingestMetadata .getPipelines ().values ()) {
231
- Map <String , Object > configMap = configuration .getConfigAsMap ();
232
- try {
233
- List <Map <String , Object >> processorConfigs = ConfigurationUtils .readList (null , null , configMap , PROCESSORS_KEY );
234
- for (Map <String , Object > processorConfigWithKey : processorConfigs ) {
235
- for (Map .Entry <String , Object > entry : processorConfigWithKey .entrySet ()) {
236
- count += numInferenceProcessors (entry .getKey (), entry .getValue ());
237
- }
238
- }
239
- // We cannot throw any exception here. It might break other pipelines.
240
- } catch (Exception ex ) {
241
- logger .debug (() -> "failed gathering processors for pipeline [" + configuration .getId () + "]" , ex );
242
- }
209
+ try {
210
+ currentInferenceProcessors = InferenceProcessorInfoExtractor .countInferenceProcessors (state );
211
+ } catch (Exception ex ) {
212
+ // We cannot throw any exception here. It might break other pipelines.
213
+ logger .debug ("failed gathering processors for pipelines" , ex );
243
214
}
244
- return count ;
245
- }
246
-
247
- @ SuppressWarnings ("unchecked" )
248
- static int numInferenceProcessors (String processorType , Object processorDefinition ) {
249
- return numInferenceProcessors (processorType , (Map <String , Object >) processorDefinition , 0 );
250
- }
251
-
252
- @ SuppressWarnings ("unchecked" )
253
- static int numInferenceProcessors (String processorType , Map <String , Object > processorDefinition , int level ) {
254
- int count = 0 ;
255
- // arbitrary, but we must limit this somehow
256
- if (level > MAX_INFERENCE_PROCESSOR_SEARCH_RECURSIONS ) {
257
- return count ;
258
- }
259
- if (processorType == null || processorDefinition == null ) {
260
- return count ;
261
- }
262
- if (TYPE .equals (processorType )) {
263
- count ++;
264
- }
265
- if (FOREACH_PROCESSOR_NAME .equals (processorType )) {
266
- Map <String , Object > innerProcessor = (Map <String , Object >) processorDefinition .get ("processor" );
267
- if (innerProcessor != null ) {
268
- // a foreach processor should only have a SINGLE nested processor. Iteration is for simplicity's sake.
269
- for (Map .Entry <String , Object > innerProcessorWithName : innerProcessor .entrySet ()) {
270
- count += numInferenceProcessors (
271
- innerProcessorWithName .getKey (),
272
- (Map <String , Object >) innerProcessorWithName .getValue (),
273
- level + 1
274
- );
275
- }
276
- }
277
- }
278
- if (processorDefinition .containsKey (Pipeline .ON_FAILURE_KEY )) {
279
- List <Map <String , Object >> onFailureConfigs = ConfigurationUtils .readList (
280
- null ,
281
- null ,
282
- processorDefinition ,
283
- Pipeline .ON_FAILURE_KEY
284
- );
285
- count += onFailureConfigs .stream ()
286
- .flatMap (map -> map .entrySet ().stream ())
287
- .mapToInt (entry -> numInferenceProcessors (entry .getKey (), (Map <String , Object >) entry .getValue (), level + 1 ))
288
- .sum ();
289
- }
290
- return count ;
291
- }
292
-
293
- // Used for testing
294
- int numInferenceProcessors () {
295
- return currentInferenceProcessors ;
296
215
}
297
216
298
217
@ Override
0 commit comments