Skip to content

Commit 81a3528

Browse files
authored
Remove rusqlite dependency from crucible-common (#1659)
A rusqlite dependency was being dragged into crucible-common in order to have a `From` impl to CrucibleError - push that down into the downstairs so that the dependency can be removed.
1 parent 4ec8152 commit 81a3528

File tree

6 files changed

+66
-35
lines changed

6 files changed

+66
-35
lines changed

Cargo.lock

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ edition = "2021"
99
anyhow.workspace = true
1010
atty.workspace = true
1111
nix.workspace = true
12-
rusqlite.workspace = true
1312
rustls-pemfile.workspace = true
1413
schemars.workspace = true
1514
serde.workspace = true

common/src/lib.rs

-6
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,6 @@ impl From<anyhow::Error> for CrucibleError {
185185
}
186186
}
187187

188-
impl From<rusqlite::Error> for CrucibleError {
189-
fn from(e: rusqlite::Error) -> Self {
190-
CrucibleError::GenericError(format!("{:?}", e))
191-
}
192-
}
193-
194188
impl<T> From<std::sync::mpsc::SendError<T>> for CrucibleError {
195189
fn from(e: std::sync::mpsc::SendError<T>) -> Self {
196190
CrucibleError::GenericError(format!("{:?}", e))

downstairs/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ slog-dtrace.workspace = true
4545
slog-term.workspace = true
4646
slog.workspace = true
4747
statistical.workspace = true
48+
thiserror.workspace = true
4849
tokio-rustls.workspace = true
4950
tokio-util.workspace = true
5051
tokio.workspace = true

downstairs/src/extent_inner_sqlite.rs

+64-24
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ pub struct SqliteInner(std::sync::Mutex<SqliteMoreInner>);
2727

2828
impl ExtentInner for SqliteInner {
2929
fn gen_number(&self) -> Result<u64, CrucibleError> {
30-
self.0.lock().unwrap().gen_number()
30+
let v = self.0.lock().unwrap().gen_number()?;
31+
Ok(v)
3132
}
3233
fn flush_number(&self) -> Result<u64, CrucibleError> {
33-
self.0.lock().unwrap().flush_number()
34+
let v = self.0.lock().unwrap().flush_number()?;
35+
Ok(v)
3436
}
3537
fn dirty(&self) -> Result<bool, CrucibleError> {
36-
self.0.lock().unwrap().dirty()
38+
Ok(self.0.lock().unwrap().dirty())
3739
}
3840

3941
fn pre_flush(
@@ -42,7 +44,8 @@ impl ExtentInner for SqliteInner {
4244
new_gen: u64,
4345
job_id: JobOrReconciliationId,
4446
) -> Result<(), CrucibleError> {
45-
self.0.lock().unwrap().pre_flush(new_flush, new_gen, job_id)
47+
self.0.lock().unwrap().pre_flush(new_flush, new_gen, job_id);
48+
Ok(())
4649
}
4750

4851
fn flush_inner(
@@ -61,7 +64,8 @@ impl ExtentInner for SqliteInner {
6164
self.0
6265
.lock()
6366
.unwrap()
64-
.post_flush(new_flush, new_gen, job_id)
67+
.post_flush(new_flush, new_gen, job_id)?;
68+
Ok(())
6569
}
6670

6771
fn read(
@@ -85,7 +89,8 @@ impl ExtentInner for SqliteInner {
8589
write,
8690
only_write_unwritten,
8791
iov_max,
88-
)
92+
)?;
93+
Ok(())
8994
}
9095

9196
#[cfg(test)]
@@ -94,7 +99,8 @@ impl ExtentInner for SqliteInner {
9499
block: u64,
95100
count: u64,
96101
) -> Result<Vec<Option<DownstairsBlockContext>>, CrucibleError> {
97-
self.0.lock().unwrap().get_block_contexts(block, count)
102+
let bc = self.0.lock().unwrap().get_block_contexts(block, count)?;
103+
Ok(bc)
98104
}
99105

100106
#[cfg(test)]
@@ -158,7 +164,8 @@ impl SqliteInner {
158164
.unwrap()
159165
.truncate_encryption_contexts_and_hashes(
160166
extent_block_indexes_and_hashes,
161-
)
167+
)?;
168+
Ok(())
162169
}
163170
}
164171

@@ -186,8 +193,42 @@ struct SqliteMoreInner {
186193
dirty_blocks: BTreeMap<usize, Option<u64>>,
187194
}
188195

196+
// To avoid CrucibleError having a Rusqlite variant, use an "inner" error
197+
// variant and convert it to CrucibleError for the ExtentInner function
198+
// implementations.
199+
#[derive(thiserror::Error, Debug)]
200+
enum SqliteMoreInnerError {
201+
#[error("crucible error")]
202+
Crucible(#[from] CrucibleError),
203+
204+
#[error("rusqlite error")]
205+
Rusqlite(#[from] rusqlite::Error),
206+
207+
#[error("io error")]
208+
Io(#[from] std::io::Error),
209+
210+
#[error("anyhow error")]
211+
Anyhow(#[from] anyhow::Error),
212+
}
213+
214+
impl From<SqliteMoreInnerError> for CrucibleError {
215+
fn from(e: SqliteMoreInnerError) -> CrucibleError {
216+
match e {
217+
SqliteMoreInnerError::Crucible(e) => e,
218+
219+
SqliteMoreInnerError::Rusqlite(e) => {
220+
CrucibleError::GenericError(format!("{:?}", e))
221+
}
222+
223+
SqliteMoreInnerError::Io(e) => e.into(),
224+
225+
SqliteMoreInnerError::Anyhow(e) => e.into(),
226+
}
227+
}
228+
}
229+
189230
impl SqliteMoreInner {
190-
fn gen_number(&self) -> Result<u64, CrucibleError> {
231+
fn gen_number(&self) -> Result<u64, SqliteMoreInnerError> {
191232
let mut stmt = self.metadb.prepare_cached(
192233
"SELECT value FROM metadata where name='gen_number'",
193234
)?;
@@ -198,7 +239,7 @@ impl SqliteMoreInner {
198239
Ok(gen_number)
199240
}
200241

201-
fn flush_number(&self) -> Result<u64, CrucibleError> {
242+
fn flush_number(&self) -> Result<u64, SqliteMoreInnerError> {
202243
let mut stmt = self.metadb.prepare_cached(
203244
"SELECT value FROM metadata where name='flush_number'",
204245
)?;
@@ -209,24 +250,22 @@ impl SqliteMoreInner {
209250
Ok(flush_number)
210251
}
211252

212-
fn dirty(&self) -> Result<bool, CrucibleError> {
213-
Ok(self.dirty.get())
253+
fn dirty(&self) -> bool {
254+
self.dirty.get()
214255
}
215256

216257
fn pre_flush(
217258
&mut self,
218259
_new_flush: u64,
219260
_new_gen: u64,
220261
job_id: JobOrReconciliationId,
221-
) -> Result<(), CrucibleError> {
262+
) {
222263
// Used for profiling
223264
let n_dirty_blocks = self.dirty_blocks.len() as u64;
224265

225266
cdt::extent__flush__start!(|| {
226267
(job_id.get(), self.extent_number.0, n_dirty_blocks)
227268
});
228-
229-
Ok(())
230269
}
231270

232271
fn flush_inner(
@@ -262,7 +301,7 @@ impl SqliteMoreInner {
262301
new_flush: u64,
263302
new_gen: u64,
264303
job_id: JobOrReconciliationId,
265-
) -> Result<(), CrucibleError> {
304+
) -> Result<(), SqliteMoreInnerError> {
266305
// Clear old block contexts. In order to be crash consistent, only
267306
// perform this after the extent fsync is done. For each block
268307
// written since the last flush, remove all block context rows where
@@ -404,7 +443,7 @@ impl SqliteMoreInner {
404443
write: &ExtentWrite,
405444
only_write_unwritten: bool,
406445
_iov_max: usize,
407-
) -> Result<(), CrucibleError> {
446+
) -> Result<(), SqliteMoreInnerError> {
408447
check_input(self.extent_size, write.offset, write.data.len())?;
409448

410449
/*
@@ -622,7 +661,7 @@ impl SqliteMoreInner {
622661
&self,
623662
block: u64,
624663
count: u64,
625-
) -> Result<Vec<Option<DownstairsBlockContext>>, CrucibleError> {
664+
) -> Result<Vec<Option<DownstairsBlockContext>>, SqliteMoreInnerError> {
626665
let stmt =
627666
"SELECT block, hash, nonce, tag, on_disk_hash FROM block_context \
628667
WHERE block BETWEEN ?1 AND ?2";
@@ -695,7 +734,8 @@ impl SqliteMoreInner {
695734
"extent {}: incomplete read \
696735
(expected {block_size}, got {num_bytes})",
697736
self.extent_number
698-
)));
737+
))
738+
.into());
699739
}
700740
let hash = integrity_hash(&[&buffer]);
701741
known_hashes[i] = Some(hash);
@@ -964,7 +1004,7 @@ impl SqliteMoreInner {
9641004
Ok(out)
9651005
}
9661006

967-
fn set_dirty(&self) -> Result<()> {
1007+
fn set_dirty(&self) -> Result<(), SqliteMoreInnerError> {
9681008
if !self.dirty.get() {
9691009
let _ = self
9701010
.metadb
@@ -980,7 +1020,7 @@ impl SqliteMoreInner {
9801020
fn set_block_context(
9811021
&self,
9821022
block_context: &DownstairsBlockContext,
983-
) -> Result<(), CrucibleError> {
1023+
) -> Result<(), SqliteMoreInnerError> {
9841024
let stmt =
9851025
"INSERT OR IGNORE INTO block_context (block, hash, nonce, tag, on_disk_hash) \
9861026
VALUES (?1, ?2, ?3, ?4, ?5)";
@@ -1016,7 +1056,7 @@ impl SqliteMoreInner {
10161056
fn truncate_encryption_contexts_and_hashes(
10171057
&self,
10181058
extent_block_indexes_and_hashes: &[(usize, u64)],
1019-
) -> Result<()> {
1059+
) -> Result<(), SqliteMoreInnerError> {
10201060
let tx = self.metadb.unchecked_transaction()?;
10211061

10221062
self.truncate_encryption_contexts_and_hashes_with_tx(
@@ -1038,7 +1078,7 @@ impl SqliteMoreInner {
10381078
&self,
10391079
extent_block_indexes_and_hashes: impl ExactSizeIterator<Item = (usize, u64)>,
10401080
tx: &Transaction,
1041-
) -> Result<()> {
1081+
) -> Result<(), SqliteMoreInnerError> {
10421082
let n_blocks = extent_block_indexes_and_hashes.len();
10431083
cdt::extent__context__truncate__start!(|| n_blocks as u64);
10441084

@@ -1134,7 +1174,7 @@ impl SqliteMoreInner {
11341174
&mut self,
11351175
force_override_dirty: bool,
11361176
) -> Result<(), CrucibleError> {
1137-
if !force_override_dirty && !self.dirty()? {
1177+
if !force_override_dirty && !self.dirty() {
11381178
return Ok(());
11391179
}
11401180

workspace-hack/Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ futures-executor = { version = "0.3" }
3131
futures-sink = { version = "0.3" }
3232
futures-util = { version = "0.3", features = ["channel", "io", "sink"] }
3333
getrandom = { version = "0.2", default-features = false, features = ["std"] }
34-
hashbrown = { version = "0.14" }
3534
hex = { version = "0.4", features = ["serde"] }
3635
indexmap = { version = "2", features = ["serde"] }
3736
libc = { version = "0.2", features = ["extra_traits"] }
@@ -81,7 +80,6 @@ futures-executor = { version = "0.3" }
8180
futures-sink = { version = "0.3" }
8281
futures-util = { version = "0.3", features = ["channel", "io", "sink"] }
8382
getrandom = { version = "0.2", default-features = false, features = ["std"] }
84-
hashbrown = { version = "0.14" }
8583
hex = { version = "0.4", features = ["serde"] }
8684
indexmap = { version = "2", features = ["serde"] }
8785
libc = { version = "0.2", features = ["extra_traits"] }

0 commit comments

Comments
 (0)