policies.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Copyright (c) 2012 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //This file will be the future home for more policies
  5. package gocql
  6. import (
  7. "log"
  8. "sync"
  9. "sync/atomic"
  10. )
  11. //RetryableQuery is an interface that represents a query or batch statement that
  12. //exposes the correct functions for the retry policy logic to evaluate correctly.
  13. type RetryableQuery interface {
  14. Attempts() int
  15. GetConsistency() Consistency
  16. }
  17. // RetryPolicy interace is used by gocql to determine if a query can be attempted
  18. // again after a retryable error has been received. The interface allows gocql
  19. // users to implement their own logic to determine if a query can be attempted
  20. // again.
  21. //
  22. // See SimpleRetryPolicy as an example of implementing and using a RetryPolicy
  23. // interface.
  24. type RetryPolicy interface {
  25. Attempt(RetryableQuery) bool
  26. }
  27. /*
  28. SimpleRetryPolicy has simple logic for attempting a query a fixed number of times.
  29. See below for examples of usage:
  30. //Assign to the cluster
  31. cluster.RetryPolicy = &gocql.SimpleRetryPolicy{NumRetries: 3}
  32. //Assign to a query
  33. query.RetryPolicy(&gocql.SimpleRetryPolicy{NumRetries: 1})
  34. */
  35. type SimpleRetryPolicy struct {
  36. NumRetries int //Number of times to retry a query
  37. }
  38. // Attempt tells gocql to attempt the query again based on query.Attempts being less
  39. // than the NumRetries defined in the policy.
  40. func (s *SimpleRetryPolicy) Attempt(q RetryableQuery) bool {
  41. return q.Attempts() <= s.NumRetries
  42. }
  43. //HostSelectionPolicy is an interface for selecting
  44. //the most appropriate host to execute a given query.
  45. type HostSelectionPolicy interface {
  46. SetHosts
  47. SetPartitioner
  48. //Pick returns an iteration function over selected hosts
  49. Pick(*Query) NextHost
  50. }
  51. //NextHost is an iteration function over picked hosts
  52. type NextHost func() *HostInfo
  53. //NewRoundRobinHostPolicy is a round-robin load balancing policy
  54. func NewRoundRobinHostPolicy() HostSelectionPolicy {
  55. return &roundRobinHostPolicy{hosts: []HostInfo{}}
  56. }
  57. type roundRobinHostPolicy struct {
  58. hosts []HostInfo
  59. pos uint32
  60. mu sync.RWMutex
  61. }
  62. func (r *roundRobinHostPolicy) SetHosts(hosts []HostInfo) {
  63. r.mu.Lock()
  64. r.hosts = hosts
  65. r.mu.Unlock()
  66. }
  67. func (r *roundRobinHostPolicy) SetPartitioner(partitioner string) {
  68. // noop
  69. }
  70. func (r *roundRobinHostPolicy) Pick(qry *Query) NextHost {
  71. // i is used to limit the number of attempts to find a host
  72. // to the number of hosts known to this policy
  73. var i uint32 = 0
  74. return func() *HostInfo {
  75. r.mu.RLock()
  76. if len(r.hosts) == 0 {
  77. r.mu.RUnlock()
  78. return nil
  79. }
  80. var host *HostInfo
  81. // always increment pos to evenly distribute traffic in case of
  82. // failures
  83. pos := atomic.AddUint32(&r.pos, 1)
  84. if int(i) < len(r.hosts) {
  85. host = &r.hosts[(pos)%uint32(len(r.hosts))]
  86. i++
  87. }
  88. r.mu.RUnlock()
  89. return host
  90. }
  91. }
  92. //NewTokenAwareHostPolicy is a token aware host selection policy
  93. func NewTokenAwareHostPolicy(fallback HostSelectionPolicy) HostSelectionPolicy {
  94. return &tokenAwareHostPolicy{fallback: fallback, hosts: []HostInfo{}}
  95. }
  96. type tokenAwareHostPolicy struct {
  97. mu sync.RWMutex
  98. hosts []HostInfo
  99. partitioner string
  100. tokenRing *tokenRing
  101. fallback HostSelectionPolicy
  102. }
  103. func (t *tokenAwareHostPolicy) SetHosts(hosts []HostInfo) {
  104. t.mu.Lock()
  105. defer t.mu.Unlock()
  106. // always update the fallback
  107. t.fallback.SetHosts(hosts)
  108. t.hosts = hosts
  109. t.resetTokenRing()
  110. }
  111. func (t *tokenAwareHostPolicy) SetPartitioner(partitioner string) {
  112. t.mu.Lock()
  113. defer t.mu.Unlock()
  114. if t.partitioner != partitioner {
  115. t.fallback.SetPartitioner(partitioner)
  116. t.partitioner = partitioner
  117. t.resetTokenRing()
  118. }
  119. }
  120. func (t *tokenAwareHostPolicy) resetTokenRing() {
  121. if t.partitioner == "" {
  122. // partitioner not yet set
  123. return
  124. }
  125. // create a new token ring
  126. tokenRing, err := newTokenRing(t.partitioner, t.hosts)
  127. if err != nil {
  128. log.Printf("Unable to update the token ring due to error: %s", err)
  129. return
  130. }
  131. // replace the token ring
  132. t.tokenRing = tokenRing
  133. }
  134. func (t *tokenAwareHostPolicy) Pick(qry *Query) NextHost {
  135. if qry == nil {
  136. return t.fallback.Pick(qry)
  137. }
  138. routingKey, err := qry.GetRoutingKey()
  139. if err != nil {
  140. return t.fallback.Pick(qry)
  141. }
  142. if routingKey == nil {
  143. return t.fallback.Pick(qry)
  144. }
  145. var host *HostInfo
  146. t.mu.RLock()
  147. // TODO retrieve a list of hosts based on the replication strategy
  148. host = t.tokenRing.GetHostForPartitionKey(routingKey)
  149. t.mu.RUnlock()
  150. if host == nil {
  151. return t.fallback.Pick(qry)
  152. }
  153. // scope these variables for the same lifetime as the iterator function
  154. var (
  155. hostReturned bool
  156. fallbackIter NextHost
  157. )
  158. return func() *HostInfo {
  159. if !hostReturned {
  160. hostReturned = true
  161. return host
  162. }
  163. // fallback
  164. if fallbackIter == nil {
  165. fallbackIter = t.fallback.Pick(qry)
  166. }
  167. fallbackHost := fallbackIter()
  168. // filter the token aware selected hosts from the fallback hosts
  169. if fallbackHost == host {
  170. fallbackHost = fallbackIter()
  171. }
  172. return fallbackHost
  173. }
  174. }
  175. //ConnSelectionPolicy is an interface for selecting an
  176. //appropriate connection for executing a query
  177. type ConnSelectionPolicy interface {
  178. SetConns(conns []*Conn)
  179. Pick(*Query) *Conn
  180. }
  181. type roundRobinConnPolicy struct {
  182. conns []*Conn
  183. pos uint32
  184. mu sync.RWMutex
  185. }
  186. func NewRoundRobinConnPolicy() ConnSelectionPolicy {
  187. return &roundRobinConnPolicy{}
  188. }
  189. func (r *roundRobinConnPolicy) SetConns(conns []*Conn) {
  190. r.mu.Lock()
  191. r.conns = conns
  192. r.mu.Unlock()
  193. }
  194. func (r *roundRobinConnPolicy) Pick(qry *Query) *Conn {
  195. pos := atomic.AddUint32(&r.pos, 1)
  196. var conn *Conn
  197. r.mu.RLock()
  198. if len(r.conns) > 0 {
  199. conn = r.conns[pos%uint32(len(r.conns))]
  200. }
  201. r.mu.RUnlock()
  202. return conn
  203. }