|
|
@@ -26,6 +26,26 @@ import (
|
|
|
// ServerStats encapsulates various statistics about an EtcdServer and its
|
|
|
// communication with other members of the cluster
|
|
|
type ServerStats struct {
|
|
|
+ serverStats
|
|
|
+ sync.Mutex
|
|
|
+}
|
|
|
+
|
|
|
+func NewServerStats(name, id string) *ServerStats {
|
|
|
+ ss := &ServerStats{
|
|
|
+ serverStats: serverStats{
|
|
|
+ Name: name,
|
|
|
+ ID: id,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ now := time.Now()
|
|
|
+ ss.StartTime = now
|
|
|
+ ss.LeaderInfo.StartTime = now
|
|
|
+ ss.sendRateQueue = &statsQueue{back: -1}
|
|
|
+ ss.recvRateQueue = &statsQueue{back: -1}
|
|
|
+ return ss
|
|
|
+}
|
|
|
+
|
|
|
+type serverStats struct {
|
|
|
Name string `json:"name"`
|
|
|
// ID is the raft ID of the node.
|
|
|
// TODO(jonboulle): use ID instead of name?
|
|
|
@@ -49,17 +69,15 @@ type ServerStats struct {
|
|
|
|
|
|
sendRateQueue *statsQueue
|
|
|
recvRateQueue *statsQueue
|
|
|
-
|
|
|
- sync.Mutex
|
|
|
}
|
|
|
|
|
|
func (ss *ServerStats) JSON() []byte {
|
|
|
ss.Lock()
|
|
|
- stats := *ss
|
|
|
+ stats := ss.serverStats
|
|
|
ss.Unlock()
|
|
|
stats.LeaderInfo.Uptime = time.Since(stats.LeaderInfo.StartTime).String()
|
|
|
- stats.SendingPkgRate, stats.SendingBandwidthRate = stats.SendRates()
|
|
|
- stats.RecvingPkgRate, stats.RecvingBandwidthRate = stats.RecvRates()
|
|
|
+ stats.SendingPkgRate, stats.SendingBandwidthRate = stats.sendRateQueue.Rate()
|
|
|
+ stats.RecvingPkgRate, stats.RecvingBandwidthRate = stats.recvRateQueue.Rate()
|
|
|
b, err := json.Marshal(stats)
|
|
|
// TODO(jonboulle): appropriate error handling?
|
|
|
if err != nil {
|
|
|
@@ -68,32 +86,6 @@ func (ss *ServerStats) JSON() []byte {
|
|
|
return b
|
|
|
}
|
|
|
|
|
|
-// Initialize clears the statistics of ServerStats and resets its start time
|
|
|
-func (ss *ServerStats) Initialize() {
|
|
|
- if ss == nil {
|
|
|
- return
|
|
|
- }
|
|
|
- now := time.Now()
|
|
|
- ss.StartTime = now
|
|
|
- ss.LeaderInfo.StartTime = now
|
|
|
- ss.sendRateQueue = &statsQueue{
|
|
|
- back: -1,
|
|
|
- }
|
|
|
- ss.recvRateQueue = &statsQueue{
|
|
|
- back: -1,
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-// RecvRates calculates and returns the rate of received append requests
|
|
|
-func (ss *ServerStats) RecvRates() (float64, float64) {
|
|
|
- return ss.recvRateQueue.Rate()
|
|
|
-}
|
|
|
-
|
|
|
-// SendRates calculates and returns the rate of sent append requests
|
|
|
-func (ss *ServerStats) SendRates() (float64, float64) {
|
|
|
- return ss.sendRateQueue.Rate()
|
|
|
-}
|
|
|
-
|
|
|
// RecvAppendReq updates the ServerStats in response to an AppendRequest
|
|
|
// from the given leader being received
|
|
|
func (ss *ServerStats) RecvAppendReq(leader string, reqSize int) {
|