Search in sources :

Example 1 with SplitsAddition

use of org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition in project flink by apache.

the class PulsarPartitionSplitReaderTestBase method seekStartPositionAndHandleSplit.

private void seekStartPositionAndHandleSplit(PulsarPartitionSplitReaderBase<String> reader, String topicName, int partitionId, MessageId startPosition) {
    TopicPartition partition = new TopicPartition(topicName, partitionId, createFullRange());
    PulsarPartitionSplit split = new PulsarPartitionSplit(partition, StopCursor.never(), null, null);
    SplitsAddition<PulsarPartitionSplit> addition = new SplitsAddition<>(singletonList(split));
    // create consumer and seek before split changes
    try (Consumer<byte[]> consumer = reader.createPulsarConsumer(partition)) {
        // inclusive messageId
        StartCursor startCursor = StartCursor.fromMessageId(startPosition);
        startCursor.seekPosition(partition.getTopic(), partition.getPartitionId(), consumer);
    } catch (PulsarClientException e) {
        sneakyThrow(e);
    }
    reader.handleSplitsChanges(addition);
}
Also used : TopicPartition(org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition) StartCursor(org.apache.flink.connector.pulsar.source.enumerator.cursor.StartCursor) SplitsAddition(org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarPartitionSplit(org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit)

Example 2 with SplitsAddition

use of org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition in project flink by apache.

the class PulsarPartitionSplitReaderTestBase method handleSplit.

protected void handleSplit(PulsarPartitionSplitReaderBase<String> reader, String topicName, int partitionId, MessageId startPosition) {
    TopicPartition partition = new TopicPartition(topicName, partitionId, createFullRange());
    PulsarPartitionSplit split = new PulsarPartitionSplit(partition, StopCursor.never(), startPosition, null);
    SplitsAddition<PulsarPartitionSplit> addition = new SplitsAddition<>(singletonList(split));
    reader.handleSplitsChanges(addition);
}
Also used : TopicPartition(org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition) SplitsAddition(org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition) PulsarPartitionSplit(org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit)

Example 3 with SplitsAddition

use of org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition in project flink by apache.

the class KafkaPartitionSplitReaderTest method testUsingCommittedOffsetsWithNoneOffsetResetStrategy.

@Test
public void testUsingCommittedOffsetsWithNoneOffsetResetStrategy() {
    final Properties props = new Properties();
    props.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "using-committed-offset-with-none-offset-reset");
    KafkaPartitionSplitReader reader = createReader(props, UnregisteredMetricsGroup.createSourceReaderMetricGroup());
    // We expect that there is a committed offset, but the group does not actually have a
    // committed offset, and the offset reset strategy is none (Throw exception to the consumer
    // if no previous offset is found for the consumer's group);
    // So it is expected to throw an exception that missing the committed offset.
    final KafkaException undefinedOffsetException = Assertions.assertThrows(KafkaException.class, () -> reader.handleSplitsChanges(new SplitsAddition<>(Collections.singletonList(new KafkaPartitionSplit(new TopicPartition(TOPIC1, 0), KafkaPartitionSplit.COMMITTED_OFFSET)))));
    MatcherAssert.assertThat(undefinedOffsetException.getMessage(), CoreMatchers.containsString("Undefined offset with no reset policy for partition"));
}
Also used : KafkaPartitionSplit(org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit) TopicPartition(org.apache.kafka.common.TopicPartition) SplitsAddition(org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition) KafkaException(org.apache.kafka.common.KafkaException) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with SplitsAddition

use of org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition in project flink by apache.

the class PulsarPartitionSplitReaderBase method handleSplitsChanges.

@Override
public void handleSplitsChanges(SplitsChange<PulsarPartitionSplit> splitsChanges) {
    LOG.debug("Handle split changes {}", splitsChanges);
    // Get all the partition assignments and stopping offsets.
    if (!(splitsChanges instanceof SplitsAddition)) {
        throw new UnsupportedOperationException(String.format("The SplitChange type of %s is not supported.", splitsChanges.getClass()));
    }
    if (registeredSplit != null) {
        throw new IllegalStateException("This split reader have assigned split.");
    }
    List<PulsarPartitionSplit> newSplits = splitsChanges.splits();
    Preconditions.checkArgument(newSplits.size() == 1, "This pulsar split reader only support one split.");
    PulsarPartitionSplit newSplit = newSplits.get(0);
    // Create pulsar consumer.
    Consumer<byte[]> consumer = createPulsarConsumer(newSplit);
    // Open start & stop cursor.
    newSplit.open(pulsarAdmin);
    // Start Consumer.
    startConsumer(newSplit, consumer);
    LOG.info("Register split {} consumer for current reader.", newSplit);
    this.registeredSplit = newSplit;
    this.pulsarConsumer = consumer;
}
Also used : SplitsAddition(org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition) PulsarPartitionSplit(org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit)

Aggregations

SplitsAddition (org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition)4 PulsarPartitionSplit (org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit)3 TopicPartition (org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition)2 Properties (java.util.Properties)1 KafkaPartitionSplit (org.apache.flink.connector.kafka.source.split.KafkaPartitionSplit)1 StartCursor (org.apache.flink.connector.pulsar.source.enumerator.cursor.StartCursor)1 KafkaException (org.apache.kafka.common.KafkaException)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1