-
Notifications
You must be signed in to change notification settings - Fork 63
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
feat: Implementing Redis-Raft commands to build a cluster. #136
feat: Implementing Redis-Raft commands to build a cluster. #136
Conversation
* feat: import braft * fix: fix braft.cmake * fix:fix import braft cmake * fix: fix fetchcontent_populate * fix: fix find gflags brpc braft cmake * add independent code and fix brpc * fix: delete unnecessary comments * fix: fix fetch content to ExternalProject_Add * fix: add counter.pb.h and counter.pb.cc --------- Co-authored-by: century <919745273@qq.com>
…ation#134) * feat: import braft * fix: fix braft.cmake * fix:fix import braft cmake * fix: fix fetchcontent_populate * fix: fix find gflags brpc braft cmake * add independent code and fix brpc * fix: delete unnecessary comments * fix: fix fetch content to ExternalProject_Add * fix: add counter.pb.h and counter.pb.cc * fix: fixed a bug where the compiler could not find glog --------- Co-authored-by: century <919745273@qq.com>
When implementing, I just pulled in the braft library directly. gotta roll back those changes I made to the cmake file for that.
交流一个问题:cmd_raft.h 放这里肯定没问题,raft.h/cc 是放这里合适还是放 storage 层更合适?on_apply 实际上是对rocksdb的写入,我个人是觉得放storage层或许更合适一点?(仅个人意见) |
好像 storage 确实更好一点,目前只有上层调用 raft 的逻辑。 |
src/praft/praft.h
Outdated
namespace pikiwidb { | ||
|
||
#define RAFT_DBID_LEN 32 | ||
#define RAFT_PORT_OFFSET 10 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用配置文件传入,不要使用固定的漂移量
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改成配置偏移量了
src/praft/praft.h
Outdated
PClient* client_ = nullptr; | ||
std::string peer_ip_; | ||
int port_ = 0; | ||
std::mutex mtx_; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
锁放在要保护的成员上面
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的
65e299c
into
OpenAtomFoundation:import-braft
…oundation#136) * feat: Implementing Redis-Raft commands to build a cluster Co-authored-by: panlei-coder <panleiCSDN@163.com> Co-authored-by: century <919745273@qq.com> Co-authored-by: panlei-coder <62509266+panlei-coder@users.noreply.github.com> Co-authored-by: alexstocks <alexstocks@foxmail.com> Co-authored-by: dingxiaoshuai123 <2486016589@qq.com> Co-authored-by: longfar <ningchangyuan@nuaa.edu.cn>
* feat:import braft (#130) * feat: Implementing Redis-Raft commands to build a cluster. (#136) Co-authored-by: panlei-coder <panleiCSDN@163.com> Co-authored-by: century <919745273@qq.com> Co-authored-by: panlei-coder <62509266+panlei-coder@users.noreply.github.com> Co-authored-by: alexstocks <alexstocks@foxmail.com> Co-authored-by: dingxiaoshuai123 <2486016589@qq.com> Co-authored-by: longfar <ningchangyuan@nuaa.edu.cn>
建集群需要实现的三条指令
RAFT.CLUSTER INIT <id>
RAFT.CLUSTER JOIN [addr:port]
RAFT.NODE ADD
指令(具体细节看这里Raft ),故也需要实现这条指令:RAFT.NODE ADD [id] [address:port]
# redis 中 join 指令的实现 ret = redisAsyncCommand(ConnGetRedisCtx(conn), handleNodeAddResponse, conn, "RAFT.NODE ADD %d %s:%u", rr->config.id, rr->config.addr.host, rr->config.addr.port);
主要改动
编译问题:
我直接将我目前的提交 rebase 到了潘磊的分支上,但 pikiwidb 可执行文件好像还没有链接 braft 库,需要进一步修改才能编译成功。
TARGET_LINK_LIBRARIES(pikiwidb net; dl; leveldb; fmt; pikiwidb-folly; storage; rocksdb;)
引入了四个文件:
指令实现
RAFT.CLUSTER INIT <id>
RAFT.NODE ADD [id] [address:port]
向集群中添加节点的操作。RAFT.CLUSTER JOIN [addr:port]
比较麻烦的一个指令,需要向 Leader 发送 ADD 请求。
参考现有的主从实现,会在收到指令时,建立一个连接,建立连接前,会将当前 client 指针等信息保存在 PRaft 中,用于稍后主动回复。
当前线程先不回复 client,所以把消息清空。
client->Clear();
连接建立成功后,会走到 g_pikiwidb->OnNewConnection(obj) 这里
最终在 OnConnect() 中调用 PRaft 的接口,向主节点发送 ADD 请求。
而 pikiwidb 收到消息时,会判断当前连接是否属于 Join 指令发起的连接,如果是,则处理对应的回复。并且从 PRaft 中获取之前发送 join 指令的 client,向其发送回复,这里指令流程才完整结束。