server.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package stats
  2. import (
  3. "encoding/json"
  4. "log"
  5. "sync"
  6. "time"
  7. "github.com/coreos/etcd/raft"
  8. )
  9. // ServerStats encapsulates various statistics about an EtcdServer and its
  10. // communication with other members of the cluster
  11. type ServerStats struct {
  12. Name string `json:"name"`
  13. // TODO(jonboulle): use ID instead of name?
  14. ID string `json:"id"`
  15. State raft.StateType `json:"state"`
  16. StartTime time.Time `json:"startTime"`
  17. LeaderInfo struct {
  18. Name string `json:"leader"`
  19. Uptime string `json:"uptime"`
  20. StartTime time.Time `json:"startTime"`
  21. } `json:"leaderInfo"`
  22. RecvAppendRequestCnt uint64 `json:"recvAppendRequestCnt,"`
  23. RecvingPkgRate float64 `json:"recvPkgRate,omitempty"`
  24. RecvingBandwidthRate float64 `json:"recvBandwidthRate,omitempty"`
  25. SendAppendRequestCnt uint64 `json:"sendAppendRequestCnt"`
  26. SendingPkgRate float64 `json:"sendPkgRate,omitempty"`
  27. SendingBandwidthRate float64 `json:"sendBandwidthRate,omitempty"`
  28. sendRateQueue *statsQueue
  29. recvRateQueue *statsQueue
  30. sync.Mutex
  31. }
  32. func (ss *ServerStats) JSON() []byte {
  33. ss.Lock()
  34. stats := *ss
  35. ss.Unlock()
  36. stats.LeaderInfo.Uptime = time.Now().Sub(stats.LeaderInfo.StartTime).String()
  37. stats.SendingPkgRate, stats.SendingBandwidthRate = stats.SendRates()
  38. stats.RecvingPkgRate, stats.RecvingBandwidthRate = stats.RecvRates()
  39. b, err := json.Marshal(stats)
  40. // TODO(jonboulle): appropriate error handling?
  41. if err != nil {
  42. log.Printf("error marshalling server stats: %v", err)
  43. }
  44. return b
  45. }
  46. // Initialize clears the statistics of ServerStats and resets its start time
  47. func (ss *ServerStats) Initialize() {
  48. if ss == nil {
  49. return
  50. }
  51. now := time.Now()
  52. ss.StartTime = now
  53. ss.LeaderInfo.StartTime = now
  54. ss.sendRateQueue = &statsQueue{
  55. back: -1,
  56. }
  57. ss.recvRateQueue = &statsQueue{
  58. back: -1,
  59. }
  60. }
  61. // RecvRates calculates and returns the rate of received append requests
  62. func (ss *ServerStats) RecvRates() (float64, float64) {
  63. return ss.recvRateQueue.Rate()
  64. }
  65. // SendRates calculates and returns the rate of sent append requests
  66. func (ss *ServerStats) SendRates() (float64, float64) {
  67. return ss.sendRateQueue.Rate()
  68. }
  69. // RecvAppendReq updates the ServerStats in response to an AppendRequest
  70. // from the given leader being received
  71. func (ss *ServerStats) RecvAppendReq(leader string, reqSize int) {
  72. ss.Lock()
  73. defer ss.Unlock()
  74. now := time.Now()
  75. ss.State = raft.StateFollower
  76. if leader != ss.LeaderInfo.Name {
  77. ss.LeaderInfo.Name = leader
  78. ss.LeaderInfo.StartTime = now
  79. }
  80. ss.recvRateQueue.Insert(
  81. &RequestStats{
  82. SendingTime: now,
  83. Size: reqSize,
  84. },
  85. )
  86. ss.RecvAppendRequestCnt++
  87. }
  88. // SendAppendReq updates the ServerStats in response to an AppendRequest
  89. // being sent by this server
  90. func (ss *ServerStats) SendAppendReq(reqSize int) {
  91. ss.Lock()
  92. defer ss.Unlock()
  93. now := time.Now()
  94. if ss.State != raft.StateLeader {
  95. ss.State = raft.StateLeader
  96. ss.LeaderInfo.Name = ss.ID
  97. ss.LeaderInfo.StartTime = now
  98. }
  99. ss.sendRateQueue.Insert(
  100. &RequestStats{
  101. SendingTime: now,
  102. Size: reqSize,
  103. },
  104. )
  105. ss.SendAppendRequestCnt++
  106. }