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

项目代码理解问题 #71

Closed
Lxxx11 opened this issue Jul 4, 2024 · 6 comments
Closed

项目代码理解问题 #71

Lxxx11 opened this issue Jul 4, 2024 · 6 comments

Comments

@Lxxx11
Copy link

Lxxx11 commented Jul 4, 2024

接收快照那块有些没看懂什么逻辑

sendRequestVote中

我知道m_nextIndex是leader下次同步日志的时候需要从哪个日志开始发送

但是这个for循环没理解啥意思 不应该存的是将要发送的日志下标吗 那数组长度不应该是peer节点的数量吗
​​​​​​​​​​​​ int lastLogIndex = getLastLogIndex();
for (int i = 0; i <m_nextIndex.size() ; i++) {
m_nextIndex[i] = lastLogIndex + 1 ;
m_matchIndex[i] = 0;
}

接收领导者的快照,没太看懂以下代码逻辑

auto lastLogIndex = getLastLogIndex();

if (lastLogIndex > args->lastsnapshotincludeindex()) {

m_logs.erase(m_logs.begin(), m_logs.begin() + getSlicesIndexFromLogIndex(args->lastsnapshotincludeindex()) + 1);

} else {

m_logs.clear();

}

doHeartBeat():没太看懂以下代码逻辑
if (preLogIndex != m_lastSnapshotIncludeIndex) {
for (int j = getSlicesIndexFromLogIndex(preLogIndex) + 1; j < m_logs.size(); ++j) {
mprrpc::LogEntry *sendEntryPtr = appendEntriesArgs->add_entries();
*sendEntryPtr = m_logs[j];
}
} else {
for (const auto& item: m_logs) {
mprrpc::LogEntry *sendEntryPtr = appendEntriesArgs->add_entries();
*sendEntryPtr = item;
}
}
int lastLogIndex = getLastLogIndex();
//构造返回值
const std::shared_ptrmprrpc::AppendEntriesReply appendEntriesReply = std::make_sharedmprrpc::AppendEntriesReply();
appendEntriesReply->set_appstate(Disconnected);
std::thread t(&Raft::sendAppendEntries, this, i, appendEntriesArgs, appendEntriesReply,
appendNums);
t.detach();
}

@578223592
Copy link
Collaborator

接收快照那块有些没看懂什么逻辑

sendRequestVote中

我知道m_nextIndex是leader下次同步日志的时候需要从哪个日志开始发送

但是这个for循环没理解啥意思 不应该存的是将要发送的日志下标吗 那数组长度不应该是peer节点的数量吗​​​​​​​​​​​​ int lastLogIndex = getLastLogIndex(); for (int i = 0; i <m_nextIndex.size() ; i++) { m_nextIndex[i] = lastLogIndex + 1 ; m_matchIndex[i] = 0; }

@Lxxx11
你这段话是两个问题:
不应该存的是将要发送的日志下标吗 ? A:存的就是发送日志的下标,你说的没错。
那数组长度不应该是peer节点的数量吗​​​​​​​​​​​​? A:m_nextIndex长度大于等于peer节点数量即可。我这里可能多申请了,写起来舒服(且内存影响不大),不影响具体执行逻辑。

@578223592
Copy link
Collaborator

@Lxxx11 剩下的问题我看都是代码逻辑不清晰,你可以先尝试问下GPT之类的东西。那种工具对于raft这种常见代码回答的会很准。

@Lxxx11
Copy link
Author

Lxxx11 commented Jul 7, 2024

doHeartBeat():
if (preLogIndex != m_lastSnapshotIncludeIndex) {
for (int j = getSlicesIndexFromLogIndex(preLogIndex) + 1; j < m_logs.size(); ++j) {
mprrpc::LogEntry *sendEntryPtr = appendEntriesArgs->add_entries();
*sendEntryPtr = m_logs[j];
}
} else {
for (const auto& item: m_logs) {
mprrpc::LogEntry *sendEntryPtr = appendEntriesArgs->add_entries();
*sendEntryPtr = item;
}
}
int lastLogIndex = getLastLogIndex();
//构造返回值
const std::shared_ptrmprrpc::AppendEntriesReply appendEntriesReply = std::make_sharedmprrpc::AppendEntriesReply();
appendEntriesReply->set_appstate(Disconnected);
std::thread t(&Raft::sendAppendEntries, this, i, appendEntriesArgs, appendEntriesReply,
appendNums);
t.detach();
}
这里放在gpt里面看的半懂不懂 可以简单解释一下吗 感谢感谢

@578223592
Copy link
Collaborator

doHeartBeat(): if (preLogIndex != m_lastSnapshotIncludeIndex) { for (int j = getSlicesIndexFromLogIndex(preLogIndex) + 1; j < m_logs.size(); ++j) { mprrpc::LogEntry *sendEntryPtr = appendEntriesArgs->add_entries(); *sendEntryPtr = m_logs[j]; } } else { for (const auto& item: m_logs) { mprrpc::LogEntry *sendEntryPtr = appendEntriesArgs->add_entries(); *sendEntryPtr = item; } } int lastLogIndex = getLastLogIndex(); //构造返回值 const std::shared_ptrmprrpc::AppendEntriesReply appendEntriesReply = std::make_sharedmprrpc::AppendEntriesReply(); appendEntriesReply->set_appstate(Disconnected); std::thread t(&Raft::sendAppendEntries, this, i, appendEntriesArgs, appendEntriesReply, appendNums); t.detach(); } 这里放在gpt里面看的半懂不懂 可以简单解释一下吗 感谢感谢

@Lxxx11 这块的代码写的确实很恶心,我记得当时是因为什么特殊情况所以这些写的。

doHeartBeat():
/**
构造发送的日志,preLogIndex == m_lastSnapshotIncludeIndex 表示第i个follower的日志只更新到了快照,则从快照后面的第一条日志开始发送。preLogIndex != m_lastSnapshotIncludeIndex 表示第i个follower的日志除了快照之外还有一些更新进度,则从最新的更新进度开始发送。
**/
if (preLogIndex != m_lastSnapshotIncludeIndex) {
for (int j = getSlicesIndexFromLogIndex(preLogIndex) + 1; j < m_logs.size(); ++j) {
mprrpc::LogEntry *sendEntryPtr = appendEntriesArgs->add_entries();
*sendEntryPtr = m_logs[j];
}
} else {
for (const auto& item: m_logs) {
mprrpc::LogEntry *sendEntryPtr = appendEntriesArgs->add_entries();
*sendEntryPtr = item;
}
}
int lastLogIndex = getLastLogIndex();
//构造返回值
const std::shared_ptrmprrpc::AppendEntriesReply appendEntriesReply = std::make_sharedmprrpc::AppendEntriesReply();
appendEntriesReply->set_appstate(Disconnected); //appstate的状态信息是无用的,这个说来话长,目前这么理解即可
std::thread t(&Raft::sendAppendEntries, this, i, appendEntriesArgs, appendEntriesReply,
appendNums); //开多线程执行
t.detach();
}

@578223592 578223592 changed the title 问的有点多 但是确实没有研究懂 非常感谢能够回答 项目代码理解问题 Jul 7, 2024
@Lxxx11
Copy link
Author

Lxxx11 commented Jul 7, 2024

明白了!感谢答复!

@578223592
Copy link
Collaborator

@Lxxx11 不客气,有帮助的话可以帮忙宣传下项目,有时间的话也可以提个pr改进下项目质量。我先关闭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