forked from scylladb/scylla-rust-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel-prepared.rs
61 lines (48 loc) · 1.69 KB
/
parallel-prepared.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use anyhow::Result;
use scylla::{Session, SessionBuilder};
use std::env;
use std::sync::Arc;
use tokio::sync::Semaphore;
#[tokio::main]
async fn main() -> Result<()> {
let uri = env::var("SCYLLA_URI").unwrap_or_else(|_| "127.0.0.1:9042".to_string());
println!("Connecting to {} ...", uri);
let session: Session = SessionBuilder::new().known_node(uri).build().await?;
let session = Arc::new(session);
session.query_unpaged("CREATE KEYSPACE IF NOT EXISTS examples_ks WITH REPLICATION = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : 1}", &[]).await?;
session
.query_unpaged(
"CREATE TABLE IF NOT EXISTS examples_ks.parallel_prepared (a int, b int, c text, primary key (a, b))",
&[],
)
.await?;
let prepared = Arc::new(
session
.prepare("INSERT INTO examples_ks.parallel_prepared (a, b, c) VALUES (?, ?, 'abc')")
.await?,
);
println!("Prepared statement: {:#?}", prepared);
let parallelism = 256;
let sem = Arc::new(Semaphore::new(parallelism));
for i in 0..100_000usize {
if i % 1000 == 0 {
println!("{}", i);
}
let session = session.clone();
let prepared = prepared.clone();
let permit = sem.clone().acquire_owned().await;
tokio::task::spawn(async move {
session
.execute_unpaged(&prepared, (i as i32, 2 * i as i32))
.await
.unwrap();
let _permit = permit;
});
}
// Wait for all in-flight requests to finish
for _ in 0..parallelism {
sem.acquire().await.unwrap().forget();
}
println!("Ok.");
Ok(())
}