Skip to content

Commit

Permalink
resource_control: fix consumption less zero (tikv#6983)
Browse files Browse the repository at this point in the history
close tikv#6973

Signed-off-by: Cabinfever_B <[email protected]>

Co-authored-by: ShuNing <[email protected]>
Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Sep 1, 2023
1 parent 87f2da8 commit ee8654e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
14 changes: 7 additions & 7 deletions client/resource_group/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,9 @@ func (gc *groupCostController) collectRequestAndConsumption(selectTyp selectType
}
// collect request resource
selected := gc.run.requestInProgress
failpoint.Inject("triggerUpdate", func() {
selected = true
})
switch gc.mode {
case rmpb.GroupMode_RawMode:
requests := make([]*rmpb.RawResourceItem, 0, len(requestResourceLimitTypeList))
Expand Down Expand Up @@ -1070,13 +1073,7 @@ func (gc *groupCostController) collectRequestAndConsumption(selectTyp selectType
if !selected {
return nil
}

deltaConsumption := &rmpb.Consumption{}
*deltaConsumption = *gc.run.consumption
sub(deltaConsumption, gc.run.lastRequestConsumption)
req.ConsumptionSinceLastRequest = deltaConsumption

*gc.run.lastRequestConsumption = *gc.run.consumption
req.ConsumptionSinceLastRequest = updateDeltaConsumption(gc.run.lastRequestConsumption, gc.run.consumption)
gc.run.lastRequestTime = time.Now()
gc.run.requestInProgress = true
return req
Expand Down Expand Up @@ -1155,6 +1152,9 @@ func (gc *groupCostController) onRequestWait(
gc.mu.Lock()
sub(gc.mu.consumption, delta)
gc.mu.Unlock()
failpoint.Inject("triggerUpdate", func() {
gc.lowRUNotifyChan <- struct{}{}
})
return nil, nil, err
}
gc.successfulRequestDuration.Observe(d.Seconds())
Expand Down
37 changes: 37 additions & 0 deletions client/resource_group/controller/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,43 @@ func add(custom1 *rmpb.Consumption, custom2 *rmpb.Consumption) {
custom1.KvWriteRpcCount += custom2.KvWriteRpcCount
}

func updateDeltaConsumption(last *rmpb.Consumption, now *rmpb.Consumption) *rmpb.Consumption {
delta := &rmpb.Consumption{}
if now.RRU >= last.RRU {
delta.RRU = now.RRU - last.RRU
last.RRU = now.RRU
}
if now.WRU >= last.WRU {
delta.WRU = now.WRU - last.WRU
last.WRU = now.WRU
}
if now.ReadBytes >= last.ReadBytes {
delta.ReadBytes = now.ReadBytes - last.ReadBytes
last.ReadBytes = now.ReadBytes
}
if now.WriteBytes >= last.WriteBytes {
delta.WriteBytes = now.WriteBytes - last.WriteBytes
last.WriteBytes = now.WriteBytes
}
if now.TotalCpuTimeMs >= last.TotalCpuTimeMs {
delta.TotalCpuTimeMs = now.TotalCpuTimeMs - last.TotalCpuTimeMs
last.TotalCpuTimeMs = now.TotalCpuTimeMs
}
if now.SqlLayerCpuTimeMs >= last.SqlLayerCpuTimeMs {
delta.SqlLayerCpuTimeMs = now.SqlLayerCpuTimeMs - last.SqlLayerCpuTimeMs
last.SqlLayerCpuTimeMs = now.SqlLayerCpuTimeMs
}
if now.KvReadRpcCount >= last.KvReadRpcCount {
delta.KvReadRpcCount = now.KvReadRpcCount - last.KvReadRpcCount
last.KvReadRpcCount = now.KvReadRpcCount
}
if now.KvWriteRpcCount >= last.KvWriteRpcCount {
delta.KvWriteRpcCount = now.KvWriteRpcCount - last.KvWriteRpcCount
last.KvWriteRpcCount = now.KvWriteRpcCount
}
return delta
}

func sub(custom1 *rmpb.Consumption, custom2 *rmpb.Consumption) {
if custom1 == nil || custom2 == nil {
return
Expand Down
12 changes: 6 additions & 6 deletions pkg/mcs/resourcemanager/server/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,17 +320,17 @@ func (m *Manager) backgroundMetricsFlush(ctx context.Context) {
writeRequestCountMetrics = requestCount.WithLabelValues(name, writeTypeLabel)
)
// RU info.
if consumption.RRU != 0 {
if consumption.RRU > 0 {
rruMetrics.Add(consumption.RRU)
}
if consumption.WRU != 0 {
if consumption.WRU > 0 {
wruMetrics.Add(consumption.WRU)
}
// Byte info.
if consumption.ReadBytes != 0 {
if consumption.ReadBytes > 0 {
readByteMetrics.Add(consumption.ReadBytes)
}
if consumption.WriteBytes != 0 {
if consumption.WriteBytes > 0 {
writeByteMetrics.Add(consumption.WriteBytes)
}
// CPU time info.
Expand All @@ -342,10 +342,10 @@ func (m *Manager) backgroundMetricsFlush(ctx context.Context) {
kvCPUMetrics.Add(consumption.TotalCpuTimeMs - consumption.SqlLayerCpuTimeMs)
}
// RPC count info.
if consumption.KvReadRpcCount != 0 {
if consumption.KvReadRpcCount > 0 {
readRequestCountMetrics.Add(consumption.KvReadRpcCount)
}
if consumption.KvWriteRpcCount != 0 {
if consumption.KvWriteRpcCount > 0 {
writeRequestCountMetrics.Add(consumption.KvWriteRpcCount)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,13 @@ func (suite *resourceManagerClientTestSuite) TestResourceGroupController() {
break
}
}
re.NoError(failpoint.Enable("github.com/tikv/pd/client/resource_group/controller/triggerUpdate", "return(true)"))
tcs := tokenConsumptionPerSecond{rruTokensAtATime: 1, wruTokensAtATime: 900000000, times: 1, waitDuration: 0}
wreq := tcs.makeWriteRequest()
_, _, err := controller.OnRequestWait(suite.ctx, suite.initGroups[0].Name, wreq)
re.Error(err)
time.Sleep(time.Millisecond * 200)
re.NoError(failpoint.Disable("github.com/tikv/pd/client/resource_group/controller/triggerUpdate"))
controller.Stop()
}

Expand Down

0 comments on commit ee8654e

Please sign in to comment.