epsilon_greedy.go 5.3 KB

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