Skip to content

Commit

Permalink
server: set system_time_zone from systemTZ (#13934)
Browse files Browse the repository at this point in the history
  • Loading branch information
bb7133 committed Dec 17, 2019
1 parent b9ba3ef commit 35771be
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
14 changes: 14 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import (
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/sys/linux"
"github.com/pingcap/tidb/util/timeutil"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -203,6 +204,7 @@ func NewServer(cfg *config.Config, driver IDriver) (*Server, error) {
stopListenerCh: make(chan struct{}, 1),
}
s.loadTLSCertificates()
setSystemTimeZoneVariable()

s.capability = defaultCapability
if s.tlsConfig != nil {
Expand Down Expand Up @@ -613,6 +615,18 @@ func (s *Server) kickIdleConnection() {
}
}

func setSystemTimeZoneVariable() {
tz, err := timeutil.GetSystemTZ()
if err != nil {
logutil.Logger(context.Background()).Error(
"Error getting SystemTZ, use default value instead",
zap.Error(err),
zap.String("default system_time_zone", variable.SysVars["system_time_zone"].Value))
return
}
variable.SysVars["system_time_zone"].Value = tz
}

// Server error codes.
const (
codeUnknownFieldType = 1
Expand Down
14 changes: 14 additions & 0 deletions server/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/util/testkit"
)

type TidbTestSuite struct {
Expand Down Expand Up @@ -294,6 +295,19 @@ func registerTLSConfig(configName string, caCertPath string, clientCertPath stri
return nil
}

func (ts *TidbTestSuite) TestSystemTimeZone(c *C) {
tk := testkit.NewTestKit(c, ts.store)
cfg := config.NewConfig()
cfg.Port = 4002
cfg.Status.ReportStatus = false
server, err := NewServer(cfg, ts.tidbdrv)
c.Assert(err, IsNil)
defer server.Close()

tz1 := tk.MustQuery("select variable_value from mysql.tidb where variable_name = 'system_tz'").Rows()
tk.MustQuery("select @@system_time_zone").Check(tz1)
}

func (ts *TidbTestSuite) TestTLS(c *C) {
// Generate valid TLS certificates.
caCert, caKey, err := generateCert(0, "TiDB CA", nil, nil, "/tmp/ca-key.pem", "/tmp/ca-cert.pem")
Expand Down
8 changes: 8 additions & 0 deletions util/timeutil/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ func SetSystemTZ(name string) {
})
}

// GetSystemTZ gets the value of systemTZ, an error is returned if systemTZ is not properly set.
func GetSystemTZ() (string, error) {
if systemTZ == "System" || systemTZ == "" {
return "", fmt.Errorf("variable `systemTZ` is not properly set")
}
return systemTZ, nil
}

// getLoc first trying to load location from a cache map. If nothing found in such map, then call
// `time.LoadLocation` to get a timezone location. After trying both way, an error will be returned
// if valid Location is not found.
Expand Down

0 comments on commit 35771be

Please sign in to comment.