Skip to content

Commit

Permalink
Handle RaftServiceClient.propose error
Browse files Browse the repository at this point in the history
  • Loading branch information
jopemachine committed Feb 1, 2024
1 parent 2ab6750 commit e1c5968
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
12 changes: 10 additions & 2 deletions raftify/proto/raft_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import "eraftpb.proto";
service RaftService {
rpc RequestId(RequestIdArgs) returns (RequestIdResponse) {}
rpc ChangeConfig(eraftpb.ConfChangeV2) returns (ChangeConfigResponse) {}
rpc Propose(ProposeArgs) returns (Empty) {}
rpc Propose(ProposeArgs) returns (ProposeResponse) {}
rpc SendMessage(eraftpb.Message) returns (Empty) {}
rpc GetPeers(Empty) returns (GetPeersResponse) {}
rpc DebugNode(Empty) returns (DebugNodeResponse) {}
rpc CreateSnapshot(Empty) returns (Empty) {}
}

// Common

message Empty {}

enum ResultCode {
Expand All @@ -27,13 +29,17 @@ message ProposeArgs {
bytes msg = 1;
}

message ProposeResponse {
bytes error = 1;
}

// Used in GetPeers

message GetPeersResponse {
string peers_json = 1;
}

// Used in Dynamic Member's Bootstrap
// Used in Dynamic Membership Change

message RequestIdArgs {
string raft_addr = 1;
Expand Down Expand Up @@ -65,6 +71,8 @@ message ChangeConfigResponse {
bytes error = 4; // Used in Handling error
}

// Debug Node

message DebugNodeResponse {
string result_json = 1;
}
16 changes: 12 additions & 4 deletions raftify/src/raft_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl RaftService for RaftServer {
async fn propose(
&self,
request: Request<raft_service::ProposeArgs>,
) -> Result<Response<raft_service::Empty>, Status> {
) -> Result<Response<raft_service::ProposeResponse>, Status> {
let request_args = request.into_inner();
let sender = self.snd.clone();

Expand All @@ -254,8 +254,14 @@ impl RaftService for RaftServer {
match response {
ServerResponseMsg::Propose { result } => {
match result {
ResponseResult::Success => Ok(Response::new(raft_service::Empty {})),
ResponseResult::Error(_) => Ok(Response::new(raft_service::Empty {})),
ResponseResult::Success => Ok(Response::new(raft_service::ProposeResponse {
..Default::default()
})),
ResponseResult::Error(error) => {
Ok(Response::new(raft_service::ProposeResponse {
error: error.to_string().as_bytes().to_vec(),
}))
}
ResponseResult::WrongLeader { leader_addr, .. } => {
// TODO: Handle this kind of errors
let mut client = create_client(leader_addr).await.unwrap();
Expand All @@ -265,7 +271,9 @@ impl RaftService for RaftServer {
})
.await?;

Ok(Response::new(raft_service::Empty {}))
Ok(Response::new(raft_service::ProposeResponse {
..Default::default()
}))
}
}
}
Expand Down

0 comments on commit e1c5968

Please sign in to comment.