Skip to content

Commit

Permalink
Add SWITCH_GROUP syntax for runaway watch
Browse files Browse the repository at this point in the history
Signed-off-by: JmPotato <[email protected]>
  • Loading branch information
JmPotato committed Jul 25, 2024
1 parent 1acb8f7 commit 4ac322e
Show file tree
Hide file tree
Showing 8 changed files with 11,032 additions and 10,975 deletions.
17 changes: 15 additions & 2 deletions pkg/parser/ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2308,14 +2308,27 @@ func (n *ResourceGroupRunawayRuleOption) restore(ctx *format.RestoreCtx) error {
// ResourceGroupRunawayActionOption is used for parsing the resource group runaway action.
type ResourceGroupRunawayActionOption struct {
node
Type model.RunawayActionType
Type model.RunawayActionType
SwitchGroupName model.CIStr
}

// Restore implements Node interface.
func (n *ResourceGroupRunawayActionOption) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("ACTION ")
ctx.WritePlain("= ")
ctx.WriteKeyWord(n.Type.String())
switch n.Type {
case model.RunawayActionNone, model.RunawayActionDryRun, model.RunawayActionCooldown, model.RunawayActionKill:
ctx.WriteKeyWord(n.Type.String())
case model.RunawayActionSwitchGroup:
switchGroup := n.SwitchGroupName.String()
if len(switchGroup) == 0 {
return errors.New("SWITCH_GROUP runaway watch action requires a non-empty group name")
}
ctx.WriteKeyWord("SWITCH_GROUP")
ctx.WritePlain("(")
ctx.WriteName(switchGroup)
ctx.WritePlain(")")
}
return nil
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/parser/ast/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,10 @@ func TestResourceGroupDDLStmtRestore(t *testing.T) {
"CREATE RESOURCE GROUP IF NOT EXISTS rg1 RU_PER_SEC = 500 QUERY_LIMIT=(EXEC_ELAPSED='60s', ACTION=COOLDOWN)",
"CREATE RESOURCE GROUP IF NOT EXISTS `rg1` RU_PER_SEC = 500, QUERY_LIMIT = (EXEC_ELAPSED = '60s' ACTION = COOLDOWN)",
},
{
"CREATE RESOURCE GROUP IF NOT EXISTS rg1 RU_PER_SEC = 500 QUERY_LIMIT=(ACTION=SWITCH_GROUP(rg2))",
"CREATE RESOURCE GROUP IF NOT EXISTS `rg1` RU_PER_SEC = 500, QUERY_LIMIT = (ACTION = SWITCH_GROUP(`rg2`))",
},
}
extractNodeFunc := func(node Node) Node {
return node.(*CreateResourceGroupStmt)
Expand All @@ -963,6 +967,10 @@ func TestResourceGroupDDLStmtRestore(t *testing.T) {
"ALTER RESOURCE GROUP rg1 QUERY_LIMIT=(EXEC_ELAPSED='60s', ACTION=KILL, WATCH=SIMILAR DURATION='10m')",
"ALTER RESOURCE GROUP `rg1` QUERY_LIMIT = (EXEC_ELAPSED = '60s' ACTION = KILL WATCH = SIMILAR DURATION = '10m')",
},
{
"ALTER RESOURCE GROUP rg1 QUERY_LIMIT=(EXEC_ELAPSED='1m', ACTION=SWITCH_GROUP(rg2), WATCH=SIMILAR DURATION='10m')",
"ALTER RESOURCE GROUP `rg1` QUERY_LIMIT = (EXEC_ELAPSED = '1m' ACTION = SWITCH_GROUP(`rg2`) WATCH = SIMILAR DURATION = '10m')",
},
{
"ALTER RESOURCE GROUP rg1 QUERY_LIMIT=NULL",
"ALTER RESOURCE GROUP `rg1` QUERY_LIMIT = NULL",
Expand Down
4 changes: 4 additions & 0 deletions pkg/parser/ast/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,10 @@ func TestAddQueryWatchStmtRestore(t *testing.T) {
"QUERY WATCH ADD RESOURCE GROUP rg1 ACTION COOLDOWN PLAN DIGEST 'd08bc323a934c39dc41948b0a073725be3398479b6fa4f6dd1db2a9b115f7f57'",
"QUERY WATCH ADD RESOURCE GROUP `rg1` ACTION = COOLDOWN PLAN DIGEST _UTF8MB4'd08bc323a934c39dc41948b0a073725be3398479b6fa4f6dd1db2a9b115f7f57'",
},
{
"QUERY WATCH ADD ACTION SWITCH_GROUP(rg1) SQL TEXT EXACT TO 'select * from test.t1'",
"QUERY WATCH ADD ACTION = SWITCH_GROUP(`rg1`) SQL TEXT EXACT TO _UTF8MB4'select * from test.t1'",
},
}
extractNodeFunc := func(node ast.Node) ast.Node {
return node.(*ast.AddQueryWatchStmt)
Expand Down
1 change: 1 addition & 0 deletions pkg/parser/keywords.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ var Keywords = []KeywordsType{
{"VALIDATION", false, "unreserved"},
{"VALUE", false, "unreserved"},
{"VARIABLES", false, "unreserved"},
{"VECTOR", false, "unreserved"},
{"VIEW", false, "unreserved"},
{"VISIBLE", false, "unreserved"},
{"WAIT", false, "unreserved"},
Expand Down
1 change: 1 addition & 0 deletions pkg/parser/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ var tokenMap = map[string]int{
"SURVIVAL_PREFERENCES": survivalPreferences,
"SWAPS": swaps,
"SWITCHES": switchesSym,
"SWITCH_GROUP": switchGroup,
"SYSTEM": system,
"SYSTEM_TIME": systemTime,
"TARGET": target,
Expand Down
3 changes: 3 additions & 0 deletions pkg/parser/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,7 @@ const (
RunawayActionDryRun
RunawayActionCooldown
RunawayActionKill
RunawayActionSwitchGroup
)

// RunawayWatchType is the type of runaway watch.
Expand Down Expand Up @@ -1995,6 +1996,8 @@ func (t RunawayActionType) String() string {
return "COOLDOWN"
case RunawayActionKill:
return "KILL"
case RunawayActionSwitchGroup:
return "SWITCH_GROUP"
default:
return "DRYRUN"
}
Expand Down
Loading

0 comments on commit 4ac322e

Please sign in to comment.