Skip to content
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ authors = ["The TiKV Project Authors"]
repository = "https://github.com/tikv/client-rust"
description = "The Rust language implementation of TiKV client."
edition = "2021"
rust-version = "1.93.0"

[features]
default = ["prometheus"]
Expand Down
1 change: 1 addition & 0 deletions examples/raw.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018 TiKV Project Authors. Licensed under Apache-2.0.

#![type_length_limit = "8165158"]
#![allow(clippy::result_large_err)]

mod common;

Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.84.1"
channel = "1.93.0"
components = ["rustfmt", "clippy"]
67 changes: 66 additions & 1 deletion src/common/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub enum Error {
/// Wraps a `reqwest::Error`.
/// Wraps a `grpcio::Error`.
#[error("gRPC api error: {0}")]
GrpcAPI(#[from] tonic::Status),
GrpcAPI(tonic::Status),
/// Wraps a `grpcio::Error`.
#[error("url error: {0}")]
Url(#[from] tonic::codegen::http::uri::InvalidUri),
Expand Down Expand Up @@ -142,6 +142,12 @@ impl From<ProtoKeyError> for Error {
}
}

impl From<tonic::Status> for Error {
fn from(status: tonic::Status) -> Error {
Error::GrpcAPI(status)
}
}

/// A result holding an [`Error`](enum@Error).
pub type Result<T> = result::Result<T, Error>;

Expand All @@ -157,3 +163,62 @@ macro_rules! internal_err {
internal_err!(format!($f, $($arg),+))
});
}

#[cfg(test)]
mod test {
use tonic::Code;

use super::Error;
use super::ProtoKeyError;
use super::ProtoRegionError;

#[test]
fn from_tonic_status_produces_grpc_api_error() {
let status = tonic::Status::new(Code::Unavailable, "network unavailable");
let error: Error = status.clone().into();

let Error::GrpcAPI(actual_status) = error else {
panic!("expected Error::GrpcAPI");
};
assert_eq!(actual_status.code(), status.code());
assert_eq!(actual_status.message(), status.message());
}

#[test]
fn grpc_api_variant_accepts_tonic_status_payload() {
let error = Error::GrpcAPI(tonic::Status::new(Code::DeadlineExceeded, "timeout"));
let Error::GrpcAPI(status) = error else {
panic!("expected Error::GrpcAPI");
};
assert_eq!(status.code(), Code::DeadlineExceeded);
assert_eq!(status.message(), "timeout");
}

#[test]
fn from_proto_region_error_wraps_region_error() {
let region_error = ProtoRegionError {
message: "region stale".to_owned(),
..Default::default()
};
let error: Error = region_error.clone().into();

let Error::RegionError(actual_region_error) = error else {
panic!("expected Error::RegionError");
};
assert_eq!(actual_region_error.message, region_error.message);
}

#[test]
fn from_proto_key_error_wraps_key_error() {
let key_error = ProtoKeyError {
retryable: "retry later".to_owned(),
..Default::default()
};
let error: Error = key_error.clone().into();

let Error::KeyError(actual_key_error) = error else {
panic!("expected Error::KeyError");
};
assert_eq!(actual_key_error.retryable, key_error.retryable);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
//! ```

#![allow(clippy::field_reassign_with_default)]
#![allow(clippy::result_large_err)]

pub mod backoff;
#[doc(hidden)]
Expand Down
2 changes: 1 addition & 1 deletion src/pd/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl RetryClientTrait for RetryClient<Cluster> {
cluster
.get_all_stores(self.timeout)
.await
.map(|resp| resp.stores.into_iter().map(Into::into).collect())
.map(|resp| resp.stores)
})
}

Expand Down
3 changes: 3 additions & 0 deletions src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

pub use protos::*;

// Rust 1.93's clippy::all flags many protobuf-generated wire types as dead code.
// Prior toolchain (1.84.1) did not fail on these generated definitions.
#[allow(clippy::doc_lazy_continuation)]
#[allow(dead_code)]
mod protos {
include!("generated/mod.rs");
}
2 changes: 1 addition & 1 deletion src/store/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ macro_rules! impl_request {
.$fun(req)
.await
.map(|r| Box::new(r.into_inner()) as Box<dyn Any>)
.map_err(Error::GrpcAPI)
.map_err(Error::from)
}

fn label(&self) -> &'static str {
Expand Down
9 changes: 4 additions & 5 deletions src/transaction/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,8 @@ pub fn new_pessimistic_prewrite_request(
let len = mutations.len();
let mut req = new_prewrite_request(mutations, primary_lock, start_version, lock_ttl);
req.for_update_ts = for_update_ts;
req.pessimistic_actions = iter::repeat(PessimisticAction::DoPessimisticCheck.into())
.take(len)
.collect();
req.pessimistic_actions =
iter::repeat_n(PessimisticAction::DoPessimisticCheck.into(), len).collect();
req
}

Expand Down Expand Up @@ -343,7 +342,7 @@ impl Shardable for kvrpcpb::CommitRequest {
}

fn apply_shard(&mut self, shard: Self::Shard) {
self.keys = shard.into_iter().map(Into::into).collect();
self.keys = shard;
}

fn apply_store(&mut self, store: &RegionStore) -> Result<()> {
Expand Down Expand Up @@ -570,7 +569,7 @@ impl Merge<kvrpcpb::ScanLockResponse> for Collect {
fn merge(&self, input: Vec<Result<kvrpcpb::ScanLockResponse>>) -> Result<Self::Out> {
input
.into_iter()
.flat_map_ok(|mut resp| resp.take_locks().into_iter().map(Into::into))
.flat_map_ok(|mut resp| resp.take_locks().into_iter())
.collect()
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/transaction/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,7 @@ impl<PdC: PdClient> Transaction<PdC> {
.retry_multi_region(retry_options.region_backoff)
.merge(Collect)
.plan();
plan.execute()
.await
.map(|r| r.into_iter().map(Into::into).collect())
plan.execute().await.map(|r| r.into_iter().collect())
})
.await
.map(move |pairs| pairs.map(move |pair| pair.truncate_keyspace(keyspace)))
Expand Down Expand Up @@ -806,9 +804,7 @@ impl<PdC: PdClient> Transaction<PdC> {
.retry_multi_region(retry_options.region_backoff)
.merge(Collect)
.plan();
plan.execute()
.await
.map(|r| r.into_iter().map(Into::into).collect())
plan.execute().await.map(|r| r.into_iter().collect())
},
)
.await
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ async fn raw_write_million() -> Result<()> {
// test batch_scan
for batch_num in 1..4 {
let _ = client
.batch_scan(iter::repeat(vec![]..).take(batch_num), limit)
.batch_scan(iter::repeat_n(vec![].., batch_num), limit)
.await?;
// FIXME: `each_limit` parameter does no work as expected. It limits the
// entries on each region of each rangqe, instead of each range.
Expand Down