wangkang created FLINK-40397:
--------------------------------

             Summary: Add currentBinlogPositionLag metric for MySQL binlog 
reader 
                 Key: FLINK-40397
                 URL: https://issues.apache.org/jira/browse/FLINK-40397
             Project: Flink
          Issue Type: Improvement
          Components: Flink CDC
            Reporter: wangkang


## Problem                                                                      
                                                                                
              
   
  The existing `currentFetchEventTimeLag` metric (FetchTime - messageTimestamp) 
has a fundamental flaw:                                                         
                
  it only updates when data flows through the reader.                           
                                                                                
             
                                                                                
                                                                                
                
  For example, if the reader consumed a record from 1:00 AM at 10:00 AM, the 
metric shows 9 hours of                                                         
                   
  lag. Even after the reader has fully caught up (no new binlog events on 
master), the metric remains                                                     
                      
  frozen at 9 hours until the next record arrives.                              
                                                                                
                
                                                                                
                                                                                
                
  This causes:                                                                  
                                                                                
                
  - False alerts: reader has caught up but metric still shows high lag          
                                                                                
                
  - Cannot distinguish "caught up" from "still catching up"                     
                                                                                
                
                                                                                
                                                                                
                
  ## Proposal                                                                   
                                                                                
                
                                                                                
                                                                                
                
  Add a new gauge metric `currentBinlogPositionLag` that reflects the binlog 
position gap between the                                                        
                   
  reader's current consumed offset and the MySQL master's latest offset.
                                                                                
                                                                                
                
  Semantics:                                                                    
                                                                                
                
  - Value = 0: fully caught up                                                  
                                                                                
                
  - Value > 0: unconsumed binlog data exists                                    
                                                                                
                
  - Value = -1 (UNDEFINED): still in snapshot phase                             
                                                                                
                
                                                                                
                                                                                
                
  ### GTID Mode                                                                 
                                                                                
                
  Sum of (master_max_txn_id - current_max_txn_id) for each server UUID.         
                                                                                
                
                                                                                
                                                                                
                
  Note: GtidSet.subtract() cannot be used because CDC may resume from a 
checkpoint midpoint                                                             
                        
  (e.g., uuid:1774595494-1775564172), causing subtract to incorrectly count 
historical                                                                      
                    
  transactions as lag.                                                          
                                                                                
                
                                                                                
                                                                                
                
  ### Non-GTID Mode                                                             
                                                                                
                
  - Same file: master.position - current.position (byte difference)             
                                                                                
                
  - Cross-file: (file_sequence_gap) × scale_factor + master.position (synthetic 
weight                                                                          
                
    indicating severity — not actual byte count)                                
                                                                                
                
                                                                                
                                                                                
                
  ### Design                                                                    
                                                                                
                
                                                                                
                                                                                
                
  - Reuse existing JDBC connection in BinlogSplitReader to query SHOW MASTER 
STATUS every ~10s                                                               
                   
  - Share master offset to RecordEmitter via AtomicReference (zero extra 
threads/connections)
  - RecordEmitter calculates and reports lag on data change records and 
heartbeat events                                                                
                        
  - No impact on data processing path                                           
                                                                                
                
                                                                                
                                                                                
                
  ## Compatibility                                                              
                                                                                
                
                                                                                
                                                                                
                
  - No breaking API changes to public interfaces                                
                                                                                
                
  - New metric is purely additive              
  - Works with both GTID and non-GTID MySQL configurations                      
                                                                                
                
  - Compatible with MySQL 5.7, 8.0, and 8.4+            



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to