forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host_source.go
166 lines (134 loc) · 3.93 KB
/
host_source.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
package gocql
import (
"fmt"
"log"
"net"
"sync"
"time"
)
type HostInfo struct {
Peer string
DataCenter string
Rack string
HostId string
Tokens []string
}
func (h HostInfo) String() string {
return fmt.Sprintf("[hostinfo peer=%q data_centre=%q rack=%q host_id=%q num_tokens=%d]", h.Peer, h.DataCenter, h.Rack, h.HostId, len(h.Tokens))
}
// Polls system.peers at a specific interval to find new hosts
type ringDescriber struct {
dcFilter string
rackFilter string
prevHosts []HostInfo
prevPartitioner string
session *Session
closeChan chan bool
// indicates that we can use system.local to get the connections remote address
localHasRpcAddr bool
mu sync.Mutex
}
func checkSystemLocal(control *controlConn) (bool, error) {
iter := control.query("SELECT broadcast_address FROM system.local")
if err := iter.err; err != nil {
if errf, ok := err.(*errorFrame); ok {
if errf.code == errSyntax {
return false, nil
}
}
return false, err
}
return true, nil
}
func (r *ringDescriber) GetHosts() (hosts []HostInfo, partitioner string, err error) {
r.mu.Lock()
defer r.mu.Unlock()
// we need conn to be the same because we need to query system.peers and system.local
// on the same node to get the whole cluster
const (
legacyLocalQuery = "SELECT data_center, rack, host_id, tokens, partitioner FROM system.local"
// only supported in 2.2.0, 2.1.6, 2.0.16
localQuery = "SELECT broadcast_address, data_center, rack, host_id, tokens, partitioner FROM system.local"
)
var localHost HostInfo
if r.localHasRpcAddr {
iter := r.session.control.query(localQuery)
if iter == nil {
return r.prevHosts, r.prevPartitioner, nil
}
iter.Scan(&localHost.Peer, &localHost.DataCenter, &localHost.Rack,
&localHost.HostId, &localHost.Tokens, &partitioner)
if err = iter.Close(); err != nil {
return nil, "", err
}
} else {
iter := r.session.control.query(legacyLocalQuery)
if iter == nil {
return r.prevHosts, r.prevPartitioner, nil
}
iter.Scan(&localHost.DataCenter, &localHost.Rack, &localHost.HostId, &localHost.Tokens, &partitioner)
if err = iter.Close(); err != nil {
return nil, "", err
}
addr, _, err := net.SplitHostPort(r.session.control.addr())
if err != nil {
// this should not happen, ever, as this is the address that was dialed by conn, here
// a panic makes sense, please report a bug if it occurs.
panic(err)
}
localHost.Peer = addr
}
hosts = []HostInfo{localHost}
iter := r.session.control.query("SELECT rpc_address, data_center, rack, host_id, tokens FROM system.peers")
if iter == nil {
return r.prevHosts, r.prevPartitioner, nil
}
host := HostInfo{}
for iter.Scan(&host.Peer, &host.DataCenter, &host.Rack, &host.HostId, &host.Tokens) {
if r.matchFilter(&host) {
hosts = append(hosts, host)
}
host = HostInfo{}
}
if err = iter.Close(); err != nil {
return nil, "", err
}
r.prevHosts = hosts
r.prevPartitioner = partitioner
return hosts, partitioner, nil
}
func (r *ringDescriber) matchFilter(host *HostInfo) bool {
if r.dcFilter != "" && r.dcFilter != host.DataCenter {
return false
}
if r.rackFilter != "" && r.rackFilter != host.Rack {
return false
}
return true
}
func (r *ringDescriber) refreshRing() {
// if we have 0 hosts this will return the previous list of hosts to
// attempt to reconnect to the cluster otherwise we would never find
// downed hosts again, could possibly have an optimisation to only
// try to add new hosts if GetHosts didnt error and the hosts didnt change.
hosts, partitioner, err := r.GetHosts()
if err != nil {
log.Println("RingDescriber: unable to get ring topology:", err)
return
}
r.session.pool.SetHosts(hosts)
r.session.pool.SetPartitioner(partitioner)
}
func (r *ringDescriber) run(sleep time.Duration) {
if sleep == 0 {
sleep = 30 * time.Second
}
for {
r.refreshRing()
select {
case <-time.After(sleep):
case <-r.closeChan:
return
}
}
}