policies.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. "fmt"
  8. "math"
  9. "math/rand"
  10. "net"
  11. "sync"
  12. "sync/atomic"
  13. "time"
  14. "github.com/hailocab/go-hostpool"
  15. )
  16. // cowHostList implements a copy on write host list, its equivalent type is []*HostInfo
  17. type cowHostList struct {
  18. list atomic.Value
  19. mu sync.Mutex
  20. }
  21. func (c *cowHostList) String() string {
  22. return fmt.Sprintf("%+v", c.get())
  23. }
  24. func (c *cowHostList) get() []*HostInfo {
  25. // TODO(zariel): should we replace this with []*HostInfo?
  26. l, ok := c.list.Load().(*[]*HostInfo)
  27. if !ok {
  28. return nil
  29. }
  30. return *l
  31. }
  32. func (c *cowHostList) set(list []*HostInfo) {
  33. c.mu.Lock()
  34. c.list.Store(&list)
  35. c.mu.Unlock()
  36. }
  37. // add will add a host if it not already in the list
  38. func (c *cowHostList) add(host *HostInfo) bool {
  39. c.mu.Lock()
  40. l := c.get()
  41. if n := len(l); n == 0 {
  42. l = []*HostInfo{host}
  43. } else {
  44. newL := make([]*HostInfo, n+1)
  45. for i := 0; i < n; i++ {
  46. if host.Equal(l[i]) {
  47. c.mu.Unlock()
  48. return false
  49. }
  50. newL[i] = l[i]
  51. }
  52. newL[n] = host
  53. l = newL
  54. }
  55. c.list.Store(&l)
  56. c.mu.Unlock()
  57. return true
  58. }
  59. func (c *cowHostList) update(host *HostInfo) {
  60. c.mu.Lock()
  61. l := c.get()
  62. if len(l) == 0 {
  63. c.mu.Unlock()
  64. return
  65. }
  66. found := false
  67. newL := make([]*HostInfo, len(l))
  68. for i := range l {
  69. if host.Equal(l[i]) {
  70. newL[i] = host
  71. found = true
  72. } else {
  73. newL[i] = l[i]
  74. }
  75. }
  76. if found {
  77. c.list.Store(&newL)
  78. }
  79. c.mu.Unlock()
  80. }
  81. func (c *cowHostList) remove(ip net.IP) bool {
  82. c.mu.Lock()
  83. l := c.get()
  84. size := len(l)
  85. if size == 0 {
  86. c.mu.Unlock()
  87. return false
  88. }
  89. found := false
  90. newL := make([]*HostInfo, 0, size)
  91. for i := 0; i < len(l); i++ {
  92. if !l[i].ConnectAddress().Equal(ip) {
  93. newL = append(newL, l[i])
  94. } else {
  95. found = true
  96. }
  97. }
  98. if !found {
  99. c.mu.Unlock()
  100. return false
  101. }
  102. newL = newL[:size-1 : size-1]
  103. c.list.Store(&newL)
  104. c.mu.Unlock()
  105. return true
  106. }
  107. // RetryableQuery is an interface that represents a query or batch statement that
  108. // exposes the correct functions for the retry policy logic to evaluate correctly.
  109. type RetryableQuery interface {
  110. Attempts() int
  111. GetConsistency() Consistency
  112. }
  113. // RetryPolicy interface is used by gocql to determine if a query can be attempted
  114. // again after a retryable error has been received. The interface allows gocql
  115. // users to implement their own logic to determine if a query can be attempted
  116. // again.
  117. //
  118. // See SimpleRetryPolicy as an example of implementing and using a RetryPolicy
  119. // interface.
  120. type RetryPolicy interface {
  121. Attempt(RetryableQuery) bool
  122. }
  123. // SimpleRetryPolicy has simple logic for attempting a query a fixed number of times.
  124. //
  125. // See below for examples of usage:
  126. //
  127. // //Assign to the cluster
  128. // cluster.RetryPolicy = &gocql.SimpleRetryPolicy{NumRetries: 3}
  129. //
  130. // //Assign to a query
  131. // query.RetryPolicy(&gocql.SimpleRetryPolicy{NumRetries: 1})
  132. //
  133. type SimpleRetryPolicy struct {
  134. NumRetries int //Number of times to retry a query
  135. }
  136. // Attempt tells gocql to attempt the query again based on query.Attempts being less
  137. // than the NumRetries defined in the policy.
  138. func (s *SimpleRetryPolicy) Attempt(q RetryableQuery) bool {
  139. return q.Attempts() <= s.NumRetries
  140. }
  141. // ExponentialBackoffRetryPolicy sleeps between attempts
  142. type ExponentialBackoffRetryPolicy struct {
  143. NumRetries int
  144. Min, Max time.Duration
  145. }
  146. func (e *ExponentialBackoffRetryPolicy) Attempt(q RetryableQuery) bool {
  147. if q.Attempts() > e.NumRetries {
  148. return false
  149. }
  150. time.Sleep(e.napTime(q.Attempts()))
  151. return true
  152. }
  153. func (e *ExponentialBackoffRetryPolicy) napTime(attempts int) time.Duration {
  154. if e.Min <= 0 {
  155. e.Min = 100 * time.Millisecond
  156. }
  157. if e.Max <= 0 {
  158. e.Max = 10 * time.Second
  159. }
  160. minFloat := float64(e.Min)
  161. napDuration := minFloat * math.Pow(2, float64(attempts-1))
  162. // add some jitter
  163. napDuration += rand.Float64()*minFloat - (minFloat / 2)
  164. if napDuration > float64(e.Max) {
  165. return time.Duration(e.Max)
  166. }
  167. return time.Duration(napDuration)
  168. }
  169. type HostStateNotifier interface {
  170. AddHost(host *HostInfo)
  171. RemoveHost(host *HostInfo)
  172. HostUp(host *HostInfo)
  173. HostDown(host *HostInfo)
  174. }
  175. // HostSelectionPolicy is an interface for selecting
  176. // the most appropriate host to execute a given query.
  177. type HostSelectionPolicy interface {
  178. HostStateNotifier
  179. SetPartitioner
  180. //Pick returns an iteration function over selected hosts
  181. Pick(ExecutableQuery) NextHost
  182. }
  183. // SelectedHost is an interface returned when picking a host from a host
  184. // selection policy.
  185. type SelectedHost interface {
  186. Info() *HostInfo
  187. Mark(error)
  188. }
  189. type selectedHost HostInfo
  190. func (host *selectedHost) Info() *HostInfo {
  191. return (*HostInfo)(host)
  192. }
  193. func (host *selectedHost) Mark(err error) {}
  194. // NextHost is an iteration function over picked hosts
  195. type NextHost func() SelectedHost
  196. // RoundRobinHostPolicy is a round-robin load balancing policy, where each host
  197. // is tried sequentially for each query.
  198. func RoundRobinHostPolicy() HostSelectionPolicy {
  199. return &roundRobinHostPolicy{}
  200. }
  201. type roundRobinHostPolicy struct {
  202. hosts cowHostList
  203. pos uint32
  204. mu sync.RWMutex
  205. }
  206. func (r *roundRobinHostPolicy) SetPartitioner(partitioner string) {
  207. // noop
  208. }
  209. func (r *roundRobinHostPolicy) Pick(qry ExecutableQuery) NextHost {
  210. // i is used to limit the number of attempts to find a host
  211. // to the number of hosts known to this policy
  212. var i int
  213. return func() SelectedHost {
  214. hosts := r.hosts.get()
  215. if len(hosts) == 0 {
  216. return nil
  217. }
  218. // always increment pos to evenly distribute traffic in case of
  219. // failures
  220. pos := atomic.AddUint32(&r.pos, 1) - 1
  221. if i >= len(hosts) {
  222. return nil
  223. }
  224. host := hosts[(pos)%uint32(len(hosts))]
  225. i++
  226. return (*selectedHost)(host)
  227. }
  228. }
  229. func (r *roundRobinHostPolicy) AddHost(host *HostInfo) {
  230. r.hosts.add(host)
  231. }
  232. func (r *roundRobinHostPolicy) RemoveHost(host *HostInfo) {
  233. r.hosts.remove(host.ConnectAddress())
  234. }
  235. func (r *roundRobinHostPolicy) HostUp(host *HostInfo) {
  236. r.AddHost(host)
  237. }
  238. func (r *roundRobinHostPolicy) HostDown(host *HostInfo) {
  239. r.RemoveHost(host)
  240. }
  241. // TokenAwareHostPolicy is a token aware host selection policy, where hosts are
  242. // selected based on the partition key, so queries are sent to the host which
  243. // owns the partition. Fallback is used when routing information is not available.
  244. func TokenAwareHostPolicy(fallback HostSelectionPolicy) HostSelectionPolicy {
  245. return &tokenAwareHostPolicy{fallback: fallback}
  246. }
  247. type tokenAwareHostPolicy struct {
  248. hosts cowHostList
  249. mu sync.RWMutex
  250. partitioner string
  251. tokenRing *tokenRing
  252. fallback HostSelectionPolicy
  253. }
  254. func (t *tokenAwareHostPolicy) SetPartitioner(partitioner string) {
  255. if t.partitioner != partitioner {
  256. t.fallback.SetPartitioner(partitioner)
  257. t.partitioner = partitioner
  258. t.resetTokenRing()
  259. }
  260. }
  261. func (t *tokenAwareHostPolicy) AddHost(host *HostInfo) {
  262. t.hosts.add(host)
  263. t.fallback.AddHost(host)
  264. t.resetTokenRing()
  265. }
  266. func (t *tokenAwareHostPolicy) RemoveHost(host *HostInfo) {
  267. t.hosts.remove(host.ConnectAddress())
  268. t.fallback.RemoveHost(host)
  269. t.resetTokenRing()
  270. }
  271. func (t *tokenAwareHostPolicy) HostUp(host *HostInfo) {
  272. t.AddHost(host)
  273. }
  274. func (t *tokenAwareHostPolicy) HostDown(host *HostInfo) {
  275. t.RemoveHost(host)
  276. }
  277. func (t *tokenAwareHostPolicy) resetTokenRing() {
  278. t.mu.Lock()
  279. defer t.mu.Unlock()
  280. if t.partitioner == "" {
  281. // partitioner not yet set
  282. return
  283. }
  284. // create a new token ring
  285. hosts := t.hosts.get()
  286. tokenRing, err := newTokenRing(t.partitioner, hosts)
  287. if err != nil {
  288. Logger.Printf("Unable to update the token ring due to error: %s", err)
  289. return
  290. }
  291. // replace the token ring
  292. t.tokenRing = tokenRing
  293. }
  294. func (t *tokenAwareHostPolicy) Pick(qry ExecutableQuery) NextHost {
  295. if qry == nil {
  296. return t.fallback.Pick(qry)
  297. }
  298. routingKey, err := qry.GetRoutingKey()
  299. if err != nil {
  300. return t.fallback.Pick(qry)
  301. }
  302. if routingKey == nil {
  303. return t.fallback.Pick(qry)
  304. }
  305. t.mu.RLock()
  306. // TODO retrieve a list of hosts based on the replication strategy
  307. host := t.tokenRing.GetHostForPartitionKey(routingKey)
  308. t.mu.RUnlock()
  309. if host == nil {
  310. return t.fallback.Pick(qry)
  311. }
  312. // scope these variables for the same lifetime as the iterator function
  313. var (
  314. hostReturned bool
  315. fallbackIter NextHost
  316. )
  317. return func() SelectedHost {
  318. if !hostReturned {
  319. hostReturned = true
  320. return (*selectedHost)(host)
  321. }
  322. // fallback
  323. if fallbackIter == nil {
  324. fallbackIter = t.fallback.Pick(qry)
  325. }
  326. fallbackHost := fallbackIter()
  327. // filter the token aware selected hosts from the fallback hosts
  328. if fallbackHost != nil && fallbackHost.Info() == host {
  329. fallbackHost = fallbackIter()
  330. }
  331. return fallbackHost
  332. }
  333. }
  334. // HostPoolHostPolicy is a host policy which uses the bitly/go-hostpool library
  335. // to distribute queries between hosts and prevent sending queries to
  336. // unresponsive hosts. When creating the host pool that is passed to the policy
  337. // use an empty slice of hosts as the hostpool will be populated later by gocql.
  338. // See below for examples of usage:
  339. //
  340. // // Create host selection policy using a simple host pool
  341. // cluster.PoolConfig.HostSelectionPolicy = HostPoolHostPolicy(hostpool.New(nil))
  342. //
  343. // // Create host selection policy using an epsilon greedy pool
  344. // cluster.PoolConfig.HostSelectionPolicy = HostPoolHostPolicy(
  345. // hostpool.NewEpsilonGreedy(nil, 0, &hostpool.LinearEpsilonValueCalculator{}),
  346. // )
  347. //
  348. func HostPoolHostPolicy(hp hostpool.HostPool) HostSelectionPolicy {
  349. return &hostPoolHostPolicy{hostMap: map[string]*HostInfo{}, hp: hp}
  350. }
  351. type hostPoolHostPolicy struct {
  352. hp hostpool.HostPool
  353. mu sync.RWMutex
  354. hostMap map[string]*HostInfo
  355. }
  356. func (r *hostPoolHostPolicy) SetHosts(hosts []*HostInfo) {
  357. peers := make([]string, len(hosts))
  358. hostMap := make(map[string]*HostInfo, len(hosts))
  359. for i, host := range hosts {
  360. ip := host.ConnectAddress().String()
  361. peers[i] = ip
  362. hostMap[ip] = host
  363. }
  364. r.mu.Lock()
  365. r.hp.SetHosts(peers)
  366. r.hostMap = hostMap
  367. r.mu.Unlock()
  368. }
  369. func (r *hostPoolHostPolicy) AddHost(host *HostInfo) {
  370. ip := host.ConnectAddress().String()
  371. r.mu.Lock()
  372. defer r.mu.Unlock()
  373. // If the host addr is present and isn't nil return
  374. if h, ok := r.hostMap[ip]; ok && h != nil {
  375. return
  376. }
  377. // otherwise, add the host to the map
  378. r.hostMap[ip] = host
  379. // and construct a new peer list to give to the HostPool
  380. hosts := make([]string, 0, len(r.hostMap))
  381. for addr := range r.hostMap {
  382. hosts = append(hosts, addr)
  383. }
  384. r.hp.SetHosts(hosts)
  385. }
  386. func (r *hostPoolHostPolicy) RemoveHost(host *HostInfo) {
  387. ip := host.ConnectAddress().String()
  388. r.mu.Lock()
  389. defer r.mu.Unlock()
  390. if _, ok := r.hostMap[ip]; !ok {
  391. return
  392. }
  393. delete(r.hostMap, ip)
  394. hosts := make([]string, 0, len(r.hostMap))
  395. for _, host := range r.hostMap {
  396. hosts = append(hosts, host.ConnectAddress().String())
  397. }
  398. r.hp.SetHosts(hosts)
  399. }
  400. func (r *hostPoolHostPolicy) HostUp(host *HostInfo) {
  401. r.AddHost(host)
  402. }
  403. func (r *hostPoolHostPolicy) HostDown(host *HostInfo) {
  404. r.RemoveHost(host)
  405. }
  406. func (r *hostPoolHostPolicy) SetPartitioner(partitioner string) {
  407. // noop
  408. }
  409. func (r *hostPoolHostPolicy) Pick(qry ExecutableQuery) NextHost {
  410. return func() SelectedHost {
  411. r.mu.RLock()
  412. defer r.mu.RUnlock()
  413. if len(r.hostMap) == 0 {
  414. return nil
  415. }
  416. hostR := r.hp.Get()
  417. host, ok := r.hostMap[hostR.Host()]
  418. if !ok {
  419. return nil
  420. }
  421. return selectedHostPoolHost{
  422. policy: r,
  423. info: host,
  424. hostR: hostR,
  425. }
  426. }
  427. }
  428. // selectedHostPoolHost is a host returned by the hostPoolHostPolicy and
  429. // implements the SelectedHost interface
  430. type selectedHostPoolHost struct {
  431. policy *hostPoolHostPolicy
  432. info *HostInfo
  433. hostR hostpool.HostPoolResponse
  434. }
  435. func (host selectedHostPoolHost) Info() *HostInfo {
  436. return host.info
  437. }
  438. func (host selectedHostPoolHost) Mark(err error) {
  439. ip := host.info.ConnectAddress().String()
  440. host.policy.mu.RLock()
  441. defer host.policy.mu.RUnlock()
  442. if _, ok := host.policy.hostMap[ip]; !ok {
  443. // host was removed between pick and mark
  444. return
  445. }
  446. host.hostR.Mark(err)
  447. }
  448. type dcAwareRR struct {
  449. local string
  450. mu sync.RWMutex
  451. localHosts map[string]*HostInfo
  452. remoteHosts map[string]*HostInfo
  453. }
  454. // DCAwareRoundRobinPolicy is a host selection policies which will priorities and
  455. // return hosts which are in the local datacentre before returning hosts in all
  456. // other datercentres
  457. func DCAwareRoundRobinPolicy(localDC string) HostSelectionPolicy {
  458. return &dcAwareRR{
  459. local: localDC,
  460. localHosts: make(map[string]*HostInfo),
  461. remoteHosts: make(map[string]*HostInfo),
  462. }
  463. }
  464. func (d *dcAwareRR) AddHost(host *HostInfo) {
  465. d.mu.Lock()
  466. if host.DataCenter() == d.local {
  467. d.localHosts[host.HostID()] = host
  468. } else {
  469. d.remoteHosts[host.HostID()] = host
  470. }
  471. d.mu.Unlock()
  472. }
  473. func (d *dcAwareRR) RemoveHost(host *HostInfo) {
  474. d.mu.Lock()
  475. delete(d.localHosts, host.HostID())
  476. delete(d.remoteHosts, host.HostID())
  477. d.mu.Unlock()
  478. }
  479. func (d *dcAwareRR) HostUp(host *HostInfo) {
  480. d.AddHost(host)
  481. }
  482. func (d *dcAwareRR) HostDown(host *HostInfo) {
  483. d.RemoveHost(host)
  484. }
  485. func (d *dcAwareRR) SetPartitioner(p string) {}
  486. func (d *dcAwareRR) Pick(q ExecutableQuery) NextHost {
  487. d.mu.RLock()
  488. // TODO: this is O(len(hosts)) and requires calculating a full query plan for
  489. // every query. On the other hand it is stupidly simply and provides random host
  490. // order prefering local dcs over remote ones.
  491. hosts := make([]*HostInfo, 0, len(d.localHosts)+len(d.remoteHosts))
  492. for _, host := range d.localHosts {
  493. hosts = append(hosts, host)
  494. }
  495. for _, host := range d.remoteHosts {
  496. hosts = append(hosts, host)
  497. }
  498. d.mu.RUnlock()
  499. return func() SelectedHost {
  500. if len(hosts) == 0 {
  501. return nil
  502. }
  503. host := hosts[0]
  504. hosts = hosts[1:]
  505. return (*selectedHost)(host)
  506. }
  507. }