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

Make Trigger trait generic over RuntimeFactors #2790

Merged
merged 13 commits into from
Sep 4, 2024
Merged

Make Trigger trait generic over RuntimeFactors #2790

merged 13 commits into from
Sep 4, 2024

Conversation

rylev
Copy link
Collaborator

@rylev rylev commented Aug 30, 2024

For the Spin CLI this change should be a no-op, but it does open up lots of possibilities for future changes including custom runtimes that benefit from all generic functionality Spin has (e.g., logging, telemetry, etc.) without having to opt into a specific wit world all guests must target.

The main change is that Trigger is now generic the RuntimeFactors type. This means that the set of Factors a Spin runtime can support is no longer hard coded to the TriggerFactors type (which contains implementations for the fermyon:spin/imports world). The decision of which RuntimeFactors type to use is pushed all the way to the edge of the system. Now the spin-cli crate is where TriggerFactors is plugged in. In the future, we may add configuration knobs to tweak which RuntimeFactors type gets plugged in allowing users of the Spin runtime to decide what worlds they wish to support.

The HttpTrigger and RedisTrigger are now also generic over RuntimeFactors meaning that they should be easily pluggable into custom runtimes. Making triggers generic over which RuntimeFactors they support should be easy, but it also allows hardcoding that decision should the trigger author wish to do so.

The key to all of this working is the RuntimeFactorsBuilder which makes constructing and configuring a RuntimeFactors type generic. This includes the ability for implementors of this trait to specify custom CLI arguments that are just used for configuring the RuntimeFactors type.

In the generic trigger code, there are a few places where a RuntimeFactors type is expected to have a certain Factor in its collection. This has been changed to a dynamic query that allows generic code to do something if a RuntimeFactors contains a certain Factor. This is accomplished through the new HasInstanceBuilder trait.

@rylev rylev requested a review from lann August 30, 2024 11:53
@@ -19,7 +19,9 @@ use spin_factors::runtime_config::toml::GetTomlValue as _;
use spin_factors::{
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file can largely be ignored for review should you wish to as it's mostly just surface level refactoring.

let mut core_engine_builder = {
self.trigger.update_core_config(&mut self.engine_config)?;

spin_core::Engine::builder(&self.engine_config)?
};
self.trigger.add_to_linker(core_engine_builder.linker())?;

let runtime_config_path = options.runtime_config_file;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of this logic has been moved to the RuntimeFactorsBuilder implementation in spin-cli.

@@ -0,0 +1,42 @@
use anyhow::Context as _;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file might seem to be an unnecessary change, but it was the easiest way to continue to support setting key-value pairs on start up. I imagine even if we don't accept this PR, we'll want to implement this change.


use crate::factors::TriggerFactors;

pub fn summarize_runtime_config<T>(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of this has been moved to the spin-cli crate.

examples/spin-timer/Cargo.toml Outdated Show resolved Hide resolved
Copy link
Member

@radu-matei radu-matei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conceptually, I am 100% in favor of this change — it is a huge step in the direction of allowing embedders to assemble their own runtime that makes zero assumptions about the world they are targeting (and more specifically, making it trivial to build a trigger that does not implicitly assume the Spin world).

Thank you for the work that went into this!
(I plan to review in more detail and test this soon, but wanted to share my initial thoughts early on)

Copy link
Collaborator

@lann lann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initial pass

crates/factors/src/runtime_factors.rs Show resolved Hide resolved
crates/trigger-http/src/server.rs Outdated Show resolved Hide resolved
crates/trigger-http/src/wagi.rs Outdated Show resolved Hide resolved
let app_variables = trigger_app
.configured_app()
.app_state::<VariablesFactor>()
.context("RedisTrigger depends on VariablesFactor")?;

let app = trigger_app.app();
let trigger_type = <Self as Trigger<F>>::TYPE;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could probably avoid this awkwardness by adding a Trigger::trigger_type() -> &'static str method. 🤷

crates/runtime-factors/src/lib.rs Outdated Show resolved Hide resolved
@@ -0,0 +1,41 @@
[package]
name = "spin-runtime-factors"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably need a different name here given that RuntimeFactors is already a thing...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fully agree. I'd love to have a succienct way to differentiate between generic "any Spin runtime code" vs. code specific to a specific runtime. One could be tempted to call this spin-cli-runtime-factors, but I think that's almost too specific.

Copy link
Collaborator

@lann lann Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been thinking that "the spin up runtime" might be a good way to refer to this most-opinionated layer of the runtime. Maybe spin-up-runtime-factors here?

crates/trigger/src/cli.rs Outdated Show resolved Hide resolved
@rylev rylev force-pushed the generic-factors2 branch 3 times, most recently from 9fef2b1 to c661f97 Compare September 3, 2024 09:22
@@ -46,82 +49,85 @@ pub struct ResolvedRuntimeConfig<T> {
pub toml: toml::Table,
}

impl<T> ResolvedRuntimeConfig<T> {
pub fn summarize(&self, runtime_config_path: Option<&Path>) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this code here seems a bit odd. Is this crate intended to be tightly coupled to the Spin CLI?

If it stays here, maybe rename to something like print_summary to make its purpose more clear?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea I agree - though the inlining it is also not great. If someone wants to create an alternate RuntimeFactorsBuilder and still print out this summary (which I would expect since they're already going to get all other logging for free), they'll have to copy this code from the spin-runtime-factors crate.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about this more - I think as the crate stands today, this is the best place for this. This has a high chance to change though as we start tweaking things.

@@ -0,0 +1,41 @@
[package]
name = "spin-runtime-factors"
Copy link
Collaborator

@lann lann Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been thinking that "the spin up runtime" might be a good way to refer to this most-opinionated layer of the runtime. Maybe spin-up-runtime-factors here?

crates/trigger/src/cli.rs Show resolved Hide resolved
@rylev rylev enabled auto-merge September 4, 2024 09:29
@rylev rylev merged commit 3e62d2e into main Sep 4, 2024
17 checks passed
@rylev rylev deleted the generic-factors2 branch September 4, 2024 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants