Class AbstractReader
- java.lang.Object
-
- io.debezium.connector.mysql.legacy.AbstractReader
-
- All Implemented Interfaces:
Reader
- Direct Known Subclasses:
BinlogReader,SnapshotReader
public abstract class AbstractReader extends Object implements Reader
A component that performs a snapshot of a MySQL server, and records the schema changes inMySqlSchema.- Author:
- Randall Hauch
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAbstractReader.AcceptAllPredicateA predicate that returns true for all sourceRecords-
Nested classes/interfaces inherited from interface io.debezium.connector.mysql.legacy.Reader
Reader.State
-
-
Field Summary
Fields Modifier and Type Field Description private HaltingPredicateacceptAndContinueprotected ChangeEventQueueMetricschangeEventQueueMetricsprotected MySqlJdbcContextconnectionContextprotected MySqlTaskContextcontextprivate AtomicReference<org.apache.kafka.connect.errors.ConnectException>failureprivate org.apache.kafka.connect.errors.ConnectExceptionfailureExceptionprotected org.slf4j.Loggerloggerprivate intmaxBatchSizeprivate Metronomemetronomeprivate Stringnameprivate AtomicReference<MySqlPartition>partitionprivate DurationpollIntervalprivate BlockingQueue<org.apache.kafka.connect.source.SourceRecord>recordsprivate AtomicBooleanrunningprivate AtomicBooleansuccessprivate AtomicReference<Consumer<MySqlPartition>>uponCompletion
-
Constructor Summary
Constructors Constructor Description AbstractReader(String name, MySqlTaskContext context, HaltingPredicate acceptAndContinue)Create a snapshot reader.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidcleanupResources(MySqlPartition partition)protected voidcompleteSuccessfully()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.voiddestroy()After the reader has stopped, there may still be some resources we want left available until the connector task is destroyed.protected abstract voiddoCleanup()The reader has completed all processing and allenqueued recordshave beenconsumed, so this reader should clean up any resources that might remain.protected voiddoDestroy()The reader has been requested to de-initialize resources after stopping.protected voiddoInitialize()The 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 voidfailed(Throwable error)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.protected voidfailed(Throwable error, String msg)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.voidinitialize()Perform any initialization of the reader before being started.protected booleanisRunning()Stringname()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.Reader.Statestate()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.StringtoString()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.ConnectExceptionwrap(Throwable error)Wraps the specified exception in aConnectException, ensuring that all useful state is captured inside the new exception's message.
-
-
-
Field Detail
-
logger
protected final org.slf4j.Logger logger
-
name
private final String name
-
context
protected final MySqlTaskContext context
-
connectionContext
protected final MySqlJdbcContext connectionContext
-
records
private final BlockingQueue<org.apache.kafka.connect.source.SourceRecord> records
-
running
private final AtomicBoolean running
-
partition
private final AtomicReference<MySqlPartition> partition
-
success
private final AtomicBoolean success
-
failure
private final AtomicReference<org.apache.kafka.connect.errors.ConnectException> failure
-
failureException
private org.apache.kafka.connect.errors.ConnectException failureException
-
maxBatchSize
private final int maxBatchSize
-
metronome
private final Metronome metronome
-
uponCompletion
private final AtomicReference<Consumer<MySqlPartition>> uponCompletion
-
pollInterval
private final Duration pollInterval
-
changeEventQueueMetrics
protected final ChangeEventQueueMetrics changeEventQueueMetrics
-
acceptAndContinue
private final HaltingPredicate acceptAndContinue
-
-
Constructor Detail
-
AbstractReader
public AbstractReader(String name, MySqlTaskContext context, HaltingPredicate acceptAndContinue)
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 Detail
-
uponCompletion
public void uponCompletion(Consumer<MySqlPartition> handler)
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
public void start(MySqlPartition partition)
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
protected abstract void doStart(MySqlPartition partition)
The reader has been requested to start, so initialize any un-initialized resources required by the reader.
-
doStop
protected abstract void doStop(MySqlPartition partition)
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
protected void failed(Throwable error)
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
protected void failed(Throwable error, String msg)
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
protected org.apache.kafka.connect.errors.ConnectException wrap(Throwable error)
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
public Reader.State state()
Description copied from interface:ReaderGet the current state of this reader.
-
isRunning
protected boolean isRunning()
-
poll
public List<org.apache.kafka.connect.source.SourceRecord> poll() throws InterruptedException
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
protected void cleanupResources(MySqlPartition partition)
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
protected void pollComplete(List<org.apache.kafka.connect.source.SourceRecord> batch)
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 InterruptedExceptionEnqueue 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
-
-