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