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 wasmtime to runtime config #891

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions crates/testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl HttpTestConfig {
TriggerExecutorBuilder::new(self.build_loader())
.build(
TEST_APP_URI.to_string(),
TriggerExecutorBuilderConfig::default(),
&TriggerExecutorBuilderConfig::default(),
)
.await
.unwrap()
Expand Down Expand Up @@ -141,7 +141,7 @@ impl RedisTestConfig {
TriggerExecutorBuilder::new(self.build_loader())
.build(
TEST_APP_URI.to_string(),
TriggerExecutorBuilderConfig::default(),
&TriggerExecutorBuilderConfig::default(),
)
.await
.unwrap()
Expand Down
1 change: 1 addition & 0 deletions crates/trigger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ toml = "0.5.9"
tracing = { version = "0.1", features = [ "log" ] }
url = "2"
wasmtime = "0.39.1"
wasmtime-cache = "0.39.1"

[dev-dependencies]
tempfile = "3.3.0"
Expand Down
69 changes: 37 additions & 32 deletions crates/trigger/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use anyhow::{anyhow, Context, Result};
use clap::{Args, IntoApp, Parser};
use serde::de::DeserializeOwned;

Expand All @@ -9,9 +9,7 @@ use crate::{config::TriggerExecutorBuilderConfig, loader::TriggerLoader, stdio::
use crate::{TriggerExecutor, TriggerExecutorBuilder};

pub const APP_LOG_DIR: &str = "APP_LOG_DIR";
pub const DISABLE_WASMTIME_CACHE: &str = "DISABLE_WASMTIME_CACHE";
pub const FOLLOW_LOG_OPT: &str = "FOLLOW_ID";
pub const WASMTIME_CACHE_FILE: &str = "WASMTIME_CACHE_FILE";
pub const RUNTIME_CONFIG_FILE: &str = "RUNTIME_CONFIG_FILE";

// Set by `spin up`
Expand All @@ -33,25 +31,6 @@ where
)]
pub log: Option<PathBuf>,

/// Disable Wasmtime cache.
#[clap(
name = DISABLE_WASMTIME_CACHE,
long = "disable-cache",
env = DISABLE_WASMTIME_CACHE,
conflicts_with = WASMTIME_CACHE_FILE,
takes_value = false,
)]
pub disable_cache: bool,

/// Wasmtime cache configuration file.
#[clap(
name = WASMTIME_CACHE_FILE,
long = "cache",
env = WASMTIME_CACHE_FILE,
conflicts_with = DISABLE_WASMTIME_CACHE,
)]
pub cache: Option<PathBuf>,

/// Print output for given component(s) to stdout/stderr
#[clap(
name = FOLLOW_LOG_OPT,
Expand Down Expand Up @@ -110,19 +89,32 @@ where
let working_dir = std::env::var(SPIN_WORKING_DIR).context(SPIN_WORKING_DIR)?;
let locked_url = std::env::var(SPIN_LOCKED_URL).context(SPIN_LOCKED_URL)?;

let loader = TriggerLoader::new(working_dir, self.allow_transient_write);
let loader = TriggerLoader::new(&working_dir, self.allow_transient_write);

let base_runtime_config_dir = match &self.runtime_config_file {
Some(p) => p.parent().ok_or_else(|| {
anyhow!(
"Failed to get containing directory for runtime config file '{}'",
&p.display()
)
})?,
None => Path::new(&working_dir),
};
let trigger_config =
TriggerExecutorBuilderConfig::load_from_file(self.runtime_config_file.clone())?;

let executor: Executor = {
let mut builder = TriggerExecutorBuilder::new(loader);
self.update_wasmtime_config(builder.wasmtime_config_mut())?;
self.update_wasmtime_config(
builder.wasmtime_config_mut(),
&trigger_config,
base_runtime_config_dir,
)?;

let logging_hooks = StdioLoggingTriggerHooks::new(self.follow_components(), self.log);
builder.hooks(logging_hooks);

builder.build(locked_url, trigger_config).await?
builder.build(locked_url, &trigger_config).await?
};

let run_fut = executor.run(self.run_config);
Expand Down Expand Up @@ -156,14 +148,27 @@ where
}
}

fn update_wasmtime_config(&self, config: &mut spin_core::wasmtime::Config) -> Result<()> {
// Apply --cache / --disable-cache
if !self.disable_cache {
match &self.cache {
Some(p) => config.cache_config_load(p)?,
None => config.cache_config_load_default()?,
fn update_wasmtime_config(
&self,
wasmtime_config: &mut spin_core::wasmtime::Config,
trigger_config: &TriggerExecutorBuilderConfig,
base_dir: impl AsRef<Path>,
) -> Result<()> {
if let Some(p) = &trigger_config.wasmtime_config.cache_file {
let p = match Path::new(p).is_absolute() {
true => PathBuf::from(p),
false => base_dir.as_ref().join(p),
};
wasmtime_config.cache_config_load(p)?;
}

let wasm_backtrace_details = match &*trigger_config.wasmtime_config.wasm_backtrace_details {
"Enable" => wasmtime::WasmBacktraceDetails::Enable,
"Environment" => wasmtime::WasmBacktraceDetails::Environment,
_ => wasmtime::WasmBacktraceDetails::Disable,
};
tracing::trace!("wasm_backtrace_details {:?}", wasm_backtrace_details);
wasmtime_config.wasm_backtrace_details(wasm_backtrace_details);
Ok(())
}
}
16 changes: 16 additions & 0 deletions crates/trigger/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use toml;
pub struct TriggerExecutorBuilderConfig {
#[serde(rename = "config_provider", default)]
pub config_providers: Vec<ConfigProvider>,

#[serde(rename = "wasmtime", default)]
pub wasmtime_config: WasmtimeConfig,
}

#[derive(Debug, Deserialize)]
Expand All @@ -26,6 +29,15 @@ pub struct VaultConfig {
pub prefix: Option<String>,
}

// Wasmtime config to initialize wasmtime engine
#[derive(Debug, Default, Deserialize)]
pub struct WasmtimeConfig {
pub cache_file: Option<String>,

#[serde(default = "default_wask_backtrace_details")]
pub wasm_backtrace_details: String,
}

impl TriggerExecutorBuilderConfig {
pub fn load_from_file(config_file: Option<PathBuf>) -> Result<Self> {
let config_file = match config_file {
Expand All @@ -39,3 +51,7 @@ impl TriggerExecutorBuilderConfig {
Ok(config)
}
}

fn default_wask_backtrace_details() -> String {
"Disable".to_string()
}
4 changes: 2 additions & 2 deletions crates/trigger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<Executor: TriggerExecutor> TriggerExecutorBuilder<Executor> {
pub async fn build(
mut self,
app_uri: String,
builder_config: config::TriggerExecutorBuilderConfig,
builder_config: &config::TriggerExecutorBuilderConfig,
) -> Result<Executor>
where
Executor::TriggerConfig: DeserializeOwned,
Expand All @@ -101,7 +101,7 @@ impl<Executor: TriggerExecutor> TriggerExecutorBuilder<Executor> {
self.loader.add_dynamic_host_component(
&mut builder,
spin_config::ConfigHostComponent::new(
self.get_config_providers(&app_uri, &builder_config),
self.get_config_providers(&app_uri, builder_config),
),
)?;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/http/simple-spin-rust/runtime_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[wasmtime]
cache_file = "wasmtime_config.toml"
wasm_backtrace_details = "Enable"
2 changes: 2 additions & 0 deletions tests/http/simple-spin-rust/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ files = [ { source = "assets", destination = "/" } ]
route = "/hello/..."
[component.config]
message = "I'm a {{object}}"
[component.build]
command = "cargo build --target wasm32-wasi --release"
6 changes: 6 additions & 0 deletions tests/http/simple-spin-rust/wasmtime_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Comment out certain settings to use default values.
# For more settings, please refer to the documentation:
# https://bytecodealliance.github.io/wasmtime/cli-cache.html

[cache]
enabled = false