epsilon_greedy.go 5.4 KB

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