forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
topology_test.go
205 lines (180 loc) · 4.84 KB
/
topology_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
package gocql
import (
"fmt"
"sort"
"testing"
)
func TestPlacementStrategy_SimpleStrategy(t *testing.T) {
host0 := &HostInfo{hostId: "0"}
host25 := &HostInfo{hostId: "25"}
host50 := &HostInfo{hostId: "50"}
host75 := &HostInfo{hostId: "75"}
tokens := []hostToken{
{intToken(0), host0},
{intToken(25), host25},
{intToken(50), host50},
{intToken(75), host75},
}
hosts := []*HostInfo{host0, host25, host50, host75}
strat := &simpleStrategy{rf: 2}
tokenReplicas := strat.replicaMap(&tokenRing{hosts: hosts, tokens: tokens})
if len(tokenReplicas) != len(tokens) {
t.Fatalf("expected replica map to have %d items but has %d", len(tokens), len(tokenReplicas))
}
for _, replicas := range tokenReplicas {
if len(replicas.hosts) != strat.rf {
t.Errorf("expected to have %d replicas got %d for token=%v", strat.rf, len(replicas.hosts), replicas.token)
}
}
for i, token := range tokens {
ht := tokenReplicas.replicasFor(token.token)
if ht.token != token.token {
t.Errorf("token %v not in replica map: %v", token, ht.hosts)
}
for j, replica := range ht.hosts {
exp := tokens[(i+j)%len(tokens)].host
if exp != replica {
t.Errorf("expected host %v to be a replica of %v got %v", exp.hostId, token, replica.hostId)
}
}
}
}
func TestPlacementStrategy_NetworkStrategy(t *testing.T) {
const (
totalDCs = 3
racksPerDC = 3
hostsPerDC = 5
)
tests := []struct {
name string
strat *networkTopology
expectedReplicaMapSize int
}{
{
name: "full",
strat: &networkTopology{
dcs: map[string]int{
"dc1": 1,
"dc2": 2,
"dc3": 3,
},
},
expectedReplicaMapSize: hostsPerDC * totalDCs,
},
{
name: "missing",
strat: &networkTopology{
dcs: map[string]int{
"dc2": 2,
"dc3": 3,
},
},
expectedReplicaMapSize: hostsPerDC * 2,
},
{
name: "zero",
strat: &networkTopology{
dcs: map[string]int{
"dc1": 0,
"dc2": 2,
"dc3": 3,
},
},
expectedReplicaMapSize: hostsPerDC * 2,
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
var (
hosts []*HostInfo
tokens []hostToken
)
dcRing := make(map[string][]hostToken, totalDCs)
for i := 0; i < totalDCs; i++ {
var dcTokens []hostToken
dc := fmt.Sprintf("dc%d", i+1)
for j := 0; j < hostsPerDC; j++ {
rack := fmt.Sprintf("rack%d", (j%racksPerDC)+1)
h := &HostInfo{hostId: fmt.Sprintf("%s:%s:%d", dc, rack, j), dataCenter: dc, rack: rack}
token := hostToken{
token: orderedToken([]byte(h.hostId)),
host: h,
}
tokens = append(tokens, token)
dcTokens = append(dcTokens, token)
hosts = append(hosts, h)
}
sort.Sort(&tokenRing{tokens: dcTokens})
dcRing[dc] = dcTokens
}
if len(tokens) != hostsPerDC*totalDCs {
t.Fatalf("expected %d tokens in the ring got %d", hostsPerDC*totalDCs, len(tokens))
}
sort.Sort(&tokenRing{tokens: tokens})
var expReplicas int
for _, rf := range test.strat.dcs {
expReplicas += rf
}
tokenReplicas := test.strat.replicaMap(&tokenRing{hosts: hosts, tokens: tokens})
if len(tokenReplicas) != test.expectedReplicaMapSize {
t.Fatalf("expected replica map to have %d items but has %d", test.expectedReplicaMapSize,
len(tokenReplicas))
}
if !sort.IsSorted(tokenReplicas) {
t.Fatal("replica map was not sorted by token")
}
for token, replicas := range tokenReplicas {
if len(replicas.hosts) != expReplicas {
t.Fatalf("expected to have %d replicas got %d for token=%v", expReplicas, len(replicas.hosts), token)
}
}
for dc, rf := range test.strat.dcs {
if rf == 0 {
continue
}
dcTokens := dcRing[dc]
for i, th := range dcTokens {
token := th.token
allReplicas := tokenReplicas.replicasFor(token)
if allReplicas.token != token {
t.Fatalf("token %v not in replica map", token)
}
var replicas []*HostInfo
for _, replica := range allReplicas.hosts {
if replica.dataCenter == dc {
replicas = append(replicas, replica)
}
}
if len(replicas) != rf {
t.Fatalf("expected %d replicas in dc %q got %d", rf, dc, len(replicas))
}
var lastRack string
for j, replica := range replicas {
// expected is in the next rack
var exp *HostInfo
if lastRack == "" {
// primary, first replica
exp = dcTokens[(i+j)%len(dcTokens)].host
} else {
for k := 0; k < len(dcTokens); k++ {
// walk around the ring from i + j to find the next host the
// next rack
p := (i + j + k) % len(dcTokens)
h := dcTokens[p].host
if h.rack != lastRack {
exp = h
break
}
}
if exp.rack == lastRack {
panic("no more racks")
}
}
lastRack = replica.rack
}
}
}
})
}
}