[FLINK-40397][mysql] Add binlog position lag metrics for MySQL binlog reader - #4509
[FLINK-40397][mysql] Add binlog position lag metrics for MySQL binlog reader#4509hadoopkandy wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Adds a new MySQL source metric (currentBinlogPositionLag) to report position-level lag between consumed binlog offset and the master’s latest offset, including during idle periods.
Changes:
- Introduced
BinlogLagCalculator(GTID + file/pos modes) and added unit tests. - Wired a shared
AtomicReference<BinlogOffset>fromBinlogSplitReader(periodic master offset fetch) toMySqlRecordEmitter(periodic lag reporting). - Registered a new gauge metric in
MySqlSourceReaderMetricsand updated reader/emitter constructors and tests accordingly.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mysql-cdc/src/test/java/org/apache/flink/cdc/connectors/mysql/source/utils/BinlogLagCalculatorTest.java | Adds unit coverage for GTID and file-position lag calculations. |
| flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mysql-cdc/src/test/java/org/apache/flink/cdc/connectors/mysql/source/reader/MySqlSourceReaderTest.java | Updates test helpers to pass shared master-offset reference through constructors. |
| flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mysql-cdc/src/test/java/org/apache/flink/cdc/connectors/mysql/source/reader/MySqlRecordEmitterTest.java | Adds a test for the new lag metric and updates emitter construction. |
| flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mysql-cdc/src/test/java/org/apache/flink/cdc/connectors/mysql/debezium/reader/BinlogSplitReaderTest.java | Updates binlog reader tests to pass the shared master-offset reference. |
| flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mysql-cdc/src/main/java/org/apache/flink/cdc/connectors/mysql/source/utils/BinlogLagCalculator.java | Implements lag computation for GTID and file-position modes. |
| flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mysql-cdc/src/main/java/org/apache/flink/cdc/connectors/mysql/source/reader/MySqlSplitReader.java | Threads the shared master-offset reference into BinlogSplitReader. |
| flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mysql-cdc/src/main/java/org/apache/flink/cdc/connectors/mysql/source/reader/MySqlRecordEmitter.java | Periodically computes and records the new lag metric during binlog phase. |
| flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mysql-cdc/src/main/java/org/apache/flink/cdc/connectors/mysql/source/metrics/MySqlSourceReaderMetrics.java | Registers currentBinlogPositionLag gauge and stores the latest reported value. |
| flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mysql-cdc/src/main/java/org/apache/flink/cdc/connectors/mysql/source/MySqlSource.java | Wires shared master-offset reference between split reader and record emitter. |
| flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mysql-cdc/src/main/java/org/apache/flink/cdc/connectors/mysql/debezium/reader/BinlogSplitReader.java | Periodically fetches master binlog offset and exposes it via shared reference. |
| flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-mysql/src/main/java/org/apache/flink/cdc/connectors/mysql/source/reader/MySqlPipelineRecordEmitter.java | Updates pipeline emitter constructor to pass shared master-offset reference. |
| flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-mysql/src/main/java/org/apache/flink/cdc/connectors/mysql/source/MySqlDataSource.java | Updates record emitter supplier wiring for the new constructor signature. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Could we first clarify the unit and semantics of Currently, the same gauge represents different quantities:
As a result, the metric may switch units at runtime and cannot be interpreted consistently or used with a stable alert threshold. The cross-file value also has no physical unit. I think we should define whether this metric represents transaction count, byte lag, or an abstract position distance before finalizing the calculation. If both transaction and byte lag are useful, exposing separate metrics may be clearer. |
|
And Could we make this metric opt-in through a source configuration instead of enabling it by default? This is not a purely local metric. It executes I suggest disabling this feature by default and providing a configuration option to enable it explicitly. When disabled, the connector should skip both metric registration and the periodic master-status query. |
f76476b to
db92924
Compare
Great feedback. Addressed both concerns:
This avoids the ambiguity of mixing different units in one metric and gives users clear, actionable signals. |
db92924 to
d7f94d1
Compare
d7f94d1 to
0d66a58
Compare
What is the purpose of the change
This PR adds binlog position lag metrics for the MySQL binlog reader, which measure the lag between the current consumed binlog offset and the latest master binlog offset.
Unlike the existing
currentFetchEventTimeLagmetric (which only updates when there are events flowing), these metrics are meaningful even during idle periods — they reflect how far behind the reader is from the MySQL masterat the position level.
The feature is opt-in via
scan.binlog.position-lag.interval.ms. A value of-1(default) disables the feature entirely. When set to a positive value, the connector periodically executesSHOW MASTER STATUSat thisinterval to calculate lag.
Brief change log
BinlogLagCalculatorto compute binlog position lag, producing two independent results: transaction lag (GTID-based) and byte position lag (file+position based).BinlogSplitReader, conditionally fetch the master's current binlog offset viaSHOW MASTER STATUSat the configured interval and store it in a sharedAtomicReference<BinlogOffset>.MySqlRecordEmitter, read the shared master offset and calculate lag against the current consumed offset, then report it viaMySqlSourceReaderMetrics.MySqlSourceReaderMetrics:currentBinlogTransactionLag: GTID-based transaction count lag (-1 when GTID is unavailable)currentBinlogBytePositionLag: byte-level position lag (-1 when position info is unavailable)scan.binlog.position-lag.interval.ms(default-1, disabled) to control the feature and polling frequency.Lag calculation strategy
currentBinlogTransactionLagcurrentBinlogBytePositionLagcurrentBinlogBytePositionLagVerifying this change
This change added tests:
BinlogLagCalculatorTest: unit tests covering GTID mode (single/multiple UUIDs, disjoint intervals, starts-from-middle, same GTID fallback to position), file-position mode (same file, cross file, edge cases).MySqlRecordEmitterTest#testBinlogPositionLagMetricIsUpdated: verifies that the byte position lag metric is updated correctly during record emission through the public API path.Does this pull request potentially affect one of the following parts
binlogPositionLagIntervalMsand config option)Documentation
mysql-cdc.md).