Skip to content

Commit

Permalink
log-backup: check timezone when using datatime format ts string (#35811)
Browse files Browse the repository at this point in the history
ref #31975
  • Loading branch information
3pointer authored Jun 29, 2022
1 parent 4538cf5 commit 5eac3b6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
10 changes: 8 additions & 2 deletions br/pkg/task/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (cfg *BackupConfig) ParseFromFlags(flags *pflag.FlagSet) error {
if err != nil {
return errors.Trace(err)
}
cfg.BackupTS, err = ParseTSString(backupTS)
cfg.BackupTS, err = ParseTSString(backupTS, false)
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -528,7 +528,7 @@ func RunBackup(c context.Context, g glue.Glue, cmdName string, cfg *BackupConfig
}

// ParseTSString port from tidb setSnapshotTS.
func ParseTSString(ts string) (uint64, error) {
func ParseTSString(ts string, tzCheck bool) (uint64, error) {
if len(ts) == 0 {
return 0, nil
}
Expand All @@ -540,6 +540,12 @@ func ParseTSString(ts string) (uint64, error) {
sc := &stmtctx.StatementContext{
TimeZone: loc,
}
if tzCheck {
tzIdx, _, _, _, _ := types.GetTimezone(ts)
if tzIdx < 0 {
return 0, errors.Errorf("must set timezone when using datetime format ts")
}
}
t, err := types.ParseTime(sc, ts, mysql.TypeTimestamp, types.MaxFsp)
if err != nil {
return 0, errors.Trace(err)
Expand Down
27 changes: 23 additions & 4 deletions br/pkg/task/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,40 @@ func TestParseTSString(t *testing.T) {
err error
)

ts, err = ParseTSString("")
ts, err = ParseTSString("", false)
require.NoError(t, err)
require.Zero(t, ts)

ts, err = ParseTSString("400036290571534337")
ts, err = ParseTSString("400036290571534337", false)
require.NoError(t, err)
require.Equal(t, uint64(400036290571534337), ts)

ts, err = ParseTSString("2021-01-01 01:42:23")
ts, err = ParseTSString("2021-01-01 01:42:23", false)
require.NoError(t, err)
localTime := time.Date(2021, time.Month(1), 1, 1, 42, 23, 0, time.Local)

localTimestamp := localTime.Unix()
localTSO := uint64((localTimestamp << 18) * 1000)
require.Equal(t, localTSO, ts)

_, err = ParseTSString("2021-01-01 01:42:23", true)
require.Error(t, err)
require.Regexp(t, "must set timezone*", err.Error())

ts, err = ParseTSString("2021-01-01 01:42:23+00:00", true)
require.NoError(t, err)
localTime = time.Date(2021, time.Month(1), 1, 1, 42, 23, 0, time.UTC)
localTimestamp = localTime.Unix()
localTSO = uint64((localTimestamp << 18) * 1000)
require.Equal(t, localTSO, ts)

ts, err = ParseTSString("2021-01-01 01:42:23+08:00", true)
require.NoError(t, err)
secondsEastOfUTC := int((8 * time.Hour).Seconds())
beijing := time.FixedZone("Beijing Time", secondsEastOfUTC)
localTime = time.Date(2021, time.Month(1), 1, 1, 42, 23, 0, beijing)
localTimestamp = localTime.Unix()
localTSO = uint64((localTimestamp << 18) * 1000)
require.Equal(t, localTSO, ts)
}

func TestParseCompressionType(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions br/pkg/task/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,14 @@ func (cfg *RestoreConfig) ParseStreamRestoreFlags(flags *pflag.FlagSet) error {
if err != nil {
return errors.Trace(err)
}
if cfg.StartTS, err = ParseTSString(tsString); err != nil {
if cfg.StartTS, err = ParseTSString(tsString, true); err != nil {
return errors.Trace(err)
}
tsString, err = flags.GetString(FlagStreamRestoreTS)
if err != nil {
return errors.Trace(err)
}
if cfg.RestoreTS, err = ParseTSString(tsString); err != nil {
if cfg.RestoreTS, err = ParseTSString(tsString, true); err != nil {
return errors.Trace(err)
}

Expand Down
6 changes: 3 additions & 3 deletions br/pkg/task/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (cfg *StreamConfig) ParseStreamTruncateFromFlags(flags *pflag.FlagSet) erro
if err != nil {
return errors.Trace(err)
}
if cfg.Until, err = ParseTSString(tsString); err != nil {
if cfg.Until, err = ParseTSString(tsString, true); err != nil {
return errors.Trace(err)
}
if cfg.SkipPrompt, err = flags.GetBool(flagYes); err != nil {
Expand All @@ -213,7 +213,7 @@ func (cfg *StreamConfig) ParseStreamStartFromFlags(flags *pflag.FlagSet) error {
return errors.Trace(err)
}

if cfg.StartTS, err = ParseTSString(tsString); err != nil {
if cfg.StartTS, err = ParseTSString(tsString, true); err != nil {
return errors.Trace(err)
}

Expand All @@ -222,7 +222,7 @@ func (cfg *StreamConfig) ParseStreamStartFromFlags(flags *pflag.FlagSet) error {
return errors.Trace(err)
}

if cfg.EndTS, err = ParseTSString(tsString); err != nil {
if cfg.EndTS, err = ParseTSString(tsString, true); err != nil {
return errors.Trace(err)
}

Expand Down

0 comments on commit 5eac3b6

Please sign in to comment.