mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
raft: refactor Node.Add/Remove
This commit is contained in:
committed by
Yicheng Qin
parent
96059a496a
commit
8c12d6d00f
@@ -92,9 +92,11 @@ func buildCluster(size int) (nt *network, nodes []*Node) {
|
||||
}
|
||||
nt = newNetwork(nis...)
|
||||
|
||||
Dictate(nodes[0]).Next()
|
||||
lead := Dictate(nodes[0])
|
||||
lead.Next()
|
||||
for i := 1; i < size; i++ {
|
||||
nt.send(nodes[0].newConfMessage(configAdd, &Config{NodeId: i}))
|
||||
lead.Add(i)
|
||||
nt.send(lead.Msgs()...)
|
||||
for j := 0; j < i; j++ {
|
||||
nodes[j].Next()
|
||||
}
|
||||
|
||||
20
raft/node.go
20
raft/node.go
@@ -44,25 +44,23 @@ func New(id int, heartbeat, election tick) *Node {
|
||||
|
||||
func Dictate(n *Node) *Node {
|
||||
n.Step(Message{Type: msgHup})
|
||||
n.Step(n.newConfMessage(configAdd, &Config{NodeId: n.Id()}))
|
||||
n.Add(n.Id())
|
||||
return n
|
||||
}
|
||||
|
||||
func (n *Node) Id() int { return n.sm.id }
|
||||
|
||||
// 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}}}
|
||||
func (n *Node) Propose(data []byte) { n.propose(normal, data) }
|
||||
|
||||
func (n *Node) propose(t int, data []byte) {
|
||||
m := Message{Type: msgProp, Entries: []Entry{{Type: t, Data: data}}}
|
||||
n.Step(m)
|
||||
}
|
||||
|
||||
func (n *Node) Add(id int) {
|
||||
n.Step(n.newConfMessage(configAdd, &Config{NodeId: id}))
|
||||
}
|
||||
func (n *Node) Add(id int) { n.updateConf(configAdd, &Config{NodeId: id}) }
|
||||
|
||||
func (n *Node) Remove(id int) {
|
||||
n.Step(n.newConfMessage(configRemove, &Config{NodeId: id}))
|
||||
}
|
||||
func (n *Node) Remove(id int) { n.updateConf(configRemove, &Config{NodeId: id}) }
|
||||
|
||||
func (n *Node) Msgs() []Message {
|
||||
return n.sm.Msgs()
|
||||
@@ -132,10 +130,10 @@ func (n *Node) Tick() {
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Node) newConfMessage(t int, c *Config) Message {
|
||||
func (n *Node) updateConf(t int, c *Config) {
|
||||
data, err := json.Marshal(c)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return Message{Type: msgProp, Entries: []Entry{Entry{Type: t, Data: data}}}
|
||||
n.propose(t, data)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user