epsilon_greedy.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package hostpool
  2. import (
  3. "log"
  4. "math/rand"
  5. "time"
  6. )
  7. type epsilonHostPoolResponse struct {
  8. standardHostPoolResponse
  9. started time.Time
  10. ended time.Time
  11. }
  12. func (r *epsilonHostPoolResponse) Mark(err error) {
  13. r.Do(func() {
  14. r.ended = time.Now()
  15. doMark(err, r)
  16. })
  17. }
  18. type epsilonGreedyHostPool struct {
  19. standardHostPool // TODO - would be nifty if we could embed HostPool and Locker interfaces
  20. epsilon float32 // this is our exploration factor
  21. decayDuration time.Duration
  22. EpsilonValueCalculator // embed the epsilonValueCalculator
  23. timer
  24. quit chan bool
  25. }
  26. // Construct an Epsilon Greedy HostPool
  27. //
  28. // Epsilon Greedy is an algorithm that allows HostPool not only to track failure state,
  29. // but also to learn about "better" options in terms of speed, and to pick from available hosts
  30. // based on how well they perform. This gives a weighted request rate to better
  31. // performing hosts, while still distributing requests to all hosts (proportionate to their performance).
  32. // The interface is the same as the standard HostPool, but be sure to mark the HostResponse immediately
  33. // after executing the request to the host, as that will stop the implicitly running request timer.
  34. //
  35. // A good overview of Epsilon Greedy is here http://stevehanov.ca/blog/index.php?id=132
  36. //
  37. // To compute the weighting scores, we perform a weighted average of recent response times, over the course of
  38. // `decayDuration`. decayDuration may be set to 0 to use the default value of 5 minutes
  39. // We then use the supplied EpsilonValueCalculator to calculate a score from that weighted average response time.
  40. func NewEpsilonGreedy(hosts []string, decayDuration time.Duration, calc EpsilonValueCalculator) HostPool {
  41. if decayDuration <= 0 {
  42. decayDuration = defaultDecayDuration
  43. }
  44. stdHP := New(hosts).(*standardHostPool)
  45. p := &epsilonGreedyHostPool{
  46. standardHostPool: *stdHP,
  47. epsilon: float32(initialEpsilon),
  48. decayDuration: decayDuration,
  49. EpsilonValueCalculator: calc,
  50. timer: &realTimer{},
  51. quit: make(chan bool),
  52. }
  53. // allocate structures
  54. for _, h := range p.hostList {
  55. h.epsilonCounts = make([]int64, epsilonBuckets)
  56. h.epsilonValues = make([]int64, epsilonBuckets)
  57. }
  58. go p.epsilonGreedyDecay()
  59. return p
  60. }
  61. func (p *epsilonGreedyHostPool) Close() {
  62. // No need to do p.quit <- true as close(p.quit) does the trick.
  63. close(p.quit)
  64. }
  65. func (p *epsilonGreedyHostPool) SetEpsilon(newEpsilon float32) {
  66. p.Lock()
  67. defer p.Unlock()
  68. p.epsilon = newEpsilon
  69. }
  70. func (p *epsilonGreedyHostPool) SetHosts(hosts []string) {
  71. p.Lock()
  72. defer p.Unlock()
  73. p.standardHostPool.setHosts(hosts)
  74. for _, h := range p.hostList {
  75. h.epsilonCounts = make([]int64, epsilonBuckets)
  76. h.epsilonValues = make([]int64, epsilonBuckets)
  77. }
  78. }
  79. func (p *epsilonGreedyHostPool) epsilonGreedyDecay() {
  80. durationPerBucket := p.decayDuration / epsilonBuckets
  81. ticker := time.NewTicker(durationPerBucket)
  82. for {
  83. select {
  84. case <-p.quit:
  85. ticker.Stop()
  86. return
  87. case <-ticker.C:
  88. p.performEpsilonGreedyDecay()
  89. }
  90. }
  91. }
  92. func (p *epsilonGreedyHostPool) performEpsilonGreedyDecay() {
  93. p.Lock()
  94. for _, h := range p.hostList {
  95. h.epsilonIndex += 1
  96. h.epsilonIndex = h.epsilonIndex % epsilonBuckets
  97. h.epsilonCounts[h.epsilonIndex] = 0
  98. h.epsilonValues[h.epsilonIndex] = 0
  99. }
  100. p.Unlock()
  101. }
  102. func (p *epsilonGreedyHostPool) Get() HostPoolResponse {
  103. p.Lock()
  104. defer p.Unlock()
  105. host := p.getEpsilonGreedy()
  106. if host == "" {
  107. return nil
  108. }
  109. started := time.Now()
  110. return &epsilonHostPoolResponse{
  111. standardHostPoolResponse: standardHostPoolResponse{host: host, pool: p},
  112. started: started,
  113. }
  114. }
  115. func (p *epsilonGreedyHostPool) getEpsilonGreedy() string {
  116. var hostToUse *hostEntry
  117. // this is our exploration phase
  118. if rand.Float32() < p.epsilon {
  119. p.epsilon = p.epsilon * epsilonDecay
  120. if p.epsilon < minEpsilon {
  121. p.epsilon = minEpsilon
  122. }
  123. return p.getRoundRobin()
  124. }
  125. // calculate values for each host in the 0..1 range (but not ormalized)
  126. var possibleHosts []*hostEntry
  127. now := time.Now()
  128. var sumValues float64
  129. for _, h := range p.hostList {
  130. if h.canTryHost(now) {
  131. v := h.getWeightedAverageResponseTime()
  132. if v > 0 {
  133. ev := p.CalcValueFromAvgResponseTime(v)
  134. h.epsilonValue = ev
  135. sumValues += ev
  136. possibleHosts = append(possibleHosts, h)
  137. }
  138. }
  139. }
  140. if len(possibleHosts) != 0 {
  141. // now normalize to the 0..1 range to get a percentage
  142. for _, h := range possibleHosts {
  143. h.epsilonPercentage = h.epsilonValue / sumValues
  144. }
  145. // do a weighted random choice among hosts
  146. ceiling := 0.0
  147. pickPercentage := rand.Float64()
  148. for _, h := range possibleHosts {
  149. ceiling += h.epsilonPercentage
  150. if pickPercentage <= ceiling {
  151. hostToUse = h
  152. break
  153. }
  154. }
  155. }
  156. if hostToUse == nil {
  157. if len(possibleHosts) != 0 {
  158. log.Println("Failed to randomly choose a host, Dan loses")
  159. }
  160. return p.getRoundRobin()
  161. }
  162. if hostToUse.dead {
  163. hostToUse.willRetryHost(p.maxRetryInterval)
  164. }
  165. return hostToUse.host
  166. }
  167. func (p *epsilonGreedyHostPool) markSuccess(hostR HostPoolResponse) {
  168. // first do the base markSuccess - a little redundant with host lookup but cleaner than repeating logic
  169. p.standardHostPool.markSuccess(hostR)
  170. eHostR, ok := hostR.(*epsilonHostPoolResponse)
  171. if !ok {
  172. log.Printf("Incorrect type in eps markSuccess!") // TODO reflection to print out offending type
  173. return
  174. }
  175. host := eHostR.host
  176. duration := p.between(eHostR.started, eHostR.ended)
  177. p.Lock()
  178. defer p.Unlock()
  179. h, ok := p.hosts[host]
  180. if !ok {
  181. log.Fatalf("host %s not in HostPool %v", host, p.Hosts())
  182. }
  183. h.epsilonCounts[h.epsilonIndex]++
  184. h.epsilonValues[h.epsilonIndex] += int64(duration.Seconds() * 1000)
  185. }
  186. // --- timer: this just exists for testing
  187. type timer interface {
  188. between(time.Time, time.Time) time.Duration
  189. }
  190. type realTimer struct{}
  191. func (rt *realTimer) between(start time.Time, end time.Time) time.Duration {
  192. return end.Sub(start)
  193. }