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

pkg/tz(ticdc): use correct ctx to get timezone (#8799) #8831

Merged
Next Next commit
pkg/tz(ticdc): fix some bugs when setting timezone
  • Loading branch information
Rustin170506 authored and ti-chi-bot committed Apr 21, 2023
commit 6cf6057c4c054df9f4a53fcbcbf1314ff70637a5
21 changes: 13 additions & 8 deletions pkg/sink/mysql/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ import (
"github.com/pingcap/log"
"github.com/pingcap/tiflow/cdc/contextutil"
"github.com/pingcap/tiflow/cdc/model"
cmdcontext "github.com/pingcap/tiflow/pkg/cmd/context"
"github.com/pingcap/tiflow/pkg/config"
cerror "github.com/pingcap/tiflow/pkg/errors"
"github.com/pingcap/tiflow/pkg/security"
"github.com/pingcap/tiflow/pkg/sink"
"github.com/pingcap/tiflow/pkg/util"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -160,7 +162,7 @@ func (c *Config) Apply(
if err = getSafeMode(query, &c.SafeMode); err != nil {
return err
}
if err = getTimezone(ctx, query, &c.Timezone); err != nil {
if err = getTimezone(query, &c.Timezone); err != nil {
return err
}
if err = getDuration(query, "read-timeout", &c.ReadTimeout); err != nil {
Expand Down Expand Up @@ -357,24 +359,27 @@ func getSafeMode(values url.Values, safeMode *bool) error {
return nil
}

func getTimezone(ctx context.Context, values url.Values, timezone *string) error {
func getTimezone(values url.Values, timezone *string) error {
if _, ok := values["time-zone"]; !ok {
tz := contextutil.TimezoneFromCtx(ctx)
// If time-zone is not specified, use the timezone of the server.
tz := contextutil.TimezoneFromCtx(cmdcontext.GetDefaultContext())
log.Warn("Because time-zone is not specified, the timezone of the TiCDC server will be used. "+
"We recommend that you specify the time-zone explicitly. "+
"Please make sure that the timezone of the TiCDC server, sink-uri and the downstream database are consistent.",
zap.String("time-zone", tz.String()))
*timezone = fmt.Sprintf(`"%s"`, tz.String())
return nil
}

s := values.Get("time-zone")
if len(s) == 0 {
*timezone = ""
log.Warn("Because time-zone is empty, the timezone of the downstream database will be used. " +
"We recommend that you specify the time-zone explicitly. ")
return nil
}

value, err := url.QueryUnescape(s)
if err != nil {
return cerror.WrapError(cerror.ErrMySQLInvalidConfig, err)
}
_, err = time.LoadLocation(value)
_, err := util.GetTimezone(s)
if err != nil {
return cerror.WrapError(cerror.ErrMySQLInvalidConfig, err)
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/util/tz.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
"strings"
"time"

"github.com/pingcap/log"
"github.com/pingcap/tidb/util/timeutil"
cerror "github.com/pingcap/tiflow/pkg/errors"
"go.uber.org/zap"
)

// GetTimezone returns the timezone specified by the name
Expand All @@ -28,9 +30,19 @@ func GetTimezone(name string) (tz *time.Location, err error) {
case "", "system", "local":
tz, err = GetLocalTimezone()
err = cerror.WrapError(cerror.ErrLoadTimezone, err)
if err == nil {
log.Info("Use the timezone of the TiCDC server machine",
zap.String("timezoneName", name),
zap.String("timezone", tz.String()))
}
default:
tz, err = time.LoadLocation(name)
err = cerror.WrapError(cerror.ErrLoadTimezone, err)
if err == nil {
log.Info("Load the timezone specified by the user",
zap.String("timezoneName", name),
zap.String("timezone", tz.String()))
}
}
return
}
Expand Down