DevLog[1]: Building a serverless platform for Rust in 4 weeks – part deux

23
[favorite_button]
DevLog[1]: Building a serverless platform for Rust in 4 weeks – part deux
Advertisements

shuttle is a serverless platform built for Rust. The goal of shuttle is to create the best possible developer experience for deploying Rust apps. Also, shuttle introduces a new paradigm for developing on the cloud called Infrastructure From Code (IFC).

IFC uses application code as the source of truth for provisioning infrastructure. No longer are your applications and servers decoupled, the two go hand in hand. shuttle does this by doing static analysis of user code and generating the corresponding infrastructure in real time. A bit like this:

In the previous DevLog we started the journey of building the shuttle MVP. We went over the design and implementation of the cargo subcommand which deploys cargo projects to shuttle. This has been a race against the clock, so corners were cut and tradeoffs were made. A similar theme emerges in this DevLog which covers the deployment state machine. We’re going to think about compiling and deploying user code, while also covering one of my favourite design patterns in Rust.

Deployment State
shuttle exposes an HTTP endpoint under POST /deploy. This endpoint receives a series of bytes, from cargo shuttle, which correspond to a packaged cargo project (basically a compressed tarball with a bunch of .rs files).

Advertisements

The aim of the Game, is to convert that series of bytes into a deployed web service – how do we go about doing that?

The deployment process is broken into 4 stages:

Queued – the cargo project is received and waiting to be compiled
Built – the cargo project is compiled successfully
Loaded – the output of the compilation is loaded as a dynamically-linked library
Deployed – the app inside the DLL is running and listening for connections

Then life happens so you need a couple more states:

Advertisements

Error – there was an issue anywhere in the build process
Deleted – user-initiated deletion of the deploymentThis endpoint

Which corresponds to:

All this can be expressed nicely in an enum since all these states are mutually exclusive:

1enum DeploymentState {
2 Queued,
3 Built,
4 Loaded,
5 Deployed,
6 Error,
7 Deleted
8}
9
Even though we have a nice representation of our states – these states don’t actually hold any data yet and the state transitions are not defined. We would like the DeploymentState to own all the data that corresponds to the specific stage in it’s deployment. We’ll create some structs to hold the data required for each stage.

Advertisements

First, the QueuedState just has a vector of bytes from the packaged cargo project that was received from cargo-shuttle:

1struct QueuedState {
2 crate_bytes: Vec,
3}
4
When a deployment is queued, the shuttle build system writes the crate_bytes (just a tarball of a cargo project) to the file system. It then extracts the tarball and starts the compilation process by running cargo::ops::compile.

The output of the build process is an .so file which is held in the next stage – the BuildState:

1struct BuiltState {
2 so_path: PathBuf,
3}
4
So far so good. At this point we have a pointer to a compiled shared object file – next we need to load it into memory.

Advertisements

shuttle uses the libloading crate to dynamically load from a .so file a value of a type implementing the Service trait. The Service trait is code-generated for the user via the #[shuttle_service::main] annotation and it’s how shuttle interfaces with client apps.

1pub struct LoadedState {
2 service: Box,
3 so: Library,
4}
5
We keep the Library struct around since Box is just a pointer to data loaded and managed by Library. Library going out of scope deallocates that data; meaning service will be pointing to deallocated memory hence we get a segfault. So it’s important to keep Library around for the lifetime of the deployment.

Finally we find a free port, spin Up a new tokio runtime (we keep the handle so that we can kill it in the future) and bind the service to the port. We’ll be covering this stuff in depth on a future DevLog but if you’re insatiably curious you can check out the source.

All of this is put into the DeployedState and we’re done!

Advertisements

1struct DeployedState {
2 so: Library,
3 port: Port,
4 handle: ServeHandle,
5}
6
To tie it all together, we modify our initial DeploymentState own the various states corresponding to the stages of the deployment process:

1enum DeploymentState {
2 Queued(QueuedState),
3 Built(BuiltState),
4 Loaded(LoadedState),
5 Deployed(DeployedState),
6 Error(anyhow::Error),
7 Deleted
8}
9
We also wrote a really light impl to define the state transitions:

1impl DeploymentState {
2 fn queued(crate_bytes: Vec) -> Self {
3 Self::Queued(QueuedState { crate_bytes })
4 }
5
6 fn built(build: Build) -> Self {
7 Self::Built(BuiltState { build })
8 }
9
10 fn loaded(loader: Loader) -> Self {
11 Self::Loaded(loader)
12 }
13
14 fn deployed(
15 so: Library,
16 port: Port,
17 handle: ServeHandle
18 ) -> Self {
19 Self::Deployed(DeployedState {
20 so,
21 port,
22 handle,
23 })
24 }
25}
26
You’ll also notice that there is no mutation happening here. We found it cleaner to simply drop the old state and construct a new one (although we did try).

Conclusion
In the case of shuttle, using enum variants and structs to represent states in a state machine seemed like the natural thing to do. The states were distinct and clear, and for the most part the transitions are clean and self-contained.

Advertisements

So what do you think about enum variants as states in a state machine? What would you have done differently?

Next Steps
In the next DevLog we’ll be looking at the implementation of our reverse proxy and routing table – how we keep a ledger of deployed services and route network calls appropriately.

In the meantime, if you want to try out shuttle head over to the getting started section! It’s completely free while shuttle is still in Alpha.

Read More
Share this on knowasiak.com to discuss with people on this topicSign Up on Knowasiak.com now if you’re not registered yet.

Advertisements
Advertisements
Knowasiak
WRITEN BY

Knowasiak

Hey! look, i give tutorials to all my users and i help them!
Get Connected!
One of the Biggest Social Platform for Entrepreneurs, College Students and all. Come and join our community. Expand your network and get to know new people!

Discussion(s)

No comments yet
Knowasiak We would like to show you notifications so you don't miss chats & status updates.
Dismiss
Allow Notifications