Class SqlServerStreamingChangeEventSource
- All Implemented Interfaces:
ChangeEventSource,StreamingChangeEventSource<SqlServerPartition,SqlServerOffsetContext>
A StreamingChangeEventSource based on SQL Server change data capture functionality.
A main loop polls database DDL change and change data tables and turns them into change events.
The connector uses CDC functionality of SQL Server that is implemented as as a process that monitors source table and write changes from the table into the change table.
The main loop keeps a pointer to the LSN of changes that were already processed. It queries all change tables and get result set of changes. It always finds the smallest LSN across all tables and the change is converted into the event message and sent downstream. The process repeats until all result sets are empty. The LSN is marked and the procedure repeats.
The schema changes detection follows the procedure recommended by SQL Server CDC documentation. The database operator should create one more capture process (and table) when a table schema is updated. The code detects presence of two change tables for a single source table. It decides which table is the new one depending on LSNs stored in them. The loop streams changes from the older table till there are events in new table with the LSN larger than in the old one. Then the change table is switched and streaming is executed from the new one.
- Author:
- Jiri Pechanec
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.debezium.pipeline.source.spi.ChangeEventSource
ChangeEventSource.ChangeEventSourceContext -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Map<SqlServerPartition,Set<SqlServerChangeTable>> private booleanprivate final Clockprivate final SqlServerConnectorConfigprivate final SqlServerConnectionConnection used for reading CDC tables.private static final Durationprivate final EventDispatcher<SqlServerPartition,TableId> private SqlServerOffsetContextprivate final ErrorHandlerprivate static final intprivate static final org.slf4j.Loggerprivate final SqlServerConnectionA separate connection for retrieving details of the schema changes; without it, adaptive buffering will not work.private static final Patternprivate final NotificationService<SqlServerPartition,SqlServerOffsetContext> private final ElapsedTimeStrategyprivate final Durationprivate final SqlServerDatabaseSchemaprivate final Map<SqlServerPartition,SqlServerStreamingExecutionContext> -
Constructor Summary
ConstructorsConstructorDescriptionSqlServerStreamingChangeEventSource(SqlServerConnectorConfig connectorConfig, SqlServerConnection dataConnection, SqlServerConnection metadataConnection, EventDispatcher<SqlServerPartition, TableId> dispatcher, ErrorHandler errorHandler, Clock clock, SqlServerDatabaseSchema schema, NotificationService<SqlServerPartition, SqlServerOffsetContext> notificationService) -
Method Summary
Modifier and TypeMethodDescriptionprivate voidcollectChangeTablesWithKnownStopLsn(SqlServerPartition partition, SqlServerChangeTable[] tables) voidcommitOffset(Map<String, ?> sourcePartition, Map<String, ?> offset) private voidvoidexecute(ChangeEventSource.ChangeEventSourceContext context, SqlServerPartition partition, SqlServerOffsetContext offsetContext) booleanexecuteIteration(ChangeEventSource.ChangeEventSourceContext context, SqlServerPartition partition, SqlServerOffsetContext offsetContext) private SqlServerChangeTable[]getChangeTablesToQuery(SqlServerPartition partition, SqlServerOffsetContext offsetContext, Lsn toLsn) private LsngetToLsn(SqlServerConnection connection, String databaseName, TxLogPosition lastProcessedPosition, int maxTransactionsPerIteration) voidinit(SqlServerOffsetContext offsetContext) private voidmigrateTable(SqlServerPartition partition, Queue<SqlServerChangeTable> schemaChangeCheckpoints, SqlServerOffsetContext offsetContext) private SqlServerChangeTable[]processErrorFromChangeTableQuery(String databaseName, SQLException exception, SqlServerChangeTable[] currentChangeTables)
-
Field Details
-
MISSING_CDC_FUNCTION_CHANGES_ERROR
-
LOGGER
private static final org.slf4j.Logger LOGGER -
DEFAULT_INTERVAL_BETWEEN_COMMITS
-
INTERVAL_BETWEEN_COMMITS_BASED_ON_POLL_FACTOR
private static final int INTERVAL_BETWEEN_COMMITS_BASED_ON_POLL_FACTOR- See Also:
-
dataConnection
Connection used for reading CDC tables. -
metadataConnection
A separate connection for retrieving details of the schema changes; without it, adaptive buffering will not work. -
dispatcher
-
errorHandler
-
clock
-
schema
-
pollInterval
-
connectorConfig
-
pauseBetweenCommits
-
streamingExecutionContexts
-
changeTablesWithKnownStopLsn
-
checkAgent
private boolean checkAgent -
effectiveOffset
-
notificationService
-
-
Constructor Details
-
SqlServerStreamingChangeEventSource
public SqlServerStreamingChangeEventSource(SqlServerConnectorConfig connectorConfig, SqlServerConnection dataConnection, SqlServerConnection metadataConnection, EventDispatcher<SqlServerPartition, TableId> dispatcher, ErrorHandler errorHandler, Clock clock, SqlServerDatabaseSchema schema, NotificationService<SqlServerPartition, SqlServerOffsetContext> notificationService)
-
-
Method Details
-
init
- Specified by:
initin interfaceStreamingChangeEventSource<SqlServerPartition,SqlServerOffsetContext> - Throws:
InterruptedException
-
execute
public void execute(ChangeEventSource.ChangeEventSourceContext context, SqlServerPartition partition, SqlServerOffsetContext offsetContext) throws InterruptedException - Specified by:
executein interfaceStreamingChangeEventSource<SqlServerPartition,SqlServerOffsetContext> - Throws:
InterruptedException
-
executeIteration
public boolean executeIteration(ChangeEventSource.ChangeEventSourceContext context, SqlServerPartition partition, SqlServerOffsetContext offsetContext) - Specified by:
executeIterationin interfaceStreamingChangeEventSource<SqlServerPartition,SqlServerOffsetContext>
-
getOffsetContext
- Specified by:
getOffsetContextin interfaceStreamingChangeEventSource<SqlServerPartition,SqlServerOffsetContext>
-
collectChangeTablesWithKnownStopLsn
private void collectChangeTablesWithKnownStopLsn(SqlServerPartition partition, SqlServerChangeTable[] tables) -
commitTransaction
- Throws:
SQLException
-
migrateTable
private void migrateTable(SqlServerPartition partition, Queue<SqlServerChangeTable> schemaChangeCheckpoints, SqlServerOffsetContext offsetContext) throws InterruptedException, SQLException - Throws:
InterruptedExceptionSQLException
-
processErrorFromChangeTableQuery
private SqlServerChangeTable[] processErrorFromChangeTableQuery(String databaseName, SQLException exception, SqlServerChangeTable[] currentChangeTables) throws Exception - Throws:
Exception
-
getChangeTablesToQuery
private SqlServerChangeTable[] getChangeTablesToQuery(SqlServerPartition partition, SqlServerOffsetContext offsetContext, Lsn toLsn) throws SQLException, InterruptedException - Throws:
SQLExceptionInterruptedException
-
getToLsn
private Lsn getToLsn(SqlServerConnection connection, String databaseName, TxLogPosition lastProcessedPosition, int maxTransactionsPerIteration) throws SQLException - Returns:
- the log sequence number up until which the connector should query changes from the database.
- Throws:
SQLException
-
commitOffset
- Specified by:
commitOffsetin interfaceStreamingChangeEventSource<SqlServerPartition,SqlServerOffsetContext>
-