Search in sources :

Example 1 with StartCursor

use of org.apache.flink.connector.pulsar.source.enumerator.cursor.StartCursor 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 StartCursor

use of org.apache.flink.connector.pulsar.source.enumerator.cursor.StartCursor 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)

Aggregations

StartCursor (org.apache.flink.connector.pulsar.source.enumerator.cursor.StartCursor)2 TopicPartition (org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition)2 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)2 SplitsAddition (org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition)1 PulsarPartitionSplit (org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit)1 MessageId (org.apache.pulsar.client.api.MessageId)1