Skip to content

Commit 975d177

Browse files
authored
*: add resource group name into information_schema.processlist (#40739)
close #40724
1 parent 15bac9e commit 975d177

22 files changed

+192
-148
lines changed

ddl/resource_group_test.go

+24-15
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package ddl_test
1616

1717
import (
1818
"context"
19-
"strconv"
2019
"testing"
2120

2221
"github.com/pingcap/tidb/ddl/internal/callback"
@@ -89,16 +88,17 @@ func TestResourceGroupBasic(t *testing.T) {
8988
re.Equal(uint64(2000), g.RURate)
9089
re.Equal(int64(-1), g.BurstLimit)
9190

92-
tk.MustExec("alter resource group if exists not_exists RU_PER_SEC=2000")
93-
// Check warning message
94-
res = tk.MustQuery("show warnings")
95-
res.Check(testkit.Rows("Note 8249 Unknown resource group 'not_exists'"))
91+
tk.MustQuery("select * from information_schema.resource_groups where name = 'x'").Check(testkit.Rows("x 2000 0 YES"))
9692

97-
tk.MustQuery("select * from information_schema.resource_groups where group_name = 'x' ").Check(testkit.Rows(strconv.FormatInt(g.ID, 10) + " x 2000"))
9893
tk.MustExec("drop resource group x")
9994
g = testResourceGroupNameFromIS(t, tk.Session(), "x")
10095
re.Nil(g)
10196

97+
tk.MustExec("alter resource group if exists not_exists RU_PER_SEC=2000")
98+
// Check warning message
99+
res = tk.MustQuery("show warnings")
100+
res.Check(testkit.Rows("Note 8249 Unknown resource group 'not_exists'"))
101+
102102
tk.MustExec("create resource group y " +
103103
"CPU='4000m' " +
104104
"IO_READ_BANDWIDTH='1G' " +
@@ -137,29 +137,38 @@ func TestResourceGroupBasic(t *testing.T) {
137137

138138
// Check information schema table information_schema.resource_groups
139139
tk.MustExec("create resource group x RU_PER_SEC=1000")
140-
g1 := testResourceGroupNameFromIS(t, tk.Session(), "x")
141-
tk.MustQuery("select * from information_schema.resource_groups where group_name = 'x'").Check(testkit.Rows(strconv.FormatInt(g1.ID, 10) + " x 1000"))
140+
tk.MustQuery("select * from information_schema.resource_groups where name = 'x'").Check(testkit.Rows("x 1000 0 NO"))
142141
tk.MustQuery("show create resource group x").Check(testkit.Rows("x CREATE RESOURCE GROUP `x` RU_PER_SEC=1000"))
143142

144143
tk.MustExec("create resource group y RU_PER_SEC=2000")
145-
g2 := testResourceGroupNameFromIS(t, tk.Session(), "y")
146-
tk.MustQuery("select * from information_schema.resource_groups where group_name = 'y'").Check(testkit.Rows(strconv.FormatInt(g2.ID, 10) + " y 2000"))
144+
tk.MustQuery("select * from information_schema.resource_groups where name = 'y'").Check(testkit.Rows("y 2000 0 NO"))
147145
tk.MustQuery("show create resource group y").Check(testkit.Rows("y CREATE RESOURCE GROUP `y` RU_PER_SEC=2000"))
148146

149-
tk.MustExec("alter resource group y RU_PER_SEC=4000")
150-
151-
g2 = testResourceGroupNameFromIS(t, tk.Session(), "y")
152-
tk.MustQuery("select * from information_schema.resource_groups where group_name = 'y'").Check(testkit.Rows(strconv.FormatInt(g2.ID, 10) + " y 4000"))
153-
tk.MustQuery("show create resource group y").Check(testkit.Rows("y CREATE RESOURCE GROUP `y` RU_PER_SEC=4000"))
147+
tk.MustExec("alter resource group y RU_PER_SEC=4000 BURSTABLE")
148+
tk.MustQuery("select * from information_schema.resource_groups where name = 'y'").Check(testkit.Rows("y 4000 0 YES"))
149+
tk.MustQuery("show create resource group y").Check(testkit.Rows("y CREATE RESOURCE GROUP `y` RU_PER_SEC=4000 BURSTABLE"))
154150

155151
tk.MustQuery("select count(*) from information_schema.resource_groups").Check(testkit.Rows("2"))
156152
tk.MustGetErrCode("create user usr_fail resource group nil_group", mysql.ErrResourceGroupNotExists)
153+
tk.MustContainErrMsg("create user usr_fail resource group nil_group", "Unknown resource group 'nil_group'")
157154
tk.MustExec("create user user2")
158155
tk.MustGetErrCode("alter user user2 resource group nil_group", mysql.ErrResourceGroupNotExists)
156+
tk.MustContainErrMsg("alter user user2 resource group nil_group", "Unknown resource group 'nil_group'")
157+
158+
tk.MustExec("create resource group z " +
159+
"CPU='4000m' " +
160+
"IO_READ_BANDWIDTH='1G' " +
161+
"IO_WRITE_BANDWIDTH='300M'")
162+
tk.MustQuery("show create resource group z").Check(testkit.Rows("z CREATE RESOURCE GROUP `z` CPU=\"4000m\" IO_READ_BANDWIDTH=\"1G\" IO_WRITE_BANDWIDTH=\"300M\""))
159163

160164
tk.MustExec("create resource group do_not_delete_rg ru_per_sec=100")
161165
tk.MustExec("create user usr3 resource group do_not_delete_rg")
166+
tk.MustQuery("select user_attributes from mysql.user where user = 'usr3'").Check(testkit.Rows(`{"resource_group": "do_not_delete_rg"}`))
162167
tk.MustContainErrMsg("drop resource group do_not_delete_rg", "user [usr3] depends on the resource group to drop")
168+
tk.MustExec("alter user usr3 resource group `default`")
169+
tk.MustExec("alter user usr3 resource group ``")
170+
tk.MustExec("alter user usr3 resource group `DeFault`")
171+
tk.MustQuery("select user_attributes from mysql.user where user = 'usr3'").Check(testkit.Rows(`{"resource_group": "default"}`))
163172
}
164173

165174
func testResourceGroupNameFromIS(t *testing.T, ctx sessionctx.Context, name string) *model.ResourceGroupInfo {

ddl/resourcegroup/group.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ import (
2121
"k8s.io/apimachinery/pkg/api/resource"
2222
)
2323

24-
const maxGroupNameLength = 32
24+
// MaxGroupNameLength is max length of the name of a resource group
25+
const MaxGroupNameLength = 32
2526

2627
// NewGroupFromOptions creates a new resource group from the given options.
2728
func NewGroupFromOptions(groupName string, options *model.ResourceGroupSettings) (*rmpb.ResourceGroup, error) {
2829
if options == nil {
2930
return nil, ErrInvalidGroupSettings
3031
}
31-
if len(groupName) > maxGroupNameLength {
32+
if len(groupName) > MaxGroupNameLength {
3233
return nil, ErrTooLongResourceGroupName
3334
}
3435
group := &rmpb.ResourceGroup{

domain/domain_sysvars.go

-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ func (do *Domain) setGlobalResourceControl(enable bool) {
7676
} else {
7777
variable.DisableGlobalResourceControlFunc()
7878
}
79-
logutil.BgLogger().Info("set resource control", zap.Bool("enable", enable))
8079
}
8180

8281
// updatePDClient is used to set the dynamic option into the PD client.

executor/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ go_library(
214214
"@com_github_pingcap_kvproto//pkg/encryptionpb",
215215
"@com_github_pingcap_kvproto//pkg/kvrpcpb",
216216
"@com_github_pingcap_kvproto//pkg/metapb",
217+
"@com_github_pingcap_kvproto//pkg/resource_manager",
217218
"@com_github_pingcap_kvproto//pkg/tikvpb",
218219
"@com_github_pingcap_log//:log",
219220
"@com_github_pingcap_sysutil//:sysutil",

executor/infoschema_reader.go

+31-10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/pingcap/errors"
3131
"github.com/pingcap/failpoint"
3232
"github.com/pingcap/kvproto/pkg/deadlock"
33+
rmpb "github.com/pingcap/kvproto/pkg/resource_manager"
3334
"github.com/pingcap/tidb/ddl/label"
3435
"github.com/pingcap/tidb/domain"
3536
"github.com/pingcap/tidb/domain/infosync"
@@ -185,7 +186,7 @@ func (e *memtableRetriever) retrieve(ctx context.Context, sctx sessionctx.Contex
185186
case infoschema.ClusterTableMemoryUsageOpsHistory:
186187
err = e.setDataForClusterMemoryUsageOpsHistory(sctx)
187188
case infoschema.TableResourceGroups:
188-
err = e.setDataFromResourceGroups(sctx)
189+
err = e.setDataFromResourceGroups()
189190
}
190191
if err != nil {
191192
return nil, err
@@ -3388,17 +3389,37 @@ func (e *memtableRetriever) setDataFromPlacementPolicies(sctx sessionctx.Context
33883389
return nil
33893390
}
33903391

3391-
func (e *memtableRetriever) setDataFromResourceGroups(sctx sessionctx.Context) error {
3392-
is := sessiontxn.GetTxnManager(sctx).GetTxnInfoSchema()
3393-
resourceGroups := is.AllResourceGroups()
3392+
func (e *memtableRetriever) setDataFromResourceGroups() error {
3393+
resourceGroups, err := infosync.GetAllResourceGroups(context.TODO())
3394+
if err != nil {
3395+
return errors.Errorf("failed to access resource group manager, error message is %s", err.Error())
3396+
}
33943397
rows := make([][]types.Datum, 0, len(resourceGroups))
33953398
for _, group := range resourceGroups {
3396-
row := types.MakeDatums(
3397-
group.ID,
3398-
group.Name.O,
3399-
group.RURate,
3400-
)
3401-
rows = append(rows, row)
3399+
//mode := ""
3400+
burstable := "NO"
3401+
switch group.Mode {
3402+
case rmpb.GroupMode_RUMode:
3403+
if group.RUSettings.RU.Settings.BurstLimit < 0 {
3404+
burstable = "YES"
3405+
}
3406+
row := types.MakeDatums(
3407+
group.Name,
3408+
group.RUSettings.RU.Settings.FillRate,
3409+
uint64(group.RUSettings.RU.Tokens),
3410+
burstable,
3411+
)
3412+
rows = append(rows, row)
3413+
default:
3414+
//mode = "UNKNOWN_MODE"
3415+
row := types.MakeDatums(
3416+
group.Name,
3417+
nil,
3418+
nil,
3419+
nil,
3420+
)
3421+
rows = append(rows, row)
3422+
}
34023423
}
34033424
e.rows = rows
34043425
return nil

executor/simple.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -1090,24 +1090,23 @@ func (e *SimpleExec) executeCreateUser(ctx context.Context, s *ast.CreateUserStm
10901090
userAttributes = append(userAttributes, fmt.Sprintf("\"metadata\": %s", s.CommentOrAttributeOption.Value))
10911091
}
10921092
}
1093-
resourceGroupName := "default"
1093+
10941094
if s.ResourceGroupNameOption != nil {
10951095
if !variable.EnableResourceControl.Load() {
10961096
return infoschema.ErrResourceGroupSupportDisabled
10971097
}
1098-
if s.ResourceGroupNameOption.Type == ast.UserResourceGroupName {
1099-
resourceGroupName = s.ResourceGroupNameOption.Value
1100-
}
1098+
1099+
resourceGroupName := strings.ToLower(s.ResourceGroupNameOption.Value)
11011100

11021101
// check if specified resource group exists
11031102
if resourceGroupName != "default" && resourceGroupName != "" {
11041103
_, exists := e.is.ResourceGroupByName(model.NewCIStr(resourceGroupName))
11051104
if !exists {
1106-
return infoschema.ErrResourceGroupNotExists
1105+
return infoschema.ErrResourceGroupNotExists.GenWithStackByArgs(resourceGroupName)
11071106
}
11081107
}
1108+
userAttributes = append(userAttributes, fmt.Sprintf("\"resource_group\": \"%s\"", resourceGroupName))
11091109
}
1110-
userAttributes = append(userAttributes, fmt.Sprintf("\"resource_group\": \"%s\"", resourceGroupName))
11111110
// If FAILED_LOGIN_ATTEMPTS and PASSWORD_LOCK_TIME are both specified to 0, a string of 0 length is generated.
11121111
// When inserting the attempts into json, an error occurs. This requires special handling.
11131112
if PasswordLocking != "" {
@@ -1904,20 +1903,21 @@ func (e *SimpleExec) executeAlterUser(ctx context.Context, s *ast.AlterUserStmt)
19041903
newAttributes = append(newAttributes, fmt.Sprintf(`"metadata": %s`, s.CommentOrAttributeOption.Value))
19051904
}
19061905
}
1907-
if s.ResourceGroupNameOption != nil && s.ResourceGroupNameOption.Type == ast.UserResourceGroupName {
1906+
if s.ResourceGroupNameOption != nil {
19081907
if !variable.EnableResourceControl.Load() {
19091908
return infoschema.ErrResourceGroupSupportDisabled
19101909
}
19111910

19121911
// check if specified resource group exists
1913-
if s.ResourceGroupNameOption.Value != "default" && s.ResourceGroupNameOption.Value != "" {
1914-
_, exists := e.is.ResourceGroupByName(model.NewCIStr(s.ResourceGroupNameOption.Value))
1912+
resourceGroupName := strings.ToLower(s.ResourceGroupNameOption.Value)
1913+
if resourceGroupName != "default" && s.ResourceGroupNameOption.Value != "" {
1914+
_, exists := e.is.ResourceGroupByName(model.NewCIStr(resourceGroupName))
19151915
if !exists {
1916-
return infoschema.ErrResourceGroupNotExists
1916+
return infoschema.ErrResourceGroupNotExists.GenWithStackByArgs(resourceGroupName)
19171917
}
19181918
}
19191919

1920-
newAttributes = append(newAttributes, fmt.Sprintf(`"resource_group": "%s"`, s.ResourceGroupNameOption.Value))
1920+
newAttributes = append(newAttributes, fmt.Sprintf(`"resource_group": "%s"`, resourceGroupName))
19211921
}
19221922
if passwordLockingStr != "" {
19231923
newAttributes = append(newAttributes, passwordLockingStr)

executor/simple_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ func TestUserAttributes(t *testing.T) {
9494
_, err := rootTK.Exec(`CREATE USER testuser2 ATTRIBUTE '{"name": "Tom", age: 19}'`)
9595
rootTK.MustExec(`CREATE USER testuser2`)
9696
require.Error(t, err)
97-
rootTK.MustQuery(`SELECT user_attributes FROM mysql.user WHERE user = 'testuser'`).Check(testkit.Rows(`{"metadata": {"comment": "1234"}, "resource_group": "default"}`))
98-
rootTK.MustQuery(`SELECT user_attributes FROM mysql.user WHERE user = 'testuser1'`).Check(testkit.Rows(`{"metadata": {"age": 19, "name": "Tom"}, "resource_group": "default"}`))
99-
rootTK.MustQuery(`SELECT user_attributes FROM mysql.user WHERE user = 'testuser2'`).Check(testkit.Rows(`{"resource_group": "default"}`))
97+
rootTK.MustQuery(`SELECT user_attributes FROM mysql.user WHERE user = 'testuser'`).Check(testkit.Rows(`{"metadata": {"comment": "1234"}}`))
98+
rootTK.MustQuery(`SELECT user_attributes FROM mysql.user WHERE user = 'testuser1'`).Check(testkit.Rows(`{"metadata": {"age": 19, "name": "Tom"}}`))
99+
rootTK.MustQuery(`SELECT user_attributes FROM mysql.user WHERE user = 'testuser2'`).Check(testkit.Rows(`{}`))
100100
rootTK.MustQueryWithContext(ctx, `SELECT attribute FROM information_schema.user_attributes WHERE user = 'testuser'`).Check(testkit.Rows(`{"comment": "1234"}`))
101101
rootTK.MustQueryWithContext(ctx, `SELECT attribute FROM information_schema.user_attributes WHERE user = 'testuser1'`).Check(testkit.Rows(`{"age": 19, "name": "Tom"}`))
102102
rootTK.MustQueryWithContext(ctx, `SELECT attribute->>"$.age" AS age, attribute->>"$.name" AS name FROM information_schema.user_attributes WHERE user = 'testuser1'`).Check(testkit.Rows(`19 Tom`))
@@ -127,7 +127,7 @@ func TestUserAttributes(t *testing.T) {
127127
// https://github.com/pingcap/tidb/issues/39207
128128
rootTK.MustExec("create user usr1@'%' identified by 'passord'")
129129
rootTK.MustExec("alter user usr1 comment 'comment1'")
130-
rootTK.MustQuery("select user_attributes from mysql.user where user = 'usr1'").Check(testkit.Rows(`{"metadata": {"comment": "comment1"}, "resource_group": "default"}`))
130+
rootTK.MustQuery("select user_attributes from mysql.user where user = 'usr1'").Check(testkit.Rows(`{"metadata": {"comment": "comment1"}}`))
131131
rootTK.MustExec("set global tidb_enable_resource_control = 'on'")
132132
rootTK.MustExec("CREATE RESOURCE GROUP rg1 ru_per_sec = 100")
133133
rootTK.MustExec("alter user usr1 resource group rg1")

0 commit comments

Comments
 (0)