server.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2015 The etcd Authors
  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. // ID is the raft ID of the node.
  27. // TODO(jonboulle): use ID instead of name?
  28. ID string `json:"id"`
  29. State raft.StateType `json:"state"`
  30. StartTime time.Time `json:"startTime"`
  31. LeaderInfo struct {
  32. Name string `json:"leader"`
  33. Uptime string `json:"uptime"`
  34. StartTime time.Time `json:"startTime"`
  35. } `json:"leaderInfo"`
  36. RecvAppendRequestCnt uint64 `json:"recvAppendRequestCnt,"`
  37. RecvingPkgRate float64 `json:"recvPkgRate,omitempty"`
  38. RecvingBandwidthRate float64 `json:"recvBandwidthRate,omitempty"`
  39. SendAppendRequestCnt uint64 `json:"sendAppendRequestCnt"`
  40. SendingPkgRate float64 `json:"sendPkgRate,omitempty"`
  41. SendingBandwidthRate float64 `json:"sendBandwidthRate,omitempty"`
  42. sendRateQueue *statsQueue
  43. recvRateQueue *statsQueue
  44. sync.Mutex
  45. }
  46. func (ss *ServerStats) JSON() []byte {
  47. ss.Lock()
  48. stats := *ss
  49. ss.Unlock()
  50. stats.LeaderInfo.Uptime = time.Since(stats.LeaderInfo.StartTime).String()
  51. stats.SendingPkgRate, stats.SendingBandwidthRate = stats.SendRates()
  52. stats.RecvingPkgRate, stats.RecvingBandwidthRate = stats.RecvRates()
  53. b, err := json.Marshal(stats)
  54. // TODO(jonboulle): appropriate error handling?
  55. if err != nil {
  56. log.Printf("stats: error marshalling server stats: %v", err)
  57. }
  58. return b
  59. }
  60. // Initialize clears the statistics of ServerStats and resets its start time
  61. func (ss *ServerStats) Initialize() {
  62. if ss == nil {
  63. return
  64. }
  65. now := time.Now()
  66. ss.StartTime = now
  67. ss.LeaderInfo.StartTime = now
  68. ss.sendRateQueue = &statsQueue{
  69. back: -1,
  70. }
  71. ss.recvRateQueue = &statsQueue{
  72. back: -1,
  73. }
  74. }
  75. // RecvRates calculates and returns the rate of received append requests
  76. func (ss *ServerStats) RecvRates() (float64, float64) {
  77. return ss.recvRateQueue.Rate()
  78. }
  79. // SendRates calculates and returns the rate of sent append requests
  80. func (ss *ServerStats) SendRates() (float64, float64) {
  81. return ss.sendRateQueue.Rate()
  82. }
  83. // RecvAppendReq updates the ServerStats in response to an AppendRequest
  84. // from the given leader being received
  85. func (ss *ServerStats) RecvAppendReq(leader string, reqSize int) {
  86. ss.Lock()
  87. defer ss.Unlock()
  88. now := time.Now()
  89. ss.State = raft.StateFollower
  90. if leader != ss.LeaderInfo.Name {
  91. ss.LeaderInfo.Name = leader
  92. ss.LeaderInfo.StartTime = now
  93. }
  94. ss.recvRateQueue.Insert(
  95. &RequestStats{
  96. SendingTime: now,
  97. Size: reqSize,
  98. },
  99. )
  100. ss.RecvAppendRequestCnt++
  101. }
  102. // SendAppendReq updates the ServerStats in response to an AppendRequest
  103. // being sent by this server
  104. func (ss *ServerStats) SendAppendReq(reqSize int) {
  105. ss.Lock()
  106. defer ss.Unlock()
  107. ss.becomeLeader()
  108. ss.sendRateQueue.Insert(
  109. &RequestStats{
  110. SendingTime: time.Now(),
  111. Size: reqSize,
  112. },
  113. )
  114. ss.SendAppendRequestCnt++
  115. }
  116. func (ss *ServerStats) BecomeLeader() {
  117. ss.Lock()
  118. defer ss.Unlock()
  119. ss.becomeLeader()
  120. }
  121. func (ss *ServerStats) becomeLeader() {
  122. if ss.State != raft.StateLeader {
  123. ss.State = raft.StateLeader
  124. ss.LeaderInfo.Name = ss.ID
  125. ss.LeaderInfo.StartTime = time.Now()
  126. }
  127. }