etcd/raft/node.go

151 lines
3.1 KiB
Go
Raw Normal View History

package raft
2014-06-05 21:49:34 +04:00
import (
"encoding/json"
golog "log"
2014-07-11 07:54:16 +04:00
"sync/atomic"
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-07-11 09:51:37 +04:00
type tick int64
2014-05-29 00:53:26 +04:00
2014-06-27 00:48:59 +04:00
type Config struct {
2014-07-08 09:27:40 +04:00
NodeId int64
Addr string
Context []byte
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-07-09 22:53:27 +04:00
func New(id int64, 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-07-09 22:53:27 +04:00
sm: newStateMachine(id, []int64{id}),
}
2014-05-29 00:53:26 +04:00
return n
}
2014-07-11 07:54:16 +04:00
func (n *Node) Id() int64 {
return atomic.LoadInt64(&n.sm.id)
}
2014-06-14 01:50:12 +04:00
2014-07-11 09:51:37 +04:00
func (n *Node) Index() int64 { return n.sm.log.lastIndex() }
2014-07-06 21:19:23 +04:00
2014-07-11 09:51:37 +04:00
func (n *Node) Term() int64 { return n.sm.term }
2014-07-06 21:19:23 +04:00
2014-07-11 09:51:37 +04:00
func (n *Node) Applied() int64 { return n.sm.log.applied }
2014-07-06 21:19:23 +04:00
2014-07-11 07:54:16 +04:00
func (n *Node) HasLeader() bool { return n.Leader() != none }
2014-06-20 01:39:17 +04:00
2014-07-11 07:54:16 +04:00
func (n *Node) IsLeader() bool { return n.Leader() == n.Id() }
2014-07-06 21:19:23 +04:00
2014-07-11 09:12:55 +04:00
func (n *Node) Leader() int64 { return n.sm.lead.Get() }
2014-07-06 21:19:23 +04:00
2014-06-14 01:41:22 +04:00
// Propose asynchronously proposes data be applied to the underlying state machine.
2014-06-20 01:08:35 +04:00
func (n *Node) Propose(data []byte) { n.propose(Normal, data) }
2014-06-14 01:55:49 +04:00
2014-07-11 09:51:37 +04:00
func (n *Node) propose(t int64, 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-27 00:48:59 +04:00
func (n *Node) Campaign() { n.Step(Message{Type: msgHup}) }
2014-06-05 21:49:34 +04:00
2014-07-08 09:27:40 +04:00
func (n *Node) Add(id int64, addr string, context []byte) {
n.updateConf(AddNode, &Config{NodeId: id, Addr: addr, Context: context})
}
2014-06-27 00:48:59 +04:00
2014-07-09 22:53:27 +04:00
func (n *Node) Remove(id int64) { n.updateConf(RemoveNode, &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 {
2014-06-20 01:08:35 +04:00
case Normal:
case AddNode:
2014-06-27 00:48:59 +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 RemoveNode:
2014-06-27 00:48:59 +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() {
if !n.sm.promotable() {
return
}
2014-05-29 00:53:26 +04:00
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-07-11 09:51:37 +04:00
func (n *Node) updateConf(t int64, 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
}