Skip to content

Commit cd52c1e

Browse files
authored
sync_diff_inspector: add auto-position of tidb_snapshot (#664)
close #663
1 parent 33e7ff8 commit cd52c1e

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

sync_diff_inspector/config/config.go

+13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"net/url"
2222
"os"
2323
"path/filepath"
24+
"strings"
2425
"syscall"
2526
"time"
2627

@@ -97,6 +98,18 @@ type DataSource struct {
9798
// SourceType string `toml:"source-type" json:"source-type"`
9899
}
99100

101+
// IsAutoSnapshot returns true if the tidb_snapshot is expected to automatically
102+
// be set from the syncpoint from the target TiDB instance.
103+
func (d *DataSource) IsAutoSnapshot() bool {
104+
return strings.EqualFold(d.Snapshot, "auto")
105+
}
106+
107+
// SetSnapshot changes the snapshot in configuration. This is typically
108+
// used with the auto-snapshot feature.
109+
func (d *DataSource) SetSnapshot(newSnapshot string) {
110+
d.Snapshot = newSnapshot
111+
}
112+
100113
func (d *DataSource) ToDBConfig() *dbutil.DBConfig {
101114
return &dbutil.DBConfig{
102115
Host: d.Host,

sync_diff_inspector/config/config.toml

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ check-struct-only = false
3030
# remove comment if use tidb's snapshot data
3131
# snapshot = "2016-10-08 16:45:26"
3232
# snapshot = "386902609362944000"
33+
# When using TiCDC syncpoint source and target can be set to auto
34+
# snapshot = "auto"
3335

3436
######################### Task config #########################
3537
# Required

sync_diff_inspector/source/source.go

+40-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ const (
4545
const UnifiedTimeZone string = "+0:00"
4646

4747
const (
48-
ShieldDBName = "_no__exists__db_"
49-
ShieldTableName = "_no__exists__table_"
48+
ShieldDBName = "_no__exists__db_"
49+
ShieldTableName = "_no__exists__table_"
50+
GetSyncPointQuery = "SELECT primary_ts, secondary_ts FROM tidb_cdc.syncpoint_v1 ORDER BY primary_ts DESC LIMIT 1"
5051
)
5152

5253
type ChecksumInfo struct {
@@ -226,11 +227,43 @@ func buildSourceFromCfg(ctx context.Context, tableDiffs []*common.TableDiff, che
226227
return NewMySQLSources(ctx, tableDiffs, dbs, checkThreadCount, f)
227228
}
228229

230+
func getAutoSnapshotPosition(dbConfig *dbutil.DBConfig, vars map[string]string) (string, string, error) {
231+
tmpConn, err := dbutil.OpenDB(*dbConfig, vars)
232+
if err != nil {
233+
return "", "", errors.Annotatef(err, "connecting to auto-position tidb_snapshot failed")
234+
}
235+
defer tmpConn.Close()
236+
var primaryTs, secondaryTs string
237+
err = tmpConn.QueryRow(GetSyncPointQuery).Scan(&primaryTs, &secondaryTs)
238+
if err != nil {
239+
return "", "", errors.Annotatef(err, "fetching auto-position tidb_snapshot failed")
240+
}
241+
return primaryTs, secondaryTs, nil
242+
}
243+
229244
func initDBConn(ctx context.Context, cfg *config.Config) error {
230245
// Unified time zone
231246
vars := map[string]string{
232247
"time_zone": UnifiedTimeZone,
233248
}
249+
// Fill in tidb_snapshot if it is set to AUTO
250+
// This is only supported when set to auto on both target/source.
251+
if cfg.Task.TargetInstance.IsAutoSnapshot() {
252+
if len(cfg.Task.SourceInstances) > 1 {
253+
return errors.Errorf("'auto' snapshot only supports one tidb source")
254+
}
255+
if !cfg.Task.SourceInstances[0].IsAutoSnapshot() {
256+
return errors.Errorf("'auto' snapshot should be set on both target and source")
257+
}
258+
tmpDBConfig := cfg.Task.TargetInstance.ToDBConfig()
259+
tmpDBConfig.Snapshot = ""
260+
primaryTs, secondaryTs, err := getAutoSnapshotPosition(tmpDBConfig, vars)
261+
if err != nil {
262+
return err
263+
}
264+
cfg.Task.TargetInstance.SetSnapshot(secondaryTs)
265+
cfg.Task.SourceInstances[0].SetSnapshot(primaryTs)
266+
}
234267
// we had 3 producers and `cfg.CheckThreadCount` consumer to use db connections.
235268
// so the connection count need to be cfg.CheckThreadCount + 3.
236269
targetConn, err := common.CreateDB(ctx, cfg.Task.TargetInstance.ToDBConfig(), vars, cfg.CheckThreadCount+3)
@@ -241,6 +274,11 @@ func initDBConn(ctx context.Context, cfg *config.Config) error {
241274
cfg.Task.TargetInstance.Conn = targetConn
242275

243276
for _, source := range cfg.Task.SourceInstances {
277+
// If it is still set to AUTO it means it was not set on the target.
278+
// We require it to be set to AUTO on both.
279+
if source.IsAutoSnapshot() {
280+
return errors.Errorf("'auto' snapshot should be set on both target and source")
281+
}
244282
// connect source db with target db time_zone
245283
conn, err := common.CreateDB(ctx, source.ToDBConfig(), vars, cfg.CheckThreadCount+1)
246284
if err != nil {

0 commit comments

Comments
 (0)