Skip to content

Commit

Permalink
cluster: avoid using cap as the var name
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 12, 2019
1 parent 0f2d2bd commit 9a0493d
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions server/cluster_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,22 @@ type StatEntries struct {
func NewStatEntries(size int) *StatEntries {
return &StatEntries{
total: 0,
entries: make([]*StatEntry, size, size),
entries: make([]*StatEntry, size),
}
}

// Append a StatEntry
func (s *StatEntries) Append(stat *StatEntry) {
idx := s.total % cap(s.entries)
idx := s.total % len(s.entries)
s.entries[idx] = stat
s.total++
}

// CPU returns the cpu usage
func (s *StatEntries) CPU(steps int) float64 {
cap := cap(s.entries)
if steps > cap {
steps = cap
size := len(s.entries)
if steps > size {
steps = size
}
if steps > s.total {
steps = s.total
Expand All @@ -140,13 +140,13 @@ func (s *StatEntries) CPU(steps int) float64 {

log.Debug("caculate CPU", zap.Int("steps", steps))
usage := 0.0
idx := (s.total - 1) % cap
idx := (s.total - 1) % size
for i := 0; i < steps; i++ {
stat := s.entries[idx]
usage += cpuUsageAll(stat.CpuUsages)
idx--
if idx < 0 {
idx += cap
idx += size
}
}
return usage / float64(steps)
Expand All @@ -166,9 +166,9 @@ func cpuUsageAll(usages []*pdpb.RecordPair) float64 {
// Keys returns the average written and read keys duration
// an interval of heartbeats
func (s *StatEntries) Keys(steps int) (read int64, written int64) {
cap := cap(s.entries)
if steps > cap {
steps = cap
size := len(s.entries)
if steps > size {
steps = size
}
if steps > s.total {
steps = s.total
Expand All @@ -177,14 +177,14 @@ func (s *StatEntries) Keys(steps int) (read int64, written int64) {
return 0, 0
}

idx := (s.total - 1) % cap
idx := (s.total - 1) % size
for i := 0; i < steps; i++ {
stat := s.entries[idx]
read += int64(stat.KeysRead)
written += int64(stat.KeysWritten)
idx--
if idx < 0 {
idx += cap
idx += size
}
}
return read / int64(steps), written / int64(steps)
Expand All @@ -193,24 +193,24 @@ func (s *StatEntries) Keys(steps int) (read int64, written int64) {
// Bytes returns the average written and read bytes duration
// an interval of heartbeats
func (s *StatEntries) Bytes(steps int) (read int64, written int64) {
cap := cap(s.entries)
if steps > cap {
steps = cap
size := len(s.entries)
if steps > size {
steps = size
}
if steps > s.total {
steps = s.total
}
if steps == 0 {
return 0, 0
}
idx := (s.total - 1) % cap
idx := (s.total - 1) % size
for i := 0; i < steps; i++ {
stat := s.entries[idx]
read += int64(stat.BytesRead)
written += int64(stat.BytesWritten)
idx--
if idx < 0 {
idx += cap
idx += size
}
}
return read / int64(steps), written / int64(steps)
Expand Down

0 comments on commit 9a0493d

Please sign in to comment.