Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl: prevent dropping in-use resource group #40716

Merged
merged 10 commits into from
Jan 19, 2023
14 changes: 14 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7668,6 +7668,20 @@ func (d *ddl) DropResourceGroup(ctx sessionctx.Context, stmt *ast.DropResourceGr
return err
}

// check to see if some user has dependency on the group
exec := ctx.(sqlexec.RestrictedSQLExecutor)
internalCtx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnDDL)
sql := "select user from mysql.user where lower(json_extract(user_attributes, '$.resource_group')) ='\"" + groupName.L + "\"' limit 1"
BornChanger marked this conversation as resolved.
Show resolved Hide resolved
rows, _, err := exec.ExecRestrictedSQL(internalCtx, nil, sql)
if err != nil {
err = errors.Errorf("system table mysql.user access failed")
return err
}
if len(rows) != 0 {
err = errors.Errorf("user [%s] depends on the resource group to drop", rows[0].GetString(0))
return err
}

job := &model.Job{
SchemaID: group.ID,
SchemaName: group.Name.L,
Expand Down
4 changes: 4 additions & 0 deletions ddl/resource_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ func TestResourceGroupBasic(t *testing.T) {
tk.MustGetErrCode("create user usr_fail resource group nil_group", mysql.ErrResourceGroupNotExists)
tk.MustExec("create user user2")
tk.MustGetErrCode("alter user user2 resource group nil_group", mysql.ErrResourceGroupNotExists)

tk.MustExec("create resource group do_not_delete_rg rru_per_sec=100 wru_per_sec=200")
tk.MustExec("create user usr3 resource group do_not_delete_rg")
tk.MustContainErrMsg("drop resource group do_not_delete_rg", "user [usr3] depends on the resource group to drop")
}

func testResourceGroupNameFromIS(t *testing.T, ctx sessionctx.Context, name string) *model.ResourceGroupInfo {
Expand Down
2 changes: 1 addition & 1 deletion errno/errname.go
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ var MySQLErrName = map[uint16]*mysql.ErrMessage{
ErrResourceGroupNotExists: mysql.Message("Unknown resource group '%-.192s'", nil),

ErrColumnInChange: mysql.Message("column %s id %d does not exist, this column may have been updated by other DDL ran in parallel", nil),
ErrResourceGroupSupportDisabled: mysql.Message("Resource group feature is disabled", nil),
ErrResourceGroupSupportDisabled: mysql.Message("Resource control feature is disabled. Run `SET GLOBAL tidb_enable_resource_control='on'` to enable the feature", nil),

// TiKV/PD errors.
ErrPDServerTimeout: mysql.Message("PD server timeout: %s", nil),
Expand Down