forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events_ccm_test.go
297 lines (235 loc) · 6.26 KB
/
events_ccm_test.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
// +build ccm, ignore
package gocql
import (
"log"
"testing"
"time"
"github.com/gocql/gocql/internal/ccm"
)
func TestEventDiscovery(t *testing.T) {
t.Skip("FLAKE skipping")
if err := ccm.AllUp(); err != nil {
t.Fatal(err)
}
session := createSession(t)
defer session.Close()
status, err := ccm.Status()
if err != nil {
t.Fatal(err)
}
t.Logf("status=%+v\n", status)
session.pool.mu.RLock()
poolHosts := session.pool.hostConnPools // TODO: replace with session.ring
t.Logf("poolhosts=%+v\n", poolHosts)
// check we discovered all the nodes in the ring
for _, host := range status {
if _, ok := poolHosts[host.Addr]; !ok {
t.Errorf("did not discover %q", host.Addr)
}
}
session.pool.mu.RUnlock()
if t.Failed() {
t.FailNow()
}
}
func TestEventNodeDownControl(t *testing.T) {
t.Skip("FLAKE skipping")
const targetNode = "node1"
if err := ccm.AllUp(); err != nil {
t.Fatal(err)
}
status, err := ccm.Status()
if err != nil {
t.Fatal(err)
}
cluster := createCluster()
cluster.Hosts = []string{status[targetNode].Addr}
session := createSessionFromCluster(cluster, t)
defer session.Close()
t.Log("marking " + targetNode + " as down")
if err := ccm.NodeDown(targetNode); err != nil {
t.Fatal(err)
}
t.Logf("status=%+v\n", status)
t.Logf("marking node %q down: %v\n", targetNode, status[targetNode])
time.Sleep(5 * time.Second)
session.pool.mu.RLock()
poolHosts := session.pool.hostConnPools
node := status[targetNode]
t.Logf("poolhosts=%+v\n", poolHosts)
if _, ok := poolHosts[node.Addr]; ok {
session.pool.mu.RUnlock()
t.Fatal("node not removed after remove event")
}
session.pool.mu.RUnlock()
host := session.ring.getHost(node.Addr)
if host == nil {
t.Fatal("node not in metadata ring")
} else if host.IsUp() {
t.Fatalf("not not marked as down after event in metadata: %v", host)
}
}
func TestEventNodeDown(t *testing.T) {
t.Skip("FLAKE skipping")
const targetNode = "node3"
if err := ccm.AllUp(); err != nil {
t.Fatal(err)
}
session := createSession(t)
defer session.Close()
if err := ccm.NodeDown(targetNode); err != nil {
t.Fatal(err)
}
status, err := ccm.Status()
if err != nil {
t.Fatal(err)
}
t.Logf("status=%+v\n", status)
t.Logf("marking node %q down: %v\n", targetNode, status[targetNode])
time.Sleep(5 * time.Second)
session.pool.mu.RLock()
defer session.pool.mu.RUnlock()
poolHosts := session.pool.hostConnPools
node := status[targetNode]
t.Logf("poolhosts=%+v\n", poolHosts)
if _, ok := poolHosts[node.Addr]; ok {
t.Fatal("node not removed after remove event")
}
host := session.ring.getHost(node.Addr)
if host == nil {
t.Fatal("node not in metadata ring")
} else if host.IsUp() {
t.Fatalf("not not marked as down after event in metadata: %v", host)
}
}
func TestEventNodeUp(t *testing.T) {
t.Skip("FLAKE skipping")
if err := ccm.AllUp(); err != nil {
t.Fatal(err)
}
status, err := ccm.Status()
if err != nil {
t.Fatal(err)
}
log.Printf("status=%+v\n", status)
session := createSession(t)
defer session.Close()
const targetNode = "node2"
node := status[targetNode]
_, ok := session.pool.getPool(node.Addr)
if !ok {
session.pool.mu.RLock()
t.Errorf("target pool not in connection pool: addr=%q pools=%v", status[targetNode].Addr, session.pool.hostConnPools)
session.pool.mu.RUnlock()
t.FailNow()
}
if err := ccm.NodeDown(targetNode); err != nil {
t.Fatal(err)
}
time.Sleep(5 * time.Second)
_, ok = session.pool.getPool(node.Addr)
if ok {
t.Fatal("node not removed after remove event")
}
if err := ccm.NodeUp(targetNode); err != nil {
t.Fatal(err)
}
// cassandra < 2.2 needs 10 seconds to start up the binary service
time.Sleep(15 * time.Second)
_, ok = session.pool.getPool(node.Addr)
if !ok {
t.Fatal("node not added after node added event")
}
host := session.ring.getHost(node.Addr)
if host == nil {
t.Fatal("node not in metadata ring")
} else if !host.IsUp() {
t.Fatalf("not not marked as UP after event in metadata: addr=%q host=%p: %v", node.Addr, host, host)
}
}
func TestEventFilter(t *testing.T) {
t.Skip("FLAKE skipping")
if err := ccm.AllUp(); err != nil {
t.Fatal(err)
}
status, err := ccm.Status()
if err != nil {
t.Fatal(err)
}
log.Printf("status=%+v\n", status)
cluster := createCluster()
cluster.HostFilter = WhiteListHostFilter(status["node1"].Addr)
session := createSessionFromCluster(cluster, t)
defer session.Close()
if _, ok := session.pool.getPool(status["node1"].Addr); !ok {
t.Errorf("should have %v in pool but dont", "node1")
}
for _, host := range [...]string{"node2", "node3"} {
_, ok := session.pool.getPool(status[host].Addr)
if ok {
t.Errorf("should not have %v in pool", host)
}
}
if t.Failed() {
t.FailNow()
}
if err := ccm.NodeDown("node2"); err != nil {
t.Fatal(err)
}
time.Sleep(5 * time.Second)
if err := ccm.NodeUp("node2"); err != nil {
t.Fatal(err)
}
time.Sleep(15 * time.Second)
for _, host := range [...]string{"node2", "node3"} {
_, ok := session.pool.getPool(status[host].Addr)
if ok {
t.Errorf("should not have %v in pool", host)
}
}
if t.Failed() {
t.FailNow()
}
}
func TestEventDownQueryable(t *testing.T) {
t.Skip("FLAKE skipping")
if err := ccm.AllUp(); err != nil {
t.Fatal(err)
}
status, err := ccm.Status()
if err != nil {
t.Fatal(err)
}
log.Printf("status=%+v\n", status)
const targetNode = "node1"
addr := status[targetNode].Addr
cluster := createCluster()
cluster.Hosts = []string{addr}
cluster.HostFilter = WhiteListHostFilter(addr)
session := createSessionFromCluster(cluster, t)
defer session.Close()
if pool, ok := session.pool.getPool(addr); !ok {
t.Fatalf("should have %v in pool but dont", addr)
} else if !pool.host.IsUp() {
t.Fatalf("host is not up %v", pool.host)
}
if err := ccm.NodeDown(targetNode); err != nil {
t.Fatal(err)
}
time.Sleep(5 * time.Second)
if err := ccm.NodeUp(targetNode); err != nil {
t.Fatal(err)
}
time.Sleep(15 * time.Second)
if pool, ok := session.pool.getPool(addr); !ok {
t.Fatalf("should have %v in pool but dont", addr)
} else if !pool.host.IsUp() {
t.Fatalf("host is not up %v", pool.host)
}
var rows int
if err := session.Query("SELECT COUNT(*) FROM system.local").Scan(&rows); err != nil {
t.Fatal(err)
} else if rows != 1 {
t.Fatalf("expected to get 1 row got %d", rows)
}
}