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

*: fix wrong timezone when env TZ is not set #512

Merged
merged 5 commits into from
Apr 28, 2020
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
2 changes: 1 addition & 1 deletion cdc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var defaultServerOptions = options{
pdEndpoints: "http://127.0.0.1:2379",
statusHost: "127.0.0.1",
statusPort: defaultStatusPort,
timezone: time.Local,
timezone: nil,
gcTTL: DefaultCDCGCSafePointTTL,
}

Expand Down
1 change: 0 additions & 1 deletion cdc/sink/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ var defaultParams = params{
}

func configureSinkURI(dsnCfg *dmysql.Config, tz *time.Location) (string, error) {
dsnCfg.Loc = tz
if dsnCfg.Params == nil {
dsnCfg.Params = make(map[string]string, 1)
}
Expand Down
20 changes: 11 additions & 9 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strconv"
"strings"
"syscall"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/log"
Expand Down Expand Up @@ -54,15 +53,18 @@ func runEServer(cmd *cobra.Command, args []string) error {
return errors.Annotatef(err, "invalid status address: %s", statusAddr)
}

var opts []cdc.ServerOption
opts = append(opts, cdc.PDEndpoints(serverPdAddr), cdc.StatusHost(addrs[0]), cdc.StatusPort(int(statusPort)), cdc.GCTTL(gcTTL))
if strings.ToLower(timezone) != "system" {
tz, err := time.LoadLocation(timezone)
if err != nil {
return errors.Annotate(err, "can not load timezone")
}
opts = append(opts, cdc.Timezone(tz))
tz, err := util.GetTimezone(timezone)
if err != nil {
return errors.Annotate(err, "can not load timezone")
}

opts := []cdc.ServerOption{
cdc.PDEndpoints(serverPdAddr),
cdc.StatusHost(addrs[0]),
cdc.StatusPort(int(statusPort)),
cdc.GCTTL(gcTTL),
cdc.Timezone(tz)}

server, err := cdc.NewServer(opts...)
if err != nil {
return errors.Annotate(err, "new server")
Expand Down
34 changes: 34 additions & 0 deletions pkg/util/tz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package util

import (
"os"
"path"
"strings"
"time"
)

// GetTimezone returns the timezone specified by the name
func GetTimezone(name string) (*time.Location, error) {
switch strings.ToLower(name) {
case "", "system", "local":
return GetLocalTimezone()
default:
return time.LoadLocation(name)
}
}

// GetLocalTimezone returns the timezone in local system
func GetLocalTimezone() (*time.Location, error) {
if time.Local.String() != "Local" {
return time.Local, nil
}
str, err := os.Readlink("/etc/localtime")
if err != nil {
return nil, err
}
// the linked path of `/etc/localtime`
// MacOS: /var/db/timezone/zoneinfo/Asia/Shanghai
// Linux: /etc/usr/share/zoneinfo/Asia/Shanghai
tzName := path.Join(path.Base(path.Dir(str)), path.Base(str))
return time.LoadLocation(tzName)
}