Skip to content

BatchGet with tokio::spawn test error the trait std::marker::Send is not implemented for dyn Iterator<Item = tikv_client::Key> #374

@tank-plus

Description

@tank-plus

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions