|
| 1 | +title = "SIP 0XX - Spin Factors" |
| 2 | +template = "main" |
| 3 | +date = "2024-05-20T12:00:00Z" |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +Summary: Refactor Spin Runtime Functionality with "Factors" |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +Created: May 20, 2024 |
| 12 | + |
| 13 | +## Background |
| 14 | + |
| 15 | +Spin 1.0 shipped with a mechanism for extending the runtime environment with |
| 16 | +loosely-coupled host functionality called "host components". As Spin has |
| 17 | +evolved, more and more runtime functionality has fallen outside of the scope of |
| 18 | +this mechanism, leading to the code for these features spreading out over the |
| 19 | +Spin codebase. This not only makes it hard to read and understand these features |
| 20 | +but also raises the bar for unfamiliar developers trying to add new features. |
| 21 | + |
| 22 | +Separately, the introduction of the SpinKube project has made it clear that |
| 23 | +there will be (at least) two major embeddings of the Spin runtime environment: |
| 24 | +the Spin CLI (`spin up`) and SpinKube (`containerd-shim-spin`). In order to |
| 25 | +provide better integration into the Kubernetes ecosystem, some runtime feature |
| 26 | +implementations will diverge between these embeddings, making the loose coupling |
| 27 | +of features even more important. |
| 28 | + |
| 29 | +## Proposal |
| 30 | + |
| 31 | +The basic inversion of control approach used by `HostComponent`s will be |
| 32 | +redesigned and expanded into a new system called Spin Factors, where independent |
| 33 | +feature sets are organized into individual "factors". Some of the goals of this |
| 34 | +new system: |
| 35 | + |
| 36 | +- Expand the set integration points that a factor can use to hook into the |
| 37 | + lifecycle of an application request. These will be driven by feature |
| 38 | + requirements and may include e.g.: |
| 39 | + - Runtime startup |
| 40 | + - Application initialization |
| 41 | + - Runtime config parsing |
| 42 | + - Component pre-instantiation |
| 43 | + - Component instantiation |
| 44 | + - Cleanup (post-execution) |
| 45 | + |
| 46 | +- Allow loosely-coupled dependencies between "factors" (features). In order to |
| 47 | + simplify implementation and reasoning about these dependencies the dependency |
| 48 | + graph must be acyclic. |
| 49 | + |
| 50 | +## Implementation Plan |
| 51 | + |
| 52 | +The overall implementation plan will be to: |
| 53 | + |
| 54 | +1. Introduce a new `spin-factors` crate containing the basic framework. |
| 55 | + |
| 56 | +2. Refactor existing Spin features (mostly existing `HostComponent`s) to use |
| 57 | + this new framework. |
| 58 | + - Framework features will be developed as needed to support this refactoring. |
| 59 | + |
| 60 | +3. Refactor `spin-core`, `spin-trigger`, etc. to use `spin-factors`. |
| 61 | + - Depending on subjective evaluation at this point, possibly merge |
| 62 | + `spin-core` and `spin-factors`. |
| 63 | + |
| 64 | +## Implementation Details |
| 65 | + |
| 66 | +Based on initial prototyping, the following Rust types represent the starting |
| 67 | +point for `spin-factors` (note that some type details are elided for clarity): |
| 68 | + |
| 69 | +```rust |
| 70 | +pub trait Factor { |
| 71 | + // Runtime configuration |
| 72 | + type RuntimeConfig; |
| 73 | + // Application-wide state; may be reused between instances |
| 74 | + type AppState; |
| 75 | + // Builds instance state |
| 76 | + type InstanceBuilder: FactorInstanceBuilder; |
| 77 | + |
| 78 | + // Init is the runtime startup lifecycle hook. |
| 79 | + // |
| 80 | + // The `InitContext` type here gives the factor the ability to update global |
| 81 | + // engine configuration, most notably the `Linker`. This takes the place of |
| 82 | + // `HostComponent::add_to_linker`. |
| 83 | + fn init(&mut self, ctx: InitContext) -> Result<()> { |
| 84 | + Ok(()) |
| 85 | + } |
| 86 | + |
| 87 | + // Validates application configuration and prepares AppState. |
| 88 | + // |
| 89 | + // Takes the place of `DynamicHostComponent::validate_app`. |
| 90 | + fn configure_app(&self, ctx: ConfigureAppContext) -> Result<Self::AppState>; |
| 91 | + |
| 92 | + // Prepares InstanceState. This can access other Factors' InstanceBuilders |
| 93 | + // to implement inter-factor dependencies. |
| 94 | + fn prepare( |
| 95 | + &self, |
| 96 | + ctx: PrepareContext, |
| 97 | + builders: &mut InstanceBuilders, |
| 98 | + ) --> Result<Self::InstanceBuilder> |
| 99 | +} |
| 100 | + |
| 101 | +pub trait FactorInstanceBuilder { |
| 102 | + // Per-instance state, equivalent to `HostComponent::Data`. |
| 103 | + type InstanceState; |
| 104 | + |
| 105 | + // Builds the InstanceState. |
| 106 | + // |
| 107 | + // Takes the place of `HostComponent::build_data` and |
| 108 | + // `DynamicHostComponent::update_data`. |
| 109 | + fn build(self) -> Result<Self::InstanceState>; |
| 110 | +} |
| 111 | +``` |
0 commit comments