Skip to content

Commit

Permalink
Merge pull request etcd-io#14262 from mind1949/update-server-etcdserv…
Browse files Browse the repository at this point in the history
…er-raft

server/etcdserver: check whether raftNode has stopped
  • Loading branch information
ahrtr authored Aug 1, 2022
2 parents 21995a3 + f3bd535 commit 4f0e92d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
9 changes: 8 additions & 1 deletion server/etcdserver/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,14 @@ func (r *raftNode) apply() chan toApply {
}

func (r *raftNode) stop() {
r.stopped <- struct{}{}
select {
case r.stopped <- struct{}{}:
// Not already stopped, so trigger it
case <-r.done:
// Has already been stopped - no need to do anything
return
}
// Block until the stop has been acknowledged by start()
<-r.done
}

Expand Down
26 changes: 26 additions & 0 deletions server/etcdserver/raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,29 @@ func TestExpvarWithNoRaftStatus(t *testing.T) {
_ = kv.Value.String()
})
}

func TestStopRaftNodeMoreThanOnce(t *testing.T) {
n := newNopReadyNode()
r := newRaftNode(raftNodeConfig{
lg: zaptest.NewLogger(t),
Node: n,
storage: mockstorage.NewStorageRecorder(""),
raftStorage: raft.NewMemoryStorage(),
transport: newNopTransporter(),
})
r.start(&raftReadyHandler{})

for i := 0; i < 2; i++ {
stopped := make(chan struct{})
go func() {
r.stop()
close(stopped)
}()

select {
case <-stopped:
case <-time.After(time.Second):
t.Errorf("*raftNode.stop() is blocked !")
}
}
}

0 comments on commit 4f0e92d

Please sign in to comment.