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