fix(node):fix the failure of node test

This commit is contained in:
jiangzhe 2020-04-17 17:15:35 +08:00
parent c42ed255b3
commit e2c1bc8db1
1 changed files with 9 additions and 5 deletions

View File

@ -24,6 +24,7 @@ var defaultSnapshotCount uint64 = 10000
type Node struct { type Node struct {
id uint64 // raft id id uint64 // raft id
leader uint64 // leader id
node raft.Node // raft node node raft.Node // raft node
peerMgr peermgr.PeerManager // network manager peerMgr peermgr.PeerManager // network manager
peers []raft.Peer // raft peers peers []raft.Peer // raft peers
@ -246,11 +247,11 @@ func (n *Node) Step(ctx context.Context, msg []byte) error {
} }
func (n *Node) IsLeader() bool { func (n *Node) IsLeader() bool {
return n.node.Status().SoftState.Lead == n.id return n.leader == n.id
} }
func (n *Node) Ready() bool { func (n *Node) Ready() bool {
return n.node.Status().SoftState.Lead != 0 return n.leader != 0
} }
// main work loop // main work loop
@ -323,15 +324,18 @@ func (n *Node) run() {
} }
// 2: Apply Snapshot (if any) and CommittedEntries to the state machine. // 2: Apply Snapshot (if any) and CommittedEntries to the state machine.
if len(rd.CommittedEntries) != 0 || rd.SoftState != nil { if len(rd.CommittedEntries) != 0 {
n.tp.CheckExecute(n.IsLeader())
if ok := n.publishEntries(n.entriesToApply(rd.CommittedEntries)); !ok { if ok := n.publishEntries(n.entriesToApply(rd.CommittedEntries)); !ok {
n.Stop() n.Stop()
return return
} }
} }
if rd.SoftState != nil {
n.leader = rd.SoftState.Lead
n.tp.CheckExecute(n.IsLeader())
}
// 3: Send all Messages to the nodes named in the To field. // 3: Send all Messages to the nodes named in the To field.
n.send(rd.Messages) go n.send(rd.Messages)
n.maybeTriggerSnapshot() n.maybeTriggerSnapshot()