policies.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. if len(r.hosts) == 0 {
  76. return nil
  77. }
  78. var host *HostInfo
  79. r.mu.RLock()
  80. // always increment pos to evenly distribute traffic in case of
  81. // failures
  82. pos := atomic.AddUint32(&r.pos, 1)
  83. if int(i) < len(r.hosts) {
  84. host = &r.hosts[(pos)%uint32(len(r.hosts))]
  85. i++
  86. }
  87. r.mu.RUnlock()
  88. return host
  89. }
  90. }
  91. //NewTokenAwareHostPolicy is a token aware host selection policy
  92. func NewTokenAwareHostPolicy(fallback HostSelectionPolicy) HostSelectionPolicy {
  93. return &tokenAwareHostPolicy{fallback: fallback, hosts: []HostInfo{}}
  94. }
  95. type tokenAwareHostPolicy struct {
  96. mu sync.RWMutex
  97. hosts []HostInfo
  98. partitioner string
  99. tokenRing *tokenRing
  100. fallback HostSelectionPolicy
  101. }
  102. func (t *tokenAwareHostPolicy) SetHosts(hosts []HostInfo) {
  103. t.mu.Lock()
  104. defer t.mu.Unlock()
  105. // always update the fallback
  106. t.fallback.SetHosts(hosts)
  107. t.hosts = hosts
  108. t.resetTokenRing()
  109. }
  110. func (t *tokenAwareHostPolicy) SetPartitioner(partitioner string) {
  111. t.mu.Lock()
  112. defer t.mu.Unlock()
  113. if t.partitioner != partitioner {
  114. t.fallback.SetPartitioner(partitioner)
  115. t.partitioner = partitioner
  116. t.resetTokenRing()
  117. }
  118. }
  119. func (t *tokenAwareHostPolicy) resetTokenRing() {
  120. if t.partitioner == "" {
  121. // partitioner not yet set
  122. return
  123. }
  124. // create a new token ring
  125. tokenRing, err := newTokenRing(t.partitioner, t.hosts)
  126. if err != nil {
  127. log.Printf("Unable to update the token ring due to error: %s", err)
  128. return
  129. }
  130. // replace the token ring
  131. t.tokenRing = tokenRing
  132. }
  133. func (t *tokenAwareHostPolicy) Pick(qry *Query) NextHost {
  134. if qry == nil {
  135. return t.fallback.Pick(qry)
  136. }
  137. routingKey, err := qry.GetRoutingKey()
  138. if err != nil {
  139. return t.fallback.Pick(qry)
  140. }
  141. if routingKey == nil {
  142. return t.fallback.Pick(qry)
  143. }
  144. var host *HostInfo
  145. t.mu.RLock()
  146. // TODO retrieve a list of hosts based on the replication strategy
  147. host = t.tokenRing.GetHostForPartitionKey(routingKey)
  148. t.mu.RUnlock()
  149. if host == nil {
  150. return t.fallback.Pick(qry)
  151. }
  152. // scope these variables for the same lifetime as the iterator function
  153. var (
  154. hostReturned bool
  155. fallbackIter NextHost
  156. )
  157. return func() *HostInfo {
  158. if !hostReturned {
  159. hostReturned = true
  160. return host
  161. }
  162. // fallback
  163. if fallbackIter == nil {
  164. fallbackIter = t.fallback.Pick(qry)
  165. }
  166. fallbackHost := fallbackIter()
  167. // filter the token aware selected hosts from the fallback hosts
  168. if fallbackHost == host {
  169. fallbackHost = fallbackIter()
  170. }
  171. return fallbackHost
  172. }
  173. }
  174. //ConnSelectionPolicy is an interface for selecting an
  175. //appropriate connection for executing a query
  176. type ConnSelectionPolicy interface {
  177. SetConns(conns []*Conn)
  178. Pick(*Query) *Conn
  179. }
  180. type roundRobinConnPolicy struct {
  181. conns []*Conn
  182. pos uint32
  183. mu sync.RWMutex
  184. }
  185. func NewRoundRobinConnPolicy() ConnSelectionPolicy {
  186. return &roundRobinConnPolicy{}
  187. }
  188. func (r *roundRobinConnPolicy) SetConns(conns []*Conn) {
  189. r.mu.Lock()
  190. r.conns = conns
  191. r.mu.Unlock()
  192. }
  193. func (r *roundRobinConnPolicy) Pick(qry *Query) *Conn {
  194. pos := atomic.AddUint32(&r.pos, 1)
  195. var conn *Conn
  196. r.mu.RLock()
  197. if len(r.conns) > 0 {
  198. conn = r.conns[pos%uint32(len(r.conns))]
  199. }
  200. r.mu.RUnlock()
  201. return conn
  202. }