From c75192304624576a02c2046c64506823f960b818 Mon Sep 17 00:00:00 2001 From: luancheng Date: Fri, 5 Feb 2021 16:38:41 +0800 Subject: [PATCH 1/3] use system_time_zone to encode kv if tidb set it --- lightning/backend/session.go | 20 +++++++++++++++++++- lightning/restore/tidb.go | 1 + 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lightning/backend/session.go b/lightning/backend/session.go index 3087229c0..b1e994a6d 100644 --- a/lightning/backend/session.go +++ b/lightning/backend/session.go @@ -18,12 +18,14 @@ import ( "errors" "fmt" "strconv" + "time" "github.com/pingcap/parser/model" "github.com/pingcap/parser/mysql" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/sessionctx" "github.com/pingcap/tidb/sessionctx/variable" + "github.com/pingcap/tidb/util/timeutil" "github.com/pingcap/tidb-lightning/lightning/common" ) @@ -194,12 +196,28 @@ func newSession(options *SessionOptions) *session { vars.StmtCtx.OverflowAsWarning = !sqlMode.HasStrictMode() vars.StmtCtx.AllowInvalidDate = sqlMode.HasAllowInvalidDatesMode() vars.StmtCtx.IgnoreZeroInDate = !sqlMode.HasStrictMode() || sqlMode.HasAllowInvalidDatesMode() + tz := vars.Location() if options.SysVars != nil { + var tidbTimeZone, tidbSystemTimeZone string for k, v := range options.SysVars { vars.SetSystemVar(k, v) + if k == "time_zone" { + tidbTimeZone = v + } + if k == "system_time_zone" { + tidbSystemTimeZone = v + } + } + // we get system zone from TiDB server, we can set it for lightning session. + if tidbTimeZone == "SYSTEM" && tidbSystemTimeZone != "" { + loc, err := timeutil.LoadLocation(tidbSystemTimeZone) + if err != nil { + loc = time.Local + } + tz = loc } } - vars.StmtCtx.TimeZone = vars.Location() + vars.StmtCtx.TimeZone = tz vars.SetSystemVar("timestamp", strconv.FormatInt(options.Timestamp, 10)) vars.TxnCtx = nil diff --git a/lightning/restore/tidb.go b/lightning/restore/tidb.go index 33a1773eb..f4a9d62f5 100644 --- a/lightning/restore/tidb.go +++ b/lightning/restore/tidb.go @@ -50,6 +50,7 @@ var ( "max_allowed_packet": "67108864", "div_precision_increment": "4", "time_zone": "SYSTEM", + "system_time_zone": "", "lc_time_names": "en_US", "default_week_format": "0", "block_encryption_mode": "aes-128-ecb", From 58d4ae8995aef963a348ce4ba588418c1a7e5bb1 Mon Sep 17 00:00:00 2001 From: luancheng Date: Thu, 18 Feb 2021 15:32:06 +0800 Subject: [PATCH 2/3] fix unit test --- lightning/restore/tidb_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lightning/restore/tidb_test.go b/lightning/restore/tidb_test.go index 664e47067..adefec2f7 100644 --- a/lightning/restore/tidb_test.go +++ b/lightning/restore/tidb_test.go @@ -416,6 +416,7 @@ func (s *tidbSuite) TestObtainRowFormatVersionSucceed(c *C) { c.Assert(sysVars, DeepEquals, map[string]string{ "tidb_row_format_version": "2", "max_allowed_packet": "1073741824", + "system_time_zone": "", "div_precision_increment": "10", "time_zone": "-08:00", "lc_time_names": "ja_JP", @@ -442,6 +443,7 @@ func (s *tidbSuite) TestObtainRowFormatVersionFailure(c *C) { c.Assert(sysVars, DeepEquals, map[string]string{ "tidb_row_format_version": "1", "max_allowed_packet": "67108864", + "system_time_zone": "", "div_precision_increment": "4", "time_zone": "+00:00", "lc_time_names": "en_US", From 014879318ae7a66c26854b10a082ef394d102df3 Mon Sep 17 00:00:00 2001 From: luancheng Date: Fri, 19 Feb 2021 11:56:21 +0800 Subject: [PATCH 3/3] fix ci --- lightning/backend/session.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lightning/backend/session.go b/lightning/backend/session.go index b1e994a6d..95138be2c 100644 --- a/lightning/backend/session.go +++ b/lightning/backend/session.go @@ -196,7 +196,7 @@ func newSession(options *SessionOptions) *session { vars.StmtCtx.OverflowAsWarning = !sqlMode.HasStrictMode() vars.StmtCtx.AllowInvalidDate = sqlMode.HasAllowInvalidDatesMode() vars.StmtCtx.IgnoreZeroInDate = !sqlMode.HasStrictMode() || sqlMode.HasAllowInvalidDatesMode() - tz := vars.Location() + var tz *time.Location if options.SysVars != nil { var tidbTimeZone, tidbSystemTimeZone string for k, v := range options.SysVars { @@ -208,7 +208,7 @@ func newSession(options *SessionOptions) *session { tidbSystemTimeZone = v } } - // we get system zone from TiDB server, we can set it for lightning session. + // if we get system zone from TiDB server, we can set it for lightning session. if tidbTimeZone == "SYSTEM" && tidbSystemTimeZone != "" { loc, err := timeutil.LoadLocation(tidbSystemTimeZone) if err != nil { @@ -217,6 +217,9 @@ func newSession(options *SessionOptions) *session { tz = loc } } + if tz == nil { + tz = vars.Location() + } vars.StmtCtx.TimeZone = tz vars.SetSystemVar("timestamp", strconv.FormatInt(options.Timestamp, 10)) vars.TxnCtx = nil