From 6f55148dd94f8527e4b668bc47d78010ca1b5b84 Mon Sep 17 00:00:00 2001 From: Cabinfever_B Date: Wed, 8 Feb 2023 01:04:43 +0800 Subject: [PATCH 1/3] remove ru token and set brust limit Signed-off-by: Cabinfever_B --- ddl/resource_group_test.go | 8 ++++---- ddl/resourcegroup/group.go | 20 ++++++++++++++++---- executor/infoschema_reader.go | 2 -- infoschema/tables.go | 1 - 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/ddl/resource_group_test.go b/ddl/resource_group_test.go index a0921037c475d..2627e93415d5e 100644 --- a/ddl/resource_group_test.go +++ b/ddl/resource_group_test.go @@ -88,7 +88,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 0 YES")) + tk.MustQuery("select * from information_schema.resource_groups where name = 'x'").Check(testkit.Rows("x 2000 YES")) tk.MustExec("drop resource group x") g = testResourceGroupNameFromIS(t, tk.Session(), "x") @@ -137,15 +137,15 @@ func TestResourceGroupBasic(t *testing.T) { // 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 0 NO")) + tk.MustQuery("select * from information_schema.resource_groups where name = 'x'").Check(testkit.Rows("x 1000 NO")) tk.MustQuery("show create resource group x").Check(testkit.Rows("x CREATE RESOURCE GROUP `x` RU_PER_SEC=1000")) tk.MustExec("create resource group y RU_PER_SEC=2000") - tk.MustQuery("select * from information_schema.resource_groups where name = 'y'").Check(testkit.Rows("y 2000 0 NO")) + tk.MustQuery("select * from information_schema.resource_groups where name = 'y'").Check(testkit.Rows("y 2000 NO")) tk.MustQuery("show create resource group y").Check(testkit.Rows("y CREATE RESOURCE GROUP `y` RU_PER_SEC=2000")) 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 0 YES")) + 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 count(*) from information_schema.resource_groups").Check(testkit.Rows("2")) diff --git a/ddl/resourcegroup/group.go b/ddl/resourcegroup/group.go index 17c4a0afd407a..640986c1ba88b 100644 --- a/ddl/resourcegroup/group.go +++ b/ddl/resourcegroup/group.go @@ -39,11 +39,15 @@ func NewGroupFromOptions(groupName string, options *model.ResourceGroupSettings) if options.RURate > 0 { isRUMode = true group.Mode = rmpb.GroupMode_RUMode + burstLimit := options.BurstLimit + if burstLimit >= 0 { + burstLimit = int64(options.RURate) + } group.RUSettings = &rmpb.GroupRequestUnitSettings{ RU: &rmpb.TokenBucket{ Settings: &rmpb.TokenLimitSettings{ FillRate: options.RURate, - BurstLimit: options.BurstLimit, + BurstLimit: burstLimit, }, }, } @@ -76,23 +80,31 @@ func NewGroupFromOptions(groupName string, options *model.ResourceGroupSettings) } group.Mode = rmpb.GroupMode_RawMode + cpuBurst := options.BurstLimit + ioReadBurst := options.BurstLimit + ioWriteBurst := options.BurstLimit + if options.BurstLimit >= 0 { + cpuBurst = int64(cpuRate) + ioReadBurst = int64(ioReadRate) + ioWriteBurst = int64(ioWriteRate) + } group.RawResourceSettings = &rmpb.GroupRawResourceSettings{ Cpu: &rmpb.TokenBucket{ Settings: &rmpb.TokenLimitSettings{ FillRate: cpuRate, - BurstLimit: options.BurstLimit, + BurstLimit: cpuBurst, }, }, IoRead: &rmpb.TokenBucket{ Settings: &rmpb.TokenLimitSettings{ FillRate: ioReadRate, - BurstLimit: options.BurstLimit, + BurstLimit: ioReadBurst, }, }, IoWrite: &rmpb.TokenBucket{ Settings: &rmpb.TokenLimitSettings{ FillRate: ioWriteRate, - BurstLimit: options.BurstLimit, + BurstLimit: ioWriteBurst, }, }, } diff --git a/executor/infoschema_reader.go b/executor/infoschema_reader.go index 39666333762ab..00efce5ae36e7 100644 --- a/executor/infoschema_reader.go +++ b/executor/infoschema_reader.go @@ -3406,7 +3406,6 @@ func (e *memtableRetriever) setDataFromResourceGroups() error { row := types.MakeDatums( group.Name, group.RUSettings.RU.Settings.FillRate, - uint64(group.RUSettings.RU.Tokens), burstable, ) rows = append(rows, row) @@ -3416,7 +3415,6 @@ func (e *memtableRetriever) setDataFromResourceGroups() error { group.Name, nil, nil, - nil, ) rows = append(rows, row) } diff --git a/infoschema/tables.go b/infoschema/tables.go index 3cbf9ee1b464f..7a2631b4abe13 100644 --- a/infoschema/tables.go +++ b/infoschema/tables.go @@ -1595,7 +1595,6 @@ 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: "RU_TOKENS", tp: mysql.TypeLonglong, size: 21}, {name: "BURSTABLE", tp: mysql.TypeVarchar, size: 3}, } From ed33da0e5ad132268cd905b11719295113cae3df Mon Sep 17 00:00:00 2001 From: Cabinfever_B Date: Wed, 8 Feb 2023 15:08:06 +0800 Subject: [PATCH 2/3] refactor Signed-off-by: Cabinfever_B --- ddl/ddl_api.go | 12 +++++------- ddl/resourcegroup/group.go | 6 +----- parser/model/model.go | 8 ++++++++ 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 431ff16a2c403..d67e0401243b0 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -7615,14 +7615,11 @@ func checkIgnorePlacementDDL(ctx sessionctx.Context) bool { // AddResourceGroup implements the DDL interface, creates a resource group. func (d *ddl) AddResourceGroup(ctx sessionctx.Context, stmt *ast.CreateResourceGroupStmt) (err error) { - groupInfo := &model.ResourceGroupInfo{ResourceGroupSettings: &model.ResourceGroupSettings{}} groupName := stmt.ResourceGroupName - groupInfo.Name = groupName - for _, opt := range stmt.ResourceGroupOptionList { - err := SetDirectResourceGroupUnit(groupInfo.ResourceGroupSettings, opt.Tp, opt.StrValue, opt.UintValue, opt.BoolValue) - if err != nil { - return err - } + groupInfo := &model.ResourceGroupInfo{Name: groupName, ResourceGroupSettings: &model.ResourceGroupSettings{}} + groupInfo, err = buildResourceGroup(groupInfo, stmt.ResourceGroupOptionList) + if err != nil { + return err } if _, ok := d.GetInfoSchemaWithInterceptor(ctx).ResourceGroupByName(groupName); ok { @@ -7711,6 +7708,7 @@ func buildResourceGroup(oldGroup *model.ResourceGroupInfo, options []*ast.Resour return nil, err } } + groupInfo.ResourceGroupSettings.Adjust() return groupInfo, nil } diff --git a/ddl/resourcegroup/group.go b/ddl/resourcegroup/group.go index 1d541bac424a4..6e371a501d3ab 100644 --- a/ddl/resourcegroup/group.go +++ b/ddl/resourcegroup/group.go @@ -35,15 +35,11 @@ func NewGroupFromOptions(groupName string, options *model.ResourceGroupSettings) } if options.RURate > 0 { group.Mode = rmpb.GroupMode_RUMode - burstLimit := options.BurstLimit - if burstLimit >= 0 { - burstLimit = int64(options.RURate) - } group.RUSettings = &rmpb.GroupRequestUnitSettings{ RU: &rmpb.TokenBucket{ Settings: &rmpb.TokenLimitSettings{ FillRate: options.RURate, - BurstLimit: burstLimit, + BurstLimit: options.BurstLimit, }, }, } diff --git a/parser/model/model.go b/parser/model/model.go index b953b1140bf7b..fdd92fc094ac9 100644 --- a/parser/model/model.go +++ b/parser/model/model.go @@ -1874,6 +1874,14 @@ func (p *ResourceGroupSettings) String() string { return sb.String() } +// Adjust adjusts the resource group settings. +func (p *ResourceGroupSettings) Adjust() { + // Curretly we only support ru_per_sec sytanx, so BurstLimit(capicity) is always same as ru_per_sec. + if p.BurstLimit == 0 { + p.BurstLimit = int64(p.RURate) + } +} + // Clone clones the resource group settings. func (p *ResourceGroupSettings) Clone() *ResourceGroupSettings { cloned := *p From 41774a26786a2535bc9d10dd6372609c44d92664 Mon Sep 17 00:00:00 2001 From: Cabinfever_B Date: Wed, 8 Feb 2023 20:57:25 +0800 Subject: [PATCH 3/3] fix a parser bug Signed-off-by: Cabinfever_B --- ddl/resource_group_test.go | 6 +++++- parser/parser.go | 10 ++++++---- parser/parser.y | 14 ++++++++------ 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/ddl/resource_group_test.go b/ddl/resource_group_test.go index ad7f8dd00d2ea..162a3ac233a05 100644 --- a/ddl/resource_group_test.go +++ b/ddl/resource_group_test.go @@ -104,7 +104,7 @@ func TestResourceGroupBasic(t *testing.T) { require.Equal(t, "y", groupInfo.Name.L) require.Equal(t, groupID, groupInfo.ID) require.Equal(t, uint64(4000), groupInfo.RURate) - require.Equal(t, int64(0), groupInfo.BurstLimit) + require.Equal(t, int64(4000), groupInfo.BurstLimit) } g = testResourceGroupNameFromIS(t, tk.Session(), "y") checkFunc(g) @@ -126,6 +126,10 @@ func TestResourceGroupBasic(t *testing.T) { tk.MustContainErrMsg("create resource group x ru_per_sec=1000 ru_per_sec=200, ru_per_sec=300", "Dupliated options specified") tk.MustGetErrCode("create resource group x burstable, burstable", mysql.ErrParse) tk.MustContainErrMsg("create resource group x burstable, burstable", "Dupliated options specified") + tk.MustGetErrCode("create resource group x ru_per_sec=1000, burstable, burstable", mysql.ErrParse) + tk.MustContainErrMsg("create resource group x ru_per_sec=1000, burstable, burstable", "Dupliated options specified") + tk.MustGetErrCode("create resource group x burstable, ru_per_sec=1000, burstable", mysql.ErrParse) + tk.MustContainErrMsg("create resource group x burstable, ru_per_sec=1000, burstable", "Dupliated options specified") groups, err := infosync.ListResourceGroups(context.TODO()) require.Equal(t, 0, len(groups)) require.NoError(t, err) diff --git a/parser/parser.go b/parser/parser.go index b51c7d031af09..ae9c3c5c5a373 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -11960,19 +11960,21 @@ yynewstate: } case 10: { - parser.yyVAL.item = append(yyS[yypt-1].item.([]*ast.ResourceGroupOption), yyS[yypt-0].item.(*ast.ResourceGroupOption)) - if yyS[yypt-1].item.([]*ast.ResourceGroupOption)[0].Tp == yyS[yypt-0].item.(*ast.ResourceGroupOption).Tp { + if yyS[yypt-1].item.([]*ast.ResourceGroupOption)[0].Tp == yyS[yypt-0].item.(*ast.ResourceGroupOption).Tp || + (len(yyS[yypt-1].item.([]*ast.ResourceGroupOption)) > 1 && yyS[yypt-1].item.([]*ast.ResourceGroupOption)[1].Tp == yyS[yypt-0].item.(*ast.ResourceGroupOption).Tp) { yylex.AppendError(yylex.Errorf("Dupliated options specified")) return 1 } + parser.yyVAL.item = append(yyS[yypt-1].item.([]*ast.ResourceGroupOption), yyS[yypt-0].item.(*ast.ResourceGroupOption)) } case 11: { - parser.yyVAL.item = append(yyS[yypt-2].item.([]*ast.ResourceGroupOption), yyS[yypt-0].item.(*ast.ResourceGroupOption)) - if yyS[yypt-2].item.([]*ast.ResourceGroupOption)[0].Tp == yyS[yypt-0].item.(*ast.ResourceGroupOption).Tp { + if yyS[yypt-2].item.([]*ast.ResourceGroupOption)[0].Tp == yyS[yypt-0].item.(*ast.ResourceGroupOption).Tp || + (len(yyS[yypt-2].item.([]*ast.ResourceGroupOption)) > 1 && yyS[yypt-2].item.([]*ast.ResourceGroupOption)[1].Tp == yyS[yypt-0].item.(*ast.ResourceGroupOption).Tp) { yylex.AppendError(yylex.Errorf("Dupliated options specified")) return 1 } + parser.yyVAL.item = append(yyS[yypt-2].item.([]*ast.ResourceGroupOption), yyS[yypt-0].item.(*ast.ResourceGroupOption)) } case 12: { diff --git a/parser/parser.y b/parser/parser.y index 2e19a41723d63..e62872834f450 100644 --- a/parser/parser.y +++ b/parser/parser.y @@ -1598,19 +1598,21 @@ ResourceGroupOptionList: } | ResourceGroupOptionList DirectResourceGroupOption { - $$ = append($1.([]*ast.ResourceGroupOption), $2.(*ast.ResourceGroupOption)) - if $1.([]*ast.ResourceGroupOption)[0].Tp == $2.(*ast.ResourceGroupOption).Tp { + if $1.([]*ast.ResourceGroupOption)[0].Tp == $2.(*ast.ResourceGroupOption).Tp || + (len($1.([]*ast.ResourceGroupOption)) > 1 && $1.([]*ast.ResourceGroupOption)[1].Tp == $2.(*ast.ResourceGroupOption).Tp) { yylex.AppendError(yylex.Errorf("Dupliated options specified")) return 1 } + $$ = append($1.([]*ast.ResourceGroupOption), $2.(*ast.ResourceGroupOption)) } | ResourceGroupOptionList ',' DirectResourceGroupOption { + if $1.([]*ast.ResourceGroupOption)[0].Tp == $3.(*ast.ResourceGroupOption).Tp || + (len($1.([]*ast.ResourceGroupOption)) > 1 && $1.([]*ast.ResourceGroupOption)[1].Tp == $3.(*ast.ResourceGroupOption).Tp) { + yylex.AppendError(yylex.Errorf("Dupliated options specified")) + return 1 + } $$ = append($1.([]*ast.ResourceGroupOption), $3.(*ast.ResourceGroupOption)) - if $1.([]*ast.ResourceGroupOption)[0].Tp == $3.(*ast.ResourceGroupOption).Tp { - yylex.AppendError(yylex.Errorf("Dupliated options specified")) - return 1 - } } DirectResourceGroupOption: