manager.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package flowcontrol
  17. import (
  18. "fmt"
  19. "math"
  20. "sync"
  21. "time"
  22. "github.com/ethereum/go-ethereum/common/mclock"
  23. "github.com/ethereum/go-ethereum/common/prque"
  24. )
  25. // cmNodeFields are ClientNode fields used by the client manager
  26. // Note: these fields are locked by the client manager's mutex
  27. type cmNodeFields struct {
  28. corrBufValue int64 // buffer value adjusted with the extra recharge amount
  29. rcLastIntValue int64 // past recharge integrator value when corrBufValue was last updated
  30. rcFullIntValue int64 // future recharge integrator value when corrBufValue will reach maximum
  31. queueIndex int // position in the recharge queue (-1 if not queued)
  32. }
  33. // FixedPointMultiplier is applied to the recharge integrator and the recharge curve.
  34. //
  35. // Note: fixed point arithmetic is required for the integrator because it is a
  36. // constantly increasing value that can wrap around int64 limits (which behavior is
  37. // also supported by the priority queue). A floating point value would gradually lose
  38. // precision in this application.
  39. // The recharge curve and all recharge values are encoded as fixed point because
  40. // sumRecharge is frequently updated by adding or subtracting individual recharge
  41. // values and perfect precision is required.
  42. const FixedPointMultiplier = 1000000
  43. var (
  44. capacityDropFactor = 0.1
  45. capacityRaiseTC = 1 / (3 * float64(time.Hour)) // time constant for raising the capacity factor
  46. capacityRaiseThresholdRatio = 1.125 // total/connected capacity ratio threshold for raising the capacity factor
  47. )
  48. // ClientManager controls the capacity assigned to the clients of a server.
  49. // Since ServerParams guarantee a safe lower estimate for processable requests
  50. // even in case of all clients being active, ClientManager calculates a
  51. // corrugated buffer value and usually allows a higher remaining buffer value
  52. // to be returned with each reply.
  53. type ClientManager struct {
  54. clock mclock.Clock
  55. lock sync.Mutex
  56. stop chan chan struct{}
  57. curve PieceWiseLinear
  58. sumRecharge, totalRecharge, totalConnected uint64
  59. logTotalCap, totalCapacity float64
  60. logTotalCapRaiseLimit float64
  61. minLogTotalCap, maxLogTotalCap float64
  62. capacityRaiseThreshold uint64
  63. capLastUpdate mclock.AbsTime
  64. totalCapacityCh chan uint64
  65. // recharge integrator is increasing in each moment with a rate of
  66. // (totalRecharge / sumRecharge)*FixedPointMultiplier or 0 if sumRecharge==0
  67. rcLastUpdate mclock.AbsTime // last time the recharge integrator was updated
  68. rcLastIntValue int64 // last updated value of the recharge integrator
  69. // recharge queue is a priority queue with currently recharging client nodes
  70. // as elements. The priority value is rcFullIntValue which allows to quickly
  71. // determine which client will first finish recharge.
  72. rcQueue *prque.Prque
  73. }
  74. // NewClientManager returns a new client manager.
  75. // Client manager enhances flow control performance by allowing client buffers
  76. // to recharge quicker than the minimum guaranteed recharge rate if possible.
  77. // The sum of all minimum recharge rates (sumRecharge) is updated each time
  78. // a clients starts or finishes buffer recharging. Then an adjusted total
  79. // recharge rate is calculated using a piecewise linear recharge curve:
  80. //
  81. // totalRecharge = curve(sumRecharge)
  82. // (totalRecharge >= sumRecharge is enforced)
  83. //
  84. // Then the "bonus" buffer recharge is distributed between currently recharging
  85. // clients proportionally to their minimum recharge rates.
  86. //
  87. // Note: total recharge is proportional to the average number of parallel running
  88. // serving threads. A recharge value of 1000000 corresponds to one thread in average.
  89. // The maximum number of allowed serving threads should always be considerably
  90. // higher than the targeted average number.
  91. //
  92. // Note 2: although it is possible to specify a curve allowing the total target
  93. // recharge starting from zero sumRecharge, it makes sense to add a linear ramp
  94. // starting from zero in order to not let a single low-priority client use up
  95. // the entire server capacity and thus ensure quick availability for others at
  96. // any moment.
  97. func NewClientManager(curve PieceWiseLinear, clock mclock.Clock) *ClientManager {
  98. cm := &ClientManager{
  99. clock: clock,
  100. rcQueue: prque.NewWrapAround(func(a interface{}, i int) { a.(*ClientNode).queueIndex = i }),
  101. capLastUpdate: clock.Now(),
  102. stop: make(chan chan struct{}),
  103. }
  104. if curve != nil {
  105. cm.SetRechargeCurve(curve)
  106. }
  107. go func() {
  108. // regularly recalculate and update total capacity
  109. for {
  110. select {
  111. case <-time.After(time.Minute):
  112. cm.lock.Lock()
  113. cm.updateTotalCapacity(cm.clock.Now(), true)
  114. cm.lock.Unlock()
  115. case stop := <-cm.stop:
  116. close(stop)
  117. return
  118. }
  119. }
  120. }()
  121. return cm
  122. }
  123. // Stop stops the client manager
  124. func (cm *ClientManager) Stop() {
  125. stop := make(chan struct{})
  126. cm.stop <- stop
  127. <-stop
  128. }
  129. // SetRechargeCurve updates the recharge curve
  130. func (cm *ClientManager) SetRechargeCurve(curve PieceWiseLinear) {
  131. cm.lock.Lock()
  132. defer cm.lock.Unlock()
  133. now := cm.clock.Now()
  134. cm.updateRecharge(now)
  135. cm.curve = curve
  136. if len(curve) > 0 {
  137. cm.totalRecharge = curve[len(curve)-1].Y
  138. } else {
  139. cm.totalRecharge = 0
  140. }
  141. }
  142. // SetCapacityRaiseThreshold sets a threshold value used for raising capFactor.
  143. // Either if the difference between total allowed and connected capacity is less
  144. // than this threshold or if their ratio is less than capacityRaiseThresholdRatio
  145. // then capFactor is allowed to slowly raise.
  146. func (cm *ClientManager) SetCapacityLimits(min, max, raiseThreshold uint64) {
  147. if min < 1 {
  148. min = 1
  149. }
  150. cm.minLogTotalCap = math.Log(float64(min))
  151. if max < 1 {
  152. max = 1
  153. }
  154. cm.maxLogTotalCap = math.Log(float64(max))
  155. cm.logTotalCap = cm.maxLogTotalCap
  156. cm.capacityRaiseThreshold = raiseThreshold
  157. cm.refreshCapacity()
  158. }
  159. // connect should be called when a client is connected, before passing it to any
  160. // other ClientManager function
  161. func (cm *ClientManager) connect(node *ClientNode) {
  162. cm.lock.Lock()
  163. defer cm.lock.Unlock()
  164. now := cm.clock.Now()
  165. cm.updateRecharge(now)
  166. node.corrBufValue = int64(node.params.BufLimit)
  167. node.rcLastIntValue = cm.rcLastIntValue
  168. node.queueIndex = -1
  169. cm.updateTotalCapacity(now, true)
  170. cm.totalConnected += node.params.MinRecharge
  171. cm.updateRaiseLimit()
  172. }
  173. // disconnect should be called when a client is disconnected
  174. func (cm *ClientManager) disconnect(node *ClientNode) {
  175. cm.lock.Lock()
  176. defer cm.lock.Unlock()
  177. now := cm.clock.Now()
  178. cm.updateRecharge(cm.clock.Now())
  179. cm.updateTotalCapacity(now, true)
  180. cm.totalConnected -= node.params.MinRecharge
  181. cm.updateRaiseLimit()
  182. }
  183. // accepted is called when a request with given maximum cost is accepted.
  184. // It returns a priority indicator for the request which is used to determine placement
  185. // in the serving queue. Older requests have higher priority by default. If the client
  186. // is almost out of buffer, request priority is reduced.
  187. func (cm *ClientManager) accepted(node *ClientNode, maxCost uint64, now mclock.AbsTime) (priority int64) {
  188. cm.lock.Lock()
  189. defer cm.lock.Unlock()
  190. cm.updateNodeRc(node, -int64(maxCost), &node.params, now)
  191. rcTime := (node.params.BufLimit - uint64(node.corrBufValue)) * FixedPointMultiplier / node.params.MinRecharge
  192. return -int64(now) - int64(rcTime)
  193. }
  194. // processed updates the client buffer according to actual request cost after
  195. // serving has been finished.
  196. //
  197. // Note: processed should always be called for all accepted requests
  198. func (cm *ClientManager) processed(node *ClientNode, maxCost, realCost uint64, now mclock.AbsTime) {
  199. if realCost > maxCost {
  200. realCost = maxCost
  201. }
  202. cm.updateBuffer(node, int64(maxCost-realCost), now)
  203. }
  204. // updateBuffer recalulates the corrected buffer value, adds the given value to it
  205. // and updates the node's actual buffer value if possible
  206. func (cm *ClientManager) updateBuffer(node *ClientNode, add int64, now mclock.AbsTime) {
  207. cm.lock.Lock()
  208. defer cm.lock.Unlock()
  209. cm.updateNodeRc(node, add, &node.params, now)
  210. if node.corrBufValue > node.bufValue {
  211. if node.log != nil {
  212. node.log.add(now, fmt.Sprintf("corrected bv=%d oldBv=%d", node.corrBufValue, node.bufValue))
  213. }
  214. node.bufValue = node.corrBufValue
  215. }
  216. }
  217. // updateParams updates the flow control parameters of a client node
  218. func (cm *ClientManager) updateParams(node *ClientNode, params ServerParams, now mclock.AbsTime) {
  219. cm.lock.Lock()
  220. defer cm.lock.Unlock()
  221. cm.updateRecharge(now)
  222. cm.updateTotalCapacity(now, true)
  223. cm.totalConnected += params.MinRecharge - node.params.MinRecharge
  224. cm.updateRaiseLimit()
  225. cm.updateNodeRc(node, 0, &params, now)
  226. }
  227. // updateRaiseLimit recalculates the limiting value until which logTotalCap
  228. // can be raised when no client freeze events occur
  229. func (cm *ClientManager) updateRaiseLimit() {
  230. if cm.capacityRaiseThreshold == 0 {
  231. cm.logTotalCapRaiseLimit = 0
  232. return
  233. }
  234. limit := float64(cm.totalConnected + cm.capacityRaiseThreshold)
  235. limit2 := float64(cm.totalConnected) * capacityRaiseThresholdRatio
  236. if limit2 > limit {
  237. limit = limit2
  238. }
  239. if limit < 1 {
  240. limit = 1
  241. }
  242. cm.logTotalCapRaiseLimit = math.Log(limit)
  243. }
  244. // updateRecharge updates the recharge integrator and checks the recharge queue
  245. // for nodes with recently filled buffers
  246. func (cm *ClientManager) updateRecharge(now mclock.AbsTime) {
  247. lastUpdate := cm.rcLastUpdate
  248. cm.rcLastUpdate = now
  249. // updating is done in multiple steps if node buffers are filled and sumRecharge
  250. // is decreased before the given target time
  251. for cm.sumRecharge > 0 {
  252. sumRecharge := cm.sumRecharge
  253. if sumRecharge > cm.totalRecharge {
  254. sumRecharge = cm.totalRecharge
  255. }
  256. bonusRatio := float64(1)
  257. v := cm.curve.ValueAt(sumRecharge)
  258. s := float64(sumRecharge)
  259. if v > s && s > 0 {
  260. bonusRatio = v / s
  261. }
  262. dt := now - lastUpdate
  263. // fetch the client that finishes first
  264. rcqNode := cm.rcQueue.PopItem().(*ClientNode) // if sumRecharge > 0 then the queue cannot be empty
  265. // check whether it has already finished
  266. dtNext := mclock.AbsTime(float64(rcqNode.rcFullIntValue-cm.rcLastIntValue) / bonusRatio)
  267. if dt < dtNext {
  268. // not finished yet, put it back, update integrator according
  269. // to current bonusRatio and return
  270. cm.rcQueue.Push(rcqNode, -rcqNode.rcFullIntValue)
  271. cm.rcLastIntValue += int64(bonusRatio * float64(dt))
  272. return
  273. }
  274. lastUpdate += dtNext
  275. // finished recharging, update corrBufValue and sumRecharge if necessary and do next step
  276. if rcqNode.corrBufValue < int64(rcqNode.params.BufLimit) {
  277. rcqNode.corrBufValue = int64(rcqNode.params.BufLimit)
  278. cm.sumRecharge -= rcqNode.params.MinRecharge
  279. }
  280. cm.rcLastIntValue = rcqNode.rcFullIntValue
  281. }
  282. }
  283. // updateNodeRc updates a node's corrBufValue and adds an external correction value.
  284. // It also adds or removes the rcQueue entry and updates ServerParams and sumRecharge if necessary.
  285. func (cm *ClientManager) updateNodeRc(node *ClientNode, bvc int64, params *ServerParams, now mclock.AbsTime) {
  286. cm.updateRecharge(now)
  287. wasFull := true
  288. if node.corrBufValue != int64(node.params.BufLimit) {
  289. wasFull = false
  290. node.corrBufValue += (cm.rcLastIntValue - node.rcLastIntValue) * int64(node.params.MinRecharge) / FixedPointMultiplier
  291. if node.corrBufValue > int64(node.params.BufLimit) {
  292. node.corrBufValue = int64(node.params.BufLimit)
  293. }
  294. node.rcLastIntValue = cm.rcLastIntValue
  295. }
  296. node.corrBufValue += bvc
  297. diff := int64(params.BufLimit - node.params.BufLimit)
  298. if diff > 0 {
  299. node.corrBufValue += diff
  300. }
  301. isFull := false
  302. if node.corrBufValue >= int64(params.BufLimit) {
  303. node.corrBufValue = int64(params.BufLimit)
  304. isFull = true
  305. }
  306. if !wasFull {
  307. cm.sumRecharge -= node.params.MinRecharge
  308. }
  309. if params != &node.params {
  310. node.params = *params
  311. }
  312. if !isFull {
  313. cm.sumRecharge += node.params.MinRecharge
  314. if node.queueIndex != -1 {
  315. cm.rcQueue.Remove(node.queueIndex)
  316. }
  317. node.rcLastIntValue = cm.rcLastIntValue
  318. node.rcFullIntValue = cm.rcLastIntValue + (int64(node.params.BufLimit)-node.corrBufValue)*FixedPointMultiplier/int64(node.params.MinRecharge)
  319. cm.rcQueue.Push(node, -node.rcFullIntValue)
  320. }
  321. }
  322. // reduceTotalCapacity reduces the total capacity allowance in case of a client freeze event
  323. func (cm *ClientManager) reduceTotalCapacity(frozenCap uint64) {
  324. cm.lock.Lock()
  325. defer cm.lock.Unlock()
  326. ratio := float64(1)
  327. if frozenCap < cm.totalConnected {
  328. ratio = float64(frozenCap) / float64(cm.totalConnected)
  329. }
  330. now := cm.clock.Now()
  331. cm.updateTotalCapacity(now, false)
  332. cm.logTotalCap -= capacityDropFactor * ratio
  333. if cm.logTotalCap < cm.minLogTotalCap {
  334. cm.logTotalCap = cm.minLogTotalCap
  335. }
  336. cm.updateTotalCapacity(now, true)
  337. }
  338. // updateTotalCapacity updates the total capacity factor. The capacity factor allows
  339. // the total capacity of the system to go over the allowed total recharge value
  340. // if clients go to frozen state sufficiently rarely.
  341. // The capacity factor is dropped instantly by a small amount if a clients is frozen.
  342. // It is raised slowly (with a large time constant) if the total connected capacity
  343. // is close to the total allowed amount and no clients are frozen.
  344. func (cm *ClientManager) updateTotalCapacity(now mclock.AbsTime, refresh bool) {
  345. dt := now - cm.capLastUpdate
  346. cm.capLastUpdate = now
  347. if cm.logTotalCap < cm.logTotalCapRaiseLimit {
  348. cm.logTotalCap += capacityRaiseTC * float64(dt)
  349. if cm.logTotalCap > cm.logTotalCapRaiseLimit {
  350. cm.logTotalCap = cm.logTotalCapRaiseLimit
  351. }
  352. }
  353. if cm.logTotalCap > cm.maxLogTotalCap {
  354. cm.logTotalCap = cm.maxLogTotalCap
  355. }
  356. if refresh {
  357. cm.refreshCapacity()
  358. }
  359. }
  360. // refreshCapacity recalculates the total capacity value and sends an update to the subscription
  361. // channel if the relative change of the value since the last update is more than 0.1 percent
  362. func (cm *ClientManager) refreshCapacity() {
  363. totalCapacity := math.Exp(cm.logTotalCap)
  364. if totalCapacity >= cm.totalCapacity*0.999 && totalCapacity <= cm.totalCapacity*1.001 {
  365. return
  366. }
  367. cm.totalCapacity = totalCapacity
  368. if cm.totalCapacityCh != nil {
  369. select {
  370. case cm.totalCapacityCh <- uint64(cm.totalCapacity):
  371. default:
  372. }
  373. }
  374. }
  375. // SubscribeTotalCapacity returns all future updates to the total capacity value
  376. // through a channel and also returns the current value
  377. func (cm *ClientManager) SubscribeTotalCapacity(ch chan uint64) uint64 {
  378. cm.lock.Lock()
  379. defer cm.lock.Unlock()
  380. cm.totalCapacityCh = ch
  381. return uint64(cm.totalCapacity)
  382. }
  383. // PieceWiseLinear is used to describe recharge curves
  384. type PieceWiseLinear []struct{ X, Y uint64 }
  385. // ValueAt returns the curve's value at a given point
  386. func (pwl PieceWiseLinear) ValueAt(x uint64) float64 {
  387. l := 0
  388. h := len(pwl)
  389. if h == 0 {
  390. return 0
  391. }
  392. for h != l {
  393. m := (l + h) / 2
  394. if x > pwl[m].X {
  395. l = m + 1
  396. } else {
  397. h = m
  398. }
  399. }
  400. if l == 0 {
  401. return float64(pwl[0].Y)
  402. }
  403. l--
  404. if h == len(pwl) {
  405. return float64(pwl[l].Y)
  406. }
  407. dx := pwl[h].X - pwl[l].X
  408. if dx < 1 {
  409. return float64(pwl[l].Y)
  410. }
  411. return float64(pwl[l].Y) + float64(pwl[h].Y-pwl[l].Y)*float64(x-pwl[l].X)/float64(dx)
  412. }
  413. // Valid returns true if the X coordinates of the curve points are non-strictly monotonic
  414. func (pwl PieceWiseLinear) Valid() bool {
  415. var lastX uint64
  416. for _, i := range pwl {
  417. if i.X < lastX {
  418. return false
  419. }
  420. lastX = i.X
  421. }
  422. return true
  423. }