forked from mediocregopher/radix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_stub_test.go
402 lines (349 loc) · 10.6 KB
/
cluster_stub_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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package radix
import (
"errors"
"fmt"
"sort"
"strconv"
"strings"
"sync"
. "testing"
"github.com/groupme/radix/resp/resp2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type clusterSlotStub struct {
kv map[string]string
migrating, importing string // addr migrating to/importing from, if either
}
// clusterDatasetStub describes a dataset hosted by a clusterStub instance. This
// is separated out because different instances can host the same dataset
// (primary and secondaries)
type clusterDatasetStub struct {
sync.Mutex
slots map[uint16]clusterSlotStub
}
func (sd *clusterDatasetStub) slotRanges() [][2]uint16 {
slotIs := make([]uint16, 0, len(sd.slots))
for i := range sd.slots {
slotIs = append(slotIs, i)
}
sort.Slice(slotIs, func(i, j int) bool { return slotIs[i] < slotIs[j] })
ranges := make([][2]uint16, 0, 1)
for _, slot := range slotIs {
if len(ranges) == 0 {
ranges = append(ranges, [2]uint16{slot, slot + 1})
} else if lastRange := &(ranges[len(ranges)-1]); (*lastRange)[1] == slot {
(*lastRange)[1] = slot + 1
} else {
ranges = append(ranges, [2]uint16{slot, slot + 1})
}
}
return ranges
}
////////////////////////////////////////////////////////////////////////////////
// equivalent to a single redis instance
type clusterNodeStub struct {
addr, id string
secondaryOfAddr, secondaryOfID string // set if secondary
*clusterDatasetStub
*clusterStub
}
func (s *clusterNodeStub) withKey(key string, asking bool, fn func(clusterSlotStub) interface{}) interface{} {
s.clusterDatasetStub.Lock()
defer s.clusterDatasetStub.Unlock()
slotI := ClusterSlot([]byte(key))
slot, ok := s.clusterDatasetStub.slots[slotI]
if !ok {
movedStub := s.clusterStub.stubForSlot(slotI)
return resp2.Error{E: fmt.Errorf("MOVED %d %s", slotI, movedStub.addr)}
} else if _, ok := slot.kv[key]; !ok && slot.migrating != "" {
return resp2.Error{E: fmt.Errorf("ASK %d %s", slotI, slot.migrating)}
} else if slot.importing != "" && !asking {
return resp2.Error{E: fmt.Errorf("MOVED %d %s", slotI, slot.importing)}
}
return fn(slot)
}
func (s *clusterNodeStub) newConn() Conn {
asking := false // flag we hold onto in between commands
return Stub("tcp", s.addr, func(args []string) interface{} {
cmd := strings.ToUpper(args[0])
// If the cmd is not ASKING we need to unset the flag at the _end_ of
// this command, if it's set
if cmd != "ASKING" {
defer func() {
asking = false
}()
}
switch cmd {
case "GET":
k := args[1]
return s.withKey(k, asking, func(slot clusterSlotStub) interface{} {
s, ok := slot.kv[k]
if !ok {
return nil
}
return s
})
case "SET":
k := args[1]
return s.withKey(k, asking, func(slot clusterSlotStub) interface{} {
slot.kv[k] = args[2]
return resp2.SimpleString{S: "OK"}
})
case "EVALSHA":
return resp2.Error{E: errors.New("NOSCRIPT: clusterNodeStub does not support EVALSHA")}
case "EVAL":
// see if any keys were sent
if len(args) < 3 {
return resp2.Error{E: errors.New("malformed EVAL command")}
}
numKeys, err := strconv.Atoi(args[2])
if err != nil {
return resp2.Error{E: err}
} else if numKeys == 0 {
return "EVAL: no keys"
}
return s.withKey(args[3], asking, func(slot clusterSlotStub) interface{} {
return "EVAL: success!"
})
case "PING":
return resp2.SimpleString{S: "PONG"}
case "CLUSTER":
switch strings.ToUpper(args[1]) {
case "SLOTS":
return s.clusterStub.topo()
}
case "ASKING":
asking = true
return resp2.SimpleString{S: "OK"}
case "ADDR":
return s.addr
case "SCAN":
if cur := args[1]; cur == "0" {
var keys []string
s.clusterDatasetStub.Lock()
for _, slot := range s.clusterDatasetStub.slots {
for key := range slot.kv {
keys = append(keys, key)
}
}
s.clusterDatasetStub.Unlock()
return []interface{}{"1", keys}
}
return []interface{}{"0", []string{}}
}
return resp2.Error{E: fmt.Errorf("unknown command %#v", args)}
})
}
func (s *clusterNodeStub) Close() error {
*s = clusterNodeStub{}
return nil
}
////////////////////////////////////////////////////////////////////////////////
type clusterStub struct {
stubs map[string]*clusterNodeStub // addr -> stub
}
func newStubCluster(tt ClusterTopo) *clusterStub {
// map of addrs to dataset
m := map[string]*clusterDatasetStub{}
sc := &clusterStub{
stubs: make(map[string]*clusterNodeStub, len(tt)),
}
for _, t := range tt {
addr := t.Addr
if t.SecondaryOfAddr != "" {
addr = t.SecondaryOfAddr
}
sd, ok := m[addr]
if !ok {
sd = &clusterDatasetStub{slots: map[uint16]clusterSlotStub{}}
for _, slots := range t.Slots {
for i := slots[0]; i < slots[1]; i++ {
sd.slots[i] = clusterSlotStub{kv: map[string]string{}}
}
}
m[addr] = sd
}
sc.stubs[t.Addr] = &clusterNodeStub{
addr: t.Addr,
id: t.ID,
secondaryOfAddr: t.SecondaryOfAddr,
secondaryOfID: t.SecondaryOfID,
clusterDatasetStub: sd,
clusterStub: sc,
}
}
return sc
}
func (scl *clusterStub) stubForSlot(slot uint16) *clusterNodeStub {
for _, s := range scl.stubs {
if slot, ok := s.clusterDatasetStub.slots[slot]; ok && s.secondaryOfAddr == "" && slot.importing == "" {
return s
}
}
panic(fmt.Sprintf("couldn't find stub for slot %d", slot))
}
func (scl *clusterStub) topo() ClusterTopo {
var tt ClusterTopo
for _, s := range scl.stubs {
slotRanges := s.clusterDatasetStub.slotRanges()
if len(slotRanges) == 0 {
continue
}
tt = append(tt, ClusterNode{
Addr: s.addr,
ID: s.id,
Slots: slotRanges,
SecondaryOfAddr: s.secondaryOfAddr,
SecondaryOfID: s.secondaryOfID,
})
}
tt.sort()
return tt
}
func (scl *clusterStub) clientFunc() ClientFunc {
return func(network, addr string) (Client, error) {
for _, s := range scl.stubs {
if s.addr == addr {
return s.newConn(), nil
}
}
return nil, fmt.Errorf("unknown addr: %q", addr)
}
}
func (scl *clusterStub) addrs() []string {
var res []string
for _, s := range scl.stubs {
res = append(res, s.addr)
}
return res
}
func (scl *clusterStub) newCluster() *Cluster {
c, err := NewCluster(scl.addrs(), ClusterPoolFunc(scl.clientFunc()))
if err != nil {
panic(err)
}
return c
}
func (scl *clusterStub) randStub() *clusterNodeStub {
for _, s := range scl.stubs {
if s.secondaryOfAddr != "" {
return s
}
}
panic("cluster is empty?")
}
// Migration steps:
// * Mark slot as migrating on src and importing on dst
// * Move each key individually
// * Mark slots as migrated, note slot change in datasets
//
// At any point inside those steps we need to be able to run a test
func (scl *clusterStub) migrateInit(dstAddr string, slot uint16) {
src := scl.stubForSlot(slot)
src.clusterDatasetStub.Lock()
defer src.clusterDatasetStub.Unlock()
dst := scl.stubs[dstAddr]
dst.clusterDatasetStub.Lock()
defer dst.clusterDatasetStub.Unlock()
srcSlot := src.clusterDatasetStub.slots[slot]
srcSlot.migrating = dst.addr
src.clusterDatasetStub.slots[slot] = srcSlot
dst.clusterDatasetStub.slots[slot] = clusterSlotStub{
kv: map[string]string{},
importing: src.addr,
}
}
// migrateInit must have been called on the slot this key belongs to
func (scl *clusterStub) migrateKey(key string) {
slot := ClusterSlot([]byte(key))
src := scl.stubForSlot(slot)
src.clusterDatasetStub.Lock()
defer src.clusterDatasetStub.Unlock()
srcSlot := src.clusterDatasetStub.slots[slot]
dst := scl.stubs[srcSlot.migrating]
dst.clusterDatasetStub.Lock()
defer dst.clusterDatasetStub.Unlock()
dst.clusterDatasetStub.slots[slot].kv[key] = srcSlot.kv[key]
delete(srcSlot.kv, key)
}
// migrateInit must have been called on the slot already
func (scl *clusterStub) migrateAllKeys(slot uint16) {
src := scl.stubForSlot(slot)
src.clusterDatasetStub.Lock()
defer src.clusterDatasetStub.Unlock()
srcSlot := src.clusterDatasetStub.slots[slot]
dst := scl.stubs[srcSlot.migrating]
dst.clusterDatasetStub.Lock()
defer dst.clusterDatasetStub.Unlock()
for k, v := range srcSlot.kv {
dst.clusterDatasetStub.slots[slot].kv[k] = v
delete(srcSlot.kv, k)
}
}
// all keys must have been migrated to call this, probably via migrateAllKeys
func (scl *clusterStub) migrateDone(slot uint16) {
src := scl.stubForSlot(slot)
src.clusterDatasetStub.Lock()
defer src.clusterDatasetStub.Unlock()
srcSlot := src.clusterDatasetStub.slots[slot]
dst := scl.stubs[srcSlot.migrating]
dst.clusterDatasetStub.Lock()
defer dst.clusterDatasetStub.Unlock()
delete(src.clusterDatasetStub.slots, slot)
dstSlot := dst.clusterDatasetStub.slots[slot]
dstSlot.importing = ""
dst.clusterDatasetStub.slots[slot] = dstSlot
}
func (scl *clusterStub) migrateSlotRange(dstAddr string, start, end uint16) {
for slot := start; slot < end; slot++ {
scl.migrateInit(dstAddr, slot)
scl.migrateAllKeys(slot)
scl.migrateDone(slot)
}
}
// Who watches the watchmen?
func TestClusterStub(t *T) {
scl := newStubCluster(testTopo)
var outTT ClusterTopo
err := scl.randStub().newConn().Do(Cmd(&outTT, "CLUSTER", "SLOTS"))
require.Nil(t, err)
assert.Equal(t, testTopo, outTT)
// make sure that moving slots works, start by marking the slot 0 as
// migrating to another addr (dst). We choose dst as the node which holds
// some arbitrary high number slot
src := scl.stubForSlot(0)
srcConn := src.newConn()
key := clusterSlotKeys[0]
require.Nil(t, srcConn.Do(Cmd(nil, "SET", key, "foo")))
dst := scl.stubForSlot(10000)
dstConn := dst.newConn()
scl.migrateInit(dst.addr, 0)
// getting a key from that slot from the original should still work
var val string
require.Nil(t, srcConn.Do(Cmd(&val, "GET", key)))
assert.Equal(t, "foo", val)
// getting on the new dst should give MOVED
err = dstConn.Do(Cmd(nil, "GET", key))
assert.Equal(t, "MOVED 0 "+src.addr, err.Error())
// actually migrate that key ...
scl.migrateKey(key)
// ... then doing the GET on the src should give an ASK error ...
err = srcConn.Do(Cmd(nil, "GET", key))
assert.Equal(t, "ASK 0 "+dst.addr, err.Error())
// ... doing the GET on the dst _without_ asking should give MOVED again ...
err = dstConn.Do(Cmd(nil, "GET", key))
assert.Equal(t, "MOVED 0 "+src.addr, err.Error())
// ... but doing it with ASKING on dst should work
require.Nil(t, dstConn.Do(Cmd(nil, "ASKING")))
require.Nil(t, dstConn.Do(Cmd(nil, "GET", key)))
assert.Equal(t, "foo", val)
// finish the migration, then src should always MOVED, dst should always
// work
scl.migrateAllKeys(0)
scl.migrateDone(0)
err = srcConn.Do(Cmd(nil, "GET", key))
assert.Equal(t, "MOVED 0 "+dst.addr, err.Error())
require.Nil(t, dstConn.Do(Cmd(nil, "GET", key)))
assert.Equal(t, "foo", val)
}