Skip to content

Commit

Permalink
deactivate snapshot use on OX x86 for harmonizer (#465)
Browse files Browse the repository at this point in the history
this will allow us to cross compile the x86 version from ARM in CI

---------

Co-authored-by: o0Ignition0o <[email protected]>
  • Loading branch information
Geal and o0Ignition0o authored Jul 12, 2024
1 parent d8280bf commit 1961c91
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
4 changes: 2 additions & 2 deletions apollo-federation-types/src/config/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ impl FederationVersion {
if self.is_latest() {
supports_arm = true;
} else if let Some(exact) = self.get_exact() {
// v2.7.3 is the earliest version published with aarch64 support for macOS
supports_arm = exact.ge(&Version::parse("2.7.3").unwrap())
// v2.7.3 is the earliest version published with aarch64 support for macOS
supports_arm = exact.ge(&Version::parse("2.7.3").unwrap())
}
}
supports_arm
Expand Down
6 changes: 6 additions & 0 deletions harmonizer/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ fn get_underlying_composition_npm_module_version() -> Version {
parsed_version
}

#[cfg(all(target_os = "macos", target_arch = "x86_64"))]
fn create_snapshot(out_dir: &Path) -> Result<(), Box<dyn Error>> {
Ok(())
}

#[cfg(not(all(target_os = "macos", target_arch = "x86_64")))]
fn create_snapshot(out_dir: &Path) -> Result<(), Box<dyn Error>> {
let options = RuntimeOptions {
..Default::default()
Expand Down
47 changes: 34 additions & 13 deletions harmonizer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ composition implementation while we work toward something else.
#![forbid(unsafe_code)]
#![deny(missing_debug_implementations, nonstandard_style)]
#![warn(missing_docs, future_incompatible, unreachable_pub, rust_2018_idioms)]
use deno_core::{JsRuntime, RuntimeOptions, Snapshot};
#[cfg(not(all(target_os = "macos", target_arch = "x86_64")))]
use deno_core::Snapshot;
use deno_core::{JsRuntime, RuntimeOptions};

mod js_types;

Expand All @@ -44,7 +46,6 @@ const APOLLO_HARMONIZER_EXPERIMENTAL_V8_INITIAL_HEAP_SIZE_DEFAULT: &str = "256";
// A reasonable default max limit for our deno heap.
const APOLLO_HARMONIZER_EXPERIMENTAL_V8_MAX_HEAP_SIZE_DEFAULT: &str = "1400";


/// The `harmonize` function receives a [`Vec<SubgraphDefinition>`] and invokes JavaScript
/// composition on it, either returning the successful output, or a list of error messages.
pub fn harmonize(subgraph_definitions: Vec<SubgraphDefinition>) -> BuildResult {
Expand All @@ -58,19 +59,15 @@ pub fn harmonize_limit(
subgraph_definitions: Vec<SubgraphDefinition>,
nodes_limit: Option<u32>,
) -> BuildResult {
// The snapshot is created in the build_harmonizer.rs script and included in our binary image
let buffer = include_bytes!(concat!(env!("OUT_DIR"), "/composition.snap"));

let initial_heap_size =
std::env::var("APOLLO_HARMONIZER_EXPERIMENTAL_V8_INITIAL_HEAP_SIZE").unwrap_or_else(|_e| {
let initial_heap_size = std::env::var("APOLLO_HARMONIZER_EXPERIMENTAL_V8_INITIAL_HEAP_SIZE")
.unwrap_or_else(|_e| {
APOLLO_HARMONIZER_EXPERIMENTAL_V8_INITIAL_HEAP_SIZE_DEFAULT.to_string()
});

let max_heap_size_maybe = std::env::var("APOLLO_HARMONIZER_EXPERIMENTAL_V8_MAX_HEAP_SIZE").ok();
let max_heap_size_provided = max_heap_size_maybe.is_some();
let max_heap_size = max_heap_size_maybe.unwrap_or_else(|| {
APOLLO_HARMONIZER_EXPERIMENTAL_V8_MAX_HEAP_SIZE_DEFAULT.to_string()
});
let max_heap_size = max_heap_size_maybe
.unwrap_or_else(|| APOLLO_HARMONIZER_EXPERIMENTAL_V8_MAX_HEAP_SIZE_DEFAULT.to_string());

// The first flag is argv[0], so provide an ignorable value
let flags = vec![
Expand All @@ -91,12 +88,36 @@ pub fn harmonize_limit(
panic!("deno ignored these flags: {:?}", ignored);
}

// Use our snapshot to provision our new runtime
let options = RuntimeOptions {
// The snapshot is created in the build_harmonizer.rs script and included in our binary image
#[cfg(not(all(target_os = "macos", target_arch = "x86_64")))]
let buffer = include_bytes!(concat!(env!("OUT_DIR"), "/composition.snap"));

#[cfg(not(all(target_os = "macos", target_arch = "x86_64")))]
let mut runtime = JsRuntime::new(RuntimeOptions {
startup_snapshot: Some(Snapshot::Static(buffer)),
..Default::default()
});

#[cfg(all(target_os = "macos", target_arch = "x86_64"))]
let mut runtime = {
let mut runtime = JsRuntime::new(RuntimeOptions {
..Default::default()
});

// The runtime automatically contains a Deno.core object with several
// functions for interacting with it.
let runtime_str = include_str!("../bundled/runtime.js");
runtime
.execute_script("<init>", deno_core::FastString::Owned(runtime_str.into()))
.expect("unable to initialize router bridge runtime environment");

// Load the composition library.
let bridge_str = include_str!("../bundled/composition_bridge.js");
runtime
.execute_script("bridge.js", deno_core::FastString::Owned(bridge_str.into()))
.expect("unable to evaluate bridge module");
runtime
};
let mut runtime = JsRuntime::new(options);

// if max_heap_size was not set, we resize the heap every time
// we approach the limit. This is a tradeoff as it might cause
Expand Down
4 changes: 3 additions & 1 deletion router-bridge/src/js.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::error::Error;
#[cfg(not(all(target_os = "macos", target_arch = "x86_64")))]
use deno_core::Snapshot;
/// Wraps creating the Deno Js runtime collecting parameters and executing a script.
use deno_core::{Extension, JsRuntime, RuntimeOptions, Snapshot};
use deno_core::{Extension, JsRuntime, RuntimeOptions};
use serde::de::DeserializeOwned;
use serde::Serialize;

Expand Down

0 comments on commit 1961c91

Please sign in to comment.