raft_stats.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. Copyright 2013 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 main
  14. import (
  15. "math"
  16. "sync"
  17. "time"
  18. "github.com/coreos/go-raft"
  19. )
  20. const (
  21. queueCapacity = 200
  22. )
  23. // packageStats represent the stats we need for a package.
  24. // It has sending time and the size of the package.
  25. type packageStats struct {
  26. sendingTime time.Time
  27. size int
  28. }
  29. // NewPackageStats creates a pacakgeStats and return the pointer to it.
  30. func NewPackageStats(now time.Time, size int) *packageStats {
  31. return &packageStats{
  32. sendingTime: now,
  33. size: size,
  34. }
  35. }
  36. // Time return the sending time of the package.
  37. func (ps *packageStats) Time() time.Time {
  38. return ps.sendingTime
  39. }
  40. type raftServerStats struct {
  41. State string `json:"state"`
  42. StartTime time.Time `json:"startTime"`
  43. LeaderInfo struct {
  44. Name string `json:"leader"`
  45. Uptime string `json:"uptime"`
  46. startTime time.Time
  47. } `json:"leaderInfo"`
  48. RecvAppendRequestCnt uint64 `json:"recvAppendRequestCnt,"`
  49. RecvingPkgRate float64 `json:"recvPkgRate,omitempty"`
  50. RecvingBandwidthRate float64 `json:"recvBandwidthRate,omitempty"`
  51. SendAppendRequestCnt uint64 `json:"sendAppendRequestCnt"`
  52. SendingPkgRate float64 `json:"sendPkgRate,omitempty"`
  53. SendingBandwidthRate float64 `json:"sendBandwidthRate,omitempty"`
  54. sendRateQueue *statsQueue
  55. recvRateQueue *statsQueue
  56. }
  57. func (ss *raftServerStats) RecvAppendReq(leaderName string, pkgSize int) {
  58. ss.State = raft.Follower
  59. if leaderName != ss.LeaderInfo.Name {
  60. ss.LeaderInfo.Name = leaderName
  61. ss.LeaderInfo.startTime = time.Now()
  62. }
  63. ss.recvRateQueue.Insert(NewPackageStats(time.Now(), pkgSize))
  64. ss.RecvAppendRequestCnt++
  65. }
  66. func (ss *raftServerStats) SendAppendReq(pkgSize int) {
  67. now := time.Now()
  68. if ss.State != raft.Leader {
  69. ss.State = raft.Leader
  70. ss.LeaderInfo.Name = r.Name()
  71. ss.LeaderInfo.startTime = now
  72. }
  73. ss.sendRateQueue.Insert(NewPackageStats(now, pkgSize))
  74. ss.SendAppendRequestCnt++
  75. }
  76. type raftFollowersStats struct {
  77. Leader string `json:"leader"`
  78. Followers map[string]*raftFollowerStats `json:"followers"`
  79. }
  80. type raftFollowerStats struct {
  81. Latency struct {
  82. Current float64 `json:"current"`
  83. Average float64 `json:"average"`
  84. averageSquare float64
  85. StandardDeviation float64 `json:"standardDeviation"`
  86. Minimum float64 `json:"minimum"`
  87. Maximum float64 `json:"maximum"`
  88. } `json:"latency"`
  89. Counts struct {
  90. Fail uint64 `json:"fail"`
  91. Success uint64 `json:"success"`
  92. } `json:"counts"`
  93. }
  94. // Succ function update the raftFollowerStats with a successful send
  95. func (ps *raftFollowerStats) Succ(d time.Duration) {
  96. total := float64(ps.Counts.Success) * ps.Latency.Average
  97. totalSquare := float64(ps.Counts.Success) * ps.Latency.averageSquare
  98. ps.Counts.Success++
  99. ps.Latency.Current = float64(d) / (1000000.0)
  100. if ps.Latency.Current > ps.Latency.Maximum {
  101. ps.Latency.Maximum = ps.Latency.Current
  102. }
  103. if ps.Latency.Current < ps.Latency.Minimum {
  104. ps.Latency.Minimum = ps.Latency.Current
  105. }
  106. ps.Latency.Average = (total + ps.Latency.Current) / float64(ps.Counts.Success)
  107. ps.Latency.averageSquare = (totalSquare + ps.Latency.Current*ps.Latency.Current) / float64(ps.Counts.Success)
  108. // sdv = sqrt(avg(x^2) - avg(x)^2)
  109. ps.Latency.StandardDeviation = math.Sqrt(ps.Latency.averageSquare - ps.Latency.Average*ps.Latency.Average)
  110. }
  111. // Fail function update the raftFollowerStats with a unsuccessful send
  112. func (ps *raftFollowerStats) Fail() {
  113. ps.Counts.Fail++
  114. }
  115. type statsQueue struct {
  116. items [queueCapacity]*packageStats
  117. size int
  118. front int
  119. back int
  120. totalPkgSize int
  121. rwl sync.RWMutex
  122. }
  123. func (q *statsQueue) Len() int {
  124. return q.size
  125. }
  126. func (q *statsQueue) PkgSize() int {
  127. return q.totalPkgSize
  128. }
  129. // FrontAndBack gets the front and back elements in the queue
  130. // We must grab front and back together with the protection of the lock
  131. func (q *statsQueue) frontAndBack() (*packageStats, *packageStats) {
  132. q.rwl.RLock()
  133. defer q.rwl.RUnlock()
  134. if q.size != 0 {
  135. return q.items[q.front], q.items[q.back]
  136. }
  137. return nil, nil
  138. }
  139. // Insert function insert a packageStats into the queue and update the records
  140. func (q *statsQueue) Insert(p *packageStats) {
  141. q.rwl.Lock()
  142. defer q.rwl.Unlock()
  143. q.back = (q.back + 1) % queueCapacity
  144. if q.size == queueCapacity { //dequeue
  145. q.totalPkgSize -= q.items[q.front].size
  146. q.front = (q.back + 1) % queueCapacity
  147. } else {
  148. q.size++
  149. }
  150. q.items[q.back] = p
  151. q.totalPkgSize += q.items[q.back].size
  152. }
  153. // Rate function returns the package rate and byte rate
  154. func (q *statsQueue) Rate() (float64, float64) {
  155. front, back := q.frontAndBack()
  156. if front == nil || back == nil {
  157. return 0, 0
  158. }
  159. if time.Now().Sub(back.Time()) > time.Second {
  160. q.Clear()
  161. return 0, 0
  162. }
  163. sampleDuration := back.Time().Sub(front.Time())
  164. pr := float64(q.Len()) / float64(sampleDuration) * float64(time.Second)
  165. br := float64(q.PkgSize()) / float64(sampleDuration) * float64(time.Second)
  166. return pr, br
  167. }
  168. // Clear function clear up the statsQueue
  169. func (q *statsQueue) Clear() {
  170. q.rwl.Lock()
  171. defer q.rwl.Unlock()
  172. q.back = -1
  173. q.front = 0
  174. q.size = 0
  175. q.totalPkgSize = 0
  176. }