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

add rpc to query raft status #3336

Merged
merged 7 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/interface/raftex.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ namespace cpp nebula.raftex

include "common.thrift"

enum Role {
LEADER = 1, // the leader
FOLLOWER = 2; // following a leader
CANDIDATE = 3; // Has sent AskForVote request
LEARNER = 4; // It is the same with FOLLOWER,
// except it does not participate in leader election
}
Copy link
Contributor

@critical27 critical27 Nov 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could add a (cpp.enum_strict) here and below


enum Status {
STARTING = 0; // The part is starting, not ready for service
RUNNING = 1; // The part is running
STOPPED = 2; // The part has been stopped
WAITING_SNAPSHOT = 3; // Waiting for the snapshot.
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These enum types have been defined in following source file, could you unify the usage of these enum types?

enum class Status {
STARTING = 0, // The part is starting, not ready for service
RUNNING, // The part is running
STOPPED, // The part has been stopped
WAITING_SNAPSHOT // Waiting for the snapshot.
};
enum class Role {
LEADER = 1, // the leader
FOLLOWER, // following a leader
CANDIDATE, // Has sent AskForVote request
LEARNER // It is the same with FOLLOWER,
// except it does not participate in leader election
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yixinglu updated, PTAL

enum ErrorCode {
SUCCEEDED = 0;

Expand Down Expand Up @@ -154,9 +169,26 @@ struct SendSnapshotResponse {
1: ErrorCode error_code;
}

struct GetStateRequest {
1: GraphSpaceID space; // Graphspace ID
2: PartitionID part; // Partition ID
}

struct GetStateResponse {
1: ErrorCode error_code;
2: Role role;
3: TermID term;
4: bool is_leader;
5: LogID committed_log_id;
6: LogID last_log_id;
7: TermID last_log_term;
8: Status status;
}

service RaftexService {
AskForVoteResponse askForVote(1: AskForVoteRequest req);
AppendLogResponse appendLog(1: AppendLogRequest req);
SendSnapshotResponse sendSnapshot(1: SendSnapshotRequest req);
HeartbeatResponse heartbeat(1: HeartbeatRequest req) (thread = 'eb');
GetStateResponse getState(1: GetStateRequest req);
}
12 changes: 12 additions & 0 deletions src/kvstore/raftex/RaftPart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,18 @@ bool RaftPart::prepareElectionRequest(cpp2::AskForVoteRequest& req,
return true;
}

void RaftPart::getState(cpp2::GetStateResponse& resp) {
std::lock_guard<std::mutex> g(raftLock_);
resp.set_term(term_);
resp.set_role(nebula::raftex::cpp2::Role(role_));
resp.set_is_leader(role_ == Role::LEADER);
resp.set_error_code(cpp2::ErrorCode::SUCCEEDED);
resp.set_committed_log_id(committedLogId_);
resp.set_last_log_id(lastLogId_);
resp.set_last_log_term(lastLogTerm_);
resp.set_status(nebula::raftex::cpp2::Status(status_));
}

typename RaftPart::Role RaftPart::processElectionResponses(
const RaftPart::ElectionResponses& results,
std::vector<std::shared_ptr<Host>> hosts,
Expand Down
2 changes: 2 additions & 0 deletions src/kvstore/raftex/RaftPart.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ class RaftPart : public std::enable_shared_from_this<RaftPart> {
* Methods to process incoming raft requests
*
****************************************************/
void getState(cpp2::GetStateResponse& resp);

// Process the incoming leader election request
void processAskForVoteRequest(const cpp2::AskForVoteRequest& req, cpp2::AskForVoteResponse& resp);

Expand Down
10 changes: 10 additions & 0 deletions src/kvstore/raftex/RaftexService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ std::shared_ptr<RaftPart> RaftexService::findPart(GraphSpaceID spaceId, Partitio
return it->second;
}

void RaftexService::getState(cpp2::GetStateResponse& resp, const cpp2::GetStateRequest& req) {
auto part = findPart(req.get_space(), req.get_part());
if (part != nullptr) {
part->getState(resp);
} else {
resp.set_term(-1);
resp.set_error_code(cpp2::ErrorCode::E_UNKNOWN_PART);
}
}

void RaftexService::askForVote(cpp2::AskForVoteResponse& resp, const cpp2::AskForVoteRequest& req) {
auto part = findPart(req.get_space(), req.get_part());
if (!part) {
Expand Down
2 changes: 2 additions & 0 deletions src/kvstore/raftex/RaftexService.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class RaftexService : public cpp2::RaftexServiceSvIf {

void askForVote(cpp2::AskForVoteResponse& resp, const cpp2::AskForVoteRequest& req) override;

void getState(cpp2::GetStateResponse& resp, const cpp2::GetStateRequest& req) override;

void appendLog(cpp2::AppendLogResponse& resp, const cpp2::AppendLogRequest& req) override;

void sendSnapshot(cpp2::SendSnapshotResponse& resp,
Expand Down