-
Notifications
You must be signed in to change notification settings - Fork 2
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
fix: supporting learners and added an interface for acquiring Learners #6
Conversation
@CodiumAI-Agent /review |
PR Review 🔍
Code feedback:
|
@CodiumAI-Agent /improve |
PR Code Suggestions ✨
|
@@ -903,6 +903,15 @@ butil::Status NodeImpl::list_peers(std::vector<PeerId>* peers) { | |||
return butil::Status::OK(); | |||
} | |||
|
|||
butil::Status NodeImpl::list_learners(std::vector<PeerId>* learners) { |
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.
如果是自己的代码,这里一般就会考虑做个简单的抽像,让list_peers和list_learner调用相同的代码。这里做抽像可能会有潜在的好处,比如合并官方代码时list_peers的实现方法变了就会产生冲突就不会导致 list_learners错过一些修改。
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.
好的
WalkthroughThe recent changes focus on enhancing the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Node
participant ConfigurationEntry
User ->> Node: list_peers()
Node ->> ConfigurationEntry: Get peers
ConfigurationEntry -->> Node: Return peers
Node -->> User: Return peers list
User ->> Node: list_learners()
Node ->> ConfigurationEntry: Get learners
ConfigurationEntry -->> Node: Return learners
Node -->> User: Return learners list
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
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.
Actionable comments posted: 3
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (4)
- src/braft/node.cpp (1 hunks)
- src/braft/node.h (1 hunks)
- src/braft/raft.cpp (2 hunks)
- src/braft/raft.h (2 hunks)
Additional comments not posted (4)
src/braft/node.h (1)
137-139
: The new method declarations forlist_members
,list_peers
, andlist_learners
are correctly implemented.src/braft/raft.h (1)
285-288
: The update to include "LEARNER" instate2str
function is correctly implemented and handles the new state appropriately.src/braft/node.cpp (2)
906-908
: The implementation oflist_peers
function is correct and concise.It directly utilizes the
list_members
function, passing the current configuration. This reuse of code enhances maintainability and prevents code duplication.
910-912
: Ensure correct configuration is used inlist_learners
.#!/bin/bash # Description: Verify that `_learner_conf` is correctly configured to list learners. # Test: Search for the configuration setup of `_learner_conf`. rg --type cpp '_learner_conf.conf ='This script checks if
_learner_conf
is set up elsewhere in the codebase to ensure it's intended to list learners and not peers, as indicated by previous comments.
butil::Status Node::list_learners(std::vector<PeerId>* learners) { | ||
return _impl->list_learners(learners); | ||
} |
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.
Add error handling for the list_learners
method.
- return _impl->list_learners(learners);
+ auto status = _impl->list_learners(learners);
+ if (!status.ok()) {
+ return status;
+ }
+ return butil::Status::OK();
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
butil::Status Node::list_learners(std::vector<PeerId>* learners) { | |
return _impl->list_learners(learners); | |
} | |
butil::Status Node::list_learners(std::vector<PeerId>* learners) { | |
auto status = _impl->list_learners(learners); | |
if (!status.ok()) { | |
return status; | |
} | |
return butil::Status::OK(); | |
} |
butil::Status NodeImpl::list_members(std::vector<PeerId>* peers, const ConfigurationEntry& conf) { | ||
BAIDU_SCOPED_LOCK(_mutex); | ||
if (_state != STATE_LEADER) { | ||
return butil::Status(EPERM, "Not leader"); | ||
} | ||
_conf.conf.list_peers(peers); | ||
conf.conf.list_peers(peers); |
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.
Consider adding error handling for the list_members
function.
- return butil::Status::OK();
+ if (!peers) {
+ return butil::Status(EINVAL, "Invalid argument: peers is nullptr");
+ }
+ return butil::Status::OK();
This change ensures that the function handles the case where the peers
pointer might be null, preventing potential segmentation faults or undefined behavior.
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
butil::Status NodeImpl::list_members(std::vector<PeerId>* peers, const ConfigurationEntry& conf) { | |
BAIDU_SCOPED_LOCK(_mutex); | |
if (_state != STATE_LEADER) { | |
return butil::Status(EPERM, "Not leader"); | |
} | |
_conf.conf.list_peers(peers); | |
conf.conf.list_peers(peers); | |
butil::Status NodeImpl::list_members(std::vector<PeerId>* peers, const ConfigurationEntry& conf) { | |
BAIDU_SCOPED_LOCK(_mutex); | |
if (_state != STATE_LEADER) { | |
return butil::Status(EPERM, "Not leader"); | |
} | |
if (!peers) { | |
return butil::Status(EINVAL, "Invalid argument: peers is nullptr"); | |
} | |
conf.conf.list_peers(peers); | |
return butil::Status::OK(); |
// list learner peers of this raft group, only leader retruns ok | ||
// [NOTE] when list_learner_peers concurrency with add_learner/remove_learner, maybe return learner_peers is staled. | ||
// because add_learner/remove_learner immediately modify configuration in memory | ||
butil::Status list_learners(std::vector<PeerId>* learners); | ||
|
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.
The addition of the list_learners
method is well-implemented. Consider adding error handling to manage potential issues during the listing process.
- butil::Status list_learners(std::vector<PeerId>* learners);
+ butil::Status list_learners(std::vector<PeerId>* learners) {
+ if (!is_leader()) {
+ return butil::Status(EPERM, "Only leader can list learners");
+ }
+ // existing logic here
+ }
Committable suggestion was skipped due to low confidence.
该 pr 需要在 OpenAtomFoundation/pikiwidb#342 之前合并
Summary by CodeRabbit
New Features
Improvements
state2str
function to include the "LEARNER" state, providing more accurate state representation.