Skip to content

Commit 5818632

Browse files
committed
Use spawn_blocking
1 parent 29085e2 commit 5818632

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

downstairs/src/region.rs

+22-18
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,8 @@ impl Region {
959959
}
960960
};
961961

962-
self.flush_extents(&dirty_extents, flush_number, gen_number, job_id)?;
962+
self.flush_extents(&dirty_extents, flush_number, gen_number, job_id)
963+
.await?;
963964
cdt::os__flush__done!(|| job_id.0);
964965

965966
// Now everything has succeeded, we can remove these extents from the
@@ -1039,7 +1040,8 @@ impl Region {
10391040
}
10401041

10411042
#[cfg(not(feature = "omicron-build"))]
1042-
fn flush_extents(
1043+
#[allow(clippy::unused_async)]
1044+
async fn flush_extents(
10431045
&mut self,
10441046
dirty_extents: &BTreeSet<usize>,
10451047
flush_number: u64,
@@ -1107,7 +1109,7 @@ impl Region {
11071109
/// region is backed by a ZFS dataset. Issue a _FIOFFS call, which
11081110
/// will result in a `zfs_sync` to the entire region dataset.
11091111
#[cfg(feature = "omicron-build")]
1110-
fn flush_extents(
1112+
async fn flush_extents(
11111113
&mut self,
11121114
dirty_extents: &BTreeSet<usize>,
11131115
flush_number: u64,
@@ -1137,28 +1139,30 @@ impl Region {
11371139
// Send the ioctl!
11381140
use std::os::fd::AsRawFd;
11391141
// Open the region's mountpoint
1140-
let file = File::open(&self.dir)?;
1142+
let dir = self.dir.clone();
11411143
// "file system flush", defined in illumos' sys/filio.h
11421144
const FIOFFS_MAGIC: u8 = b'f';
11431145
const FIOFFS_TYPE_MODE: u8 = 66;
11441146
nix::ioctl_none!(zfs_fioffs, FIOFFS_MAGIC, FIOFFS_TYPE_MODE);
11451147

11461148
// Do the flush in a worker thread if possible, to avoid blocking the
11471149
// main tokio thread pool.
1148-
let f = || unsafe { zfs_fioffs(file.as_raw_fd()) };
1149-
let rc = if matches!(
1150-
tokio::runtime::Handle::try_current().map(|r| r.runtime_flavor()),
1151-
Ok(tokio::runtime::RuntimeFlavor::MultiThread)
1152-
) {
1153-
tokio::task::block_in_place(f)
1154-
} else {
1155-
f()
1156-
};
1157-
1158-
if let Err(e) = rc {
1159-
let e: std::io::Error = e.into();
1160-
return Err(CrucibleError::from(e));
1161-
}
1150+
let handle = tokio::task::spawn_blocking(
1151+
move || -> Result<(), CrucibleError> {
1152+
let file = File::open(&dir)?;
1153+
let rc = unsafe {
1154+
zfs_fioffs(file.as_raw_fd())
1155+
};
1156+
if let Err(e) = rc {
1157+
let e: std::io::Error = e.into();
1158+
Err(CrucibleError::from(e))
1159+
} else {
1160+
Ok(())
1161+
}
1162+
},
1163+
);
1164+
// Wait for the async work to finish
1165+
handle.await.expect("could not join spawned task")?;
11621166

11631167
// After the bits have been committed to durable storage, execute any
11641168
// post flush routine that needs to happen

0 commit comments

Comments
 (0)