فهرست منبع

raft: add useful comments

These comments were things I learned about the units, use case or
meaning of various fields and functions in the raft package.
Brandon Philips 11 سال پیش
والد
کامیت
98f9ee3613
2فایلهای تغییر یافته به همراه10 افزوده شده و 2 حذف شده
  1. 7 1
      raft/node.go
  2. 3 1
      raft/raft.go

+ 7 - 1
raft/node.go

@@ -1,4 +1,3 @@
-// Package raft implements raft.
 package raft
 package raft
 
 
 import (
 import (
@@ -55,6 +54,8 @@ type Node struct {
 	done   chan struct{}
 	done   chan struct{}
 }
 }
 
 
+// Start returns a new Node given a unique raft id, a list of raft peers, and
+// the election and heartbeat timeouts in units of ticks.
 func Start(id int64, peers []int64, election, heartbeat int) Node {
 func Start(id int64, peers []int64, election, heartbeat int) Node {
 	n := newNode()
 	n := newNode()
 	r := newRaft(id, peers, election, heartbeat)
 	r := newRaft(id, peers, election, heartbeat)
@@ -62,6 +63,9 @@ func Start(id int64, peers []int64, election, heartbeat int) Node {
 	return n
 	return n
 }
 }
 
 
+// Restart is identical to Start but takes an initial State and a slice of
+// entries. Generally this is used when restarting from a stable storage
+// log.
 func Restart(id int64, peers []int64, election, heartbeat int, st pb.State, ents []pb.Entry) Node {
 func Restart(id int64, peers []int64, election, heartbeat int, st pb.State, ents []pb.Entry) Node {
 	n := newNode()
 	n := newNode()
 	r := newRaft(id, peers, election, heartbeat)
 	r := newRaft(id, peers, election, heartbeat)
@@ -131,6 +135,8 @@ func (n *Node) run(r *raft) {
 	}
 	}
 }
 }
 
 
+// Tick increments the internal logical clock for this Node. Election timeouts
+// and heartbeat timeouts are in units of ticks.
 func (n *Node) Tick() error {
 func (n *Node) Tick() error {
 	select {
 	select {
 	case n.tickc <- struct{}{}:
 	case n.tickc <- struct{}{}:

+ 3 - 1
raft/raft.go

@@ -108,7 +108,7 @@ type raft struct {
 	// the leader id
 	// the leader id
 	lead int64
 	lead int64
 
 
-	elapsed          int
+	elapsed          int // number of ticks since the last msg
 	heartbeatTimeout int
 	heartbeatTimeout int
 	electionTimeout  int
 	electionTimeout  int
 	tick             func()
 	tick             func()
@@ -258,6 +258,7 @@ func (r *raft) appendEntry(e pb.Entry) {
 	r.maybeCommit()
 	r.maybeCommit()
 }
 }
 
 
+// tickElection is ran by followers and candidates after r.electionTimeout.
 func (r *raft) tickElection() {
 func (r *raft) tickElection() {
 	r.elapsed++
 	r.elapsed++
 	// TODO (xiangli): elctionTimeout should be randomized.
 	// TODO (xiangli): elctionTimeout should be randomized.
@@ -267,6 +268,7 @@ func (r *raft) tickElection() {
 	}
 	}
 }
 }
 
 
+// tickHeartbeat is ran by leaders to send a msgBeat after r.heartbeatTimeout.
 func (r *raft) tickHeartbeat() {
 func (r *raft) tickHeartbeat() {
 	r.elapsed++
 	r.elapsed++
 	if r.elapsed > r.heartbeatTimeout {
 	if r.elapsed > r.heartbeatTimeout {