Skip to content

Commit

Permalink
raft: fix flaky leader index in waitLeader function
Browse files Browse the repository at this point in the history
Fixes cockroachdb#127413.

This commit bypasses the larger rebase in cockroachdb#122133 to pick up the test
flake fix in etcd-io/raft#188. There was some
discussion in etcd-io/raft#181 about alternatives
for fixing this test. For now, we stick with a direct cherry-pick.

Release note: None
  • Loading branch information
nvanbenschoten committed Sep 6, 2024
1 parent e80b68d commit bd9edcb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
1 change: 0 additions & 1 deletion pkg/raft/rafttest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,5 @@ go_test(
deps = [
"//pkg/raft",
"//pkg/raft/raftpb",
"//pkg/testutils/skip",
],
)
15 changes: 5 additions & 10 deletions pkg/raft/rafttest/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"github.com/cockroachdb/cockroach/pkg/raft"
pb "github.com/cockroachdb/cockroach/pkg/raft/raftpb"
"github.com/cockroachdb/cockroach/pkg/testutils/skip"
)

func TestBasicProgress(t *testing.T) {
Expand Down Expand Up @@ -54,9 +53,6 @@ func TestBasicProgress(t *testing.T) {
}

func TestRestart(t *testing.T) {
// TODO(pav-kv): de-flake it. See https://github.com/etcd-io/raft/issues/181.
skip.UnderStress(t, "the test is flaky")

peers := []raft.Peer{{ID: 1, Context: nil}, {ID: 2, Context: nil}, {ID: 3, Context: nil}, {ID: 4, Context: nil}, {ID: 5, Context: nil}}
nt := newRaftNetwork(1, 2, 3, 4, 5)

Expand Down Expand Up @@ -136,23 +132,22 @@ func TestPause(t *testing.T) {
}

func waitLeader(ns []*node) int {
var l map[pb.PeerID]struct{}
var lindex int

l := make(map[pb.PeerID]struct{})
for {
l = make(map[pb.PeerID]struct{})
clear(l)
lindex := -1

for i, n := range ns {
lead := n.Status().HardState.Lead
if lead != 0 {
if lead != raft.None {
l[lead] = struct{}{}
if n.id == lead {
lindex = i
}
}
}

if len(l) == 1 {
if len(l) == 1 && lindex != -1 {
return lindex
}
}
Expand Down

0 comments on commit bd9edcb

Please sign in to comment.