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

Implement wasi-keyvalue #8983

Merged
merged 3 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
43 changes: 43 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 @@ -737,6 +742,44 @@ 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:
name: Test wasi-keyvalue
runs-on: ubuntu-latest
needs: determine
if: needs.determine.outputs.run-wasi-keyvalue
# Setup redis server
services:
redis:
# Docker Hub image
image: redis
# Set health checks to wait until redis has started
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Maps port 6379 on service container to the host
- 6379:6379
steps:
- uses: actions/checkout@v4
with:
submodules: true
- uses: ./.github/actions/install-rust
# Install Rust targets
- run: rustup target add wasm32-wasi
- 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 @@ -195,6 +196,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 @@ -326,6 +328,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
51 changes: 51 additions & 0 deletions crates/test-programs/src/bin/keyvalue_main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use test_programs::keyvalue::wasi::keyvalue::{atomics, batch, store};

fn main() {
let identifier = std::env::var_os("IDENTIFIER")
.unwrap()
.into_string()
.unwrap();
let bucket = store::open(&identifier).unwrap();

if identifier != "" {
// for In-Memory provider, we have preset this data
assert_eq!(atomics::increment(&bucket, "atomics_key", 5).unwrap(), 5);
}
assert_eq!(atomics::increment(&bucket, "atomics_key", 1).unwrap(), 6);

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

bucket.set("hello", "world".as_bytes()).unwrap();

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

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

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