etcd/raft/node.go

137 lines
2.6 KiB
Go
Raw Normal View History

package raft
2014-06-05 21:49:34 +04:00
import (
"encoding/json"
)
type Interface interface {
Step(m Message)
Msgs() []Message
}
2014-05-29 00:53:26 +04:00
type tick int
2014-06-05 21:49:34 +04:00
type ConfigCmd struct {
Type string
2014-06-08 13:50:39 +04:00
Addr int
2014-06-05 21:49:34 +04:00
}
type Node struct {
2014-05-29 00:53:26 +04:00
// election timeout and heartbeat timeout in tick
election tick
heartbeat tick
// elapsed ticks after the last reset
elapsed tick
sm *stateMachine
2014-06-08 13:50:39 +04:00
addr int
}
2014-06-10 04:17:38 +04:00
func New(addr int, peers []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-06-10 04:17:38 +04:00
sm: newStateMachine(addr, peers),
2014-05-29 00:53:26 +04:00
heartbeat: heartbeat,
election: election,
2014-06-08 13:50:39 +04:00
addr: addr,
}
2014-05-29 00:53:26 +04:00
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)
}
2014-06-08 13:50:39 +04:00
func (n *Node) Add(addr int) {
n.Step(n.confMessage(&ConfigCmd{Type: "add", Addr: addr}))
}
2014-06-05 21:49:34 +04:00
2014-06-08 13:50:39 +04:00
func (n *Node) Remove(addr int) {
n.Step(n.confMessage(&ConfigCmd{Type: "remove", Addr: addr}))
2014-06-05 21:49:34 +04:00
}
func (n *Node) Msgs() []Message {
return n.sm.Msgs()
}
func (n *Node) Step(m Message) {
l := len(n.sm.msgs)
n.sm.Step(m)
for _, m := range n.sm.msgs[l:] {
2014-05-29 00:53:26 +04:00
// reset elapsed in two cases:
// msgAppResp -> heard from the leader of the same term
// msgVoteResp with grant -> heard from the candidate the node voted for
switch m.Type {
case msgAppResp:
n.elapsed = 0
case msgVoteResp:
if m.Index >= 0 {
n.elapsed = 0
}
}
}
}
2014-05-19 11:24:02 +04:00
2014-06-05 21:49:34 +04:00
// Next applies all available committed commands.
func (n *Node) Next() {
ents := n.sm.nextEnts()
for i := range ents {
switch ents[i].Type {
case normal:
// dispatch to the application state machine
case config:
c := new(ConfigCmd)
err := json.Unmarshal(ents[i].Data, c)
if err != nil {
// warning
continue
}
n.updateConf(c)
default:
panic("unexpected entry type")
}
}
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-08 13:50:39 +04:00
func (n *Node) confMessage(c *ConfigCmd) Message {
data, err := json.Marshal(c)
if err != nil {
panic(err)
}
return Message{Type: msgProp, Entries: []Entry{Entry{Type: config, Data: data}}}
}
2014-06-05 21:49:34 +04:00
func (n *Node) updateConf(c *ConfigCmd) {
switch c.Type {
case "add":
2014-06-08 13:50:39 +04:00
n.sm.Add(c.Addr)
case "remove":
n.sm.Remove(c.Addr)
2014-06-05 21:49:34 +04:00
default:
// warn
}
}