Skip to content

Commit

Permalink
Fix test by generating random path for DB
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Shih authored and Brian Shih committed Jan 5, 2024
1 parent 1bb7401 commit 56477be
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 35 deletions.
12 changes: 12 additions & 0 deletions src/db/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
sync::{Arc, RwLock},
};

use rand::distributions::{Alphanumeric, DistString};
use serde::{de::DeserializeOwned, Serialize};
use std::panic;
use tokio::sync::mpsc;
Expand Down Expand Up @@ -83,6 +84,17 @@ impl DB {
}
}

pub fn new_random_path(initial_time: Timestamp) -> Self {
let string = Alphanumeric.sample_string(&mut rand::thread_rng(), 16);

DB {
db: Arc::new(InternalDB::new_cleaned(
&format!("./tmp/{}", string),
initial_time,
)),
}
}

pub fn new(path: &str, initial_time: Timestamp) -> Self {
DB {
db: Arc::new(InternalDB::new(path, initial_time)),
Expand Down
26 changes: 13 additions & 13 deletions src/db/db_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod test {

#[tokio::test]
async fn two_writes_with_different_keys() {
let db = Arc::new(DB::new_cleaned("./tmp/data", Timestamp::new(10)));
let db = Arc::new(DB::new_random_path(Timestamp::new(10)));

let key_a = "A";
let key_b = "B";
Expand Down Expand Up @@ -51,7 +51,7 @@ mod test {

#[tokio::test]
async fn read_waits_for_uncommitted_write() {
let db = Arc::new(DB::new_cleaned("./tmp/data", Timestamp::new(10)));
let db = Arc::new(DB::new_random_path(Timestamp::new(10)));
let write_txn = db.begin_txn().await;
let key = "foo";
db.write(key, 12, write_txn).await;
Expand Down Expand Up @@ -82,7 +82,7 @@ mod test {

#[tokio::test]
async fn ignores_intent_with_higher_timestamp() {
let db = Arc::new(DB::new_cleaned("./tmp/data", Timestamp::new(10)));
let db = Arc::new(DB::new_random_path(Timestamp::new(10)));
let read_txn = db.begin_txn().await;
let key = "foo";

Expand All @@ -109,7 +109,7 @@ mod test {

#[tokio::test]
async fn write_waits_for_uncommitted_write() {
let db = Arc::new(DB::new_cleaned("./tmp/data", Timestamp::new(10)));
let db = Arc::new(DB::new_random_path(Timestamp::new(10)));
let txn_1 = db.begin_txn().await;
let txn_2 = db.begin_txn().await;
let key = "baz";
Expand Down Expand Up @@ -156,7 +156,7 @@ mod test {

#[tokio::test]
async fn bump_write_timestamp_before_committing() {
let db = Arc::new(DB::new_cleaned("./tmp/data", Timestamp::new(10)));
let db = Arc::new(DB::new_random_path(Timestamp::new(10)));
let key = "foo";

// begin txn1
Expand Down Expand Up @@ -208,7 +208,7 @@ mod test {

#[tokio::test]
async fn bump_write_timestamp_before_committing() {
let db = Arc::new(DB::new_cleaned("./tmp/data", Timestamp::new(10)));
let db = Arc::new(DB::new_random_path(Timestamp::new(10)));
let key = "foo";

let write_txn = db.begin_txn().await;
Expand Down Expand Up @@ -249,7 +249,7 @@ mod test {

#[tokio::test]
async fn read_refresh_from_write_write_conflict() {
let db = Arc::new(DB::new_cleaned("./tmp/data", Timestamp::new(10)));
let db = Arc::new(DB::new_random_path(Timestamp::new(10)));

let read_key = "foo";
let write_key = "bar";
Expand Down Expand Up @@ -289,7 +289,7 @@ mod test {

#[tokio::test]
async fn read_refresh_failure() {
let db = Arc::new(DB::new_cleaned("./tmp/data", Timestamp::new(10)));
let db = Arc::new(DB::new_random_path(Timestamp::new(10)));

let read_key = "foo";
let write_key = "bar";
Expand Down Expand Up @@ -332,7 +332,7 @@ mod test {

#[tokio::test]
async fn read_write_after_abort_transaction() {
let db = DB::new_cleaned("./tmp/data", Timestamp::new(10));
let db = DB::new_random_path(Timestamp::new(10));
let key = "foo";

let txn_to_abort = db.begin_txn().await;
Expand Down Expand Up @@ -363,7 +363,7 @@ mod test {

#[tokio::test]
async fn conflicting_writes() {
let db = Arc::new(DB::new_cleaned("./tmp/data", Timestamp::new(10)));
let db = Arc::new(DB::new_random_path(Timestamp::new(10)));
let txn1 = db.begin_txn().await;
let txn2 = db.begin_txn().await;
println!("Txn1 is: {}", txn1);
Expand Down Expand Up @@ -472,7 +472,7 @@ mod test {

#[tokio::test]
async fn reading_its_txn_own_write() {
let db = DB::new_cleaned("./tmp/data", Timestamp::new(10));
let db = DB::new_random_path(Timestamp::new(10));
db.run_txn(|txn_context| async move {
let key = "foo";
txn_context.write(key, 12).await.unwrap();
Expand All @@ -484,7 +484,7 @@ mod test {

#[tokio::test]
async fn writing_its_own_write() {
let db = DB::new_cleaned("./tmp/data", Timestamp::new(10));
let db = DB::new_random_path(Timestamp::new(10));
db.run_txn(|txn_context| async move {
let key = "foo";
txn_context.write(key, 12).await;
Expand All @@ -497,7 +497,7 @@ mod test {

#[tokio::test]
async fn two_concurrent_writes_to_same_key() {
let db = Arc::new(DB::new_cleaned("./tmp/data", Timestamp::new(10)));
let db = Arc::new(DB::new_random_path(Timestamp::new(10)));

let db_1 = db.clone();
let key = "foo";
Expand Down
2 changes: 1 addition & 1 deletion src/lock_table/lock_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl LockTable {
txn_map: Arc::new(RwLock::new(HashMap::new())),
txn_wait_queue: TxnWaitQueue::new(
Arc::new(sender),
Arc::new(KVStore::new_cleaned("/tmp/foo")),
Arc::new(KVStore::new_random_path()),
),
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/storage/mvcc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rand::distributions::{Alphanumeric, DistString};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

Expand Down Expand Up @@ -53,6 +54,12 @@ impl KVStore {
}
}

pub fn new_random_path() -> Self {
let string = Alphanumeric.sample_string(&mut rand::thread_rng(), 16);

KVStore::new_cleaned(&format!("./tmp/{}", string))
}

pub fn new(path: &str) -> Self {
KVStore {
storage: Storage::new(path),
Expand Down
8 changes: 4 additions & 4 deletions src/storage/mvcc_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ mod tests {

#[test]
fn test_current_key_and_current_value() {
let storage = Storage::new_cleaned("./tmp/testt");
let storage = Storage::new_random_path();
let mvcc_key = MVCCKey::new(
str_to_key("hello"),
Timestamp {
Expand All @@ -190,7 +190,7 @@ mod tests {

#[test]
fn test_order_of_iteration_with_intent_timestamp() {
let storage = Storage::new_cleaned("./tmp/testt");
let storage = Storage::new_random_path();
let key = "hello";

let mvcc_key_2 = MVCCKey::new(
Expand Down Expand Up @@ -264,7 +264,7 @@ mod tests {

#[test]
fn test_multiple_timestamps_with_same_prefix() {
let storage = Storage::new_cleaned("./tmp/testt");
let storage = Storage::new_random_path();
let key = "foo";
let mvcc_key_1 = MVCCKey::new(
str_to_key(key),
Expand Down Expand Up @@ -315,7 +315,7 @@ mod tests {

#[test]
fn empty_db() {
let storage = Storage::new_cleaned("./tmp/testt");
let storage = Storage::new_random_path();
let key = "foo";
let mvcc_key_1 = MVCCKey::new(
str_to_key(key),
Expand Down
12 changes: 6 additions & 6 deletions src/storage/mvcc_scanner_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod tests {

#[test]
fn no_intent_and_no_end_key() {
let storage = Storage::new_cleaned("./tmp/test");
let storage = Storage::new_random_path();
let key = "foo";
let mvcc_key_1 = MVCCKey::new(
str_to_key(key),
Expand Down Expand Up @@ -63,7 +63,7 @@ mod tests {

#[test]
fn intent_found() {
let kv_store = KVStore::new_cleaned("./tmp/data");
let kv_store = KVStore::new_random_path();
let timestamp = Timestamp::new(12, 0);
let txn_id = Uuid::new_v4();
let txn = Txn::new_link(txn_id, timestamp);
Expand Down Expand Up @@ -113,7 +113,7 @@ mod tests {

#[test]
fn advances_to_next_key() {
let kv_store = KVStore::new_cleaned("./tmp/data");
let kv_store = KVStore::new_random_path();
let first_key = "apple";
let first_key_timestamp1 = Timestamp::new(2, 3);
let first_key_timestamp2 = Timestamp::new(3, 0);
Expand Down Expand Up @@ -155,7 +155,7 @@ mod tests {

#[test]
fn there_is_no_next_key() {
let kv_store = KVStore::new_cleaned("./tmp/data");
let kv_store = KVStore::new_random_path();
let iterator = MVCCIterator::new(&kv_store.storage, IterOptions { prefix: true });
let scanner_timestamp = Timestamp {
logical_time: 3,
Expand Down Expand Up @@ -191,7 +191,7 @@ mod tests {

#[test]
fn multiple_timestamps_for_same_keys() {
let kv_store = KVStore::new_cleaned("./tmp/data");
let kv_store = KVStore::new_random_path();

let scan_timestamp = Timestamp::new(12, 3);

Expand Down Expand Up @@ -255,7 +255,7 @@ mod tests {

#[test]
fn multiple_intents() {
let kv_store = KVStore::new_cleaned("./tmp/data");
let kv_store = KVStore::new_random_path();
let txn_id = Uuid::new_v4();
let transaction_timestamp = Timestamp::new(12, 0);
let txn = Txn::new_link(txn_id, transaction_timestamp);
Expand Down
14 changes: 7 additions & 7 deletions src/storage/mvcc_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod tests {

#[test]
fn create_pending_transaction_record() -> () {
let kv_store = KVStore::new_cleaned("./tmp/data");
let kv_store = KVStore::new_random_path();
let transaction_id = Uuid::new_v4();
let write_timestamp = Timestamp {
wall_time: 0,
Expand Down Expand Up @@ -44,7 +44,7 @@ mod tests {

#[test]
fn put_with_transaction() {
let kv_store = KVStore::new_cleaned("./tmp/data");
let kv_store = KVStore::new_random_path();
let key = "foo";
let txn1_id = Uuid::new_v4();

Expand All @@ -66,7 +66,7 @@ mod tests {

#[test]
fn write_intent_error() {
let kv_store = KVStore::new_cleaned("./tmp/data");
let kv_store = KVStore::new_random_path();
let key = "foo";
let txn1_id = Uuid::new_v4();

Expand Down Expand Up @@ -113,7 +113,7 @@ mod tests {

#[test]
fn get_key_with_multiple_timestamps() {
let kv_store = KVStore::new_cleaned("./tmp/data");
let kv_store = KVStore::new_random_path();
let read_timestamp = Timestamp::new(10, 10);

let key1 = str_to_key("apple");
Expand Down Expand Up @@ -159,7 +159,7 @@ mod tests {

#[test]
fn get_intent() {
let kv_store = KVStore::new_cleaned("./tmp/data");
let kv_store = KVStore::new_random_path();
let key = "foo";
let txn1_id = Uuid::new_v4();

Expand Down Expand Up @@ -212,7 +212,7 @@ mod tests {

#[test]
fn resolve_intent_with_higher_timestamp() {
let kv_store = KVStore::new_cleaned("./tmp/dataa");
let kv_store = KVStore::new_random_path();
let uncommitted_timestamp = Timestamp::new(10, 10);

let key = str_to_key("apple");
Expand Down Expand Up @@ -259,7 +259,7 @@ mod tests {

#[test]
fn resolve_intent_owned_by_another_txn() {
let kv_store = KVStore::new_cleaned("./tmp/dataa");
let kv_store = KVStore::new_random_path();
let uncommitted_timestamp = Timestamp::new(10, 10);

let key = str_to_key("apple");
Expand Down
13 changes: 9 additions & 4 deletions src/storage/storage.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{cmp::Ordering, iter::Peekable, path::Path};

use rand::distributions::{Alphanumeric, DistString};
use rocksdb::{ColumnFamily, DBIterator, DB};
use serde::{de::DeserializeOwned, Serialize};
use uuid::Uuid;
Expand Down Expand Up @@ -36,6 +37,11 @@ impl Storage {
Storage { db }
}

pub fn new_random_path() -> Storage {
let string = Alphanumeric.sample_string(&mut rand::thread_rng(), 16);
Storage::new_cleaned(&format!("./tmp/{}", string))
}

// path example: "./tmp/data";
pub fn new_cleaned(path: &str) -> Storage {
if Path::new(path).exists() {
Expand Down Expand Up @@ -228,7 +234,7 @@ mod Test {

#[test]
fn put_mvcc() {
let storage = Storage::new_cleaned("./tmp/foo");
let storage = Storage::new_random_path();
let mvcc_key = MVCCKey::new(
str_to_key("hello"),
Timestamp {
Expand All @@ -245,7 +251,6 @@ mod Test {
}

mod storage_order {


use crate::{
hlc::timestamp::Timestamp,
Expand All @@ -256,7 +261,7 @@ mod Test {

#[test]
fn check_order_no_intent() {
let storage = Storage::new_cleaned("./tmp/foo");
let storage = Storage::new_random_path();
let first_mvcc_key = MVCCKey::new(str_to_key("a"), Timestamp::new(1, 0));
let second_mvcc_key = MVCCKey::new(str_to_key("a"), Timestamp::new(2, 0));

Expand All @@ -278,7 +283,7 @@ mod Test {

#[test]
fn check_order_intent() {
let storage = Storage::new_cleaned("./tmp/foobars");
let storage = Storage::new_random_path();
let key = "a";
let intent_key = MVCCKey::create_intent_key_with_str(key);
let non_intent_key = MVCCKey::new(str_to_key(key), Timestamp::new(2, 0));
Expand Down

0 comments on commit 56477be

Please sign in to comment.