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 5b45f64 commit 86c8928
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 @@ -52,6 +52,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 @@ -23,7 +23,6 @@ import (
"time"

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

func TestBasicProgress(t *testing.T) {
Expand Down Expand Up @@ -53,9 +52,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 @@ -135,23 +131,22 @@ func TestPause(t *testing.T) {
}

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

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

for i, n := range ns {
lead := n.Status().SoftState.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 86c8928

Please sign in to comment.