Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/noa/wasmtime' into jdetter/test-…
Browse files Browse the repository at this point in the history
…branch-off-master
  • Loading branch information
John Detter committed Nov 15, 2023
2 parents 54c7589 + d82a5e2 commit 211ccfb
Show file tree
Hide file tree
Showing 21 changed files with 905 additions and 2,042 deletions.
613 changes: 105 additions & 508 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,6 @@ uuid = { version = "1.2.1", features = ["v4"] }
walkdir = "2.2.5"
wasmbin = "0.6"

# wasmer prior to 4.1.1 had a signal handling bug on macOS.
wasmer = "4.1.1"
wasmer-middlewares = "4.1.1"
wasmer-types = "4.1.1"
wasmer-vm = "4.1.1"

wasmtime = { version = "14", default-features = false, features = ["cranelift"] }

# We use the "ondemand" feature to allow connecting after the start,
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/src/subcommands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub fn cli() -> clap::Command {
Arg::new("host_type")
.long("host-type")
.short('t')
.value_parser(["wasmer"])
.default_value("wasmer")
.value_parser(["wasmtime"])
.default_value("wasmtime")
.help("The type of host that should be for hosting this module"),
)
.arg(
Expand Down
6 changes: 2 additions & 4 deletions crates/client-api/src/routes/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use spacetimedb::host::ReducerOutcome;
use spacetimedb::host::UpdateDatabaseSuccess;
use spacetimedb::identity::Identity;
use spacetimedb::json::client_api::StmtResultJson;
use spacetimedb::messages::control_db::{Database, DatabaseInstance, HostType};
use spacetimedb::messages::control_db::{Database, DatabaseInstance};
use spacetimedb::sql::execute::execute;
use spacetimedb_lib::address::AddressForUrl;
use spacetimedb_lib::identity::AuthCtx;
Expand Down Expand Up @@ -386,9 +386,7 @@ pub async fn info<S: ControlStateDelegate>(
.ok_or((StatusCode::NOT_FOUND, "No such database."))?;
log::trace!("Fetched database from the worker db for address: {address:?}");

let host_type = match database.host_type {
HostType::Wasmer => "wasmer",
};
let host_type: &str = database.host_type.as_ref();
let response_json = json!({
"address": database.address,
"identity": database.identity,
Expand Down
15 changes: 6 additions & 9 deletions crates/client-api/src/routes/energy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ pub async fn add_energy<S: ControlStateDelegate>(
let mut balance = ctx
.get_energy_balance(&auth.identity)
.map_err(log_and_500)?
.map(|quanta| quanta.0)
.unwrap_or(0);
.map_or(0, |quanta| quanta.get());

if let Some(satoshi) = amount {
ctx.add_energy(&auth.identity, EnergyQuanta(satoshi))
ctx.add_energy(&auth.identity, EnergyQuanta::new(satoshi))
.await
.map_err(log_and_500)?;
balance += satoshi;
Expand All @@ -68,8 +67,7 @@ fn get_budget_inner(ctx: impl ControlStateDelegate, identity: &Identity) -> axum
let balance = ctx
.get_energy_balance(identity)
.map_err(log_and_500)?
.map(|quanta| quanta.0)
.unwrap_or(0);
.map_or(0, |quanta| quanta.get());

let response_json = json!({
// Note: balance must be returned as a string to avoid truncation.
Expand Down Expand Up @@ -114,18 +112,17 @@ pub async fn set_energy_balance<S: ControlStateDelegate>(
let current_balance = ctx
.get_energy_balance(&identity)
.map_err(log_and_500)?
.map(|quanta| quanta.0)
.unwrap_or(0);
.map_or(0, |quanta| quanta.get());

let balance: i128 = if desired_balance > current_balance {
let delta = desired_balance - current_balance;
ctx.add_energy(&identity, EnergyQuanta(delta))
ctx.add_energy(&identity, EnergyQuanta::new(delta))
.await
.map_err(log_and_500)?;
delta
} else {
let delta = current_balance - desired_balance;
ctx.withdraw_energy(&identity, EnergyQuanta(delta))
ctx.withdraw_energy(&identity, EnergyQuanta::new(delta))
.await
.map_err(log_and_500)?;
delta
Expand Down
6 changes: 2 additions & 4 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ tempfile.workspace = true
thiserror.workspace = true
tokio-util.workspace = true
tokio.workspace = true
toml.workspace = true
tracing-appender.workspace = true
tracing-core.workspace = true
tracing-flame.workspace = true
Expand All @@ -87,10 +88,7 @@ tracing.workspace = true
url.workspace = true
urlencoding.workspace = true
uuid.workspace = true
wasmer-middlewares.workspace = true
wasmer-types.workspace = true
wasmer-vm.workspace = true
wasmer.workspace = true
wasmtime = { workspace = true, features = ["cache"] }
# Rocksdb ostorage backend, linked only if "rocksdb" feature enabled.
rocksdb = {workspace = true, optional = true}

Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/control_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ impl ControlDb {
return Err(Error::DecodingError(bsatn::DecodeError::BufferLength));
};
let balance = i128::from_be_bytes(arr);
Ok(Some(EnergyQuanta(balance)))
Ok(Some(EnergyQuanta::new(balance)))
} else {
Ok(None)
}
Expand All @@ -573,7 +573,7 @@ impl ControlDb {
/// `control_budget`, where a cached copy is stored along with business logic for managing it.
pub fn set_energy_balance(&self, identity: Identity, energy_balance: EnergyQuanta) -> Result<()> {
let tree = self.db.open_tree("energy_budget")?;
tree.insert(identity.as_bytes(), &energy_balance.0.to_be_bytes())?;
tree.insert(identity.as_bytes(), &energy_balance.get().to_be_bytes())?;

Ok(())
}
Expand Down
30 changes: 11 additions & 19 deletions crates/core/src/host/host_controller.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::hash::hash_bytes;
use crate::host::wasmer;
use crate::host;
use crate::messages::control_db::HostType;
use crate::module_host_context::ModuleHostContext;
use anyhow::Context;
// use parking_lot::{Condvar, Mutex};
use parking_lot::Mutex;
use serde::Serialize;
use std::collections::HashMap;
Expand Down Expand Up @@ -90,28 +89,21 @@ impl fmt::Display for DescribedEntityType {
/// for a user's balance to go negative. This is allowable
/// for reasons of eventual consistency motivated by performance.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct EnergyQuanta(pub i128);
pub struct EnergyQuanta(i128);

impl EnergyQuanta {
pub const ZERO: Self = EnergyQuanta(0);

pub const DEFAULT_BUDGET: Self = EnergyQuanta(1_000_000_000_000_000_000);

/// A conversion function to convert from the canonical unit to points used
/// by Wasmer to track energy usage.
pub fn as_points(&self) -> u64 {
if self.0 < 0 {
return 0;
} else if self.0 > u64::MAX as i128 {
return u64::MAX;
}
self.0 as u64
#[inline]
pub fn new(v: i128) -> Self {
Self(v)
}

/// A conversion function to convert from point used
/// by Wasmer to track energy usage, to our canonical unit.
pub fn from_points(points: u64) -> Self {
Self(points as i128)
#[inline]
pub fn get(&self) -> i128 {
self.0
}
}

Expand Down Expand Up @@ -273,13 +265,13 @@ impl HostController {
let module_hash = hash_bytes(&mhc.program_bytes);
let (threadpool, energy_monitor) = (self.threadpool.clone(), self.energy_monitor.clone());
let module_host = match mhc.host_type {
HostType::Wasmer => {
HostType::Wasmtime => {
// make_actor with block_in_place since it's going to take some time to compute.
let start = Instant::now();
let actor = tokio::task::block_in_place(|| {
wasmer::make_actor(mhc.dbic, module_hash, &mhc.program_bytes, mhc.scheduler, energy_monitor)
host::wasmtime::make_actor(mhc.dbic, module_hash, &mhc.program_bytes, mhc.scheduler, energy_monitor)
})?;
log::trace!("wasmer::make_actor blocked for {:?}", start.elapsed());
log::trace!("wasmtime::make_actor blocked for {:?}", start.elapsed());
ModuleHost::new(threadpool, actor)
}
};
Expand Down
6 changes: 3 additions & 3 deletions crates/core/src/host/instance_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,13 @@ impl InstanceEnv {
///
/// Errors with `TableNotFound` if the table does not exist.
#[tracing::instrument(skip_all)]
pub fn get_table_id(&self, table_name: String) -> Result<TableId, NodesError> {
pub fn get_table_id(&self, table_name: &str) -> Result<TableId, NodesError> {
let stdb = &*self.dbic.relational_db;
let tx = &mut *self.get_tx()?;

// Query the table id from the name.
let table_id = stdb
.table_id_from_name(tx, &table_name)?
.table_id_from_name(tx, table_name)?
.ok_or(NodesError::TableNotFound)?;

Ok(table_id)
Expand Down Expand Up @@ -235,7 +235,7 @@ impl InstanceEnv {
IndexType::Hash => todo!("Hash indexes not yet supported"),
};

let cols = NonEmpty::from_slice(&col_ids)
let cols = NonEmpty::from_vec(col_ids)
.expect("Attempt to create an index with zero columns")
.map(Into::into);

Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/host/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::time::Duration;
mod host_controller;
pub(crate) mod module_host;
pub mod scheduler;
mod wasmer;
mod wasmtime;
// Visible for integration testing.
pub mod instance_env;
mod timestamp;
Expand Down Expand Up @@ -168,7 +168,7 @@ impl EnergyMonitor for NullEnergyMonitor {
}

/// Tags for each call that a `WasmInstanceEnv` can make.
#[derive(Debug, Display, Enum)]
#[derive(Debug, Display, Enum, Clone, Copy)]
pub enum AbiCall {
CancelReducer,
ConsoleLog,
Expand Down
27 changes: 18 additions & 9 deletions crates/core/src/host/wasm_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod module_host_actor;

use std::time::Instant;

use super::AbiCall;
use crate::error::{DBError, IndexError, NodesError};

pub const CALL_REDUCER_DUNDER: &str = "__call_reducer__";
Expand Down Expand Up @@ -67,7 +68,7 @@ macro_rules! type_eq {
}
};
}
type_eq!(wasmer::Type);
type_eq!(wasmtime::ValType);

#[derive(Debug)]
pub struct FuncSig<T: AsRef<[WasmType]>> {
Expand All @@ -81,22 +82,22 @@ impl StaticFuncSig {
Self { params, results }
}
}
impl<T: AsRef<[WasmType]>> PartialEq<FuncSig<T>> for wasmer::ExternType {
impl<T: AsRef<[WasmType]>> PartialEq<FuncSig<T>> for wasmtime::ExternType {
fn eq(&self, other: &FuncSig<T>) -> bool {
self.func().map_or(false, |f| {
f.params() == other.params.as_ref() && f.results() == other.results.as_ref()
f.params().eq(other.params.as_ref()) && f.results().eq(other.results.as_ref())
})
}
}
impl FuncSigLike for wasmer::ExternType {
impl FuncSigLike for wasmtime::ExternType {
fn to_func_sig(&self) -> Option<BoxFuncSig> {
self.func().map(|f| FuncSig {
params: f.params().iter().map(|t| (*t).into()).collect(),
results: f.results().iter().map(|t| (*t).into()).collect(),
params: f.params().map(Into::into).collect(),
results: f.results().map(Into::into).collect(),
})
}
fn is_memory(&self) -> bool {
matches!(self, wasmer::ExternType::Memory(_))
matches!(self, wasmtime::ExternType::Memory(_))
}
}

Expand Down Expand Up @@ -220,7 +221,7 @@ pub trait ResourceIndex {

macro_rules! decl_index {
($name:ident => $resource:ty) => {
#[derive(Copy, Clone, wasmer::ValueType)]
#[derive(Copy, Clone)]
#[repr(transparent)]
pub(super) struct $name(pub u32);

Expand All @@ -233,6 +234,14 @@ macro_rules! decl_index {
self.0
}
}

impl $name {
#[allow(unused)]
#[doc(hidden)]
pub(super) fn to_le_bytes(self) -> [u8; 4] {
self.0.to_le_bytes()
}
}
};
}

Expand Down Expand Up @@ -355,7 +364,7 @@ pub fn err_to_errno(err: &NodesError) -> Option<u16> {
#[derive(Debug, thiserror::Error)]
#[error("runtime error calling {func}: {err}")]
pub struct AbiRuntimeError {
pub func: &'static str,
pub func: AbiCall,
#[source]
pub err: NodesError,
}
2 changes: 1 addition & 1 deletion crates/core/src/host/wasm_common/module_host_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ impl<T: WasmInstance> WasmModuleInstance<T> {
let reducer_span = tracing::trace_span!(
"run_reducer",
timings.total_duration = tracing::field::Empty,
energy.budget = budget.0,
energy.budget = budget.get(),
energy.used = tracing::field::Empty,
)
.entered();
Expand Down
Loading

0 comments on commit 211ccfb

Please sign in to comment.