forked from scylladb/gocql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scylla_shard_aware_port_common_test.go
388 lines (309 loc) · 10.2 KB
/
scylla_shard_aware_port_common_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
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
//go:build integration || unit
// +build integration unit
package gocql
import (
"context"
"fmt"
"net"
"strconv"
"sync"
"testing"
"time"
)
type makeClusterTestFunc func() *ClusterConfig
func testShardAwarePortNoReconnections(t *testing.T, makeCluster makeClusterTestFunc) {
ctx, cancel := context.WithCancel(context.Background())
wg := &sync.WaitGroup{}
// Initialize 10 sessions in parallel.
// If shard-aware port is used and configured properly, we should get
// a connection to each shard without any retries.
// For each host, there should be N-1 connections to the special port.
// Run 10 sessions in parallel
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
// Each session gets a separate configuration, because we need to have
// separate connection listeners - we need to differentiate connections
// made for each session separately
dialer := newLoggingTestDialer()
cluster := makeCluster()
cluster.PoolConfig.HostSelectionPolicy = TokenAwareHostPolicy(RoundRobinHostPolicy())
cluster.Dialer = dialer
useTLS := cluster.SslOpts != nil
sess, err := cluster.CreateSession()
if err != nil {
cancel()
return
}
defer sess.Close()
if err := waitUntilPoolsStopFilling(ctx, sess, 10*time.Second); err != nil {
cancel()
return
}
hosts := sess.ring.allHosts()
for _, host := range hosts {
t.Logf("checking host %q hostID: %q", host.hostname, host.hostId)
hostPool, ok := sess.pool.getPool(host)
if !ok {
t.Fatalf("host %q not found in session connection pool", host.HostID())
}
shardAwarePort := getShardAwarePort(hostPool, useTLS)
if shardAwarePort == 0 {
// Shard aware port was not exposed by the host
t.Log("the host does not expose a shard-aware port, skipping")
continue
}
// Verify that we have a sharded connPicker
shardedPicker, ok := hostPool.connPicker.(*scyllaConnPicker)
if !ok {
t.Errorf("not a sharded connection")
continue
}
numberOfShards := shardedPicker.nrShards
// Verify that there were no duplicate connections to the same shard
// Make sure that we didn't connect to the same shard twice
// There should be numberOfShards-1 connections to the new port
events := dialer.events[host.hostname]
shardAwareConnectionCount := 0
shardsConnected := make(map[int]testConnectionEvent)
for _, evt := range events {
if evt.destinationPort != shardAwarePort {
continue
}
shardAwareConnectionCount++
shard := scyllaShardForSourcePort(evt.sourcePort, numberOfShards)
if oldPort, hasShard := shardsConnected[shard]; hasShard {
t.Errorf("there was more than one connection to the shard aware port from the same shard (shard %d, port %d and %d)",
shard, oldPort, evt.sourcePort)
}
}
if shardAwareConnectionCount != numberOfShards-1 {
t.Errorf("expected %d connections to the shard aware port, but got %d", numberOfShards-1, shardAwareConnectionCount)
}
}
return
}()
}
wg.Wait()
}
func testShardAwarePortMaliciousNAT(t *testing.T, makeCluster makeClusterTestFunc) {
cluster := makeCluster()
cluster.PoolConfig.HostSelectionPolicy = TokenAwareHostPolicy(RoundRobinHostPolicy())
cluster.Dialer = &sourcePortOffByOneTestDialer{}
sess, err := cluster.CreateSession()
if err != nil {
t.Fatalf("an error occurred while creating a session: %s", err)
}
defer sess.Close()
// In this situation we are guaranteed that the connection will miss one
// shard at this point. The first connection receives a random shard,
// then we establish N-1 connections, targeting remaining shards.
// Because the malicious port translator shifts the port by one,
// one shard will be missed (if the host has more than one shard).
// Retry until we establish one connection per shard
for {
if err := waitUntilPoolsStopFilling(context.Background(), sess, 10*time.Second); err != nil {
t.Fatal(err)
}
if checkIfPoolsAreFull(sess) {
break
}
triggerPoolsRefill(sess)
}
}
func testShardAwarePortUnreachable(t *testing.T, makeCluster makeClusterTestFunc) {
cluster := makeCluster()
cluster.PoolConfig.HostSelectionPolicy = TokenAwareHostPolicy(RoundRobinHostPolicy())
cluster.Dialer = &allowOnlyNonShardAwarePortDialer{allowedPort: getClusterPort(cluster)}
sess, err := cluster.CreateSession()
if err != nil {
t.Fatalf("an error occurred while creating a session: %s", err)
}
defer sess.Close()
// In this situation, the connecting to the shard-aware port will fail,
// but connections to the non-shard-aware port will succeed. This test
// checks that we detect that the shard-aware-port is unreachable and
// we fall back to the old port.
// Retry until we establish one connection per shard
for {
if err := waitUntilPoolsStopFilling(context.Background(), sess, 10*time.Second); err != nil {
t.Fatal(err)
}
if checkIfPoolsAreFull(sess) {
break
}
triggerPoolsRefill(sess)
}
}
func testShardAwarePortUnusedIfNotEnabled(t *testing.T, makeCluster makeClusterTestFunc) {
dialer := newLoggingTestDialer()
cluster := makeCluster()
cluster.PoolConfig.HostSelectionPolicy = TokenAwareHostPolicy(RoundRobinHostPolicy())
// Explicitly disable the shard aware port
cluster.DisableShardAwarePort = true
cluster.Dialer = dialer
useTLS := cluster.SslOpts != nil
sess, err := cluster.CreateSession()
if err != nil {
t.Fatalf("an error occurred while creating a session: %s", err)
}
defer sess.Close()
if err := waitUntilPoolsStopFilling(context.Background(), sess, 10*time.Second); err != nil {
t.Fatal(err)
}
hosts := sess.ring.allHosts()
for _, host := range hosts {
t.Logf("checking host %s", host.hostname)
hostPool, _ := sess.pool.getPool(host)
shardAwarePort := getShardAwarePort(hostPool, useTLS)
if shardAwarePort == 0 {
// Shard aware port was not exposed by the host
t.Log("the host does not expose a shard-aware port, skipping")
continue
}
events, _ := dialer.events[host.hostname]
for _, evt := range events {
if evt.destinationPort == shardAwarePort {
t.Error("there was an attempt to connect to a shard aware port, but the configuration does not allow that")
}
}
}
}
func checkIfShardAwarePortIsExposed(pool *hostConnPool, useTLS bool) bool {
return getShardAwareAddress(pool, useTLS) != ""
}
func getShardAwarePort(pool *hostConnPool, useTLS bool) uint16 {
addr := getShardAwareAddress(pool, useTLS)
if addr == "" {
return 0
}
_, portS, _ := net.SplitHostPort(addr)
port, _ := strconv.Atoi(portS)
return uint16(port)
}
func getShardAwareAddress(pool *hostConnPool, useTLS bool) string {
picker, ok := pool.connPicker.(*scyllaConnPicker)
if !ok {
return ""
}
return picker.shardAwareAddress
}
func triggerPoolsRefill(sess *Session) {
hosts := sess.ring.allHosts()
for _, host := range hosts {
hostPool, _ := sess.pool.getPool(host)
go hostPool.fill()
}
}
func waitUntilPoolsStopFilling(ctx context.Context, sess *Session, timeout time.Duration) error {
deadline := time.After(timeout)
for !checkIfPoolsStoppedFilling(sess) {
select {
case <-ctx.Done():
return ctx.Err()
case <-deadline:
return fmt.Errorf("failed to fill all connection pools in %s", timeout)
case <-time.After(250 * time.Millisecond):
continue
}
}
return nil
}
func checkIfPoolsStoppedFilling(sess *Session) bool {
hosts := sess.ring.allHosts()
for _, host := range hosts {
hostPool, _ := sess.pool.getPool(host)
hostPool.mu.Lock()
isFilling := hostPool.filling
hostPool.mu.Unlock()
if isFilling {
return false
}
}
return true
}
func checkIfPoolsAreFull(sess *Session) bool {
hosts := sess.ring.allHosts()
for _, host := range hosts {
hostPool, _ := sess.pool.getPool(host)
hostPool.mu.Lock()
_, remaining := hostPool.connPicker.Size()
hostPool.mu.Unlock()
if remaining > 0 {
return false
}
}
return true
}
func getClusterPort(cluster *ClusterConfig) uint16 {
_, portStr, _ := net.SplitHostPort(cluster.Hosts[0])
port, _ := strconv.Atoi(portStr)
if port == 0 {
// Assume default if it's not explicitly specified
return 9042
}
return uint16(port)
}
type sourcePortOffByOneTestDialer struct {
ScyllaShardAwareDialer
}
func (spobo *sourcePortOffByOneTestDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
// Simulate a NAT that always increases the source port by 1
// This should always result in wrong shard being assigned if host
// has more than one shard.
sourcePort := ScyllaGetSourcePort(ctx)
if sourcePort > 0 {
sourcePort++
}
newCtx := context.WithValue(ctx, scyllaSourcePortCtx{}, sourcePort)
return spobo.ScyllaShardAwareDialer.DialContext(newCtx, network, addr)
}
type allowOnlyNonShardAwarePortDialer struct {
net.Dialer
allowedPort uint16
}
func (aonsa *allowOnlyNonShardAwarePortDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
// Simulate a network configuration which allows connections to the
// non-shard-aware port, but not to the shard-aware one.
_, targetPort, _ := net.SplitHostPort(addr)
if targetPort != strconv.Itoa(int(aonsa.allowedPort)) {
return nil, fmt.Errorf("allowOnlyNonShardAwarePortDialer: tried to connect to port %s, but only %d is allowed", targetPort, aonsa.allowedPort)
}
return aonsa.Dialer.DialContext(ctx, network, addr)
}
type loggingTestDialer struct {
ScyllaShardAwareDialer
events map[string][]testConnectionEvent
mu sync.Mutex
}
type testConnectionEvent struct {
sourcePort, destinationPort uint16
}
func newLoggingTestDialer() *loggingTestDialer {
return &loggingTestDialer{
events: make(map[string][]testConnectionEvent),
}
}
func (ltd *loggingTestDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
sourcePort := ScyllaGetSourcePort(ctx)
hostname, destinationPortStr, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
destinationPort, err := strconv.ParseUint(destinationPortStr, 10, 16)
if err != nil {
return nil, err
}
conn, err := ltd.ScyllaShardAwareDialer.DialContext(ctx, network, addr)
if err == nil {
ltd.mu.Lock()
defer ltd.mu.Unlock()
evt := testConnectionEvent{
sourcePort: sourcePort,
destinationPort: uint16(destinationPort),
}
ltd.events[hostname] = append(ltd.events[hostname], evt)
}
return conn, err
}