Skip to content

Commit a9685b0

Browse files
committed
kvserver: hold required locks on destroy status set
Epic: none Release note: none
1 parent 2f09641 commit a9685b0

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

pkg/kv/kvserver/replica_application_state_machine_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ func TestReplicaStateMachineChangeReplicas(t *testing.T) {
149149
// should there be a command in the raft log (i.e. some errant lease request
150150
// or whatnot) this will fire assertions because it will conflict with the
151151
// log index that we pulled out of thin air above.
152+
r.readOnlyCmdMu.Lock()
153+
defer r.readOnlyCmdMu.Unlock()
152154
r.mu.Lock()
153155
defer r.mu.Unlock()
154156
r.mu.destroyStatus.Set(errors.New("test done"), destroyReasonRemoved)
@@ -231,14 +233,16 @@ func TestReplicaStateMachineRaftLogTruncationStronglyCoupled(t *testing.T) {
231233
_, err = sm.ApplySideEffects(checkedCmd.Ctx(), checkedCmd)
232234
require.NoError(t, err)
233235
func() {
234-
r.mu.Lock()
235-
defer r.mu.Unlock()
236236
// Set a destroyStatus to make sure there won't be any raft processing once
237237
// we release raftMu. We applied a command but not one from the raft log, so
238238
// should there be a command in the raft log (i.e. some errant lease request
239239
// or whatnot) this will fire assertions because it will conflict with the
240240
// log index that we pulled out of thin air above.
241+
r.readOnlyCmdMu.Lock()
242+
r.mu.Lock()
243+
defer r.mu.Unlock()
241244
r.mu.destroyStatus.Set(errors.New("test done"), destroyReasonRemoved)
245+
r.readOnlyCmdMu.Unlock()
242246

243247
require.Equal(t, raftAppliedIndex+1, r.shMu.state.RaftAppliedIndex)
244248
require.Equal(t, truncatedIndex+1, ls.shMu.trunc.Index)
@@ -427,7 +431,13 @@ func TestReplicaStateMachineEphemeralAppBatchRejection(t *testing.T) {
427431
defer r.raftMu.Unlock()
428432
// Avoid additional raft processing after we're done with this replica because
429433
// we've applied entries that aren't in the log.
430-
defer r.mu.destroyStatus.Set(errors.New("boom"), destroyReasonRemoved)
434+
defer func() {
435+
r.readOnlyCmdMu.Lock()
436+
defer r.readOnlyCmdMu.Unlock()
437+
r.mu.Lock()
438+
defer r.mu.Unlock()
439+
r.mu.destroyStatus.Set(errors.New("boom"), destroyReasonRemoved)
440+
}()
431441

432442
sm := r.getStateMachine()
433443

pkg/kv/kvserver/replica_command_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -677,9 +677,13 @@ func TestWaitForLeaseAppliedIndex(t *testing.T) {
677677
stopper.Stop(ctx)
678678

679679
destroyErr := errors.New("destroyed")
680-
tc.repl.mu.Lock()
681-
tc.repl.mu.destroyStatus.Set(destroyErr, destroyReasonRemoved)
682-
tc.repl.mu.Unlock()
680+
func() {
681+
tc.repl.readOnlyCmdMu.Lock()
682+
defer tc.repl.readOnlyCmdMu.Unlock()
683+
tc.repl.mu.Lock()
684+
defer tc.repl.mu.Unlock()
685+
tc.repl.mu.destroyStatus.Set(destroyErr, destroyReasonRemoved)
686+
}()
683687

684688
_, err = tc.repl.WaitForLeaseAppliedIndex(ctx, maxLAI)
685689
require.Error(t, err)

pkg/kv/kvserver/replica_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13781,9 +13781,13 @@ func TestAdminScatterDestroyedReplica(t *testing.T) {
1378113781
tc.Start(ctx, t, stopper)
1378213782

1378313783
errBoom := errors.New("boom")
13784-
tc.repl.mu.Lock()
13785-
tc.repl.mu.destroyStatus.Set(errBoom, destroyReasonMergePending)
13786-
tc.repl.mu.Unlock()
13784+
func() {
13785+
tc.repl.readOnlyCmdMu.Lock()
13786+
defer tc.repl.readOnlyCmdMu.Unlock()
13787+
tc.repl.mu.Lock()
13788+
defer tc.repl.mu.Unlock()
13789+
tc.repl.mu.destroyStatus.Set(errBoom, destroyReasonMergePending)
13790+
}()
1378713791

1378813792
desc := tc.repl.Desc()
1378913793
resp, err := tc.repl.adminScatter(ctx, kvpb.AdminScatterRequest{

0 commit comments

Comments
 (0)