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

Refactor TriggerExecutor to have associated types for instances #2366

Merged
merged 1 commit into from
Mar 18, 2024

Conversation

alexcrichton
Copy link
Contributor

Moves the EitherInstance* logic into just the spin-trigger-http crate where the redis trigger, for example, only has to deal with components. The intention of this is to open up future customization of instances within a trigger that don't necessarily need to affect all other triggers.

@alexcrichton alexcrichton force-pushed the triggers-custom-instances branch 2 times, most recently from 3613467 to ea0e02b Compare March 15, 2024 17:06
@lann
Copy link
Collaborator

lann commented Mar 15, 2024

@itowlson Thoughts on how this might impact #2305?

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.

lgtm but @itowlson and/or @dicej probably ought to review

@lann lann requested review from itowlson and dicej March 15, 2024 18:42
@itowlson
Copy link
Contributor

Is the proposal that every custom trigger should have to copy the stuff that is added to the timer sample? I'm not enthusiastic about it if so. I'd like the direction to be helping trigger authors focus on the trigger rather than to making them wrangle wasmtime internals.

@lann I don't think this will affect service chaining, at least as it stands today. The current implementation re-enters at HttpHandlerExecutor::execute() which doesn't seem to be affected. I may be missing something though - that you're expressing concern makes me think I probably am.

@@ -113,15 +118,30 @@ impl TriggerExecutor for TimerTrigger {
}
Ok(())
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Per my comment on the issue, let's find a way that doesn't require every custom trigger to copy this code. (That might be as simple as defaulting things in the trait, I don't know.)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sadly this can't be defaulted as-is because you can't have associated type defaults on traits (rust-lang/rust#29661).

Copy link
Collaborator

Choose a reason for hiding this comment

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

@alexcrichton Would it be reasonable to add traits e.g. spin_core::SpinInstance(Pre) and have these methods return impl SpinInstance(Pre)?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I suppose the SpinInstance trait might be a bit gnarly there and could end up boiling down to a component-or-module enum in disguise. Depends on what exactly you're hoping to enable here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds like a reasonable concern to me! I ended up creating a TriggerInstancePre trait for the spin-trigger trait before I came here and read @lann's comments. I think that this should have the desired ergonomics but I'm happy to move/rename the trait too

Copy link
Collaborator

Choose a reason for hiding this comment

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

lgtm

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think given the amount of cargo-culty copying required to make a custom trigger today, this is fine. The whole interaction between spin-core, spin-app, and component instantiation is pretty wonky and became wonkier with the (partial) migration to components, and I don't think this trigger architecture can or should survive long-term.

Probably more important in the near-term would be to actually document (mea culpa) this interface a bit to help people along.

Copy link
Contributor

Choose a reason for hiding this comment

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

If I read this correctly, trigger authors would no longer cargo-cult the let EitherInstance::Component... fragment and instead cargo-cult the type InstancePre = ... fragment. That feels like less cargo-culting and I'm in favour. Thank you Alex for simplifying this!

(If I've misread it, though, @alexcrichton, please hold off so I can get my head around the actual experience.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No yep you've read correctly, and once Rust supports defaults for associated types we can remove the type InstancePre = ... as well (or at least that's my understanding of associated typ defaults)

@lann
Copy link
Collaborator

lann commented Mar 18, 2024

that you're expressing concern makes me think I probably am.

Nope, I just knew you've been iterating on that implementation and wanted to make sure you were in the loop here.

@alexcrichton alexcrichton force-pushed the triggers-custom-instances branch 2 times, most recently from f203ef5 to fa754ad Compare March 18, 2024 16:43
crates/trigger/src/cli.rs Outdated Show resolved Hide resolved
Moves the `EitherInstance*` logic into just the `spin-trigger-http`
crate where the redis trigger, for example, only has to deal with
components. The intention of this is to open up future customization of
instances within a trigger that don't necessarily need to affect all
other triggers.

The new associated type is bound itself by a new trait
`TriggerInstancePre` which encapsulates the required functionality. This
trait has a default implementation for `spin_core::InstancePre<T>` which
serves for triggers that only work with components.

Signed-off-by: Alex Crichton <alex@alexcrichton.com>
@alexcrichton alexcrichton merged commit d11bdd7 into fermyon:main Mar 18, 2024
15 checks passed
@alexcrichton alexcrichton deleted the triggers-custom-instances branch March 18, 2024 19:52
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.

4 participants