Skip to content

Commit 464f43a

Browse files
committed
Refactor election timeout and remove bootstrap logic
1 parent 45f1742 commit 464f43a

File tree

2 files changed

+4
-28
lines changed

2 files changed

+4
-28
lines changed

adapter/test_util.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const (
5757
redisPort = 63790
5858

5959
// followers wait longer before starting elections to give the leader time to bootstrap and share config.
60-
followerElectionTimeout = 30 * time.Second
60+
followerElectionTimeout = 10 * time.Second
6161
)
6262

6363
var mu sync.Mutex
@@ -125,32 +125,13 @@ func createNode(t *testing.T, n int) ([]Node, []string, []string) {
125125
cfg := buildRaftConfig(n, ports)
126126
nodes, grpcAdders, redisAdders := setupNodes(t, ctx, n, ports, cfg)
127127

128-
bootstrapFollowers(t, cfg, nodes)
129128
waitForNodeListeners(t, ctx, nodes, waitTimeout, waitInterval)
130129
waitForConfigReplication(t, cfg, nodes, waitTimeout, waitInterval)
131130
waitForRaftReadiness(t, nodes, waitTimeout, waitInterval)
132131

133132
return nodes, grpcAdders, redisAdders
134133
}
135134

136-
func bootstrapFollowers(t *testing.T, cfg raft.Configuration, nodes []Node) {
137-
t.Helper()
138-
for i, n := range nodes {
139-
if i == 0 {
140-
continue
141-
}
142-
143-
future := n.raft.GetConfiguration()
144-
assert.NoError(t, future.Error())
145-
if len(future.Configuration().Servers) != 0 {
146-
continue
147-
}
148-
149-
boot := n.raft.BootstrapCluster(cfg)
150-
assert.NoError(t, boot.Error())
151-
}
152-
}
153-
154135
func waitForNodeListeners(t *testing.T, ctx context.Context, nodes []Node, waitTimeout, waitInterval time.Duration) {
155136
t.Helper()
156137
d := &net.Dialer{Timeout: time.Second}
@@ -255,7 +236,7 @@ func buildRaftConfig(n int, ports []portsAdress) raft.Configuration {
255236
return cfg
256237
}
257238

258-
const leaderElectionTimeout = 10 * time.Second
239+
const leaderElectionTimeout = 0 * time.Second
259240

260241
func setupNodes(t *testing.T, ctx context.Context, n int, ports []portsAdress, cfg raft.Configuration) ([]Node, []string, []string) {
261242
t.Helper()

cmd/server/demo.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"net"
77
"os"
88
"strconv"
9-
"time"
109

1110
"github.com/Jille/raft-grpc-leader-rpc/leaderhealth"
1211
transport "github.com/Jille/raft-grpc-transport"
@@ -81,7 +80,7 @@ func run(eg *errgroup.Group) error {
8180
trxSt := store.NewMemoryStoreDefaultTTL()
8281
fsm := kv.NewKvFSM(st, trxSt)
8382

84-
r, tm, err := newRaft(strconv.Itoa(i), grpcAdders[i], fsm, i == 0, cfg, 0)
83+
r, tm, err := newRaft(strconv.Itoa(i), grpcAdders[i], fsm, i == 0, cfg)
8584
if err != nil {
8685
return errors.WithStack(err)
8786
}
@@ -120,14 +119,10 @@ func run(eg *errgroup.Group) error {
120119
return nil
121120
}
122121

123-
func newRaft(myID string, myAddress string, fsm raft.FSM, bootstrap bool, cfg raft.Configuration, electionTimeout time.Duration) (*raft.Raft, *transport.Manager, error) {
122+
func newRaft(myID string, myAddress string, fsm raft.FSM, bootstrap bool, cfg raft.Configuration) (*raft.Raft, *transport.Manager, error) {
124123
c := raft.DefaultConfig()
125124
c.LocalID = raft.ServerID(myID)
126125

127-
if electionTimeout > 0 {
128-
c.ElectionTimeout = electionTimeout
129-
}
130-
131126
// this config is for development
132127
ldb := raft.NewInmemStore()
133128
sdb := raft.NewInmemStore()

0 commit comments

Comments
 (0)