server.go 2.6 KB

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