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

parser: add traffic capture/replay statements in the parser #57808

Merged
merged 9 commits into from
Dec 5, 2024
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
107 changes: 107 additions & 0 deletions pkg/parser/ast/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ var (
_ StmtNode = &PlanReplayerStmt{}
_ StmtNode = &CompactTableStmt{}
_ StmtNode = &SetResourceGroupStmt{}
_ StmtNode = &TrafficStmt{}

_ Node = &PrivElem{}
_ Node = &VariableAssignment{}
Expand Down Expand Up @@ -415,6 +416,112 @@ func (n *PlanReplayerStmt) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

// TrafficOpType is traffic operation type.
type TrafficOpType int

const (
TrafficOpCapture TrafficOpType = iota
TrafficOpReplay
TrafficOpShow
TrafficOpCancel
)

// TrafficOptionType is traffic option type.
type TrafficOptionType int

const (
// capture options
TrafficOptionDuration TrafficOptionType = iota
TrafficOptionEncryptionMethod
TrafficOptionCompress
// replay options
TrafficOptionUsername
TrafficOptionPassword
TrafficOptionSpeed
TrafficOptionReadOnly
)

// TrafficStmt is traffic operation statement.
type TrafficStmt struct {
stmtNode
OpType TrafficOpType
Options []*TrafficOption
Dir string
}

// TrafficOption is traffic option.
type TrafficOption struct {
OptionType TrafficOptionType
FloatValue ValueExpr
StrValue string
BoolValue bool
}

// Restore implements Node interface.
func (n *TrafficStmt) Restore(ctx *format.RestoreCtx) error {
switch n.OpType {
case TrafficOpCapture:
ctx.WriteKeyWord("TRAFFIC CAPTURE TO ")
ctx.WriteString(n.Dir)
for _, option := range n.Options {
ctx.WritePlain(" ")
switch option.OptionType {
case TrafficOptionDuration:
ctx.WriteKeyWord("DURATION ")
ctx.WritePlain("= ")
ctx.WriteString(option.StrValue)
case TrafficOptionEncryptionMethod:
ctx.WriteKeyWord("ENCRYPTION_METHOD ")
ctx.WritePlain("= ")
ctx.WriteString(option.StrValue)
case TrafficOptionCompress:
ctx.WriteKeyWord("COMPRESS ")
ctx.WritePlain("= ")
ctx.WritePlain(strings.ToUpper(fmt.Sprintf("%v", option.BoolValue)))
}
}
case TrafficOpReplay:
ctx.WriteKeyWord("TRAFFIC REPLAY FROM ")
ctx.WriteString(n.Dir)
for _, option := range n.Options {
ctx.WritePlain(" ")
switch option.OptionType {
case TrafficOptionUsername:
ctx.WriteKeyWord("USER ")
ctx.WritePlain("= ")
ctx.WriteString(option.StrValue)
case TrafficOptionPassword:
ctx.WriteKeyWord("PASSWORD ")
ctx.WritePlain("= ")
ctx.WriteString(option.StrValue)
case TrafficOptionSpeed:
ctx.WriteKeyWord("SPEED ")
ctx.WritePlain("= ")
ctx.WritePlainf("%v", option.FloatValue.GetValue())
case TrafficOptionReadOnly:
ctx.WriteKeyWord("READONLY ")
ctx.WritePlain("= ")
ctx.WritePlain(strings.ToUpper(fmt.Sprintf("%v", option.BoolValue)))
}
}
case TrafficOpShow:
ctx.WriteKeyWord("SHOW TRAFFIC JOBS")
case TrafficOpCancel:
ctx.WriteKeyWord("CANCEL TRAFFIC JOBS")
}
return nil
}

// Accept implements Node Accept interface.
func (n *TrafficStmt) Accept(v Visitor) (Node, bool) {
newNode, skipChildren := v.Enter(n)
if skipChildren {
return v.Leave(newNode)
}
n = newNode.(*TrafficStmt)
return v.Leave(n)
}

type CompactReplicaKind string

const (
Expand Down
5 changes: 5 additions & 0 deletions pkg/parser/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ var tokenMap = map[string]int{
"COMMIT": commit,
"COMMITTED": committed,
"COMPACT": compact,
"COMPRESS": compress,
"COMPRESSED": compressed,
"COMPRESSION": compression,
"CONCURRENCY": concurrency,
Expand Down Expand Up @@ -647,6 +648,7 @@ var tokenMap = map[string]int{
"RANGE": rangeKwd,
"RATE_LIMIT": rateLimit,
"READ": read,
"READ_ONLY": readOnly,
"REAL": realType,
"REBUILD": rebuild,
"RECENT": recent,
Expand All @@ -667,6 +669,7 @@ var tokenMap = map[string]int{
"REPEAT": repeat,
"REPEATABLE": repeatable,
"REPLACE": replace,
"REPLAY": replay,
"REPLAYER": replayer,
"REPLICA": replica,
"REPLICAS": replicas,
Expand Down Expand Up @@ -741,6 +744,7 @@ var tokenMap = map[string]int{
"SOME": some,
"SOURCE": source,
"SPATIAL": spatial,
"SPEED": speed,
"SPLIT": split,
"SQL_BIG_RESULT": sqlBigResult,
"SQL_BUFFER_RESULT": sqlBufferResult,
Expand Down Expand Up @@ -847,6 +851,7 @@ var tokenMap = map[string]int{
"TPCC": tpcc,
"TRACE": trace,
"TRADITIONAL": traditional,
"TRAFFIC": traffic,
"TRAILING": trailing,
"TRANSACTION": transaction,
"TRIGGER": trigger,
Expand Down
Loading