-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathreplication_test.go
More file actions
147 lines (127 loc) · 3.01 KB
/
Copy pathreplication_test.go
File metadata and controls
147 lines (127 loc) · 3.01 KB
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
// Copyright IBM Corp. 2013, 2026
// SPDX-License-Identifier: MPL-2.0
package raft
import (
"context"
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestLeaderHeartbeatsWithStalledDisk(t *testing.T) {
c := MakeClusterCustom(t, &MakeClusterOpts{
Peers: 3,
Bootstrap: true,
LogstoreWrapperFunc: newBlockingLogStore,
})
t.Cleanup(c.Close)
go func() {
i := 0
ticker := time.NewTicker(500 * time.Millisecond)
for {
select {
case <-t.Context().Done():
return
case <-ticker.C:
i++
leader := c.Leader()
fut := leader.Apply(fmt.Appendf([]byte{}, "test%d", i), 0)
if err := fut.Error(); err != nil {
t.Logf("got error trying to write: %v", err)
} else {
t.Logf("write %d ok", i)
}
}
}
}()
t.Log("waiting for 5 seconds before partitioning leader")
time.Sleep(time.Second * 5)
oldLeader := c.Leader()
oldLeaderID := oldLeader.leaderID
oldLeaderTerm := oldLeader.getCurrentTerm()
c.Partition([]ServerAddress{c.Leader().localAddr})
var newLeaderTerm uint64
ctx, cancel := context.WithTimeout(t.Context(), 10*c.propagateTimeout)
t.Cleanup(cancel)
DONE:
for {
select {
case <-ctx.Done():
t.Fatal("election didn't happen!")
default:
newLeader := c.Leader()
if newLeader.leaderID != oldLeaderID {
t.Log("leader has stepped down!")
newLeaderTerm = newLeader.getCurrentTerm()
require.NotEqual(t, newLeaderTerm, oldLeaderTerm)
cancel()
break DONE
}
}
}
require.Len(t, c.WaitForFollowers(1), 1)
t.Log("leader was elected. healing parition")
// reconnect the partitioned node
c.FullyConnect()
time.Sleep(3 * c.propagateTimeout)
leaderTerm := c.Leader().getCurrentTerm()
require.Equal(t, newLeaderTerm, leaderTerm)
t.Log("blocking disk on leader")
leader := c.Leader()
leaderID := leader.leaderID
leaderStore := leader.logs.(*blockingLogStore)
leaderStore.block()
t.Cleanup(leaderStore.unblock)
ctx, cancel = context.WithTimeout(t.Context(), 5*time.Second)
t.Cleanup(cancel)
for {
select {
case <-ctx.Done():
t.Fatal("leader did not step down!")
default:
if c.Leader().leaderID != leaderID {
t.Log("leader has stepped down!")
return
}
}
}
}
// blockingLogStore wraps a LogStore and blocks GetLog calls on demand,
// simulating disk IO stalls.
type blockingLogStore struct {
LogStore
mu sync.Mutex
blocked atomic.Bool
unblockC chan struct{}
}
func newBlockingLogStore(inner LogStore) LogStore {
return &blockingLogStore{
LogStore: inner,
unblockC: make(chan struct{}),
}
}
func (b *blockingLogStore) block() {
b.mu.Lock()
defer b.mu.Unlock()
b.blocked.Store(true)
b.unblockC = make(chan struct{})
}
func (b *blockingLogStore) unblock() {
b.mu.Lock()
defer b.mu.Unlock()
if b.blocked.Load() {
b.blocked.Store(false)
close(b.unblockC)
}
}
func (b *blockingLogStore) GetLog(index uint64, log *Log) error {
if b.blocked.Load() {
b.mu.Lock()
ch := b.unblockC
b.mu.Unlock()
<-ch
}
return b.LogStore.GetLog(index, log)
}