Skip to content

Commit

Permalink
fix set_context
Browse files Browse the repository at this point in the history
Signed-off-by: Ping Yu <[email protected]>
  • Loading branch information
pingyu committed Aug 28, 2023
1 parent 12b217d commit 19f829d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/raw/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl Shardable for kvrpcpb::RawBatchPutRequest {
}

fn apply_shard(&mut self, shard: Self::Shard, store: &RegionStore) -> Result<()> {
self.context = Some(store.region_with_leader.context()?);
self.set_context(store.region_with_leader.context()?);
self.pairs = shard;
Ok(())
}
Expand Down Expand Up @@ -293,7 +293,7 @@ impl Shardable for kvrpcpb::RawBatchScanRequest {
}

fn apply_shard(&mut self, shard: Self::Shard, store: &RegionStore) -> Result<()> {
self.context = Some(store.region_with_leader.context()?);
self.set_context(store.region_with_leader.context()?);
self.ranges = shard;
Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions src/request/shard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ macro_rules! shardable_key {
mut shard: Self::Shard,
store: &$crate::store::RegionStore,
) -> $crate::Result<()> {
self.context = Some(store.region_with_leader.context()?);
self.set_context(store.region_with_leader.context()?);
assert!(shard.len() == 1);
self.key = shard.pop().unwrap();
Ok(())
Expand Down Expand Up @@ -196,7 +196,7 @@ macro_rules! shardable_keys {
shard: Self::Shard,
store: &$crate::store::RegionStore,
) -> $crate::Result<()> {
self.context = Some(store.region_with_leader.context()?);
self.set_context(store.region_with_leader.context()?);
self.keys = shard.into_iter().map(Into::into).collect();
Ok(())
}
Expand Down Expand Up @@ -225,7 +225,7 @@ macro_rules! shardable_range {
shard: Self::Shard,
store: &$crate::store::RegionStore,
) -> $crate::Result<()> {
self.context = Some(store.region_with_leader.context()?);
self.set_context(store.region_with_leader.context()?);

self.start_key = shard.0.into();
self.end_key = shard.1.into();
Expand Down
8 changes: 8 additions & 0 deletions src/store/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub trait Request: Any + Sync + Send + 'static {
) -> Result<Box<dyn Any>>;
fn label(&self) -> &'static str;
fn as_any(&self) -> &dyn Any;
/// Set the context for the request.
/// Should always use `set_context` other than modify the `self.context` directly.
fn set_context(&mut self, context: kvrpcpb::Context);
fn set_api_version(&mut self, api_version: kvrpcpb::ApiVersion);
}
Expand Down Expand Up @@ -53,7 +55,13 @@ macro_rules! impl_request {
}

fn set_context(&mut self, context: kvrpcpb::Context) {
let api_version = self
.context
.as_ref()
.map(|c| c.api_version)
.unwrap_or_default();
self.context = Some(context);
self.set_api_version(kvrpcpb::ApiVersion::from_i32(api_version).unwrap());
}

fn set_api_version(&mut self, api_version: kvrpcpb::ApiVersion) {
Expand Down
13 changes: 7 additions & 6 deletions src/transaction/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use crate::shardable_range;
use crate::store::store_stream_for_keys;
use crate::store::store_stream_for_range;
use crate::store::RegionStore;
use crate::store::Request;
use crate::timestamp::TimestampExt;
use crate::transaction::HasLocks;
use crate::util::iter::FlatMapOkIterExt;
Expand Down Expand Up @@ -294,7 +295,7 @@ impl Shardable for kvrpcpb::PrewriteRequest {
}

fn apply_shard(&mut self, shard: Self::Shard, store: &RegionStore) -> Result<()> {
self.context = Some(store.region_with_leader.context()?);
self.set_context(store.region_with_leader.context()?);

// Only need to set secondary keys if we're sending the primary key.
if self.use_async_commit && !self.mutations.iter().any(|m| m.key == self.primary_lock) {
Expand Down Expand Up @@ -361,7 +362,7 @@ impl Shardable for kvrpcpb::CommitRequest {
}

fn apply_shard(&mut self, shard: Self::Shard, store: &RegionStore) -> Result<()> {
self.context = Some(store.region_with_leader.context()?);
self.set_context(store.region_with_leader.context()?);
self.keys = shard.into_iter().map(Into::into).collect();
Ok(())
}
Expand Down Expand Up @@ -452,7 +453,7 @@ impl Shardable for kvrpcpb::PessimisticLockRequest {
}

fn apply_shard(&mut self, shard: Self::Shard, store: &RegionStore) -> Result<()> {
self.context = Some(store.region_with_leader.context()?);
self.set_context(store.region_with_leader.context()?);
self.mutations = shard;
Ok(())
}
Expand Down Expand Up @@ -553,7 +554,7 @@ impl Shardable for kvrpcpb::ScanLockRequest {
}

fn apply_shard(&mut self, shard: Self::Shard, store: &RegionStore) -> Result<()> {
self.context = Some(store.region_with_leader.context()?);
self.set_context(store.region_with_leader.context()?);
self.start_key = shard.0;
Ok(())
}
Expand Down Expand Up @@ -614,7 +615,7 @@ impl Shardable for kvrpcpb::TxnHeartBeatRequest {
}

fn apply_shard(&mut self, mut shard: Self::Shard, store: &RegionStore) -> Result<()> {
self.context = Some(store.region_with_leader.context()?);
self.set_context(store.region_with_leader.context()?);
assert!(shard.len() == 1);
self.primary_lock = shard.pop().unwrap();
Ok(())
Expand Down Expand Up @@ -672,7 +673,7 @@ impl Shardable for kvrpcpb::CheckTxnStatusRequest {
}

fn apply_shard(&mut self, mut shard: Self::Shard, store: &RegionStore) -> Result<()> {
self.context = Some(store.region_with_leader.context()?);
self.set_context(store.region_with_leader.context()?);
assert!(shard.len() == 1);
self.primary_key = shard.pop().unwrap();
Ok(())
Expand Down

0 comments on commit 19f829d

Please sign in to comment.