Skip to content

Commit

Permalink
flashback: support to flashback to tso syntax (pingcap#48493) (pingca…
Browse files Browse the repository at this point in the history
  • Loading branch information
BornChanger authored Dec 11, 2023
1 parent c870a52 commit 575a4e6
Show file tree
Hide file tree
Showing 8 changed files with 11,117 additions and 10,984 deletions.
6 changes: 6 additions & 0 deletions executor/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,12 @@ func (e *DDLExec) getRecoverTableByTableName(tableName *ast.TableName) (*model.J
}

func (e *DDLExec) executeFlashBackCluster(s *ast.FlashBackToTimestampStmt) error {
// Check `TO TSO` clause
if s.FlashbackTSO > 0 {
return domain.GetDomain(e.ctx).DDL().FlashbackCluster(e.ctx, s.FlashbackTSO)
}

// Check `TO TIMESTAMP` clause
flashbackTS, err := staleread.CalculateAsOfTsExpr(context.Background(), e.ctx, s.FlashbackTS)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions executor/recover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,19 +476,19 @@ func TestFlashbackTSOWithSafeTs(t *testing.T) {
}{
{
name: "5 seconds ago to now, safeTS 5 secs ago",
sql: fmt.Sprintf("flashback cluster to timestamp '%d'", ts),
sql: fmt.Sprintf("flashback cluster to tso %d", ts),
injectSafeTS: oracle.GoTimeToTS(flashbackTs),
compareWithSafeTS: 0,
},
{
name: "10 seconds ago to now, safeTS 5 secs ago",
sql: fmt.Sprintf("flashback cluster to timestamp '%d'", ts),
sql: fmt.Sprintf("flashback cluster to tso %d", ts),
injectSafeTS: oracle.GoTimeToTS(flashbackTs.Add(10 * time.Second)),
compareWithSafeTS: -1,
},
{
name: "5 seconds ago to now, safeTS 10 secs ago",
sql: fmt.Sprintf("flashback cluster to timestamp '%d'", ts),
sql: fmt.Sprintf("flashback cluster to tso %d", ts),
injectSafeTS: oracle.GoTimeToTS(flashbackTs.Add(-10 * time.Second)),
compareWithSafeTS: 1,
},
Expand Down
29 changes: 19 additions & 10 deletions parser/ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4264,9 +4264,10 @@ func (n *RecoverTableStmt) Accept(v Visitor) (Node, bool) {
type FlashBackToTimestampStmt struct {
ddlNode

FlashbackTS ExprNode
Tables []*TableName
DBName model.CIStr
FlashbackTS ExprNode
FlashbackTSO uint64
Tables []*TableName
DBName model.CIStr
}

// Restore implements Node interface
Expand All @@ -4288,9 +4289,14 @@ func (n *FlashBackToTimestampStmt) Restore(ctx *format.RestoreCtx) error {
} else {
ctx.WriteKeyWord("CLUSTER")
}
ctx.WriteKeyWord(" TO TIMESTAMP ")
if err := n.FlashbackTS.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while splicing FlashBackToTimestampStmt.FlashbackTS")
if n.FlashbackTSO == 0 {
ctx.WriteKeyWord(" TO TIMESTAMP ")
if err := n.FlashbackTS.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while splicing FlashBackToTimestampStmt.FlashbackTS")
}
} else {
ctx.WriteKeyWord(" TO TSO ")
ctx.WritePlainf("%d", n.FlashbackTSO)
}
return nil
}
Expand All @@ -4311,11 +4317,14 @@ func (n *FlashBackToTimestampStmt) Accept(v Visitor) (Node, bool) {
n.Tables[i] = node.(*TableName)
}
}
node, ok := n.FlashbackTS.Accept(v)
if !ok {
return n, false

if n.FlashbackTSO == 0 {
node, ok := n.FlashbackTS.Accept(v)
if !ok {
return n, false
}
n.FlashbackTS = node.(ExprNode)
}
n.FlashbackTS = node.(ExprNode)
return v.Leave(n)
}

Expand Down
9 changes: 9 additions & 0 deletions parser/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@ func (s *Scanner) Lex(v *yySymType) int {
v.offset = pos.Offset
return toTimestamp
}

if tok1 == tsoType && tok2 == intLit {
_, pos, lit = s.scan()
v.ident = fmt.Sprintf("%s %s", v.ident, lit)
s.lastKeyword = toTSO
s.lastScanOffset = pos.Offset
v.offset = pos.Offset
return toTSO
}
}
// fix shift/reduce conflict with DEFINED NULL BY xxx OPTIONALLY ENCLOSED
if tok == optionally {
Expand Down
1 change: 1 addition & 0 deletions parser/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@ var tokenMap = map[string]int{
"TRUE": trueKwd,
"TRUNCATE": truncate,
"TRUE_CARD_COST": trueCardCost,
"TSO": tsoType,
"TTL": ttl,
"TTL_ENABLE": ttlEnable,
"TTL_JOB_INTERVAL": ttlJobInterval,
Expand Down
Loading

0 comments on commit 575a4e6

Please sign in to comment.