Skip to content

Commit a3e3b2e

Browse files
committed
impl: instance pool based on global state
- `KvsBuilder` is initializing KVS for given set of params. - Future instances must be requested with same set of params.
1 parent ae13754 commit a3e3b2e

File tree

17 files changed

+847
-806
lines changed

17 files changed

+847
-806
lines changed

src/rust/rust_kvs/examples/basic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn main() -> Result<(), ErrorCode> {
1919
// Build KVS instance for given instance ID and temporary directory.
2020
// `kvs_load` is explicitly set to `KvsLoad::Optional`, but this is the default value.
2121
// KVS files are not required.
22-
let builder = KvsBuilder::<Kvs>::new(instance_id.clone())
22+
let builder = KvsBuilder::new(instance_id.clone())
2323
.dir(dir_string.clone())
2424
.kvs_load(KvsLoad::Optional);
2525
let kvs = builder.build()?;
@@ -64,7 +64,7 @@ fn main() -> Result<(), ErrorCode> {
6464
{
6565
// Build KVS instance for given instance ID and temporary directory.
6666
// `kvs_load` is set to `KvsLoad::Required` - KVS files must already exist from previous KVS instance.
67-
let builder = KvsBuilder::<Kvs>::new(instance_id)
67+
let builder = KvsBuilder::new(instance_id)
6868
.dir(dir_string)
6969
.kvs_load(KvsLoad::Required);
7070
let kvs = builder.build()?;

src/rust/rust_kvs/examples/defaults.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn main() -> Result<(), ErrorCode> {
4343

4444
// Build KVS instance for given instance ID and temporary directory.
4545
// `defaults` is set to `Defaults::Required` - defaults are required.
46-
let builder = KvsBuilder::<Kvs>::new(instance_id)
46+
let builder = KvsBuilder::new(instance_id)
4747
.dir(dir_string)
4848
.defaults(Defaults::Required);
4949
let kvs = builder.build()?;

src/rust/rust_kvs/examples/flush.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn main() -> Result<(), ErrorCode> {
3333

3434
{
3535
// Build KVS instance for given instance ID and temporary directory.
36-
let builder = KvsBuilder::<Kvs>::new(instance_id.clone()).dir(dir_string.clone());
36+
let builder = KvsBuilder::new(instance_id.clone()).dir(dir_string.clone());
3737
let mut kvs = builder.build()?;
3838

3939
// Disable flush on exit.
@@ -54,7 +54,7 @@ fn main() -> Result<(), ErrorCode> {
5454

5555
{
5656
// Build KVS instance for given instance ID and temporary directory.
57-
let builder = KvsBuilder::<Kvs>::new(instance_id.clone()).dir(dir_string.clone());
57+
let builder = KvsBuilder::new(instance_id.clone()).dir(dir_string.clone());
5858
let mut kvs = builder.build()?;
5959

6060
// Explicitly enable flush on exit - this is the default.
@@ -75,7 +75,7 @@ fn main() -> Result<(), ErrorCode> {
7575

7676
{
7777
// Build KVS instance for given instance ID and temporary directory.
78-
let builder = KvsBuilder::<Kvs>::new(instance_id.clone()).dir(dir_string.clone());
78+
let builder = KvsBuilder::new(instance_id.clone()).dir(dir_string.clone());
7979
let mut kvs = builder.build()?;
8080

8181
// Disable flush on exit.
@@ -91,7 +91,7 @@ fn main() -> Result<(), ErrorCode> {
9191

9292
{
9393
// Build KVS instance to check current state.
94-
let builder = KvsBuilder::<Kvs>::new(instance_id.clone())
94+
let builder = KvsBuilder::new(instance_id.clone())
9595
.dir(dir_string)
9696
.kvs_load(KvsLoad::Required);
9797
let mut kvs = builder.build()?;

src/rust/rust_kvs/examples/snapshots.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn main() -> Result<(), ErrorCode> {
1717
println!("-> `snapshot_count` and `snapshot_max_count` usage");
1818

1919
// Build KVS instance for given instance ID and temporary directory.
20-
let builder = KvsBuilder::<Kvs>::new(instance_id.clone()).dir(dir_string.clone());
20+
let builder = KvsBuilder::new(instance_id.clone()).dir(dir_string.clone());
2121
let mut kvs = builder.build()?;
2222
kvs.set_flush_on_exit(FlushOnExit::No);
2323

@@ -39,7 +39,7 @@ fn main() -> Result<(), ErrorCode> {
3939
println!("-> `snapshot_restore` usage");
4040

4141
// Build KVS instance for given instance ID and temporary directory.
42-
let builder = KvsBuilder::<Kvs>::new(instance_id.clone()).dir(dir_string.clone());
42+
let builder = KvsBuilder::new(instance_id.clone()).dir(dir_string.clone());
4343
let mut kvs = builder.build()?;
4444
kvs.set_flush_on_exit(FlushOnExit::No);
4545

@@ -52,7 +52,7 @@ fn main() -> Result<(), ErrorCode> {
5252

5353
// Print current counter value, then restore oldest snapshot.
5454
println!("{counter_key} = {:?}", kvs.get_value(counter_key)?);
55-
kvs.snapshot_restore(SnapshotId(2))?;
55+
kvs.snapshot_restore(&SnapshotId(2))?;
5656
println!("{counter_key} = {:?}", kvs.get_value(counter_key)?);
5757

5858
println!();

src/rust/rust_kvs/src/error_code.rs

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ extern crate alloc;
1414
use alloc::string::FromUtf8Error;
1515
use core::array::TryFromSliceError;
1616

17-
use crate::kvs_value::KvsValue;
18-
use std::collections::HashMap;
19-
use std::sync::{MutexGuard, PoisonError};
20-
2117
/// Runtime Error Codes
2218
#[derive(Debug, PartialEq)]
2319
pub enum ErrorCode {
@@ -75,11 +71,17 @@ pub enum ErrorCode {
7571
/// Invalid snapshot ID
7672
InvalidSnapshotId,
7773

74+
/// Invalid instance ID
75+
InvalidInstanceId,
76+
7877
/// Conversion failed
7978
ConversionFailed,
8079

8180
/// Mutex failed
8281
MutexLockFailed,
82+
83+
/// Instance parameters mismatch
84+
InstanceParametersMismatch,
8385
}
8486

8587
impl From<std::io::Error> for ErrorCode {
@@ -116,21 +118,10 @@ impl From<Vec<u8>> for ErrorCode {
116118
}
117119
}
118120

119-
impl From<PoisonError<MutexGuard<'_, HashMap<std::string::String, KvsValue>>>> for ErrorCode {
120-
fn from(cause: PoisonError<MutexGuard<'_, HashMap<std::string::String, KvsValue>>>) -> Self {
121-
eprintln!("error: Mutex locking failed: {cause:#?}");
122-
ErrorCode::MutexLockFailed
123-
}
124-
}
125-
126121
#[cfg(test)]
127122
mod error_code_tests {
128123
use crate::error_code::ErrorCode;
129-
use crate::kvs_value::KvsValue;
130-
use std::collections::HashMap;
131124
use std::io::{Error, ErrorKind};
132-
use std::sync::{Arc, Mutex};
133-
use std::thread;
134125

135126
#[test]
136127
fn test_from_io_error_to_file_not_found() {
@@ -165,20 +156,4 @@ mod error_code_tests {
165156
let bytes: Vec<u8> = vec![];
166157
assert_eq!(ErrorCode::from(bytes), ErrorCode::ConversionFailed);
167158
}
168-
169-
#[test]
170-
fn test_from_poison_error_mutex_lock_failed() {
171-
let mutex: Arc<Mutex<HashMap<String, KvsValue>>> = Arc::default();
172-
173-
// test from: https://doc.rust-lang.org/std/sync/struct.PoisonError.html
174-
let c_mutex = Arc::clone(&mutex);
175-
let _ = thread::spawn(move || {
176-
let _unused = c_mutex.lock().unwrap();
177-
panic!();
178-
})
179-
.join();
180-
181-
let error = mutex.lock().unwrap_err();
182-
assert_eq!(ErrorCode::from(error), ErrorCode::MutexLockFailed);
183-
}
184159
}

0 commit comments

Comments
 (0)