Search in sources :

Example 1 with PulsarMessageCollector

use of org.apache.flink.connector.pulsar.source.reader.message.PulsarMessageCollector 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

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 StopCursor (org.apache.flink.connector.pulsar.source.enumerator.cursor.StopCursor)1 PulsarMessage (org.apache.flink.connector.pulsar.source.reader.message.PulsarMessage)1 PulsarMessageCollector (org.apache.flink.connector.pulsar.source.reader.message.PulsarMessageCollector)1 ConsumerBuilder (org.apache.pulsar.client.api.ConsumerBuilder)1 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)1