mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
raft: Step returns ok
This commit is contained in:
committed by
Yicheng Qin
parent
a10461f60d
commit
e5b9e22518
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
type Interface interface {
|
||||
Step(m Message)
|
||||
Step(m Message) bool
|
||||
Msgs() []Message
|
||||
}
|
||||
|
||||
@@ -60,9 +60,11 @@ func (n *Node) Remove(id int) { n.updateConf(configRemove, &config{NodeId: id})
|
||||
|
||||
func (n *Node) Msgs() []Message { return n.sm.Msgs() }
|
||||
|
||||
func (n *Node) Step(m Message) {
|
||||
func (n *Node) Step(m Message) bool {
|
||||
l := len(n.sm.msgs)
|
||||
n.sm.Step(m)
|
||||
if !n.sm.Step(m) {
|
||||
return false
|
||||
}
|
||||
for _, m := range n.sm.msgs[l:] {
|
||||
// reset elapsed in two cases:
|
||||
// msgAppResp -> heard from the leader of the same term
|
||||
@@ -76,6 +78,7 @@ func (n *Node) Step(m Message) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Next returns all the appliable entries
|
||||
|
||||
19
raft/raft.go
19
raft/raft.go
@@ -240,13 +240,13 @@ func (sm *stateMachine) Msgs() []Message {
|
||||
return msgs
|
||||
}
|
||||
|
||||
func (sm *stateMachine) Step(m Message) {
|
||||
func (sm *stateMachine) Step(m Message) (ok bool) {
|
||||
switch m.Type {
|
||||
case msgHup:
|
||||
sm.becomeCandidate()
|
||||
if sm.q() == sm.poll(sm.id, true) {
|
||||
sm.becomeLeader()
|
||||
return
|
||||
return true
|
||||
}
|
||||
for i := range sm.ins {
|
||||
if i == sm.id {
|
||||
@@ -255,10 +255,10 @@ func (sm *stateMachine) Step(m Message) {
|
||||
lasti := sm.log.lastIndex()
|
||||
sm.send(Message{To: i, Type: msgVote, Index: lasti, LogTerm: sm.log.term(lasti)})
|
||||
}
|
||||
return
|
||||
return true
|
||||
case msgBeat:
|
||||
if sm.state != stateLeader {
|
||||
return
|
||||
return true
|
||||
}
|
||||
sm.bcastAppend()
|
||||
return
|
||||
@@ -272,8 +272,7 @@ func (sm *stateMachine) Step(m Message) {
|
||||
e := m.Entries[0]
|
||||
if e.Type == configAdd || e.Type == configRemove {
|
||||
if sm.pendingConf {
|
||||
// todo: deny
|
||||
return
|
||||
return false
|
||||
}
|
||||
sm.pendingConf = true
|
||||
}
|
||||
@@ -284,12 +283,13 @@ func (sm *stateMachine) Step(m Message) {
|
||||
sm.maybeCommit()
|
||||
sm.bcastAppend()
|
||||
case none:
|
||||
panic("msgProp given without leader")
|
||||
// msgProp given without leader
|
||||
return false
|
||||
default:
|
||||
m.To = sm.lead
|
||||
sm.send(m)
|
||||
}
|
||||
return
|
||||
return true
|
||||
}
|
||||
|
||||
switch {
|
||||
@@ -297,7 +297,7 @@ func (sm *stateMachine) Step(m Message) {
|
||||
sm.becomeFollower(m.Term, m.From)
|
||||
case m.Term < sm.term:
|
||||
// ignore
|
||||
return
|
||||
return true
|
||||
}
|
||||
|
||||
handleAppendEntries := func() {
|
||||
@@ -354,6 +354,7 @@ func (sm *stateMachine) Step(m Message) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (sm *stateMachine) add(id int) {
|
||||
|
||||
@@ -742,7 +742,7 @@ type connem struct {
|
||||
|
||||
type blackHole struct{}
|
||||
|
||||
func (blackHole) Step(Message) {}
|
||||
func (blackHole) Msgs() []Message { return nil }
|
||||
func (blackHole) Step(Message) bool { return true }
|
||||
func (blackHole) Msgs() []Message { return nil }
|
||||
|
||||
var nopStepper = &blackHole{}
|
||||
|
||||
Reference in New Issue
Block a user