etcd/raft/node.go

133 lines
2.6 KiB
Go
Raw Normal View History

package raft
2014-06-05 21:49:34 +04:00
import (
"encoding/json"
golog "log"
2014-06-05 21:49:34 +04:00
)
type Interface interface {
2014-06-14 03:07:27 +04:00
Step(m Message) bool
Msgs() []Message
}
2014-05-29 00:53:26 +04:00
type tick int
2014-06-14 02:00:31 +04:00
type config struct {
NodeId int
Address string
2014-06-05 21:49:34 +04:00
}
type Node struct {
2014-06-14 02:23:08 +04:00
sm *stateMachine
elapsed tick
2014-05-29 00:53:26 +04:00
election tick
heartbeat tick
}
2014-06-14 01:48:36 +04:00
func New(id int, heartbeat, election tick) *Node {
2014-05-29 00:53:26 +04:00
if election < heartbeat*3 {
panic("election is least three times as heartbeat [election: %d, heartbeat: %d]")
}
n := &Node{
2014-05-29 00:53:26 +04:00
heartbeat: heartbeat,
election: election,
2014-06-14 01:48:36 +04:00
sm: newStateMachine(id, []int{id}),
}
2014-05-29 00:53:26 +04:00
return n
}
2014-06-14 01:36:51 +04:00
func Dictate(n *Node) *Node {
2014-06-10 06:37:41 +04:00
n.Step(Message{Type: msgHup})
2014-06-14 01:55:49 +04:00
n.Add(n.Id())
2014-06-14 01:36:51 +04:00
return n
2014-06-10 06:37:41 +04:00
}
2014-06-14 01:50:12 +04:00
func (n *Node) Id() int { return n.sm.id }
2014-06-14 01:41:22 +04:00
// Propose asynchronously proposes data be applied to the underlying state machine.
2014-06-14 01:55:49 +04:00
func (n *Node) Propose(data []byte) { n.propose(normal, data) }
func (n *Node) propose(t int, data []byte) {
2014-06-14 02:02:43 +04:00
n.Step(Message{Type: msgProp, Entries: []Entry{{Type: t, Data: data}}})
2014-06-10 06:37:41 +04:00
}
2014-06-14 02:00:31 +04:00
func (n *Node) Add(id int) { n.updateConf(configAdd, &config{NodeId: id}) }
2014-06-05 21:49:34 +04:00
2014-06-14 02:00:31 +04:00
func (n *Node) Remove(id int) { n.updateConf(configRemove, &config{NodeId: id}) }
2014-06-05 21:49:34 +04:00
2014-06-14 02:03:30 +04:00
func (n *Node) Msgs() []Message { return n.sm.Msgs() }
2014-06-14 03:07:27 +04:00
func (n *Node) Step(m Message) bool {
l := len(n.sm.msgs)
2014-06-14 03:07:27 +04:00
if !n.sm.Step(m) {
return false
}
for _, m := range n.sm.msgs[l:] {
2014-05-29 00:53:26 +04:00
switch m.Type {
case msgAppResp:
2014-06-14 03:31:40 +04:00
// We just heard from the leader of the same term.
2014-05-29 00:53:26 +04:00
n.elapsed = 0
case msgVoteResp:
2014-06-14 03:31:40 +04:00
// We just heard from the candidate the node voted for.
2014-05-29 00:53:26 +04:00
if m.Index >= 0 {
n.elapsed = 0
}
}
}
2014-06-14 03:07:27 +04:00
return true
}
2014-05-19 11:24:02 +04:00
2014-06-14 02:15:52 +04:00
// Next returns all the appliable entries
2014-06-11 00:59:05 +04:00
func (n *Node) Next() []Entry {
2014-06-05 21:49:34 +04:00
ents := n.sm.nextEnts()
for i := range ents {
switch ents[i].Type {
case normal:
case configAdd:
2014-06-14 02:00:31 +04:00
c := new(config)
if err := json.Unmarshal(ents[i].Data, c); err != nil {
golog.Println(err)
2014-06-05 21:49:34 +04:00
continue
}
2014-06-14 03:08:59 +04:00
n.sm.addNode(c.NodeId)
case configRemove:
2014-06-14 02:00:31 +04:00
c := new(config)
if err := json.Unmarshal(ents[i].Data, c); err != nil {
golog.Println(err)
continue
}
2014-06-14 03:08:59 +04:00
n.sm.removeNode(c.NodeId)
2014-06-05 21:49:34 +04:00
default:
panic("unexpected entry type")
}
}
2014-06-14 02:14:44 +04:00
return ents
2014-05-19 11:24:02 +04:00
}
2014-05-29 00:53:26 +04:00
// Tick triggers the node to do a tick.
// If the current elapsed is greater or equal than the timeout,
// node will send corresponding message to the statemachine.
func (n *Node) Tick() {
timeout, msgType := n.election, msgHup
if n.sm.state == stateLeader {
timeout, msgType = n.heartbeat, msgBeat
}
if n.elapsed >= timeout {
n.Step(Message{Type: msgType})
n.elapsed = 0
} else {
n.elapsed++
}
}
2014-06-05 21:49:34 +04:00
2014-06-14 02:00:31 +04:00
func (n *Node) updateConf(t int, c *config) {
2014-06-08 13:50:39 +04:00
data, err := json.Marshal(c)
if err != nil {
panic(err)
}
2014-06-14 01:55:49 +04:00
n.propose(t, data)
2014-06-05 21:49:34 +04:00
}