上一篇(L7)

下一篇(L9)

2021 L8:

老师使用的:间隔50ms检查timeout,超时是 1s 间隔 0~300ms(1+0 or 1+0.3);


type Raft struct {
	mu        sync.Mutex   //Lock to protect shared access to this peer's stat
	peers     []*ClientEnd //RPC end points of all peers
	persister *Persister   //Object to hold this peer's persisted state
	me        int          //this peer's index into peers[]
	dead      int32        //set by Kill()
	applyCh   chan ApplyMsg
	applyCond *sync.Cond
	state        Tstate
	electionTime time.Time

	//Look at the paper's Figure 2 for a description of what
	//state a Raft server must maintain.

	//Persistent state
	currentTerm int
	votedFor    int
	log         LogEntries

	//Volatile state
	commitIndex int
	lastApplied int

	//Leader state
	nextIndex  []int
	matchIndex []int

	//Snapshot state
	snapshot      []byte
	snapshotIndex int
	snapshotTerm  int

	//Temporary location to give the service snapshot to the
	//apply thread.
	waitingSnapshot []byte
	waitingIndex    int //lastIncludedIndex
	waitingTerm     int //lastIncludedTerm
}

编码习惯:如果函数假设调用者持有锁,则在函数名末尾加L

rpc请求响应后才加锁处理对应的数据。