Search in sources :

Example 1 with OffsetRange

use of org.apache.beam.sdk.io.range.OffsetRange in project beam by apache.

the class OffsetBasedSource method split.

@Override
public List<? extends OffsetBasedSource<T>> split(long desiredBundleSizeBytes, PipelineOptions options) throws Exception {
    // Split the range into bundles based on the desiredBundleSizeBytes. If the desired bundle
    // size is smaller than the minBundleSize of the source then minBundleSize will be used instead.
    long desiredBundleSizeOffsetUnits = Math.max(Math.max(1, desiredBundleSizeBytes / getBytesPerOffset()), minBundleSize);
    List<OffsetBasedSource<T>> subSources = new ArrayList<>();
    for (OffsetRange range : new OffsetRange(startOffset, Math.min(endOffset, getMaxEndOffset(options))).split(desiredBundleSizeOffsetUnits, minBundleSize)) {
        subSources.add(createSourceForSubrange(range.getFrom(), range.getTo()));
    }
    return subSources;
}
Also used : OffsetRange(org.apache.beam.sdk.io.range.OffsetRange) ArrayList(java.util.ArrayList)

Example 2 with OffsetRange

use of org.apache.beam.sdk.io.range.OffsetRange in project beam by apache.

the class ReadFromKafkaDoFnTest method testInitialRestrictionWhenHasStopOffset.

@Test
public void testInitialRestrictionWhenHasStopOffset() throws Exception {
    long expectedStartOffset = 10L;
    long expectedStopOffset = 20L;
    consumer.setStartOffsetForTime(15L, Instant.now());
    consumer.setStopOffsetForTime(18L, Instant.now());
    consumer.setCurrentPos(5L);
    OffsetRange result = dofnInstance.initialRestriction(KafkaSourceDescriptor.of(topicPartition, expectedStartOffset, null, expectedStopOffset, null, ImmutableList.of()));
    assertEquals(new OffsetRange(expectedStartOffset, expectedStopOffset), result);
}
Also used : OffsetRange(org.apache.beam.sdk.io.range.OffsetRange) Test(org.junit.Test)

Example 3 with OffsetRange

use of org.apache.beam.sdk.io.range.OffsetRange in project beam by apache.

the class ReadFromKafkaDoFnTest method testProcessElementWithEmptyPoll.

@Test
public void testProcessElementWithEmptyPoll() throws Exception {
    MockOutputReceiver receiver = new MockOutputReceiver();
    consumer.setNumOfRecordsPerPoll(-1);
    OffsetRangeTracker tracker = new OffsetRangeTracker(new OffsetRange(0L, Long.MAX_VALUE));
    ProcessContinuation result = dofnInstance.processElement(KafkaSourceDescriptor.of(topicPartition, null, null, null, null, null), tracker, null, (OutputReceiver) receiver);
    assertEquals(ProcessContinuation.resume(), result);
    assertTrue(receiver.getOutputs().isEmpty());
}
Also used : OffsetRange(org.apache.beam.sdk.io.range.OffsetRange) OffsetRangeTracker(org.apache.beam.sdk.transforms.splittabledofn.OffsetRangeTracker) ProcessContinuation(org.apache.beam.sdk.transforms.DoFn.ProcessContinuation) Test(org.junit.Test)

Example 4 with OffsetRange

use of org.apache.beam.sdk.io.range.OffsetRange in project beam by apache.

the class ReadFromKafkaDoFnTest method testInitialRestrictionWhenHasStartOffset.

@Test
public void testInitialRestrictionWhenHasStartOffset() throws Exception {
    long expectedStartOffset = 10L;
    consumer.setStartOffsetForTime(15L, Instant.now());
    consumer.setCurrentPos(5L);
    OffsetRange result = dofnInstance.initialRestriction(KafkaSourceDescriptor.of(topicPartition, expectedStartOffset, null, null, null, ImmutableList.of()));
    assertEquals(new OffsetRange(expectedStartOffset, Long.MAX_VALUE), result);
}
Also used : OffsetRange(org.apache.beam.sdk.io.range.OffsetRange) Test(org.junit.Test)

Example 5 with OffsetRange

use of org.apache.beam.sdk.io.range.OffsetRange in project beam by apache.

the class ReadFromKafkaDoFnTest method testProcessElementWhenTopicPartitionIsStopped.

@Test
public void testProcessElementWhenTopicPartitionIsStopped() throws Exception {
    MockOutputReceiver receiver = new MockOutputReceiver();
    ReadFromKafkaDoFn<String, String> instance = new ReadFromKafkaDoFn(makeReadSourceDescriptor(consumer).toBuilder().setCheckStopReadingFn(new SerializableFunction<TopicPartition, Boolean>() {

        @Override
        public Boolean apply(TopicPartition input) {
            assertTrue(input.equals(topicPartition));
            return true;
        }
    }).build());
    consumer.setNumOfRecordsPerPoll(10);
    OffsetRangeTracker tracker = new OffsetRangeTracker(new OffsetRange(0L, Long.MAX_VALUE));
    ProcessContinuation result = instance.processElement(KafkaSourceDescriptor.of(topicPartition, null, null, null, null, null), tracker, null, (OutputReceiver) receiver);
    assertEquals(ProcessContinuation.stop(), result);
}
Also used : OffsetRange(org.apache.beam.sdk.io.range.OffsetRange) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetRangeTracker(org.apache.beam.sdk.transforms.splittabledofn.OffsetRangeTracker) ProcessContinuation(org.apache.beam.sdk.transforms.DoFn.ProcessContinuation) Test(org.junit.Test)

Aggregations

OffsetRange (org.apache.beam.sdk.io.range.OffsetRange)63 Test (org.junit.Test)53 Instant (org.joda.time.Instant)8 ArrayList (java.util.ArrayList)5 OffsetRangeTracker (org.apache.beam.sdk.transforms.splittabledofn.OffsetRangeTracker)5 Progress (org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker.Progress)5 ProcessContinuation (org.apache.beam.sdk.transforms.DoFn.ProcessContinuation)4 PartitionMetadata (org.apache.beam.sdk.io.gcp.spanner.changestreams.model.PartitionMetadata)3 DoFn (org.apache.beam.sdk.transforms.DoFn)3 BigDecimal (java.math.BigDecimal)2 RestrictionTracker (org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker)2 Offset (com.google.cloud.pubsublite.Offset)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 Scope (io.opencensus.common.Scope)1 AttributeValue (io.opencensus.trace.AttributeValue)1 Tracer (io.opencensus.trace.Tracer)1 Tracing (io.opencensus.trace.Tracing)1 Serializable (java.io.Serializable)1 Map (java.util.Map)1 Optional (java.util.Optional)1