Skip to content

Commit

Permalink
add a callback register interface of Part (#2485)
Browse files Browse the repository at this point in the history
* add a callback register interface of Part

* add one space

* clang-format

* do another clang format

Co-authored-by: Doodle <13706157+critical27@users.noreply.github.com>
  • Loading branch information
liuyu85cn and critical27 authored Aug 20, 2021
1 parent a8a4e84 commit 35fddd9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/kvstore/Part.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,23 @@ void Part::setBlocking(bool sign) { blocking_ = sign; }
void Part::onLostLeadership(TermID term) { VLOG(1) << "Lost the leadership for the term " << term; }

void Part::onElected(TermID term) {
VLOG(1) << "Being elected as the leader for the term " << term;
VLOG(1) << "Being elected as the leader for the term: " << term;
if (onElectedCallBacks_.empty()) {
return;
}

CallbackOptions opt;
opt.spaceId = spaceId_;
opt.partId = partId_;
opt.term = term_;

for (auto& cb : onElectedCallBacks_) {
cb(opt);
}
}

void Part::registerOnElected(OnElectedCallBack cb) {
onElectedCallBacks_.emplace_back(std::move(cb));
}

void Part::onDiscoverNewLeader(HostAddr nLeader) {
Expand Down
11 changes: 11 additions & 0 deletions src/kvstore/Part.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,22 @@ class Part : public raftex::RaftPart {

nebula::cpp2::ErrorCode toResultCode(raftex::AppendLogResult res);

public:
struct CallbackOptions {
GraphSpaceID spaceId;
PartitionID partId;
TermID term;
};

using OnElectedCallBack = std::function<void(const CallbackOptions& opt)>;
void registerOnElected(OnElectedCallBack cb);

protected:
GraphSpaceID spaceId_;
PartitionID partId_;
std::string walPath_;
NewLeaderCallback newLeaderCb_ = nullptr;
std::vector<OnElectedCallBack> onElectedCallBacks_;

private:
KVEngine* engine_ = nullptr;
Expand Down

0 comments on commit 35fddd9

Please sign in to comment.