Search in sources :

Example 1 with AbstractProcessor

use of org.opensearch.ingest.AbstractProcessor in project OpenSearch by opensearch-project.

the class SimulateExecutionServiceTests method testAsyncSimulation.

public void testAsyncSimulation() throws Exception {
    int numDocs = randomIntBetween(1, 64);
    List<IngestDocument> documents = new ArrayList<>(numDocs);
    for (int id = 0; id < numDocs; id++) {
        documents.add(new IngestDocument("_index", Integer.toString(id), null, 0L, VersionType.INTERNAL, new HashMap<>()));
    }
    Processor processor1 = new AbstractProcessor(null, null) {

        @Override
        public void execute(IngestDocument ingestDocument, BiConsumer<IngestDocument, Exception> handler) {
            threadPool.executor(ThreadPool.Names.GENERIC).execute(() -> {
                ingestDocument.setFieldValue("processed", true);
                handler.accept(ingestDocument, null);
            });
        }

        @Override
        public IngestDocument execute(IngestDocument ingestDocument) throws Exception {
            throw new UnsupportedOperationException();
        }

        @Override
        public String getType() {
            return "none-of-your-business";
        }
    };
    Pipeline pipeline = new Pipeline("_id", "_description", version, new CompoundProcessor(processor1));
    SimulatePipelineRequest.Parsed request = new SimulatePipelineRequest.Parsed(pipeline, documents, false);
    AtomicReference<SimulatePipelineResponse> responseHolder = new AtomicReference<>();
    AtomicReference<Exception> errorHolder = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    executionService.execute(request, ActionListener.wrap(response -> {
        responseHolder.set(response);
        latch.countDown();
    }, e -> {
        errorHolder.set(e);
        latch.countDown();
    }));
    latch.await(1, TimeUnit.MINUTES);
    assertThat(errorHolder.get(), nullValue());
    SimulatePipelineResponse response = responseHolder.get();
    assertThat(response, notNullValue());
    assertThat(response.getResults().size(), equalTo(numDocs));
    for (int id = 0; id < numDocs; id++) {
        SimulateDocumentBaseResult result = (SimulateDocumentBaseResult) response.getResults().get(id);
        assertThat(result.getIngestDocument().getMetadata().get(IngestDocument.Metadata.ID), equalTo(Integer.toString(id)));
        assertThat(result.getIngestDocument().getSourceAndMetadata().get("processed"), is(true));
    }
}
Also used : ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Matchers.not(org.hamcrest.Matchers.not) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) Processor(org.opensearch.ingest.Processor) ArrayList(java.util.ArrayList) IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) After(org.junit.After) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) BiConsumer(java.util.function.BiConsumer) AbstractProcessor(org.opensearch.ingest.AbstractProcessor) CompoundProcessor(org.opensearch.ingest.CompoundProcessor) TestProcessor(org.opensearch.ingest.TestProcessor) ActionListener(org.opensearch.action.ActionListener) RandomDocumentPicks(org.opensearch.ingest.RandomDocumentPicks) Before(org.junit.Before) DropProcessor(org.opensearch.ingest.DropProcessor) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Pipeline(org.opensearch.ingest.Pipeline) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) TimeUnit(java.util.concurrent.TimeUnit) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) CountDownLatch(java.util.concurrent.CountDownLatch) VersionType(org.opensearch.index.VersionType) List(java.util.List) IngestProcessorException(org.opensearch.ingest.IngestProcessorException) IngestDocument(org.opensearch.ingest.IngestDocument) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) Processor(org.opensearch.ingest.Processor) AbstractProcessor(org.opensearch.ingest.AbstractProcessor) CompoundProcessor(org.opensearch.ingest.CompoundProcessor) TestProcessor(org.opensearch.ingest.TestProcessor) DropProcessor(org.opensearch.ingest.DropProcessor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CompoundProcessor(org.opensearch.ingest.CompoundProcessor) IngestDocumentMatcher.assertIngestDocument(org.opensearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.opensearch.ingest.IngestDocument) AbstractProcessor(org.opensearch.ingest.AbstractProcessor) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IngestProcessorException(org.opensearch.ingest.IngestProcessorException) Pipeline(org.opensearch.ingest.Pipeline) BiConsumer(java.util.function.BiConsumer)

Aggregations

ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 BiConsumer (java.util.function.BiConsumer)1 Matchers.equalTo (org.hamcrest.Matchers.equalTo)1 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)1 Matchers.is (org.hamcrest.Matchers.is)1 Matchers.not (org.hamcrest.Matchers.not)1 Matchers.notNullValue (org.hamcrest.Matchers.notNullValue)1 Matchers.nullValue (org.hamcrest.Matchers.nullValue)1 Matchers.sameInstance (org.hamcrest.Matchers.sameInstance)1 After (org.junit.After)1 Before (org.junit.Before)1 ActionListener (org.opensearch.action.ActionListener)1 VersionType (org.opensearch.index.VersionType)1