policies.go 5.4 KB

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