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

Introduce WAPM query caching to reduce wasmer run's startup delay #3983

Merged
merged 6 commits into from
Jun 16, 2023
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
29 changes: 28 additions & 1 deletion lib/cli/src/commands/publish.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use anyhow::Context;
use clap::Parser;
use wasmer_registry::WasmerConfig;
use wasmer_wasix::runtime::resolver::WapmSource;

/// Publish a package to the package registry.
#[derive(Debug, Parser)]
Expand Down Expand Up @@ -44,7 +47,16 @@ impl Publish {
no_validate: self.no_validate,
package_path: self.package_path.clone(),
};
publish.execute().map_err(on_error)
publish.execute().map_err(on_error)?;

if let Err(e) = invalidate_graphql_query_cache() {
tracing::warn!(
error = &*e,
"Unable to invalidate the cache used for package version queries",
);
}

Ok(())
}
}

Expand All @@ -54,3 +66,18 @@ fn on_error(e: anyhow::Error) -> anyhow::Error {

e
}

// HACK: We want to invalidate the cache used for GraphQL queries so
// the current user sees the results of publishing immediately. There
// are cleaner ways to achieve this, but for now we're just going to
// clear out the whole GraphQL query cache.
// See https://github.com/wasmerio/wasmer/pull/3983 for more
fn invalidate_graphql_query_cache() -> Result<(), anyhow::Error> {
let wasmer_dir = WasmerConfig::get_wasmer_dir()
.map_err(anyhow::Error::msg)
.context("Unable to determine the wasmer dir")?;

WapmSource::invalidate_local_cache(wasmer_dir)?;

Ok(())
}
8 changes: 7 additions & 1 deletion lib/cli/src/commands/run/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{
collections::{BTreeSet, HashMap},
path::{Path, PathBuf},
sync::{mpsc::Sender, Arc},
time::Duration,
};

use anyhow::{Context, Result};
Expand Down Expand Up @@ -36,6 +37,8 @@ use wasmer_wasix::{

use crate::utils::{parse_envvar, parse_mapdir};

const WAPM_SOURCE_CACHE_TIMEOUT: Duration = Duration::from_secs(10 * 60);

#[derive(Debug, Parser, Clone, Default)]
/// WASI Options
pub struct Wasi {
Expand Down Expand Up @@ -341,7 +344,10 @@ impl Wasi {
source.add_source(preloaded);

let graphql_endpoint = self.graphql_endpoint(wasmer_dir)?;
source.add_source(WapmSource::new(graphql_endpoint, Arc::clone(&client)));
let cache_dir = WapmSource::default_cache_dir(wasmer_dir);
let wapm_source = WapmSource::new(graphql_endpoint, Arc::clone(&client))
.with_local_cache(cache_dir, WAPM_SOURCE_CACHE_TIMEOUT);
source.add_source(wapm_source);

let cache_dir = WebSource::default_cache_dir(wasmer_dir);
source.add_source(WebSource::new(cache_dir, client));
Expand Down
13 changes: 13 additions & 0 deletions lib/wasix/src/runtime/package_loader/builtin_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl BuiltinPackageLoader {
Ok(None)
}

#[tracing::instrument(level = "debug", skip_all, fields(%dist.webc, %dist.webc_sha256))]
async fn download(&self, dist: &DistributionInfo) -> Result<Bytes, Error> {
if dist.webc.scheme() == "file" {
match crate::runtime::resolver::utils::file_path_from_url(&dist.webc) {
Expand Down Expand Up @@ -114,8 +115,19 @@ impl BuiltinPackageLoader {
options: Default::default(),
};

tracing::debug!(%request.url, %request.method, "Downloading a webc file");
tracing::trace!(?request.headers);

let response = self.client.request(request).await?;

tracing::trace!(
%response.status,
%response.redirected,
?response.headers,
response.len=response.body.as_ref().map(|body| body.len()),
"Received a response",
);

if !response.is_ok() {
let url = &dist.webc;
return Err(crate::runtime::resolver::utils::http_error(&response)
Expand All @@ -129,6 +141,7 @@ impl BuiltinPackageLoader {
Ok(body.into())
}

#[tracing::instrument(level = "debug", skip_all)]
async fn save_and_load_as_mmapped(
&self,
webc: &[u8],
Expand Down
4 changes: 2 additions & 2 deletions lib/wasix/src/runtime/resolver/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod filesystem_source;
mod in_memory_source;
mod inputs;
mod multi_source_registry;
mod multi_source;
mod outputs;
mod resolve;
mod source;
Expand All @@ -16,7 +16,7 @@ pub use self::{
Command, Dependency, DistributionInfo, PackageInfo, PackageSpecifier, PackageSummary,
WebcHash,
},
multi_source_registry::MultiSource,
multi_source::MultiSource,
outputs::{
DependencyGraph, Edge, ItemLocation, Node, PackageId, Resolution,
ResolvedFileSystemMapping, ResolvedPackage,
Expand Down
Loading