Class AbstractReader
java.lang.Object
io.debezium.connector.mysql.legacy.AbstractReader
- All Implemented Interfaces:
Reader
- Direct Known Subclasses:
BinlogReader,SnapshotReader
A component that performs a snapshot of a MySQL server, and records the schema changes in
MySqlSchema.- Author:
- Randall Hauch
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA predicate that returns true for all sourceRecordsNested classes/interfaces inherited from interface io.debezium.connector.mysql.legacy.Reader
Reader.State -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final HaltingPredicateprotected final ChangeEventQueueMetricsprotected final MySqlJdbcContextprotected final MySqlTaskContextprivate final AtomicReference<org.apache.kafka.connect.errors.ConnectException>private org.apache.kafka.connect.errors.ConnectExceptionprotected final org.slf4j.Loggerprivate final intprivate final Metronomeprivate final Stringprivate final AtomicReference<MySqlPartition>private final Durationprivate final BlockingQueue<org.apache.kafka.connect.source.SourceRecord>private final AtomicBooleanprivate final AtomicBooleanprivate final AtomicReference<Consumer<MySqlPartition>> -
Constructor Summary
ConstructorsConstructorDescriptionAbstractReader(String name, MySqlTaskContext context, HaltingPredicate acceptAndContinue) Create a snapshot reader. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcleanupResources(MySqlPartition partition) protected voidCall this method only when the reader has successfully completed all of its work, signaling that subsequent calls topoll()should forever returnnulland that this reader should transition fromReader.State.STOPPINGtoReader.State.STOPPED.final voiddestroy()After the reader has stopped, there may still be some resources we want left available until the connector task is destroyed.protected abstract voidThe reader has completed all processing and allenqueued recordshave beenconsumed, so this reader should clean up any resources that might remain.protected voidThe reader has been requested to de-initialize resources after stopping.protected voidThe reader has been requested to initialize resources prior to starting.protected abstract voiddoStart(MySqlPartition partition) The reader has been requested to start, so initialize any un-initialized resources required by the reader.protected abstract voiddoStop(MySqlPartition partition) The reader has been requested to stop, so perform any work required to stop the reader's resources that were previouslystarted.protected voidenqueueRecord(org.apache.kafka.connect.source.SourceRecord record) Enqueue a record so that it can be obtained when this reader ispolled.protected voidCall this method only when the reader has failed, that a subsequent call topoll()should throw this error, and thatdoCleanup()can be called at any time.protected voidCall this method only when the reader has failed, that a subsequent call topoll()should throw this error, and thatdoCleanup()can be called at any time.final voidPerform any initialization of the reader before being started.protected booleanname()Get the name of this reader.List<org.apache.kafka.connect.source.SourceRecord>poll()Poll for the next batch of source records.protected voidpollComplete(List<org.apache.kafka.connect.source.SourceRecord> batch) Method called whenpoll()completes sending a non-zero-sized batch of records.voidstart(MySqlPartition partition) Start the reader and return immediately.state()Get the current state of this reader.voidstop()Stop the reader from running and transition to theReader.State.STOPPINGstate until all remaining records areconsumed, at which point its state transitions toReader.State.STOPPED.toString()voiduponCompletion(Consumer<MySqlPartition> handler) Set the function that should be called when this reader transitions from theReader.State.STOPPINGtoReader.State.STOPPEDstate, which is after all generated records have been consumed via thepollmethod.protected org.apache.kafka.connect.errors.ConnectExceptionWraps the specified exception in aConnectException, ensuring that all useful state is captured inside the new exception's message.
-
Field Details
-
logger
protected final org.slf4j.Logger logger -
name
-
context
-
connectionContext
-
records
-
running
-
partition
-
success
-
failure
-
failureException
private org.apache.kafka.connect.errors.ConnectException failureException -
maxBatchSize
private final int maxBatchSize -
metronome
-
uponCompletion
-
pollInterval
-
changeEventQueueMetrics
-
acceptAndContinue
-
-
Constructor Details
-
AbstractReader
Create a snapshot reader.- Parameters:
name- the name of the readercontext- the task context in which this reader is running; may not be nullacceptAndContinue- a predicate that returns true if the testedSourceRecordshould be accepted and false if the record and all subsequent records should be ignored. The reader will stop accepting records onceenqueueRecord(SourceRecord)is called with a record that tests as false. Can be null. If null, all records will be accepted.
-
-
Method Details
-
name
Description copied from interface:ReaderGet the name of this reader. -
uponCompletion
Description copied from interface:ReaderSet the function that should be called when this reader transitions from theReader.State.STOPPINGtoReader.State.STOPPEDstate, which is after all generated records have been consumed via thepollmethod.This method should only be called while the reader is in the
Reader.State.STOPPEDstate.- Specified by:
uponCompletionin interfaceReader- Parameters:
handler- the function; may not be null
-
initialize
public final void initialize()Description copied from interface:ReaderPerform any initialization of the reader before being started. This method should be called exactly once beforeReader.start(MySqlPartition)()} is called, and it should block until all initialization is completed.- Specified by:
initializein interfaceReader
-
destroy
public final void destroy()Description copied from interface:ReaderAfter the reader has stopped, there may still be some resources we want left available until the connector task is destroyed. This method is used to clean up those remaining resources upon shutdown. This method is effectively the opposite ofReader.initialize(), performing any de-initialization of the reader entity before shutdown. This method should be called exactly once afterReader.stop()is called, and it should block until all de-initialization is completed. -
start
Description copied from interface:ReaderStart the reader and return immediately. Once started, theSourceRecords generated by the reader can be obtained by periodically callingReader.poll()until that method returnsnull.This method does nothing if it is already running.
-
stop
public void stop()Description copied from interface:ReaderStop the reader from running and transition to theReader.State.STOPPINGstate until all remaining records areconsumed, at which point its state transitions toReader.State.STOPPED. -
doInitialize
protected void doInitialize()The reader has been requested to initialize resources prior to starting. This should only be called once beforedoStart(MySqlPartition). -
doDestroy
protected void doDestroy()The reader has been requested to de-initialize resources after stopping. This should only be called once afterdoStop(MySqlPartition). -
doStart
The reader has been requested to start, so initialize any un-initialized resources required by the reader. -
doStop
The reader has been requested to stop, so perform any work required to stop the reader's resources that were previouslystarted.This method is always called when
Reader.stop()is called, and the first timeisRunning()will returntruethe first time andfalsefor any subsequent calls. -
doCleanup
protected abstract void doCleanup()The reader has completed all processing and allenqueued recordshave beenconsumed, so this reader should clean up any resources that might remain. -
completeSuccessfully
protected void completeSuccessfully()Call this method only when the reader has successfully completed all of its work, signaling that subsequent calls topoll()should forever returnnulland that this reader should transition fromReader.State.STOPPINGtoReader.State.STOPPED. -
failed
Call this method only when the reader has failed, that a subsequent call topoll()should throw this error, and thatdoCleanup()can be called at any time.- Parameters:
error- the error that resulted in the failure; should not benull
-
failed
Call this method only when the reader has failed, that a subsequent call topoll()should throw this error, and thatdoCleanup()can be called at any time.- Parameters:
error- the error that resulted in the failure; should not benullmsg- the error message; may not be null
-
wrap
Wraps the specified exception in aConnectException, ensuring that all useful state is captured inside the new exception's message.- Parameters:
error- the exception; may not be null- Returns:
- the wrapped Kafka Connect exception
-
state
Description copied from interface:ReaderGet the current state of this reader. -
isRunning
protected boolean isRunning() -
poll
Description copied from interface:ReaderPoll for the next batch of source records. This method returnsnullonly when all records generated by this reader have been processed, following the natural or explicitstoppingof this reader. Note that this method may block if no additional records are available but the reader may produce more, thus callers should call this method continually until this method returnsnull.- Specified by:
pollin interfaceReader- Returns:
- the list of source records that may or may not be empty; or
nullwhen there will be no more records because the reader has completelyReader.State.STOPPED. - Throws:
InterruptedException- if this thread is interrupted while waiting for more records
-
cleanupResources
This method is normally called bypoll()when there this reader finishes normally and all generated records are consumed prior to beingstopped. However, if this reader is explicitlystoppedwhile still working, then subclasses should call this method when they have completed all of their shutdown work. -
pollComplete
Method called whenpoll()completes sending a non-zero-sized batch of records.- Parameters:
batch- the batch of records being recorded
-
enqueueRecord
protected void enqueueRecord(org.apache.kafka.connect.source.SourceRecord record) throws InterruptedException Enqueue a record so that it can be obtained when this reader ispolled. This method will block if the queue is full.- Parameters:
record- the record to be enqueued- Throws:
InterruptedException- if interrupted while waiting for the queue to have room for this record
-
toString
-