events.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. }
  93. func (s *Session) handleNodeEvent(frames []frame) {
  94. type nodeEvent struct {
  95. change string
  96. host net.IP
  97. port int
  98. }
  99. events := make(map[string]*nodeEvent)
  100. for _, frame := range frames {
  101. // TODO: can we be sure the order of events in the buffer is correct?
  102. switch f := frame.(type) {
  103. case *topologyChangeEventFrame:
  104. event, ok := events[f.host.String()]
  105. if !ok {
  106. event = &nodeEvent{change: f.change, host: f.host, port: f.port}
  107. events[f.host.String()] = event
  108. }
  109. event.change = f.change
  110. case *statusChangeEventFrame:
  111. event, ok := events[f.host.String()]
  112. if !ok {
  113. event = &nodeEvent{change: f.change, host: f.host, port: f.port}
  114. events[f.host.String()] = event
  115. }
  116. event.change = f.change
  117. }
  118. }
  119. for _, f := range events {
  120. if gocqlDebug {
  121. log.Printf("gocql: dispatching event: %+v\n", f)
  122. }
  123. switch f.change {
  124. case "NEW_NODE":
  125. s.handleNewNode(f.host, f.port, true)
  126. case "REMOVED_NODE":
  127. s.handleRemovedNode(f.host, f.port)
  128. case "MOVED_NODE":
  129. // java-driver handles this, not mentioned in the spec
  130. // TODO(zariel): refresh token map
  131. case "UP":
  132. s.handleNodeUp(f.host, f.port, true)
  133. case "DOWN":
  134. s.handleNodeDown(f.host, f.port)
  135. }
  136. }
  137. }
  138. func (s *Session) handleNewNode(host net.IP, port int, waitForBinary bool) {
  139. // TODO(zariel): need to be able to filter discovered nodes
  140. var hostInfo *HostInfo
  141. if s.control != nil {
  142. var err error
  143. hostInfo, err = s.control.fetchHostInfo(host, port)
  144. if err != nil {
  145. log.Printf("gocql: events: unable to fetch host info for %v: %v\n", host, err)
  146. return
  147. }
  148. } else {
  149. hostInfo = &HostInfo{peer: host.String(), port: port, state: NodeUp}
  150. }
  151. addr := host.String()
  152. if s.cfg.IgnorePeerAddr && hostInfo.Peer() != addr {
  153. hostInfo.setPeer(addr)
  154. }
  155. if s.cfg.HostFilter != nil {
  156. if !s.cfg.HostFilter.Accept(hostInfo) {
  157. return
  158. }
  159. } else if !s.cfg.Discovery.matchFilter(hostInfo) {
  160. // TODO: remove this when the host selection policy is more sophisticated
  161. return
  162. }
  163. if t := hostInfo.Version().nodeUpDelay(); t > 0 && waitForBinary {
  164. time.Sleep(t)
  165. }
  166. // should this handle token moving?
  167. if existing, ok := s.ring.addHostIfMissing(hostInfo); ok {
  168. existing.update(hostInfo)
  169. hostInfo = existing
  170. }
  171. s.pool.addHost(hostInfo)
  172. hostInfo.setState(NodeUp)
  173. if s.control != nil {
  174. s.hostSource.refreshRing()
  175. }
  176. }
  177. func (s *Session) handleRemovedNode(ip net.IP, port int) {
  178. // we remove all nodes but only add ones which pass the filter
  179. addr := ip.String()
  180. host := s.ring.getHost(addr)
  181. if host == nil {
  182. host = &HostInfo{peer: addr}
  183. }
  184. if s.cfg.HostFilter != nil && !s.cfg.HostFilter.Accept(host) {
  185. return
  186. }
  187. host.setState(NodeDown)
  188. s.pool.removeHost(addr)
  189. s.ring.removeHost(addr)
  190. s.hostSource.refreshRing()
  191. }
  192. func (s *Session) handleNodeUp(ip net.IP, port int, waitForBinary bool) {
  193. addr := ip.String()
  194. host := s.ring.getHost(addr)
  195. if host != nil {
  196. if s.cfg.IgnorePeerAddr && host.Peer() != addr {
  197. host.setPeer(addr)
  198. }
  199. if s.cfg.HostFilter != nil {
  200. if !s.cfg.HostFilter.Accept(host) {
  201. return
  202. }
  203. } else if !s.cfg.Discovery.matchFilter(host) {
  204. // TODO: remove this when the host selection policy is more sophisticated
  205. return
  206. }
  207. if t := host.Version().nodeUpDelay(); t > 0 && waitForBinary {
  208. time.Sleep(t)
  209. }
  210. host.setPort(port)
  211. s.pool.hostUp(host)
  212. host.setState(NodeUp)
  213. return
  214. }
  215. s.handleNewNode(ip, port, waitForBinary)
  216. }
  217. func (s *Session) handleNodeDown(ip net.IP, port int) {
  218. addr := ip.String()
  219. host := s.ring.getHost(addr)
  220. if host == nil {
  221. host = &HostInfo{peer: addr}
  222. }
  223. if s.cfg.HostFilter != nil && !s.cfg.HostFilter.Accept(host) {
  224. return
  225. }
  226. host.setState(NodeDown)
  227. s.pool.hostDown(addr)
  228. }