events.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. package gocql
  2. import (
  3. "log"
  4. "net"
  5. "sync"
  6. "time"
  7. )
  8. type eventDeouncer struct {
  9. name string
  10. timer *time.Timer
  11. mu sync.Mutex
  12. events []frame
  13. callback func([]frame)
  14. quit chan struct{}
  15. }
  16. func newEventDeouncer(name string, eventHandler func([]frame)) *eventDeouncer {
  17. e := &eventDeouncer{
  18. name: name,
  19. quit: make(chan struct{}),
  20. timer: time.NewTimer(eventDebounceTime),
  21. callback: eventHandler,
  22. }
  23. e.timer.Stop()
  24. go e.flusher()
  25. return e
  26. }
  27. func (e *eventDeouncer) stop() {
  28. e.quit <- struct{}{} // sync with flusher
  29. close(e.quit)
  30. }
  31. func (e *eventDeouncer) flusher() {
  32. for {
  33. select {
  34. case <-e.timer.C:
  35. e.mu.Lock()
  36. e.flush()
  37. e.mu.Unlock()
  38. case <-e.quit:
  39. return
  40. }
  41. }
  42. }
  43. const (
  44. eventBufferSize = 1000
  45. eventDebounceTime = 1 * time.Second
  46. )
  47. // flush must be called with mu locked
  48. func (e *eventDeouncer) flush() {
  49. if len(e.events) == 0 {
  50. return
  51. }
  52. // if the flush interval is faster than the callback then we will end up calling
  53. // the callback multiple times, probably a bad idea. In this case we could drop
  54. // frames?
  55. go e.callback(e.events)
  56. e.events = make([]frame, 0, eventBufferSize)
  57. }
  58. func (e *eventDeouncer) debounce(frame frame) {
  59. e.mu.Lock()
  60. e.timer.Reset(eventDebounceTime)
  61. // TODO: probably need a warning to track if this threshold is too low
  62. if len(e.events) < eventBufferSize {
  63. e.events = append(e.events, frame)
  64. } else {
  65. log.Printf("%s: buffer full, dropping event frame: %s", e.name, frame)
  66. }
  67. e.mu.Unlock()
  68. }
  69. func (s *Session) handleEvent(framer *framer) {
  70. // TODO(zariel): need to debounce events frames, and possible also events
  71. defer framerPool.Put(framer)
  72. frame, err := framer.parseFrame()
  73. if err != nil {
  74. // TODO: logger
  75. log.Printf("gocql: unable to parse event frame: %v\n", err)
  76. return
  77. }
  78. if gocqlDebug {
  79. log.Printf("gocql: handling frame: %v\n", frame)
  80. }
  81. // TODO: handle medatadata events
  82. switch f := frame.(type) {
  83. case *schemaChangeKeyspace, *schemaChangeFunction, *schemaChangeTable:
  84. s.schemaEvents.debounce(frame)
  85. case *topologyChangeEventFrame, *statusChangeEventFrame:
  86. s.nodeEvents.debounce(frame)
  87. default:
  88. log.Printf("gocql: invalid event frame (%T): %v\n", f, f)
  89. }
  90. }
  91. func (s *Session) handleSchemaEvent(frames []frame) {
  92. if s.schemaDescriber == nil {
  93. return
  94. }
  95. for _, frame := range frames {
  96. switch f := frame.(type) {
  97. case *schemaChangeKeyspace:
  98. s.schemaDescriber.clearSchema(f.keyspace)
  99. case *schemaChangeTable:
  100. s.schemaDescriber.clearSchema(f.keyspace)
  101. }
  102. }
  103. }
  104. func (s *Session) handleNodeEvent(frames []frame) {
  105. type nodeEvent struct {
  106. change string
  107. host net.IP
  108. port int
  109. }
  110. events := make(map[string]*nodeEvent)
  111. for _, frame := range frames {
  112. // TODO: can we be sure the order of events in the buffer is correct?
  113. switch f := frame.(type) {
  114. case *topologyChangeEventFrame:
  115. event, ok := events[f.host.String()]
  116. if !ok {
  117. event = &nodeEvent{change: f.change, host: f.host, port: f.port}
  118. events[f.host.String()] = event
  119. }
  120. event.change = f.change
  121. case *statusChangeEventFrame:
  122. event, ok := events[f.host.String()]
  123. if !ok {
  124. event = &nodeEvent{change: f.change, host: f.host, port: f.port}
  125. events[f.host.String()] = event
  126. }
  127. event.change = f.change
  128. }
  129. }
  130. for _, f := range events {
  131. if gocqlDebug {
  132. log.Printf("gocql: dispatching event: %+v\n", f)
  133. }
  134. switch f.change {
  135. case "NEW_NODE":
  136. s.handleNewNode(f.host, f.port, true)
  137. case "REMOVED_NODE":
  138. s.handleRemovedNode(f.host, f.port)
  139. case "MOVED_NODE":
  140. // java-driver handles this, not mentioned in the spec
  141. // TODO(zariel): refresh token map
  142. case "UP":
  143. s.handleNodeUp(f.host, f.port, true)
  144. case "DOWN":
  145. s.handleNodeDown(f.host, f.port)
  146. }
  147. }
  148. }
  149. func (s *Session) handleNewNode(host net.IP, port int, waitForBinary bool) {
  150. // TODO(zariel): need to be able to filter discovered nodes
  151. var hostInfo *HostInfo
  152. if s.control != nil && !s.cfg.IgnorePeerAddr {
  153. var err error
  154. hostInfo, err = s.control.fetchHostInfo(host, port)
  155. if err != nil {
  156. log.Printf("gocql: events: unable to fetch host info for %v: %v\n", host, err)
  157. return
  158. }
  159. } else {
  160. hostInfo = &HostInfo{peer: host.String(), port: port, state: NodeUp}
  161. }
  162. addr := host.String()
  163. if s.cfg.IgnorePeerAddr && hostInfo.Peer() != addr {
  164. hostInfo.setPeer(addr)
  165. }
  166. if s.cfg.HostFilter != nil && !s.cfg.HostFilter.Accept(hostInfo) {
  167. return
  168. }
  169. if t := hostInfo.Version().nodeUpDelay(); t > 0 && waitForBinary {
  170. time.Sleep(t)
  171. }
  172. // should this handle token moving?
  173. if existing, ok := s.ring.addHostIfMissing(hostInfo); ok {
  174. existing.update(hostInfo)
  175. hostInfo = existing
  176. }
  177. s.pool.addHost(hostInfo)
  178. s.policy.AddHost(hostInfo)
  179. hostInfo.setState(NodeUp)
  180. if s.control != nil && !s.cfg.IgnorePeerAddr {
  181. s.hostSource.refreshRing()
  182. }
  183. }
  184. func (s *Session) handleRemovedNode(ip net.IP, port int) {
  185. // we remove all nodes but only add ones which pass the filter
  186. addr := ip.String()
  187. host := s.ring.getHost(addr)
  188. if host == nil {
  189. host = &HostInfo{peer: addr}
  190. }
  191. if s.cfg.HostFilter != nil && !s.cfg.HostFilter.Accept(host) {
  192. return
  193. }
  194. host.setState(NodeDown)
  195. s.policy.RemoveHost(addr)
  196. s.pool.removeHost(addr)
  197. s.ring.removeHost(addr)
  198. if !s.cfg.IgnorePeerAddr {
  199. s.hostSource.refreshRing()
  200. }
  201. }
  202. func (s *Session) handleNodeUp(ip net.IP, port int, waitForBinary bool) {
  203. if gocqlDebug {
  204. log.Printf("gocql: Session.handleNodeUp: %s:%d\n", ip.String(), port)
  205. }
  206. addr := ip.String()
  207. host := s.ring.getHost(addr)
  208. if host != nil {
  209. if s.cfg.IgnorePeerAddr && host.Peer() != addr {
  210. host.setPeer(addr)
  211. }
  212. if s.cfg.HostFilter != nil && !s.cfg.HostFilter.Accept(host) {
  213. return
  214. }
  215. if t := host.Version().nodeUpDelay(); t > 0 && waitForBinary {
  216. time.Sleep(t)
  217. }
  218. host.setPort(port)
  219. s.pool.hostUp(host)
  220. s.policy.HostUp(host)
  221. host.setState(NodeUp)
  222. return
  223. }
  224. s.handleNewNode(ip, port, waitForBinary)
  225. }
  226. func (s *Session) handleNodeDown(ip net.IP, port int) {
  227. if gocqlDebug {
  228. log.Printf("gocql: Session.handleNodeDown: %s:%d\n", ip.String(), port)
  229. }
  230. addr := ip.String()
  231. host := s.ring.getHost(addr)
  232. if host == nil {
  233. host = &HostInfo{peer: addr}
  234. }
  235. if s.cfg.HostFilter != nil && !s.cfg.HostFilter.Accept(host) {
  236. return
  237. }
  238. host.setState(NodeDown)
  239. s.policy.HostDown(addr)
  240. s.pool.hostDown(addr)
  241. }