raft_server_stats.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package server
  2. import (
  3. "sync"
  4. "time"
  5. "github.com/coreos/etcd/third_party/github.com/goraft/raft"
  6. )
  7. type raftServerStats struct {
  8. Name string `json:"name"`
  9. State string `json:"state"`
  10. StartTime time.Time `json:"startTime"`
  11. LeaderInfo struct {
  12. Name string `json:"leader"`
  13. Uptime string `json:"uptime"`
  14. StartTime time.Time `json:"startTime"`
  15. } `json:"leaderInfo"`
  16. RecvAppendRequestCnt uint64 `json:"recvAppendRequestCnt,"`
  17. RecvingPkgRate float64 `json:"recvPkgRate,omitempty"`
  18. RecvingBandwidthRate float64 `json:"recvBandwidthRate,omitempty"`
  19. SendAppendRequestCnt uint64 `json:"sendAppendRequestCnt"`
  20. SendingPkgRate float64 `json:"sendPkgRate,omitempty"`
  21. SendingBandwidthRate float64 `json:"sendBandwidthRate,omitempty"`
  22. sendRateQueue *statsQueue
  23. recvRateQueue *statsQueue
  24. sync.Mutex
  25. }
  26. func NewRaftServerStats(name string) *raftServerStats {
  27. stats := &raftServerStats{
  28. Name: name,
  29. StartTime: time.Now(),
  30. sendRateQueue: &statsQueue{
  31. back: -1,
  32. },
  33. recvRateQueue: &statsQueue{
  34. back: -1,
  35. },
  36. }
  37. stats.LeaderInfo.StartTime = time.Now()
  38. return stats
  39. }
  40. func (ss *raftServerStats) RecvAppendReq(leaderName string, pkgSize int) {
  41. ss.Lock()
  42. defer ss.Unlock()
  43. ss.State = raft.Follower
  44. if leaderName != ss.LeaderInfo.Name {
  45. ss.LeaderInfo.Name = leaderName
  46. ss.LeaderInfo.StartTime = time.Now()
  47. }
  48. ss.recvRateQueue.Insert(NewPackageStats(time.Now(), pkgSize))
  49. ss.RecvAppendRequestCnt++
  50. }
  51. func (ss *raftServerStats) SendAppendReq(pkgSize int) {
  52. ss.Lock()
  53. defer ss.Unlock()
  54. now := time.Now()
  55. if ss.State != raft.Leader {
  56. ss.State = raft.Leader
  57. ss.LeaderInfo.Name = ss.Name
  58. ss.LeaderInfo.StartTime = now
  59. }
  60. ss.sendRateQueue.Insert(NewPackageStats(now, pkgSize))
  61. ss.SendAppendRequestCnt++
  62. }