Search in sources :

Example 1 with CheckpointState

use of com.amazon.dataprepper.model.CheckpointState in project data-prepper by opensearch-project.

the class HTTPSourceTest method testHTTPJsonResponse200.

@Test
public void testHTTPJsonResponse200() {
    // Prepare
    final String testData = "[{\"log\": \"somelog\"}]";
    final int testPayloadSize = testData.getBytes().length;
    HTTPSourceUnderTest.start(testBuffer);
    refreshMeasurements();
    // When
    WebClient.of().execute(RequestHeaders.builder().scheme(SessionProtocol.HTTP).authority("127.0.0.1:2021").method(HttpMethod.POST).path("/log/ingest").contentType(MediaType.JSON_UTF_8).build(), HttpData.ofUtf8(testData)).aggregate().whenComplete((i, ex) -> assertSecureResponseWithStatusCode(i, HttpStatus.OK)).join();
    // Then
    Assertions.assertFalse(testBuffer.isEmpty());
    final Map.Entry<Collection<Record<Log>>, CheckpointState> result = testBuffer.read(100);
    List<Record<Log>> records = new ArrayList<>(result.getKey());
    Assertions.assertEquals(1, records.size());
    final Record<Log> record = records.get(0);
    Assertions.assertEquals("somelog", record.getData().get("log", String.class));
    // Verify metrics
    final Measurement requestReceivedCount = MetricsTestUtil.getMeasurementFromList(requestsReceivedMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, requestReceivedCount.getValue());
    final Measurement successRequestsCount = MetricsTestUtil.getMeasurementFromList(successRequestsMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, successRequestsCount.getValue());
    final Measurement requestProcessDurationCount = MetricsTestUtil.getMeasurementFromList(requestProcessDurationMeasurements, Statistic.COUNT);
    Assertions.assertEquals(1.0, requestProcessDurationCount.getValue());
    final Measurement requestProcessDurationMax = MetricsTestUtil.getMeasurementFromList(requestProcessDurationMeasurements, Statistic.MAX);
    Assertions.assertTrue(requestProcessDurationMax.getValue() > 0);
    final Measurement payloadSizeMax = MetricsTestUtil.getMeasurementFromList(payloadSizeSummaryMeasurements, Statistic.MAX);
    Assertions.assertEquals(testPayloadSize, payloadSizeMax.getValue());
}
Also used : Statistic(io.micrometer.core.instrument.Statistic) BeforeEach(org.junit.jupiter.api.BeforeEach) RequestHeaders(com.linecorp.armeria.common.RequestHeaders) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) MetricsTestUtil(com.amazon.dataprepper.metrics.MetricsTestUtil) Matchers.not(org.hamcrest.Matchers.not) ArmeriaHttpAuthenticationProvider(com.amazon.dataprepper.armeria.authentication.ArmeriaHttpAuthenticationProvider) AsciiString(io.netty.util.AsciiString) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Server(com.linecorp.armeria.server.Server) WebClient(com.linecorp.armeria.client.WebClient) Map(java.util.Map) Record(com.amazon.dataprepper.model.record.Record) PluginFactory(com.amazon.dataprepper.model.plugin.PluginFactory) Path(java.nio.file.Path) ClientFactory(com.linecorp.armeria.client.ClientFactory) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) CheckpointState(com.amazon.dataprepper.model.CheckpointState) Collection(java.util.Collection) CompletionException(java.util.concurrent.CompletionException) HttpMethod(com.linecorp.armeria.common.HttpMethod) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) IOUtils(org.apache.commons.io.IOUtils) MockedStatic(org.mockito.MockedStatic) List(java.util.List) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) ServerBuilder(com.linecorp.armeria.server.ServerBuilder) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SessionProtocol(com.linecorp.armeria.common.SessionProtocol) Mock(org.mockito.Mock) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) MetricNames(com.amazon.dataprepper.metrics.MetricNames) ResponseTimeoutException(com.linecorp.armeria.client.ResponseTimeoutException) Mockito.lenient(org.mockito.Mockito.lenient) MediaType(com.linecorp.armeria.common.MediaType) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) HttpStatus(com.linecorp.armeria.common.HttpStatus) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) BlockingBuffer(com.amazon.dataprepper.plugins.buffer.blockingbuffer.BlockingBuffer) PluginMetrics(com.amazon.dataprepper.metrics.PluginMetrics) HttpData(com.linecorp.armeria.common.HttpData) PluginSetting(com.amazon.dataprepper.model.configuration.PluginSetting) Log(com.amazon.dataprepper.model.log.Log) Files(java.nio.file.Files) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Measurement(io.micrometer.core.instrument.Measurement) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) StringJoiner(java.util.StringJoiner) Assertions(org.junit.jupiter.api.Assertions) Mockito.reset(org.mockito.Mockito.reset) InputStream(java.io.InputStream) Measurement(io.micrometer.core.instrument.Measurement) Log(com.amazon.dataprepper.model.log.Log) ArrayList(java.util.ArrayList) AsciiString(io.netty.util.AsciiString) Collection(java.util.Collection) Record(com.amazon.dataprepper.model.record.Record) CheckpointState(com.amazon.dataprepper.model.CheckpointState) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 2 with CheckpointState

use of com.amazon.dataprepper.model.CheckpointState in project data-prepper by opensearch-project.

the class AbstractBufferTest method testCheckpointMetrics.

@Test
public void testCheckpointMetrics() throws Exception {
    // Given
    final AbstractBuffer<Record<String>> abstractBuffer = new AbstractBufferImpl(testPluginSetting);
    final Collection<Record<String>> testRecords = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        testRecords.add(new Record<>(UUID.randomUUID().toString()));
    }
    abstractBuffer.writeAll(testRecords, 1000);
    final Map.Entry<Collection<Record<String>>, CheckpointState> readResult = abstractBuffer.read(1000);
    final List<Measurement> checkpointTimeMeasurements = MetricsTestUtil.getMeasurementList(new StringJoiner(MetricNames.DELIMITER).add(PIPELINE_NAME).add(BUFFER_NAME).add(MetricNames.CHECKPOINT_TIME_ELAPSED).toString());
    Assert.assertEquals(0.0, MetricsTestUtil.getMeasurementFromList(checkpointTimeMeasurements, Statistic.COUNT).getValue(), 0);
    Assert.assertEquals(0.0, MetricsTestUtil.getMeasurementFromList(checkpointTimeMeasurements, Statistic.TOTAL_TIME).getValue(), 0);
    // When
    abstractBuffer.checkpoint(readResult.getValue());
    // Then
    Assert.assertEquals(0, abstractBuffer.getRecordsInFlight());
    final List<Measurement> recordsInFlightMeasurements = MetricsTestUtil.getMeasurementList(new StringJoiner(MetricNames.DELIMITER).add(PIPELINE_NAME).add(BUFFER_NAME).add(MetricNames.RECORDS_INFLIGHT).toString());
    final List<Measurement> recordsProcessedMeasurements = MetricsTestUtil.getMeasurementList(new StringJoiner(MetricNames.DELIMITER).add(PIPELINE_NAME).add(MetricNames.RECORDS_PROCESSED).toString());
    final Measurement recordsInFlightMeasurement = recordsInFlightMeasurements.get(0);
    final Measurement recordsProcessedMeasurement = recordsProcessedMeasurements.get(0);
    Assert.assertEquals(0.0, recordsInFlightMeasurement.getValue(), 0);
    Assert.assertEquals(5.0, recordsProcessedMeasurement.getValue(), 0);
    Assert.assertEquals(1.0, MetricsTestUtil.getMeasurementFromList(checkpointTimeMeasurements, Statistic.COUNT).getValue(), 0);
    Assert.assertTrue(MetricsTestUtil.isBetween(MetricsTestUtil.getMeasurementFromList(checkpointTimeMeasurements, Statistic.TOTAL_TIME).getValue(), 0.0, 0.001));
}
Also used : Measurement(io.micrometer.core.instrument.Measurement) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Record(com.amazon.dataprepper.model.record.Record) CheckpointState(com.amazon.dataprepper.model.CheckpointState) AbstractMap(java.util.AbstractMap) Map(java.util.Map) StringJoiner(java.util.StringJoiner) Test(org.junit.jupiter.api.Test)

Example 3 with CheckpointState

use of com.amazon.dataprepper.model.CheckpointState in project data-prepper by opensearch-project.

the class ProcessWorker method run.

@Override
public void run() {
    try {
        do {
            final Map.Entry<Collection, CheckpointState> readResult = readBuffer.read(pipeline.getReadBatchTimeoutInMillis());
            Collection records = readResult.getKey();
            final CheckpointState checkpointState = readResult.getValue();
            // TODO Hacky way to avoid logging continuously - Will be removed as part of metrics implementation
            if (records.isEmpty()) {
                if (!isEmptyRecordsLogged) {
                    LOG.info(" {} Worker: No records received from buffer", pipeline.getName());
                    isEmptyRecordsLogged = true;
                }
            } else {
                LOG.info(" {} Worker: Processing {} records from buffer", pipeline.getName(), records.size());
            }
            // Should Empty list from buffer should be sent to the processors? For now sending as the Stateful processors expects it.
            for (final Processor processor : processors) {
                records = processor.execute(records);
            }
            if (!records.isEmpty()) {
                postToSink(records);
            }
            // Checkpoint the current batch read from the buffer after being processed by processors and sinks.
            readBuffer.checkpoint(checkpointState);
        } while (!shouldStop());
    } catch (final Exception e) {
        LOG.error("Encountered exception during pipeline {} processing", pipeline.getName(), e);
    }
}
Also used : Processor(com.amazon.dataprepper.model.processor.Processor) Collection(java.util.Collection) CheckpointState(com.amazon.dataprepper.model.CheckpointState) Map(java.util.Map)

Example 4 with CheckpointState

use of com.amazon.dataprepper.model.CheckpointState in project data-prepper by opensearch-project.

the class TestBuffer method read.

@Override
public Map.Entry<Collection<Record<Event>>, CheckpointState> read(int timeoutInMillis) {
    final List<Record<Event>> records = new ArrayList<>();
    int index = 0;
    Record<Event> record;
    while (index < batchSize && (record = buffer.poll()) != null) {
        records.add(record);
        index++;
    }
    final CheckpointState checkpointState = new CheckpointState(records.size());
    return new AbstractMap.SimpleEntry<>(records, checkpointState);
}
Also used : ArrayList(java.util.ArrayList) Event(com.amazon.dataprepper.model.event.Event) Record(com.amazon.dataprepper.model.record.Record) CheckpointState(com.amazon.dataprepper.model.CheckpointState)

Example 5 with CheckpointState

use of com.amazon.dataprepper.model.CheckpointState in project data-prepper by opensearch-project.

the class StdInSourceTests method testStdInSourceSuccessfulWriteToBuffer.

@Test
public void testStdInSourceSuccessfulWriteToBuffer() {
    final Queue<Record<Event>> bufferQueue = new LinkedList<>();
    final TestBuffer buffer = new TestBuffer(bufferQueue, 1);
    final StdInSource stdInSource = new StdInSource(TEST_WRITE_TIMEOUT, TEST_PIPELINE_NAME);
    assertThat(buffer.size(), is(equalTo(0)));
    stdInSource.start(buffer);
    assertThat(buffer.size(), is(equalTo(1)));
    final Map.Entry<Collection<Record<Event>>, CheckpointState> readResult = buffer.read(TEST_WRITE_TIMEOUT);
    final Collection<Record<Event>> recordsFromBuffer = readResult.getKey();
    assertThat(recordsFromBuffer.size(), is(equalTo(1)));
    recordsFromBuffer.forEach(actualRecord -> assertThat(actualRecord.getData().get("message", String.class), is(equalTo(READ_CONTENT))));
}
Also used : TestBuffer(com.amazon.dataprepper.plugins.buffer.TestBuffer) Collection(java.util.Collection) Event(com.amazon.dataprepper.model.event.Event) Record(com.amazon.dataprepper.model.record.Record) CheckpointState(com.amazon.dataprepper.model.CheckpointState) Map(java.util.Map) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

CheckpointState (com.amazon.dataprepper.model.CheckpointState)7 Record (com.amazon.dataprepper.model.record.Record)5 Collection (java.util.Collection)5 Map (java.util.Map)5 ArrayList (java.util.ArrayList)4 PluginSetting (com.amazon.dataprepper.model.configuration.PluginSetting)2 Event (com.amazon.dataprepper.model.event.Event)2 Measurement (io.micrometer.core.instrument.Measurement)2 HashMap (java.util.HashMap)2 StringJoiner (java.util.StringJoiner)2 Test (org.junit.jupiter.api.Test)2 ArmeriaHttpAuthenticationProvider (com.amazon.dataprepper.armeria.authentication.ArmeriaHttpAuthenticationProvider)1 MetricNames (com.amazon.dataprepper.metrics.MetricNames)1 MetricsTestUtil (com.amazon.dataprepper.metrics.MetricsTestUtil)1 PluginMetrics (com.amazon.dataprepper.metrics.PluginMetrics)1 Log (com.amazon.dataprepper.model.log.Log)1 PluginFactory (com.amazon.dataprepper.model.plugin.PluginFactory)1 Processor (com.amazon.dataprepper.model.processor.Processor)1 TestBuffer (com.amazon.dataprepper.plugins.buffer.TestBuffer)1 BlockingBuffer (com.amazon.dataprepper.plugins.buffer.blockingbuffer.BlockingBuffer)1