Skip to content

Commit

Permalink
smdk(wasi): add build flags for println/panic enabled (#3978)
Browse files Browse the repository at this point in the history
* smdk(wasi): add build flags for println/panic enabled

smkd build/test/load accepts --wasi flags, or set
export SMDK_WASI=true
build build/test/load

* add(smartengine): enable wasi by default

* fix(smartengine): output errors to log

* chore: bump fluvio-smartengine patch version
  • Loading branch information
digikata authored Apr 29, 2024
1 parent 91fb2f2 commit 8061420
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

10 changes: 10 additions & 0 deletions crates/cargo-builder/src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ impl PackageInfo {
Ok(path)
}

/// path to package's wasm32 target
pub fn target_wasm32_wasi_path(&self) -> anyhow::Result<PathBuf> {
let mut path = self.target_dir.clone();
path.push("wasm32-wasi");
path.push(&self.profile);
path.push(self.target_name()?.replace('-', "_"));
path.set_extension("wasm");
Ok(path)
}

pub fn target_name(&self) -> anyhow::Result<&str> {
self.package
.targets
Expand Down
4 changes: 2 additions & 2 deletions crates/fluvio-smartengine/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fluvio-smartengine"
version = "0.7.10"
version = "0.7.11"
edition = "2021"
license = "Apache-2.0"
authors = ["Fluvio Contributors <[email protected]>"]
Expand All @@ -15,7 +15,7 @@ description = "The official Fluvio SmartEngine"
engine = ["wasmtime"]
wasi = ["wasmtime-wasi", "engine"]
transformation = ["serde_json", "serde_yaml", "humantime-serde"]
default = ["engine"]
default = ["engine", "wasi"]


[dependencies]
Expand Down
8 changes: 6 additions & 2 deletions crates/fluvio-smartengine/src/engine/wasmtime/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ impl SmartModuleChainInstance {
debug!(fuel_used, "fuel used");
metric.add_fuel_used(fuel_used);

if output.error.is_some() {
if let Some(ref smerr) = output.error {
// encountered error, we stop processing and return partial output
tracing::error!(err=?smerr);
return Ok(output);
} else {
next_input =
Expand All @@ -169,6 +170,9 @@ impl SmartModuleChainInstance {

self.store.top_up_fuel();
let output = last.process(next_input, &mut self.store)?;
if let Some(ref smerr) = output.error {
tracing::error!(err=?smerr);
}
let fuel_used = self.store.get_used_fuel();
debug!(fuel_used, "fuel used");
metric.add_fuel_used(fuel_used);
Expand Down Expand Up @@ -239,7 +243,7 @@ mod chaining_test {

use fluvio_protocol::record::Record;
use fluvio_protocol::link::smartmodule::SmartModuleLookbackRuntimeError;
use fluvio_smartmodule::{dataplane::smartmodule::SmartModuleInput};
use fluvio_smartmodule::dataplane::smartmodule::SmartModuleInput;

use crate::engine::error::EngineError;
use crate::engine::config::{Lookback, DEFAULT_SMARTENGINE_VERSION};
Expand Down
2 changes: 1 addition & 1 deletion crates/smartmodule-development-kit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fluvio = { path = "../fluvio", default-features = false }
fluvio-hub-util = { path = "../fluvio-hub-util" }
fluvio-protocol = { path = "../fluvio-protocol", features=["record","api"] }
fluvio-future = { workspace = true, features = ["subscriber"]}
fluvio-smartengine = { path = "../fluvio-smartengine", features = ["transformation"] }
fluvio-smartengine = { path = "../fluvio-smartengine", features = ["transformation", "wasi"] }
fluvio-extension-common = { path = "../fluvio-extension-common", features = ["target"] }
fluvio-controlplane-metadata = { path = "../fluvio-controlplane-metadata", features = ["smartmodule"] }
fluvio-sc-schema = { path = "../fluvio-sc-schema" }
Expand Down
13 changes: 12 additions & 1 deletion crates/smartmodule-development-kit/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use cargo_builder::package::PackageInfo;
use cargo_builder::cargo::Cargo;

use crate::cmd::PackageCmd;
use crate::ENV_SMDK_WASI;

pub(crate) const BUILD_TARGET: &str = "wasm32-unknown-unknown";
pub(crate) const BUILD_TARGET_WASI: &str = "wasm32-wasi";

/// Builds the SmartModule in the current working directory into a WASM file
#[derive(Debug, Parser)]
Expand All @@ -20,18 +22,27 @@ pub struct BuildCmd {
/// Extra arguments to be passed to cargo
#[arg(raw = true)]
extra_arguments: Vec<String>,

/// Build wasi target
#[arg(long, env = ENV_SMDK_WASI)]
wasi: bool,
}

impl BuildCmd {
pub(crate) fn process(self) -> Result<()> {
let opt = self.package.as_opt();
let p = PackageInfo::from_options(&opt)?;

let build_target = if self.wasi {
BUILD_TARGET_WASI
} else {
BUILD_TARGET
};
let cargo = Cargo::build()
.profile(opt.release)
.lib(true)
.package(p.package_name())
.target(BUILD_TARGET)
.target(build_target)
.extra_arguments(self.extra_arguments)
.build()?;

Expand Down
14 changes: 13 additions & 1 deletion crates/smartmodule-development-kit/src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use fluvio_future::task::run_block_on;
use cargo_builder::package::PackageInfo;

use crate::cmd::PackageCmd;
use crate::ENV_SMDK_WASI;

pub const DEFAULT_META_LOCATION: &str = "SmartModule.toml";

Expand Down Expand Up @@ -37,6 +38,10 @@ pub struct LoadCmd {
/// Skip SmartModule load to cluster
#[arg(long)]
dry_run: bool,

/// Build wasi target
#[arg(long, env = ENV_SMDK_WASI)]
wasi: bool,
}
impl LoadCmd {
pub(crate) fn process(self) -> Result<()> {
Expand Down Expand Up @@ -64,7 +69,14 @@ impl LoadCmd {
let sm_id = pkg_metadata.package.name.clone(); // pass anything, this should be overriden by SC
let raw_bytes = match &self.wasm_file {
Some(wasm_file) => crate::read_bytes_from_path(wasm_file)?,
None => crate::read_bytes_from_path(&package_info.target_wasm32_path()?)?,
None => {
let tgtpath = if self.wasi {
package_info.target_wasm32_wasi_path()?
} else {
package_info.target_wasm32_path()?
};
crate::read_bytes_from_path(&tgtpath)?
}
};

let spec = SmartModuleSpec {
Expand Down
2 changes: 2 additions & 0 deletions crates/smartmodule-development-kit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use tracing::debug;

use cmd::SmdkCommand;

pub const ENV_SMDK_WASI: &str = "SMDK_WASI";

fn main() -> Result<()> {
fluvio_future::subscriber::init_tracer(None);

Expand Down
14 changes: 11 additions & 3 deletions crates/smartmodule-development-kit/src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use hubutil::{
use tracing::debug;

use crate::cmd::PackageCmd;
use crate::ENV_SMDK_WASI;

pub const SMARTMODULE_TOML: &str = "SmartModule.toml";

Expand Down Expand Up @@ -43,6 +44,9 @@ pub struct PublishCmd {

#[arg(long, hide_short_help = true)]
remote: Option<String>,

#[arg(long, env=ENV_SMDK_WASI)]
wasi: bool,
}

impl PublishCmd {
Expand Down Expand Up @@ -97,7 +101,7 @@ impl PublishCmd {

Self::cleanup(&hubdir)?;

init_package_template(&package_info)?;
init_package_template(&package_info, self.wasi)?;
check_package_meta_visiblity(&package_info)?;

Ok(hubdir)
Expand Down Expand Up @@ -166,7 +170,7 @@ pub fn package_push(opts: &PublishCmd, pkgpath: &str, access: &HubAccess) -> Res
Ok(())
}

pub fn init_package_template(package_info: &PackageInfo) -> Result<()> {
pub fn init_package_template(package_info: &PackageInfo, wasi: bool) -> Result<()> {
let sm_toml_path = find_smartmodule_toml(package_info)?;
let sm_metadata = SmartModuleMetadata::from_toml(&sm_toml_path)?;

Expand Down Expand Up @@ -198,7 +202,11 @@ pub fn init_package_template(package_info: &PackageInfo) -> Result<()> {
})?,
);

let wasmpath = package_info.target_wasm32_path()?;
let wasmpath = if wasi {
package_info.target_wasm32_wasi_path()?
} else {
package_info.target_wasm32_path()?
};
pm.manifest.push(
package_meta_relative_path(&package_meta_path, &wasmpath).ok_or_else(|| {
anyhow!(
Expand Down
9 changes: 8 additions & 1 deletion crates/smartmodule-development-kit/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use clap::Parser;
use fluvio_future::task::run_block_on;
use fluvio_smartengine::{SmartModuleChainBuilder, SmartModuleConfig, Lookback};
use crate::cmd::PackageCmd;
use crate::ENV_SMDK_WASI;

use fluvio_cli_common::smartmodule::{BaseTestCmd, WithChainBuilder};
#[derive(Debug, Parser)]
Expand All @@ -18,6 +19,8 @@ pub struct TestCmd {
package: PackageCmd,
#[arg(long, group = "TestSmartModule")]
wasm_file: Option<PathBuf>,
#[arg(long, env=ENV_SMDK_WASI)]
wasi: bool,
}

impl TestCmd {
Expand All @@ -32,7 +35,11 @@ impl TestCmd {
wasm_file
} else {
let package_info = PackageInfo::from_options(&self.package.as_opt())?;
package_info.target_wasm32_path()?
if self.wasi {
package_info.target_wasm32_wasi_path()?
} else {
package_info.target_wasm32_path()?
}
};
build_chain_ad_hoc(crate::read_bytes_from_path(&wasm_file)?, params, lookback)
}))
Expand Down

0 comments on commit 8061420

Please sign in to comment.