host_source.go 2.4 KB

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