mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
raft: init stateMachine in New
This commit is contained in:
parent
7cdd148e24
commit
c24b6b4150
@ -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()
|
||||
}
|
||||
|
21
raft/node.go
21
raft/node.go
@ -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}))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user