Class SqlServerStreamingChangeEventSource
- java.lang.Object
-
- io.debezium.connector.sqlserver.SqlServerStreamingChangeEventSource
-
- All Implemented Interfaces:
ChangeEventSource,StreamingChangeEventSource<SqlServerOffsetContext>
public class SqlServerStreamingChangeEventSource extends Object implements StreamingChangeEventSource<SqlServerOffsetContext>
A
StreamingChangeEventSourcebased 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
Fields Modifier and Type Field Description private Clockclockprivate SqlServerConnectorConfigconnectorConfigprivate SqlServerConnectiondataConnectionConnection used for reading CDC tables.private static DurationDEFAULT_INTERVAL_BETWEEN_COMMITSprivate EventDispatcher<TableId>dispatcherprivate ErrorHandlererrorHandlerprivate static intINTERVAL_BETWEEN_COMMITS_BASED_ON_POLL_FACTORprivate static org.slf4j.LoggerLOGGERprivate SqlServerConnectionmetadataConnectionA separate connection for retrieving timestamps; without it, adaptive buffering will not work.private static PatternMISSING_CDC_FUNCTION_CHANGES_ERRORprivate ElapsedTimeStrategypauseBetweenCommitsprivate DurationpollIntervalprivate SqlServerDatabaseSchemaschema
-
Constructor Summary
Constructors Constructor Description SqlServerStreamingChangeEventSource(SqlServerConnectorConfig connectorConfig, SqlServerConnection dataConnection, SqlServerConnection metadataConnection, EventDispatcher<TableId> dispatcher, ErrorHandler errorHandler, Clock clock, SqlServerDatabaseSchema schema)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidcommitTransaction()voidexecute(ChangeEventSource.ChangeEventSourceContext context, SqlServerOffsetContext offsetContext)private SqlServerChangeTable[]getCdcTablesToQuery(SqlServerOffsetContext offsetContext)private LsngetToLsn(SqlServerConnection connection, TxLogPosition lastProcessedPosition, int maxTransactionsPerIteration)private voidmigrateTable(Queue<SqlServerChangeTable> schemaChangeCheckpoints, SqlServerOffsetContext offsetContext)private SqlServerChangeTable[]processErrorFromChangeTableQuery(SQLException exception, SqlServerChangeTable[] currentChangeTables)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.debezium.pipeline.source.spi.StreamingChangeEventSource
commitOffset
-
-
-
-
Field Detail
-
MISSING_CDC_FUNCTION_CHANGES_ERROR
private static final Pattern MISSING_CDC_FUNCTION_CHANGES_ERROR
-
LOGGER
private static final org.slf4j.Logger LOGGER
-
DEFAULT_INTERVAL_BETWEEN_COMMITS
private static final Duration DEFAULT_INTERVAL_BETWEEN_COMMITS
-
INTERVAL_BETWEEN_COMMITS_BASED_ON_POLL_FACTOR
private static final int INTERVAL_BETWEEN_COMMITS_BASED_ON_POLL_FACTOR
- See Also:
- Constant Field Values
-
dataConnection
private final SqlServerConnection dataConnection
Connection used for reading CDC tables.
-
metadataConnection
private final SqlServerConnection metadataConnection
A separate connection for retrieving timestamps; without it, adaptive buffering will not work.
-
dispatcher
private final EventDispatcher<TableId> dispatcher
-
errorHandler
private final ErrorHandler errorHandler
-
clock
private final Clock clock
-
schema
private final SqlServerDatabaseSchema schema
-
pollInterval
private final Duration pollInterval
-
connectorConfig
private final SqlServerConnectorConfig connectorConfig
-
pauseBetweenCommits
private final ElapsedTimeStrategy pauseBetweenCommits
-
-
Constructor Detail
-
SqlServerStreamingChangeEventSource
public SqlServerStreamingChangeEventSource(SqlServerConnectorConfig connectorConfig, SqlServerConnection dataConnection, SqlServerConnection metadataConnection, EventDispatcher<TableId> dispatcher, ErrorHandler errorHandler, Clock clock, SqlServerDatabaseSchema schema)
-
-
Method Detail
-
execute
public void execute(ChangeEventSource.ChangeEventSourceContext context, SqlServerOffsetContext offsetContext) throws InterruptedException
- Specified by:
executein interfaceStreamingChangeEventSource<SqlServerOffsetContext>- Throws:
InterruptedException
-
commitTransaction
private void commitTransaction() throws SQLException- Throws:
SQLException
-
migrateTable
private void migrateTable(Queue<SqlServerChangeTable> schemaChangeCheckpoints, SqlServerOffsetContext offsetContext) throws InterruptedException, SQLException
- Throws:
InterruptedExceptionSQLException
-
processErrorFromChangeTableQuery
private SqlServerChangeTable[] processErrorFromChangeTableQuery(SQLException exception, SqlServerChangeTable[] currentChangeTables) throws Exception
- Throws:
Exception
-
getCdcTablesToQuery
private SqlServerChangeTable[] getCdcTablesToQuery(SqlServerOffsetContext offsetContext) throws SQLException, InterruptedException
- Throws:
SQLExceptionInterruptedException
-
getToLsn
private Lsn getToLsn(SqlServerConnection connection, TxLogPosition lastProcessedPosition, int maxTransactionsPerIteration) throws SQLException
- Returns:
- the log sequence number up until which the connector should query changes from the database.
- Throws:
SQLException
-
-