Search in sources :

Example 1 with SimulateProcessorResult

use of org.opensearch.action.ingest.SimulateProcessorResult in project OpenSearch by opensearch-project.

the class TrackingResultProcessor method execute.

@Override
public void execute(IngestDocument ingestDocument, BiConsumer<IngestDocument, Exception> handler) {
    Tuple<String, Boolean> conditionalWithResult;
    if (conditionalProcessor != null) {
        if (conditionalProcessor.evaluate(ingestDocument) == false) {
            conditionalWithResult = new Tuple<>(conditionalProcessor.getCondition(), Boolean.FALSE);
            processorResultList.add(new SimulateProcessorResult(actualProcessor.getType(), actualProcessor.getTag(), actualProcessor.getDescription(), conditionalWithResult));
            handler.accept(ingestDocument, null);
            return;
        } else {
            conditionalWithResult = new Tuple<>(conditionalProcessor.getCondition(), Boolean.TRUE);
        }
    } else {
        // no condition
        conditionalWithResult = null;
    }
    if (actualProcessor instanceof PipelineProcessor) {
        PipelineProcessor pipelineProcessor = ((PipelineProcessor) actualProcessor);
        Pipeline pipeline = pipelineProcessor.getPipeline(ingestDocument);
        // runtime check for cycles against a copy of the document. This is needed to properly handle conditionals around pipelines
        IngestDocument ingestDocumentCopy = new IngestDocument(ingestDocument);
        Pipeline pipelineToCall = pipelineProcessor.getPipeline(ingestDocument);
        if (pipelineToCall == null) {
            throw new IllegalArgumentException("Pipeline processor configured for non-existent pipeline [" + pipelineProcessor.getPipelineToCallName(ingestDocument) + ']');
        }
        ingestDocumentCopy.executePipeline(pipelineToCall, (result, e) -> {
            // special handling for pipeline cycle errors
            if (e instanceof OpenSearchException && e.getCause() instanceof IllegalStateException && e.getCause().getMessage().startsWith(PIPELINE_CYCLE_ERROR_MESSAGE)) {
                if (ignoreFailure) {
                    processorResultList.add(new SimulateProcessorResult(pipelineProcessor.getType(), pipelineProcessor.getTag(), pipelineProcessor.getDescription(), new IngestDocument(ingestDocument), e, conditionalWithResult));
                } else {
                    processorResultList.add(new SimulateProcessorResult(pipelineProcessor.getType(), pipelineProcessor.getTag(), pipelineProcessor.getDescription(), e, conditionalWithResult));
                }
                handler.accept(null, e);
            } else {
                // now that we know that there are no cycles between pipelines, decorate the processors for this pipeline and
                // execute it
                CompoundProcessor verbosePipelineProcessor = decorate(pipeline.getCompoundProcessor(), null, processorResultList);
                // add the pipeline process to the results
                processorResultList.add(new SimulateProcessorResult(actualProcessor.getType(), actualProcessor.getTag(), actualProcessor.getDescription(), conditionalWithResult));
                Pipeline verbosePipeline = new Pipeline(pipeline.getId(), pipeline.getDescription(), pipeline.getVersion(), verbosePipelineProcessor);
                ingestDocument.executePipeline(verbosePipeline, handler);
            }
        });
        return;
    }
    actualProcessor.execute(ingestDocument, (result, e) -> {
        if (e != null) {
            if (ignoreFailure) {
                processorResultList.add(new SimulateProcessorResult(actualProcessor.getType(), actualProcessor.getTag(), actualProcessor.getDescription(), new IngestDocument(ingestDocument), e, conditionalWithResult));
            } else {
                processorResultList.add(new SimulateProcessorResult(actualProcessor.getType(), actualProcessor.getTag(), actualProcessor.getDescription(), e, conditionalWithResult));
            }
            handler.accept(null, e);
        } else {
            if (result != null) {
                processorResultList.add(new SimulateProcessorResult(actualProcessor.getType(), actualProcessor.getTag(), actualProcessor.getDescription(), new IngestDocument(ingestDocument), conditionalWithResult));
                handler.accept(result, null);
            } else {
                processorResultList.add(new SimulateProcessorResult(actualProcessor.getType(), actualProcessor.getTag(), actualProcessor.getDescription(), conditionalWithResult));
                handler.accept(null, null);
            }
        }
    });
}
Also used : SimulateProcessorResult(org.opensearch.action.ingest.SimulateProcessorResult) OpenSearchException(org.opensearch.OpenSearchException)

Example 2 with SimulateProcessorResult

use of org.opensearch.action.ingest.SimulateProcessorResult in project OpenSearch by opensearch-project.

the class TrackingResultProcessorTests method testActualPipelineProcessor.

public void testActualPipelineProcessor() throws Exception {
    String pipelineId = "pipeline1";
    IngestService ingestService = createIngestService();
    Map<String, Object> pipelineConfig = new HashMap<>();
    pipelineConfig.put("name", pipelineId);
    PipelineProcessor.Factory factory = new PipelineProcessor.Factory(ingestService);
    String key1 = randomAlphaOfLength(10);
    String key2 = randomAlphaOfLength(10);
    String key3 = randomAlphaOfLength(10);
    Pipeline pipeline = new Pipeline(pipelineId, null, null, new CompoundProcessor(new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key1, randomInt());
    }), new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key2, randomInt());
    }), new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key3, randomInt());
    })));
    when(ingestService.getPipeline(pipelineId)).thenReturn(pipeline);
    PipelineProcessor pipelineProcessor = factory.create(Collections.emptyMap(), null, null, pipelineConfig);
    CompoundProcessor actualProcessor = new CompoundProcessor(pipelineProcessor);
    CompoundProcessor trackingProcessor = decorate(actualProcessor, null, resultList);
    trackingProcessor.execute(ingestDocument, (result, e) -> {
    });
    SimulateProcessorResult expectedResult = new SimulateProcessorResult(actualProcessor.getType(), actualProcessor.getTag(), actualProcessor.getDescription(), ingestDocument, null);
    expectedResult.getIngestDocument().getIngestMetadata().put("pipeline", pipelineId);
    verify(ingestService, Mockito.atLeast(1)).getPipeline(pipelineId);
    assertThat(resultList.size(), equalTo(4));
    assertNull(resultList.get(0).getConditionalWithResult());
    assertThat(resultList.get(0).getType(), equalTo("pipeline"));
    assertTrue(resultList.get(1).getIngestDocument().hasField(key1));
    assertFalse(resultList.get(1).getIngestDocument().hasField(key2));
    assertFalse(resultList.get(1).getIngestDocument().hasField(key3));
    assertTrue(resultList.get(2).getIngestDocument().hasField(key1));
    assertTrue(resultList.get(2).getIngestDocument().hasField(key2));
    assertFalse(resultList.get(2).getIngestDocument().hasField(key3));
    assertThat(resultList.get(3).getIngestDocument(), equalTo(expectedResult.getIngestDocument()));
    assertThat(resultList.get(3).getFailure(), nullValue());
    assertThat(resultList.get(3).getProcessorTag(), nullValue());
}
Also used : PipelineProcessorTests.createIngestService(org.opensearch.ingest.PipelineProcessorTests.createIngestService) HashMap(java.util.HashMap) SimulateProcessorResult(org.opensearch.action.ingest.SimulateProcessorResult) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 3 with SimulateProcessorResult

use of org.opensearch.action.ingest.SimulateProcessorResult in project OpenSearch by opensearch-project.

the class TrackingResultProcessorTests method testActualCompoundProcessorWithFalseConditional.

public void testActualCompoundProcessorWithFalseConditional() throws Exception {
    String key1 = randomAlphaOfLength(10);
    String key2 = randomAlphaOfLength(10);
    String key3 = randomAlphaOfLength(10);
    String scriptName = "conditionalScript";
    ScriptService scriptService = new ScriptService(Settings.builder().build(), Collections.singletonMap(Script.DEFAULT_SCRIPT_LANG, new MockScriptEngine(Script.DEFAULT_SCRIPT_LANG, Collections.singletonMap(scriptName, ctx -> false), Collections.emptyMap())), new HashMap<>(ScriptModule.CORE_CONTEXTS));
    CompoundProcessor compoundProcessor = new CompoundProcessor(new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key1, randomInt());
    }), new ConditionalProcessor(randomAlphaOfLength(10), null, new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptName, Collections.emptyMap()), scriptService, new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key2, randomInt());
    })), new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key3, randomInt());
    }));
    CompoundProcessor trackingProcessor = decorate(compoundProcessor, null, resultList);
    trackingProcessor.execute(ingestDocument, (result, e) -> {
    });
    SimulateProcessorResult expectedResult = new SimulateProcessorResult(compoundProcessor.getType(), compoundProcessor.getTag(), compoundProcessor.getDescription(), ingestDocument, null);
    assertThat(resultList.size(), equalTo(3));
    assertTrue(resultList.get(0).getIngestDocument().hasField(key1));
    assertFalse(resultList.get(0).getIngestDocument().hasField(key2));
    assertFalse(resultList.get(0).getIngestDocument().hasField(key3));
    assertThat(resultList.get(1).getConditionalWithResult().v1(), equalTo(scriptName));
    assertThat(resultList.get(1).getConditionalWithResult().v2(), is(Boolean.FALSE));
    assertTrue(resultList.get(2).getIngestDocument().hasField(key1));
    assertFalse(resultList.get(2).getIngestDocument().hasField(key2));
    assertTrue(resultList.get(2).getIngestDocument().hasField(key3));
    assertThat(resultList.get(2).getIngestDocument(), equalTo(expectedResult.getIngestDocument()));
    assertThat(resultList.get(2).getFailure(), nullValue());
    assertThat(resultList.get(2).getProcessorTag(), nullValue());
}
Also used : ScriptService(org.opensearch.script.ScriptService) PipelineProcessorTests.createIngestService(org.opensearch.ingest.PipelineProcessorTests.createIngestService) Arrays(java.util.Arrays) ON_FAILURE_PROCESSOR_TAG_FIELD(org.opensearch.ingest.CompoundProcessor.ON_FAILURE_PROCESSOR_TAG_FIELD) TrackingResultProcessor.decorate(org.opensearch.ingest.TrackingResultProcessor.decorate) ScriptModule(org.opensearch.script.ScriptModule) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) CoreMatchers.not(org.hamcrest.CoreMatchers.not) HashMap(java.util.HashMap) SimulateProcessorResult(org.opensearch.action.ingest.SimulateProcessorResult) ArrayList(java.util.ArrayList) ScriptType(org.opensearch.script.ScriptType) ON_FAILURE_PROCESSOR_TYPE_FIELD(org.opensearch.ingest.CompoundProcessor.ON_FAILURE_PROCESSOR_TYPE_FIELD) ON_FAILURE_MESSAGE_FIELD(org.opensearch.ingest.CompoundProcessor.ON_FAILURE_MESSAGE_FIELD) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) MockScriptEngine(org.opensearch.script.MockScriptEngine) Before(org.junit.Before) ScriptService(org.opensearch.script.ScriptService) Script(org.opensearch.script.Script) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Mockito(org.mockito.Mockito) List(java.util.List) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) Script(org.opensearch.script.Script) SimulateProcessorResult(org.opensearch.action.ingest.SimulateProcessorResult) MockScriptEngine(org.opensearch.script.MockScriptEngine) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 4 with SimulateProcessorResult

use of org.opensearch.action.ingest.SimulateProcessorResult in project OpenSearch by opensearch-project.

the class TrackingResultProcessorTests method testActualPipelineProcessorRepeatedInvocation.

public void testActualPipelineProcessorRepeatedInvocation() throws Exception {
    String pipelineId = "pipeline1";
    IngestService ingestService = createIngestService();
    Map<String, Object> pipelineConfig = new HashMap<>();
    pipelineConfig.put("name", pipelineId);
    PipelineProcessor.Factory factory = new PipelineProcessor.Factory(ingestService);
    String key1 = randomAlphaOfLength(10);
    PipelineProcessor pipelineProcessor = factory.create(Collections.emptyMap(), null, null, pipelineConfig);
    Pipeline pipeline = new Pipeline(pipelineId, null, null, new CompoundProcessor(new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key1, randomInt());
    })));
    when(ingestService.getPipeline(pipelineId)).thenReturn(pipeline);
    // calls the same pipeline twice
    CompoundProcessor actualProcessor = new CompoundProcessor(pipelineProcessor, pipelineProcessor);
    CompoundProcessor trackingProcessor = decorate(actualProcessor, null, resultList);
    trackingProcessor.execute(ingestDocument, (result, e) -> {
    });
    SimulateProcessorResult expectedResult = new SimulateProcessorResult(actualProcessor.getType(), actualProcessor.getTag(), actualProcessor.getDescription(), ingestDocument, null);
    expectedResult.getIngestDocument().getIngestMetadata().put("pipeline", pipelineId);
    verify(ingestService, Mockito.atLeast(2)).getPipeline(pipelineId);
    assertThat(resultList.size(), equalTo(4));
    assertNull(resultList.get(0).getConditionalWithResult());
    assertThat(resultList.get(0).getType(), equalTo("pipeline"));
    assertThat(resultList.get(1).getIngestDocument(), not(equalTo(expectedResult.getIngestDocument())));
    assertThat(resultList.get(1).getFailure(), nullValue());
    assertThat(resultList.get(1).getProcessorTag(), nullValue());
    assertNull(resultList.get(2).getConditionalWithResult());
    assertThat(resultList.get(2).getType(), equalTo("pipeline"));
    assertThat(resultList.get(3).getIngestDocument(), equalTo(expectedResult.getIngestDocument()));
    assertThat(resultList.get(3).getFailure(), nullValue());
    assertThat(resultList.get(3).getProcessorTag(), nullValue());
    // each invocation updates key1 with a random int
    assertNotEquals(resultList.get(1).getIngestDocument().getSourceAndMetadata().get(key1), resultList.get(3).getIngestDocument().getSourceAndMetadata().get(key1));
}
Also used : PipelineProcessorTests.createIngestService(org.opensearch.ingest.PipelineProcessorTests.createIngestService) HashMap(java.util.HashMap) SimulateProcessorResult(org.opensearch.action.ingest.SimulateProcessorResult) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 5 with SimulateProcessorResult

use of org.opensearch.action.ingest.SimulateProcessorResult in project OpenSearch by opensearch-project.

the class TrackingResultProcessorTests method testActualPipelineProcessorWithFalseConditional.

public void testActualPipelineProcessorWithFalseConditional() throws Exception {
    String pipelineId1 = "pipeline1";
    String pipelineId2 = "pipeline2";
    IngestService ingestService = createIngestService();
    Map<String, Object> pipelineConfig0 = new HashMap<>();
    pipelineConfig0.put("name", pipelineId1);
    Map<String, Object> pipelineConfig1 = new HashMap<>();
    pipelineConfig1.put("name", pipelineId1);
    Map<String, Object> pipelineConfig2 = new HashMap<>();
    pipelineConfig2.put("name", pipelineId2);
    PipelineProcessor.Factory factory = new PipelineProcessor.Factory(ingestService);
    String key1 = randomAlphaOfLength(10);
    String key2 = randomAlphaOfLength(10);
    String key3 = randomAlphaOfLength(10);
    String scriptName = "conditionalScript";
    ScriptService scriptService = new ScriptService(Settings.builder().build(), Collections.singletonMap(Script.DEFAULT_SCRIPT_LANG, new MockScriptEngine(Script.DEFAULT_SCRIPT_LANG, Collections.singletonMap(scriptName, ctx -> false), Collections.emptyMap())), new HashMap<>(ScriptModule.CORE_CONTEXTS));
    Pipeline pipeline1 = new Pipeline(pipelineId1, null, null, new CompoundProcessor(new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key1, randomInt());
    }), new ConditionalProcessor(randomAlphaOfLength(10), null, new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptName, Collections.emptyMap()), scriptService, factory.create(Collections.emptyMap(), null, null, pipelineConfig2)), new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key3, randomInt());
    })));
    Pipeline pipeline2 = new Pipeline(pipelineId2, null, null, new CompoundProcessor(new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key2, randomInt());
    })));
    when(ingestService.getPipeline(pipelineId1)).thenReturn(pipeline1);
    when(ingestService.getPipeline(pipelineId2)).thenReturn(pipeline2);
    PipelineProcessor pipelineProcessor = factory.create(Collections.emptyMap(), null, null, pipelineConfig0);
    CompoundProcessor actualProcessor = new CompoundProcessor(pipelineProcessor);
    CompoundProcessor trackingProcessor = decorate(actualProcessor, null, resultList);
    trackingProcessor.execute(ingestDocument, (result, e) -> {
    });
    SimulateProcessorResult expectedResult = new SimulateProcessorResult(actualProcessor.getType(), actualProcessor.getTag(), actualProcessor.getDescription(), ingestDocument, null);
    expectedResult.getIngestDocument().getIngestMetadata().put("pipeline", pipelineId1);
    verify(ingestService, Mockito.atLeast(1)).getPipeline(pipelineId1);
    verify(ingestService, Mockito.never()).getPipeline(pipelineId2);
    assertThat(resultList.size(), equalTo(4));
    assertNull(resultList.get(0).getConditionalWithResult());
    assertThat(resultList.get(0).getType(), equalTo("pipeline"));
    assertTrue(resultList.get(1).getIngestDocument().hasField(key1));
    assertFalse(resultList.get(1).getIngestDocument().hasField(key2));
    assertFalse(resultList.get(1).getIngestDocument().hasField(key3));
    assertThat(resultList.get(2).getConditionalWithResult().v1(), equalTo(scriptName));
    assertThat(resultList.get(2).getConditionalWithResult().v2(), is(Boolean.FALSE));
    assertThat(resultList.get(3).getIngestDocument(), equalTo(expectedResult.getIngestDocument()));
    assertThat(resultList.get(3).getFailure(), nullValue());
    assertThat(resultList.get(3).getProcessorTag(), nullValue());
}
Also used : PipelineProcessorTests.createIngestService(org.opensearch.ingest.PipelineProcessorTests.createIngestService) Arrays(java.util.Arrays) ON_FAILURE_PROCESSOR_TAG_FIELD(org.opensearch.ingest.CompoundProcessor.ON_FAILURE_PROCESSOR_TAG_FIELD) TrackingResultProcessor.decorate(org.opensearch.ingest.TrackingResultProcessor.decorate) ScriptModule(org.opensearch.script.ScriptModule) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) CoreMatchers.not(org.hamcrest.CoreMatchers.not) HashMap(java.util.HashMap) SimulateProcessorResult(org.opensearch.action.ingest.SimulateProcessorResult) ArrayList(java.util.ArrayList) ScriptType(org.opensearch.script.ScriptType) ON_FAILURE_PROCESSOR_TYPE_FIELD(org.opensearch.ingest.CompoundProcessor.ON_FAILURE_PROCESSOR_TYPE_FIELD) ON_FAILURE_MESSAGE_FIELD(org.opensearch.ingest.CompoundProcessor.ON_FAILURE_MESSAGE_FIELD) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) MockScriptEngine(org.opensearch.script.MockScriptEngine) Before(org.junit.Before) ScriptService(org.opensearch.script.ScriptService) Script(org.opensearch.script.Script) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Mockito(org.mockito.Mockito) List(java.util.List) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) Script(org.opensearch.script.Script) PipelineProcessorTests.createIngestService(org.opensearch.ingest.PipelineProcessorTests.createIngestService) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) ScriptService(org.opensearch.script.ScriptService) SimulateProcessorResult(org.opensearch.action.ingest.SimulateProcessorResult) MockScriptEngine(org.opensearch.script.MockScriptEngine)

Aggregations

SimulateProcessorResult (org.opensearch.action.ingest.SimulateProcessorResult)13 Matchers.containsString (org.hamcrest.Matchers.containsString)9 HashMap (java.util.HashMap)8 PipelineProcessorTests.createIngestService (org.opensearch.ingest.PipelineProcessorTests.createIngestService)8 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 List (java.util.List)4 Map (java.util.Map)4 CoreMatchers.equalTo (org.hamcrest.CoreMatchers.equalTo)4 CoreMatchers.not (org.hamcrest.CoreMatchers.not)4 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)4 Matchers.is (org.hamcrest.Matchers.is)4 Matchers.nullValue (org.hamcrest.Matchers.nullValue)4 Matchers.sameInstance (org.hamcrest.Matchers.sameInstance)4 Before (org.junit.Before)4 Mockito (org.mockito.Mockito)4 Mockito.verify (org.mockito.Mockito.verify)4 Mockito.when (org.mockito.Mockito.when)4 Settings (org.opensearch.common.settings.Settings)4