etcd/raft/node.go

166 lines
3.4 KiB
Go
Raw Normal View History

2014-08-25 07:09:06 +04:00
// Package raft implements raft.
package raft
import (
2014-09-01 05:49:07 +04:00
"errors"
2014-09-03 07:21:58 +04:00
"log"
2014-09-01 05:49:07 +04:00
2014-08-28 05:53:18 +04:00
pb "github.com/coreos/etcd/raft/raftpb"
2014-09-04 07:06:16 +04:00
"github.com/coreos/etcd/third_party/code.google.com/p/go.net/context"
)
2014-06-05 21:49:34 +04:00
2014-09-01 05:49:07 +04:00
var ErrStopped = errors.New("raft: stopped")
type Ready struct {
// The current state of a Node
2014-08-28 05:53:18 +04:00
pb.State
// Entries specifies entries to be saved to stable storage BEFORE
// Messages are sent.
2014-08-28 05:53:18 +04:00
Entries []pb.Entry
// CommittedEntries specifies entries to be committed to a
// store/state-machine. These have previously been committed to stable
// store.
2014-08-28 05:53:18 +04:00
CommittedEntries []pb.Entry
// Messages specifies outbound messages to be sent AFTER Entries are
// committed to stable storage.
2014-08-28 05:53:18 +04:00
Messages []pb.Message
}
2014-08-28 05:53:18 +04:00
func isStateEqual(a, b pb.State) bool {
2014-08-25 07:09:06 +04:00
return a.Term == b.Term && a.Vote == b.Vote && a.LastIndex == b.LastIndex
}
2014-05-29 00:53:26 +04:00
func (rd Ready) containsUpdates(prev Ready) bool {
2014-08-28 05:53:18 +04:00
return !isStateEqual(prev.State, rd.State) || len(rd.Entries) > 0 || len(rd.CommittedEntries) > 0 || len(rd.Messages) > 0
2014-06-05 21:49:34 +04:00
}
type Node struct {
2014-08-30 02:32:41 +04:00
ctx context.Context
propc chan pb.Message
recvc chan pb.Message
readyc chan Ready
tickc chan struct{}
alwaysreadyc chan Ready
2014-09-01 05:49:07 +04:00
done chan struct{}
}
2014-09-03 03:59:29 +04:00
func Start(id int64, peers []int64, election, heartbeat int) Node {
2014-08-26 05:39:02 +04:00
n := Node{
2014-08-30 02:32:41 +04:00
propc: make(chan pb.Message),
recvc: make(chan pb.Message),
readyc: make(chan Ready),
tickc: make(chan struct{}),
alwaysreadyc: make(chan Ready),
2014-09-01 05:49:07 +04:00
done: make(chan struct{}),
2014-08-25 07:09:06 +04:00
}
2014-09-03 03:59:29 +04:00
r := newRaft(id, peers, election, heartbeat)
2014-08-25 07:09:06 +04:00
go n.run(r)
return n
}
2014-09-01 05:49:07 +04:00
func (n *Node) Stop() {
close(n.done)
}
2014-08-25 07:09:06 +04:00
func (n *Node) run(r *raft) {
propc := n.propc
readyc := n.readyc
2014-06-20 01:39:17 +04:00
2014-09-03 07:21:58 +04:00
var lead int64
var prev Ready
2014-09-04 22:09:55 +04:00
prev.Vote = none
2014-08-25 07:09:06 +04:00
for {
2014-09-03 07:21:58 +04:00
if lead != r.lead {
log.Printf("raft: leader changed from %#x to %#x", lead, r.lead)
lead = r.lead
if r.hasLeader() {
propc = n.propc
} else {
propc = nil
}
}
rd := Ready{
2014-08-25 07:09:06 +04:00
r.State,
r.raftLog.unstableEnts(),
r.raftLog.nextEnts(),
r.msgs,
2014-05-29 00:53:26 +04:00
}
2014-05-19 11:24:02 +04:00
if rd.containsUpdates(prev) {
readyc = n.readyc
2014-08-29 03:41:42 +04:00
prev = rd
2014-08-25 07:09:06 +04:00
} else {
readyc = nil
2014-06-05 21:49:34 +04:00
}
2014-08-25 07:09:06 +04:00
select {
case m := <-propc:
m.From = r.id
r.Step(m)
2014-08-25 07:09:06 +04:00
case m := <-n.recvc:
r.Step(m) // raft never returns an error
case <-n.tickc:
2014-09-03 03:59:29 +04:00
r.tick()
case readyc <- rd:
2014-08-25 07:09:06 +04:00
r.raftLog.resetNextEnts()
r.raftLog.resetUnstable()
r.msgs = nil
2014-08-30 02:32:41 +04:00
case n.alwaysreadyc <- rd:
// this is for testing only
2014-09-01 05:49:07 +04:00
case <-n.done:
2014-08-25 07:09:06 +04:00
return
2014-07-15 10:41:19 +04:00
}
2014-05-29 00:53:26 +04:00
}
}
2014-06-05 21:49:34 +04:00
2014-08-25 07:09:06 +04:00
func (n *Node) Tick() error {
select {
case n.tickc <- struct{}{}:
return nil
2014-09-01 05:49:07 +04:00
case <-n.done:
2014-08-25 07:09:06 +04:00
return n.ctx.Err()
2014-06-08 13:50:39 +04:00
}
2014-06-05 21:49:34 +04:00
}
2014-08-29 03:41:42 +04:00
func (n *Node) Campaign(ctx context.Context) error {
return n.Step(ctx, pb.Message{Type: msgHup})
}
2014-08-25 07:09:06 +04:00
// Propose proposes data be appended to the log.
2014-08-29 03:41:42 +04:00
func (n *Node) Propose(ctx context.Context, data []byte) error {
return n.Step(ctx, pb.Message{Type: msgProp, Entries: []pb.Entry{{Data: data}}})
2014-07-25 01:11:53 +04:00
}
2014-07-31 04:21:27 +04:00
2014-08-28 04:22:34 +04:00
// Step advances the state machine using msgs. The ctx.Err() will be returned,
2014-08-25 23:49:14 +04:00
// if any.
2014-08-28 05:53:18 +04:00
func (n *Node) Step(ctx context.Context, m pb.Message) error {
2014-08-28 04:22:34 +04:00
ch := n.recvc
if m.Type == msgProp {
ch = n.propc
}
2014-08-28 04:22:34 +04:00
select {
case ch <- m:
return nil
case <-ctx.Done():
return ctx.Err()
2014-09-01 05:49:07 +04:00
case <-n.done:
return ErrStopped
2014-08-01 02:18:44 +04:00
}
2014-07-31 04:21:27 +04:00
}
2014-08-25 07:09:06 +04:00
// ReadState returns the current point-in-time state.
func (n *Node) Ready() <-chan Ready {
return n.readyc
2014-07-31 04:21:27 +04:00
}
2014-08-30 02:32:41 +04:00
// RecvReadyNow returns the state of n without blocking. It is primarly for
// testing purposes only.
func RecvReadyNow(n Node) Ready {
return <-n.alwaysreadyc
}