Skip to content
File

Blob: chord/local_recovery_test.go

go111 lines
1package chord
2 
3import (
4 "errors"
5 "testing"
6 "time"
7 
8 "go.miragespace.co/specter/spec/chord"
9 "go.miragespace.co/specter/spec/mocks"
10 "go.miragespace.co/specter/spec/protocol"
11 
12 "github.com/stretchr/testify/require"
13)
14 
15func TestFindSuccessorWhenFingersLag(t *testing.T) {
16 for _, tc := range []struct {
17 name string
18 ids [3]uint64
19 key uint64
20 }{
21 {name: "ordinary", ids: [3]uint64{10, 20, 30}, key: 25},
22 {name: "wraparound", ids: [3]uint64{chord.MaxIdentitifer - 20, chord.MaxIdentitifer - 10, 10}, key: 5},
23 } {
24 t.Run(tc.name, func(t *testing.T) {
25 as := require.New(t)
26 nodes := make([]*LocalNode, len(tc.ids))
27 for i, id := range tc.ids {
28 conf := devConfig(t, as)
29 conf.Identity.Id = id
30 nodes[i] = NewLocalNode(conf)
31 nodes[i].state.Set(chord.Active)
32 }
33 for i, node := range nodes {
34 node.predecessor = nodes[(i+len(nodes)-1)%len(nodes)]
35 node.successors = []chord.VNode{nodes[(i+1)%len(nodes)]}
36 }
37 
38 // The successor reflects a join, while fingers still point past the
39 // requested key. Keep maintenance stopped so the gap cannot repair itself.
40 for k := 1; k <= chord.MaxFingerEntries; k++ {
41 nodes[0].fingers[k].node = nodes[2]
42 }
43 successor, err := nodes[0].FindSuccessor(tc.key)
44 as.NoError(err)
45 as.Same(nodes[2], successor)
46 })
47 }
48}
49 
50func TestStabilizeRecordsSuccessfulExchange(t *testing.T) {
51 failed := errors.New("peer unavailable")
52 for _, tc := range []struct {
53 name string
54 state chord.State
55 failedHead bool
56 probeErr error
57 notifyErr error
58 wantErr error
59 wantTimestamp bool
60 }{
61 {name: "unchanged successors", state: chord.Active, wantTimestamp: true},
62 {name: "failed head with live successor", state: chord.Active, failedHead: true, wantTimestamp: true},
63 {name: "all probes failed", state: chord.Active, probeErr: failed, wantErr: chord.ErrNodeNoSuccessor},
64 {name: "notification failed after refresh", state: chord.Active, failedHead: true, notifyErr: failed, wantErr: failed},
65 {name: "leaving skips notification", state: chord.Leaving},
66 } {
67 t.Run(tc.name, func(t *testing.T) {
68 as := require.New(t)
69 conf := devConfig(t, as)
70 conf.Identity.Id = 10
71 node := NewLocalNode(conf)
72 node.state.Set(tc.state)
73 previous := time.Now().Add(-time.Hour)
74 node.lastStabilized.Store(previous)
75 
76 peer := new(mocks.VNode)
77 peer.On("ID").Return(uint64(30))
78 peer.On("Identity").Return(&protocol.Node{Id: 30}).Maybe()
79 peer.On("GetPredecessor").Return(nil, tc.probeErr).Once()
80 peer.On("GetSuccessors").Return(nil, tc.probeErr).Once()
81 if tc.probeErr == nil && tc.state != chord.Leaving {
82 peer.On("Notify", node).Return(tc.notifyErr).Once()
83 }
84 node.successors = []chord.VNode{peer}
85 if tc.failedHead {
86 dead := new(mocks.VNode)
87 dead.On("ID").Return(uint64(20))
88 dead.On("Identity").Return(&protocol.Node{Id: 20})
89 dead.On("GetPredecessor").Return(nil, failed).Once()
90 dead.On("GetSuccessors").Return(nil, failed).Once()
91 node.successors = append([]chord.VNode{dead}, node.successors...)
92 defer dead.AssertExpectations(t)
93 }
94 node.succListHash.Store(node.hash(node.successors))
95 
96 started := time.Now()
97 err := node.stabilize()
98 as.ErrorIs(err, tc.wantErr)
99 if tc.wantTimestamp {
100 as.WithinRange(node.lastStabilized.Load(), started, time.Now())
101 } else {
102 as.Equal(previous, node.lastStabilized.Load())
103 }
104 // A successful refresh survives notification failure; exhausted
105 // probes retain the previous list for the next maintenance pass.
106 as.Equal([]chord.VNode{peer}, node.getSuccessors())
107 peer.AssertExpectations(t)
108 })
109 }
110}