This repository has been archived by the owner on Nov 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathfinn_test.go
448 lines (420 loc) · 11.9 KB
/
finn_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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package finn
import (
"bufio"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/tidwall/raft-redcon"
"github.com/tidwall/redcon"
)
type KVM struct {
mu sync.RWMutex
keys map[string][]byte
}
func NewKVM() *KVM {
return &KVM{
keys: make(map[string][]byte),
}
}
func (kvm *KVM) Command(m Applier, conn redcon.Conn, cmd redcon.Command) (interface{}, error) {
switch strings.ToLower(string(cmd.Args[0])) {
default:
return nil, ErrUnknownCommand
case "set":
if len(cmd.Args) != 3 {
return nil, ErrWrongNumberOfArguments
}
return m.Apply(conn, cmd,
func() (interface{}, error) {
kvm.mu.Lock()
defer kvm.mu.Unlock()
kvm.keys[string(cmd.Args[1])] = cmd.Args[2]
return nil, nil
},
func(v interface{}) (interface{}, error) {
conn.WriteString("OK")
return nil, nil
},
)
case "get":
if len(cmd.Args) != 2 {
return nil, ErrWrongNumberOfArguments
}
return m.Apply(conn, cmd,
nil,
func(interface{}) (interface{}, error) {
kvm.mu.RLock()
defer kvm.mu.RUnlock()
if val, ok := kvm.keys[string(cmd.Args[1])]; !ok {
conn.WriteNull()
} else {
conn.WriteBulk(val)
}
return nil, nil
},
)
}
}
func (kvm *KVM) Restore(rd io.Reader) error {
kvm.mu.Lock()
defer kvm.mu.Unlock()
data, err := ioutil.ReadAll(rd)
if err != nil {
return err
}
var keys map[string][]byte
if err := json.Unmarshal(data, &keys); err != nil {
return err
}
kvm.keys = keys
return nil
}
func (kvm *KVM) Snapshot(wr io.Writer) error {
kvm.mu.RLock()
defer kvm.mu.RUnlock()
data, err := json.Marshal(kvm.keys)
if err != nil {
return err
}
if _, err := wr.Write(data); err != nil {
return err
}
return nil
}
var killed = make(map[int]bool)
var killCond = sync.NewCond(&sync.Mutex{})
func killNodes(basePort int) {
killCond.L.Lock()
killed[basePort] = true
killCond.Broadcast()
killCond.L.Unlock()
}
func startTestNode(t testing.TB, basePort int, num int, opts *Options) {
node := fmt.Sprintf("%d", num)
if err := os.MkdirAll("data/"+node, 0700); err != nil {
t.Fatal(err)
}
join := ""
if node == "" {
node = "0"
}
addr := fmt.Sprintf(":%d", basePort/10*10+num)
if node != "0" {
join = fmt.Sprintf(":%d", basePort)
}
n, err := Open("data/"+node, addr, join, NewKVM(), opts)
if err != nil {
t.Fatal(err)
}
defer n.Close()
for {
killCond.L.Lock()
if killed[basePort] {
killCond.L.Unlock()
return
}
killCond.Wait()
killCond.L.Unlock()
}
}
func waitFor(t testing.TB, basePort, node int) {
target := fmt.Sprintf(":%d", basePort/10*10+node)
start := time.Now()
for {
if time.Now().Sub(start) > time.Second*10 {
t.Fatal("timeout looking for leader")
}
time.Sleep(time.Second / 4)
resp, _, err := raftredcon.Do(target, nil, []byte("raftleader"))
if err != nil {
continue
}
if len(resp) != 0 {
return
}
}
}
func testDo(t testing.TB, basePort, node int, expect string, args ...string) string {
var bargs [][]byte
for _, arg := range args {
bargs = append(bargs, []byte(arg))
}
target := fmt.Sprintf(":%d", basePort/10*10+node)
resp, _, err := raftredcon.Do(target, nil, bargs...)
if err != nil {
if err.Error() == expect {
return ""
}
t.Fatalf("node %d: %v", node, err)
}
if expect != "???" && string(resp) != expect {
t.Fatalf("node %d: expected '%v', got '%v'", node, expect, string(resp))
}
return string(resp)
}
func TestVarious(t *testing.T) {
t.Run("Level", SubTestLevel)
t.Run("Backend", SubTestBackend)
}
func SubTestLevel(t *testing.T) {
var level Level
level = Level(-99)
if level.String() != "unknown" {
t.Fatalf("expecting '%v', got '%v'", "unknown", level.String())
}
level = Low
if level.String() != "low" {
t.Fatalf("expecting '%v', got '%v'", "low", level.String())
}
level = Medium
if level.String() != "medium" {
t.Fatalf("expecting '%v', got '%v'", "medium", level.String())
}
level = High
if level.String() != "high" {
t.Fatalf("expecting '%v', got '%v'", "high", level.String())
}
}
func SubTestBackend(t *testing.T) {
var backend Backend
backend = Backend(-99)
if backend.String() != "unknown" {
t.Fatalf("expecting '%v', got '%v'", "unknown", backend.String())
}
backend = FastLog
if backend.String() != "fastlog" {
t.Fatalf("expecting '%v', got '%v'", "fastlog", backend.String())
}
backend = Bolt
if backend.String() != "bolt" {
t.Fatalf("expecting '%v', got '%v'", "bolt", backend.String())
}
backend = LevelDB
if backend.String() != "leveldb" {
t.Fatalf("expecting '%v', got '%v'", "leveldb", backend.String())
}
backend = InMem
if backend.String() != "inmem" {
t.Fatalf("expecting '%v', got '%v'", "inmem", backend.String())
}
}
func TestCluster(t *testing.T) {
var optsArr []Options
for _, backend := range []Backend{LevelDB, Bolt, FastLog, InMem} {
for _, consistency := range []Level{Low, Medium, High} {
optsArr = append(optsArr, Options{
Backend: backend,
Consistency: consistency,
})
}
}
for i := 0; i < len(optsArr); i++ {
func() {
opts := optsArr[i]
if os.Getenv("LOG") != "1" {
opts.LogOutput = ioutil.Discard
}
basePort := (7480/10 + i) * 10
tag := fmt.Sprintf("%v-%v-%d", opts.Backend, opts.Consistency, basePort)
t.Logf("%s", tag)
t.Run(tag, func(t *testing.T) {
os.RemoveAll("data")
defer os.RemoveAll("data")
defer killNodes(basePort)
for i := 0; i < 3; i++ {
go startTestNode(t, basePort, i, &opts)
waitFor(t, basePort, i)
}
t.Run("Leader", func(t *testing.T) { SubTestLeader(t, basePort, &opts) })
t.Run("Set", func(t *testing.T) { SubTestSet(t, basePort, &opts) })
t.Run("Get", func(t *testing.T) { SubTestGet(t, basePort, &opts) })
t.Run("Snapshot", func(t *testing.T) { SubTestSnapshot(t, basePort, &opts) })
t.Run("Ping", func(t *testing.T) { SubTestPing(t, basePort, &opts) })
t.Run("RaftShrinkLog", func(t *testing.T) { SubTestRaftShrinkLog(t, basePort, &opts) })
t.Run("RaftStats", func(t *testing.T) { SubTestRaftStats(t, basePort, &opts) })
t.Run("RaftState", func(t *testing.T) { SubTestRaftState(t, basePort, &opts) })
t.Run("AddPeer", func(t *testing.T) { SubTestAddPeer(t, basePort, &opts) })
t.Run("RemovePeer", func(t *testing.T) { SubTestRemovePeer(t, basePort, &opts) })
})
}()
}
}
func SubTestLeader(t *testing.T, basePort int, opts *Options) {
baseAddr := fmt.Sprintf(":%d", basePort)
testDo(t, basePort, 0, baseAddr, "raftleader")
testDo(t, basePort, 1, baseAddr, "raftleader")
testDo(t, basePort, 2, baseAddr, "raftleader")
}
func SubTestSet(t *testing.T, basePort int, opts *Options) {
baseAddr := fmt.Sprintf(":%d", basePort)
testDo(t, basePort, 0, "OK", "set", "hello", "world")
testDo(t, basePort, 1, "TRY "+baseAddr, "set", "hello", "world")
testDo(t, basePort, 2, "TRY "+baseAddr, "set", "hello", "world")
}
func SubTestGet(t *testing.T, basePort int, opts *Options) {
baseAddr := fmt.Sprintf(":%d", basePort)
testDo(t, basePort, 0, "world", "get", "hello")
testDo(t, basePort, 1, "TRY "+baseAddr, "set", "hello", "world")
testDo(t, basePort, 2, "TRY "+baseAddr, "set", "hello", "world")
}
func SubTestPing(t *testing.T, basePort int, opts *Options) {
for i := 0; i < 3; i++ {
testDo(t, basePort, i, "PONG", "ping")
testDo(t, basePort, i, "HELLO", "ping", "HELLO")
testDo(t, basePort, i, "ERR wrong number of arguments for 'ping' command", "ping", "HELLO", "WORLD")
}
}
func SubTestRaftShrinkLog(t *testing.T, basePort int, opts *Options) {
for i := 0; i < 3; i++ {
if opts.Backend == Bolt || opts.Backend == LevelDB {
testDo(t, basePort, i, "ERR log is not shrinkable", "raftshrinklog")
} else {
testDo(t, basePort, i, "OK", "raftshrinklog")
}
testDo(t, basePort, i, "ERR wrong number of arguments for 'raftshrinklog' command", "raftshrinklog", "abc")
}
}
func SubTestRaftStats(t *testing.T, basePort int, opts *Options) {
for i := 0; i < 3; i++ {
resp := testDo(t, basePort, i, "???", "raftstats")
if !strings.Contains(resp, "applied_index") || !strings.Contains(resp, "num_peers") {
t.Fatal("expected values")
}
testDo(t, basePort, i, "ERR wrong number of arguments for 'raftstats' command", "raftstats", "abc")
}
}
func SubTestRaftState(t *testing.T, basePort int, opts *Options) {
for i := 0; i < 3; i++ {
if i == 0 {
testDo(t, basePort, i, "Leader", "raftstate")
} else {
testDo(t, basePort, i, "Follower", "raftstate")
}
testDo(t, basePort, i, "ERR wrong number of arguments for 'raftstate' command", "raftstate", "abc")
}
}
func SubTestSnapshot(t *testing.T, basePort int, opts *Options) {
// insert 1000 items
for i := 0; i < 1000; i++ {
testDo(t, basePort, 0, "OK", "set", fmt.Sprintf("key:%d", i), fmt.Sprintf("val:%d", i))
}
testDo(t, basePort, 0, "OK", "raftsnapshot")
testDo(t, basePort, 1, "OK", "raftsnapshot")
testDo(t, basePort, 2, "OK", "raftsnapshot")
}
func SubTestAddPeer(t *testing.T, basePort int, opts *Options) {
baseAddr := fmt.Sprintf(":%d", basePort)
go startTestNode(t, basePort, 3, opts)
waitFor(t, basePort, 3)
testDo(t, basePort, 3, baseAddr, "raftleader")
testDo(t, basePort, 3, "TRY "+baseAddr, "set", "hello", "world")
testDo(t, basePort, 3, "OK", "raftsnapshot")
}
func SubTestRemovePeer(t *testing.T, basePort int, opts *Options) {
baseAddr := fmt.Sprintf(":%d", basePort)
testDo(t, basePort, 1, "TRY "+baseAddr, "raftremovepeer", fmt.Sprintf(":%d3", basePort/10))
testDo(t, basePort, 0, "OK", "raftremovepeer", fmt.Sprintf(":%d3", basePort/10))
testDo(t, basePort, 0, "peer is unknown", "raftremovepeer", fmt.Sprintf(":%d3", basePort/10))
}
func BenchmarkCluster(t *testing.B) {
os.RemoveAll("data")
defer os.RemoveAll("data")
for i := 0; i < 3; i++ {
go startTestNode(t, 7480, i, &Options{LogOutput: ioutil.Discard})
waitFor(t, 7480, i)
}
t.Run("PL", func(t *testing.B) {
pl := []int{1, 4, 16, 64}
for i := 0; i < len(pl); i++ {
func(pl int) {
t.Run(fmt.Sprintf("%d", pl), func(t *testing.B) {
t.Run("Ping", func(t *testing.B) { SubBenchmarkPing(t, pl) })
t.Run("Set", func(t *testing.B) { SubBenchmarkSet(t, pl) })
t.Run("Get", func(t *testing.B) { SubBenchmarkGet(t, pl) })
})
}(pl[i])
}
})
}
func testDial(t testing.TB, node int) (net.Conn, *bufio.ReadWriter) {
conn, err := net.Dial("tcp", fmt.Sprintf(":748%d", node))
if err != nil {
t.Fatal(err)
}
return conn, bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
}
func buildCommand(args ...string) []byte {
var buf []byte
buf = append(buf, '*')
buf = append(buf, strconv.FormatInt(int64(len(args)), 10)...)
buf = append(buf, '\r', '\n')
for _, arg := range args {
buf = append(buf, '$')
buf = append(buf, strconv.FormatInt(int64(len(arg)), 10)...)
buf = append(buf, '\r', '\n')
buf = append(buf, arg...)
buf = append(buf, '\r', '\n')
}
return buf
}
func testConnDo(t testing.TB, rw *bufio.ReadWriter, pl int, expect string, cmd []byte) {
for i := 0; i < pl; i++ {
rw.Write(cmd)
}
if err := rw.Flush(); err != nil {
t.Fatal(err)
}
buf := make([]byte, len(expect))
for i := 0; i < pl; i++ {
if _, err := io.ReadFull(rw, buf); err != nil {
t.Fatal(err)
}
if string(buf) != expect {
t.Fatalf("expected '%v', got '%v'", expect, string(buf))
}
}
}
func SubBenchmarkPing(t *testing.B, pipeline int) {
conn, rw := testDial(t, 0)
defer conn.Close()
t.ResetTimer()
for i := 0; i < t.N; i += pipeline {
n := pipeline
if t.N-i < pipeline {
n = t.N - i
}
testConnDo(t, rw, n, "+PONG\r\n", []byte("*1\r\n$4\r\nPING\r\n"))
}
}
func SubBenchmarkSet(t *testing.B, pipeline int) {
conn, rw := testDial(t, 0)
defer conn.Close()
t.ResetTimer()
for i := 0; i < t.N; i += pipeline {
n := pipeline
if t.N-i < pipeline {
n = t.N - i
}
testConnDo(t, rw, n, "+OK\r\n", buildCommand("set", fmt.Sprintf("key:%d", i), fmt.Sprintf("val:%d", i)))
}
}
func SubBenchmarkGet(t *testing.B, pipeline int) {
conn, rw := testDial(t, 0)
defer conn.Close()
t.ResetTimer()
for i := 0; i < t.N; i += pipeline {
n := pipeline
if t.N-i < pipeline {
n = t.N - i
}
testConnDo(t, rw, n, "$-1\r\n", buildCommand("get", "key:na"))
}
}