Skip to content

Commit

Permalink
fix consumption less zero
Browse files Browse the repository at this point in the history
Signed-off-by: Cabinfever_B <[email protected]>
  • Loading branch information
CabinfeverB committed Aug 24, 2023
1 parent fbd386a commit d36d48c
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 @@ -1070,13 +1070,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 +1149,12 @@ func (gc *groupCostController) onRequestWait(
gc.mu.Lock()
sub(gc.mu.consumption, delta)
gc.mu.Unlock()
failpoint.Inject("triggerUpdate", func() {
select {
case gc.lowRUNotifyChan <- struct{}{}:
default:
}
})
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 * 500)
re.NoError(failpoint.Disable("github.com/tikv/pd/client/resource_group/controller/triggerUpdate"))
controller.Stop()
}

Expand Down

0 comments on commit d36d48c

Please sign in to comment.