Xiang Li 12 years ago
parent
commit
e4b164c324
2 changed files with 21 additions and 1 deletions
  1. 17 0
      raft_stats.go
  2. 4 1
      transporter.go

+ 17 - 0
raft_stats.go

@@ -0,0 +1,17 @@
+package main
+
+import (
+	"encoding/json"
+)
+
+type peerStats struct {
+	Latency    float64 `json:"latency"`
+	AvgLatency float64 `json:"averageLatency"`
+	FailCnt    uint64  `json:"failsCount"`
+	SuccCnt    uint64  `json:"successCount"`
+}
+
+func (r *raftServer) Stats() []byte {
+	b, _ := json.Marshal(r.peersStats)
+	return b
+}

+ 4 - 1
transporter.go

@@ -62,9 +62,12 @@ func (t transporter) SendAppendEntriesRequest(server *raft.Server, peer *raft.Pe
 
 
 	if err != nil {
 	if err != nil {
 		debugf("Cannot send AppendEntriesRequest to %s: %s", u, err)
 		debugf("Cannot send AppendEntriesRequest to %s: %s", u, err)
-		thisPeerStats.Failcnt++
+		thisPeerStats.FailCnt++
 	} else {
 	} else {
+		total := float64(thisPeerStats.SuccCnt) * thisPeerStats.AvgLatency
+		thisPeerStats.SuccCnt++
 		thisPeerStats.Latency = float64(end.Sub(start)) / (1000000.0)
 		thisPeerStats.Latency = float64(end.Sub(start)) / (1000000.0)
+		thisPeerStats.AvgLatency = (total + thisPeerStats.Latency) / float64(thisPeerStats.SuccCnt)
 	}
 	}
 
 
 	r.peersStats[peer.Name] = thisPeerStats
 	r.peersStats[peer.Name] = thisPeerStats