Skip to content

Commit

Permalink
ttl: fix TTL will delete unexpected rows when timezone changed (#41044)
Browse files Browse the repository at this point in the history
close #41043
  • Loading branch information
lcwangchao authored Feb 6, 2023
1 parent bc2c1b2 commit 76f1c52
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 69 deletions.
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

0 comments on commit 76f1c52

Please sign in to comment.