|
@@ -25,6 +25,10 @@ type Node struct {
|
|
|
elapsed tick
|
|
elapsed tick
|
|
|
election tick
|
|
election tick
|
|
|
heartbeat tick
|
|
heartbeat tick
|
|
|
|
|
+
|
|
|
|
|
+ // TODO: it needs garbage collection later
|
|
|
|
|
+ rmNodes map[int64]struct{}
|
|
|
|
|
+ removed bool
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func New(id int64, heartbeat, election tick) *Node {
|
|
func New(id int64, heartbeat, election tick) *Node {
|
|
@@ -36,6 +40,7 @@ func New(id int64, heartbeat, election tick) *Node {
|
|
|
heartbeat: heartbeat,
|
|
heartbeat: heartbeat,
|
|
|
election: election,
|
|
election: election,
|
|
|
sm: newStateMachine(id, []int64{id}),
|
|
sm: newStateMachine(id, []int64{id}),
|
|
|
|
|
+ rmNodes: make(map[int64]struct{}),
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return n
|
|
return n
|
|
@@ -57,6 +62,8 @@ func (n *Node) IsLeader() bool { return n.Leader() == n.Id() }
|
|
|
|
|
|
|
|
func (n *Node) Leader() int64 { return n.sm.lead.Get() }
|
|
func (n *Node) Leader() int64 { return n.sm.lead.Get() }
|
|
|
|
|
|
|
|
|
|
+func (n *Node) IsRemoved() bool { return n.removed }
|
|
|
|
|
+
|
|
|
// 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) { n.propose(Normal, data) }
|
|
func (n *Node) Propose(data []byte) { n.propose(Normal, data) }
|
|
|
|
|
|
|
@@ -75,6 +82,17 @@ func (n *Node) Remove(id int64) { n.updateConf(RemoveNode, &Config{NodeId: id})
|
|
|
func (n *Node) Msgs() []Message { return n.sm.Msgs() }
|
|
func (n *Node) Msgs() []Message { return n.sm.Msgs() }
|
|
|
|
|
|
|
|
func (n *Node) Step(m Message) bool {
|
|
func (n *Node) Step(m Message) bool {
|
|
|
|
|
+ if m.Type == msgDenied {
|
|
|
|
|
+ n.removed = true
|
|
|
|
|
+ return false
|
|
|
|
|
+ }
|
|
|
|
|
+ if m.Term != 0 {
|
|
|
|
|
+ if _, ok := n.rmNodes[m.From]; ok {
|
|
|
|
|
+ n.sm.send(Message{To: m.From, Type: msgDenied})
|
|
|
|
|
+ return true
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
l := len(n.sm.msgs)
|
|
l := len(n.sm.msgs)
|
|
|
if !n.sm.Step(m) {
|
|
if !n.sm.Step(m) {
|
|
|
return false
|
|
return false
|
|
@@ -107,6 +125,7 @@ func (n *Node) Next() []Entry {
|
|
|
continue
|
|
continue
|
|
|
}
|
|
}
|
|
|
n.sm.addNode(c.NodeId)
|
|
n.sm.addNode(c.NodeId)
|
|
|
|
|
+ delete(n.rmNodes, c.NodeId)
|
|
|
case RemoveNode:
|
|
case RemoveNode:
|
|
|
c := new(Config)
|
|
c := new(Config)
|
|
|
if err := json.Unmarshal(ents[i].Data, c); err != nil {
|
|
if err := json.Unmarshal(ents[i].Data, c); err != nil {
|
|
@@ -114,6 +133,10 @@ func (n *Node) Next() []Entry {
|
|
|
continue
|
|
continue
|
|
|
}
|
|
}
|
|
|
n.sm.removeNode(c.NodeId)
|
|
n.sm.removeNode(c.NodeId)
|
|
|
|
|
+ n.rmNodes[c.NodeId] = struct{}{}
|
|
|
|
|
+ if c.NodeId == n.sm.id {
|
|
|
|
|
+ n.removed = true
|
|
|
|
|
+ }
|
|
|
default:
|
|
default:
|
|
|
panic("unexpected entry type")
|
|
panic("unexpected entry type")
|
|
|
}
|
|
}
|