control.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package gocql
  2. import (
  3. "errors"
  4. "fmt"
  5. "log"
  6. "net"
  7. "sync/atomic"
  8. "time"
  9. )
  10. // Ensure that the atomic variable is aligned to a 64bit boundary
  11. // so that atomic operations can be applied on 32bit architectures.
  12. type controlConn struct {
  13. connecting uint64
  14. session *Session
  15. conn atomic.Value
  16. retry RetryPolicy
  17. quit chan struct{}
  18. }
  19. func createControlConn(session *Session) *controlConn {
  20. control := &controlConn{
  21. session: session,
  22. quit: make(chan struct{}),
  23. retry: &SimpleRetryPolicy{NumRetries: 3},
  24. }
  25. control.conn.Store((*Conn)(nil))
  26. go control.heartBeat()
  27. return control
  28. }
  29. func (c *controlConn) heartBeat() {
  30. for {
  31. select {
  32. case <-c.quit:
  33. return
  34. case <-time.After(5 * time.Second):
  35. }
  36. resp, err := c.writeFrame(&writeOptionsFrame{})
  37. if err != nil {
  38. goto reconn
  39. }
  40. switch resp.(type) {
  41. case *supportedFrame:
  42. continue
  43. case error:
  44. goto reconn
  45. default:
  46. panic(fmt.Sprintf("gocql: unknown frame in response to options: %T", resp))
  47. }
  48. reconn:
  49. c.reconnect(true)
  50. // time.Sleep(5 * time.Second)
  51. continue
  52. }
  53. }
  54. func (c *controlConn) reconnect(refreshring bool) {
  55. if !atomic.CompareAndSwapUint64(&c.connecting, 0, 1) {
  56. return
  57. }
  58. success := false
  59. defer func() {
  60. // debounce reconnect a little
  61. if success {
  62. go func() {
  63. time.Sleep(500 * time.Millisecond)
  64. atomic.StoreUint64(&c.connecting, 0)
  65. }()
  66. } else {
  67. atomic.StoreUint64(&c.connecting, 0)
  68. }
  69. }()
  70. oldConn := c.conn.Load().(*Conn)
  71. // TODO: should have our own roundrobbin for hosts so that we can try each
  72. // in succession and guantee that we get a different host each time.
  73. host, conn := c.session.pool.Pick(nil)
  74. if conn == nil {
  75. return
  76. }
  77. newConn, err := Connect(conn.addr, conn.cfg, c, c.session)
  78. if err != nil {
  79. host.Mark(err)
  80. // TODO: add log handler for things like this
  81. return
  82. }
  83. frame, err := c.writeFrame(&writeRegisterFrame{
  84. events: []string{"TOPOLOGY_CHANGE", "STATUS_CHANGE", "STATUS_CHANGE"},
  85. })
  86. if err != nil {
  87. host.Mark(err)
  88. return
  89. } else if _, ok := frame.(*readyFrame); !ok {
  90. log.Printf("gocql: unexpected frame in response to register: got %T: %v\n", frame, frame)
  91. return
  92. }
  93. host.Mark(nil)
  94. c.conn.Store(newConn)
  95. success = true
  96. if oldConn != nil {
  97. oldConn.Close()
  98. }
  99. if refreshring && c.session.cfg.DiscoverHosts {
  100. c.session.hostSource.refreshRing()
  101. }
  102. }
  103. func (c *controlConn) HandleError(conn *Conn, err error, closed bool) {
  104. if !closed {
  105. return
  106. }
  107. oldConn := c.conn.Load().(*Conn)
  108. if oldConn != conn {
  109. return
  110. }
  111. c.reconnect(true)
  112. }
  113. func (c *controlConn) writeFrame(w frameWriter) (frame, error) {
  114. conn := c.conn.Load().(*Conn)
  115. if conn == nil {
  116. return nil, errNoControl
  117. }
  118. framer, err := conn.exec(w, nil)
  119. if err != nil {
  120. return nil, err
  121. }
  122. return framer.parseFrame()
  123. }
  124. func (c *controlConn) withConn(fn func(*Conn) *Iter) *Iter {
  125. const maxConnectAttempts = 5
  126. connectAttempts := 0
  127. for i := 0; i < maxConnectAttempts; i++ {
  128. conn := c.conn.Load().(*Conn)
  129. if conn == nil {
  130. if connectAttempts > maxConnectAttempts {
  131. break
  132. }
  133. connectAttempts++
  134. c.reconnect(false)
  135. continue
  136. }
  137. return fn(conn)
  138. }
  139. return &Iter{err: errNoControl}
  140. }
  141. // query will return nil if the connection is closed or nil
  142. func (c *controlConn) query(statement string, values ...interface{}) (iter *Iter) {
  143. q := c.session.Query(statement, values...).Consistency(One)
  144. for {
  145. iter = c.withConn(func(conn *Conn) *Iter {
  146. return conn.executeQuery(q)
  147. })
  148. q.attempts++
  149. if iter.err == nil || !c.retry.Attempt(q) {
  150. break
  151. }
  152. }
  153. return
  154. }
  155. func (c *controlConn) fetchHostInfo(addr net.IP, port int) (*HostInfo, error) {
  156. // TODO(zariel): we should probably move this into host_source or atleast
  157. // share code with it.
  158. isLocal := c.addr() == addr.String()
  159. var fn func(*HostInfo) error
  160. if isLocal {
  161. fn = func(host *HostInfo) error {
  162. // TODO(zariel): should we fetch rpc_address from here?
  163. iter := c.query("SELECT data_center, rack, host_id, tokens FROM system.local WHERE key='local'")
  164. iter.Scan(&host.DataCenter, &host.Rack, &host.HostId, &host.Tokens)
  165. return iter.Close()
  166. }
  167. } else {
  168. fn = func(host *HostInfo) error {
  169. // TODO(zariel): should we fetch rpc_address from here?
  170. iter := c.query("SELECT data_center, rack, host_id, tokens FROM system.peers WHERE peer=?", addr)
  171. iter.Scan(&host.DataCenter, &host.Rack, &host.HostId, &host.Tokens)
  172. return iter.Close()
  173. }
  174. }
  175. host := &HostInfo{}
  176. if err := fn(host); err != nil {
  177. return nil, err
  178. }
  179. host.Peer = addr.String()
  180. return host, nil
  181. }
  182. func (c *controlConn) awaitSchemaAgreement() error {
  183. return c.withConn(func(conn *Conn) *Iter {
  184. return &Iter{err: conn.awaitSchemaAgreement()}
  185. }).err
  186. }
  187. func (c *controlConn) addr() string {
  188. conn := c.conn.Load().(*Conn)
  189. if conn == nil {
  190. return ""
  191. }
  192. return conn.addr
  193. }
  194. func (c *controlConn) close() {
  195. // TODO: handle more gracefully
  196. close(c.quit)
  197. }
  198. var errNoControl = errors.New("gocql: no control connection available")