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

[DNM]resource group: support more mode for burstable #59218

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions pkg/ddl/resource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (

const (
defaultInfosyncTimeout = 5 * time.Second
unlimitedRURate = uint64(math.MaxInt32)

// FIXME: this is a workaround for the compatibility, format the error code.
alreadyExists = "already exists"
Expand Down Expand Up @@ -201,7 +202,11 @@ func SetDirectResourceGroupSettings(groupInfo *model.ResourceGroupInfo, opt *ast
resourceGroupSettings := groupInfo.ResourceGroupSettings
switch opt.Tp {
case ast.ResourceRURate:
return SetDirectResourceGroupRUSecondOption(resourceGroupSettings, opt.UintValue, opt.BoolValue)
if opt.Burstable == ast.BurstableUnlimited {
resourceGroupSettings.RURate = unlimitedRURate
} else {
resourceGroupSettings.RURate = opt.UintValue
}
case ast.ResourcePriority:
resourceGroupSettings.Priority = opt.UintValue
case ast.ResourceUnitCPU:
Expand All @@ -210,16 +215,21 @@ func SetDirectResourceGroupSettings(groupInfo *model.ResourceGroupInfo, opt *ast
resourceGroupSettings.IOReadBandwidth = opt.StrValue
case ast.ResourceUnitIOWriteBandwidth:
resourceGroupSettings.IOWriteBandwidth = opt.StrValue
case ast.ResourceBurstableOpiton:
case ast.ResourceBurstable:
// Some about BurstLimit(b):
// - If b == 0, that means the limiter is unlimited capacity. default use in resource controller (burst with a rate within a unlimited capacity).
// - If b < 0, that means the limiter is unlimited capacity and fillrate(r) is ignored, can be seen as r == Inf (burst with a inf rate within a unlimited capacity).
// - If b > 0, that means the limiter is limited capacity. (current not used).
limit := int64(0)
if opt.BoolValue {
limit = -1
// - If b == 0, that means the limiter is unlimited capacity. default use in resource controller (burst with a rate within a unlimited capacity).
// - If b == -1, that means the limiter is unlimited capacity and fillrate(r) is ignored, can be seen as r == Inf (burst with a inf rate within a unlimited capacity).
// - If b == -2, that means the limiter is unlimited capacity and fillrate(r) is burstable, can be seen as r == n*fillrate (burst with a n times rate within a unlimited capacity).
// Note: If RU_PER_SEC=unlimted, it means unlimited whatever BURSTABLE is.
switch opt.Burstable {
case ast.BurstableUnlimited:
resourceGroupSettings.BurstLimit = -1
case ast.BurstableModerated:
resourceGroupSettings.BurstLimit = -2
default: // ast.BurstableDisable
resourceGroupSettings.BurstLimit = 0
}
resourceGroupSettings.BurstLimit = limit
case ast.ResourceGroupRunaway:
if len(opt.RunawayOptionList) == 0 {
resourceGroupSettings.Runaway = nil
Expand Down Expand Up @@ -252,17 +262,6 @@ 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 {
settings := resourceGroupSettings.Runaway
Expand Down
15 changes: 10 additions & 5 deletions pkg/executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3818,9 +3818,10 @@ func (e *memtableRetriever) setDataFromRunawayWatches(sctx sessionctx.Context) e

// used in resource_groups
const (
burstableStr = "YES"
burstdisableStr = "NO"
unlimitedFillRate = "UNLIMITED"
burstableModeratedStr = "YES(MODERATED)"
burstableUnlimitedStr = "YES(UNLIMITED)"
burstdisableStr = "NO"
unlimitedFillRate = "UNLIMITED"
)

func (e *memtableRetriever) setDataFromResourceGroups() error {
Expand Down Expand Up @@ -3903,8 +3904,12 @@ func (e *memtableRetriever) setDataFromResourceGroups() error {

switch group.Mode {
case rmpb.GroupMode_RUMode:
if group.RUSettings.RU.Settings.BurstLimit < 0 {
burstable = burstableStr
// When the burst limit is less than 0, it means burstable or unlimited.
switch group.RUSettings.RU.Settings.BurstLimit {
case -1:
burstable = burstableUnlimitedStr
case -2:
burstable = burstableModeratedStr
}
row := types.MakeDatums(
group.Name,
Expand Down
27 changes: 23 additions & 4 deletions pkg/meta/model/resource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ package model

import (
"fmt"
"math"
"strings"
"time"

"github.com/pingcap/tidb/pkg/parser/ast"
)

const unlimitedRURate = uint64(math.MaxInt32)

// ResourceGroupRunawaySettings is the runaway settings of the resource group
type ResourceGroupRunawaySettings struct {
ExecElapsedTimeMs uint64 `json:"exec_elapsed_time_ms"`
Expand Down Expand Up @@ -51,6 +54,14 @@ type ResourceGroupSettings struct {
Background *ResourceGroupBackgroundSettings `json:"background"`
}

// GetBurstLimitAdjusted returns the burst limit of the resource group after adjustment.
func (p *ResourceGroupSettings) GetBurstLimitAdjusted() int64 {
if p.RURate == unlimitedRURate {
return -1
}
return p.BurstLimit
}

// NewResourceGroupSettings creates a new ResourceGroupSettings.
func NewResourceGroupSettings() *ResourceGroupSettings {
return &ResourceGroupSettings{
Expand Down Expand Up @@ -82,9 +93,14 @@ func (p *ResourceGroupSettings) String() string {
if len(p.IOWriteBandwidth) > 0 {
writeSettingStringToBuilder(sb, "IO_WRITE_BANDWIDTH", p.IOWriteBandwidth, separatorFn)
}
// Once burst limit is negative, meaning allow burst with unlimit.
if p.BurstLimit < 0 {
writeSettingItemToBuilder(sb, "BURSTABLE", separatorFn)
// If BurstLimit is -2, it means the resource group is burstable.
// If BurstLimit is -1, it means the resource group is unlimited.
switch p.BurstLimit {
case -2:
writeSettingItemToBuilder(sb, "BURSTABLE(MODERATED)", separatorFn)
case -1:
writeSettingItemToBuilder(sb, "BURSTABLE(UNLIMITED)", separatorFn)
default:
}
if p.Runaway != nil {
fmt.Fprintf(sb, ", QUERY_LIMIT=(")
Expand Down Expand Up @@ -145,7 +161,10 @@ func (p *ResourceGroupSettings) String() 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 except burstable.
if p.BurstLimit >= 0 {
// Note: If BurstLimit is -2, it means the resource group is burstable.
// If BurstLimit is -1, it means the resource group is unlimited.
// If ru_per_sec is set to math.MaxInt32, it means the resource group is unlimited and we should not change BurstLimit.
if p.RURate != unlimitedRURate && p.BurstLimit >= 0 {
p.BurstLimit = int64(p.RURate)
}
}
Expand Down
28 changes: 21 additions & 7 deletions pkg/parser/ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
package ast

import (
"fmt"
"strings"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/pkg/parser/auth"
"github.com/pingcap/tidb/pkg/parser/format"
Expand Down Expand Up @@ -2251,7 +2248,7 @@ type ResourceGroupOption struct {
Tp ResourceUnitType
StrValue string
UintValue uint64
BoolValue bool
Burstable BurstableType
RunawayOptionList []*ResourceGroupRunawayOption
BackgroundOptions []*ResourceGroupBackgroundOption
}
Expand All @@ -2262,23 +2259,33 @@ const (
// RU mode
ResourceRURate ResourceUnitType = iota
ResourcePriority
ResourceBurstable
// Raw mode
ResourceUnitCPU
ResourceUnitIOReadBandwidth
ResourceUnitIOWriteBandwidth

// Options
ResourceBurstableOpiton
ResourceUnlimitedOption
ResourceGroupRunaway
ResourceGroupBackground
)

type BurstableType int

const (
BurstableDisable BurstableType = iota
BurstableModerated
BurstableUnlimited
)

func (n *ResourceGroupOption) Restore(ctx *format.RestoreCtx) error {
switch n.Tp {
case ResourceRURate:
ctx.WriteKeyWord("RU_PER_SEC ")
ctx.WritePlain("= ")
if n.BoolValue {
if n.Burstable == BurstableUnlimited {
ctx.WriteKeyWord("UNLIMITED")
} else {
ctx.WritePlainf("%d", n.UintValue)
Expand All @@ -2299,10 +2306,17 @@ func (n *ResourceGroupOption) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("IO_WRITE_BANDWIDTH ")
ctx.WritePlain("= ")
ctx.WriteString(n.StrValue)
case ResourceBurstableOpiton:
case ResourceBurstable:
ctx.WriteKeyWord("BURSTABLE ")
ctx.WritePlain("= ")
ctx.WritePlain(strings.ToUpper(fmt.Sprintf("%v", n.BoolValue)))
switch n.Burstable {
case BurstableDisable:
ctx.WritePlain("OFF")
case BurstableModerated:
ctx.WritePlain("MODERATED")
case BurstableUnlimited:
ctx.WritePlain("UNLIMITED")
}
case ResourceGroupRunaway:
ctx.WritePlain("QUERY_LIMIT ")
ctx.WritePlain("= ")
Expand Down
22 changes: 21 additions & 1 deletion pkg/parser/ast/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,27 @@ func TestResourceGroupDDLStmtRestore(t *testing.T) {
createTestCases := []NodeRestoreTestCase{
{
"CREATE RESOURCE GROUP IF NOT EXISTS rg1 RU_PER_SEC = 500 BURSTABLE",
"CREATE RESOURCE GROUP IF NOT EXISTS `rg1` RU_PER_SEC = 500, BURSTABLE = TRUE",
"CREATE RESOURCE GROUP IF NOT EXISTS `rg1` RU_PER_SEC = 500, BURSTABLE = UNLIMITED",
},
{
"CREATE RESOURCE GROUP IF NOT EXISTS rg1 RU_PER_SEC = 500 BURSTABLE=true",
"CREATE RESOURCE GROUP IF NOT EXISTS `rg1` RU_PER_SEC = 500, BURSTABLE = UNLIMITED",
},
{
"CREATE RESOURCE GROUP IF NOT EXISTS rg1 RU_PER_SEC = 500 BURSTABLE=false",
"CREATE RESOURCE GROUP IF NOT EXISTS `rg1` RU_PER_SEC = 500, BURSTABLE = OFF",
},
{
"CREATE RESOURCE GROUP IF NOT EXISTS rg1 RU_PER_SEC = 500 BURSTABLE=UNLIMITED",
"CREATE RESOURCE GROUP IF NOT EXISTS `rg1` RU_PER_SEC = 500, BURSTABLE = UNLIMITED",
},
{
"CREATE RESOURCE GROUP IF NOT EXISTS rg1 RU_PER_SEC = 500 BURSTABLE=MODERATED",
"CREATE RESOURCE GROUP IF NOT EXISTS `rg1` RU_PER_SEC = 500, BURSTABLE = MODERATED",
},
{
"CREATE RESOURCE GROUP IF NOT EXISTS rg1 RU_PER_SEC = 500 BURSTABLE=OFF",
"CREATE RESOURCE GROUP IF NOT EXISTS `rg1` RU_PER_SEC = 500, BURSTABLE = OFF",
},
{
"CREATE RESOURCE GROUP IF NOT EXISTS rg2 RU_PER_SEC = 600",
Expand Down
1 change: 1 addition & 0 deletions pkg/parser/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,7 @@ var tokenMap = map[string]int{
"UNKNOWN": unknown,
"UNLOCK": unlock,
"UNLIMITED": unlimited,
"MODERATED": moderated,
"UNSET": unset,
"UNSIGNED": unsigned,
"UNTIL": until,
Expand Down
Loading