Skip to content

Commit

Permalink
resource_control: support unlimited keyword when setting the resource…
Browse files Browse the repository at this point in the history
… group (#54704)

close #54413
  • Loading branch information
HuSharp authored Jul 26, 2024
1 parent e366584 commit 0dff610
Show file tree
Hide file tree
Showing 13 changed files with 4,642 additions and 4,597 deletions.
13 changes: 12 additions & 1 deletion pkg/ddl/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3699,7 +3699,7 @@ func SetDirectResourceGroupSettings(groupInfo *model.ResourceGroupInfo, opt *ast
resourceGroupSettings := groupInfo.ResourceGroupSettings
switch opt.Tp {
case ast.ResourceRURate:
resourceGroupSettings.RURate = opt.UintValue
return SetDirectResourceGroupRUSecondOption(resourceGroupSettings, opt.UintValue, opt.BoolValue)
case ast.ResourcePriority:
resourceGroupSettings.Priority = opt.UintValue
case ast.ResourceUnitCPU:
Expand Down Expand Up @@ -3746,6 +3746,17 @@ func SetDirectResourceGroupSettings(groupInfo *model.ResourceGroupInfo, opt *ast
return nil
}

// SetDirectResourceGroupRUSecondOption tries to set ru second part of the ResourceGroupSettings.
func SetDirectResourceGroupRUSecondOption(resourceGroupSettings *model.ResourceGroupSettings, intVal uint64, unlimited bool) error {
if unlimited {
resourceGroupSettings.RURate = uint64(math.MaxInt32)
resourceGroupSettings.BurstLimit = -1
} else {
resourceGroupSettings.RURate = intVal
}
return nil
}

// SetDirectResourceGroupRunawayOption tries to set runaway part of the ResourceGroupSettings.
func SetDirectResourceGroupRunawayOption(resourceGroupSettings *model.ResourceGroupSettings, opt *ast.ResourceGroupRunawayOption) error {
if resourceGroupSettings.Runaway == nil {
Expand Down
20 changes: 17 additions & 3 deletions pkg/ddl/tests/resourcegroup/resource_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package resourcegrouptest_test
import (
"context"
"fmt"
"math"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -92,11 +93,13 @@ func TestResourceGroupBasic(t *testing.T) {

tk.MustExec("set global tidb_enable_resource_control = off")
tk.MustGetErrCode("alter resource group x RU_PER_SEC=2000 ", mysql.ErrResourceGroupSupportDisabled)
tk.MustGetErrCode("alter resource group x RU_PER_SEC=unlimited ", mysql.ErrResourceGroupSupportDisabled)
tk.MustGetErrCode("drop resource group x ", mysql.ErrResourceGroupSupportDisabled)

tk.MustExec("set global tidb_enable_resource_control = DEFAULT")

tk.MustGetErrCode("create resource group x RU_PER_SEC=1000 ", mysql.ErrResourceGroupExists)
tk.MustGetErrCode("create resource group x RU_PER_SEC=UNLIMITED ", mysql.ErrResourceGroupExists)

tk.MustExec("alter resource group x RU_PER_SEC=2000 BURSTABLE QUERY_LIMIT=(EXEC_ELAPSED='15s' ACTION DRYRUN WATCH SIMILAR DURATION '10m0s')")
g = testResourceGroupNameFromIS(t, tk.Session(), "x")
Expand All @@ -107,16 +110,27 @@ func TestResourceGroupBasic(t *testing.T) {
re.Equal(model.WatchSimilar, g.Runaway.WatchType)
re.Equal(int64(time.Minute*10/time.Millisecond), g.Runaway.WatchDurationMs)

tk.MustExec("alter resource group x QUERY_LIMIT=(EXEC_ELAPSED='20s' ACTION DRYRUN WATCH SIMILAR)")
tk.MustExec("alter resource group x QUERY_LIMIT=(EXEC_ELAPSED='20s' ACTION DRYRUN WATCH SIMILAR) BURSTABLE=FALSE")
g = testResourceGroupNameFromIS(t, tk.Session(), "x")
re.Equal(uint64(2000), g.RURate)
re.Equal(int64(-1), g.BurstLimit)
re.Equal(int64(2000), g.BurstLimit)
re.Equal(uint64(time.Second*20/time.Millisecond), g.Runaway.ExecElapsedTimeMs)
re.Equal(model.RunawayActionDryRun, g.Runaway.Action)
re.Equal(model.WatchSimilar, g.Runaway.WatchType)
re.Equal(int64(0), g.Runaway.WatchDurationMs)

tk.MustQuery("select * from information_schema.resource_groups where name = 'x'").Check(testkit.Rows("x 2000 MEDIUM YES EXEC_ELAPSED='20s', ACTION=DRYRUN, WATCH=SIMILAR DURATION=UNLIMITED <nil>"))
tk.MustQuery("select * from information_schema.resource_groups where name = 'x'").Check(testkit.Rows("x 2000 MEDIUM NO EXEC_ELAPSED='20s', ACTION=DRYRUN, WATCH=SIMILAR DURATION=UNLIMITED <nil>"))

tk.MustExec("alter resource group x RU_PER_SEC= unlimited QUERY_LIMIT=(EXEC_ELAPSED='15s' ACTION DRYRUN WATCH SIMILAR DURATION '10m0s')")
g = testResourceGroupNameFromIS(t, tk.Session(), "x")
re.Equal(uint64(math.MaxInt32), g.RURate)
re.Equal(int64(-1), g.BurstLimit)
re.Equal(uint64(time.Second*15/time.Millisecond), g.Runaway.ExecElapsedTimeMs)
re.Equal(model.RunawayActionDryRun, g.Runaway.Action)
re.Equal(model.WatchSimilar, g.Runaway.WatchType)
re.Equal(int64(time.Minute*10/time.Millisecond), g.Runaway.WatchDurationMs)

tk.MustQuery("select * from information_schema.resource_groups where name = 'x'").Check(testkit.Rows("x UNLIMITED MEDIUM YES EXEC_ELAPSED='15s', ACTION=DRYRUN, WATCH=SIMILAR DURATION='10m0s' <nil>"))

tk.MustExec("drop resource group x")
g = testResourceGroupNameFromIS(t, tk.Session(), "x")
Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/runaway.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (do *Domain) GetRunawayWatchList() []*resourcegroup.QuarantineRecord {
return do.runawayManager.GetWatchList()
}

// TryToUpdateRunawayWatch is used to to update watch list including
// TryToUpdateRunawayWatch is used to update watch list including
// creation and deletion by manual trigger.
func (do *Domain) TryToUpdateRunawayWatch() error {
return do.updateNewAndDoneWatch()
Expand Down
3 changes: 2 additions & 1 deletion pkg/executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3618,7 +3618,8 @@ func (e *memtableRetriever) setDataFromResourceGroups() error {
burstable := burstdisableStr
priority := model.PriorityValueToName(uint64(group.Priority))
fillrate := unlimitedFillRate
isDefaultInReservedSetting := group.Name == resourcegroup.DefaultResourceGroupName && group.RUSettings.RU.Settings.FillRate == math.MaxInt32
// RU_PER_SEC = unlimited like the default group settings.
isDefaultInReservedSetting := group.RUSettings.RU.Settings.FillRate == math.MaxInt32
if !isDefaultInReservedSetting {
fillrate = strconv.FormatUint(group.RUSettings.RU.Settings.FillRate, 10)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/executor/internal/querywatch/query_watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func fromQueryWatchOptionList(ctx context.Context, sctx, newSctx sessionctx.Cont

// validateWatchRecord follows several designs:
// 1. If no resource group is set, the default resource group is used
// 2. If no action is specified, the action of the resource group is used. If no, an error message is displayed.
// 2. If no action is specified, the action of the resource group is used. If no, an error message displayed.
func validateWatchRecord(record *resourcegroup.QuarantineRecord, client *rmclient.ResourceGroupsController) error {
if len(record.ResourceGroupName) == 0 {
record.ResourceGroupName = resourcegroup.DefaultResourceGroupName
Expand Down
2 changes: 1 addition & 1 deletion pkg/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ func (m *Meta) GetResourceGroup(groupID int64) (*model.ResourceGroupInfo, error)
return nil, errors.Trace(err)
}
if value == nil {
// the default group is not persistanted to tikv by default.
// the default group is not persistent to tikv by default.
if groupID == defaultGroupID {
return defaultRGroupMeta, nil
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/parser/ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2202,7 +2202,11 @@ func (n *ResourceGroupOption) Restore(ctx *format.RestoreCtx) error {
case ResourceRURate:
ctx.WriteKeyWord("RU_PER_SEC ")
ctx.WritePlain("= ")
ctx.WritePlainf("%d", n.UintValue)
if n.BoolValue {
ctx.WriteKeyWord("UNLIMITED")
} else {
ctx.WritePlainf("%d", n.UintValue)
}
case ResourcePriority:
ctx.WriteKeyWord("PRIORITY ")
ctx.WritePlain("= ")
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
2 changes: 1 addition & 1 deletion pkg/parser/keywords_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestKeywords(t *testing.T) {
}

func TestKeywordsLength(t *testing.T) {
require.Equal(t, 653, len(parser.Keywords))
require.Equal(t, 654, len(parser.Keywords))

reservedNr := 0
for _, kw := range parser.Keywords {
Expand Down
6 changes: 0 additions & 6 deletions pkg/parser/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2002,12 +2002,6 @@ func (t RunawayActionType) String() string {
}
}

// ResourceGroupRefInfo is the struct to refer the resource group.
type ResourceGroupRefInfo struct {
ID int64 `json:"id"`
Name CIStr `json:"name"`
}

// ResourceGroupRunawaySettings is the runaway settings of the resource group
type ResourceGroupRunawaySettings struct {
ExecElapsedTimeMs uint64 `json:"exec_elapsed_time_ms"`
Expand Down
Loading

0 comments on commit 0dff610

Please sign in to comment.