host_source.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package gocql
  2. import (
  3. "log"
  4. "net"
  5. "time"
  6. )
  7. type HostInfo struct {
  8. Peer string
  9. DataCenter string
  10. Rack string
  11. HostId string
  12. Tokens []string
  13. }
  14. // Polls system.peers at a specific interval to find new hosts
  15. type ringDescriber struct {
  16. dcFilter string
  17. rackFilter string
  18. prevHosts []HostInfo
  19. prevPartitioner string
  20. session *Session
  21. closeChan chan bool
  22. }
  23. func (r *ringDescriber) GetHosts() (hosts []HostInfo, partitioner string, err error) {
  24. // we need conn to be the same because we need to query system.peers and system.local
  25. // on the same node to get the whole cluster
  26. conn := r.session.pool.Pick(nil)
  27. if conn == nil {
  28. return r.prevHosts, r.prevPartitioner, nil
  29. }
  30. query := r.session.Query("SELECT data_center, rack, host_id, tokens, partitioner FROM system.local")
  31. iter := conn.executeQuery(query)
  32. host := HostInfo{}
  33. iter.Scan(&host.DataCenter, &host.Rack, &host.HostId, &host.Tokens, &partitioner)
  34. if err = iter.Close(); err != nil {
  35. return nil, "", err
  36. }
  37. addr, _, err := net.SplitHostPort(conn.Address())
  38. if err != nil {
  39. // this should not happen, ever, as this is the address that was dialed by conn, here
  40. // a panic makes sense, please report a bug if it occurs.
  41. panic(err)
  42. }
  43. host.Peer = addr
  44. hosts = []HostInfo{host}
  45. query = r.session.Query("SELECT peer, data_center, rack, host_id, tokens FROM system.peers")
  46. iter = conn.executeQuery(query)
  47. host = HostInfo{}
  48. for iter.Scan(&host.Peer, &host.DataCenter, &host.Rack, &host.HostId, &host.Tokens) {
  49. if r.matchFilter(&host) {
  50. hosts = append(hosts, host)
  51. }
  52. host = HostInfo{}
  53. }
  54. if err = iter.Close(); err != nil {
  55. return nil, "", err
  56. }
  57. r.prevHosts = hosts
  58. r.prevPartitioner = partitioner
  59. return hosts, partitioner, nil
  60. }
  61. func (r *ringDescriber) matchFilter(host *HostInfo) bool {
  62. if r.dcFilter != "" && r.dcFilter != host.DataCenter {
  63. return false
  64. }
  65. if r.rackFilter != "" && r.rackFilter != host.Rack {
  66. return false
  67. }
  68. return true
  69. }
  70. func (h *ringDescriber) run(sleep time.Duration) {
  71. if sleep == 0 {
  72. sleep = 30 * time.Second
  73. }
  74. for {
  75. select {
  76. case <-time.After(sleep):
  77. // if we have 0 hosts this will return the previous list of hosts to
  78. // attempt to reconnect to the cluster otherwise we would never find
  79. // downed hosts again, could possibly have an optimisation to only
  80. // try to add new hosts if GetHosts didnt error and the hosts didnt change.
  81. hosts, partitioner, err := h.GetHosts()
  82. if err != nil {
  83. log.Println("RingDescriber: unable to get ring topology:", err)
  84. continue
  85. }
  86. h.session.pool.SetHosts(hosts)
  87. h.session.pool.SetPartitioner(partitioner)
  88. case <-h.closeChan:
  89. return
  90. }
  91. }
  92. }