server.go 3.6 KB

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