Search in sources :

Example 6 with CheckpointState

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

the class BlockingBuffer method doRead.

/**
 * Retrieves and removes the batch of records from the head of the queue. The batch size is defined/determined by
 * the configuration attribute {@link #ATTRIBUTE_BATCH_SIZE} or the @param timeoutInMillis. The timeoutInMillis
 * is also used for retrieving each record
 *
 * @param timeoutInMillis how long to wait before giving up
 * @return The earliest batch of records in the buffer which are still not read.
 */
@Override
public Map.Entry<Collection<T>, CheckpointState> doRead(int timeoutInMillis) {
    final List<T> records = new ArrayList<>();
    final Stopwatch stopwatch = Stopwatch.createStarted();
    try {
        while (stopwatch.elapsed(TimeUnit.MILLISECONDS) < timeoutInMillis && records.size() < batchSize) {
            final T record = blockingQueue.poll(timeoutInMillis, TimeUnit.MILLISECONDS);
            if (record != null) {
                // record can be null, avoiding adding nulls
                records.add(record);
            }
            if (records.size() < batchSize) {
                blockingQueue.drainTo(records, batchSize - records.size());
            }
        }
    } catch (InterruptedException ex) {
        LOG.info("Pipeline [{}] - Interrupt received while reading from buffer", pipelineName);
        throw new RuntimeException(ex);
    }
    final CheckpointState checkpointState = new CheckpointState(records.size());
    return new AbstractMap.SimpleEntry<>(records, checkpointState);
}
Also used : ArrayList(java.util.ArrayList) Stopwatch(com.google.common.base.Stopwatch) CheckpointState(com.amazon.dataprepper.model.CheckpointState)

Example 7 with CheckpointState

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

the class BlockingBufferTests method testBatchRead.

@Test
public void testBatchRead() throws Exception {
    final PluginSetting completePluginSetting = completePluginSettingForBlockingBuffer();
    final BlockingBuffer<Record<String>> blockingBuffer = new BlockingBuffer<>(completePluginSetting);
    assertThat(blockingBuffer, notNullValue());
    final int testSize = 5;
    for (int i = 0; i < testSize; i++) {
        Record<String> record = new Record<>("TEST" + i);
        blockingBuffer.write(record, TEST_WRITE_TIMEOUT);
    }
    final Map.Entry<Collection<Record<String>>, CheckpointState> partialReadResult = blockingBuffer.read(TEST_BATCH_READ_TIMEOUT);
    final Collection<Record<String>> partialRecords = partialReadResult.getKey();
    final CheckpointState partialCheckpointState = partialReadResult.getValue();
    final int expectedBatchSize = (Integer) completePluginSetting.getAttributeFromSettings(ATTRIBUTE_BATCH_SIZE);
    assertThat(partialRecords.size(), is(expectedBatchSize));
    assertEquals(expectedBatchSize, partialCheckpointState.getNumRecordsToBeChecked());
    int i = 0;
    for (Record<String> record : partialRecords) {
        assertThat(record.getData(), equalTo("TEST" + i));
        i++;
    }
    final Map.Entry<Collection<Record<String>>, CheckpointState> finalReadResult = blockingBuffer.read(TEST_BATCH_READ_TIMEOUT);
    final Collection<Record<String>> finalBatch = finalReadResult.getKey();
    final CheckpointState finalCheckpointState = finalReadResult.getValue();
    assertThat(finalBatch.size(), is(testSize - expectedBatchSize));
    assertEquals(testSize - expectedBatchSize, finalCheckpointState.getNumRecordsToBeChecked());
    for (Record<String> record : finalBatch) {
        assertThat(record.getData(), equalTo("TEST" + i));
        i++;
    }
}
Also used : Collection(java.util.Collection) Record(com.amazon.dataprepper.model.record.Record) CheckpointState(com.amazon.dataprepper.model.CheckpointState) PluginSetting(com.amazon.dataprepper.model.configuration.PluginSetting) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.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