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()
|
Dictate(nodes[0]).Next()
|
||||||
for i := 1; i < size; i++ {
|
for i := 1; i < size; i++ {
|
||||||
nt.send(nodes[0].newConfMessage(configAdd, &Config{NodeId: i}))
|
nt.send(nodes[0].newConfMessage(configAdd, &Config{NodeId: i}))
|
||||||
nodes[i].Start()
|
|
||||||
for j := 0; j < i; j++ {
|
for j := 0; j < i; j++ {
|
||||||
nodes[j].Next()
|
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,
|
heartbeat: heartbeat,
|
||||||
election: election,
|
election: election,
|
||||||
addr: addr,
|
addr: addr,
|
||||||
|
sm: newStateMachine(addr, []int{addr}),
|
||||||
}
|
}
|
||||||
|
|
||||||
return n
|
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.
|
// Propose asynchronously proposes data be applied to the underlying state machine.
|
||||||
func (n *Node) Propose(data []byte) {
|
func (n *Node) Propose(data []byte) {
|
||||||
m := Message{Type: msgProp, Entries: []Entry{{Data: data}}}
|
m := Message{Type: msgProp, Entries: []Entry{{Data: data}}}
|
||||||
n.Step(m)
|
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) {
|
func (n *Node) Add(addr int) {
|
||||||
n.Step(n.newConfMessage(configAdd, &Config{NodeId: addr}))
|
n.Step(n.newConfMessage(configAdd, &Config{NodeId: addr}))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user