server.go 3.5 KB

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