Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dm: parse start-time using upstream timezone #5476

Merged
merged 7 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions dm/syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion dm/syncer/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions dm/tests/openapi/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
15 changes: 14 additions & 1 deletion dm/tests/start_task/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);'
Expand Down