events.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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) handleNodeEvent(frames []frame) {
  70. type nodeEvent struct {
  71. change string
  72. host net.IP
  73. port int
  74. }
  75. events := make(map[string]*nodeEvent)
  76. for _, frame := range frames {
  77. // TODO: can we be sure the order of events in the buffer is correct?
  78. switch f := frame.(type) {
  79. case *topologyChangeEventFrame:
  80. event, ok := events[f.host.String()]
  81. if !ok {
  82. event = &nodeEvent{change: f.change, host: f.host, port: f.port}
  83. events[f.host.String()] = event
  84. }
  85. event.change = f.change
  86. case *statusChangeEventFrame:
  87. event, ok := events[f.host.String()]
  88. if !ok {
  89. event = &nodeEvent{change: f.change, host: f.host, port: f.port}
  90. events[f.host.String()] = event
  91. }
  92. event.change = f.change
  93. }
  94. }
  95. for _, f := range events {
  96. log.Printf("debouncing event %+v\n", f)
  97. switch f.change {
  98. case "NEW_NODE":
  99. s.handleNewNode(f.host, f.port)
  100. case "REMOVED_NODE":
  101. s.handleRemovedNode(f.host, f.port)
  102. case "MOVED_NODE":
  103. // java-driver handles this, not mentioned in the spec
  104. // TODO(zariel): refresh token map
  105. case "UP":
  106. s.handleNodeUp(f.host, f.port)
  107. case "DOWN":
  108. s.handleNodeDown(f.host, f.port)
  109. }
  110. }
  111. }
  112. func (s *Session) handleEvent(framer *framer) {
  113. // TODO(zariel): need to debounce events frames, and possible also events
  114. defer framerPool.Put(framer)
  115. frame, err := framer.parseFrame()
  116. if err != nil {
  117. // TODO: logger
  118. log.Printf("gocql: unable to parse event frame: %v\n", err)
  119. return
  120. }
  121. log.Println(frame)
  122. // TODO: handle medatadata events
  123. switch f := frame.(type) {
  124. case *schemaChangeKeyspace:
  125. case *schemaChangeFunction:
  126. case *schemaChangeTable:
  127. case *topologyChangeEventFrame, *statusChangeEventFrame:
  128. s.nodeEvents.debounce(frame)
  129. default:
  130. log.Printf("gocql: invalid event frame (%T): %v\n", f, f)
  131. }
  132. }
  133. func (s *Session) handleNewNode(host net.IP, port int) {
  134. // TODO(zariel): need to be able to filter discovered nodes
  135. var hostInfo *HostInfo
  136. if s.control != nil {
  137. var err error
  138. hostInfo, err = s.control.fetchHostInfo(host, port)
  139. if err != nil {
  140. log.Printf("gocql: events: unable to fetch host info for %v: %v\n", host, err)
  141. return
  142. }
  143. } else {
  144. hostInfo = &HostInfo{peer: host.String(), port: port, state: NodeUp}
  145. }
  146. // should this handle token moving?
  147. if existing, ok := s.ring.addHostIfMissing(hostInfo); !ok {
  148. existing.update(hostInfo)
  149. hostInfo = existing
  150. }
  151. s.pool.addHost(hostInfo)
  152. if s.control != nil {
  153. s.hostSource.refreshRing()
  154. }
  155. }
  156. func (s *Session) handleRemovedNode(ip net.IP, port int) {
  157. // we remove all nodes but only add ones which pass the filter
  158. addr := ip.String()
  159. s.pool.removeHost(addr)
  160. s.ring.removeHost(addr)
  161. s.hostSource.refreshRing()
  162. }
  163. func (s *Session) handleNodeUp(ip net.IP, port int) {
  164. addr := ip.String()
  165. host := s.ring.getHost(addr)
  166. if host != nil {
  167. host.setState(NodeUp)
  168. s.pool.hostUp(host)
  169. return
  170. }
  171. // TODO: this could infinite loop
  172. s.handleNewNode(ip, port)
  173. }
  174. func (s *Session) handleNodeDown(ip net.IP, port int) {
  175. addr := ip.String()
  176. host := s.ring.getHost(addr)
  177. if host != nil {
  178. host.setState(NodeDown)
  179. }
  180. s.pool.hostDown(addr)
  181. }