Skip to content

Commit

Permalink
label ru with role
Browse files Browse the repository at this point in the history
Signed-off-by: AmoebaProtozoa <[email protected]>
  • Loading branch information
AmoebaProtozoa committed Jan 2, 2024
1 parent 3ef40bd commit dc4d167
Show file tree
Hide file tree
Showing 13 changed files with 3,351 additions and 61 deletions.
2 changes: 2 additions & 0 deletions client/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ require (
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/pingcap/kvproto => github.com/AmoebaProtozoa/kvproto v0.0.0-20240102100144-34b417c2a46f
4 changes: 2 additions & 2 deletions client/go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/AmoebaProtozoa/kvproto v0.0.0-20240102100144-34b417c2a46f h1:dqMp5cZXoEDwouHEBwd2PEd0hozuEJtj4IAAPFR+q3A=
github.com/AmoebaProtozoa/kvproto v0.0.0-20240102100144-34b417c2a46f/go.mod h1:rXxWk2UnwfUhLXha1jxRWPADw9eMZGWEWCg92Tgmb/8=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down Expand Up @@ -94,8 +96,6 @@ github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c h1:xpW9bvK+HuuTm
github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c/go.mod h1:X2r9ueLEUZgtx2cIogM0v4Zj5uvvzhuuiu7Pn8HzMPg=
github.com/pingcap/failpoint v0.0.0-20210918120811-547c13e3eb00 h1:C3N3itkduZXDZFh4N3vQ5HEtld3S+Y+StULhWVvumU0=
github.com/pingcap/failpoint v0.0.0-20210918120811-547c13e3eb00/go.mod h1:4qGtCB0QK0wBzKtFEGDhxXnSnbQApw1gc9siScUl8ew=
github.com/pingcap/kvproto v0.0.0-20231222062942-c0c73f41d0b2 h1:364A6VCS+l0oHBKZKotX9LzmfEtIO/NTccTIQcPp3Ug=
github.com/pingcap/kvproto v0.0.0-20231222062942-c0c73f41d0b2/go.mod h1:rXxWk2UnwfUhLXha1jxRWPADw9eMZGWEWCg92Tgmb/8=
github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3 h1:HR/ylkkLmGdSSDaD8IDP+SZrdhV1Kibl9KrHxJ9eciw=
github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
1 change: 1 addition & 0 deletions client/resource_group/controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ type RUConfig struct {
// some config for client
LTBMaxWaitDuration time.Duration
DegradedModeWaitDuration time.Duration
role string
}

// DefaultRUConfig returns the default configuration.
Expand Down
8 changes: 8 additions & 0 deletions client/resource_group/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ func WithMaxWaitDuration(d time.Duration) ResourceControlCreateOption {
}
}

// WithRoleLabel labels the ru to attribute usages to specific role.
func WithRoleLabel(role string) ResourceControlCreateOption {
return func(controller *ResourceGroupsController) {
controller.ruConfig.role = role
}
}

var _ ResourceGroupKVInterceptor = (*ResourceGroupsController)(nil)

// ResourceGroupsController implements ResourceGroupKVInterceptor.
Expand Down Expand Up @@ -1102,6 +1109,7 @@ func initCounterNotify(counter *tokenCounter) {
func (gc *groupCostController) collectRequestAndConsumption(selectTyp selectType) *rmpb.TokenBucketRequest {
req := &rmpb.TokenBucketRequest{
ResourceGroupName: gc.name,
Role: gc.mainCfg.role,
}
// collect request resource
selected := gc.run.requestInProgress
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ require (
golang.org/x/image v0.10.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/oauth2 v0.4.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
Expand All @@ -210,4 +210,4 @@ replace google.golang.org/grpc v1.59.0 => google.golang.org/grpc v1.26.0
// When you modify PD cooperatively with kvproto, this will be useful to submit the PR to PD and the PR to
// 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/AmoebaProtozoa/kvproto v0.0.0-20240102100144-34b417c2a46f
1,657 changes: 1,645 additions & 12 deletions go.sum

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion pkg/mcs/resourcemanager/server/grpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func (s *Service) AcquireTokenBuckets(stream rmpb.ResourceManager_AcquireTokenBu
// Send the consumption to update the metrics.
isBackground := req.GetIsBackground()
isTiFlash := req.GetIsTiflash()
role := req.GetRole()
if isBackground && isTiFlash {
return errors.New("background and tiflash cannot be true at the same time")
}
Expand All @@ -201,7 +202,8 @@ func (s *Service) AcquireTokenBuckets(stream rmpb.ResourceManager_AcquireTokenBu
*rmpb.Consumption
isBackground bool
isTiFlash bool
}{resourceGroupName, req.GetConsumptionSinceLastRequest(), isBackground, isTiFlash}
role string
}{resourceGroupName, req.GetConsumptionSinceLastRequest(), isBackground, isTiFlash, role}
if isBackground {
continue
}
Expand Down
42 changes: 23 additions & 19 deletions pkg/mcs/resourcemanager/server/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Manager struct {
*rmpb.Consumption
isBackground bool
isTiFlash bool
role string
}
// record update time of each resource group
consumptionRecord map[consumptionRecordKey]time.Time
Expand All @@ -69,6 +70,7 @@ type Manager struct {
type consumptionRecordKey struct {
name string
ruType string
role string
}

// ConfigProvider is used to get resource manager config from the given
Expand All @@ -88,6 +90,7 @@ func NewManager[T ConfigProvider](srv bs.Server) *Manager {
*rmpb.Consumption
isBackground bool
isTiFlash bool
role string
}, defaultConsumptionChanSize),
consumptionRecord: make(map[consumptionRecordKey]time.Time),
}
Expand Down Expand Up @@ -373,18 +376,19 @@ func (m *Manager) backgroundMetricsFlush(ctx context.Context) {
if consumptionInfo.isTiFlash {
ruLabelType = tiflashTypeLabel
}
role := consumptionInfo.role

var (
name = consumptionInfo.resourceGroupName
rruMetrics = readRequestUnitCost.WithLabelValues(name, name, ruLabelType)
wruMetrics = writeRequestUnitCost.WithLabelValues(name, name, ruLabelType)
sqlLayerRuMetrics = sqlLayerRequestUnitCost.WithLabelValues(name, name)
readByteMetrics = readByteCost.WithLabelValues(name, name, ruLabelType)
writeByteMetrics = writeByteCost.WithLabelValues(name, name, ruLabelType)
kvCPUMetrics = kvCPUCost.WithLabelValues(name, name, ruLabelType)
sqlCPUMetrics = sqlCPUCost.WithLabelValues(name, name, ruLabelType)
readRequestCountMetrics = requestCount.WithLabelValues(name, name, readTypeLabel)
writeRequestCountMetrics = requestCount.WithLabelValues(name, name, writeTypeLabel)
rruMetrics = readRequestUnitCost.WithLabelValues(name, name, ruLabelType, role)
wruMetrics = writeRequestUnitCost.WithLabelValues(name, name, ruLabelType, role)
sqlLayerRuMetrics = sqlLayerRequestUnitCost.WithLabelValues(name, name, role)
readByteMetrics = readByteCost.WithLabelValues(name, name, ruLabelType, role)
writeByteMetrics = writeByteCost.WithLabelValues(name, name, ruLabelType, role)
kvCPUMetrics = kvCPUCost.WithLabelValues(name, name, ruLabelType, role)
sqlCPUMetrics = sqlCPUCost.WithLabelValues(name, name, ruLabelType, role)
readRequestCountMetrics = requestCount.WithLabelValues(name, name, readTypeLabel, role)
writeRequestCountMetrics = requestCount.WithLabelValues(name, name, writeTypeLabel, role)
)
// RU info.
if consumption.RRU > 0 {
Expand Down Expand Up @@ -426,16 +430,16 @@ func (m *Manager) backgroundMetricsFlush(ctx context.Context) {
// Clean up the metrics that have not been updated for a long time.
for r, lastTime := range m.consumptionRecord {
if time.Since(lastTime) > metricsCleanupTimeout {
readRequestUnitCost.DeleteLabelValues(r.name, r.name, r.ruType)
writeRequestUnitCost.DeleteLabelValues(r.name, r.name, r.ruType)
sqlLayerRequestUnitCost.DeleteLabelValues(r.name, r.name, r.ruType)
readByteCost.DeleteLabelValues(r.name, r.name, r.ruType)
writeByteCost.DeleteLabelValues(r.name, r.name, r.ruType)
kvCPUCost.DeleteLabelValues(r.name, r.name, r.ruType)
sqlCPUCost.DeleteLabelValues(r.name, r.name, r.ruType)
requestCount.DeleteLabelValues(r.name, r.name, readTypeLabel)
requestCount.DeleteLabelValues(r.name, r.name, writeTypeLabel)
availableRUCounter.DeleteLabelValues(r.name, r.name, r.ruType)
readRequestUnitCost.DeleteLabelValues(r.name, r.name, r.ruType, r.role)
writeRequestUnitCost.DeleteLabelValues(r.name, r.name, r.ruType, r.role)
sqlLayerRequestUnitCost.DeleteLabelValues(r.name, r.name, r.role)
readByteCost.DeleteLabelValues(r.name, r.name, r.ruType, r.role)
writeByteCost.DeleteLabelValues(r.name, r.name, r.ruType, r.role)
kvCPUCost.DeleteLabelValues(r.name, r.name, r.ruType, r.role)
sqlCPUCost.DeleteLabelValues(r.name, r.name, r.ruType, r.role)
requestCount.DeleteLabelValues(r.name, r.name, readTypeLabel, r.role)
requestCount.DeleteLabelValues(r.name, r.name, writeTypeLabel, r.role)
availableRUCounter.DeleteLabelValues(r.name, r.name)
delete(m.consumptionRecord, r)
}
}
Expand Down
17 changes: 9 additions & 8 deletions pkg/mcs/resourcemanager/server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
tiflashTypeLabel = "ap"
defaultTypeLabel = "tp"
newResourceGroupNameLabel = "resource_group"
roleLabel = "role"
)

var (
Expand All @@ -40,21 +41,21 @@ var (
Subsystem: ruSubsystem,
Name: "read_request_unit_sum",
Help: "Counter of the read request unit cost for all resource groups.",
}, []string{resourceGroupNameLabel, newResourceGroupNameLabel, typeLabel})
}, []string{resourceGroupNameLabel, newResourceGroupNameLabel, typeLabel, roleLabel})
writeRequestUnitCost = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: ruSubsystem,
Name: "write_request_unit_sum",
Help: "Counter of the write request unit cost for all resource groups.",
}, []string{resourceGroupNameLabel, newResourceGroupNameLabel, typeLabel})
}, []string{resourceGroupNameLabel, newResourceGroupNameLabel, typeLabel, roleLabel})
sqlLayerRequestUnitCost = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: ruSubsystem,
Name: "sql_layer_request_unit_sum",
Help: "The number of the sql layer request unit cost for all resource groups.",
}, []string{resourceGroupNameLabel, newResourceGroupNameLabel})
}, []string{resourceGroupNameLabel, newResourceGroupNameLabel, roleLabel})

// Resource cost metrics.
readByteCost = prometheus.NewCounterVec(
Expand All @@ -63,35 +64,35 @@ var (
Subsystem: resourceSubsystem,
Name: "read_byte_sum",
Help: "Counter of the read byte cost for all resource groups.",
}, []string{resourceGroupNameLabel, newResourceGroupNameLabel, typeLabel})
}, []string{resourceGroupNameLabel, newResourceGroupNameLabel, typeLabel, roleLabel})
writeByteCost = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: resourceSubsystem,
Name: "write_byte_sum",
Help: "Counter of the write byte cost for all resource groups.",
}, []string{resourceGroupNameLabel, newResourceGroupNameLabel, typeLabel})
}, []string{resourceGroupNameLabel, newResourceGroupNameLabel, typeLabel, roleLabel})
kvCPUCost = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: resourceSubsystem,
Name: "kv_cpu_time_ms_sum",
Help: "Counter of the KV CPU time cost in milliseconds for all resource groups.",
}, []string{resourceGroupNameLabel, newResourceGroupNameLabel, typeLabel})
}, []string{resourceGroupNameLabel, newResourceGroupNameLabel, typeLabel, roleLabel})
sqlCPUCost = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: resourceSubsystem,
Name: "sql_cpu_time_ms_sum",
Help: "Counter of the SQL CPU time cost in milliseconds for all resource groups.",
}, []string{resourceGroupNameLabel, newResourceGroupNameLabel, typeLabel})
}, []string{resourceGroupNameLabel, newResourceGroupNameLabel, typeLabel, roleLabel})
requestCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: resourceSubsystem,
Name: "request_count",
Help: "The number of read/write requests for all resource groups.",
}, []string{resourceGroupNameLabel, newResourceGroupNameLabel, typeLabel})
}, []string{resourceGroupNameLabel, newResourceGroupNameLabel, typeLabel, roleLabel})

availableRUCounter = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ require (
golang.org/x/exp v0.0.0-20230711005742-c3f37128e5a4 // indirect
golang.org/x/image v0.10.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/oauth2 v0.4.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions tests/integrations/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,8 @@ golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.4.0 h1:NF0gk8LVPg1Ml7SSbGyySuoxdsXitj7TvgvuRxIMc/M=
golang.org/x/oauth2 v0.4.0/go.mod h1:RznEsdpjGAINPTOF0UH/t+xJ75L18YO3Ho6Pyn+uRec=
golang.org/x/oauth2 v0.11.0 h1:vPL4xzxBM4niKCW6g9whtaWVXTJf1U5e4aZxxFx/gbU=
golang.org/x/oauth2 v0.11.0/go.mod h1:LdF7O/8bLR/qWK9DrpXmbHLTouvRHK0SgJl0GmDBchk=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
4 changes: 3 additions & 1 deletion tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ require (
golang.org/x/exp v0.0.0-20230711005742-c3f37128e5a4 // indirect
golang.org/x/image v0.10.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/oauth2 v0.4.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/time v0.3.0 // indirect
Expand All @@ -192,3 +192,5 @@ require (
moul.io/zapgorm2 v1.1.0 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
)

replace github.com/pingcap/kvproto => github.com/AmoebaProtozoa/kvproto v0.0.0-20240102100144-34b417c2a46f
Loading

0 comments on commit dc4d167

Please sign in to comment.