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 class
AbstractReader.AcceptAllPredicate
A 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 HaltingPredicate
acceptAndContinue
protected ChangeEventQueueMetrics
changeEventQueueMetrics
protected MySqlJdbcContext
connectionContext
protected MySqlTaskContext
context
private AtomicReference<org.apache.kafka.connect.errors.ConnectException>
failure
private org.apache.kafka.connect.errors.ConnectException
failureException
protected org.slf4j.Logger
logger
private int
maxBatchSize
private Metronome
metronome
private String
name
private Duration
pollInterval
private BlockingQueue<org.apache.kafka.connect.source.SourceRecord>
records
private AtomicBoolean
running
private AtomicBoolean
success
private AtomicReference<Runnable>
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 void
cleanupResources()
protected void
completeSuccessfully()
Call this method only when the reader has successfully completed all of its work, signaling that subsequent calls topoll()
should forever returnnull
and that this reader should transition fromReader.State.STOPPING
toReader.State.STOPPED
.void
destroy()
After the reader has stopped, there may still be some resources we want left available until the connector task is destroyed.protected abstract void
doCleanup()
The reader has completed all processing and allenqueued records
have beenconsumed
, so this reader should clean up any resources that might remain.protected void
doDestroy()
The reader has been requested to de-initialize resources after stopping.protected void
doInitialize()
The reader has been requested to initialize resources prior to starting.protected abstract void
doStart()
The reader has been requested to start, so initialize any un-initialized resources required by the reader.protected abstract void
doStop()
The reader has been requested to stop, so perform any work required to stop the reader's resources that were previouslystarted
.protected void
enqueueRecord(org.apache.kafka.connect.source.SourceRecord record)
Enqueue a record so that it can be obtained when this reader ispolled
.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.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.void
initialize()
Perform any initialization of the reader before being started.protected boolean
isRunning()
String
name()
Get the name of this reader.List<org.apache.kafka.connect.source.SourceRecord>
poll()
Poll for the next batch of source records.protected void
pollComplete(List<org.apache.kafka.connect.source.SourceRecord> batch)
Method called whenpoll()
completes sending a non-zero-sized batch of records.void
start()
Start the reader and return immediately.Reader.State
state()
Get the current state of this reader.void
stop()
Stop the reader from running and transition to theReader.State.STOPPING
state until all remaining records areconsumed
, at which point its state transitions toReader.State.STOPPED
.String
toString()
void
uponCompletion(Runnable handler)
Set the function that should be called when this reader transitions from theReader.State.STOPPING
toReader.State.STOPPED
state, which is after all generated records have been consumed via thepoll
method.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.
-
-
-
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
-
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<Runnable> 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 testedSourceRecord
should 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(Runnable handler)
Description copied from interface:Reader
Set the function that should be called when this reader transitions from theReader.State.STOPPING
toReader.State.STOPPED
state, which is after all generated records have been consumed via thepoll
method.This method should only be called while the reader is in the
Reader.State.STOPPED
state.- Specified by:
uponCompletion
in interfaceReader
- Parameters:
handler
- the function; may not be null
-
initialize
public final void initialize()
Description copied from interface:Reader
Perform any initialization of the reader before being started. This method should be called exactly once beforeReader.start()
is called, and it should block until all initialization is completed.- Specified by:
initialize
in interfaceReader
-
destroy
public final void destroy()
Description copied from interface:Reader
After 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()
Description copied from interface:Reader
Start the reader and return immediately. Once started, theSourceRecord
s 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:Reader
Stop the reader from running and transition to theReader.State.STOPPING
state 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()
.
-
doDestroy
protected void doDestroy()
The reader has been requested to de-initialize resources after stopping. This should only be called once afterdoStop()
.
-
doStart
protected abstract void doStart()
The reader has been requested to start, so initialize any un-initialized resources required by the reader.
-
doStop
protected abstract void 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
stop()
is called, and the first timeisRunning()
will returntrue
the first time andfalse
for any subsequent calls.
-
doCleanup
protected abstract void doCleanup()
The reader has completed all processing and allenqueued records
have 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 returnnull
and that this reader should transition fromReader.State.STOPPING
toReader.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 benull
msg
- 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:Reader
Get 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:Reader
Poll for the next batch of source records. This method returnsnull
only when all records generated by this reader have been processed, following the natural or explicitstopping
of 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:
poll
in interfaceReader
- Returns:
- the list of source records that may or may not be empty; or
null
when 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()
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 explicitlystopped
while 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 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
-
-