Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WASI support #583

Merged
merged 23 commits into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0ba3dfc
feat: WASI support
GregoryConrad May 21, 2023
4d065c9
Merge branch 'cberner:master' into master
GregoryConrad May 21, 2023
2d5b1b9
docs: change feature comment to be consistent with others
GregoryConrad May 22, 2023
85f0ce0
Merge branch 'cberner:master' into master
GregoryConrad May 24, 2023
abd5764
fix: improved WASI compilation warnings/errors
GregoryConrad May 24, 2023
f5f2f7e
fix: allow compiling tests for WASI
GregoryConrad May 24, 2023
58837ee
docs: clarify what some deps are for
GregoryConrad May 24, 2023
3891da5
test: fix some test failures due to tempfile
GregoryConrad May 25, 2023
1448cfc
revert: "test: fix some test failures due to tempfile"
GregoryConrad May 25, 2023
5388a42
test: update tests to use CWD as tmpdir
GregoryConrad May 25, 2023
fbfed53
test: fix remaining WASI tests
GregoryConrad May 25, 2023
420b1db
test: re-enable unused import warning
GregoryConrad May 26, 2023
b9adeda
ci: enable WASI testing in CI
GregoryConrad May 26, 2023
b776ed1
refactor: remove need for wasi feature
GregoryConrad May 26, 2023
3547ff1
refactor: wasi target clean ups
GregoryConrad May 26, 2023
509749b
ci: switch WASI test runner to macos
GregoryConrad May 27, 2023
c4271a3
Merge branch 'master' into master
GregoryConrad May 27, 2023
918f850
ci: manually specify wasi as target
GregoryConrad May 27, 2023
6698d99
test: fix up remaining tests for WASI
GregoryConrad May 27, 2023
c489ef5
ci: hopefully fix missing nightly toolchain
GregoryConrad May 27, 2023
d042b2b
style: fix some lints from nightly clippy
GregoryConrad May 27, 2023
8b6c296
test: remove unused import on wasi
GregoryConrad May 27, 2023
d32cefb
test: switch tests to use create_tempfile function
GregoryConrad May 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ libc = "0.2.104"
log = {version = "0.4.17", optional = true }
pyo3 = {version = "0.18.0", features=["extension-module", "abi3-py37"], optional = true }

[dev-dependencies]
[target.'cfg(not(target_os = "wasi"))'.dev-dependencies]
Copy link
Contributor Author

@GregoryConrad GregoryConrad May 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really hate this, but I don't think there is any other way to do benchmark specific dependencies (and you can't create a benching feature either because you can't have optional dev dependencies). If you have any ideas though, I am all ears.

(This divides test specific deps and benchmark specific deps, since essentially none of the benchmark deps compile to WASI)

ctrlc = "3.2.3"
fastrand = "1.9.0"
rand = "0.8"
lmdb-rkv = "0.14.0"
sanakirja = "1.3.3"
tempfile = "3.5.0"
sled = "0.34.7"
rocksdb = "0.21.0"
libc = "0.2.99"
comfy-table = "6.1.0"

[dev-dependencies]
rand = "0.8"
tempfile = "3.5.0"

[target.'cfg(target_os = "linux")'.dev-dependencies]
io-uring = "0.5.1"

Expand All @@ -43,6 +45,8 @@ python = ["pyo3"]
logging = ["log"]
# Enable cache hit metrics
cache_metrics = []
# Enable experimental wasi support (requires nightly and has no file locking!)
wasi = []

[profile.bench]
debug = true
Expand Down
2 changes: 1 addition & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ impl Builder {
/// Set the amount of memory (in bytes) used for caching data
pub fn set_cache_size(&mut self, bytes: usize) -> &mut Self {
// TODO: allow dynamic expansion of the read/write cache
self.read_cache_size_bytes = bytes * 9 / 10;
self.read_cache_size_bytes = bytes / 10 * 9;
GregoryConrad marked this conversation as resolved.
Show resolved Hide resolved
self.write_cache_size_bytes = bytes / 10;
self
}
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
clippy::cast_precision_loss,
clippy::cast_sign_loss
)]
// TODO remove these (and the "wasi" Cargo feature) once wasi no longer requires nightly
#![cfg_attr(feature = "wasi", feature(wasi_ext))]
#[cfg(all(target_os = "wasi", not(feature = "wasi")))]
compile_error!(
r"To compile to WASI, you must enable the 'wasi' feature for now!
See redb's Cargo.toml for why; this restriction will be lifted in the future."
);

pub use db::{
Builder, Database, MultimapTableDefinition, MultimapTableHandle, TableDefinition, TableHandle,
Expand Down
4 changes: 2 additions & 2 deletions src/tree_store/page_store/file_lock/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(unix)]
#[cfg(any(unix, target_os = "wasi"))]
mod unix;
#[cfg(unix)]
#[cfg(any(unix, target_os = "wasi"))]
pub(super) use unix::LockedFile;

#[cfg(windows)]
Expand Down
24 changes: 22 additions & 2 deletions src/tree_store/page_store/file_lock/unix.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
// TODO once Rust's libc has flock implemented for WASI, this file needs to be revisited.
// What needs to be changed is commented below.
// See also: https://github.com/WebAssembly/wasi-filesystem/issues/2

// Remove this line once wasi-libc has flock
#![cfg_attr(feature = "wasi", allow(unused_imports))]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This silences some unused import warnings that will go away once wasi-libc has flock


use crate::{Error, Result};
use std::fs::File;
use std::io;
use std::os::unix::fs::FileExt;
use std::os::unix::io::AsRawFd;

#[cfg(unix)]
use std::os::unix::{fs::FileExt, io::AsRawFd};

#[cfg(target_os = "wasi")]
use std::os::wasi::{fs::FileExt, io::AsRawFd};

pub(crate) struct LockedFile {
file: File,
}

impl LockedFile {
// This is a no-op until we get flock in wasi-libc.
// Delete this function when we get flock.
#[cfg(target_os = "wasi")]
pub(crate) fn new(file: File) -> Result<Self> {
Ok(Self { file })
}

#[cfg(unix)] // remove this line when wasi-libc gets flock
pub(crate) fn new(file: File) -> Result<Self> {
let fd = file.as_raw_fd();
let result = unsafe { libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) };
Expand Down Expand Up @@ -41,6 +60,7 @@ impl LockedFile {
}
}

#[cfg(unix)] // remove this line when wasi-libc gets flock
impl Drop for LockedFile {
fn drop(&mut self) {
unsafe { libc::flock(self.file.as_raw_fd(), libc::LOCK_UN) };
Expand Down