Documentation ¶
Index ¶
- Constants
- type AppendEntriesArgs
- type AppendEntriesReply
- type ApplyMsg
- type InstallSnapshotArgs
- type InstallSnapshotReply
- type LogEntry
- type Raft
- func (rf *Raft) AppendEntries(args *AppendEntriesArgs, reply *AppendEntriesReply) error
- func (rf *Raft) CreateSnapshots(index int)
- func (rf *Raft) GetState() (int, bool)
- func (rf *Raft) InstallSnapshot(args *InstallSnapshotArgs, reply *InstallSnapshotReply) error
- func (rf *Raft) MakeRaftServer(peers []*rpc.Client)
- func (rf *Raft) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply) error
- func (rf *Raft) Start(command interface{}) (int, int, bool)
- type RequestVoteArgs
- type RequestVoteReply
Constants ¶
View Source
const ( Follower = iota Candidate Leader )
raft的三种角色
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AppendEntriesArgs ¶
type AppendEntriesArgs struct { Term int // leader的任期号 LeaderID uint64 // leaderID 便于进行重定向 PrevLogIndex int // 新日志之前日志的索引值 PrevLogTerm int // 新日志之前日志的Term Entries []LogEntry // 存储的日志条目 为空时是心跳包 LeaderCommit int // leader已经提交的日志的索引 }
同步日志,日志项为空时可当做心跳包
type AppendEntriesReply ¶
type InstallSnapshotArgs ¶
type InstallSnapshotReply ¶
type Raft ¶
type Raft struct { CurrentTerm int // 服务器最后一次知道的任期号(初始化为 0,持续递增) VotedFor uint64 // 在当前获得选票的候选人的 Id Logs []LogEntry // 日志条目集;每一个条目包含一个用户状态机执行的指令,和收到时的任期号 ConnectIsok *int32 // 参考RaftKV中的解释 // contains filtered or unexported fields }
func MakeRaftInit ¶
func MakeRaftInit(me uint64, persister *Persister.Persister, applyCh chan ApplyMsg, IsOk *int32, address *[]string) *Raft
* @brief: 用于创建一个raft实体
func (*Raft) AppendEntries ¶
func (rf *Raft) AppendEntries(args *AppendEntriesArgs, reply *AppendEntriesReply) error
AppendEntries定义了follower节点收到appendentries以后的处理逻辑
- 其实一共四种情况,就是follower日志多于leader,follower日志少于leader,follower日志等于leader(最新index处Term是否相同)
func (*Raft) CreateSnapshots ¶
* @brief: 用于在日志超过阈值时进行日志压缩,由kv层调用,index之前已经生成快照了 * @notes: 由raft的锁保护,不需要放在kvraft的临界区内
func (*Raft) InstallSnapshot ¶
func (rf *Raft) InstallSnapshot(args *InstallSnapshotArgs, reply *InstallSnapshotReply) error
func (*Raft) MakeRaftServer ¶
func (*Raft) RequestVote ¶
func (rf *Raft) RequestVote(args *RequestVoteArgs, reply *RequestVoteReply) error
RequestVote定义了Follower收到投票以后的处理逻辑
- 我们在这个函数中需要实现将请求者的日志和被请求者的日志作对比 如果当前节点的Term比候选者节点的Term大,拒绝投票
- 1.如果当前节点的Term比候选者节点的Term大,拒绝投票
- 2.如果当前节点的Term比候选者节点的Term小,那么当前节点转换为Follwer状态
- 3.判断是否已经投过票
- 4.比较最后一项日志的Term,也就是LastLogTerm,相同的话比较索引,也就是LastLogIndex,如果当前节点较新的话就不会投票,否则投票
type RequestVoteArgs ¶
type RequestVoteReply ¶
Click to show internal directories.
Click to hide internal directories.