Class EmbeddedEngine
- java.lang.Object
-
- io.debezium.embedded.EmbeddedEngine
-
- All Implemented Interfaces:
io.debezium.engine.DebeziumEngine<org.apache.kafka.connect.source.SourceRecord>,Closeable,AutoCloseable,Runnable
@ThreadSafe public final class EmbeddedEngine extends Object implements io.debezium.engine.DebeziumEngine<org.apache.kafka.connect.source.SourceRecord>
A mechanism for running a single Kafka ConnectSourceConnectorwithin an application's process. An embedded connector is entirely standalone and only talks with the source system; no Kafka, Kafka Connect, or Zookeeper processes are needed. Applications using an embedded connector simply set one up and supply aconsumer functionto which the connector will pass allSourceRecords containing database change events.With an embedded connector, the application that runs the connector assumes all responsibility for fault tolerance, scalability, and durability. Additionally, applications must specify how the connector can store its relational database schema history and offsets. By default, this information will be stored in memory and will thus be lost upon application restart.
Embedded connectors are designed to be submitted to an
ExecutororExecutorServicefor execution by a single thread, and a running connector can be stopped either by callingstop()from another thread or by interrupting the running thread (e.g., as is the case withExecutorService.shutdownNow()).- Author:
- Randall Hauch
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceEmbeddedEngine.BuilderDeprecated.static classEmbeddedEngine.BuilderImplstatic interfaceEmbeddedEngine.ChangeConsumerDeprecated.static interfaceEmbeddedEngine.CompletionCallbackDeprecated.static classEmbeddedEngine.CompletionResultA callback function to be notified when the connector completes.static interfaceEmbeddedEngine.ConnectorCallbackDeprecated.protected static classEmbeddedEngine.EmbeddedConfigstatic interfaceEmbeddedEngine.RecordCommitterDeprecated.protected classEmbeddedEngine.SourceRecordOffsetsImplementation ofDebeziumEngine.Offsetswhich can be used to construct aSourceRecordwith its offsets.
-
Field Summary
Fields Modifier and Type Field Description protected static Field.SetALL_FIELDSThe array of all exposed fields.private ClassLoaderclassLoaderprivate Clockclockprivate io.debezium.engine.DebeziumEngine.CompletionCallbackcompletionCallbackprivate EmbeddedEngine.CompletionResultcompletionResultprivate Configurationconfigstatic FieldCONNECTOR_CLASSA required field for an embedded connector that specifies the name of the normal Debezium connector's Java class.static Field.SetCONNECTOR_FIELDSThe array of fields that are required by each connectors.private io.debezium.engine.DebeziumEngine.ConnectorCallbackconnectorCallbackstatic FieldENGINE_NAMEA required field for an embedded connector that specifies the unique name for the connector instance.private io.debezium.engine.DebeziumEngine.ChangeConsumer<org.apache.kafka.connect.source.SourceRecord>handlerprotected static FieldINTERNAL_KEY_CONVERTER_CLASSprotected static FieldINTERNAL_VALUE_CONVERTER_CLASSprivate org.apache.kafka.connect.storage.ConverterkeyConverterprivate VariableLatchlatchprivate static org.slf4j.LoggerLOGGERstatic FieldOFFSET_COMMIT_POLICYstatic FieldOFFSET_COMMIT_TIMEOUT_MSAn optional advanced field that specifies the maximum amount of time that the embedded connector should wait for an offset commit to complete.static FieldOFFSET_FLUSH_INTERVAL_MSAn optional advanced field that specifies the maximum amount of time that the embedded connector should wait for an offset commit to complete.static FieldOFFSET_STORAGEAn optional field that specifies the name of the class that implements theOffsetBackingStoreinterface, and that will be used to store offsets recorded by the connector.static FieldOFFSET_STORAGE_FILE_FILENAMEAn optional field that specifies the file location for theFileOffsetBackingStore.static FieldOFFSET_STORAGE_KAFKA_PARTITIONSAn optional field that specifies the number of partitions for theKafkaOffsetBackingStore.static FieldOFFSET_STORAGE_KAFKA_REPLICATION_FACTORAn optional field that specifies the replication factor for theKafkaOffsetBackingStore.static FieldOFFSET_STORAGE_KAFKA_TOPICAn optional field that specifies the topic name for theKafkaOffsetBackingStore.private io.debezium.engine.spi.OffsetCommitPolicyoffsetCommitPolicyprivate longrecordsSinceLastCommitprivate AtomicReference<Thread>runningThreadprivate org.apache.kafka.connect.source.SourceTasktaskprivate longtimeOfLastCommitMillisprivate Transformationstransformationsstatic FieldTRANSFORMSA list of SMTs to be applied on the messages generated by the engine.private org.apache.kafka.connect.storage.ConvertervalueConverterprivate static DurationWAIT_FOR_COMPLETION_BEFORE_INTERRUPT_DEFAULTHow long we wait before forcefully stopping the connector thread when shutting down.private static StringWAIT_FOR_COMPLETION_BEFORE_INTERRUPT_PROPprivate org.apache.kafka.connect.runtime.WorkerConfigworkerConfig
-
Constructor Summary
Constructors Modifier Constructor Description privateEmbeddedEngine(Configuration config, ClassLoader classLoader, Clock clock, io.debezium.engine.DebeziumEngine.ChangeConsumer<org.apache.kafka.connect.source.SourceRecord> handler, io.debezium.engine.DebeziumEngine.CompletionCallback completionCallback, io.debezium.engine.DebeziumEngine.ConnectorCallback connectorCallback, io.debezium.engine.spi.OffsetCommitPolicy offsetCommitPolicy)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleanawait(long timeout, TimeUnit unit)Wait for the connector to complete processing.private static EmbeddedEngine.ChangeConsumerbuildDefaultChangeConsumer(Consumer<org.apache.kafka.connect.source.SourceRecord> consumer)protected EmbeddedEngine.RecordCommitterbuildRecordCommitter(org.apache.kafka.connect.storage.OffsetStorageWriter offsetWriter, org.apache.kafka.connect.source.SourceTask task, Duration commitTimeout)Creates a new RecordCommitter that is responsible for informing the engine about the updates to the given batchvoidclose()protected voidcommitOffsets(org.apache.kafka.connect.storage.OffsetStorageWriter offsetWriter, Duration commitTimeout, org.apache.kafka.connect.source.SourceTask task)Flush offsets to storage.protected voidcompletedFlush(Throwable error, Void result)static EmbeddedEngine.Buildercreate()Deprecated.private voidfail(String msg)private voidfail(String msg, Throwable error)booleanisRunning()Determine if this embedded connector is currently running.protected voidmaybeFlush(org.apache.kafka.connect.storage.OffsetStorageWriter offsetWriter, io.debezium.engine.spi.OffsetCommitPolicy policy, Duration commitTimeout, org.apache.kafka.connect.source.SourceTask task)Determine if we should flush offsets to storage, and if so then attempt to flush offsets.voidrun()Run this embedded connector and deliver database changes to the registeredConsumer.voidrunWithTask(Consumer<org.apache.kafka.connect.source.SourceTask> consumer)booleanstop()Stop the execution of this embedded connector.private voidsucceed(String msg)StringtoString()
-
-
-
Field Detail
-
ENGINE_NAME
public static final Field ENGINE_NAME
A required field for an embedded connector that specifies the unique name for the connector instance.
-
CONNECTOR_CLASS
public static final Field CONNECTOR_CLASS
A required field for an embedded connector that specifies the name of the normal Debezium connector's Java class.
-
OFFSET_STORAGE
public static final Field OFFSET_STORAGE
An optional field that specifies the name of the class that implements theOffsetBackingStoreinterface, and that will be used to store offsets recorded by the connector.
-
OFFSET_STORAGE_FILE_FILENAME
public static final Field OFFSET_STORAGE_FILE_FILENAME
An optional field that specifies the file location for theFileOffsetBackingStore.- See Also:
OFFSET_STORAGE
-
OFFSET_STORAGE_KAFKA_TOPIC
public static final Field OFFSET_STORAGE_KAFKA_TOPIC
An optional field that specifies the topic name for theKafkaOffsetBackingStore.- See Also:
OFFSET_STORAGE
-
OFFSET_STORAGE_KAFKA_PARTITIONS
public static final Field OFFSET_STORAGE_KAFKA_PARTITIONS
An optional field that specifies the number of partitions for theKafkaOffsetBackingStore.- See Also:
OFFSET_STORAGE
-
OFFSET_STORAGE_KAFKA_REPLICATION_FACTOR
public static final Field OFFSET_STORAGE_KAFKA_REPLICATION_FACTOR
An optional field that specifies the replication factor for theKafkaOffsetBackingStore.- See Also:
OFFSET_STORAGE
-
OFFSET_FLUSH_INTERVAL_MS
public static final Field OFFSET_FLUSH_INTERVAL_MS
An optional advanced field that specifies the maximum amount of time that the embedded connector should wait for an offset commit to complete.
-
OFFSET_COMMIT_TIMEOUT_MS
public static final Field OFFSET_COMMIT_TIMEOUT_MS
An optional advanced field that specifies the maximum amount of time that the embedded connector should wait for an offset commit to complete.
-
OFFSET_COMMIT_POLICY
public static final Field OFFSET_COMMIT_POLICY
-
INTERNAL_KEY_CONVERTER_CLASS
protected static final Field INTERNAL_KEY_CONVERTER_CLASS
-
INTERNAL_VALUE_CONVERTER_CLASS
protected static final Field INTERNAL_VALUE_CONVERTER_CLASS
-
TRANSFORMS
public static final Field TRANSFORMS
A list of SMTs to be applied on the messages generated by the engine.
-
CONNECTOR_FIELDS
public static final Field.Set CONNECTOR_FIELDS
The array of fields that are required by each connectors.
-
ALL_FIELDS
protected static final Field.Set ALL_FIELDS
The array of all exposed fields.
-
WAIT_FOR_COMPLETION_BEFORE_INTERRUPT_DEFAULT
private static final Duration WAIT_FOR_COMPLETION_BEFORE_INTERRUPT_DEFAULT
How long we wait before forcefully stopping the connector thread when shutting down. Must be longer thanChangeEventSourceCoordinator.SHUTDOWN_WAIT_TIMEOUT* 2.
-
WAIT_FOR_COMPLETION_BEFORE_INTERRUPT_PROP
private static final String WAIT_FOR_COMPLETION_BEFORE_INTERRUPT_PROP
- See Also:
- Constant Field Values
-
LOGGER
private static final org.slf4j.Logger LOGGER
-
config
private final Configuration config
-
clock
private final Clock clock
-
classLoader
private final ClassLoader classLoader
-
handler
private final io.debezium.engine.DebeziumEngine.ChangeConsumer<org.apache.kafka.connect.source.SourceRecord> handler
-
completionCallback
private final io.debezium.engine.DebeziumEngine.CompletionCallback completionCallback
-
connectorCallback
private final io.debezium.engine.DebeziumEngine.ConnectorCallback connectorCallback
-
runningThread
private final AtomicReference<Thread> runningThread
-
latch
private final VariableLatch latch
-
keyConverter
private final org.apache.kafka.connect.storage.Converter keyConverter
-
valueConverter
private final org.apache.kafka.connect.storage.Converter valueConverter
-
workerConfig
private final org.apache.kafka.connect.runtime.WorkerConfig workerConfig
-
completionResult
private final EmbeddedEngine.CompletionResult completionResult
-
recordsSinceLastCommit
private long recordsSinceLastCommit
-
timeOfLastCommitMillis
private long timeOfLastCommitMillis
-
offsetCommitPolicy
private io.debezium.engine.spi.OffsetCommitPolicy offsetCommitPolicy
-
task
private org.apache.kafka.connect.source.SourceTask task
-
transformations
private final Transformations transformations
-
-
Constructor Detail
-
EmbeddedEngine
private EmbeddedEngine(Configuration config, ClassLoader classLoader, Clock clock, io.debezium.engine.DebeziumEngine.ChangeConsumer<org.apache.kafka.connect.source.SourceRecord> handler, io.debezium.engine.DebeziumEngine.CompletionCallback completionCallback, io.debezium.engine.DebeziumEngine.ConnectorCallback connectorCallback, io.debezium.engine.spi.OffsetCommitPolicy offsetCommitPolicy)
-
-
Method Detail
-
buildDefaultChangeConsumer
private static EmbeddedEngine.ChangeConsumer buildDefaultChangeConsumer(Consumer<org.apache.kafka.connect.source.SourceRecord> consumer)
-
create
@Deprecated public static EmbeddedEngine.Builder create()
Deprecated.Obtain a newEmbeddedEngine.Builderinstance that can be used to construct runnableEmbeddedEngineinstances.- Returns:
- the new builder; never null
-
isRunning
public boolean isRunning()
Determine if this embedded connector is currently running.- Returns:
trueif running, orfalseotherwise
-
fail
private void fail(String msg)
-
succeed
private void succeed(String msg)
-
run
public void run()
Run this embedded connector and deliver database changes to the registeredConsumer. This method blocks until the connector is stopped.First, the method checks to see if this instance is currently
running, and if so immediately returns.If the configuration is valid, this method starts the connector and starts polling the connector for change events. All messages are delivered in batches to the
Consumerregistered with this embedded connector. The batch size, polling frequency, and other parameters are controlled via configuration settings. This continues until this connector isstopped.Note that there are two ways to stop a connector running on a thread: calling
stop()from another thread, or interrupting the thread (e.g., viaExecutorService.shutdownNow()).This method can be called repeatedly as needed.
-
buildRecordCommitter
protected EmbeddedEngine.RecordCommitter buildRecordCommitter(org.apache.kafka.connect.storage.OffsetStorageWriter offsetWriter, org.apache.kafka.connect.source.SourceTask task, Duration commitTimeout)
Creates a new RecordCommitter that is responsible for informing the engine about the updates to the given batch- Parameters:
offsetWriter- the offsetWriter current in usetask- the sourcetaskcommitTimeout- the time in ms until a commit times out- Returns:
- the new recordCommitter to be used for a given batch
-
maybeFlush
protected void maybeFlush(org.apache.kafka.connect.storage.OffsetStorageWriter offsetWriter, io.debezium.engine.spi.OffsetCommitPolicy policy, Duration commitTimeout, org.apache.kafka.connect.source.SourceTask task) throws InterruptedExceptionDetermine if we should flush offsets to storage, and if so then attempt to flush offsets.- Parameters:
offsetWriter- the offset storage writer; may not be nullpolicy- the offset commit policy; may not be nullcommitTimeout- the timeout to wait for commit resultstask- the task which produced the records for which the offsets have been committed- Throws:
InterruptedException
-
commitOffsets
protected void commitOffsets(org.apache.kafka.connect.storage.OffsetStorageWriter offsetWriter, Duration commitTimeout, org.apache.kafka.connect.source.SourceTask task) throws InterruptedExceptionFlush offsets to storage.- Parameters:
offsetWriter- the offset storage writer; may not be nullcommitTimeout- the timeout to wait for commit resultstask- the task which produced the records for which the offsets have been committed- Throws:
InterruptedException
-
stop
public boolean stop()
Stop the execution of this embedded connector. This method does not block until the connector is stopped; useawait(long, TimeUnit)for this purpose.- Returns:
trueif the connector wasrunningand will eventually stop, orfalseif it was not running when this method is called- See Also:
await(long, TimeUnit)
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
await
public boolean await(long timeout, TimeUnit unit) throws InterruptedExceptionWait for the connector to complete processing. If the processor is not running, this method returns immediately; however, if the processor isstoppedand restarted before this method is called, this method will return only when it completes the second time.- Parameters:
timeout- the maximum amount of time to wait before returningunit- the unit of time; may not be null- Returns:
trueif the connector completed within the timeout (or was not running), orfalseif it is still running when the timeout occurred- Throws:
InterruptedException- if this thread is interrupted while waiting for the completion of the connector
-
runWithTask
public void runWithTask(Consumer<org.apache.kafka.connect.source.SourceTask> consumer)
-
-