etcd/raft/node.go

153 lines
3.3 KiB
Go
Raw Normal View History

2014-08-25 07:09:06 +04:00
// Package raft implements raft.
package raft
import (
"code.google.com/p/go.net/context"
2014-08-28 05:53:18 +04:00
pb "github.com/coreos/etcd/raft/raftpb"
)
2014-06-05 21:49:34 +04:00
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-08-26 05:39:02 +04:00
func Start(ctx context.Context, id int64, peers []int64) Node {
n := Node{
2014-08-30 02:32:41 +04:00
ctx: ctx,
propc: make(chan pb.Message),
recvc: make(chan pb.Message),
readyc: make(chan Ready),
tickc: make(chan struct{}),
alwaysreadyc: make(chan Ready),
2014-08-25 07:09:06 +04:00
}
r := newRaft(id, peers)
go n.run(r)
return n
}
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
var prev Ready
2014-08-25 07:09:06 +04:00
for {
if r.hasLeader() {
propc = n.propc
} else {
// We cannot accept proposals because we don't know who
// to send them to, so we'll apply back-pressure and
// block senders.
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:
// 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-08-25 07:09:06 +04:00
case <-n.ctx.Done():
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
case <-n.ctx.Done():
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()
case <-n.ctx.Done():
return n.ctx.Err()
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
}