Class KinesisDataFetcher<T>
- java.lang.Object
-
- org.apache.flink.streaming.connectors.kinesis.internals.KinesisDataFetcher<T>
-
- Direct Known Subclasses:
DynamoDBStreamsDataFetcher
@Internal public class KinesisDataFetcher<T> extends Object
A KinesisDataFetcher is responsible for fetching data from multiple Kinesis shards. Each parallel subtask instantiates and runs a single fetcher throughout the subtask's lifetime. The fetcher accomplishes the following:- 1. continuously poll Kinesis to discover shards that the subtask should subscribe to. The subscribed subset of shards, including future new shards, is non-overlapping across subtasks (no two subtasks will be subscribed to the same shard) and determinate across subtask restores (the subtask will always subscribe to the same subset of shards even after restoring)
- 2. decide where in each discovered shard should the fetcher start subscribing to
- 3. subscribe to shards by creating a single thread for each shard
The fetcher manages two states: 1) last seen shard ids of each subscribed stream (used for continuous shard discovery), and 2) last processed sequence numbers of each subscribed shard. Since operations on the second state will be performed by multiple threads, these operations should only be done using the handler methods provided in this class.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceKinesisDataFetcher.FlinkKinesisProxyFactoryFactory to create Kinesis proxy instances used by a fetcher.static interfaceKinesisDataFetcher.FlinkKinesisProxyV2FactoryFactory to create Kinesis proxy V@ instances used by a fetcher.
-
Constructor Summary
Constructors Modifier Constructor Description protectedKinesisDataFetcher(List<String> streams, org.apache.flink.streaming.api.functions.source.SourceFunction.SourceContext<T> sourceContext, Object checkpointLock, org.apache.flink.api.common.functions.RuntimeContext runtimeContext, Properties configProps, KinesisDeserializationSchema<T> deserializationSchema, KinesisShardAssigner shardAssigner, org.apache.flink.streaming.api.functions.AssignerWithPeriodicWatermarks<T> periodicWatermarkAssigner, WatermarkTracker watermarkTracker, AtomicReference<Throwable> error, List<KinesisStreamShardState> subscribedShardsState, HashMap<String,String> subscribedStreamsToLastDiscoveredShardIds, KinesisDataFetcher.FlinkKinesisProxyFactory kinesisProxyFactory, KinesisDataFetcher.FlinkKinesisProxyV2Factory kinesisProxyV2Factory)KinesisDataFetcher(List<String> streams, org.apache.flink.streaming.api.functions.source.SourceFunction.SourceContext<T> sourceContext, org.apache.flink.api.common.functions.RuntimeContext runtimeContext, Properties configProps, KinesisDeserializationSchema<T> deserializationSchema, KinesisShardAssigner shardAssigner, org.apache.flink.streaming.api.functions.AssignerWithPeriodicWatermarks<T> periodicWatermarkAssigner, WatermarkTracker watermarkTracker)Creates a Kinesis Data Fetcher.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadvanceLastDiscoveredShardOfStream(String stream, String shardId)Updates the last discovered shard of a subscribed stream; only updates if the update is valid.voidawaitTermination()After callingshutdownFetcher(), this can be called to await the fetcher shutdown.protected voidcloseRecordPublisherFactory()Closes recordRecordPublisherFactory.static StreamShardHandleconvertToStreamShardHandle(StreamShardMetadata streamShardMetadata)Utility function to convertStreamShardMetadataintoStreamShardHandle.static StreamShardMetadataconvertToStreamShardMetadata(StreamShardHandle streamShardHandle)Utility function to convertStreamShardHandleintoStreamShardMetadata.protected static HashMap<String,String>createInitialSubscribedStreamsToLastDiscoveredShardsState(List<String> streams)Utility function to create an initial map of the last discovered shard id of each subscribed stream, set to null; This is called in the constructor; correct values will be set later on by calling advanceLastDiscoveredShardOfStream().protected RecordPublishercreateRecordPublisher(SequenceNumber sequenceNumber, Properties configProps, org.apache.flink.metrics.MetricGroup metricGroup, StreamShardHandle subscribedShard)protected RecordPublisherFactorycreateRecordPublisherFactory()protected ShardConsumer<T>createShardConsumer(Integer subscribedShardStateIndex, StreamShardHandle subscribedShard, SequenceNumber lastSequenceNum, org.apache.flink.metrics.MetricGroup metricGroup, KinesisDeserializationSchema<T> shardDeserializer)Create a new shard consumer.protected ExecutorServicecreateShardConsumersThreadPool(String subtaskName)protected voidderegisterStreamConsumer()Deregisters stream consumers.List<StreamShardHandle>discoverNewShardsToSubscribe()A utility function that does the following:protected voidemitRecordAndUpdateState(T record, long recordTimestamp, int shardStateIndex, SequenceNumber lastSequenceNumber)Prepare a record and hand it over to theRecordEmitter, which may collect it asynchronously.protected voidemitWatermark()Called periodically to emit a watermark.protected PropertiesgetConsumerConfiguration()protected longgetCurrentTimeMillis()Return the current system time.List<KinesisStreamShardState>getSubscribedShardsState()static booleanisThisSubtaskShouldSubscribeTo(int shardHash, int totalNumberOfConsumerSubtasks, int indexOfThisConsumerSubtask)Utility function to determine whether a shard should be subscribed by this consumer subtask.intregisterNewSubscribedShardState(KinesisStreamShardState newSubscribedShardState)Register a new subscribed shard state.voidrunFetcher()Starts the fetcher.protected booleanshouldAdvanceLastDiscoveredShardId(String shardId, String lastSeenShardIdOfStream)Given lastSeenShardId, check if last discovered shardId should be advanced.voidshutdownFetcher()Starts shutting down the fetcher.HashMap<StreamShardMetadata,SequenceNumber>snapshotState()Creates a snapshot of the current last processed sequence numbers of each subscribed shard.protected voidstopWithError(Throwable throwable)Called by created threads to pass on errors.protected voidupdateState(int shardStateIndex, SequenceNumber lastSequenceNumber)Update the shard to last processed sequence number state.
-
-
-
Constructor Detail
-
KinesisDataFetcher
public KinesisDataFetcher(List<String> streams, org.apache.flink.streaming.api.functions.source.SourceFunction.SourceContext<T> sourceContext, org.apache.flink.api.common.functions.RuntimeContext runtimeContext, Properties configProps, KinesisDeserializationSchema<T> deserializationSchema, KinesisShardAssigner shardAssigner, org.apache.flink.streaming.api.functions.AssignerWithPeriodicWatermarks<T> periodicWatermarkAssigner, WatermarkTracker watermarkTracker)
Creates a Kinesis Data Fetcher.- Parameters:
streams- the streams to subscribe tosourceContext- context of the source functionruntimeContext- this subtask's runtime contextconfigProps- the consumer configuration propertiesdeserializationSchema- deserialization schema
-
KinesisDataFetcher
@VisibleForTesting protected KinesisDataFetcher(List<String> streams, org.apache.flink.streaming.api.functions.source.SourceFunction.SourceContext<T> sourceContext, Object checkpointLock, org.apache.flink.api.common.functions.RuntimeContext runtimeContext, Properties configProps, KinesisDeserializationSchema<T> deserializationSchema, KinesisShardAssigner shardAssigner, org.apache.flink.streaming.api.functions.AssignerWithPeriodicWatermarks<T> periodicWatermarkAssigner, WatermarkTracker watermarkTracker, AtomicReference<Throwable> error, List<KinesisStreamShardState> subscribedShardsState, HashMap<String,String> subscribedStreamsToLastDiscoveredShardIds, KinesisDataFetcher.FlinkKinesisProxyFactory kinesisProxyFactory, @Nullable KinesisDataFetcher.FlinkKinesisProxyV2Factory kinesisProxyV2Factory)
-
-
Method Detail
-
createShardConsumer
protected ShardConsumer<T> createShardConsumer(Integer subscribedShardStateIndex, StreamShardHandle subscribedShard, SequenceNumber lastSequenceNum, org.apache.flink.metrics.MetricGroup metricGroup, KinesisDeserializationSchema<T> shardDeserializer) throws InterruptedException
Create a new shard consumer. Override this method to customize shard consumer behavior in subclasses.- Parameters:
subscribedShardStateIndex- the state index of the shard this consumer is subscribed tosubscribedShard- the shard this consumer is subscribed tolastSequenceNum- the sequence number in the shard to start consumingmetricGroup- the metric group to report metrics to- Returns:
- shard consumer
- Throws:
InterruptedException
-
createRecordPublisherFactory
protected RecordPublisherFactory createRecordPublisherFactory()
-
createRecordPublisher
protected RecordPublisher createRecordPublisher(SequenceNumber sequenceNumber, Properties configProps, org.apache.flink.metrics.MetricGroup metricGroup, StreamShardHandle subscribedShard) throws InterruptedException
- Throws:
InterruptedException
-
runFetcher
public void runFetcher() throws ExceptionStarts the fetcher. After starting the fetcher, it can only be stopped by callingshutdownFetcher().- Throws:
Exception- the first error or exception thrown by the fetcher or any of the threads created by the fetcher.
-
snapshotState
public HashMap<StreamShardMetadata,SequenceNumber> snapshotState()
Creates a snapshot of the current last processed sequence numbers of each subscribed shard.- Returns:
- state snapshot
-
shutdownFetcher
public void shutdownFetcher()
Starts shutting down the fetcher. Must be called to allowrunFetcher()to complete. Once called, the shutdown procedure will be executed and all shard consuming threads will be interrupted.
-
closeRecordPublisherFactory
@VisibleForTesting protected void closeRecordPublisherFactory()
Closes recordRecordPublisherFactory. Allows test to override this to simulate exception for shutdown logic.
-
deregisterStreamConsumer
@VisibleForTesting protected void deregisterStreamConsumer()
Deregisters stream consumers. Allows test to override this to simulate exception for shutdown logic.
-
awaitTermination
public void awaitTermination() throws InterruptedExceptionAfter callingshutdownFetcher(), this can be called to await the fetcher shutdown.- Throws:
InterruptedException
-
stopWithError
protected void stopWithError(Throwable throwable)
Called by created threads to pass on errors. Only the first thrown error is set. Once set, the shutdown process will be executed and all shard consuming threads will be interrupted.
-
advanceLastDiscoveredShardOfStream
public void advanceLastDiscoveredShardOfStream(String stream, String shardId)
Updates the last discovered shard of a subscribed stream; only updates if the update is valid.
-
shouldAdvanceLastDiscoveredShardId
protected boolean shouldAdvanceLastDiscoveredShardId(String shardId, String lastSeenShardIdOfStream)
Given lastSeenShardId, check if last discovered shardId should be advanced.
-
discoverNewShardsToSubscribe
public List<StreamShardHandle> discoverNewShardsToSubscribe() throws InterruptedException
A utility function that does the following:1. Find new shards for each stream that we haven't seen before 2. For each new shard, determine whether this consumer subtask should subscribe to them; if yes, it is added to the returned list of shards 3. Update the subscribedStreamsToLastDiscoveredShardIds state so that we won't get shards that we have already seen before the next time this function is called
- Throws:
InterruptedException
-
getConsumerConfiguration
protected Properties getConsumerConfiguration()
-
emitRecordAndUpdateState
protected void emitRecordAndUpdateState(T record, long recordTimestamp, int shardStateIndex, SequenceNumber lastSequenceNumber)
Prepare a record and hand it over to theRecordEmitter, which may collect it asynchronously. This method is called byShardConsumers.- Parameters:
record- the record to collectrecordTimestamp- timestamp to attach to the collected recordshardStateIndex- index of the shard to update in subscribedShardsState; this index should be the returned value fromregisterNewSubscribedShardState(KinesisStreamShardState), called when the shard state was registered.lastSequenceNumber- the last sequence number value to update
-
updateState
protected final void updateState(int shardStateIndex, SequenceNumber lastSequenceNumber)Update the shard to last processed sequence number state. This method is called byShardConsumers.- Parameters:
shardStateIndex- index of the shard to update in subscribedShardsState; this index should be the returned value fromregisterNewSubscribedShardState(KinesisStreamShardState), called when the shard state was registered.lastSequenceNumber- the last sequence number value to update
-
registerNewSubscribedShardState
public int registerNewSubscribedShardState(KinesisStreamShardState newSubscribedShardState)
Register a new subscribed shard state.- Parameters:
newSubscribedShardState- the new shard state that this fetcher is to be subscribed to
-
getCurrentTimeMillis
@VisibleForTesting protected long getCurrentTimeMillis()
Return the current system time. Allow tests to override this to simulate progress for watermark logic.- Returns:
- current processing time
-
emitWatermark
@VisibleForTesting protected void emitWatermark()
Called periodically to emit a watermark. Checks all shards for the current event time watermark, and possibly emits the next watermark.Shards that have not received an update for a certain interval are considered inactive so as to not hold back the watermark indefinitely. When all shards are inactive, the subtask will be marked as temporarily idle to not block downstream operators.
-
isThisSubtaskShouldSubscribeTo
public static boolean isThisSubtaskShouldSubscribeTo(int shardHash, int totalNumberOfConsumerSubtasks, int indexOfThisConsumerSubtask)Utility function to determine whether a shard should be subscribed by this consumer subtask.- Parameters:
shardHash- hash code for the shardtotalNumberOfConsumerSubtasks- total number of consumer subtasksindexOfThisConsumerSubtask- index of this consumer subtask
-
createShardConsumersThreadPool
@VisibleForTesting protected ExecutorService createShardConsumersThreadPool(String subtaskName)
-
getSubscribedShardsState
@VisibleForTesting public List<KinesisStreamShardState> getSubscribedShardsState()
-
createInitialSubscribedStreamsToLastDiscoveredShardsState
protected static HashMap<String,String> createInitialSubscribedStreamsToLastDiscoveredShardsState(List<String> streams)
Utility function to create an initial map of the last discovered shard id of each subscribed stream, set to null; This is called in the constructor; correct values will be set later on by calling advanceLastDiscoveredShardOfStream().- Parameters:
streams- the list of subscribed streams- Returns:
- the initial map for subscribedStreamsToLastDiscoveredShardIds
-
convertToStreamShardMetadata
public static StreamShardMetadata convertToStreamShardMetadata(StreamShardHandle streamShardHandle)
Utility function to convertStreamShardHandleintoStreamShardMetadata.- Parameters:
streamShardHandle- theStreamShardHandleto be converted- Returns:
- a
StreamShardMetadataobject
-
convertToStreamShardHandle
public static StreamShardHandle convertToStreamShardHandle(StreamShardMetadata streamShardMetadata)
Utility function to convertStreamShardMetadataintoStreamShardHandle.- Parameters:
streamShardMetadata- theStreamShardMetadatato be converted- Returns:
- a
StreamShardHandleobject
-
-