Skip to content

Commit

Permalink
cluster: fix potential panic may be caused by div 0
Browse files Browse the repository at this point in the history
Signed-off-by: Shafreeck Sea <shafreeck@gmail.com>
  • Loading branch information
shafreeck committed Nov 5, 2019
1 parent 712a5e6 commit 405f60e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
44 changes: 39 additions & 5 deletions server/cluster_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"time"

"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/log"
"go.uber.org/zap"
)

// Cluster State Statistics
Expand Down Expand Up @@ -132,7 +134,11 @@ func (s *StatEntries) CPU(steps int) float64 {
if steps > s.total {
steps = s.total
}
if steps == 0 {
return float64(0)
}

log.Debug("caculate CPU", zap.Int("steps", steps))
usage := 0.0
idx := (s.total - 1) % cap
for i := 0; i < steps; i++ {
Expand All @@ -148,6 +154,9 @@ func (s *StatEntries) CPU(steps int) float64 {

func cpuUsageAll(usages []*pdpb.RecordPair) float64 {
sum := 0.0
if len(usages) == 0 {
return sum
}
for _, usage := range usages {
sum += float64(usage.Value)
}
Expand All @@ -164,6 +173,9 @@ func (s *StatEntries) Keys(steps int) (int64, int64) {
if steps > s.total {
steps = s.total
}
if steps == 0 {
return 0, 0
}

var read, written int64
idx := (s.total - 1) % cap
Expand All @@ -189,6 +201,9 @@ func (s *StatEntries) Bytes(steps int) (int64, int64) {
if steps > s.total {
steps = s.total
}
if steps == 0 {
return 0, 0
}
var read, written int64
idx := (s.total - 1) % cap
for i := 0; i < steps; i++ {
Expand Down Expand Up @@ -226,13 +241,17 @@ func (cst *ClusterStatEntries) Append(stat *StatEntry) {
defer cst.m.Unlock()

// update interval
interval := int64(stat.Interval.GetEndTimestamp() -
stat.Interval.GetStartTimestamp())
if interval == 0 {
start, end := stat.Interval.GetStartTimestamp(),
stat.Interval.GetEndTimestamp()
interval := int64(end - start)
if start == 0 || end == 0 || interval == 0 {
interval = DefaultIntervalHint
}
cst.interval = (cst.interval*cst.total + interval) /
(cst.total + 1)
if cst.total == 0 {
cst.interval = interval
} else {
cst.interval = cst.interval + (interval-cst.interval)/cst.total
}
cst.total++

// append the entry
Expand Down Expand Up @@ -260,6 +279,11 @@ func (cst *ClusterStatEntries) CPU(d time.Duration, excludes ...uint64) float64
cst.m.RLock()
defer cst.m.RUnlock()

// no entries have been collected
if cst.total == 0 {
return 0
}

steps := int64(d) / cst.interval

sum := 0.0
Expand All @@ -277,6 +301,11 @@ func (cst *ClusterStatEntries) CPU(d time.Duration, excludes ...uint64) float64
func (cst *ClusterStatEntries) Keys(d time.Duration, excludes ...uint64) (int64, int64) {
cst.m.RLock()
defer cst.m.RUnlock()
// no entries have been collected
if cst.total == 0 {
return 0, 0
}

steps := int64(d) / cst.interval

var read, written int64
Expand All @@ -296,6 +325,11 @@ func (cst *ClusterStatEntries) Keys(d time.Duration, excludes ...uint64) (int64,
func (cst *ClusterStatEntries) Bytes(d time.Duration, excludes ...uint64) (int64, int64) {
cst.m.RLock()
defer cst.m.RUnlock()
// no entries have been collected
if cst.total == 0 {
return 0, 0
}

steps := int64(d) / cst.interval

var read, written int64
Expand Down
4 changes: 2 additions & 2 deletions server/cluster_stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ func (s *testClusterStatSuite) TestClusterStatCPU(c *C) {

// heartbeat per 10s
interval := &pdpb.TimeInterval{
StartTimestamp: 0,
EndTimestamp: 10 * uint64(time.Second),
StartTimestamp: 1,
EndTimestamp: 11,
}
// the average cpu usage is 20%
usages := cpu(20)
Expand Down

0 comments on commit 405f60e

Please sign in to comment.