forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.go
417 lines (338 loc) · 8.76 KB
/
control.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package gocql
import (
crand "crypto/rand"
"errors"
"fmt"
"golang.org/x/net/context"
"log"
"math/rand"
"net"
"strconv"
"sync/atomic"
"time"
)
var (
randr *rand.Rand
)
func init() {
b := make([]byte, 4)
if _, err := crand.Read(b); err != nil {
panic(fmt.Sprintf("unable to seed random number generator: %v", err))
}
randr = rand.New(rand.NewSource(int64(readInt(b))))
}
// Ensure that the atomic variable is aligned to a 64bit boundary
// so that atomic operations can be applied on 32bit architectures.
type controlConn struct {
session *Session
conn atomic.Value
retry RetryPolicy
started int32
quit chan struct{}
}
func createControlConn(session *Session) *controlConn {
control := &controlConn{
session: session,
quit: make(chan struct{}),
retry: &SimpleRetryPolicy{NumRetries: 3},
}
control.conn.Store((*Conn)(nil))
return control
}
func (c *controlConn) heartBeat() {
if !atomic.CompareAndSwapInt32(&c.started, 0, 1) {
return
}
sleepTime := 1 * time.Second
for {
select {
case <-c.quit:
return
case <-time.After(sleepTime):
}
resp, err := c.writeFrame(&writeOptionsFrame{})
if err != nil {
goto reconn
}
switch resp.(type) {
case *supportedFrame:
// Everything ok
sleepTime = 5 * time.Second
continue
case error:
goto reconn
default:
panic(fmt.Sprintf("gocql: unknown frame in response to options: %T", resp))
}
reconn:
// try to connect a bit faster
sleepTime = 1 * time.Second
c.reconnect(true)
// time.Sleep(5 * time.Second)
continue
}
}
func hostInfo(addr string, defaultPort int) (*HostInfo, error) {
var port int
host, portStr, err := net.SplitHostPort(addr)
if err != nil {
host = addr
port = defaultPort
} else {
port, err = strconv.Atoi(portStr)
if err != nil {
return nil, err
}
}
return &HostInfo{peer: host, port: port}, nil
}
func (c *controlConn) shuffleDial(endpoints []string) (conn *Conn, err error) {
perm := randr.Perm(len(endpoints))
shuffled := make([]string, len(endpoints))
for i, endpoint := range endpoints {
shuffled[perm[i]] = endpoint
}
// shuffle endpoints so not all drivers will connect to the same initial
// node.
for _, addr := range shuffled {
if addr == "" {
return nil, fmt.Errorf("invalid address: %q", addr)
}
port := c.session.cfg.Port
addr = JoinHostPort(addr, port)
var host *HostInfo
host, err = hostInfo(addr, port)
if err != nil {
return nil, fmt.Errorf("invalid address: %q: %v", addr, err)
}
hostInfo, _ := c.session.ring.addHostIfMissing(host)
conn, err = c.session.connect(addr, c, hostInfo)
if err == nil {
return conn, err
}
log.Printf("gocql: unable to dial control conn %v: %v\n", addr, err)
}
if err != nil {
return nil, err
}
return conn, nil
}
func (c *controlConn) connect(endpoints []string) error {
if len(endpoints) == 0 {
return errors.New("control: no endpoints specified")
}
conn, err := c.shuffleDial(endpoints)
if err != nil {
return fmt.Errorf("control: unable to connect to initial hosts: %v", err)
}
if err := c.setupConn(conn); err != nil {
conn.Close()
return fmt.Errorf("control: unable to setup connection: %v", err)
}
// we could fetch the initial ring here and update initial host data. So that
// when we return from here we have a ring topology ready to go.
go c.heartBeat()
return nil
}
func (c *controlConn) setupConn(conn *Conn) error {
if err := c.registerEvents(conn); err != nil {
conn.Close()
return err
}
c.conn.Store(conn)
host, portstr, err := net.SplitHostPort(conn.conn.RemoteAddr().String())
if err != nil {
return err
}
port, err := strconv.Atoi(portstr)
if err != nil {
return err
}
c.session.handleNodeUp(net.ParseIP(host), port, false)
return nil
}
func (c *controlConn) registerEvents(conn *Conn) error {
var events []string
if !c.session.cfg.Events.DisableTopologyEvents {
events = append(events, "TOPOLOGY_CHANGE")
}
if !c.session.cfg.Events.DisableNodeStatusEvents {
events = append(events, "STATUS_CHANGE")
}
if !c.session.cfg.Events.DisableSchemaEvents {
events = append(events, "SCHEMA_CHANGE")
}
if len(events) == 0 {
return nil
}
framer, err := conn.exec(context.Background(),
&writeRegisterFrame{
events: events,
}, nil)
if err != nil {
return err
}
frame, err := framer.parseFrame()
if err != nil {
return err
} else if _, ok := frame.(*readyFrame); !ok {
return fmt.Errorf("unexpected frame in response to register: got %T: %v\n", frame, frame)
}
return nil
}
func (c *controlConn) reconnect(refreshring bool) {
// TODO: simplify this function, use session.ring to get hosts instead of the
// connection pool
addr := c.addr()
oldConn := c.conn.Load().(*Conn)
if oldConn != nil {
oldConn.Close()
}
var newConn *Conn
if addr != "" {
// try to connect to the old host
conn, err := c.session.connect(addr, c, oldConn.host)
if err != nil {
// host is dead
// TODO: this is replicated in a few places
ip, portStr, _ := net.SplitHostPort(addr)
port, _ := strconv.Atoi(portStr)
c.session.handleNodeDown(net.ParseIP(ip), port)
} else {
newConn = conn
}
}
// TODO: should have our own roundrobbin for hosts so that we can try each
// in succession and guantee that we get a different host each time.
if newConn == nil {
host := c.session.ring.rrHost()
if host == nil {
c.connect(c.session.ring.endpoints)
return
}
var err error
newConn, err = c.session.connect(host.Peer(), c, host)
if err != nil {
// TODO: add log handler for things like this
return
}
}
if err := c.setupConn(newConn); err != nil {
newConn.Close()
log.Printf("gocql: control unable to register events: %v\n", err)
return
}
if refreshring {
c.session.hostSource.refreshRing()
}
}
func (c *controlConn) HandleError(conn *Conn, err error, closed bool) {
if !closed {
return
}
oldConn := c.conn.Load().(*Conn)
if oldConn != conn {
return
}
c.reconnect(true)
}
func (c *controlConn) writeFrame(w frameWriter) (frame, error) {
conn := c.conn.Load().(*Conn)
if conn == nil {
return nil, errNoControl
}
framer, err := conn.exec(context.Background(), w, nil)
if err != nil {
return nil, err
}
return framer.parseFrame()
}
func (c *controlConn) withConn(fn func(*Conn) *Iter) *Iter {
const maxConnectAttempts = 5
connectAttempts := 0
for i := 0; i < maxConnectAttempts; i++ {
conn := c.conn.Load().(*Conn)
if conn == nil {
if connectAttempts > maxConnectAttempts {
break
}
connectAttempts++
c.reconnect(false)
continue
}
return fn(conn)
}
return &Iter{err: errNoControl}
}
// query will return nil if the connection is closed or nil
func (c *controlConn) query(statement string, values ...interface{}) (iter *Iter) {
q := c.session.Query(statement, values...).Consistency(One).RoutingKey([]byte{})
for {
iter = c.withConn(func(conn *Conn) *Iter {
return conn.executeQuery(q)
})
if gocqlDebug && iter.err != nil {
log.Printf("control: error executing %q: %v\n", statement, iter.err)
}
q.attempts++
if iter.err == nil || !c.retry.Attempt(q) {
break
}
}
return
}
func (c *controlConn) fetchHostInfo(addr net.IP, port int) (*HostInfo, error) {
// TODO(zariel): we should probably move this into host_source or atleast
// share code with it.
hostname, _, err := net.SplitHostPort(c.addr())
if err != nil {
return nil, fmt.Errorf("unable to fetch host info, invalid conn addr: %q: %v", c.addr(), err)
}
isLocal := hostname == addr.String()
var fn func(*HostInfo) error
if isLocal {
fn = func(host *HostInfo) error {
// TODO(zariel): should we fetch rpc_address from here?
iter := c.query("SELECT data_center, rack, host_id, tokens, release_version FROM system.local WHERE key='local'")
iter.Scan(&host.dataCenter, &host.rack, &host.hostId, &host.tokens, &host.version)
return iter.Close()
}
} else {
fn = func(host *HostInfo) error {
// TODO(zariel): should we fetch rpc_address from here?
iter := c.query("SELECT data_center, rack, host_id, tokens, release_version FROM system.peers WHERE peer=?", addr)
iter.Scan(&host.dataCenter, &host.rack, &host.hostId, &host.tokens, &host.version)
return iter.Close()
}
}
host := &HostInfo{
port: port,
}
if err := fn(host); err != nil {
return nil, err
}
host.peer = addr.String()
return host, nil
}
func (c *controlConn) awaitSchemaAgreement() error {
return c.withConn(func(conn *Conn) *Iter {
return &Iter{err: conn.awaitSchemaAgreement()}
}).err
}
func (c *controlConn) addr() string {
conn := c.conn.Load().(*Conn)
if conn == nil {
return ""
}
return conn.addr
}
func (c *controlConn) close() {
if atomic.CompareAndSwapInt32(&c.started, 1, -1) {
c.quit <- struct{}{}
}
conn := c.conn.Load().(*Conn)
if conn != nil {
conn.Close()
}
}
var errNoControl = errors.New("gocql: no control connection available")