Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

m_nextIndex与m_matchIndex变量值相关疑问 #61

Closed
qq507770437 opened this issue May 11, 2024 · 4 comments
Closed

m_nextIndex与m_matchIndex变量值相关疑问 #61

qq507770437 opened this issue May 11, 2024 · 4 comments

Comments

@qq507770437
Copy link

// 这两个状态所有结点都在维护,易失
int m_commitIndex;
int m_lastApplied; // 已经汇报给状态机(上层应用)的log 的index

// 这两个状态是由leader来维护,易失 ,这两个部分在内容补充的部分也会再讲解
std::vector<int> m_nextIndex; // 这两个状态的下标1开始,因为通常commitIndex和lastApplied从0开始,应该是一个无效的index,因此下标从1开始
std::vector<int> m_matchIndex;

(在raft主要算法的初始化中)
大佬能帮我解释一下这四个变量吗,尤其初始下标为什么分别从0,1开始

Copy link

Message that will be displayed on users' first issue

@578223592
Copy link
Collaborator

@qq507770437
raft中:leader向follower同步日志的过程中,需要检验 :日志的匹配,即检查发送的前一条日志的index和term是否和leader一致。
代码层面的话就会出现-1操作,如果是0-1=-1,会出现问题,具体代码在:

auto nextIndex = m_nextIndex[server];
*preIndex = nextIndex - 1;
*preTerm = m_logs[getSlicesIndexFromLogIndex(*preIndex)].logterm();

对于m_nextIndex和m_matchIndex,其拥有如下特点:

  1. 只有leader维护这两个变量,或者说这两个变量只有在一个节点是leader身份的时候才有意义
  2. 分别的含义是:leader下次同步日志的时候需要从哪个日志开始发送;对应节点已经验证匹配到了哪条日志。

@578223592 578223592 changed the title 主要流程中的变量有点看不懂 m_nextIndex与m_matchIndex变量值相关疑问 May 11, 2024
@qq507770437
Copy link
Author

谢谢大佬

@578223592
Copy link
Collaborator

一起探讨,一起加油,有用的话欢迎点个star,也可以持续关注我的分享~~
我将这个issue关了,有问题的话欢迎再打开

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants