Skip to content

Commit

Permalink
Implement wasi-keyvalue
Browse files Browse the repository at this point in the history
  • Loading branch information
iawia002 committed Jul 23, 2024
1 parent 542af68 commit 1ba5ad9
Show file tree
Hide file tree
Showing 22 changed files with 1,083 additions and 1 deletion.
34 changes: 34 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ jobs:
audit: ${{ steps.calculate.outputs.audit }}
preview1-adapter: ${{ steps.calculate.outputs.preview1-adapter }}
run-dwarf: ${{ steps.calculate.outputs.run-dwarf }}
run-wasi-keyvalue: ${{ steps.calculate.outputs.run-wasi-keyvalue }}
steps:
- uses: actions/checkout@v4
- id: calculate
Expand Down Expand Up @@ -241,6 +242,9 @@ jobs:
if grep -q debug names.log; then
echo run-dwarf=true >> $GITHUB_OUTPUT
fi
if grep -q wasi-keyvalue names.log; then
echo run-wasi-keyvalue=true >> $GITHUB_OUTPUT
fi
fi
matrix="$(node ./ci/build-test-matrix.js ./commits.log ./names.log $run_full)"
echo "test-matrix={\"include\":$(echo $matrix)}" >> $GITHUB_OUTPUT
Expand All @@ -256,6 +260,7 @@ jobs:
echo audit=true >> $GITHUB_OUTPUT
echo preview1-adapter=true >> $GITHUB_OUTPUT
echo run-dwarf=true >> $GITHUB_OUTPUT
echo run-wasi-keyvalue=true >> $GITHUB_OUTPUT
fi
# Build all documentation of Wasmtime, including the C API documentation,
Expand Down Expand Up @@ -729,6 +734,35 @@ jobs:
env:
GH_TOKEN: ${{ github.token }}

# Test the `wasmtime-wasi-keyvalue` crate. Split out from the main tests
# because it needs additional database service.
test_wasi_keyvalue:
strategy:
matrix:
os: [ "ubuntu-latest", "macOS-latest" ]
name: Test wasi-keyvalue on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
needs: determine
if: needs.determine.outputs.run-wasi-keyvalue
steps:
- uses: actions/checkout@v4
with:
submodules: true
- uses: ./.github/actions/install-rust
# Install Rust targets
- run: rustup target add wasm32-wasi
# Setup redis server
- uses: shogo82148/actions-setup-redis@v1
- run: cargo test --all-features -p wasmtime-wasi-keyvalue
env:
RUST_BACKTRACE: 1

# common logic to cancel the entire run if this job fails
- run: gh run cancel ${{ github.run_id }}
if: failure() && github.event_name != 'pull_request'
env:
GH_TOKEN: ${{ github.token }}

# Test the `wasmtime-fuzzing` crate. Split out from the main tests because
# `--all-features` brings in OCaml, which is a pain to get setup for all
# targets.
Expand Down
68 changes: 67 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ wasi-common = { workspace = true, default-features = true, features = ["exit"],
wasmtime-wasi = { workspace = true, default-features = true, optional = true }
wasmtime-wasi-nn = { workspace = true, optional = true }
wasmtime-wasi-runtime-config = { workspace = true, optional = true }
wasmtime-wasi-keyvalue = { workspace = true, optional = true }
wasmtime-wasi-threads = { workspace = true, optional = true }
wasmtime-wasi-http = { workspace = true, optional = true }
clap = { workspace = true }
Expand Down Expand Up @@ -194,6 +195,7 @@ wasmtime-wasi = { path = "crates/wasi", version = "24.0.0", default-features = f
wasmtime-wasi-http = { path = "crates/wasi-http", version = "=24.0.0", default-features = false }
wasmtime-wasi-nn = { path = "crates/wasi-nn", version = "24.0.0" }
wasmtime-wasi-runtime-config = { path = "crates/wasi-runtime-config", version = "24.0.0" }
wasmtime-wasi-keyvalue = { path = "crates/wasi-keyvalue", version = "24.0.0" }
wasmtime-wasi-threads = { path = "crates/wasi-threads", version = "24.0.0" }
wasmtime-component-util = { path = "crates/component-util", version = "=24.0.0" }
wasmtime-component-macro = { path = "crates/component-macro", version = "=24.0.0" }
Expand Down Expand Up @@ -324,6 +326,7 @@ criterion = { version = "0.5.0", default-features = false, features = ["html_rep
rustc-hash = "1.1.0"
libtest-mimic = "0.7.0"
semver = { version = "1.0.17", default-features = false }
redis = "0.25.4"

# =============================================================================
#
Expand Down
4 changes: 4 additions & 0 deletions ci/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#
# - wasm-spec-interpreter: brings in OCaml which is a pain to configure for all
# targets, tested as part of the wastime-fuzzing CI job.
#
# - wasmtime-wasi-keyvalue: additional database service dependencies, needs its
# own CI job.

cargo test \
--workspace \
Expand All @@ -20,4 +23,5 @@ cargo test \
--exclude wasmtime-wasi-nn \
--exclude wasmtime-fuzzing \
--exclude wasm-spec-interpreter \
--exclude wasmtime-wasi-keyvalue \
$@
2 changes: 2 additions & 0 deletions ci/vendor-wit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ make_vendor "wasi-http" "

make_vendor "wasi-runtime-config" "runtime-config@c667fe6"

make_vendor "wasi-keyvalue" "keyvalue@219ea36"

rm -rf $cache_dir

# Separately (for now), vendor the `wasi-nn` WIT files since their retrieval is
Expand Down
1 change: 1 addition & 0 deletions crates/test-programs/artifacts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ fn build_and_generate_tests() {
s if s.starts_with("piped_") => "piped",
s if s.starts_with("dwarf_") => "dwarf",
s if s.starts_with("runtime_config_") => "runtime_config",
s if s.starts_with("keyvalue_") => "keyvalue",
// If you're reading this because you hit this panic, either add it
// to a test suite above or add a new "suite". The purpose of the
// categorization above is to have a static assertion that tests
Expand Down
44 changes: 44 additions & 0 deletions crates/test-programs/src/bin/keyvalue_main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use test_programs::keyvalue::wasi::keyvalue::{atomics, batch, store};

fn main() {
let bucket = store::open(std::env::var_os("IDENTIFIER").unwrap().to_str().unwrap()).unwrap();
bucket.set("hello", "world".as_bytes()).unwrap();

let v = bucket.get("hello").unwrap();
assert_eq!(String::from_utf8(v.unwrap()).unwrap(), "world");

bucket.delete("hello").unwrap();
let exists = bucket.exists("hello").unwrap();
assert_eq!(exists, false);

bucket.set("aa", "bb".as_bytes()).unwrap();
let resp = bucket.list_keys(None).unwrap();
assert_eq!(resp.keys, vec!["aa".to_string()]);

assert_eq!(atomics::increment(&bucket, "atomics_key", 5).unwrap(), 5);
assert_eq!(atomics::increment(&bucket, "atomics_key", 1).unwrap(), 6);

batch::set_many(
&bucket,
&[
("a1".to_string(), "v1".as_bytes().to_vec()),
("b1".to_string(), "v1".as_bytes().to_vec()),
("c1".to_string(), "v1".as_bytes().to_vec()),
],
)
.unwrap();
batch::delete_many(&bucket, &["a1".to_string(), "c1".to_string()]).unwrap();
let values = batch::get_many(
&bucket,
&["a1".to_string(), "b1".to_string(), "c1".to_string()],
)
.unwrap();
assert_eq!(
values,
vec![
None,
Some(("b1".to_string(), "v1".as_bytes().to_vec())),
None
]
);
}
8 changes: 8 additions & 0 deletions crates/test-programs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ pub mod config {
world: "wasi:config/imports",
});
}

pub mod keyvalue {
wit_bindgen::generate!({
path: "../wasi-keyvalue/wit",
world: "wasi:keyvalue/imports",
type_section_suffix: "keyvalue",
});
}
26 changes: 26 additions & 0 deletions crates/wasi-keyvalue/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "wasmtime-wasi-keyvalue"
version.workspace = true
authors.workspace = true
edition.workspace = true
repository = "https://github.com/bytecodealliance/wasmtime"
license = "Apache-2.0 WITH LLVM-exception"
description = "Wasmtime implementation of the wasi-keyvalue API"

[lints]
workspace = true

[dependencies]
anyhow = { workspace = true }
wasmtime = { workspace = true, features = ["runtime", "async", "component-model"] }
async-trait = { workspace = true }
url = { workspace = true }
redis = { workspace = true, optional = true, features = ["tokio-comp"] }

[dev-dependencies]
test-programs-artifacts = { workspace = true }
wasmtime-wasi = { workspace = true }
tokio = { workspace = true, features = ["macros"] }

[features]
redis = ["dep:redis"]
Loading

0 comments on commit 1ba5ad9

Please sign in to comment.