epsilon_greedy.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. }
  25. // Epsilon Greedy is an algorithim 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 a percentage of 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. //
  30. // After enabling Epsilon Greedy, hosts must be marked for sucess along with a time value representing
  31. // how fast (or slow) that host was.
  32. //
  33. // host := pool.Get()
  34. // start := time.Now()
  35. // ..... do work with host
  36. // duration = time.Now().Sub(start)
  37. // pool.MarkSuccessWithTime(host, duration)
  38. //
  39. // a good overview of Epsilon Greedy is here http://stevehanov.ca/blog/index.php?id=132
  40. //
  41. // decayDuration may be set to 0 to use the default value of 5 minutes
  42. func NewEpsilonGreedy(hosts []string, decayDuration time.Duration, calc EpsilonValueCalculator) HostPool {
  43. if decayDuration <= 0 {
  44. decayDuration = defaultDecayDuration
  45. }
  46. stdHP := New(hosts).(*standardHostPool)
  47. p := &epsilonGreedyHostPool{
  48. standardHostPool: *stdHP,
  49. epsilon: float32(initialEpsilon),
  50. decayDuration: decayDuration,
  51. EpsilonValueCalculator: calc,
  52. timer: &realTimer{},
  53. }
  54. // allocate structures
  55. for _, h := range p.hostList {
  56. h.epsilonCounts = make([]int64, epsilonBuckets)
  57. h.epsilonValues = make([]int64, epsilonBuckets)
  58. }
  59. go p.epsilonGreedyDecay()
  60. return p
  61. }
  62. func (p *epsilonGreedyHostPool) SetEpsilon(newEpsilon float32) {
  63. p.Lock()
  64. defer p.Unlock()
  65. p.epsilon = newEpsilon
  66. }
  67. func (p *epsilonGreedyHostPool) epsilonGreedyDecay() {
  68. durationPerBucket := p.decayDuration / epsilonBuckets
  69. ticker := time.Tick(durationPerBucket)
  70. for {
  71. <-ticker
  72. p.performEpsilonGreedyDecay()
  73. }
  74. }
  75. func (p *epsilonGreedyHostPool) performEpsilonGreedyDecay() {
  76. p.Lock()
  77. for _, h := range p.hostList {
  78. h.epsilonIndex += 1
  79. h.epsilonIndex = h.epsilonIndex % epsilonBuckets
  80. h.epsilonCounts[h.epsilonIndex] = 0
  81. h.epsilonValues[h.epsilonIndex] = 0
  82. }
  83. p.Unlock()
  84. }
  85. func (p *epsilonGreedyHostPool) Get() HostPoolResponse {
  86. p.Lock()
  87. defer p.Unlock()
  88. host := p.getEpsilonGreedy()
  89. started := time.Now()
  90. return &epsilonHostPoolResponse{
  91. standardHostPoolResponse: standardHostPoolResponse{host: host, pool: p},
  92. started: started,
  93. }
  94. }
  95. func (p *epsilonGreedyHostPool) getEpsilonGreedy() string {
  96. var hostToUse *hostEntry
  97. // this is our exploration phase
  98. if rand.Float32() < p.epsilon {
  99. p.epsilon = p.epsilon * epsilonDecay
  100. if p.epsilon < minEpsilon {
  101. p.epsilon = minEpsilon
  102. }
  103. return p.getRoundRobin()
  104. }
  105. // calculate values for each host in the 0..1 range (but not ormalized)
  106. var possibleHosts []*hostEntry
  107. now := time.Now()
  108. var sumValues float64
  109. for _, h := range p.hostList {
  110. if h.canTryHost(now) {
  111. v := h.getWeightedAverageResponseTime()
  112. if v > 0 {
  113. ev := p.CalcValueFromAvgResponseTime(v)
  114. h.epsilonValue = ev
  115. sumValues += ev
  116. possibleHosts = append(possibleHosts, h)
  117. }
  118. }
  119. }
  120. if len(possibleHosts) != 0 {
  121. // now normalize to the 0..1 range to get a percentage
  122. for _, h := range possibleHosts {
  123. h.epsilonPercentage = h.epsilonValue / sumValues
  124. }
  125. // do a weighted random choice among hosts
  126. ceiling := 0.0
  127. pickPercentage := rand.Float64()
  128. for _, h := range possibleHosts {
  129. ceiling += h.epsilonPercentage
  130. if pickPercentage <= ceiling {
  131. hostToUse = h
  132. break
  133. }
  134. }
  135. }
  136. if hostToUse == nil {
  137. if len(possibleHosts) != 0 {
  138. log.Println("Failed to randomly choose a host, Dan loses")
  139. }
  140. return p.getRoundRobin()
  141. }
  142. if hostToUse.dead {
  143. hostToUse.willRetryHost(p.maxRetryInterval)
  144. }
  145. return hostToUse.host
  146. }
  147. func (p *epsilonGreedyHostPool) markSuccess(hostR HostPoolResponse) {
  148. // first do the base markSuccess - a little redundant with host lookup but cleaner than repeating logic
  149. p.standardHostPool.markSuccess(hostR)
  150. eHostR, ok := hostR.(*epsilonHostPoolResponse)
  151. if !ok {
  152. log.Printf("Incorrect type in eps markSuccess!") // TODO reflection to print out offending type
  153. return
  154. }
  155. host := eHostR.host
  156. duration := p.between(eHostR.started, eHostR.ended)
  157. p.Lock()
  158. defer p.Unlock()
  159. h, ok := p.hosts[host]
  160. if !ok {
  161. log.Fatalf("host %s not in HostPool %v", host, p.Hosts())
  162. }
  163. h.epsilonCounts[h.epsilonIndex]++
  164. h.epsilonValues[h.epsilonIndex] += int64(duration.Seconds() * 1000)
  165. }
  166. // --- timer: this just exists for testing
  167. type timer interface {
  168. between(time.Time, time.Time) time.Duration
  169. }
  170. type realTimer struct{}
  171. func (rt *realTimer) between(start time.Time, end time.Time) time.Duration {
  172. return end.Sub(start)
  173. }