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

prevent PANIC when leader is None #450

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion src/common/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ pub enum Error {
RegionNotFoundInResponse { region_id: u64 },
/// No leader is found for the given id.
#[error("Leader of region {} is not found", region_id)]
LeaderNotFound { region_id: u64 },
LeaderOfRegionNotFound { region_id: u64 },
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggest not to change this error as some existed user codes will be using it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

There is no concept of "Leader of cluster", but "region leader" or "PD leader". so it would be a little confused.

At the same time, it seems that it is not very necessary to add a new error enum, unless you want to handle this specified error. I think use InternalError the same as existed code of PD would be enough.

#[error("Leader of cluster {} is not found", cluster_id)]
LeaderOfClusterNotFound { cluster_id: u64 },
/// Scan limit exceeds the maximum
#[error("Limit {} exceeds max scan limit {}", limit, max_limit)]
MaxScanLimitExceeded { limit: u32, max_limit: u32 },
Expand Down
10 changes: 6 additions & 4 deletions src/pd/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,16 +279,16 @@ impl Connection {
keyspacepb::keyspace_client::KeyspaceClient<Channel>,
pdpb::GetMembersResponse,
)> {
let previous_leader = previous.leader.as_ref().unwrap();
let previous_leader = previous.leader.as_ref();
let members = &previous.members;
let cluster_id = previous.header.as_ref().unwrap().cluster_id;

let mut resp = None;
// Try to connect to other members, then the previous leader.
'outer: for m in members
.iter()
.filter(|m| *m != previous_leader)
.chain(Some(previous_leader))
.filter(|m| !previous_leader.is_some_and(|pl| *m == pl))
.chain(previous_leader)
{
for ep in &m.client_urls {
match self.try_connect(ep.as_str(), cluster_id, timeout).await {
Expand All @@ -306,7 +306,9 @@ impl Connection {

// Then try to connect the PD cluster leader.
if let Some(resp) = resp {
let leader = resp.leader.as_ref().unwrap();
let leader = resp.leader.as_ref().ok_or(Error::LeaderOfClusterNotFound{
cluster_id,
})?;
for ep in &leader.client_urls {
if let Ok((client, keyspace_client, members)) =
self.try_connect(ep.as_str(), cluster_id, timeout).await
Expand Down
2 changes: 1 addition & 1 deletion src/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl RegionWithLeader {
self.leader
.as_ref()
.cloned()
.ok_or_else(|| Error::LeaderNotFound {
.ok_or_else(|| Error::LeaderOfRegionNotFound {
region_id: self.id(),
})
.map(|s| s.store_id)
Expand Down
2 changes: 1 addition & 1 deletion src/store/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ macro_rules! impl_request {

fn set_leader(&mut self, leader: &RegionWithLeader) -> Result<()> {
let ctx = self.context.get_or_insert(kvrpcpb::Context::default());
let leader_peer = leader.leader.as_ref().ok_or(Error::LeaderNotFound {
let leader_peer = leader.leader.as_ref().ok_or(Error::LeaderOfRegionNotFound {
region_id: leader.region.id,
})?;
ctx.region_id = leader.region.id;
Expand Down
Loading