Class FlinkSourceReaderBase<T,​OutputT>

  • Type Parameters:
    OutputT - the output element type from the encapsulated Beam sources.
    All Implemented Interfaces:
    java.lang.AutoCloseable, org.apache.flink.api.common.state.CheckpointListener, org.apache.flink.api.connector.source.SourceReader<OutputT,​FlinkSourceSplit<T>>
    Direct Known Subclasses:
    FlinkBoundedSourceReader, FlinkUnboundedSourceReader

    public abstract class FlinkSourceReaderBase<T,​OutputT>
    extends java.lang.Object
    implements org.apache.flink.api.connector.source.SourceReader<OutputT,​FlinkSourceSplit<T>>
    An abstract implementation of SourceReader which encapsulates Beam Sources for data reading.
    1. Idle timeout support.
    2. Splits addition handling.
    3. Split reader creation and management.
    4. checkpoint management

    This implementation provides unified logic for both BoundedSource and UnboundedSource. The subclasses are expected to only implement the SourceReader.pollNext(ReaderOutput) method.

    • Field Detail

      • AVAILABLE_NOW

        protected static final java.util.concurrent.CompletableFuture<java.lang.Void> AVAILABLE_NOW
      • pipelineOptions

        protected final org.apache.beam.sdk.options.PipelineOptions pipelineOptions
      • timestampExtractor

        protected final @Nullable java.util.function.Function<OutputT,​java.lang.Long> timestampExtractor
      • context

        protected final org.apache.flink.api.connector.source.SourceReaderContext context
      • invocationUtil

        protected final ReaderInvocationUtil<T,​org.apache.beam.sdk.io.Source.Reader<T>> invocationUtil
      • numRecordsInCounter

        protected final org.apache.flink.metrics.Counter numRecordsInCounter
      • idleTimeoutMs

        protected final long idleTimeoutMs
    • Constructor Detail

      • FlinkSourceReaderBase

        protected FlinkSourceReaderBase​(java.lang.String stepName,
                                        org.apache.flink.api.connector.source.SourceReaderContext context,
                                        org.apache.beam.sdk.options.PipelineOptions pipelineOptions,
                                        @Nullable java.util.function.Function<OutputT,​java.lang.Long> timestampExtractor)
      • FlinkSourceReaderBase

        protected FlinkSourceReaderBase​(java.lang.String stepName,
                                        java.util.concurrent.ScheduledExecutorService executor,
                                        org.apache.flink.api.connector.source.SourceReaderContext context,
                                        org.apache.beam.sdk.options.PipelineOptions pipelineOptions,
                                        @Nullable java.util.function.Function<OutputT,​java.lang.Long> timestampExtractor)
    • Method Detail

      • start

        public void start()
        Specified by:
        start in interface org.apache.flink.api.connector.source.SourceReader<T,​OutputT>
      • snapshotState

        public java.util.List<FlinkSourceSplit<T>> snapshotState​(long checkpointId)
        Specified by:
        snapshotState in interface org.apache.flink.api.connector.source.SourceReader<T,​OutputT>
      • isAvailable

        public java.util.concurrent.CompletableFuture<java.lang.Void> isAvailable()
        Specified by:
        isAvailable in interface org.apache.flink.api.connector.source.SourceReader<T,​OutputT>
      • notifyNoMoreSplits

        public void notifyNoMoreSplits()
        Specified by:
        notifyNoMoreSplits in interface org.apache.flink.api.connector.source.SourceReader<T,​OutputT>
      • addSplits

        public void addSplits​(java.util.List<FlinkSourceSplit<T>> splits)
        Specified by:
        addSplits in interface org.apache.flink.api.connector.source.SourceReader<T,​OutputT>
      • close

        public void close()
                   throws java.lang.Exception
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        java.lang.Exception
      • isAvailableForAliveReaders

        protected abstract java.util.concurrent.CompletableFuture<java.lang.Void> isAvailableForAliveReaders()
        This method needs to be overridden by subclasses to determine if data is available when there are alive readers. For example, an unbounded source may not have any source split ready for data emission even if all the sources are still alive. Whereas for the bounded source, data is always available as long as there are alive readers.
      • createReader

        protected abstract org.apache.beam.sdk.io.Source.Reader<T> createReader​(@Nonnull
                                                                                FlinkSourceSplit<T> sourceSplit)
                                                                         throws java.io.IOException
        Create Source.Reader for given FlinkSourceSplit.
        Throws:
        java.io.IOException
      • addSplitsToUnfinishedForCheckpoint

        protected void addSplitsToUnfinishedForCheckpoint​(long checkpointId,
                                                          java.util.List<FlinkSourceSplit<T>> splits)
        To be overridden in unbounded reader. Notify the reader of created splits that will be part of checkpoint. Will be processed during notifyCheckpointComplete to finalize the associated CheckpointMarks.
      • finishSplit

        protected final void finishSplit​(int splitIndex)
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • checkIdleTimeoutAndMaybeStartCountdown

        protected final boolean checkIdleTimeoutAndMaybeStartCountdown()
      • noMoreSplits

        protected final boolean noMoreSplits()
      • scheduleTask

        protected void scheduleTask​(java.lang.Runnable runnable,
                                    long delayMs)
      • scheduleTaskAtFixedRate

        protected void scheduleTaskAtFixedRate​(java.lang.Runnable runnable,
                                               long delayMs,
                                               long periodMs)
      • execute

        protected void execute​(java.lang.Runnable runnable)
      • recordException

        protected void recordException​(java.lang.Throwable e)
      • checkExceptionAndMaybeThrow

        protected void checkExceptionAndMaybeThrow()
      • hasException

        protected boolean hasException()
      • ignoreReturnValue

        protected static void ignoreReturnValue​(java.lang.Object o)