-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathchunking_test.go
235 lines (200 loc) · 5.34 KB
/
chunking_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
package raft
import (
"bytes"
"context"
fmt "fmt"
"os"
"testing"
proto "github.com/golang/protobuf/proto"
"github.com/hashicorp/go-raftchunking"
raftchunkingtypes "github.com/hashicorp/go-raftchunking/types"
uuid "github.com/hashicorp/go-uuid"
"github.com/hashicorp/raft"
"github.com/hashicorp/raft-boltdb/v2"
"github.com/hashicorp/vault/sdk/physical"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// This chunks encoded data and then performing out-of-order applies of half
// the logs. It then snapshots, restores to a new FSM, and applies the rest.
// The goal is to verify that chunking snapshotting works as expected.
func TestRaft_Chunking_Lifecycle(t *testing.T) {
t.Parallel()
require := require.New(t)
assert := assert.New(t)
b, dir := getRaft(t, true, false)
defer os.RemoveAll(dir)
t.Log("applying configuration")
b.applyConfigSettings(raft.DefaultConfig())
t.Log("chunking")
buf := []byte("let's see how this goes, shall we?")
logData := &LogData{
Operations: []*LogOperation{
{
OpType: putOp,
Key: "foobar",
Value: buf,
},
},
}
cmdBytes, err := proto.Marshal(logData)
require.NoError(err)
var logs []*raft.Log
for i, b := range cmdBytes {
// Stage multiple operations so we can test restoring across multiple opnums
for j := 0; j < 10; j++ {
chunkInfo := &raftchunkingtypes.ChunkInfo{
OpNum: uint64(32 + j),
SequenceNum: uint32(i),
NumChunks: uint32(len(cmdBytes)),
}
chunkBytes, err := proto.Marshal(chunkInfo)
require.NoError(err)
logs = append(logs, &raft.Log{
Data: []byte{b},
Extensions: chunkBytes,
})
}
}
t.Log("applying half of the logs")
// The reason for the skipping is to test out-of-order applies which are
// theoretically possible. Some of these will actually finish though!
for i := 0; i < len(logs); i += 2 {
resp := b.fsm.chunker.Apply(logs[i])
if resp != nil {
_, ok := resp.(raftchunking.ChunkingSuccess)
assert.True(ok)
}
}
t.Log("tearing down cluster")
require.NoError(b.TeardownCluster(nil))
require.NoError(b.fsm.getDB().Close())
require.NoError(b.stableStore.(*raftboltdb.BoltStore).Close())
t.Log("starting new backend")
backendRaw, err := NewRaftBackend(b.conf, b.logger)
require.NoError(err)
b = backendRaw.(*RaftBackend)
t.Log("applying rest of the logs")
// Apply the rest of the logs
var resp interface{}
for i := 1; i < len(logs); i += 2 {
resp = b.fsm.chunker.Apply(logs[i])
if resp != nil {
_, ok := resp.(raftchunking.ChunkingSuccess)
assert.True(ok)
}
}
assert.NotNil(resp)
_, ok := resp.(raftchunking.ChunkingSuccess)
assert.True(ok)
}
func TestFSM_Chunking_TermChange(t *testing.T) {
t.Parallel()
require := require.New(t)
assert := assert.New(t)
b, dir := getRaft(t, true, false)
defer os.RemoveAll(dir)
t.Log("applying configuration")
b.applyConfigSettings(raft.DefaultConfig())
t.Log("chunking")
buf := []byte("let's see how this goes, shall we?")
logData := &LogData{
Operations: []*LogOperation{
{
OpType: putOp,
Key: "foobar",
Value: buf,
},
},
}
cmdBytes, err := proto.Marshal(logData)
require.NoError(err)
// Only need two chunks to test this
chunks := [][]byte{
cmdBytes[0:2],
cmdBytes[2:],
}
var logs []*raft.Log
for i, b := range chunks {
chunkInfo := &raftchunkingtypes.ChunkInfo{
OpNum: uint64(32),
SequenceNum: uint32(i),
NumChunks: uint32(len(chunks)),
}
chunkBytes, err := proto.Marshal(chunkInfo)
if err != nil {
t.Fatal(err)
}
logs = append(logs, &raft.Log{
Term: uint64(i),
Data: b,
Extensions: chunkBytes,
})
}
// We should see nil for both
for _, log := range logs {
resp := b.fsm.chunker.Apply(log)
assert.Nil(resp)
}
// Now verify the other baseline, that when the term doesn't change we see
// non-nil. First make the chunker have a clean state, then set the terms
// to be the same.
b.fsm.chunker.RestoreState(nil)
logs[1].Term = uint64(0)
// We should see nil only for the first one
for i, log := range logs {
resp := b.fsm.chunker.Apply(log)
if i == 0 {
assert.Nil(resp)
}
if i == 1 {
assert.NotNil(resp)
_, ok := resp.(raftchunking.ChunkingSuccess)
assert.True(ok)
}
}
}
func TestRaft_Chunking_AppliedIndex(t *testing.T) {
t.Parallel()
raft, dir := getRaft(t, true, false)
defer os.RemoveAll(dir)
// Lower the size for tests
raftchunking.ChunkSize = 1024
val, err := uuid.GenerateRandomBytes(3 * raftchunking.ChunkSize)
if err != nil {
t.Fatal(err)
}
// Write a value to fastforward the index
err = raft.Put(context.Background(), &physical.Entry{
Key: "key",
Value: []byte("test"),
})
if err != nil {
t.Fatal(err)
}
currentIndex := raft.AppliedIndex()
// Write some data
for i := 0; i < 10; i++ {
err := raft.Put(context.Background(), &physical.Entry{
Key: fmt.Sprintf("key-%d", i),
Value: val,
})
if err != nil {
t.Fatal(err)
}
}
newIndex := raft.AppliedIndex()
// Each put should generate 4 chunks
if newIndex-currentIndex != 10*4 {
t.Fatalf("Did not apply chunks as expected, applied index = %d - %d = %d", newIndex, currentIndex, newIndex-currentIndex)
}
for i := 0; i < 10; i++ {
entry, err := raft.Get(context.Background(), fmt.Sprintf("key-%d", i))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(entry.Value, val) {
t.Fatal("value is corrupt")
}
}
}