raft: init stateMachine in New

This commit is contained in:
Blake Mizerany 2014-06-13 14:41:22 -07:00 committed by Yicheng Qin
parent 7cdd148e24
commit c24b6b4150
2 changed files with 7 additions and 15 deletions

View File

@ -95,7 +95,6 @@ func buildCluster(size int) (nt *network, nodes []*Node) {
Dictate(nodes[0]).Next()
for i := 1; i < size; i++ {
nt.send(nodes[0].newConfMessage(configAdd, &Config{NodeId: i}))
nodes[i].Start()
for j := 0; j < i; j++ {
nodes[j].Next()
}

View File

@ -39,31 +39,24 @@ func New(addr int, heartbeat, election tick) *Node {
heartbeat: heartbeat,
election: election,
addr: addr,
sm: newStateMachine(addr, []int{addr}),
}
return n
}
func Dictate(n *Node) *Node {
n.Step(Message{Type: msgHup})
n.Step(n.newConfMessage(configAdd, &Config{NodeId: n.addr}))
return n
}
// Propose asynchronously proposes data be applied to the underlying state machine.
func (n *Node) Propose(data []byte) {
m := Message{Type: msgProp, Entries: []Entry{{Data: data}}}
n.Step(m)
}
func Dictate(n *Node) *Node {
n.sm = newStateMachine(n.addr, []int{n.addr})
n.Step(Message{Type: msgHup})
n.Step(n.newConfMessage(configAdd, &Config{NodeId: n.addr}))
return n
}
func (n *Node) Start() {
if n.sm != nil {
panic("node is started")
}
n.sm = newStateMachine(n.addr, nil)
}
func (n *Node) Add(addr int) {
n.Step(n.newConfMessage(configAdd, &Config{NodeId: addr}))
}