Class StreamTask
- java.lang.Object
-
- org.apache.kafka.streams.processor.internals.AbstractTask
-
- org.apache.kafka.streams.processor.internals.StreamTask
-
- All Implemented Interfaces:
ProcessorNodePunctuator,Task
public class StreamTask extends AbstractTask implements ProcessorNodePunctuator, Task
A StreamTask is associated with aPartitionGroup, and is assigned to a StreamThread for processing.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.kafka.streams.processor.internals.Task
Task.State, Task.TaskType
-
-
Field Summary
-
Fields inherited from class org.apache.kafka.streams.processor.internals.AbstractTask
id, inputPartitions, stateDirectory, stateMgr, topology
-
Fields inherited from interface org.apache.kafka.streams.processor.internals.Task
LATEST_OFFSET
-
-
Constructor Summary
Constructors Constructor Description StreamTask(TaskId id, java.util.Set<org.apache.kafka.common.TopicPartition> partitions, ProcessorTopology topology, org.apache.kafka.clients.consumer.Consumer<byte[],byte[]> mainConsumer, StreamsConfig config, StreamsMetricsImpl streamsMetrics, StateDirectory stateDirectory, ThreadCache cache, org.apache.kafka.common.utils.Time time, ProcessorStateManager stateMgr, RecordCollector recordCollector, InternalProcessorContext processorContext)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddRecords(org.apache.kafka.common.TopicPartition partition, java.lang.Iterable<org.apache.kafka.clients.consumer.ConsumerRecord<byte[],byte[]>> records)Adds records to queues.java.util.Map<org.apache.kafka.common.TopicPartition,java.lang.Long>changelogOffsets()voidcloseClean()Must be idempotent.voidcloseCleanAndRecycleState()Attempt a clean close but do not close the underlying statevoidcloseDirty()Must be idempotent.booleancommitNeeded()booleancommitRequested()Whether or not a request has been made to commit the current statevoidcompleteRestoration()booleanhasRecordsQueued()voidinitializeIfNeeded()booleanisActive()booleanisProcessable(long wallClockTime)An active task is processable if its buffer contains data for all of its input source topic partitions, or if it is enforced to be processablebooleanmaybePunctuateStreamTime()Possibly trigger registered stream-time punctuation functions if current partition group timestamp has reached the defined stamp Note, this is only called in the presence of new recordsbooleanmaybePunctuateSystemTime()Possibly trigger registered system-time punctuation functions if current system timestamp has reached the defined stamp Note, this is called irrespective of the presence of new recordsvoidpostCommit()This should only be called if the attempted commit succeeded for this taskjava.util.Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata>prepareCommit()booleanprocess(long wallClockTime)Process one record.InternalProcessorContextprocessorContext()voidpunctuate(ProcessorNode<?,?> node, long timestamp, PunctuationType type, Punctuator punctuator)java.util.Map<org.apache.kafka.common.TopicPartition,java.lang.Long>purgeableOffsets()voidrecordProcessBatchTime(long processBatchTime)voidrecordProcessTimeRatioAndBufferSize(long allTaskProcessMs, long now)voidresume()- resume the taskCancellableschedule(long interval, PunctuationType type, Punctuator punctuator)Schedules a punctuation for the processorvoidsuspend()java.lang.StringtoString()Produces a string representation containing useful information about a Task.java.lang.StringtoString(java.lang.String indent)Produces a string representation containing useful information about a Task starting with the given indent.voidupdate(java.util.Set<org.apache.kafka.common.TopicPartition> topicPartitions, java.util.Map<java.lang.String,java.util.List<java.lang.String>> allTopologyNodesToSourceTopics)Updates input partitions and topology after rebalance-
Methods inherited from class org.apache.kafka.streams.processor.internals.AbstractTask
changelogPartitions, getStore, id, inputPartitions, isClosed, markChangelogAsCorrupted, revive, state
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.kafka.streams.processor.internals.Task
changelogPartitions, getStore, id, inputPartitions, isClosed, markChangelogAsCorrupted, needsInitializationOrRestoration, revive, state
-
-
-
-
Constructor Detail
-
StreamTask
public StreamTask(TaskId id, java.util.Set<org.apache.kafka.common.TopicPartition> partitions, ProcessorTopology topology, org.apache.kafka.clients.consumer.Consumer<byte[],byte[]> mainConsumer, StreamsConfig config, StreamsMetricsImpl streamsMetrics, StateDirectory stateDirectory, ThreadCache cache, org.apache.kafka.common.utils.Time time, ProcessorStateManager stateMgr, RecordCollector recordCollector, InternalProcessorContext processorContext)
-
-
Method Detail
-
initializeIfNeeded
public void initializeIfNeeded()
- Specified by:
initializeIfNeededin interfaceTask- Throws:
LockException- could happen when multi-threads within the single instance, could retryorg.apache.kafka.common.errors.TimeoutException- if initializing record collector timed outStreamsException- fatal error, should close the thread
-
completeRestoration
public void completeRestoration()
- Specified by:
completeRestorationin interfaceTask- Throws:
org.apache.kafka.common.errors.TimeoutException- if fetching committed offsets timed out
-
prepareCommit
public java.util.Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> prepareCommit()
- Specified by:
prepareCommitin interfaceTask- Returns:
- offsets that should be committed for this task
-
postCommit
public void postCommit()
This should only be called if the attempted commit succeeded for this task- Specified by:
postCommitin interfaceTask
-
closeClean
public void closeClean()
Description copied from interface:TaskMust be idempotent.- Specified by:
closeCleanin interfaceTask
-
closeDirty
public void closeDirty()
Description copied from interface:TaskMust be idempotent.- Specified by:
closeDirtyin interfaceTask
-
update
public void update(java.util.Set<org.apache.kafka.common.TopicPartition> topicPartitions, java.util.Map<java.lang.String,java.util.List<java.lang.String>> allTopologyNodesToSourceTopics)Description copied from interface:TaskUpdates input partitions and topology after rebalance- Specified by:
updatein interfaceTask- Overrides:
updatein classAbstractTask
-
closeCleanAndRecycleState
public void closeCleanAndRecycleState()
Description copied from interface:TaskAttempt a clean close but do not close the underlying state- Specified by:
closeCleanAndRecycleStatein interfaceTask
-
isProcessable
public boolean isProcessable(long wallClockTime)
An active task is processable if its buffer contains data for all of its input source topic partitions, or if it is enforced to be processable
-
process
public boolean process(long wallClockTime)
Process one record.- Specified by:
processin interfaceTask- Returns:
- true if this method processes a record, false if it does not process a record.
- Throws:
TaskMigratedException- if the task producer got fenced (EOS only)
-
recordProcessBatchTime
public void recordProcessBatchTime(long processBatchTime)
- Specified by:
recordProcessBatchTimein interfaceTask
-
recordProcessTimeRatioAndBufferSize
public void recordProcessTimeRatioAndBufferSize(long allTaskProcessMs, long now)- Specified by:
recordProcessTimeRatioAndBufferSizein interfaceTask
-
punctuate
public void punctuate(ProcessorNode<?,?> node, long timestamp, PunctuationType type, Punctuator punctuator)
- Specified by:
punctuatein interfaceProcessorNodePunctuator- Throws:
java.lang.IllegalStateException- if the current node is not nullTaskMigratedException- if the task producer got fenced (EOS only)
-
purgeableOffsets
public java.util.Map<org.apache.kafka.common.TopicPartition,java.lang.Long> purgeableOffsets()
- Specified by:
purgeableOffsetsin interfaceTask
-
addRecords
public void addRecords(org.apache.kafka.common.TopicPartition partition, java.lang.Iterable<org.apache.kafka.clients.consumer.ConsumerRecord<byte[],byte[]>> records)Adds records to queues. If a record has an invalid (i.e., negative) timestamp, the record is skipped and not added to the queue for processing- Specified by:
addRecordsin interfaceTask- Parameters:
partition- the partitionrecords- the records
-
schedule
public Cancellable schedule(long interval, PunctuationType type, Punctuator punctuator)
Schedules a punctuation for the processor- Parameters:
interval- the interval in millisecondstype- the punctuation type- Throws:
java.lang.IllegalStateException- if the current node is not null
-
maybePunctuateStreamTime
public boolean maybePunctuateStreamTime()
Possibly trigger registered stream-time punctuation functions if current partition group timestamp has reached the defined stamp Note, this is only called in the presence of new records- Specified by:
maybePunctuateStreamTimein interfaceTask- Throws:
TaskMigratedException- if the task producer got fenced (EOS only)
-
maybePunctuateSystemTime
public boolean maybePunctuateSystemTime()
Possibly trigger registered system-time punctuation functions if current system timestamp has reached the defined stamp Note, this is called irrespective of the presence of new records- Specified by:
maybePunctuateSystemTimein interfaceTask- Throws:
TaskMigratedException- if the task producer got fenced (EOS only)
-
commitRequested
public boolean commitRequested()
Whether or not a request has been made to commit the current state- Specified by:
commitRequestedin interfaceTask
-
processorContext
public InternalProcessorContext processorContext()
-
toString
public java.lang.String toString()
Produces a string representation containing useful information about a Task. This is useful in debugging scenarios.- Overrides:
toStringin classjava.lang.Object- Returns:
- A string representation of the StreamTask instance.
-
toString
public java.lang.String toString(java.lang.String indent)
Produces a string representation containing useful information about a Task starting with the given indent. This is useful in debugging scenarios.- Returns:
- A string representation of the Task instance.
-
commitNeeded
public boolean commitNeeded()
- Specified by:
commitNeededin interfaceTask
-
changelogOffsets
public java.util.Map<org.apache.kafka.common.TopicPartition,java.lang.Long> changelogOffsets()
- Specified by:
changelogOffsetsin interfaceTask- Returns:
- the offsets of all the changelog partitions associated with this task, indicating the current positions of the logged state stores of the task.
-
hasRecordsQueued
public boolean hasRecordsQueued()
-
-