events.go 4.5 KB

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