forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.go
497 lines (407 loc) · 10.9 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package gocql
import (
"context"
crand "crypto/rand"
"errors"
"fmt"
"math/rand"
"net"
"os"
"regexp"
"strconv"
"sync"
"sync/atomic"
"time"
)
var (
randr *rand.Rand
mutRandr sync.Mutex
)
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))))
}
const (
controlConnStarting = 0
controlConnStarted = 1
controlConnClosing = -1
)
// Ensure that the atomic variable is aligned to a 64bit boundary
// so that atomic operations can be applied on 32bit architectures.
type controlConn struct {
state int32
reconnecting int32
session *Session
conn atomic.Value
retry RetryPolicy
quit chan struct{}
}
func createControlConn(session *Session) *controlConn {
control := &controlConn{
session: session,
quit: make(chan struct{}),
retry: &SimpleRetryPolicy{NumRetries: 3},
}
control.conn.Store((*connHost)(nil))
return control
}
func (c *controlConn) heartBeat() {
if !atomic.CompareAndSwapInt32(&c.state, controlConnStarting, controlConnStarted) {
return
}
sleepTime := 1 * time.Second
timer := time.NewTimer(sleepTime)
defer timer.Stop()
for {
timer.Reset(sleepTime)
select {
case <-c.quit:
return
case <-timer.C:
}
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()
continue
}
}
var hostLookupPreferV4 = os.Getenv("GOCQL_HOST_LOOKUP_PREFER_V4") == "true"
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
}
}
var hosts []*HostInfo
// Check if host is a literal IP address
if ip := net.ParseIP(host); ip != nil {
hosts = append(hosts, &HostInfo{hostname: host, connectAddress: ip, port: port})
return hosts, nil
}
// Look up host in DNS
ips, err := LookupIP(host)
if err != nil {
return nil, err
} else if len(ips) == 0 {
return nil, fmt.Errorf("no IP's returned from DNS lookup for %q", addr)
}
// Filter to v4 addresses if any present
if hostLookupPreferV4 {
var preferredIPs []net.IP
for _, v := range ips {
if v4 := v.To4(); v4 != nil {
preferredIPs = append(preferredIPs, v4)
}
}
if len(preferredIPs) != 0 {
ips = preferredIPs
}
}
for _, ip := range ips {
hosts = append(hosts, &HostInfo{hostname: host, connectAddress: ip, port: port})
}
return hosts, nil
}
func shuffleHosts(hosts []*HostInfo) []*HostInfo {
shuffled := make([]*HostInfo, len(hosts))
copy(shuffled, hosts)
mutRandr.Lock()
randr.Shuffle(len(hosts), func(i, j int) {
shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
})
mutRandr.Unlock()
return shuffled
}
// this is going to be version dependant and a nightmare to maintain :(
var protocolSupportRe = regexp.MustCompile(`the lowest supported version is \d+ and the greatest is (\d+)$`)
func parseProtocolFromError(err error) int {
// I really wish this had the actual info in the error frame...
matches := protocolSupportRe.FindAllStringSubmatch(err.Error(), -1)
if len(matches) != 1 || len(matches[0]) != 2 {
if verr, ok := err.(*protocolError); ok {
return int(verr.frame.Header().version.version())
}
return 0
}
max, err := strconv.Atoi(matches[0][1])
if err != nil {
return 0
}
return max
}
func (c *controlConn) discoverProtocol(hosts []*HostInfo) (int, error) {
hosts = shuffleHosts(hosts)
connCfg := *c.session.connCfg
connCfg.ProtoVersion = 4 // TODO: define maxProtocol
handler := connErrorHandlerFn(func(c *Conn, err error, closed bool) {
// we should never get here, but if we do it means we connected to a
// host successfully which means our attempted protocol version worked
if !closed {
c.Close()
}
})
var err error
for _, host := range hosts {
var conn *Conn
conn, err = c.session.dial(c.session.ctx, host, &connCfg, handler)
if conn != nil {
conn.Close()
}
if err == nil {
return connCfg.ProtoVersion, nil
}
if proto := parseProtocolFromError(err); proto > 0 {
return proto, nil
}
}
return 0, err
}
func (c *controlConn) connect(hosts []*HostInfo) error {
if len(hosts) == 0 {
return errors.New("control: no endpoints specified")
}
// shuffle endpoints so not all drivers will connect to the same initial
// node.
hosts = shuffleHosts(hosts)
cfg := *c.session.connCfg
cfg.disableCoalesce = true
var conn *Conn
var err error
for _, host := range hosts {
conn, err = c.session.dial(c.session.ctx, host, &cfg, c)
if err != nil {
c.session.logger.Printf("gocql: unable to dial control conn %v:%v: %v\n", host.ConnectAddress(), host.Port(), err)
continue
}
err = c.setupConn(conn)
if err == nil {
break
}
c.session.logger.Printf("gocql: unable setup control conn %v:%v: %v\n", host.ConnectAddress(), host.Port(), err)
conn.Close()
conn = nil
}
if conn == nil {
return fmt.Errorf("unable to connect to initial hosts: %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
}
type connHost struct {
conn *Conn
host *HostInfo
}
func (c *controlConn) setupConn(conn *Conn) error {
// we need up-to-date host info for the filterHost call below
iter := conn.querySystemLocal(context.TODO())
host, err := c.session.hostInfoFromIter(iter, conn.host.connectAddress, conn.conn.RemoteAddr().(*net.TCPAddr).Port)
if err != nil {
return err
}
host = c.session.ring.addOrUpdate(host)
if c.session.cfg.filterHost(host) {
return fmt.Errorf("host was filtered: %v", host.ConnectAddress())
}
if err := c.registerEvents(conn); err != nil {
return err
}
ch := &connHost{
conn: conn,
host: host,
}
c.conn.Store(ch)
if c.session.initialized() {
// We connected to control conn, so add the connect the host in pool as well.
// Notify session we can start trying to connect to the node.
// We can't start the fill before the session is initialized, otherwise the fill would interfere
// with the fill called by Session.init. Session.init needs to wait for its fill to finish and that
// would return immediately if we started the fill here.
// TODO(martin-sucha): Trigger pool refill for all hosts, like in reconnectDownedHosts?
go c.session.startPoolFill(host)
}
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() {
if atomic.LoadInt32(&c.state) == controlConnClosing {
return
}
if !atomic.CompareAndSwapInt32(&c.reconnecting, 0, 1) {
return
}
defer atomic.StoreInt32(&c.reconnecting, 0)
hosts := c.session.ring.allHosts()
hosts = shuffleHosts(hosts)
// keep the old behavior of connecting to the old host first by moving it to
// the front of the slice
ch := c.getConn()
if ch != nil {
for i := range hosts {
if hosts[i].Equal(ch.host) {
hosts[0], hosts[i] = hosts[i], hosts[0]
break
}
}
ch.conn.Close()
}
var conn *Conn
var err error
for _, host := range hosts {
conn, err = c.session.connect(c.session.ctx, host, c)
if err != nil {
c.session.logger.Printf("gocql: unable to dial control conn %v:%v: %v\n", host.ConnectAddress(), host.Port(), err)
continue
}
err = c.setupConn(conn)
if err == nil {
break
}
c.session.logger.Printf("gocql: unable setup control conn %v:%v: %v\n", host.ConnectAddress(), host.Port(), err)
conn.Close()
conn = nil
}
if conn == nil {
c.session.logger.Printf("gocql: control unable to register events: %v\n", err)
return
}
err = c.session.refreshRing()
if err != nil {
c.session.logger.Printf("gocql: unable to refresh ring: %v\n", err)
}
}
func (c *controlConn) HandleError(conn *Conn, err error, closed bool) {
if !closed {
return
}
oldConn := c.getConn()
// If connection has long gone, and not been attempted for awhile,
// it's possible to have oldConn as nil here (#1297).
if oldConn != nil && oldConn.conn != conn {
return
}
c.reconnect()
}
func (c *controlConn) getConn() *connHost {
return c.conn.Load().(*connHost)
}
func (c *controlConn) writeFrame(w frameBuilder) (frame, error) {
ch := c.getConn()
if ch == nil {
return nil, errNoControl
}
framer, err := ch.conn.exec(context.Background(), w, nil)
if err != nil {
return nil, err
}
return framer.parseFrame()
}
func (c *controlConn) withConnHost(fn func(*connHost) *Iter) *Iter {
const maxConnectAttempts = 5
connectAttempts := 0
for i := 0; i < maxConnectAttempts; i++ {
ch := c.getConn()
if ch == nil {
if connectAttempts > maxConnectAttempts {
break
}
connectAttempts++
c.reconnect()
continue
}
return fn(ch)
}
return &Iter{err: errNoControl}
}
func (c *controlConn) withConn(fn func(*Conn) *Iter) *Iter {
return c.withConnHost(func(ch *connHost) *Iter {
return fn(ch.conn)
})
}
// 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{}).Trace(nil)
for {
iter = c.withConn(func(conn *Conn) *Iter {
// we want to keep the query on the control connection
q.conn = conn
return conn.executeQuery(context.TODO(), q)
})
if gocqlDebug && iter.err != nil {
c.session.logger.Printf("control: error executing %q: %v\n", statement, iter.err)
}
q.AddAttempts(1, c.getConn().host)
if iter.err == nil || !c.retry.Attempt(q) {
break
}
}
return
}
func (c *controlConn) awaitSchemaAgreement() error {
return c.withConn(func(conn *Conn) *Iter {
return &Iter{err: conn.awaitSchemaAgreement(context.TODO())}
}).err
}
func (c *controlConn) close() {
if atomic.CompareAndSwapInt32(&c.state, controlConnStarted, controlConnClosing) {
c.quit <- struct{}{}
}
ch := c.getConn()
if ch != nil {
ch.conn.Close()
}
}
var errNoControl = errors.New("gocql: no control connection available")