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

ttl: fix TTL will delete unexpected rows when timezone changed #41044

Merged
merged 3 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 ttl/client/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (c *mockClient) WatchNotification(_ context.Context, typ string) clientv3.W
c.Lock()
defer c.Unlock()

ch := make(chan clientv3.WatchResponse, 1)
ch := make(chan clientv3.WatchResponse, 8)
c.notificationWatchers[typ] = append(c.notificationWatchers[typ], ch)
return ch
}
24 changes: 13 additions & 11 deletions ttl/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,23 @@ func (s *session) RunInTxn(ctx context.Context, fn func() error, txnMode TxnMode
// ResetWithGlobalTimeZone resets the session time zone to global time zone
func (s *session) ResetWithGlobalTimeZone(ctx context.Context) error {
sessVar := s.GetSessionVars()
globalTZ, err := sessVar.GetGlobalSystemVar(ctx, variable.TimeZone)
if err != nil {
return err
}
if sessVar.TimeZone != nil {
globalTZ, err := sessVar.GetGlobalSystemVar(ctx, variable.TimeZone)
if err != nil {
return err
}

tz, err := sessVar.GetSessionOrGlobalSystemVar(ctx, variable.TimeZone)
if err != nil {
return err
}
tz, err := sessVar.GetSessionOrGlobalSystemVar(ctx, variable.TimeZone)
if err != nil {
return err
}

if globalTZ == tz {
return nil
if globalTZ == tz {
return nil
}
}

_, err = s.ExecuteSQL(ctx, "SET @@time_zone=@@global.time_zone")
_, err := s.ExecuteSQL(ctx, "SET @@time_zone=@@global.time_zone")
return err
}

Expand Down
8 changes: 3 additions & 5 deletions ttl/sqlbuilder/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import (
"github.com/pkg/errors"
)

const dateTimeFormat = "2006-01-02 15:04:05.999999"

func writeHex(in io.Writer, d types.Datum) error {
_, err := fmt.Fprintf(in, "x'%s'", hex.EncodeToString(d.GetBytes()))
return err
Expand Down Expand Up @@ -183,9 +181,9 @@ func (b *SQLBuilder) WriteExpireCondition(expire time.Time) error {

b.writeColNames([]*model.ColumnInfo{b.tbl.TimeColumn}, false)
b.restoreCtx.WritePlain(" < ")
b.restoreCtx.WritePlain("'")
b.restoreCtx.WritePlain(expire.Format(dateTimeFormat))
b.restoreCtx.WritePlain("'")
b.restoreCtx.WritePlain("FROM_UNIXTIME(")
b.restoreCtx.WritePlain(strconv.FormatInt(expire.Unix(), 10))
b.restoreCtx.WritePlain(")")
b.hasWriteExpireCond = true
return nil
}
Expand Down
Loading