Class AbstractSourceTask
java.lang.Object
org.apache.kafka.connect.source.SourceTask
io.aiven.kafka.connect.common.source.AbstractSourceTask
- All Implemented Interfaces:
org.apache.kafka.connect.connector.Task
public abstract class AbstractSourceTask
extends org.apache.kafka.connect.source.SourceTask
This class handles extracting records from an iterator and returning them to Kafka. It uses an exponential backoff
with jitter to reduce the number of calls to the backend when there is no data. This solution:
- When polled this implementation moves available records from the SourceRecord iterator to the return array.
- if there are no records
poll()will return null.- The poll will delay no more than approx 5 seconds.
- Upto
maxPollRecordswill be sent in a single poll request - When the connector is stopped any collected records are returned to kafka before stopping.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA functional interface that will abort the timer.static classPerforms a delay based on the number of successiveAbstractSourceTask.Backoff.delay()orAbstractSourceTask.Backoff.cleanDelay()calls without aAbstractSourceTask.Backoff.reset().static interfaceAn interface to define the Backoff configuration.static interfaceA functional interface to return long values.static classCalculates elapsed time and flags when expired.Nested classes/interfaces inherited from class org.apache.kafka.connect.source.SourceTask
org.apache.kafka.connect.source.SourceTask.TransactionBoundary -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DurationThe maximum time to spend polling.static final List<org.apache.kafka.connect.source.SourceRecord>Fields inherited from class org.apache.kafka.connect.source.SourceTask
context, TRANSACTION_BOUNDARY_CONFIG -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidClose any resources the source has open.protected abstract SourceCommonConfigCalled bystart(java.util.Map<java.lang.String, java.lang.String>)to allows the concrete implementation to configure itself based on properties.protected abstract Iterator<org.apache.kafka.connect.source.SourceRecord>Gets the iterator of SourceRecords.final booleanReturns the running state of the task.final List<org.apache.kafka.connect.source.SourceRecord>poll()final voidprotected final booleanReturnstrueif the connector is not stopped and the timer has not expired.final voidstop()Methods inherited from class org.apache.kafka.connect.source.SourceTask
commit, commitRecord, commitRecord, initializeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.kafka.connect.connector.Task
version
-
Field Details
-
NULL_RESULT
-
MAX_POLL_TIME
The maximum time to spend polling. This is set to 4 seconds as 5 seconds is the kafka limit that is allotted to a system for shutdown, and this allows the polling and iterator to smoothly shutdown accounting for latentcy.
-
-
Constructor Details
-
AbstractSourceTask
protected AbstractSourceTask(org.slf4j.Logger logger) Constructor.- Parameters:
logger- the logger to use.
-
-
Method Details
-
getIterator
protected abstract Iterator<org.apache.kafka.connect.source.SourceRecord> getIterator(AbstractSourceTask.BackoffConfig config) Gets the iterator of SourceRecords. The iterator that SourceRecords are extracted from for a poll event. When this iterator runs out of records it should attempt to reset and read more records from the backend on the nexthasNext()call. In this way it should detect when new data has been added to the backend and continue processing.This method should handle any backend exception that can be retried. Any runtime exceptions that are thrown when this iterator executes may cause the task to abort.
- Parameters:
config- the configuration for the Backoff.- Returns:
- The iterator of SourceRecords.
-
configure
Called bystart(java.util.Map<java.lang.String, java.lang.String>)to allows the concrete implementation to configure itself based on properties.- Parameters:
props- the properties to use for configuration.
-
start
- Specified by:
startin interfaceorg.apache.kafka.connect.connector.Task- Specified by:
startin classorg.apache.kafka.connect.source.SourceTask
-
stillPolling
protected final boolean stillPolling()Returnstrueif the connector is not stopped and the timer has not expired.- Returns:
trueif the connector is not stopped and the timer has not expired.
-
poll
- Specified by:
pollin classorg.apache.kafka.connect.source.SourceTask
-
stop
public final void stop()- Specified by:
stopin interfaceorg.apache.kafka.connect.connector.Task- Specified by:
stopin classorg.apache.kafka.connect.source.SourceTask
-
isRunning
public final boolean isRunning()Returns the running state of the task.- Returns:
trueif the connector is running,falseotherwise.
-
closeResources
protected abstract void closeResources()Close any resources the source has open. Called by the IteratorRunnable when it is stopping.
-