Skip to content

Commit

Permalink
Merge pull request #6 from panlei-coder/fix_learner_bug
Browse files Browse the repository at this point in the history
fix: fixed a bug on supporting learners and added an interface for acquiring Learners
  • Loading branch information
AlexStocks authored Jun 13, 2024
2 parents d397128 + 7bd918f commit 3a9ef43
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/braft/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -894,15 +894,23 @@ void NodeImpl::unsafe_register_conf_change(const Configuration& old_conf,
return _conf_ctx.start(old_conf, new_conf, done);
}

butil::Status NodeImpl::list_peers(std::vector<PeerId>* 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");
}
_conf.conf.list_peers(peers);
conf.conf.list_peers(peers);
return butil::Status::OK();
}

butil::Status NodeImpl::list_peers(std::vector<PeerId>* peers) {
return list_members(peers, _conf);
}

butil::Status NodeImpl::list_learners(std::vector<PeerId>* learners) {
return list_members(learners, _learner_conf);
}

void NodeImpl::add_peer(const PeerId& peer, Closure* done) {
BAIDU_SCOPED_LOCK(_mutex);
Configuration new_conf = _conf.conf;
Expand Down
4 changes: 3 additions & 1 deletion src/braft/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,10 @@ friend class VoteBallotCtx;
// Otherwise we will specifit the error and call it.
//
void apply(const Task& task);


butil::Status list_members(std::vector<PeerId>* peers, const ConfigurationEntry& conf);
butil::Status list_peers(std::vector<PeerId>* peers);
butil::Status list_learners(std::vector<PeerId>* learners);

// @Node configuration change
void add_peer(const PeerId& peer, Closure* done);
Expand Down
5 changes: 5 additions & 0 deletions src/braft/raft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <unistd.h>
#include <butil/string_printf.h>
#include <butil/class_name.h>
#include <vector>
#include "braft/raft.h"
#include "braft/node.h"
#include "braft/storage.h"
Expand Down Expand Up @@ -186,6 +187,10 @@ butil::Status Node::list_peers(std::vector<PeerId>* peers) {
return _impl->list_peers(peers);
}

butil::Status Node::list_learners(std::vector<PeerId>* learners) {
return _impl->list_learners(learners);
}

void Node::add_peer(const PeerId& peer, Closure* done) {
_impl->add_peer(peer, done);
}
Expand Down
9 changes: 7 additions & 2 deletions src/braft/raft.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,10 @@ enum State {
};

inline const char* state2str(State state) {
const char* str[] = {"LEADER", "TRANSFERRING", "CANDIDATE", "FOLLOWER",
const char* str[] = {"LEARNER", "LEADER", "TRANSFERRING", "CANDIDATE", "FOLLOWER",
"ERROR", "UNINITIALIZED", "SHUTTING", "SHUTDOWN", };
if (state < STATE_END) {
return str[(int)state - 1];
return str[(int)state];
} else {
return "UNKNOWN";
}
Expand Down Expand Up @@ -689,6 +689,11 @@ class Node {
// because add_peer/remove_peer immediately modify configuration in memory
butil::Status list_peers(std::vector<PeerId>* peers);

// 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);

// Add a new peer to the raft group. done->Run() would be invoked after this
// operation finishes, describing the detailed result.
void add_peer(const PeerId& peer, Closure* done);
Expand Down

0 comments on commit 3a9ef43

Please sign in to comment.