diff --git a/dm/syncer/syncer.go b/dm/syncer/syncer.go index b48638c2fe6..e7557c08d48 100644 --- a/dm/syncer/syncer.go +++ b/dm/syncer/syncer.go @@ -40,7 +40,6 @@ import ( "github.com/pingcap/tidb/util/filter" regexprrouter "github.com/pingcap/tidb/util/regexpr-router" router "github.com/pingcap/tidb/util/table-router" - "github.com/pingcap/tiflow/dm/pkg/gtid" clientv3 "go.etcd.io/etcd/client/v3" "go.uber.org/atomic" "go.uber.org/zap" @@ -54,6 +53,7 @@ import ( "github.com/pingcap/tiflow/dm/pkg/conn" tcontext "github.com/pingcap/tiflow/dm/pkg/context" fr "github.com/pingcap/tiflow/dm/pkg/func-rollback" + "github.com/pingcap/tiflow/dm/pkg/gtid" "github.com/pingcap/tiflow/dm/pkg/ha" "github.com/pingcap/tiflow/dm/pkg/log" parserpkg "github.com/pingcap/tiflow/dm/pkg/parser" @@ -185,7 +185,8 @@ type Syncer struct { // the status of this track may change over time. safeMode *sm.SafeMode - timezone *time.Location + upstreamTZ *time.Location + timezone *time.Location binlogSizeCount atomic.Int64 lastBinlogSizeCount atomic.Int64 @@ -342,6 +343,10 @@ func (s *Syncer) Init(ctx context.Context) (err error) { }() tctx := s.tctx.WithContext(ctx) + s.upstreamTZ, err = str2TimezoneOrFromDB(tctx, "", &s.cfg.From) + if err != nil { + return + } s.timezone, err = str2TimezoneOrFromDB(tctx, s.cfg.Timezone, &s.cfg.To) if err != nil { return @@ -4077,9 +4082,9 @@ func (s *Syncer) flushOptimisticTableInfos(tctx *tcontext.Context) { func (s *Syncer) setGlobalPointByTime(tctx *tcontext.Context, timeStr string) error { // we support two layout - t, err := time.ParseInLocation(config.StartTimeFormat, timeStr, s.timezone) + t, err := time.ParseInLocation(config.StartTimeFormat, timeStr, s.upstreamTZ) if err != nil { - t, err = time.ParseInLocation(config.StartTimeFormat2, timeStr, s.timezone) + t, err = time.ParseInLocation(config.StartTimeFormat2, timeStr, s.upstreamTZ) } if err != nil { return err diff --git a/dm/syncer/util.go b/dm/syncer/util.go index 025b6eee1db..74a2a51cd78 100644 --- a/dm/syncer/util.go +++ b/dm/syncer/util.go @@ -138,7 +138,8 @@ func str2TimezoneOrFromDB(tctx *tcontext.Context, tzStr string, dbCfg *config.DB if err != nil { return nil, err } - tctx.L().Info("use timezone", zap.String("location", loc.String())) + tctx.L().Info("use timezone", zap.String("location", loc.String()), + zap.String("host", dbCfg.Host), zap.Int("port", dbCfg.Port)) return loc, nil } diff --git a/dm/tests/openapi/run.sh b/dm/tests/openapi/run.sh index 4f25c33c8c2..322f78efd57 100644 --- a/dm/tests/openapi/run.sh +++ b/dm/tests/openapi/run.sh @@ -715,7 +715,7 @@ function test_start_task_with_condition() { "query-status $task_name" \ "\"stage\": \"Stopped\"" 2 sleep 2 - start_time=$(date '+%Y-%m-%d %T') + start_time=$(TZ='UTC' date '+%Y-%m-%d %T') # mysql 3306 and 3307 is in UTC sleep 2 duration="" is_success="success" @@ -748,7 +748,7 @@ function test_start_task_with_condition() { "query-status $task_name" \ "\"stage\": \"Stopped\"" 2 sleep 2 - start_time=$(date '+%Y-%m-%d %T') + start_time=$(TZ='UTC' date '+%Y-%m-%d %T') # mysql 3306 and 3307 is in UTC sleep 2 duration="" is_success="success" @@ -780,7 +780,7 @@ function test_start_task_with_condition() { run_sql_source1 "CREATE TABLE openapi.t3(i TINYINT, j INT UNIQUE KEY);" run_sql_source2 "CREATE TABLE openapi.t4(i TINYINT, j INT UNIQUE KEY);" sleep 2 - start_time=$(date '+%Y-%m-%d %T') + start_time=$(TZ='UTC' date '+%Y-%m-%d %T') # mysql 3306 and 3307 is in UTC sleep 2 duration="" is_success="success" @@ -822,7 +822,7 @@ function test_start_task_with_condition() { run_sql_source2 "CREATE TABLE openapi.t2(i TINYINT, j INT UNIQUE KEY);" sleep 2 - start_time=$(date '+%Y-%m-%d %T') + start_time=$(TZ='UTC' date '+%Y-%m-%d %T') # mysql 3306 and 3307 is in UTC sleep 2 duration="" is_success="success" diff --git a/dm/tests/start_task/run.sh b/dm/tests/start_task/run.sh index adb44563510..c1983bd221d 100644 --- a/dm/tests/start_task/run.sh +++ b/dm/tests/start_task/run.sh @@ -62,7 +62,20 @@ function init_tracker_test() { cleanup_data start_task } +function restore_timezone() { + run_sql_source1 "set global time_zone = SYSTEM" + run_sql_tidb "set global time_zone = SYSTEM" +} + function start_task_by_time() { + run_sql_source1 "set global time_zone = '+02:00'" + run_sql_source1 "SELECT cast(TIMEDIFF(NOW(6), UTC_TIMESTAMP(6)) as time) time" + check_contains "time: 02:00:00" + run_sql_tidb "set global time_zone = '+06:00'" + run_sql_tidb "SELECT cast(TIMEDIFF(NOW(6), UTC_TIMESTAMP(6)) as time) time" + check_contains "time: 06:00:00" + trap restore_timezone EXIT + export GO_FAILPOINTS='github.com/pingcap/tiflow/dm/syncer/SafeModeInitPhaseSeconds=return(0)' run_dm_master $WORK_DIR/master $MASTER_PORT $cur/conf/dm-master.toml check_rpc_alive $cur/../bin/check_master_online 127.0.0.1:$MASTER_PORT @@ -75,7 +88,7 @@ function start_task_by_time() { run_sql_source1 'CREATE TABLE start_task.t1 (c INT PRIMARY KEY);' sleep 2 - start_time=$(date '+%Y-%m-%d %T') # 2022-01-26 17:32:22 + start_time=$(TZ='UTC-2' date '+%Y-%m-%d %T') # TZ=UTC-2 means +02:00 sleep 2 run_sql_source1 'CREATE TABLE start_task.t2 (c INT PRIMARY KEY);'