From 14ebedc2dcde54563e9671553f484b0811fafb3b Mon Sep 17 00:00:00 2001 From: Nathan VanBenschoten Date: Tue, 31 Jan 2023 11:30:44 -0500 Subject: [PATCH] kv: shuffle local raft messages before delivery in tests This commit adds a testing facility to shuffle local raft messages before delivering them to the raft state machine. These are not required to be in order, so we shuffle in tests ensure that re-ordering is handled properly. --- pkg/kv/kvserver/replica_raft.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkg/kv/kvserver/replica_raft.go b/pkg/kv/kvserver/replica_raft.go index 6d306b3efb38..994471cb9564 100644 --- a/pkg/kv/kvserver/replica_raft.go +++ b/pkg/kv/kvserver/replica_raft.go @@ -32,6 +32,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/roachpb" "github.com/cockroachdb/cockroach/pkg/storage" "github.com/cockroachdb/cockroach/pkg/util" + "github.com/cockroachdb/cockroach/pkg/util/buildutil" "github.com/cockroachdb/cockroach/pkg/util/encoding" "github.com/cockroachdb/cockroach/pkg/util/hlc" "github.com/cockroachdb/cockroach/pkg/util/humanizeutil" @@ -1598,6 +1599,15 @@ func (r *Replica) deliverLocalRaftMsgsRaftMuLockedReplicaMuLocked( } r.localMsgs.Unlock() + // If we are in a test build, shuffle the local messages before delivering + // them. These are not required to be in order, so ensure that re-ordering is + // handled properly. + if buildutil.CrdbTestBuild { + rand.Shuffle(len(localMsgs), func(i, j int) { + localMsgs[i], localMsgs[j] = localMsgs[j], localMsgs[i] + }) + } + for i, m := range localMsgs { if err := raftGroup.Step(m); err != nil { log.Fatalf(ctx, "unexpected error stepping local raft message [%s]: %v",