policies.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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. "context"
  8. crand "crypto/rand"
  9. "encoding/binary"
  10. "errors"
  11. "fmt"
  12. "math"
  13. "math/rand"
  14. "net"
  15. "sync"
  16. "sync/atomic"
  17. "time"
  18. "github.com/hailocab/go-hostpool"
  19. )
  20. // cowHostList implements a copy on write host list, its equivalent type is []*HostInfo
  21. type cowHostList struct {
  22. list atomic.Value
  23. mu sync.Mutex
  24. }
  25. func (c *cowHostList) String() string {
  26. return fmt.Sprintf("%+v", c.get())
  27. }
  28. func (c *cowHostList) get() []*HostInfo {
  29. // TODO(zariel): should we replace this with []*HostInfo?
  30. l, ok := c.list.Load().(*[]*HostInfo)
  31. if !ok {
  32. return nil
  33. }
  34. return *l
  35. }
  36. func (c *cowHostList) set(list []*HostInfo) {
  37. c.mu.Lock()
  38. c.list.Store(&list)
  39. c.mu.Unlock()
  40. }
  41. // add will add a host if it not already in the list
  42. func (c *cowHostList) add(host *HostInfo) bool {
  43. c.mu.Lock()
  44. l := c.get()
  45. if n := len(l); n == 0 {
  46. l = []*HostInfo{host}
  47. } else {
  48. newL := make([]*HostInfo, n+1)
  49. for i := 0; i < n; i++ {
  50. if host.Equal(l[i]) {
  51. c.mu.Unlock()
  52. return false
  53. }
  54. newL[i] = l[i]
  55. }
  56. newL[n] = host
  57. l = newL
  58. }
  59. c.list.Store(&l)
  60. c.mu.Unlock()
  61. return true
  62. }
  63. func (c *cowHostList) update(host *HostInfo) {
  64. c.mu.Lock()
  65. l := c.get()
  66. if len(l) == 0 {
  67. c.mu.Unlock()
  68. return
  69. }
  70. found := false
  71. newL := make([]*HostInfo, len(l))
  72. for i := range l {
  73. if host.Equal(l[i]) {
  74. newL[i] = host
  75. found = true
  76. } else {
  77. newL[i] = l[i]
  78. }
  79. }
  80. if found {
  81. c.list.Store(&newL)
  82. }
  83. c.mu.Unlock()
  84. }
  85. func (c *cowHostList) remove(ip net.IP) bool {
  86. c.mu.Lock()
  87. l := c.get()
  88. size := len(l)
  89. if size == 0 {
  90. c.mu.Unlock()
  91. return false
  92. }
  93. found := false
  94. newL := make([]*HostInfo, 0, size)
  95. for i := 0; i < len(l); i++ {
  96. if !l[i].ConnectAddress().Equal(ip) {
  97. newL = append(newL, l[i])
  98. } else {
  99. found = true
  100. }
  101. }
  102. if !found {
  103. c.mu.Unlock()
  104. return false
  105. }
  106. newL = newL[: size-1 : size-1]
  107. c.list.Store(&newL)
  108. c.mu.Unlock()
  109. return true
  110. }
  111. // RetryableQuery is an interface that represents a query or batch statement that
  112. // exposes the correct functions for the retry policy logic to evaluate correctly.
  113. type RetryableQuery interface {
  114. Attempts() int
  115. SetConsistency(c Consistency)
  116. GetConsistency() Consistency
  117. Context() context.Context
  118. }
  119. type RetryType uint16
  120. const (
  121. Retry RetryType = 0x00 // retry on same connection
  122. RetryNextHost RetryType = 0x01 // retry on another connection
  123. Ignore RetryType = 0x02 // ignore error and return result
  124. Rethrow RetryType = 0x03 // raise error and stop retrying
  125. )
  126. // ErrUnknownRetryType is returned if the retry policy returns a retry type
  127. // unknown to the query executor.
  128. var ErrUnknownRetryType = errors.New("unknown retry type returned by retry policy")
  129. // RetryPolicy interface is used by gocql to determine if a query can be attempted
  130. // again after a retryable error has been received. The interface allows gocql
  131. // users to implement their own logic to determine if a query can be attempted
  132. // again.
  133. //
  134. // See SimpleRetryPolicy as an example of implementing and using a RetryPolicy
  135. // interface.
  136. type RetryPolicy interface {
  137. Attempt(RetryableQuery) bool
  138. GetRetryType(error) RetryType
  139. }
  140. // SimpleRetryPolicy has simple logic for attempting a query a fixed number of times.
  141. //
  142. // See below for examples of usage:
  143. //
  144. // //Assign to the cluster
  145. // cluster.RetryPolicy = &gocql.SimpleRetryPolicy{NumRetries: 3}
  146. //
  147. // //Assign to a query
  148. // query.RetryPolicy(&gocql.SimpleRetryPolicy{NumRetries: 1})
  149. //
  150. type SimpleRetryPolicy struct {
  151. NumRetries int //Number of times to retry a query
  152. }
  153. // Attempt tells gocql to attempt the query again based on query.Attempts being less
  154. // than the NumRetries defined in the policy.
  155. func (s *SimpleRetryPolicy) Attempt(q RetryableQuery) bool {
  156. return q.Attempts() <= s.NumRetries
  157. }
  158. func (s *SimpleRetryPolicy) GetRetryType(err error) RetryType {
  159. return RetryNextHost
  160. }
  161. // ExponentialBackoffRetryPolicy sleeps between attempts
  162. type ExponentialBackoffRetryPolicy struct {
  163. NumRetries int
  164. Min, Max time.Duration
  165. }
  166. func (e *ExponentialBackoffRetryPolicy) Attempt(q RetryableQuery) bool {
  167. if q.Attempts() > e.NumRetries {
  168. return false
  169. }
  170. time.Sleep(e.napTime(q.Attempts()))
  171. return true
  172. }
  173. // used to calculate exponentially growing time
  174. func getExponentialTime(min time.Duration, max time.Duration, attempts int) time.Duration {
  175. if min <= 0 {
  176. min = 100 * time.Millisecond
  177. }
  178. if max <= 0 {
  179. max = 10 * time.Second
  180. }
  181. minFloat := float64(min)
  182. napDuration := minFloat * math.Pow(2, float64(attempts-1))
  183. // add some jitter
  184. napDuration += rand.Float64()*minFloat - (minFloat / 2)
  185. if napDuration > float64(max) {
  186. return time.Duration(max)
  187. }
  188. return time.Duration(napDuration)
  189. }
  190. func (e *ExponentialBackoffRetryPolicy) GetRetryType(err error) RetryType {
  191. return RetryNextHost
  192. }
  193. // DowngradingConsistencyRetryPolicy: Next retry will be with the next consistency level
  194. // provided in the slice
  195. //
  196. // On a read timeout: the operation is retried with the next provided consistency
  197. // level.
  198. //
  199. // On a write timeout: if the operation is an :attr:`~.UNLOGGED_BATCH`
  200. // and at least one replica acknowledged the write, the operation is
  201. // retried with the next consistency level. Furthermore, for other
  202. // write types, if at least one replica acknowledged the write, the
  203. // timeout is ignored.
  204. //
  205. // On an unavailable exception: if at least one replica is alive, the
  206. // operation is retried with the next provided consistency level.
  207. type DowngradingConsistencyRetryPolicy struct {
  208. ConsistencyLevelsToTry []Consistency
  209. }
  210. func (d *DowngradingConsistencyRetryPolicy) Attempt(q RetryableQuery) bool {
  211. currentAttempt := q.Attempts()
  212. if currentAttempt > len(d.ConsistencyLevelsToTry) {
  213. return false
  214. } else if currentAttempt > 0 {
  215. q.SetConsistency(d.ConsistencyLevelsToTry[currentAttempt-1])
  216. if gocqlDebug {
  217. Logger.Printf("%T: set consistency to %q\n",
  218. d,
  219. d.ConsistencyLevelsToTry[currentAttempt-1])
  220. }
  221. }
  222. return true
  223. }
  224. func (d *DowngradingConsistencyRetryPolicy) GetRetryType(err error) RetryType {
  225. switch t := err.(type) {
  226. case *RequestErrUnavailable:
  227. if t.Alive > 0 {
  228. return Retry
  229. }
  230. return Rethrow
  231. case *RequestErrWriteTimeout:
  232. if t.WriteType == "SIMPLE" || t.WriteType == "BATCH" || t.WriteType == "COUNTER" {
  233. if t.Received > 0 {
  234. return Ignore
  235. }
  236. return Rethrow
  237. }
  238. if t.WriteType == "UNLOGGED_BATCH" {
  239. return Retry
  240. }
  241. return Rethrow
  242. case *RequestErrReadTimeout:
  243. return Retry
  244. default:
  245. return RetryNextHost
  246. }
  247. }
  248. func (e *ExponentialBackoffRetryPolicy) napTime(attempts int) time.Duration {
  249. return getExponentialTime(e.Min, e.Max, attempts)
  250. }
  251. type HostStateNotifier interface {
  252. AddHost(host *HostInfo)
  253. RemoveHost(host *HostInfo)
  254. HostUp(host *HostInfo)
  255. HostDown(host *HostInfo)
  256. }
  257. type KeyspaceUpdateEvent struct {
  258. Keyspace string
  259. Change string
  260. }
  261. // HostSelectionPolicy is an interface for selecting
  262. // the most appropriate host to execute a given query.
  263. type HostSelectionPolicy interface {
  264. HostStateNotifier
  265. SetPartitioner
  266. KeyspaceChanged(KeyspaceUpdateEvent)
  267. Init(*Session)
  268. IsLocal(host *HostInfo) bool
  269. //Pick returns an iteration function over selected hosts
  270. Pick(ExecutableQuery) NextHost
  271. }
  272. // SelectedHost is an interface returned when picking a host from a host
  273. // selection policy.
  274. type SelectedHost interface {
  275. Info() *HostInfo
  276. Mark(error)
  277. }
  278. type selectedHost HostInfo
  279. func (host *selectedHost) Info() *HostInfo {
  280. return (*HostInfo)(host)
  281. }
  282. func (host *selectedHost) Mark(err error) {}
  283. // NextHost is an iteration function over picked hosts
  284. type NextHost func() SelectedHost
  285. // RoundRobinHostPolicy is a round-robin load balancing policy, where each host
  286. // is tried sequentially for each query.
  287. func RoundRobinHostPolicy() HostSelectionPolicy {
  288. return &roundRobinHostPolicy{}
  289. }
  290. type roundRobinHostPolicy struct {
  291. hosts cowHostList
  292. }
  293. func (r *roundRobinHostPolicy) IsLocal(*HostInfo) bool { return true }
  294. func (r *roundRobinHostPolicy) KeyspaceChanged(KeyspaceUpdateEvent) {}
  295. func (r *roundRobinHostPolicy) SetPartitioner(partitioner string) {}
  296. func (r *roundRobinHostPolicy) Init(*Session) {}
  297. var (
  298. randPool = sync.Pool{
  299. New: func() interface{} {
  300. return rand.New(randSource())
  301. },
  302. }
  303. )
  304. func (r *roundRobinHostPolicy) Pick(qry ExecutableQuery) NextHost {
  305. src := r.hosts.get()
  306. hosts := make([]*HostInfo, len(src))
  307. copy(hosts, src)
  308. rand := randPool.Get().(*rand.Rand)
  309. defer randPool.Put(rand)
  310. rand.Shuffle(len(hosts), func(i, j int) {
  311. hosts[i], hosts[j] = hosts[j], hosts[i]
  312. })
  313. return roundRobbin(hosts)
  314. }
  315. func (r *roundRobinHostPolicy) AddHost(host *HostInfo) {
  316. r.hosts.add(host)
  317. }
  318. func (r *roundRobinHostPolicy) RemoveHost(host *HostInfo) {
  319. r.hosts.remove(host.ConnectAddress())
  320. }
  321. func (r *roundRobinHostPolicy) HostUp(host *HostInfo) {
  322. r.AddHost(host)
  323. }
  324. func (r *roundRobinHostPolicy) HostDown(host *HostInfo) {
  325. r.RemoveHost(host)
  326. }
  327. func ShuffleReplicas() func(*tokenAwareHostPolicy) {
  328. return func(t *tokenAwareHostPolicy) {
  329. t.shuffleReplicas = true
  330. }
  331. }
  332. // NonLocalReplicasFallback enables fallback to replicas that are not considered local.
  333. //
  334. // TokenAwareHostPolicy used with DCAwareHostPolicy fallback first selects replicas by partition key in local DC, then
  335. // falls back to other nodes in the local DC. Enabling NonLocalReplicasFallback causes TokenAwareHostPolicy
  336. // to first select replicas by partition key in local DC, then replicas by partition key in remote DCs and fall back
  337. // to other nodes in local DC.
  338. func NonLocalReplicasFallback() func(policy *tokenAwareHostPolicy) {
  339. return func(t *tokenAwareHostPolicy) {
  340. t.nonLocalReplicasFallback = true
  341. }
  342. }
  343. // TokenAwareHostPolicy is a token aware host selection policy, where hosts are
  344. // selected based on the partition key, so queries are sent to the host which
  345. // owns the partition. Fallback is used when routing information is not available.
  346. func TokenAwareHostPolicy(fallback HostSelectionPolicy, opts ...func(*tokenAwareHostPolicy)) HostSelectionPolicy {
  347. p := &tokenAwareHostPolicy{fallback: fallback}
  348. for _, opt := range opts {
  349. opt(p)
  350. }
  351. return p
  352. }
  353. // clusterMeta holds metadata about cluster topology.
  354. // It is used inside atomic.Value and shallow copies are used when replacing it,
  355. // so fields should not be modified in-place. Instead, to modify a field a copy of the field should be made
  356. // and the pointer in clusterMeta updated to point to the new value.
  357. type clusterMeta struct {
  358. // replicas is map[keyspace]map[token]hosts
  359. replicas map[string]tokenRingReplicas
  360. tokenRing *tokenRing
  361. }
  362. type tokenAwareHostPolicy struct {
  363. fallback HostSelectionPolicy
  364. getKeyspaceMetadata func(keyspace string) (*KeyspaceMetadata, error)
  365. getKeyspaceName func() string
  366. shuffleReplicas bool
  367. nonLocalReplicasFallback bool
  368. // mu protects writes to hosts, partitioner, metadata.
  369. // reads can be unlocked as long as they are not used for updating state later.
  370. mu sync.Mutex
  371. hosts cowHostList
  372. partitioner string
  373. metadata atomic.Value // *clusterMeta
  374. }
  375. func (t *tokenAwareHostPolicy) Init(s *Session) {
  376. t.getKeyspaceMetadata = s.KeyspaceMetadata
  377. t.getKeyspaceName = func() string { return s.cfg.Keyspace }
  378. }
  379. func (t *tokenAwareHostPolicy) IsLocal(host *HostInfo) bool {
  380. return t.fallback.IsLocal(host)
  381. }
  382. func (t *tokenAwareHostPolicy) KeyspaceChanged(update KeyspaceUpdateEvent) {
  383. t.mu.Lock()
  384. defer t.mu.Unlock()
  385. meta := t.getMetadataForUpdate()
  386. t.updateReplicas(meta, update.Keyspace)
  387. t.metadata.Store(meta)
  388. }
  389. // updateReplicas updates replicas in clusterMeta.
  390. // It must be called with t.mu mutex locked.
  391. // meta must not be nil and it's replicas field will be updated.
  392. func (t *tokenAwareHostPolicy) updateReplicas(meta *clusterMeta, keyspace string) {
  393. newReplicas := make(map[string]tokenRingReplicas, len(meta.replicas))
  394. ks, err := t.getKeyspaceMetadata(keyspace)
  395. if err == nil {
  396. strat := getStrategy(ks)
  397. if strat != nil {
  398. if meta != nil && meta.tokenRing != nil {
  399. newReplicas[keyspace] = strat.replicaMap(meta.tokenRing)
  400. }
  401. }
  402. }
  403. for ks, replicas := range meta.replicas {
  404. if ks != keyspace {
  405. newReplicas[ks] = replicas
  406. }
  407. }
  408. meta.replicas = newReplicas
  409. }
  410. func (t *tokenAwareHostPolicy) SetPartitioner(partitioner string) {
  411. t.mu.Lock()
  412. defer t.mu.Unlock()
  413. if t.partitioner != partitioner {
  414. t.fallback.SetPartitioner(partitioner)
  415. t.partitioner = partitioner
  416. meta := t.getMetadataForUpdate()
  417. meta.resetTokenRing(t.partitioner, t.hosts.get())
  418. t.updateReplicas(meta, t.getKeyspaceName())
  419. t.metadata.Store(meta)
  420. }
  421. }
  422. func (t *tokenAwareHostPolicy) AddHost(host *HostInfo) {
  423. t.mu.Lock()
  424. if t.hosts.add(host) {
  425. meta := t.getMetadataForUpdate()
  426. meta.resetTokenRing(t.partitioner, t.hosts.get())
  427. t.updateReplicas(meta, t.getKeyspaceName())
  428. t.metadata.Store(meta)
  429. }
  430. t.mu.Unlock()
  431. t.fallback.AddHost(host)
  432. }
  433. func (t *tokenAwareHostPolicy) AddHosts(hosts []*HostInfo) {
  434. t.mu.Lock()
  435. for _, host := range hosts {
  436. t.hosts.add(host)
  437. }
  438. meta := t.getMetadataForUpdate()
  439. meta.resetTokenRing(t.partitioner, t.hosts.get())
  440. t.updateReplicas(meta, t.getKeyspaceName())
  441. t.metadata.Store(meta)
  442. t.mu.Unlock()
  443. for _, host := range hosts {
  444. t.fallback.AddHost(host)
  445. }
  446. }
  447. func (t *tokenAwareHostPolicy) RemoveHost(host *HostInfo) {
  448. t.mu.Lock()
  449. if t.hosts.remove(host.ConnectAddress()) {
  450. meta := t.getMetadataForUpdate()
  451. meta.resetTokenRing(t.partitioner, t.hosts.get())
  452. t.updateReplicas(meta, t.getKeyspaceName())
  453. t.metadata.Store(meta)
  454. }
  455. t.mu.Unlock()
  456. t.fallback.RemoveHost(host)
  457. }
  458. func (t *tokenAwareHostPolicy) HostUp(host *HostInfo) {
  459. t.fallback.HostUp(host)
  460. }
  461. func (t *tokenAwareHostPolicy) HostDown(host *HostInfo) {
  462. t.fallback.HostDown(host)
  463. }
  464. // getMetadataReadOnly returns current cluster metadata.
  465. // Metadata uses copy on write, so the returned value should be only used for reading.
  466. // To obtain a copy that could be updated, use getMetadataForUpdate instead.
  467. func (t *tokenAwareHostPolicy) getMetadataReadOnly() *clusterMeta {
  468. meta, _ := t.metadata.Load().(*clusterMeta)
  469. return meta
  470. }
  471. // getMetadataForUpdate returns clusterMeta suitable for updating.
  472. // It is a SHALLOW copy of current metadata in case it was already set or new empty clusterMeta otherwise.
  473. // This function should be called with t.mu mutex locked and the mutex should not be released before
  474. // storing the new metadata.
  475. func (t *tokenAwareHostPolicy) getMetadataForUpdate() *clusterMeta {
  476. metaReadOnly := t.getMetadataReadOnly()
  477. meta := new(clusterMeta)
  478. if metaReadOnly != nil {
  479. *meta = *metaReadOnly
  480. }
  481. return meta
  482. }
  483. // resetTokenRing creates a new tokenRing.
  484. // It must be called with t.mu locked.
  485. func (m *clusterMeta) resetTokenRing(partitioner string, hosts []*HostInfo) {
  486. if partitioner == "" {
  487. // partitioner not yet set
  488. return
  489. }
  490. // create a new token ring
  491. tokenRing, err := newTokenRing(partitioner, hosts)
  492. if err != nil {
  493. Logger.Printf("Unable to update the token ring due to error: %s", err)
  494. return
  495. }
  496. // replace the token ring
  497. m.tokenRing = tokenRing
  498. }
  499. func (t *tokenAwareHostPolicy) Pick(qry ExecutableQuery) NextHost {
  500. if qry == nil {
  501. return t.fallback.Pick(qry)
  502. }
  503. routingKey, err := qry.GetRoutingKey()
  504. if err != nil {
  505. return t.fallback.Pick(qry)
  506. } else if routingKey == nil {
  507. return t.fallback.Pick(qry)
  508. }
  509. meta := t.getMetadataReadOnly()
  510. if meta == nil || meta.tokenRing == nil {
  511. return t.fallback.Pick(qry)
  512. }
  513. token := meta.tokenRing.partitioner.Hash(routingKey)
  514. ht := meta.replicas[qry.Keyspace()].replicasFor(token)
  515. var replicas []*HostInfo
  516. if ht == nil {
  517. host, _ := meta.tokenRing.GetHostForToken(token)
  518. replicas = []*HostInfo{host}
  519. } else {
  520. replicas = ht.hosts
  521. if t.shuffleReplicas {
  522. replicas = shuffleHosts(replicas)
  523. }
  524. }
  525. var (
  526. fallbackIter NextHost
  527. i, j int
  528. remote []*HostInfo
  529. )
  530. used := make(map[*HostInfo]bool, len(replicas))
  531. return func() SelectedHost {
  532. for i < len(replicas) {
  533. h := replicas[i]
  534. i++
  535. if !t.fallback.IsLocal(h) {
  536. remote = append(remote, h)
  537. continue
  538. }
  539. if h.IsUp() {
  540. used[h] = true
  541. return (*selectedHost)(h)
  542. }
  543. }
  544. if t.nonLocalReplicasFallback {
  545. for j < len(remote) {
  546. h := remote[j]
  547. j++
  548. if h.IsUp() {
  549. used[h] = true
  550. return (*selectedHost)(h)
  551. }
  552. }
  553. }
  554. if fallbackIter == nil {
  555. // fallback
  556. fallbackIter = t.fallback.Pick(qry)
  557. }
  558. // filter the token aware selected hosts from the fallback hosts
  559. for fallbackHost := fallbackIter(); fallbackHost != nil; fallbackHost = fallbackIter() {
  560. if !used[fallbackHost.Info()] {
  561. used[fallbackHost.Info()] = true
  562. return fallbackHost
  563. }
  564. }
  565. return nil
  566. }
  567. }
  568. // HostPoolHostPolicy is a host policy which uses the bitly/go-hostpool library
  569. // to distribute queries between hosts and prevent sending queries to
  570. // unresponsive hosts. When creating the host pool that is passed to the policy
  571. // use an empty slice of hosts as the hostpool will be populated later by gocql.
  572. // See below for examples of usage:
  573. //
  574. // // Create host selection policy using a simple host pool
  575. // cluster.PoolConfig.HostSelectionPolicy = HostPoolHostPolicy(hostpool.New(nil))
  576. //
  577. // // Create host selection policy using an epsilon greedy pool
  578. // cluster.PoolConfig.HostSelectionPolicy = HostPoolHostPolicy(
  579. // hostpool.NewEpsilonGreedy(nil, 0, &hostpool.LinearEpsilonValueCalculator{}),
  580. // )
  581. //
  582. func HostPoolHostPolicy(hp hostpool.HostPool) HostSelectionPolicy {
  583. return &hostPoolHostPolicy{hostMap: map[string]*HostInfo{}, hp: hp}
  584. }
  585. type hostPoolHostPolicy struct {
  586. hp hostpool.HostPool
  587. mu sync.RWMutex
  588. hostMap map[string]*HostInfo
  589. }
  590. func (r *hostPoolHostPolicy) Init(*Session) {}
  591. func (r *hostPoolHostPolicy) KeyspaceChanged(KeyspaceUpdateEvent) {}
  592. func (r *hostPoolHostPolicy) SetPartitioner(string) {}
  593. func (r *hostPoolHostPolicy) IsLocal(*HostInfo) bool { return true }
  594. func (r *hostPoolHostPolicy) SetHosts(hosts []*HostInfo) {
  595. peers := make([]string, len(hosts))
  596. hostMap := make(map[string]*HostInfo, len(hosts))
  597. for i, host := range hosts {
  598. ip := host.ConnectAddress().String()
  599. peers[i] = ip
  600. hostMap[ip] = host
  601. }
  602. r.mu.Lock()
  603. r.hp.SetHosts(peers)
  604. r.hostMap = hostMap
  605. r.mu.Unlock()
  606. }
  607. func (r *hostPoolHostPolicy) AddHost(host *HostInfo) {
  608. ip := host.ConnectAddress().String()
  609. r.mu.Lock()
  610. defer r.mu.Unlock()
  611. // If the host addr is present and isn't nil return
  612. if h, ok := r.hostMap[ip]; ok && h != nil {
  613. return
  614. }
  615. // otherwise, add the host to the map
  616. r.hostMap[ip] = host
  617. // and construct a new peer list to give to the HostPool
  618. hosts := make([]string, 0, len(r.hostMap))
  619. for addr := range r.hostMap {
  620. hosts = append(hosts, addr)
  621. }
  622. r.hp.SetHosts(hosts)
  623. }
  624. func (r *hostPoolHostPolicy) RemoveHost(host *HostInfo) {
  625. ip := host.ConnectAddress().String()
  626. r.mu.Lock()
  627. defer r.mu.Unlock()
  628. if _, ok := r.hostMap[ip]; !ok {
  629. return
  630. }
  631. delete(r.hostMap, ip)
  632. hosts := make([]string, 0, len(r.hostMap))
  633. for _, host := range r.hostMap {
  634. hosts = append(hosts, host.ConnectAddress().String())
  635. }
  636. r.hp.SetHosts(hosts)
  637. }
  638. func (r *hostPoolHostPolicy) HostUp(host *HostInfo) {
  639. r.AddHost(host)
  640. }
  641. func (r *hostPoolHostPolicy) HostDown(host *HostInfo) {
  642. r.RemoveHost(host)
  643. }
  644. func (r *hostPoolHostPolicy) Pick(qry ExecutableQuery) NextHost {
  645. return func() SelectedHost {
  646. r.mu.RLock()
  647. defer r.mu.RUnlock()
  648. if len(r.hostMap) == 0 {
  649. return nil
  650. }
  651. hostR := r.hp.Get()
  652. host, ok := r.hostMap[hostR.Host()]
  653. if !ok {
  654. return nil
  655. }
  656. return selectedHostPoolHost{
  657. policy: r,
  658. info: host,
  659. hostR: hostR,
  660. }
  661. }
  662. }
  663. // selectedHostPoolHost is a host returned by the hostPoolHostPolicy and
  664. // implements the SelectedHost interface
  665. type selectedHostPoolHost struct {
  666. policy *hostPoolHostPolicy
  667. info *HostInfo
  668. hostR hostpool.HostPoolResponse
  669. }
  670. func (host selectedHostPoolHost) Info() *HostInfo {
  671. return host.info
  672. }
  673. func (host selectedHostPoolHost) Mark(err error) {
  674. ip := host.info.ConnectAddress().String()
  675. host.policy.mu.RLock()
  676. defer host.policy.mu.RUnlock()
  677. if _, ok := host.policy.hostMap[ip]; !ok {
  678. // host was removed between pick and mark
  679. return
  680. }
  681. host.hostR.Mark(err)
  682. }
  683. type dcAwareRR struct {
  684. local string
  685. localHosts cowHostList
  686. remoteHosts cowHostList
  687. }
  688. // DCAwareRoundRobinPolicy is a host selection policies which will prioritize and
  689. // return hosts which are in the local datacentre before returning hosts in all
  690. // other datercentres
  691. func DCAwareRoundRobinPolicy(localDC string) HostSelectionPolicy {
  692. return &dcAwareRR{local: localDC}
  693. }
  694. func (d *dcAwareRR) Init(*Session) {}
  695. func (d *dcAwareRR) KeyspaceChanged(KeyspaceUpdateEvent) {}
  696. func (d *dcAwareRR) SetPartitioner(p string) {}
  697. func (d *dcAwareRR) IsLocal(host *HostInfo) bool {
  698. return host.DataCenter() == d.local
  699. }
  700. func (d *dcAwareRR) AddHost(host *HostInfo) {
  701. if d.IsLocal(host) {
  702. d.localHosts.add(host)
  703. } else {
  704. d.remoteHosts.add(host)
  705. }
  706. }
  707. func (d *dcAwareRR) RemoveHost(host *HostInfo) {
  708. if d.IsLocal(host) {
  709. d.localHosts.remove(host.ConnectAddress())
  710. } else {
  711. d.remoteHosts.remove(host.ConnectAddress())
  712. }
  713. }
  714. func (d *dcAwareRR) HostUp(host *HostInfo) { d.AddHost(host) }
  715. func (d *dcAwareRR) HostDown(host *HostInfo) { d.RemoveHost(host) }
  716. var randSeed int64
  717. func init() {
  718. p := make([]byte, 8)
  719. if _, err := crand.Read(p); err != nil {
  720. panic(err)
  721. }
  722. randSeed = int64(binary.BigEndian.Uint64(p))
  723. }
  724. func randSource() rand.Source {
  725. return rand.NewSource(atomic.AddInt64(&randSeed, 1))
  726. }
  727. func roundRobbin(hosts []*HostInfo) NextHost {
  728. var i int
  729. return func() SelectedHost {
  730. for i < len(hosts) {
  731. h := hosts[i]
  732. i++
  733. if h.IsUp() {
  734. return (*selectedHost)(h)
  735. }
  736. }
  737. return nil
  738. }
  739. }
  740. func (d *dcAwareRR) Pick(q ExecutableQuery) NextHost {
  741. local := d.localHosts.get()
  742. remote := d.remoteHosts.get()
  743. hosts := make([]*HostInfo, len(local)+len(remote))
  744. n := copy(hosts, local)
  745. copy(hosts[n:], remote)
  746. // TODO: use random chose-2 but that will require plumbing information
  747. // about connection/host load to here
  748. r := randPool.Get().(*rand.Rand)
  749. defer randPool.Put(r)
  750. for _, l := range [][]*HostInfo{hosts[:len(local)], hosts[len(local):]} {
  751. r.Shuffle(len(l), func(i, j int) {
  752. l[i], l[j] = l[j], l[i]
  753. })
  754. }
  755. return roundRobbin(hosts)
  756. }
  757. // ConvictionPolicy interface is used by gocql to determine if a host should be
  758. // marked as DOWN based on the error and host info
  759. type ConvictionPolicy interface {
  760. // Implementations should return `true` if the host should be convicted, `false` otherwise.
  761. AddFailure(error error, host *HostInfo) bool
  762. //Implementations should clear out any convictions or state regarding the host.
  763. Reset(host *HostInfo)
  764. }
  765. // SimpleConvictionPolicy implements a ConvictionPolicy which convicts all hosts
  766. // regardless of error
  767. type SimpleConvictionPolicy struct {
  768. }
  769. func (e *SimpleConvictionPolicy) AddFailure(error error, host *HostInfo) bool {
  770. return true
  771. }
  772. func (e *SimpleConvictionPolicy) Reset(host *HostInfo) {}
  773. // ReconnectionPolicy interface is used by gocql to determine if reconnection
  774. // can be attempted after connection error. The interface allows gocql users
  775. // to implement their own logic to determine how to attempt reconnection.
  776. //
  777. type ReconnectionPolicy interface {
  778. GetInterval(currentRetry int) time.Duration
  779. GetMaxRetries() int
  780. }
  781. // ConstantReconnectionPolicy has simple logic for returning a fixed reconnection interval.
  782. //
  783. // Examples of usage:
  784. //
  785. // cluster.ReconnectionPolicy = &gocql.ConstantReconnectionPolicy{MaxRetries: 10, Interval: 8 * time.Second}
  786. //
  787. type ConstantReconnectionPolicy struct {
  788. MaxRetries int
  789. Interval time.Duration
  790. }
  791. func (c *ConstantReconnectionPolicy) GetInterval(currentRetry int) time.Duration {
  792. return c.Interval
  793. }
  794. func (c *ConstantReconnectionPolicy) GetMaxRetries() int {
  795. return c.MaxRetries
  796. }
  797. // ExponentialReconnectionPolicy returns a growing reconnection interval.
  798. type ExponentialReconnectionPolicy struct {
  799. MaxRetries int
  800. InitialInterval time.Duration
  801. }
  802. func (e *ExponentialReconnectionPolicy) GetInterval(currentRetry int) time.Duration {
  803. return getExponentialTime(e.InitialInterval, math.MaxInt16*time.Second, currentRetry)
  804. }
  805. func (e *ExponentialReconnectionPolicy) GetMaxRetries() int {
  806. return e.MaxRetries
  807. }
  808. type SpeculativeExecutionPolicy interface {
  809. Attempts() int
  810. Delay() time.Duration
  811. }
  812. type NonSpeculativeExecution struct{}
  813. func (sp NonSpeculativeExecution) Attempts() int { return 0 } // No additional attempts
  814. func (sp NonSpeculativeExecution) Delay() time.Duration { return 1 } // The delay. Must be positive to be used in a ticker.
  815. type SimpleSpeculativeExecution struct {
  816. NumAttempts int
  817. TimeoutDelay time.Duration
  818. }
  819. func (sp *SimpleSpeculativeExecution) Attempts() int { return sp.NumAttempts }
  820. func (sp *SimpleSpeculativeExecution) Delay() time.Duration { return sp.TimeoutDelay }