raft: start tick

This commit is contained in:
Blake Mizerany 2014-08-21 00:30:29 -07:00 committed by Yicheng Qin
parent 8d37587e47
commit e17f79ee70
2 changed files with 15 additions and 1 deletions

View File

@ -19,6 +19,7 @@ type Node struct {
propc chan proposal propc chan proposal
recvc chan Message recvc chan Message
statec chan stateResp statec chan stateResp
tickc chan struct{}
} }
func Start(ctx context.Context, name string, election, heartbeat int) *Node { func Start(ctx context.Context, name string, election, heartbeat int) *Node {
@ -27,6 +28,7 @@ func Start(ctx context.Context, name string, election, heartbeat int) *Node {
propc: make(chan proposal), propc: make(chan proposal),
recvc: make(chan Message), recvc: make(chan Message),
statec: make(chan stateResp), statec: make(chan stateResp),
tickc: make(chan struct{}),
} }
r := &raft{ r := &raft{
name: name, name: name,
@ -55,6 +57,8 @@ func (n *Node) run(r *raft) {
r.propose(p.id, p.data) r.propose(p.id, p.data)
case m := <-n.recvc: case m := <-n.recvc:
r.step(m) r.step(m)
case <-n.tickc:
r.tick()
case n.statec <- stateResp{r.State, r.ents, r.msgs}: case n.statec <- stateResp{r.State, r.ents, r.msgs}:
r.resetState() r.resetState()
case <-n.ctx.Done(): case <-n.ctx.Done():
@ -63,6 +67,15 @@ func (n *Node) run(r *raft) {
} }
} }
func (n *Node) Tick() error {
select {
case n.tickc <- struct{}{}:
return nil
case <-n.ctx.Done():
return n.ctx.Err()
}
}
// Propose proposes data be appended to the log. // Propose proposes data be appended to the log.
func (n *Node) Propose(id int64, data []byte) error { func (n *Node) Propose(id int64, data []byte) error {
select { select {
@ -83,7 +96,7 @@ func (n *Node) Step(m Message) error {
} }
} }
// ReadMessages returns the current point-in-time state. // ReadState returns the current point-in-time state.
func (n *Node) ReadState() (State, []Entry, []Message, error) { func (n *Node) ReadState() (State, []Entry, []Message, error) {
select { select {
case sr := <-n.statec: case sr := <-n.statec:

View File

@ -32,3 +32,4 @@ func (sm *raft) hasLeader() bool { return false }
func (sm *raft) step(m Message) {} func (sm *raft) step(m Message) {}
func (sm *raft) resetState() {} func (sm *raft) resetState() {}
func (sm *raft) propose(id int64, data []byte) {} func (sm *raft) propose(id int64, data []byte) {}
func (sm *raft) tick() {}