policies.go 25 KB

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