Search in sources :

Example 11 with TopicPartition

use of org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition in project flink by apache.

the class PulsarPartitionSplitSerializerTest method serializeAndDeserializePulsarPartitionSplit.

@Test
void serializeAndDeserializePulsarPartitionSplit() throws Exception {
    PulsarPartitionSplit split = new PulsarPartitionSplit(new TopicPartition(randomAlphabetic(10), 10, createFullRange()), StopCursor.defaultStopCursor());
    byte[] bytes = INSTANCE.serialize(split);
    PulsarPartitionSplit split1 = INSTANCE.deserialize(INSTANCE.getVersion(), bytes);
    assertEquals(split, split1);
    assertNotSame(split, split1);
}
Also used : TopicPartition(org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition) Test(org.junit.jupiter.api.Test)

Example 12 with TopicPartition

use of org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition in project flink by apache.

the class PulsarTestCommonUtils method createPartitionSplit.

public static PulsarPartitionSplit createPartitionSplit(String topic, int partitionId, Boundedness boundedness, MessageId latestConsumedId) {
    TopicPartition topicPartition = new TopicPartition(topic, partitionId, TopicRange.createFullRange());
    StopCursor stopCursor = boundedness == Boundedness.BOUNDED ? StopCursor.latest() : StopCursor.never();
    return new PulsarPartitionSplit(topicPartition, stopCursor, latestConsumedId, null);
}
Also used : StopCursor(org.apache.flink.connector.pulsar.source.enumerator.cursor.StopCursor) TopicPartition(org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition) PulsarPartitionSplit(org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit)

Example 13 with TopicPartition

use of org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition in project flink by apache.

the class PulsarSubscriberTest method topicPatternSubscriber.

@Test
void topicPatternSubscriber() {
    operator().createTopic(TOPIC1, NUM_PARTITIONS_PER_TOPIC);
    operator().createTopic(TOPIC2, NUM_PARTITIONS_PER_TOPIC);
    operator().createTopic(TOPIC3, NUM_PARTITIONS_PER_TOPIC);
    PulsarSubscriber subscriber = getTopicPatternSubscriber(Pattern.compile("persistent://public/default/topic*?"), AllTopics);
    Set<TopicPartition> topicPartitions = subscriber.getSubscribedTopicPartitions(operator().admin(), new FullRangeGenerator(), NUM_PARALLELISM);
    Set<TopicPartition> expectedPartitions = new HashSet<>();
    for (int i = 0; i < NUM_PARTITIONS_PER_TOPIC; i++) {
        expectedPartitions.add(new TopicPartition(TOPIC1, i, createFullRange()));
        expectedPartitions.add(new TopicPartition(TOPIC3, i, createFullRange()));
    }
    assertEquals(expectedPartitions, topicPartitions);
    operator().deleteTopic(TOPIC1);
    operator().deleteTopic(TOPIC2);
    operator().deleteTopic(TOPIC3);
}
Also used : FullRangeGenerator(org.apache.flink.connector.pulsar.source.enumerator.topic.range.FullRangeGenerator) TopicPartition(org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 14 with TopicPartition

use of org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition in project flink by apache.

the class PulsarOrderedPartitionSplitReader method startConsumer.

@Override
protected void startConsumer(PulsarPartitionSplit split, Consumer<byte[]> consumer) {
    MessageId latestConsumedId = split.getLatestConsumedId();
    // Reset the start position for ordered pulsar consumer.
    if (latestConsumedId != null) {
        StartCursor startCursor = StartCursor.fromMessageId(latestConsumedId, false);
        TopicPartition partition = split.getPartition();
        try {
            startCursor.seekPosition(partition.getTopic(), partition.getPartitionId(), consumer);
        } catch (PulsarClientException e) {
            if (sourceConfiguration.getVerifyInitialOffsets() == FAIL_ON_MISMATCH) {
                throw new IllegalArgumentException(e);
            } else {
                // WARN_ON_MISMATCH would just print this warning message.
                // No need to print the stacktrace.
                LOG.warn("Failed to reset cursor to {} on partition {}", latestConsumedId, partition, e);
            }
        }
    }
}
Also used : TopicPartition(org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition) StartCursor(org.apache.flink.connector.pulsar.source.enumerator.cursor.StartCursor) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) MessageId(org.apache.pulsar.client.api.MessageId)

Example 15 with TopicPartition

use of org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition in project flink by apache.

the class PulsarSourceEnumerator method seekStartPosition.

private void seekStartPosition(Set<TopicPartition> partitions) {
    ConsumerBuilder<byte[]> consumerBuilder = consumerBuilder();
    Set<String> seekedTopics = new HashSet<>();
    for (TopicPartition partition : partitions) {
        String topicName = partition.getFullTopicName();
        if (!assignmentState.containsTopic(topicName) && seekedTopics.add(topicName)) {
            try (Consumer<byte[]> consumer = sneakyClient(() -> consumerBuilder.clone().topic(topicName).subscribe())) {
                startCursor.seekPosition(partition.getTopic(), partition.getPartitionId(), consumer);
            } catch (PulsarClientException e) {
                if (sourceConfiguration.getVerifyInitialOffsets() == FAIL_ON_MISMATCH) {
                    throw new IllegalArgumentException(e);
                } else {
                    // WARN_ON_MISMATCH would just print this warning message.
                    // No need to print the stacktrace.
                    LOG.warn("Failed to set initial consuming position for partition {}", partition, e);
                }
            }
        }
    }
}
Also used : TopicPartition(org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) HashSet(java.util.HashSet)

Aggregations

TopicPartition (org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition)15 PulsarPartitionSplit (org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit)8 MessageId (org.apache.pulsar.client.api.MessageId)4 Test (org.junit.jupiter.api.Test)4 HashSet (java.util.HashSet)3 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)3 HashMap (java.util.HashMap)2 Set (java.util.Set)2 SplitsAddition (org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition)2 StartCursor (org.apache.flink.connector.pulsar.source.enumerator.cursor.StartCursor)2 StopCursor (org.apache.flink.connector.pulsar.source.enumerator.cursor.StopCursor)2 TopicRange (org.apache.flink.connector.pulsar.source.enumerator.topic.TopicRange)2 FullRangeGenerator (org.apache.flink.connector.pulsar.source.enumerator.topic.range.FullRangeGenerator)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInput (java.io.DataInput)1 DataInputStream (java.io.DataInputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 PulsarSerdeUtils.deserializeSet (org.apache.flink.connector.pulsar.common.utils.PulsarSerdeUtils.deserializeSet)1