Skip to content
Draft
235 changes: 223 additions & 12 deletions elastos/crates/elastos-compute/src/providers/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use async_trait::async_trait;
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
use tokio::sync::RwLock;
use std::time::Duration;
use tokio::sync::{RwLock, Semaphore};
use wasmtime::component::{bindgen, Component, HasSelf, Linker};
use wasmtime::{Config, Engine, Store, StoreLimits, StoreLimitsBuilder};

Expand All @@ -27,6 +28,7 @@ bindgen!({

const COMPONENT_FUEL: u64 = 100_000_000;
const COMPONENT_MEMORY_SIZE: usize = 128 * 1024 * 1024;
const COMPONENT_COMPILE_WAIT_TIMEOUT: Duration = Duration::from_secs(30);

struct ComponentActivation {
engine: Engine,
Expand All @@ -49,6 +51,7 @@ struct ComponentState {
/// hostcall configured by Runtime.
pub struct ComponentProvider {
instances: Arc<RwLock<HashMap<CapsuleId, ComponentActivation>>>,
compile_slot: Arc<Semaphore>,
bus_hostcall: std::sync::RwLock<Option<BridgeHostcall>>,
bridge_principals: Arc<RwLock<HashMap<CapsuleId, Option<String>>>>,
}
Expand All @@ -57,6 +60,9 @@ impl ComponentProvider {
pub fn new() -> Self {
Self {
instances: Arc::new(RwLock::new(HashMap::new())),
// Wasmtime parallelizes each compile internally. Queue loads so
// bursts do not create competing compiler jobs.
compile_slot: Arc::new(Semaphore::new(1)),
bus_hostcall: std::sync::RwLock::new(None),
bridge_principals: Arc::new(RwLock::new(HashMap::new())),
}
Expand Down Expand Up @@ -154,17 +160,39 @@ impl ComputeProvider for ComponentProvider {
}

let component_path = path.join(&manifest.entrypoint);
if !component_path.exists() {
return Err(ElastosError::CapsuleNotFound(format!(
"component file not found: {}",
component_path.display()
)));
}

let engine = Self::engine()?;
let component = Component::from_file(&engine, &component_path).map_err(|err| {
ElastosError::Compute(format!("Failed to compile component capsule: {err}"))
})?;
// File reads and Wasmtime compilation can take substantial time. Keep
// unrelated Runtime work responsive while preparing the activation.
// Bound the wait for capacity; an admitted blocking job keeps its slot
// until the actual compile finishes, even if its caller is cancelled.
let compile_permit = tokio::time::timeout(
COMPONENT_COMPILE_WAIT_TIMEOUT,
self.compile_slot.clone().acquire_owned(),
)
.await
.map_err(|_| ElastosError::Compute("Timed out waiting for the component compiler".into()))?
.map_err(|_| ElastosError::Compute("Component compiler is unavailable".into()))?;
let (engine, component) =
tokio::task::spawn_blocking(move || -> Result<(Engine, Component)> {
// A cancelled caller can leave blocking work in progress. The
// work retains its slot until it actually finishes.
let _compile_permit = compile_permit;
if !component_path.exists() {
return Err(ElastosError::CapsuleNotFound(format!(
"component file not found: {}",
component_path.display()
)));
}

let engine = Self::engine()?;
let component = Component::from_file(&engine, &component_path).map_err(|err| {
ElastosError::Compute(format!("Failed to compile component capsule: {err}"))
})?;
Ok((engine, component))
})
.await
.map_err(|err| {
ElastosError::Compute(format!("Component load task join error: {err}"))
})??;
let id = CapsuleId::new(format!("component-{}", uuid::Uuid::new_v4()));

self.instances.write().await.insert(
Expand Down Expand Up @@ -419,7 +447,190 @@ impl elastos::bus::providers::Host for ComponentState {
#[cfg(test)]
mod tests {
use super::*;
use std::future::Future;
use std::sync::Mutex;
use std::task::Poll;

fn component_fixture() -> (std::path::PathBuf, CapsuleManifest) {
let path =
Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/components/bus-v1");
let manifest =
serde_json::from_slice(&std::fs::read(path.join("capsule.json")).unwrap()).unwrap();
(path, manifest)
}

#[test]
fn component_load_keeps_async_work_responsive() {
let runtime = tokio::runtime::Builder::new_current_thread()
.max_blocking_threads(1)
.enable_all()
.build()
.unwrap();
let (release, waiting) = std::sync::mpsc::channel();
let blocker = runtime.spawn_blocking(move || {
let _ = waiting.recv();
});
let provider = Arc::new(ComponentProvider::new());
let (path, manifest) = component_fixture();

runtime.block_on(async {
let (started, observing) = tokio::sync::oneshot::channel();
let loading_provider = provider.clone();
let loading = tokio::spawn(async move {
started.send(()).unwrap();
loading_provider.load(&path, manifest).await
});
observing.await.unwrap();
tokio::task::yield_now().await;
let yielded_before_loading_finished = !loading.is_finished();

// Always release the worker before checking the result, including
// when a regression makes load run on the async executor.
release.send(()).unwrap();
let handle = loading.await.unwrap().unwrap();
blocker.await.unwrap();
assert_eq!(
provider.status(&handle).await.unwrap(),
CapsuleStatus::Loading
);
provider.stop(&handle).await.unwrap();
assert!(matches!(
provider.status(&handle).await,
Err(ElastosError::CapsuleNotFound(_))
));
assert!(
yielded_before_loading_finished,
"component loading prevented unrelated async work from running"
);
});
}

#[tokio::test(start_paused = true)]
async fn component_load_times_out_waiting_for_compile_slot() {
let provider = Arc::new(ComponentProvider::new());
let held_permit = provider.compile_slot.clone().acquire_owned().await.unwrap();
let (path, manifest) = component_fixture();
let loading_provider = provider.clone();
let mut loading = tokio::spawn(async move { loading_provider.load(&path, manifest).await });

// The outer deadline makes a missing queue limit fail instead of hang.
// Paused Tokio time avoids relying on compilation or host speed.
let result = tokio::time::timeout(
COMPONENT_COMPILE_WAIT_TIMEOUT + Duration::from_secs(1),
&mut loading,
)
.await;
if result.is_err() {
loading.abort();
let _ = loading.await;
}
drop(held_permit);

assert!(
matches!(result, Ok(Ok(Err(ElastosError::Compute(message))))
if message == "Timed out waiting for the component compiler"),
"a queued component load must return its own timeout before the outer deadline"
);
assert!(provider.instances.read().await.is_empty());
assert_eq!(provider.compile_slot.available_permits(), 1);
}

#[test]
fn component_load_retains_compile_slot_after_cancellation() {
let runtime = tokio::runtime::Builder::new_current_thread()
.max_blocking_threads(1)
.enable_all()
.start_paused(true)
.build()
.unwrap();
let (release, waiting) = std::sync::mpsc::channel();
let (blocked, observing_blocker) = tokio::sync::oneshot::channel();
let blocker = runtime.spawn_blocking(move || {
blocked.send(()).unwrap();
let _ = waiting.recv();
});
let provider = Arc::new(ComponentProvider::new());
let (path, manifest) = component_fixture();

runtime.block_on(async {
observing_blocker.await.unwrap();
let (started, observing) = tokio::sync::oneshot::channel();
let loading_provider = provider.clone();
let first_path = path.clone();
let first_manifest = manifest.clone();
let loading = tokio::spawn(async move {
let mut load = loading_provider.load(&first_path, first_manifest);
// Poll through admission before acknowledging the caller. The
// sole blocking worker stays gated until both waits finish.
let initial = std::future::poll_fn(|cx| Poll::Ready(load.as_mut().poll(cx))).await;
started.send(()).unwrap();
match initial {
Poll::Ready(result) => result,
Poll::Pending => load.await,
}
});
observing.await.unwrap();
loading.abort();
let cancelled = loading.await.unwrap_err().is_cancelled();

let result = {
let next_load = tokio::time::timeout(
COMPONENT_COMPILE_WAIT_TIMEOUT + Duration::from_secs(1),
provider.load(&path, manifest.clone()),
);
tokio::pin!(next_load);
// Register the queue wait, then advance virtual time while the
// cancelled caller's compile still owns its slot. Polling the
// load directly lets its queue timeout win over the outer guard.
match std::future::poll_fn(|cx| Poll::Ready(next_load.as_mut().poll(cx))).await {
Poll::Ready(result) => result,
Poll::Pending => {
tokio::time::advance(
COMPONENT_COMPILE_WAIT_TIMEOUT + Duration::from_secs(1),
)
.await;
next_load.await
}
}
};

// Release and drain the blocking work before asserting, including
// when early slot release admitted the second load to the worker.
release.send(()).unwrap();
blocker.await.unwrap();
tokio::time::resume();
let handle = provider.load(&path, manifest).await.unwrap();
provider.stop(&handle).await.unwrap();

assert!(cancelled);
assert!(
matches!(result, Ok(Err(ElastosError::Compute(message)))
if message == "Timed out waiting for the component compiler"),
"cancelling a load must keep later loads queued until its blocking compile finishes"
);
assert!(provider.instances.read().await.is_empty());
});
}

#[tokio::test]
async fn component_load_preserves_artifact_errors() {
let provider = ComponentProvider::new();
let (_, manifest) = component_fixture();
let directory = tempfile::tempdir().unwrap();
assert!(matches!(
provider.load(directory.path(), manifest.clone()).await,
Err(ElastosError::CapsuleNotFound(message))
if message.starts_with("component file not found: ")
));
std::fs::write(directory.path().join(&manifest.entrypoint), b"invalid wasm").unwrap();
assert!(matches!(
provider.load(directory.path(), manifest).await,
Err(ElastosError::Compute(message))
if message.starts_with("Failed to compile component capsule: ")
));
assert!(provider.instances.read().await.is_empty());
assert_eq!(provider.compile_slot.available_permits(), 1);
}

fn json_line(line: &str) -> serde_json::Value {
serde_json::from_str(line).expect("captured bridge line must be JSON")
Expand Down
Loading