Skip to content

Commit

Permalink
*: Add priority in resource group setting (#42293)
Browse files Browse the repository at this point in the history
ref #38825
  • Loading branch information
Connor1996 authored Mar 17, 2023
1 parent b3973b3 commit 61de990
Show file tree
Hide file tree
Showing 14 changed files with 10,193 additions and 10,054 deletions.
4 changes: 2 additions & 2 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3373,8 +3373,8 @@ def go_deps():
name = "com_github_pingcap_kvproto",
build_file_proto_mode = "disable_global",
importpath = "github.com/pingcap/kvproto",
sum = "h1:z6WwBPP0Txmal5zf+H7vf/lSmKZtSS8BTNwiLjEjdnA=",
version = "v0.0.0-20230312142449-01623096c924",
sum = "h1:GW25HwXCHjellWh/Q81MEAK6l7oNOD51FrNmiRInnmA=",
version = "v0.0.0-20230316070603-a626a05e1c1f",
)
go_repository(
name = "com_github_pingcap_log",
Expand Down
4 changes: 3 additions & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3061,6 +3061,8 @@ func SetDirectResourceGroupUnit(resourceGroupSettings *model.ResourceGroupSettin
switch typ {
case ast.ResourceRURate:
resourceGroupSettings.RURate = uintVal
case ast.ResourcePriority:
resourceGroupSettings.Priority = uintVal
case ast.ResourceUnitCPU:
resourceGroupSettings.CPULimiter = stringVal
case ast.ResourceUnitIOReadBandwidth:
Expand Down Expand Up @@ -7897,7 +7899,7 @@ func (d *ddl) DropResourceGroup(ctx sessionctx.Context, stmt *ast.DropResourceGr
}

func buildResourceGroup(oldGroup *model.ResourceGroupInfo, options []*ast.ResourceGroupOption) (*model.ResourceGroupInfo, error) {
groupInfo := &model.ResourceGroupInfo{Name: oldGroup.Name, ID: oldGroup.ID, ResourceGroupSettings: &model.ResourceGroupSettings{}}
groupInfo := &model.ResourceGroupInfo{Name: oldGroup.Name, ID: oldGroup.ID, ResourceGroupSettings: model.NewResourceGroupSettings()}
for _, opt := range options {
err := SetDirectResourceGroupUnit(groupInfo.ResourceGroupSettings, opt.Tp, opt.StrValue, opt.UintValue, opt.BoolValue)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions ddl/resourcegroup/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func NewGroupFromOptions(groupName string, options *model.ResourceGroupSettings)
},
},
}
group.Priority = uint32(options.Priority)
if len(options.CPULimiter) > 0 || len(options.IOReadBandwidth) > 0 || len(options.IOWriteBandwidth) > 0 {
return nil, ErrInvalidResourceGroupDuplicatedMode
}
Expand Down
44 changes: 26 additions & 18 deletions ddl/resourcegrouptest/resource_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestResourceGroupBasic(t *testing.T) {
hook.OnJobUpdatedExported.Store(&onJobUpdatedExportedFunc)
dom.DDL().SetHook(hook)

tk.MustQuery("select * from information_schema.resource_groups where name = 'default'").Check(testkit.Rows("default 1000000 YES"))
tk.MustQuery("select * from information_schema.resource_groups where name = 'default'").Check(testkit.Rows("default 1000000 MEDIUM YES"))

tk.MustExec("set global tidb_enable_resource_control = 'off'")
tk.MustGetErrCode("create user usr1 resource group rg1", mysql.ErrResourceGroupSupportDisabled)
Expand All @@ -62,7 +62,7 @@ func TestResourceGroupBasic(t *testing.T) {
tk.MustExec("set global tidb_enable_resource_control = 'on'")

tk.MustExec("alter resource group `default` ru_per_sec=10000")
tk.MustQuery("select * from information_schema.resource_groups where name = 'default'").Check(testkit.Rows("default 10000 NO"))
tk.MustQuery("select * from information_schema.resource_groups where name = 'default'").Check(testkit.Rows("default 10000 MEDIUM NO"))
tk.MustContainErrMsg("drop resource group `default`", "can't drop reserved resource group")

tk.MustExec("create resource group x RU_PER_SEC=1000")
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestResourceGroupBasic(t *testing.T) {
re.Equal(uint64(2000), g.RURate)
re.Equal(int64(-1), g.BurstLimit)

tk.MustQuery("select * from information_schema.resource_groups where name = 'x'").Check(testkit.Rows("x 2000 YES"))
tk.MustQuery("select * from information_schema.resource_groups where name = 'x'").Check(testkit.Rows("x 2000 MEDIUM YES"))

tk.MustExec("drop resource group x")
g = testResourceGroupNameFromIS(t, tk.Session(), "x")
Expand Down Expand Up @@ -146,21 +146,25 @@ func TestResourceGroupBasic(t *testing.T) {
require.NoError(t, err)

// Check information schema table information_schema.resource_groups
tk.MustExec("create resource group x RU_PER_SEC=1000")
tk.MustQuery("select * from information_schema.resource_groups where name = 'x'").Check(testkit.Rows("x 1000 NO"))
tk.MustExec("create resource group x RU_PER_SEC=1000 PRIORITY=LOW")
tk.MustQuery("select * from information_schema.resource_groups where name = 'x'").Check(testkit.Rows("x 1000 LOW NO"))
tk.MustExec("alter resource group x RU_PER_SEC=2000 BURSTABLE")
tk.MustQuery("select * from information_schema.resource_groups where name = 'x'").Check(testkit.Rows("x 2000 YES"))
tk.MustQuery("select * from information_schema.resource_groups where name = 'x'").Check(testkit.Rows("x 2000 MEDIUM YES"))
tk.MustExec("alter resource group x BURSTABLE RU_PER_SEC=3000")
tk.MustQuery("select * from information_schema.resource_groups where name = 'x'").Check(testkit.Rows("x 3000 YES"))
tk.MustQuery("show create resource group x").Check(testkit.Rows("x CREATE RESOURCE GROUP `x` RU_PER_SEC=3000 BURSTABLE"))
tk.MustQuery("select * from information_schema.resource_groups where name = 'x'").Check(testkit.Rows("x 3000 MEDIUM YES"))
tk.MustQuery("show create resource group x").Check(testkit.Rows("x CREATE RESOURCE GROUP `x` RU_PER_SEC=3000 PRIORITY=MEDIUM BURSTABLE"))

tk.MustExec("create resource group y BURSTABLE RU_PER_SEC=2000")
tk.MustQuery("select * from information_schema.resource_groups where name = 'y'").Check(testkit.Rows("y 2000 YES"))
tk.MustQuery("show create resource group y").Check(testkit.Rows("y CREATE RESOURCE GROUP `y` RU_PER_SEC=2000 BURSTABLE"))
tk.MustQuery("select * from information_schema.resource_groups where name = 'y'").Check(testkit.Rows("y 2000 MEDIUM YES"))
tk.MustQuery("show create resource group y").Check(testkit.Rows("y CREATE RESOURCE GROUP `y` RU_PER_SEC=2000 PRIORITY=MEDIUM BURSTABLE"))

tk.MustExec("alter resource group y RU_PER_SEC=4000 BURSTABLE")
tk.MustQuery("select * from information_schema.resource_groups where name = 'y'").Check(testkit.Rows("y 4000 YES"))
tk.MustQuery("show create resource group y").Check(testkit.Rows("y CREATE RESOURCE GROUP `y` RU_PER_SEC=4000 BURSTABLE"))
tk.MustQuery("select * from information_schema.resource_groups where name = 'y'").Check(testkit.Rows("y 4000 MEDIUM YES"))
tk.MustQuery("show create resource group y").Check(testkit.Rows("y CREATE RESOURCE GROUP `y` RU_PER_SEC=4000 PRIORITY=MEDIUM BURSTABLE"))

tk.MustExec("alter resource group y RU_PER_SEC=4000 PRIORITY=HIGH BURSTABLE")
tk.MustQuery("select * from information_schema.resource_groups where name = 'y'").Check(testkit.Rows("y 4000 HIGH YES"))
tk.MustQuery("show create resource group y").Check(testkit.Rows("y CREATE RESOURCE GROUP `y` RU_PER_SEC=4000 PRIORITY=HIGH BURSTABLE"))

tk.MustQuery("select count(*) from information_schema.resource_groups").Check(testkit.Rows("3"))
tk.MustGetErrCode("create user usr_fail resource group nil_group", mysql.ErrResourceGroupNotExists)
Expand Down Expand Up @@ -244,11 +248,13 @@ func TestNewResourceGroupFromOptions(t *testing.T) {
tests = append(tests, TestCase{
name: "normal case: ru case 1",
input: &model.ResourceGroupSettings{
RURate: 2000,
RURate: 2000,
Priority: 0,
},
output: &rmpb.ResourceGroup{
Name: groupName,
Mode: rmpb.GroupMode_RUMode,
Name: groupName,
Mode: rmpb.GroupMode_RUMode,
Priority: 0,
RUSettings: &rmpb.GroupRequestUnitSettings{
RU: &rmpb.TokenBucket{Settings: &rmpb.TokenLimitSettings{FillRate: 2000}},
},
Expand All @@ -258,11 +264,13 @@ func TestNewResourceGroupFromOptions(t *testing.T) {
tests = append(tests, TestCase{
name: "normal case: ru case 2",
input: &model.ResourceGroupSettings{
RURate: 5000,
RURate: 5000,
Priority: 8,
},
output: &rmpb.ResourceGroup{
Name: groupName,
Mode: rmpb.GroupMode_RUMode,
Name: groupName,
Priority: 8,
Mode: rmpb.GroupMode_RUMode,
RUSettings: &rmpb.GroupRequestUnitSettings{
RU: &rmpb.TokenBucket{Settings: &rmpb.TokenLimitSettings{FillRate: 5000}},
},
Expand Down
3 changes: 3 additions & 0 deletions executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3344,6 +3344,7 @@ func (e *memtableRetriever) setDataFromResourceGroups() error {
for _, group := range resourceGroups {
//mode := ""
burstable := "NO"
priority := model.PriorityValueToName(uint64(group.Priority))
switch group.Mode {
case rmpb.GroupMode_RUMode:
if group.RUSettings.RU.Settings.BurstLimit < 0 {
Expand All @@ -3352,6 +3353,7 @@ func (e *memtableRetriever) setDataFromResourceGroups() error {
row := types.MakeDatums(
group.Name,
group.RUSettings.RU.Settings.FillRate,
priority,
burstable,
)
rows = append(rows, row)
Expand All @@ -3361,6 +3363,7 @@ func (e *memtableRetriever) setDataFromResourceGroups() error {
group.Name,
nil,
nil,
nil,
)
rows = append(rows, row)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ require (
github.com/pingcap/errors v0.11.5-0.20221009092201-b66cddb77c32
github.com/pingcap/failpoint v0.0.0-20220801062533-2eaa32854a6c
github.com/pingcap/fn v0.0.0-20200306044125-d5540d389059
github.com/pingcap/kvproto v0.0.0-20230312142449-01623096c924
github.com/pingcap/kvproto v0.0.0-20230316070603-a626a05e1c1f
github.com/pingcap/log v1.1.1-0.20221116035753-734d527bc87c
github.com/pingcap/sysutil v0.0.0-20220114020952-ea68d2dbf5b4
github.com/pingcap/tidb/parser v0.0.0-20211011031125-9b13dc409c5e
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,8 @@ github.com/pingcap/fn v0.0.0-20200306044125-d5540d389059/go.mod h1:fMRU1BA1y+r89
github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989 h1:surzm05a8C9dN8dIUmo4Be2+pMRb6f55i+UIYrluu2E=
github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989/go.mod h1:O17XtbryoCJhkKGbT62+L2OlrniwqiGLSqrmdHCMzZw=
github.com/pingcap/kvproto v0.0.0-20191211054548-3c6b38ea5107/go.mod h1:WWLmULLO7l8IOcQG+t+ItJ3fEcrL5FxF0Wu+HrMy26w=
github.com/pingcap/kvproto v0.0.0-20230312142449-01623096c924 h1:z6WwBPP0Txmal5zf+H7vf/lSmKZtSS8BTNwiLjEjdnA=
github.com/pingcap/kvproto v0.0.0-20230312142449-01623096c924/go.mod h1:KUrW1FGoznGMMTssYBu0czfAhn6vQcIrHyZoSC6T990=
github.com/pingcap/kvproto v0.0.0-20230316070603-a626a05e1c1f h1:GW25HwXCHjellWh/Q81MEAK6l7oNOD51FrNmiRInnmA=
github.com/pingcap/kvproto v0.0.0-20230316070603-a626a05e1c1f/go.mod h1:KUrW1FGoznGMMTssYBu0czfAhn6vQcIrHyZoSC6T990=
github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8=
github.com/pingcap/log v0.0.0-20210625125904-98ed8e2eb1c7/go.mod h1:8AanEdAHATuRurdGxZXBz0At+9avep+ub7U1AGYLIMM=
github.com/pingcap/log v1.1.0/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4=
Expand Down
1 change: 1 addition & 0 deletions infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,7 @@ var tableMemoryUsageOpsHistoryCols = []columnInfo{
var tableResourceGroupsCols = []columnInfo{
{name: "NAME", tp: mysql.TypeVarchar, size: resourcegroup.MaxGroupNameLength, flag: mysql.NotNullFlag},
{name: "RU_PER_SEC", tp: mysql.TypeLonglong, size: 21},
{name: "PRIORITY", tp: mysql.TypeVarchar, size: 6},
{name: "BURSTABLE", tp: mysql.TypeVarchar, size: 3},
}

Expand Down
5 changes: 5 additions & 0 deletions parser/ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2116,6 +2116,7 @@ type ResourceUnitType int
const (
// RU mode
ResourceRURate ResourceUnitType = iota
ResourcePriority
// Raw mode
ResourceUnitCPU
ResourceUnitIOReadBandwidth
Expand All @@ -2135,6 +2136,10 @@ func (n *ResourceGroupOption) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("RU_PER_SEC ")
ctx.WritePlain("= ")
ctx.WritePlainf("%d", n.UintValue)
case ResourcePriority:
ctx.WriteKeyWord("PRIORITY ")
ctx.WritePlain("= ")
ctx.WriteKeyWord(model.PriorityValueToName(n.UintValue))
case ResourceUnitCPU:
ctx.WriteKeyWord("CPU ")
ctx.WritePlain("= ")
Expand Down
4 changes: 4 additions & 0 deletions parser/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ var tokenMap = map[string]int{
"INVOKER": invoker,
"IO": io,
"RU_PER_SEC": ruRate,
"PRIORITY": priority,
"HIGH": high,
"MEDIUM": medium,
"LOW": low,
"IO_READ_BANDWIDTH": ioReadBandwidth,
"IO_WRITE_BANDWIDTH": ioWriteBandwidth,
"IPC": ipc,
Expand Down
35 changes: 35 additions & 0 deletions parser/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -1836,17 +1836,52 @@ type ResourceGroupRefInfo struct {
// ResourceGroupSettings is the settings of the resource group
type ResourceGroupSettings struct {
RURate uint64 `json:"ru_per_sec"`
Priority uint64 `json:"priority"`
CPULimiter string `json:"cpu_limit"`
IOReadBandwidth string `json:"io_read_bandwidth"`
IOWriteBandwidth string `json:"io_write_bandwidth"`
BurstLimit int64 `json:"burst_limit"`
}

// NewResourceGroupSettings creates a new ResourceGroupSettings.
func NewResourceGroupSettings() *ResourceGroupSettings {
return &ResourceGroupSettings{
RURate: 0,
Priority: MediumPriorityValue,
CPULimiter: "",
IOReadBandwidth: "",
IOWriteBandwidth: "",
BurstLimit: 0,
}
}

// PriorityValueToName converts the priority value to corresponding name
func PriorityValueToName(value uint64) string {
switch value {
case LowPriorityValue:
return "LOW"
case MediumPriorityValue:
return "MEDIUM"
case HighPriorityValue:
return "HIGH"
default:
return "MEDIUM"
}
}

//revive:disable:exported
const (
LowPriorityValue = 1
MediumPriorityValue = 8
HighPriorityValue = 16
)

func (p *ResourceGroupSettings) String() string {
sb := new(strings.Builder)
if p.RURate != 0 {
writeSettingIntegerToBuilder(sb, "RU_PER_SEC", p.RURate)
}
writeSettingItemToBuilder(sb, "PRIORITY="+PriorityValueToName(p.Priority))
if len(p.CPULimiter) > 0 {
writeSettingStringToBuilder(sb, "CPU", p.CPULimiter)
}
Expand Down
Loading

0 comments on commit 61de990

Please sign in to comment.