Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp committed Jul 10, 2023
1 parent 5a34eab commit 2b62752
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 19 deletions.
8 changes: 7 additions & 1 deletion client/resource_group/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1062,8 +1062,14 @@ func (gc *groupCostController) isBackgroundRequest(requestResource string) bool

if gc.meta.BackgroundSettings != nil {
jobTypes := gc.meta.BackgroundSettings.JobTypes
jobTypesMap := make(map[string]struct{})
for _, jobType := range jobTypes {
if strings.Contains(requestResource, jobType) {
jobTypesMap[jobType] = struct{}{}
}

parts := strings.Split(requestResource, "_")
for _, part := range parts {
if _, ok := jobTypesMap[part]; ok {
return true
}
}
Expand Down
13 changes: 13 additions & 0 deletions client/resource_group/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func createTestGroupCostController(re *require.Assertions) *groupCostController
},
},
},
BackgroundSettings: &rmpb.BackgroundSettings{
JobTypes: []string{"lightning", "br"},
},
}
ch1 := make(chan struct{})
ch2 := make(chan *groupCostController)
Expand Down Expand Up @@ -112,3 +115,13 @@ func TestRequestAndResponseConsumption(t *testing.T) {
re.Equal(expectedConsumption.TotalCpuTimeMs, consumption.TotalCpuTimeMs, caseNum)
}
}

func TestIsBackgroundRequest(t *testing.T) {
re := require.New(t)
gc := createTestGroupCostController(re)
re.False(gc.isBackgroundRequest("test"))
re.False(gc.isBackgroundRequest("unknown_default"))
re.True(gc.isBackgroundRequest("internal_lightning"))
re.False(gc.isBackgroundRequest("external_test_default"))
re.True(gc.isBackgroundRequest("external_unknown_lightning"))
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,4 @@ replace google.golang.org/grpc v1.54.0 => google.golang.org/grpc v1.26.0
// kvproto at the same time. You can run `go mod tidy` to make it replaced with go-mod style specification.
// After the PR to kvproto is merged, remember to comment this out and run `go mod tidy`.
// replace github.com/pingcap/kvproto => github.com/$YourPrivateRepo $YourPrivateBranch
replace github.com/pingcap/kvproto => github.com/HuSharp/kvproto v0.0.0-20230706083243-12b30a04ec23
replace github.com/pingcap/kvproto => github.com/HuSharp/kvproto v0.0.0-20230710073438-f629a3cd43e9
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/AlekSi/gocov-xml v1.0.0 h1:4QctJBgXEkbzeKz6PJy6bt3JSPNSN4I2mITYW+eKUo
github.com/AlekSi/gocov-xml v1.0.0/go.mod h1:J0qYeZ6tDg4oZubW9mAAgxlqw39PDfoEkzB3HXSbEuA=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/HuSharp/kvproto v0.0.0-20230706083243-12b30a04ec23 h1:XCDwezErjCtqcfnXBbbeJ/oN3zIDAFLsZzdnLf328tg=
github.com/HuSharp/kvproto v0.0.0-20230706083243-12b30a04ec23/go.mod h1:r0q/CFcwvyeRhKtoqzmWMBebrtpIziQQ9vR+JKh1knc=
github.com/HuSharp/kvproto v0.0.0-20230710073438-f629a3cd43e9 h1:rla173qjbLMBBEAU8nXSEzDxdTpjjYBusOgqAVSr9dc=
github.com/HuSharp/kvproto v0.0.0-20230710073438-f629a3cd43e9/go.mod h1:r0q/CFcwvyeRhKtoqzmWMBebrtpIziQQ9vR+JKh1knc=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
Expand Down
5 changes: 3 additions & 2 deletions pkg/mcs/resourcemanager/server/grpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (s *Service) AcquireTokenBuckets(stream rmpb.ResourceManager_AcquireTokenBu
}
targetPeriodMs := request.GetTargetRequestPeriodMs()
clientUniqueID := request.GetClientUniqueId()
isBackground := request.GetBackground()
isBackground := request.GetIsBackground()
resps := &rmpb.TokenBucketsResponse{}
for _, req := range request.Requests {
resourceGroupName := req.GetResourceGroupName()
Expand All @@ -194,7 +194,8 @@ func (s *Service) AcquireTokenBuckets(stream rmpb.ResourceManager_AcquireTokenBu
s.manager.consumptionDispatcher <- struct {
resourceGroupName string
*rmpb.Consumption
}{resourceGroupName, req.GetConsumptionSinceLastRequest()}
isBackground bool
}{resourceGroupName, req.GetConsumptionSinceLastRequest(), isBackground}
if isBackground {
continue
}
Expand Down
17 changes: 16 additions & 1 deletion pkg/mcs/resourcemanager/server/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Manager struct {
consumptionDispatcher chan struct {
resourceGroupName string
*rmpb.Consumption
isBackground bool
}
// record update time of each resource group
consumptionRecord map[string]time.Time
Expand All @@ -76,6 +77,7 @@ func NewManager[T ResourceManagerConfigProvider](srv bs.Server) *Manager {
consumptionDispatcher: make(chan struct {
resourceGroupName string
*rmpb.Consumption
isBackground bool
}, defaultConsumptionChanSize),
consumptionRecord: make(map[string]time.Time),
}
Expand Down Expand Up @@ -297,8 +299,19 @@ func (m *Manager) backgroundMetricsFlush(ctx context.Context) {
if consumption == nil {
continue
}

name := consumptionInfo.resourceGroupName
if consumptionInfo.isBackground {
log.Info("background metrics flush", zap.String("resource group", name))
backgroundRruMetrics := backgroundReadRequestUnitCost.WithLabelValues(name)
backgroundWruMetrics := backgroundWriteRequestUnitCost.WithLabelValues(name)
backgroundRruMetrics.Add(consumption.RRU)
backgroundWruMetrics.Add(consumption.WRU)
m.consumptionRecord[name] = time.Now()
continue
}

var (
name = consumptionInfo.resourceGroupName
rruMetrics = readRequestUnitCost.WithLabelValues(name)
wruMetrics = writeRequestUnitCost.WithLabelValues(name)
sqlLayerRuMetrics = sqlLayerRequestUnitCost.WithLabelValues(name)
Expand Down Expand Up @@ -355,6 +368,8 @@ func (m *Manager) backgroundMetricsFlush(ctx context.Context) {
requestCount.DeleteLabelValues(name, readTypeLabel)
requestCount.DeleteLabelValues(name, writeTypeLabel)
availableRUCounter.DeleteLabelValues(name)
backgroundReadRequestUnitCost.DeleteLabelValues(name)
backgroundWriteRequestUnitCost.DeleteLabelValues(name)
delete(m.consumptionRecord, name)
}
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/mcs/resourcemanager/server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,22 @@ var (
Name: "available_ru",
Help: "Counter of the available RU for all resource groups.",
}, []string{resourceGroupNameLabel})

// Background RU cost metrics.
backgroundReadRequestUnitCost = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: ruSubsystem,
Name: "background_read_request_unit",
Help: "Counter of the read request unit cost for all background resource groups.",
}, []string{resourceGroupNameLabel})
backgroundWriteRequestUnitCost = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: ruSubsystem,
Name: "background_write_request_unit",
Help: "Counter of the write request unit cost for all background resource groups.",
}, []string{resourceGroupNameLabel})
)

func init() {
Expand All @@ -117,4 +133,6 @@ func init() {
prometheus.MustRegister(sqlCPUCost)
prometheus.MustRegister(requestCount)
prometheus.MustRegister(availableRUCounter)
prometheus.MustRegister(backgroundReadRequestUnitCost)
prometheus.MustRegister(backgroundWriteRequestUnitCost)
}
2 changes: 1 addition & 1 deletion tests/integrations/client/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,4 @@ require (
sigs.k8s.io/yaml v1.2.0 // indirect
)

replace github.com/pingcap/kvproto => github.com/HuSharp/kvproto v0.0.0-20230706083243-12b30a04ec23
replace github.com/pingcap/kvproto => github.com/HuSharp/kvproto v0.0.0-20230710073438-f629a3cd43e9
4 changes: 2 additions & 2 deletions tests/integrations/client/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/HuSharp/kvproto v0.0.0-20230706083243-12b30a04ec23 h1:XCDwezErjCtqcfnXBbbeJ/oN3zIDAFLsZzdnLf328tg=
github.com/HuSharp/kvproto v0.0.0-20230706083243-12b30a04ec23/go.mod h1:r0q/CFcwvyeRhKtoqzmWMBebrtpIziQQ9vR+JKh1knc=
github.com/HuSharp/kvproto v0.0.0-20230710073438-f629a3cd43e9 h1:rla173qjbLMBBEAU8nXSEzDxdTpjjYBusOgqAVSr9dc=
github.com/HuSharp/kvproto v0.0.0-20230710073438-f629a3cd43e9/go.mod h1:r0q/CFcwvyeRhKtoqzmWMBebrtpIziQQ9vR+JKh1knc=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/mcs/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,4 @@ require (
sigs.k8s.io/yaml v1.2.0 // indirect
)

replace github.com/pingcap/kvproto => github.com/HuSharp/kvproto v0.0.0-20230706083243-12b30a04ec23
replace github.com/pingcap/kvproto => github.com/HuSharp/kvproto v0.0.0-20230710073438-f629a3cd43e9
4 changes: 2 additions & 2 deletions tests/integrations/mcs/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/HuSharp/kvproto v0.0.0-20230706083243-12b30a04ec23 h1:XCDwezErjCtqcfnXBbbeJ/oN3zIDAFLsZzdnLf328tg=
github.com/HuSharp/kvproto v0.0.0-20230706083243-12b30a04ec23/go.mod h1:r0q/CFcwvyeRhKtoqzmWMBebrtpIziQQ9vR+JKh1knc=
github.com/HuSharp/kvproto v0.0.0-20230710073438-f629a3cd43e9 h1:rla173qjbLMBBEAU8nXSEzDxdTpjjYBusOgqAVSr9dc=
github.com/HuSharp/kvproto v0.0.0-20230710073438-f629a3cd43e9/go.mod h1:r0q/CFcwvyeRhKtoqzmWMBebrtpIziQQ9vR+JKh1knc=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/tso/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,4 @@ require (
sigs.k8s.io/yaml v1.2.0 // indirect
)

replace github.com/pingcap/kvproto => github.com/HuSharp/kvproto v0.0.0-20230706083243-12b30a04ec23
replace github.com/pingcap/kvproto => github.com/HuSharp/kvproto v0.0.0-20230710073438-f629a3cd43e9
4 changes: 2 additions & 2 deletions tests/integrations/tso/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/HuSharp/kvproto v0.0.0-20230706083243-12b30a04ec23 h1:XCDwezErjCtqcfnXBbbeJ/oN3zIDAFLsZzdnLf328tg=
github.com/HuSharp/kvproto v0.0.0-20230706083243-12b30a04ec23/go.mod h1:r0q/CFcwvyeRhKtoqzmWMBebrtpIziQQ9vR+JKh1knc=
github.com/HuSharp/kvproto v0.0.0-20230710073438-f629a3cd43e9 h1:rla173qjbLMBBEAU8nXSEzDxdTpjjYBusOgqAVSr9dc=
github.com/HuSharp/kvproto v0.0.0-20230710073438-f629a3cd43e9/go.mod h1:r0q/CFcwvyeRhKtoqzmWMBebrtpIziQQ9vR+JKh1knc=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
Expand Down
2 changes: 1 addition & 1 deletion tools/pd-tso-bench/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ require (

replace github.com/tikv/pd/client => ../../client

replace github.com/pingcap/kvproto => github.com/HuSharp/kvproto v0.0.0-20230706083243-12b30a04ec23
replace github.com/pingcap/kvproto => github.com/HuSharp/kvproto v0.0.0-20230710073438-f629a3cd43e9
4 changes: 2 additions & 2 deletions tools/pd-tso-bench/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,8 @@ gioui.org v0.0.0-20210308172011-57750fc8a0a6/go.mod h1:RSH6KIUZ0p2xy5zHDxgAM4zum
git.sr.ht/~sbinet/gg v0.3.1/go.mod h1:KGYtlADtqsqANL9ueOFkWymvzUvLMQllU5Ixo+8v3pc=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/HuSharp/kvproto v0.0.0-20230706083243-12b30a04ec23 h1:XCDwezErjCtqcfnXBbbeJ/oN3zIDAFLsZzdnLf328tg=
github.com/HuSharp/kvproto v0.0.0-20230706083243-12b30a04ec23/go.mod h1:r0q/CFcwvyeRhKtoqzmWMBebrtpIziQQ9vR+JKh1knc=
github.com/HuSharp/kvproto v0.0.0-20230710073438-f629a3cd43e9 h1:rla173qjbLMBBEAU8nXSEzDxdTpjjYBusOgqAVSr9dc=
github.com/HuSharp/kvproto v0.0.0-20230710073438-f629a3cd43e9/go.mod h1:r0q/CFcwvyeRhKtoqzmWMBebrtpIziQQ9vR+JKh1knc=
github.com/JohnCGriffin/overflow v0.0.0-20211019200055-46fa312c352c/go.mod h1:X0CRv0ky0k6m906ixxpzmDRLvX58TFUKS2eePweuyxk=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/ajstarks/deck v0.0.0-20200831202436-30c9fc6549a9/go.mod h1:JynElWSGnm/4RlzPXRlREEwqTHAN3T56Bv2ITsFT3gY=
Expand Down

0 comments on commit 2b62752

Please sign in to comment.