Skip to content

Commit

Permalink
cluster: add bytes and keys as the statistics to return the cluster
Browse files Browse the repository at this point in the history
state

Signed-off-by: Shafreeck Sea <shafreeck@gmail.com>
  • Loading branch information
shafreeck committed Nov 5, 2019
1 parent 405f60e commit f2d434f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
8 changes: 8 additions & 0 deletions server/cluster_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,14 @@ func (cs *ClusterState) State(d time.Duration, excludes ...uint64) LoadState {
if cs.cst.total < NumberOfEntries {
return LoadStateNone
}
read, written := cs.cst.Bytes(d, excludes...)
if read == 0 && written == 0 {
return LoadStateIdle
}
read, written = cs.cst.Keys(d, excludes...)
if read == 0 && written == 0 {
return LoadStateIdle
}
cpu := cs.cst.CPU(d, excludes...)
switch {
case cpu == 0:
Expand Down
28 changes: 18 additions & 10 deletions server/cluster_stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,31 +193,39 @@ func (s *testClusterStatSuite) TestClusterStatCPU(c *C) {
}

func (s *testClusterStatSuite) TestClusterStatState(c *C) {
CPU := func(usage int64) *ClusterState {
Load := func(usage int64, keys uint64, bytes uint64) *ClusterState {
cst := NewClusterStatEntries(10)
c.Assert(cst, NotNil)

// 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(usage)

for i := 0; i < NumberOfEntries; i++ {
entry := &StatEntry{
StoreId: 0,
Interval: interval,
CpuUsages: usages,
StoreId: 0,
KeysWritten: keys,
KeysRead: keys,
BytesWritten: bytes,
BytesRead: bytes,
Interval: interval,
CpuUsages: usages,
}
cst.Append(entry)
}
return &ClusterState{cst}
}
d := 60 * time.Second
c.Assert(CPU(0).State(d), Equals, LoadStateIdle)
c.Assert(CPU(20).State(d), Equals, LoadStateLow)
c.Assert(CPU(50).State(d), Equals, LoadStateNormal)
c.Assert(CPU(90).State(d), Equals, LoadStateHigh)
c.Assert(Load(0, 1, 1).State(d), Equals, LoadStateIdle)
c.Assert(Load(20, 1, 1).State(d), Equals, LoadStateLow)
c.Assert(Load(50, 1, 1).State(d), Equals, LoadStateNormal)
c.Assert(Load(90, 1, 1).State(d), Equals, LoadStateHigh)

c.Assert(Load(90, 1, 0).State(d), Equals, LoadStateIdle)
c.Assert(Load(90, 0, 1).State(d), Equals, LoadStateIdle)
c.Assert(Load(90, 0, 0).State(d), Equals, LoadStateIdle)
}

0 comments on commit f2d434f

Please sign in to comment.