Skip to content

Commit 4dcbdcd

Browse files
committed
kvserver: enable raft.Config.StepDownOnRemoval
This causes the Raft leader to step down when it removes itself from the range or demotes itself to a learner. This doesn't make any difference functionally, since we're careful to no longer tick the replica or otherwise use it, but in principle it could cause a leader that was no longer part of the range to continue to assert leadership, stalling the range since it wouldn't be able to propose anything. Stepping down appears safer and cleaner. Epic: none Release note: None
1 parent 10fc938 commit 4dcbdcd

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

pkg/kv/kvserver/replica_init.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,14 @@ func (r *Replica) initRaftMuLockedReplicaMuLocked(s kvstorage.LoadedReplicaState
255255
// initRaftGroupRaftMuLockedReplicaMuLocked initializes a Raft group for the
256256
// replica, replacing the existing Raft group if any.
257257
func (r *Replica) initRaftGroupRaftMuLockedReplicaMuLocked() error {
258+
ctx := r.AnnotateCtx(context.Background())
258259
rg, err := raft.NewRawNode(newRaftConfig(
260+
ctx,
259261
raft.Storage((*replicaRaftStorage)(r)),
260262
uint64(r.replicaID),
261263
r.mu.state.RaftAppliedIndex,
262264
r.store.cfg,
263-
&raftLogger{ctx: r.AnnotateCtx(context.Background())},
265+
&raftLogger{ctx: ctx},
264266
))
265267
if err != nil {
266268
return err

pkg/kv/kvserver/store.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import (
6868
"github.com/cockroachdb/cockroach/pkg/spanconfig"
6969
"github.com/cockroachdb/cockroach/pkg/spanconfig/spanconfigstore"
7070
"github.com/cockroachdb/cockroach/pkg/storage"
71+
"github.com/cockroachdb/cockroach/pkg/util"
7172
"github.com/cockroachdb/cockroach/pkg/util/admission"
7273
"github.com/cockroachdb/cockroach/pkg/util/admission/admissionpb"
7374
"github.com/cockroachdb/cockroach/pkg/util/ctxgroup"
@@ -257,6 +258,12 @@ var ExportRequestsLimit = settings.RegisterIntSetting(
257258
settings.PositiveInt,
258259
)
259260

261+
// raftStepDownOnRemoval is a metamorphic test parameter that makes Raft leaders
262+
// step down on demotion or removal. Following an upgrade, clusters may have
263+
// replicas with mixed settings, because it's only changed when initializing
264+
// replicas. Varying it makes sure we handle this state.
265+
var raftStepDownOnRemoval = util.ConstantWithMetamorphicTestBool("raft-step-down-on-removal", true)
266+
260267
// TestStoreConfig has some fields initialized with values relevant in tests.
261268
func TestStoreConfig(clock *hlc.Clock) StoreConfig {
262269
return testStoreConfig(clock, clusterversion.TestingBinaryVersion)
@@ -299,6 +306,7 @@ func testStoreConfig(clock *hlc.Clock, version roachpb.Version) StoreConfig {
299306
}
300307

301308
func newRaftConfig(
309+
ctx context.Context,
302310
strg raft.Storage,
303311
id uint64,
304312
appliedIndex kvpb.RaftIndex,
@@ -320,6 +328,20 @@ func newRaftConfig(
320328
Storage: strg,
321329
Logger: logger,
322330

331+
// StepDownOnRemoval requires 23.2. Otherwise, in a mixed-version cluster, a
332+
// 23.2 leader may step down when it demotes itself to learner, but a
333+
// designated follower (first in the range) running 23.1 will only campaign
334+
// once the leader is entirely removed from the range descriptor (see
335+
// shouldCampaignOnConfChange). This would leave the range without a leader,
336+
// having to wait out an election timeout.
337+
//
338+
// We only set this on replica initialization, so replicas without
339+
// StepDownOnRemoval may remain on 23.2 nodes until they restart. That's
340+
// totally fine, we just can't rely on this behavior until 24.1, but
341+
// we currently don't either.
342+
StepDownOnRemoval: storeCfg.Settings.Version.IsActive(ctx, clusterversion.V23_2) &&
343+
raftStepDownOnRemoval,
344+
323345
PreVote: true,
324346
CheckQuorum: storeCfg.RaftEnableCheckQuorum,
325347
}

0 commit comments

Comments
 (0)