Search in sources :

Example 1 with StopCursor

use of org.apache.flink.connector.pulsar.source.enumerator.cursor.StopCursor in project flink by apache.

the class PulsarPartitionSplitSerializer method deserializePulsarPartitionSplit.

public PulsarPartitionSplit deserializePulsarPartitionSplit(int version, DataInputStream in) throws IOException {
    // partition
    TopicPartition partition = deserializeTopicPartition(version, in);
    // stopCursor
    StopCursor stopCursor = deserializeObject(in);
    // latestConsumedId
    MessageId latestConsumedId = null;
    if (in.readBoolean()) {
        byte[] messageIdBytes = deserializeBytes(in);
        latestConsumedId = MessageId.fromByteArray(messageIdBytes);
    }
    // uncommittedTransactionId
    TxnID uncommittedTransactionId = null;
    if (in.readBoolean()) {
        long mostSigBits = in.readLong();
        long leastSigBits = in.readLong();
        uncommittedTransactionId = new TxnID(mostSigBits, leastSigBits);
    }
    // Creation
    return new PulsarPartitionSplit(partition, stopCursor, latestConsumedId, uncommittedTransactionId);
}
Also used : StopCursor(org.apache.flink.connector.pulsar.source.enumerator.cursor.StopCursor) TxnID(org.apache.pulsar.client.api.transaction.TxnID) TopicPartition(org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition) MessageId(org.apache.pulsar.client.api.MessageId)

Example 2 with StopCursor

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

use of org.apache.flink.connector.pulsar.source.enumerator.cursor.StopCursor in project flink by apache.

the class PulsarPartitionSplitReaderBase method fetch.

@Override
public RecordsWithSplitIds<PulsarMessage<OUT>> fetch() throws IOException {
    RecordsBySplits.Builder<PulsarMessage<OUT>> builder = new RecordsBySplits.Builder<>();
    // Return when no split registered to this reader.
    if (pulsarConsumer == null || registeredSplit == null) {
        return builder.build();
    }
    // Set wakeup to false for start consuming.
    wakeup.compareAndSet(true, false);
    StopCursor stopCursor = registeredSplit.getStopCursor();
    String splitId = registeredSplit.splitId();
    PulsarMessageCollector<OUT> collector = new PulsarMessageCollector<>(splitId, builder);
    Deadline deadline = Deadline.fromNow(sourceConfiguration.getMaxFetchTime());
    // Consume message from pulsar until it was woke up by flink reader.
    for (int messageNum = 0; messageNum < sourceConfiguration.getMaxFetchRecords() && deadline.hasTimeLeft() && isNotWakeup(); messageNum++) {
        try {
            Duration timeout = deadline.timeLeftIfAny();
            Message<byte[]> message = pollMessage(timeout);
            if (message == null) {
                break;
            }
            // Deserialize message.
            collector.setMessage(message);
            deserializationSchema.deserialize(message, collector);
            // Acknowledge message if need.
            finishedPollMessage(message);
            if (stopCursor.shouldStop(message)) {
                builder.addFinishedSplit(splitId);
                break;
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            break;
        } catch (TimeoutException e) {
            break;
        } catch (ExecutionException e) {
            LOG.error("Error in polling message from pulsar consumer.", e);
            break;
        } catch (Exception e) {
            throw new IOException(e);
        }
    }
    return builder.build();
}
Also used : ConsumerBuilder(org.apache.pulsar.client.api.ConsumerBuilder) PulsarSourceConfigUtils.createConsumerBuilder(org.apache.flink.connector.pulsar.source.config.PulsarSourceConfigUtils.createConsumerBuilder) Deadline(org.apache.flink.api.common.time.Deadline) Duration(java.time.Duration) IOException(java.io.IOException) RecordsBySplits(org.apache.flink.connector.base.source.reader.RecordsBySplits) TimeoutException(java.util.concurrent.TimeoutException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) StopCursor(org.apache.flink.connector.pulsar.source.enumerator.cursor.StopCursor) PulsarMessage(org.apache.flink.connector.pulsar.source.reader.message.PulsarMessage) PulsarMessageCollector(org.apache.flink.connector.pulsar.source.reader.message.PulsarMessageCollector) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

StopCursor (org.apache.flink.connector.pulsar.source.enumerator.cursor.StopCursor)3 TopicPartition (org.apache.flink.connector.pulsar.source.enumerator.topic.TopicPartition)2 IOException (java.io.IOException)1 Duration (java.time.Duration)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Deadline (org.apache.flink.api.common.time.Deadline)1 RecordsBySplits (org.apache.flink.connector.base.source.reader.RecordsBySplits)1 PulsarSourceConfigUtils.createConsumerBuilder (org.apache.flink.connector.pulsar.source.config.PulsarSourceConfigUtils.createConsumerBuilder)1 PulsarMessage (org.apache.flink.connector.pulsar.source.reader.message.PulsarMessage)1 PulsarMessageCollector (org.apache.flink.connector.pulsar.source.reader.message.PulsarMessageCollector)1 PulsarPartitionSplit (org.apache.flink.connector.pulsar.source.split.PulsarPartitionSplit)1 ConsumerBuilder (org.apache.pulsar.client.api.ConsumerBuilder)1 MessageId (org.apache.pulsar.client.api.MessageId)1 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)1 TxnID (org.apache.pulsar.client.api.transaction.TxnID)1