Skip to content

Commit

Permalink
add type for ru read and write
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp committed Jul 17, 2023
1 parent 40eaa35 commit 5ab1349
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 14 deletions.
1 change: 1 addition & 0 deletions client/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/stretchr/testify v1.8.2
go.uber.org/goleak v1.1.11
go.uber.org/zap v1.24.0
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1
google.golang.org/grpc v1.54.0
)

Expand Down
2 changes: 2 additions & 0 deletions client/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw=
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand Down
29 changes: 27 additions & 2 deletions client/resource_group/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"encoding/json"
"math"
"strings"
"sync"
"sync/atomic"
"time"
Expand All @@ -32,6 +33,7 @@ import (
pd "github.com/tikv/pd/client"
"github.com/tikv/pd/client/errs"
"go.uber.org/zap"
"golang.org/x/exp/slices"
)

const (
Expand All @@ -56,8 +58,10 @@ const (
type ResourceGroupKVInterceptor interface {
// OnRequestWait is used to check whether resource group has enough tokens. It maybe needs to wait some time.
OnRequestWait(ctx context.Context, resourceGroupName string, info RequestInfo) (*rmpb.Consumption, *rmpb.Consumption, error)
// OnResponse is used to consume tokens after receiving response
// OnResponse is used to consume tokens after receiving response.
OnResponse(resourceGroupName string, req RequestInfo, resp ResponseInfo) (*rmpb.Consumption, error)
// IsBackgroundRequest If the resource group has background jobs, we should not record consumption and wait for it.
IsBackgroundRequest(ctx context.Context, resourceGroupName, requestResource string) bool
}

// ResourceGroupProvider provides some api to interact with resource manager server.
Expand Down Expand Up @@ -454,7 +458,6 @@ func (c *ResourceGroupsController) OnRequestWait(
) (*rmpb.Consumption, *rmpb.Consumption, error) {
gc, err := c.tryGetResourceGroup(ctx, resourceGroupName)
if err != nil {
failedRequestCounter.WithLabelValues(resourceGroupName).Inc()
return nil, nil, err
}
return gc.onRequestWait(ctx, info)
Expand All @@ -472,6 +475,28 @@ func (c *ResourceGroupsController) OnResponse(
return tmp.(*groupCostController).onResponse(req, resp)
}

// IsBackgroundRequest If the resource group has background jobs, we should not record consumption and wait for it.
func (c *ResourceGroupsController) IsBackgroundRequest(ctx context.Context,
resourceGroupName, requestResource string) bool {
gc, err := c.tryGetResourceGroup(ctx, resourceGroupName)
if err != nil {
failedRequestCounter.WithLabelValues(resourceGroupName).Inc()
return false
}

gc.metaLock.Lock()
defer gc.metaLock.Unlock()
if bg := gc.meta.BackgroundSettings; bg != nil {
if len(requestResource) == 0 || len(bg.JobTypes) == 0 {
return false
}
if idx := strings.LastIndex(requestResource, "_"); idx != -1 {
return slices.Contains(bg.JobTypes, requestResource[idx+1:])
}
}
return false
}

// GetResourceGroup returns the meta setting of the given resource group name.
func (c *ResourceGroupsController) GetResourceGroup(resourceGroupName string) (*rmpb.ResourceGroup, error) {
gc, err := c.tryGetResourceGroup(c.loopCtx, resourceGroupName)
Expand Down
19 changes: 13 additions & 6 deletions 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 @@ -300,15 +302,20 @@ func (m *Manager) backgroundMetricsFlush(ctx context.Context) {
if consumption == nil {
continue
}
backgroundType := ""
if consumptionInfo.isBackground {
backgroundType = backgroundTypeLabel
}

var (
name = consumptionInfo.resourceGroupName
rruMetrics = readRequestUnitCost.WithLabelValues(name)
wruMetrics = writeRequestUnitCost.WithLabelValues(name)
rruMetrics = readRequestUnitCost.WithLabelValues(name, backgroundType)
wruMetrics = writeRequestUnitCost.WithLabelValues(name, backgroundType)
sqlLayerRuMetrics = sqlLayerRequestUnitCost.WithLabelValues(name)
readByteMetrics = readByteCost.WithLabelValues(name)
writeByteMetrics = writeByteCost.WithLabelValues(name)
kvCPUMetrics = kvCPUCost.WithLabelValues(name)
sqlCPUMetrics = sqlCPUCost.WithLabelValues(name)
readByteMetrics = readByteCost.WithLabelValues(name, backgroundType)
writeByteMetrics = writeByteCost.WithLabelValues(name, backgroundType)
kvCPUMetrics = kvCPUCost.WithLabelValues(name, backgroundType)
sqlCPUMetrics = sqlCPUCost.WithLabelValues(name, backgroundType)
readRequestCountMetrics = requestCount.WithLabelValues(name, readTypeLabel)
writeRequestCountMetrics = requestCount.WithLabelValues(name, writeTypeLabel)
)
Expand Down
13 changes: 7 additions & 6 deletions pkg/mcs/resourcemanager/server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
typeLabel = "type"
readTypeLabel = "read"
writeTypeLabel = "write"
backgroundTypeLabel = "background"
)

var (
Expand All @@ -44,14 +45,14 @@ var (
Subsystem: ruSubsystem,
Name: "read_request_unit_sum",
Help: "Counter of the read request unit cost for all resource groups.",
}, []string{resourceGroupNameLabel})
}, []string{resourceGroupNameLabel, typeLabel})
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})
}, []string{resourceGroupNameLabel, typeLabel})
sqlLayerRequestUnitCost = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Expand All @@ -67,28 +68,28 @@ var (
Subsystem: resourceSubsystem,
Name: "read_byte_sum",
Help: "Counter of the read byte cost for all resource groups.",
}, []string{resourceGroupNameLabel})
}, []string{resourceGroupNameLabel, typeLabel})
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})
}, []string{resourceGroupNameLabel, typeLabel})
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})
}, []string{resourceGroupNameLabel, typeLabel})
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})
}, []string{resourceGroupNameLabel, typeLabel})
requestCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Expand Down

0 comments on commit 5ab1349

Please sign in to comment.