Skip to content
Open
42 changes: 42 additions & 0 deletions src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ pub struct MockPdClient {
#[new(default)]
region_for_key_hook:
Option<Arc<dyn Fn(&Key) -> Result<RegionWithLeader> + Send + Sync + 'static>>,
/// Optional override for `region_for_id`, e.g. after a Region is removed.
#[new(default)]
region_for_id_hook:
Option<Arc<dyn Fn(RegionId) -> Result<RegionWithLeader> + Send + Sync + 'static>>,
/// Optional override for `prev_region_for_key`.
#[new(default)]
prev_region_for_key_hook:
Option<Arc<dyn Fn(&Key) -> Result<RegionWithLeader> + Send + Sync + 'static>>,
/// Optional observer/override for leader cache updates.
#[new(default)]
update_leader_hook:
Expand Down Expand Up @@ -140,6 +148,22 @@ impl MockPdClient {
self
}

pub fn with_prev_region_for_key_hook<F>(mut self, hook: F) -> MockPdClient
where
F: Fn(&Key) -> Result<RegionWithLeader> + Send + Sync + 'static,
{
self.prev_region_for_key_hook = Some(Arc::new(hook));
self
}

pub fn with_region_for_id_hook<F>(mut self, hook: F) -> MockPdClient
where
F: Fn(RegionId) -> Result<RegionWithLeader> + Send + Sync + 'static,
{
self.region_for_id_hook = Some(Arc::new(hook));
self
}

pub fn with_update_leader_hook<F>(mut self, hook: F) -> MockPdClient
where
F: Fn(RegionVerId, metapb::Peer) -> Result<()> + Send + Sync + 'static,
Expand Down Expand Up @@ -241,7 +265,25 @@ impl PdClient for MockPdClient {
Ok(region)
}

async fn prev_region_for_key(&self, key: &Key) -> Result<RegionWithLeader> {
if let Some(hook) = &self.prev_region_for_key_hook {
return hook(key);
}
let region = self.region_for_key(key).await?;
match region.id() {
1 => Err(Error::RegionForKeyNotFound { key: key.to_vec() }),
2 => Ok(Self::region1()),
3 => Ok(Self::region2()),
_ => Err(Error::RegionNotFoundInResponse {
region_id: region.id(),
}),
}
}

async fn region_for_id(&self, id: RegionId) -> Result<RegionWithLeader> {
if let Some(hook) = &self.region_for_id_hook {
return hook(id);
}
match id {
1 => Ok(Self::region1()),
2 => Ok(Self::region2()),
Expand Down
17 changes: 17 additions & 0 deletions src/pd/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ pub trait PdClient: Send + Sync + 'static {
/// In transactional API, the key and returned region are both decoded (keys in raw format).
async fn region_for_key(&self, key: &Key) -> Result<RegionWithLeader>;

/// Return the region immediately before the region containing `key`.
///
/// This is used by reverse scans when they reach a region boundary.
async fn prev_region_for_key(&self, key: &Key) -> Result<RegionWithLeader>;

/// In transactional API, the returned region is decoded (keys in raw format)
async fn region_for_id(&self, id: RegionId) -> Result<RegionWithLeader>;

Expand Down Expand Up @@ -242,6 +247,18 @@ impl<KvC: KvConnect + Send + Sync + 'static> PdClient for PdRpcClient<KvC> {
Self::decode_region(region, enable_codec)
}

async fn prev_region_for_key(&self, key: &Key) -> Result<RegionWithLeader> {
let enable_codec = self.enable_codec;
let key = if enable_codec {
key.to_encoded()
} else {
key.clone()
};

let region = self.region_cache.get_prev_region_by_key(&key).await?;
Self::decode_region(region, enable_codec)
}

async fn region_for_id(&self, id: RegionId) -> Result<RegionWithLeader> {
let region = self.region_cache.get_region_by_id(id).await?;
Self::decode_region(region, self.enable_codec)
Expand Down
42 changes: 41 additions & 1 deletion src/pd/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ macro_rules! pd_request {
}};
}

enum RegionRequestKind {
Current,
Previous,
}

// These methods make a single attempt to make a request.
impl Cluster {
pub async fn get_region(
Expand All @@ -50,7 +55,42 @@ impl Cluster {
) -> Result<pdpb::GetRegionResponse> {
let mut req = pd_request!(self.id, pdpb::GetRegionRequest);
req.region_key = key;
req.send(&mut self.client, timeout).await
self.send_region_request(req, timeout, RegionRequestKind::Current)
.await
}

pub async fn get_prev_region(
&mut self,
key: Vec<u8>,
timeout: Duration,
) -> Result<pdpb::GetRegionResponse> {
let mut req = pd_request!(self.id, pdpb::GetRegionRequest);
req.region_key = key;
self.send_region_request(req, timeout, RegionRequestKind::Previous)
.await
}

async fn send_region_request(
&mut self,
request: pdpb::GetRegionRequest,
timeout: Duration,
kind: RegionRequestKind,
) -> Result<pdpb::GetRegionResponse> {
let mut req = request.into_request();
req.set_timeout(timeout);
let response = match kind {
RegionRequestKind::Current => self.client.get_region(req).await?,
RegionRequestKind::Previous => self.client.get_prev_region(req).await?,
}
.into_inner();
if let Some(err) = &response
.header
.as_ref()
.and_then(|header| header.error.as_ref())
{
return Err(internal_err!(err.message.clone()));
}
Ok(response)
}

pub async fn get_region_by_id(
Expand Down
16 changes: 16 additions & 0 deletions src/pd/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub trait RetryClientTrait {
// It does not know about encoding. Caller should take care of it.
async fn get_region(self: Arc<Self>, key: Vec<u8>) -> Result<RegionWithLeader>;

async fn get_prev_region(self: Arc<Self>, key: Vec<u8>) -> Result<RegionWithLeader>;

async fn get_region_by_id(self: Arc<Self>, region_id: RegionId) -> Result<RegionWithLeader>;

async fn get_store(self: Arc<Self>, id: StoreId) -> Result<metapb::Store>;
Expand Down Expand Up @@ -159,6 +161,20 @@ impl RetryClientTrait for RetryClient<Cluster> {
})
}

async fn get_prev_region(self: Arc<Self>, key: Vec<u8>) -> Result<RegionWithLeader> {
retry_mut!(self, "get_prev_region", |cluster| {
let key = key.clone();
async {
cluster
.get_prev_region(key.clone(), self.timeout)
.await
.and_then(|resp| {
region_from_response(resp, || Error::RegionForKeyNotFound { key })
})
}
})
}

async fn get_region_by_id(self: Arc<Self>, region_id: RegionId) -> Result<RegionWithLeader> {
retry_mut!(self, "get_region_by_id", |cluster| async {
cluster
Expand Down
Loading
Loading