client-rust
client-rust copied to clipboard
BatchGet with tokio::spawn test error the trait `std::marker::Send` is not implemented for `dyn Iterator<Item = tikv_client::Key>`
test code
#[tokio::test]
async fn spawn_test(){
use crate::tikv::*;
use tikv_client::{Config, Key,TransactionClient as Client, Value};
tokio::spawn(async {
let config = Config::default();
let client = Client::new_with_config(vec![PD_ADDR], config).await.unwrap();
let tikv_client = TikvClient::new(vec![PD_ADDR.to_string()]).await;
let key1: Key = b"key1".to_vec().into();
let value1: Value = b"value1".to_vec();
let key2: Key = b"key2".to_vec().into();
let value2: Value = b"value2".to_vec();
let mut txn = client.begin_optimistic().await.unwrap();
// batch get
let result: HashMap<Key,Value> = txn
.batch_get(vec![key1,key2])
.await.unwrap()
.map(|pair| (pair.0, pair.1))
.collect();
txn.commit().await.unwrap();
print!("{:?}",result);
});
}
error:
error: future cannot be sent between threads safely
--> src/store/src/tikv/mod.rs:118:9
|
118 | tokio::spawn(async {
| ^^^^^^^^^^^^ future created by async block is not Send
|
= help: the trait std::marker::Send is not implemented for dyn Iterator<Item = tikv_client::Key>
note: future is not Send as this value is used across an await
--> /home/yuchao/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/tikv-client-0.1.0/src/transaction/buffer.rs:97:61
|
97 | let fetched_results = f(Box::new(undetermined_keys)).await?;
| --------------------------- ^^^^^^ await occurs here, with Box::new(undetermined_keys) maybe used later
| |
| has type Box<dyn Iterator<Item = tikv_client::Key>> which is not Send
note: Box::new(undetermined_keys) is later dropped here
--> /home/yuchao/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/tikv-client-0.1.0/src/transaction/buffer.rs:97:68
|
97 | let fetched_results = f(Box::new(undetermined_keys)).await?;
| ^
note: required by a bound in tokio::spawn
--> /home/yuchao/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/tokio-1.21.0/src/task/spawn.rs:127:21
|
127 | T: Future + Send + 'static,
| ^^^^ required by this bound in tokio::spawn
error: could not compile store due to previous error
Subject: Similar Issue and Potential Solution for TiKV Rust Client
Hello chaochaoyuyu19880105,
I noticed that you encountered a similar issue with the TiKV Rust client and wanted to provide some insights based on my experience. It seems that your problem is related to the following issue: link to the issue.
In my case, I found that using the master branch of the repository should resolve the issue. However, I encountered some dependency conflicts with raft and protobuf. I also noticed that the project is undergoing version updates, as indicated by this pull request: link to the pull request.
To address the issue, I forked the project and started from tag 0.1.0. I then applied the necessary fix and created a new tag 0.1.1. You can use this version in your Cargo.toml as follows:
[dependencies]
tikv-client = { git = "https://github.com/SwannHERRERA/client-rust", tag = "0.1.1" }
This approach worked for me and resolved the issue I was facing. I hope it helps you as well.