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

server: set system_time_zone from system_tz #13934

Merged
merged 2 commits into from
Dec 9, 2019
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
14 changes: 14 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,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"
"google.golang.org/grpc"
)
Expand Down Expand Up @@ -209,6 +210,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 @@ -622,6 +624,18 @@ func (s *Server) kickIdleConnection() {
}
}

func setSystemTimeZoneVariable() {
tz, err := timeutil.GetSystemTZ()
if err != nil {
logutil.BgLogger().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 @@ -293,6 +294,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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need test check equal to TZ from host?

}

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 @@ -117,6 +117,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