-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_test.go
365 lines (338 loc) · 9.11 KB
/
node_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
package paxos
import (
"fmt"
"math/rand"
"os"
"path/filepath"
"sync"
"testing"
"time"
"github.com/LiuzhouChan/go-paxos/config"
"github.com/LiuzhouChan/go-paxos/internal/logdb"
ipaxos "github.com/LiuzhouChan/go-paxos/internal/paxos"
"github.com/LiuzhouChan/go-paxos/internal/rsm"
"github.com/LiuzhouChan/go-paxos/internal/server"
"github.com/LiuzhouChan/go-paxos/internal/settings"
"github.com/LiuzhouChan/go-paxos/internal/tests"
"github.com/LiuzhouChan/go-paxos/internal/transport"
"github.com/LiuzhouChan/go-paxos/paxosio"
"github.com/LiuzhouChan/go-paxos/paxospb"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
)
const (
paxosTestTopDir = "raft_node_test_safe_to_delete"
logdbDir = "logdb_test_dir_safe_to_delete"
lowLatencyLogDBDir = "logdb_ll_test_dir_safe_to_delete"
testGroupID uint64 = 1100
tickMillisecond uint64 = 50
)
func getMemberNodes(r *rsm.StateMachine) []uint64 {
m, _, _ := r.GetMembership()
n := make([]uint64, 0)
for nid := range m {
n = append(n, nid)
}
return n
}
type testMessageRouter struct {
groupID uint64
msgReceiveCh map[uint64]*server.MessageQueue
dropRate uint8
}
func mustComplete(rs *RequestState, t *testing.T) {
select {
case v := <-rs.CompletedC:
if !v.Completed() {
t.Fatalf("got %d, want %d", v, requestCompleted)
}
default:
t.Fatalf("failed to complete the proposal")
}
}
func mustReject(rs *RequestState, t *testing.T) {
select {
case v := <-rs.CompletedC:
if !v.Rejected() {
t.Errorf("got %d, want %d", v, requestRejected)
}
default:
t.Errorf("failed to complete the add node request")
}
}
func newTestMessageRouter(groupID uint64,
nodeIDList []uint64) *testMessageRouter {
chMap := make(map[uint64]*server.MessageQueue)
for _, nodeID := range nodeIDList {
ch := server.NewMessageQueue(1000, false, 0)
chMap[nodeID] = ch
}
rand.Seed(time.Now().UnixNano())
return &testMessageRouter{msgReceiveCh: chMap, groupID: groupID}
}
func (r *testMessageRouter) shouldDrop(msg paxospb.PaxosMsg) bool {
if ipaxos.IsLocalMessageType(msg.MsgType) {
return false
}
if msg.From == msg.To {
// the local msg should not drop
return false
}
if r.dropRate == 0 {
return false
}
if rand.Uint32()%100 < uint32(r.dropRate) {
return true
}
return false
}
func (r *testMessageRouter) sendMessage(msg paxospb.PaxosMsg) {
if msg.GroupID != r.groupID {
panic("group id does not match")
}
if r.shouldDrop(msg) {
return
}
// plog.Infof("send msgs %v", msg)
if q, ok := r.msgReceiveCh[msg.To]; ok {
q.Add(msg)
}
}
func (r *testMessageRouter) getMessageReceiveChannel(groupID uint64,
nodeID uint64) *server.MessageQueue {
if groupID != r.groupID {
panic("cluster id does not match")
}
ch, ok := r.msgReceiveCh[nodeID]
if !ok {
panic("node id not found in the test msg router")
}
return ch
}
func (r *testMessageRouter) addChannel(nodeID uint64, q *server.MessageQueue) {
r.msgReceiveCh[nodeID] = q
}
func cleanupTestDir() {
os.RemoveAll(paxosTestTopDir)
}
func getTestPaxosNodes(count int) ([]*node, []*rsm.StateMachine,
*testMessageRouter, paxosio.ILogDB) {
return doGetTestPaxosNodes(1, count, nil)
}
func doGetTestPaxosNodes(startID uint64, count int,
ldb paxosio.ILogDB) ([]*node, []*rsm.StateMachine, *testMessageRouter, paxosio.ILogDB) {
nodes := make([]*node, 0)
smList := make([]*rsm.StateMachine, 0)
nodeIDList := make([]uint64, 0)
// peers map
peers := make(map[uint64]string)
endID := startID + uint64(count-1)
for i := startID; i <= endID; i++ {
nodeIDList = append(nodeIDList, i)
peers[i] = fmt.Sprintf("peer:%d", 12345+i)
}
// pools
requestStatePool := &sync.Pool{}
requestStatePool.New = func() interface{} {
obj := &RequestState{}
obj.CompletedC = make(chan RequestResult, 1)
obj.pool = requestStatePool
return obj
}
var err error
if ldb == nil {
nodeLoadDir := filepath.Join(paxosTestTopDir, logdbDir)
nodeLowLatencyLogDir := filepath.Join(paxosTestTopDir, lowLatencyLogDBDir)
os.MkdirAll(nodeLoadDir, 0755)
os.MkdirAll(nodeLowLatencyLogDir, 0755)
ldb, err = logdb.OpenLogDB([]string{nodeLoadDir}, []string{nodeLowLatencyLogDir})
if err != nil {
plog.Panicf("failed to open logdb, %v", err)
}
}
//message router
router := newTestMessageRouter(testGroupID, nodeIDList)
for i := startID; i <= endID; i++ {
// create the sm
sm := &tests.NoOP{}
ds := rsm.NewNativeStateMachine(sm, make(chan struct{}))
// node registry
nr := transport.NewNodes(settings.Soft.StreamConnections)
config := config.Config{
NodeID: uint64(i),
GroupID: testGroupID,
AskForLearnRTT: 10,
}
addr := fmt.Sprintf("a%d", i)
ch := router.getMessageReceiveChannel(testGroupID, uint64(i))
node := newNode(
addr,
peers,
true,
ds,
func(uint64) {},
router.sendMessage,
ch,
make(chan struct{}),
nr,
requestStatePool,
config,
tickMillisecond,
ldb)
nodes = append(nodes, node)
smList = append(smList, node.sm)
}
return nodes, smList, router, ldb
}
var ptc paxosio.IContext
func step(nodes []*node) bool {
hasEvent := false
nodeUpdates := make([]paxospb.Update, 0)
activeNodes := make([]*node, 0)
for _, node := range nodes {
if !node.initialized() {
node.setInitialStatus(0)
}
if node.initialized() {
if node.handleEvents() {
hasEvent = true
ud, ok := node.getUpdate()
if ok {
nodeUpdates = append(nodeUpdates, ud)
activeNodes = append(activeNodes, node)
}
}
}
}
for idx, ud := range nodeUpdates {
node := activeNodes[idx]
node.applyPaxosUpdates(ud)
// this part send msg to follower
// node.sendMessages(ud.Messages)
}
if ptc == nil {
ptc = nodes[0].logdb.GetLogDBThreadContext()
} else {
ptc.Reset()
}
// persistent state and entries
if err := nodes[0].logdb.SavePaxosState(nodeUpdates, ptc); err != nil {
panic(err)
}
for idx, ud := range nodeUpdates {
node := activeNodes[idx]
running := node.processPaxosUpdate(ud)
node.commitPaxosUpdate(ud)
if running {
node.sm.Handle(make([]rsm.Commit, 0))
}
}
return hasEvent
}
func singleStepNodes(nodes []*node, smList []*rsm.StateMachine, r *testMessageRouter) {
for _, node := range nodes {
tickMsg := paxospb.PaxosMsg{
MsgType: paxospb.LocalTick,
To: node.nodeID,
}
tickMsg.GroupID = testGroupID
r.sendMessage(tickMsg)
}
step(nodes)
}
func stepNodes(nodes []*node, smList []*rsm.StateMachine,
r *testMessageRouter, timeout uint64) {
s := timeout/tickMillisecond + 10
for i := uint64(0); i < s; i++ {
for _, node := range nodes {
tickMsg := paxospb.PaxosMsg{
MsgType: paxospb.LocalTick,
To: node.nodeID,
}
tickMsg.GroupID = testGroupID
r.sendMessage(tickMsg)
}
step(nodes)
}
}
func stopNodes(nodes []*node) {
for _, node := range nodes {
node.close()
}
}
func TestNodeCanBeCreatedAndStarted(t *testing.T) {
defer leaktest.AfterTest(t)()
defer cleanupTestDir()
nodes, smList, _, ldb := getTestPaxosNodes(3)
if len(nodes) != 3 {
t.Errorf("len(nodes)=%d, want 3", len(nodes))
}
if len(smList) != 3 {
t.Errorf("len(smList)=%d, want 3", len(nodes))
}
defer stopNodes(nodes)
defer ldb.Close()
}
func getMaxLastApplied(smList []*rsm.StateMachine) uint64 {
maxLastApplied := uint64(0)
for _, sm := range smList {
la := sm.GetLastApplied()
if la > maxLastApplied {
maxLastApplied = la
}
}
return maxLastApplied
}
func getTestTimeout(timeoutInMillisecond uint64) time.Duration {
return time.Duration(timeoutInMillisecond) * time.Millisecond
}
func makeCheckedTestProposal(t *testing.T, data []byte, timeoutInMillisecond uint64,
nodes []*node, smList []*rsm.StateMachine, router *testMessageRouter,
expectedCode RequestResultCode, checkResult bool, expectedResult uint64) {
timeout := getTestTimeout(timeoutInMillisecond)
n := nodes[0]
rs, err := n.propose(data, nil, timeout)
if err != nil {
t.Fatal("failed to make proposal")
}
stepNodes(nodes, smList, router, timeoutInMillisecond+1000)
select {
case v := <-rs.CompletedC:
if v.code != expectedCode {
t.Errorf("got %d, want %d", v, expectedCode)
}
if checkResult {
if v.GetResult() != expectedResult {
t.Errorf("result %d, want %d", v.GetResult(), expectedResult)
}
}
default:
t.Errorf("failed to complete the proposal")
}
}
func runPaxosNodeTest(t *testing.T,
tf func(t *testing.T, nodes []*node, smList []*rsm.StateMachine,
router *testMessageRouter, ldb paxosio.ILogDB)) {
defer leaktest.AfterTest(t)()
defer cleanupTestDir()
nodes, smList, router, ldb := getTestPaxosNodes(3)
defer stopNodes(nodes)
defer ldb.Close()
tf(t, nodes, smList, router, ldb)
}
func TestProposalCanBeMadeWithMessageDrops(t *testing.T) {
tf := func(t *testing.T, nodes []*node, smList []*rsm.StateMachine,
router *testMessageRouter, ldb paxosio.ILogDB) {
router.dropRate = 3
for i := 0; i < 20; i++ {
plog.Infof("making proposal id: %d", i)
maxLastApplied := getMaxLastApplied(smList)
plog.Infof("maxLastApplied is: %d", maxLastApplied)
makeCheckedTestProposal(t, []byte("test-data"), 4000, nodes,
smList, router, requestCompleted, false, 0)
if getMaxLastApplied(smList) != maxLastApplied+1 {
t.Errorf("didn't move the last applied value in smList")
}
}
}
runPaxosNodeTest(t, tf)
}