This repository contains a demo to verify data synchronization between two TiDB clusters using TiCDC with SyncPoint enabled.
This demo illustrates how to use TiCDC's SyncPoint feature to verify data synchronization between a primary and secondary TiDB cluster. The demo is based on the TiCDC Upstream and Downstream Check documentation.
The demo assumes two new VM instances with the following IPs:
- Primary Cluster (Cluster 1): 10.148.0.5
- Secondary Cluster (Cluster 2): 10.148.0.6
├── README.md # This file
├── check_sync.sh # Original script to check synchronization status
├── check_sync_new.sh # New script to check existing record sync status
├── config.toml # TiCDC SyncPoint configuration
├── setup_tidb_clusters.sh # Script to set up TiDB clusters and TiCDC
├── tidb-cluster1/ # Primary cluster configuration
│ └── topology.yaml
└── tidb-cluster2/ # Secondary cluster configuration
└── topology.yaml
The setup_tidb_clusters.sh script performs the following:
- Downloads and installs TiUP (TiDB package manager)
- Deploys two TiDB clusters using the topology files
- Starts both clusters
- Sets up a TiCDC changefeed for replication with SyncPoint enabled
The configuration in config.toml enables SyncPoint with:
- 30-second synchronization interval
- 1-hour data retention policy
Before running the check scripts, you need to create a demo table in the primary cluster:
CREATE TABLE test.t (i INT PRIMARY KEY AUTO_INCREMENT);There are two scripts available for checking synchronization:
The original script demonstrates SyncPoint functionality by:
- Inserting a new row into the primary cluster
- Getting the transaction timestamp and last insert ID
- Monitoring the SyncPoint timestamps from the secondary cluster
- Verifying when the data has been successfully synchronized
- Checking if the inserted row is available in the secondary cluster
The new script provides a more flexible way to check synchronization by:
- Taking a record ID as input
- Retrieving the record's commit timestamp from MVCC info
- Monitoring the SyncPoint timestamps from the secondary cluster
- Verifying when the data has been successfully synchronized
- Checking if the specified record is available in the secondary cluster
The check_sync_new.sh script can handle different types of tables:
-
Single Column Primary Key (Default case):
CREATE TABLE t (i INT PRIMARY KEY AUTO_INCREMENT);
Usage:
./check_sync_new.sh 1 -
Clustered Primary Key:
CREATE TABLE t1 (i varchar(20), j varchar(20), primary key (i, j));
The script can be modified to use multiple arguments for the primary key:
TIDB_ENCODE_RECORD_KEY('test', 't1', 'a', 'b')
-
Table Without Primary Key:
CREATE TABLE t2 (i int);
First, get the table row ID:
SELECT _tidb_rowid, i FROM t2;
Then use the
_tidb_rowidvalue as input to the script.
-
Make the scripts executable:
chmod +x setup_tidb_clusters.sh check_sync.sh check_sync_new.sh -
Run the setup script to deploy the TiDB clusters and configure replication:
./setup_tidb_clusters.sh -
Connect to the primary cluster and create the test table:
mysql -h 10.148.0.5 -P 4000 -u rootCREATE DATABASE IF NOT EXISTS test; CREATE TABLE test.t (i INT PRIMARY KEY AUTO_INCREMENT);
-
Run either check script to verify synchronization:
Using the original script:
./check_sync.shUsing the new script (specify a record ID):
./check_sync_new.sh 1
When running the check scripts, you'll see output similar to:
For check_sync.sh:
Initial commit_ts: 2025-03-28 12:34:56.789
Last insert id: 1
Current primary_ts: 2025-03-28 12:34:57.123, secondary_ts: 2025-03-28 12:34:56.456
In Sync
Current primary_ts: 2025-03-28 12:34:59.789, secondary_ts: 2025-03-28 12:34:58.123
Synced
Last inserted ID 1 has synced.
For check_sync_new.sh:
Checking sync status for record ID: 1
Commit timestamp: 2025-03-28 12:34:56.789
Current primary_ts: 2025-03-28 12:34:57.123, secondary_ts: 2025-03-28 12:34:56.456
In Sync
Current primary_ts: 2025-03-28 12:34:59.789, secondary_ts: 2025-03-28 12:34:58.123
Synced
Record ID 1 has synced.
Possible error messages:
- If the record doesn't exist: "Error: Record ID does not exist in the primary cluster"
- If no syncpoint is found: "No syncpoint found yet. Waiting..."
- Ensure you have MySQL client installed on your machine
- The script connects to the TiDB clusters using the default root user without password
- The demo utilizes TiCDC's SyncPoint feature which was introduced in TiDB v6.1.0