server.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package stats
  15. import (
  16. "encoding/json"
  17. "log"
  18. "sync"
  19. "time"
  20. "github.com/coreos/etcd/raft"
  21. )
  22. // ServerStats encapsulates various statistics about an EtcdServer and its
  23. // communication with other members of the cluster
  24. type ServerStats struct {
  25. Name string `json:"name"`
  26. // TODO(jonboulle): use ID instead of name?
  27. ID string `json:"id"`
  28. State raft.StateType `json:"state"`
  29. StartTime time.Time `json:"startTime"`
  30. LeaderInfo struct {
  31. Name string `json:"leader"`
  32. Uptime string `json:"uptime"`
  33. StartTime time.Time `json:"startTime"`
  34. } `json:"leaderInfo"`
  35. RecvAppendRequestCnt uint64 `json:"recvAppendRequestCnt,"`
  36. RecvingPkgRate float64 `json:"recvPkgRate,omitempty"`
  37. RecvingBandwidthRate float64 `json:"recvBandwidthRate,omitempty"`
  38. SendAppendRequestCnt uint64 `json:"sendAppendRequestCnt"`
  39. SendingPkgRate float64 `json:"sendPkgRate,omitempty"`
  40. SendingBandwidthRate float64 `json:"sendBandwidthRate,omitempty"`
  41. sendRateQueue *statsQueue
  42. recvRateQueue *statsQueue
  43. sync.Mutex
  44. }
  45. func (ss *ServerStats) JSON() []byte {
  46. ss.Lock()
  47. stats := *ss
  48. ss.Unlock()
  49. stats.LeaderInfo.Uptime = time.Now().Sub(stats.LeaderInfo.StartTime).String()
  50. stats.SendingPkgRate, stats.SendingBandwidthRate = stats.SendRates()
  51. stats.RecvingPkgRate, stats.RecvingBandwidthRate = stats.RecvRates()
  52. b, err := json.Marshal(stats)
  53. // TODO(jonboulle): appropriate error handling?
  54. if err != nil {
  55. log.Printf("stats: error marshalling server stats: %v", err)
  56. }
  57. return b
  58. }
  59. // Initialize clears the statistics of ServerStats and resets its start time
  60. func (ss *ServerStats) Initialize() {
  61. if ss == nil {
  62. return
  63. }
  64. now := time.Now()
  65. ss.StartTime = now
  66. ss.LeaderInfo.StartTime = now
  67. ss.sendRateQueue = &statsQueue{
  68. back: -1,
  69. }
  70. ss.recvRateQueue = &statsQueue{
  71. back: -1,
  72. }
  73. }
  74. // RecvRates calculates and returns the rate of received append requests
  75. func (ss *ServerStats) RecvRates() (float64, float64) {
  76. return ss.recvRateQueue.Rate()
  77. }
  78. // SendRates calculates and returns the rate of sent append requests
  79. func (ss *ServerStats) SendRates() (float64, float64) {
  80. return ss.sendRateQueue.Rate()
  81. }
  82. // RecvAppendReq updates the ServerStats in response to an AppendRequest
  83. // from the given leader being received
  84. func (ss *ServerStats) RecvAppendReq(leader string, reqSize int) {
  85. ss.Lock()
  86. defer ss.Unlock()
  87. now := time.Now()
  88. ss.State = raft.StateFollower
  89. if leader != ss.LeaderInfo.Name {
  90. ss.LeaderInfo.Name = leader
  91. ss.LeaderInfo.StartTime = now
  92. }
  93. ss.recvRateQueue.Insert(
  94. &RequestStats{
  95. SendingTime: now,
  96. Size: reqSize,
  97. },
  98. )
  99. ss.RecvAppendRequestCnt++
  100. }
  101. // SendAppendReq updates the ServerStats in response to an AppendRequest
  102. // being sent by this server
  103. func (ss *ServerStats) SendAppendReq(reqSize int) {
  104. ss.Lock()
  105. defer ss.Unlock()
  106. now := time.Now()
  107. if ss.State != raft.StateLeader {
  108. ss.State = raft.StateLeader
  109. ss.LeaderInfo.Name = ss.ID
  110. ss.LeaderInfo.StartTime = now
  111. }
  112. ss.sendRateQueue.Insert(
  113. &RequestStats{
  114. SendingTime: now,
  115. Size: reqSize,
  116. },
  117. )
  118. ss.SendAppendRequestCnt++
  119. }
  120. func (ss *ServerStats) BecomeLeader() {
  121. if ss.State != raft.StateLeader {
  122. ss.State = raft.StateLeader
  123. ss.LeaderInfo.Name = ss.ID
  124. ss.LeaderInfo.StartTime = time.Now()
  125. }
  126. }