Skip to content

[FLINK-40397][mysql] Add currentBinlogPositionLag metric for MySQL binlog reader - #4509

Open
hadoopkandy wants to merge 1 commit into
apache:masterfrom
hadoopkandy:FLINK-40397
Open

[FLINK-40397][mysql] Add currentBinlogPositionLag metric for MySQL binlog reader#4509
hadoopkandy wants to merge 1 commit into
apache:masterfrom
hadoopkandy:FLINK-40397

Conversation

@hadoopkandy

Copy link
Copy Markdown

What is the purpose of the change

This PR adds a new metric currentBinlogPositionLag for the MySQL binlog reader, which measures the lag between the current consumed binlog offset and the latest master
binlog offset.

Unlike the existing currentFetchEventTimeLag metric (which only updates when there are events flowing), this metric is meaningful even during idle periods — it reflects how
far behind the reader is from the MySQL master at the position level.

Brief change log

  • Added BinlogLagCalculator to compute binlog position lag, supporting both GTID mode and file-position mode.
  • In BinlogSplitReader, periodically (every 10s) fetch the master's current binlog offset via SHOW MASTER STATUS and store it in a shared AtomicReference<BinlogOffset>.
  • In MySqlRecordEmitter, periodically (every 10s) read the shared master offset and calculate the lag against the current consumed offset, then report it via
    MySqlSourceReaderMetrics.
  • Registered the new gauge metric currentBinlogPositionLag in MySqlSourceReaderMetrics.

Lag calculation strategy

Mode Lag meaning Calculation
GTID Transaction count difference Sum of (master max txn ID - current max txn ID) per server UUID
File-position (same file) Byte offset difference master position - current position
File-position (cross file) Estimated byte difference file sequence diff × 1,000,000 + master position

Verifying this change

This change added tests:

  • BinlogLagCalculatorTest: unit tests covering GTID mode (single/multiple UUIDs, disjoint intervals, starts-from-middle), file-position mode (same file, cross file, edge
    cases).
  • MySqlRecordEmitterTest#testBinlogPositionLagMetricIsUpdated: verifies that the lag metric is updated correctly during record emission.

Does this pull request potentially affect one of the following parts

  • Dependencies: no
  • The public API: no
  • The serializers: no
  • The runtime per-record code path: yes (lightweight gauge update during binlog phase only)
  • Anything that affects determine of shard: no

Documentation

  • Does this pull request introduce a new feature? yes — a new monitoring metric
  • If yes, how is the feature documented? JavaDoc on the metric constant; needs user-facing docs update for the metric name and semantics.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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> from BinlogSplitReader (periodic master offset fetch) to MySqlRecordEmitter (periodic lag reporting).
  • Registered a new gauge metric in MySqlSourceReaderMetrics and 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.

Comment on lines +173 to +178
lastReportBinlogLagTime = now;
BinlogOffset currentOffset = splitState.asBinlogSplitState().getStartingOffset();
BinlogOffset masterOffset = latestMasterOffset.get();
if (currentOffset == null || masterOffset == null) {
return;
}
Comment on lines +245 to +247
lastFetchMasterStatusTime = now;
} catch (Exception e) {
LOG.warn("Failed to fetch master binlog offset for lag metric", e);
// Rough estimate: actual file size is unknown, so we use 1,000,000 as a
// synthetic weight per file gap to produce a monotonically increasing lag
// value that indicates cross-file distance. This is NOT actual byte lag.
return (masterSeq - currentSeq) * 1_000_000L + master.getPosition();
Comment on lines +115 to +119
Method reportMethod =
MySqlRecordEmitter.class.getDeclaredMethod(
"reportBinlogLag", MySqlSplitState.class);
reportMethod.setAccessible(true);
reportMethod.invoke(emitter, splitState);
/** A collection class for handling metrics in {@link MySqlSourceReader}. */
public class MySqlSourceReaderMetrics {

public static final String CURRENT_BINLOG_POSITION_LAG = "currentBinlogPositionLag";
Comment on lines 51 to +53
metricGroup.gauge(
MetricNames.CURRENT_FETCH_EVENT_TIME_LAG, (Gauge<Long>) this::getFetchDelay);
metricGroup.gauge(CURRENT_BINLOG_POSITION_LAG, (Gauge<Long>) this::getBinlogPositionLag);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants