Skip to content

Commit df2f510

Browse files
committed
Cleaned up rust codebase
1 parent 8edcf1f commit df2f510

5 files changed

Lines changed: 44 additions & 53 deletions

File tree

src/checkpoint.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use std::sync::atomic::Ordering::Relaxed;
44
use std::sync::LazyLock;
55
use std::io::Write;
66
use std::ops::Range;
7-
use crate::{CHECKPOINT_FILE, PROGRESS_WORKERS};
7+
use crate::{CHECKPOINT_FILE, END_SEED, PROGRESS_WORKERS, START_SEED, WORKER_THREADS};
8+
use crate::misc::split_chunks;
89

910
static MAX_BUFFER: LazyLock<i32> = LazyLock::new(|| {
1011
2
@@ -24,6 +25,7 @@ pub fn write_checkpoints() -> Result<()> {
2425
});
2526
Ok(())
2627
}
27-
pub fn load_checkpoints() -> Result<Vec<Range<i32>>> {
28-
todo!()
28+
pub fn load_workloads() -> Result<Vec<Range<i32>>> {
29+
return Ok(split_chunks(*START_SEED..*END_SEED, *WORKER_THREADS));
30+
todo!("load workloads from checkpoints file")
2931
}

src/macros.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/main.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
mod macros;
21
mod algorithm;
32
mod generate_csv;
43
mod misc;
@@ -11,20 +10,17 @@ use crossbeam_channel::{bounded, Receiver, Sender};
1110
use lazy_static::lazy_static;
1211
use std::{
1312
time::{Duration, Instant},
14-
io::{Write, stdout},
13+
io::stdout,
1514
thread,
16-
sync::atomic::{
17-
AtomicI32
18-
}
15+
sync::atomic::AtomicI32
1916
};
2017
use crossterm::ExecutableCommand;
2118
use crossterm::terminal::{EnterAlternateScreen, LeaveAlternateScreen};
2219
use crate::{
23-
misc::split_chunks,
2420
metrics::write_metrics,
2521
threads::*
2622
};
27-
use crate::checkpoint::write_checkpoints;
23+
use crate::checkpoint::{load_workloads, write_checkpoints};
2824

2925
const STAR_COUNT: usize = 64;
3026
const REC_MULTIPLIER: f32 = 1.0;
@@ -39,8 +35,8 @@ lazy_static! {
3935
static ref END_SEED: i32 = env_int!("END_SEED", 10_000);
4036
static ref WORKER_THREADS: i32 = env_int!("WORKER_THREADS", 8);
4137
static ref WRITER_THREADS: i32 = env_int!("WRITER_THREADS", 4);
42-
static ref COMMIT_COUNT: i32 = env_int!("COMMIT_COUNT", 1000);
43-
static ref CHANNEL_SIZE: i32 = env_int!("CHANNEL_SIZE", 1000);
38+
static ref COMMIT_COUNT: usize = env_int!("COMMIT_COUNT", 1000) as usize;
39+
static ref CHANNEL_SIZE: usize = env_int!("CHANNEL_SIZE", 1000) as usize;
4440
static ref CHECKPOINT_FILE: String = env_str!("CHECKPOINT_FILE", "checkpoints.txt");
4541
static ref BENCHMARK: bool = env_int!("BENCHMARK", 0) == 1;
4642

@@ -53,20 +49,19 @@ lazy_static! {
5349
format!("postgres://{user}:{pass}@{netloc}:{port}/{db_name}?sslmode=disable")
5450
};
5551

56-
static ref MAX_BUFFER: i32 = *CHANNEL_SIZE + *COMMIT_COUNT * *WORKER_THREADS;
52+
static ref MAX_BUFFER: usize = *CHANNEL_SIZE + *COMMIT_COUNT * *WORKER_THREADS as usize;
5753
}
5854
fn main() {
5955
assert!(*START_SEED < *END_SEED);
6056
assert!(*WORKER_THREADS < *END_SEED);
6157
assert!(*WORKER_THREADS < MAX_WORKERS as i32);
6258

63-
59+
// capture start time for performance evaluation
6460
let start = Instant::now();
6561

6662
// Prepare thread resources
67-
let all_seeds = *START_SEED..*END_SEED;
68-
let workloads = split_chunks(all_seeds, *WORKER_THREADS);
69-
let (entry_sender, entry_reciever): (Sender<(String, String)>, Receiver<(String, String)>) = bounded(*CHANNEL_SIZE as usize);
63+
let workloads = load_workloads().unwrap();
64+
let (entry_sender, entry_reciever): (Sender<(String, String)>, Receiver<(String, String)>) = bounded(*CHANNEL_SIZE);
7065

7166
let mut work_handles = vec![];
7267
let mut commit_handles = vec![];
@@ -79,18 +74,17 @@ fn main() {
7974
}))
8075
}
8176
if !*BENCHMARK {
82-
let conf = ((*DB_STR).clone(), *COMMIT_COUNT);
8377
// Launch database threads
8478
for _ in 0..*WRITER_THREADS {
8579
let thread_receiver = entry_reciever.clone();
86-
let thread_conf = conf.clone();
8780
commit_handles.push(thread::spawn(move || {
88-
commit_thread(thread_receiver, thread_conf)
81+
commit_thread(thread_receiver)
8982
}))
9083
}
9184
}
9285
else {
9386
for _ in 0..*WRITER_THREADS {
87+
// launch dummy db threads that will void all results
9488
let thread_receiver = entry_reciever.clone();
9589
commit_handles.push(thread::spawn(move || {
9690
writer_sink(thread_receiver)

src/misc.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,24 @@ pub fn split_chunks(r: Range<i32>, chunks: i32) -> Vec<Range<i32>> {
1818
}
1919
out
2020
}
21+
#[macro_export]
22+
macro_rules! env_int {
23+
($var:expr) => {
24+
match std::env::var($var) {
25+
Ok(val) => val.parse(),
26+
Err(err) => Err(err)
27+
}
28+
};
29+
($var:expr, $default:expr) => {
30+
std::env::var($var).map(|e| e.parse::<i32>().unwrap_or($default)).unwrap_or($default)
31+
};
32+
}
33+
#[macro_export]
34+
macro_rules! env_str {
35+
($var:expr) => {
36+
std::env::var($var)
37+
};
38+
($var:expr, $default:expr) => {
39+
std::env::var($var).unwrap_or($default.to_string())
40+
}
41+
}

src/threads.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crossbeam_channel::{Receiver, RecvTimeoutError, Sender};
66
use postgres::{Client, NoTls};
77
use anyhow::Result;
88
use crate::generate_csv::gen_formatted;
9-
use crate::{COMMITTED_SEEDS, PROGRESS_WORKERS, REC_MULTIPLIER, STAR_COUNT};
9+
use crate::{COMMITTED_SEEDS, COMMIT_COUNT, DB_STR, PROGRESS_WORKERS, REC_MULTIPLIER, STAR_COUNT};
1010
use crate::misc::{COPY_PLANET, COPY_STAR};
1111

1212
pub fn worker_thread(seeds: Range<i32>, send: Sender<(String, String)>, id: usize) -> Result<()> {
@@ -18,24 +18,19 @@ pub fn worker_thread(seeds: Range<i32>, send: Sender<(String, String)>, id: usiz
1818
}
1919
Ok(())
2020
}
21-
pub fn commit_thread(rec: Receiver<(String, String)>, config: (String, i32)) -> Result<()> {
22-
let mut client = Client::connect(config.0.as_str(), NoTls)?;
23-
let commit_size = config.1 as usize;
21+
pub fn commit_thread(rec: Receiver<(String, String)>) -> Result<()> {
22+
let mut client = Client::connect(&*DB_STR.as_str(), NoTls)?;
2423

25-
loop {
26-
let mut batch: Vec<(String, String)> = Vec::with_capacity(commit_size);
27-
for _ in 0..commit_size {
24+
'outer: loop {
25+
let mut batch: Vec<(String, String)> = Vec::with_capacity(*COMMIT_COUNT);
26+
'inner: for i in 0..*COMMIT_COUNT {
2827
match rec.recv_timeout(Duration::new(1, 0)) {
2928
Ok(msg) => batch.push(msg),
3029
Err(RecvTimeoutError::Timeout) => panic!("commit_thread: recv_timeout reached - channel stall detected (>1s lull)"),
31-
Err(RecvTimeoutError::Disconnected) => break,
30+
Err(RecvTimeoutError::Disconnected) => if i == 0 {break 'outer} else {break 'inner},
3231
}
3332
}
3433

35-
if batch.is_empty() {
36-
break;
37-
}
38-
3934
let mut txn = client.transaction()?;
4035

4136
{

0 commit comments

Comments
 (0)