Skip to content

Commit

Permalink
feat(pegboard): implement v8 isolate runner (#1213)
Browse files Browse the repository at this point in the history
<!-- Please make sure there is an issue that this PR is correlated to. -->
Fixes RVT-3855
## Changes

<!-- If there are frontend changes, please include screenshots. -->
  • Loading branch information
MasterPtato committed Oct 22, 2024
1 parent f2d04d1 commit dc4d97e
Show file tree
Hide file tree
Showing 50 changed files with 2,567 additions and 596 deletions.
69 changes: 69 additions & 0 deletions lib/bolt/core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
[package]
name = "bolt-core"
version = "0.1.0"
authors = ["Rivet Gaming, LLC <developer@rivet.gg>"]
edition = "2021"
license = "Apache-2.0"

[dependencies]
anyhow = "1.0"
async-posthog = { git = "https://github.com/rivet-gg/posthog-rs.git", rev = "ef4e80e" }
async-recursion = "0.3"
async-trait = "0.1"
base64 = "0.13"
bolt-config = { path = "../config" }
chrono = "0.4"
cjson = "0.1"
clap = { version = "4.3", features = ["derive"] }
colored_json = "5.0.0"
derive_builder = "0.12"
duct = "0.13"
futures-util = "0.3"
handlebars = "4.3.7"
heck = "0.3"
hex = "0.4"
indexmap = "2.0"
indicatif = "0.16"
indoc = "1.0"
ipnet = { version = "2.7", features = ["serde"] }
json-patch = "1.2"
lazy_static = "1.4"
maplit = "1.0"
portpicker = "0.1"
prost = "0.10"
rand = "0.8"
regex = "1.5"
reqwest = { version = "0.11", features = ["json"] }
rivet-api = { path = "../../../sdks/full/rust" }
rivet-term = { git = "https://github.com/rivet-gg/rivet-term.git", rev = "d539a07d2920d47b88410f20e6d106b497cff1f5" }
s3-util = { path = "../../s3-util" }
serde = { version ="1.0", features = ["derive"] }
serde_json = "1.0"
serde_yaml = "0.9"
sha2 = "0.10"
strum = { version = "0.26", features = ["derive"] }
tabled = { version = "0.16.0", features = ["ansi"] }
tempfile = "3.2"
thiserror = "1.0"
tokio = { version = "1.29", features = ["full"] }
toml = "0.7"
toml_edit = "0.19"
url = "2.3"
urlencoding = "2.1"
uuid = { version = "1", features = ["v4"] }
wildmatch = "2.1"

[dependencies.sqlx]
git = "https://github.com/rivet-gg/sqlx"
rev = "e7120f59b74fb6d83ac9b1d899b166bab31ba1d6"
default-features = false
features = [
"runtime-tokio",
"runtime-tokio-native-tls",
"postgres",
"uuid",
"macros",
"ipnetwork",
"json",
"bit-vec",
]
4 changes: 2 additions & 2 deletions lib/chirp-workflow/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ serde = { version = "1.0.198", features = ["derive"] }
serde_json = "1.0.116"
strum = { version = "0.26", features = ["derive"] }
thiserror = "1.0.59"
tokio = { version = "1.37.0", features = ["full"] }
tokio = { version = "1.36.0", features = ["full"] }
tokio-util = "0.7"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
uuid = { version = "1.8.0", features = ["v4", "serde"] }

[dependencies.sqlx]
git = "https://github.com/rivet-gg/sqlx"
rev = "08d6e61aa0572e7ec557abbedb72cebb96e1ac5b"
rev = "e7120f59b74fb6d83ac9b1d899b166bab31ba1d6"
default-features = false
features = [
"runtime-tokio",
Expand Down
2 changes: 1 addition & 1 deletion lib/chirp-workflow/core/src/db/pg_nats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1706,7 +1706,7 @@ mod types {
fn encode_by_ref(
&self,
buf: &mut sqlx::postgres::PgArgumentBuffer,
) -> sqlx::encode::IsNull {
) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> {
<serde_json::Value as sqlx::Encode<'q, sqlx::Postgres>>::encode(
serialize_location(self),
buf,
Expand Down
8 changes: 8 additions & 0 deletions lib/pegboard/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[workspace]
members = [
"container-runner",
"echo",
"manager",
"runner-protocol",
"v8-isolate-runner",
]
5 changes: 0 additions & 5 deletions lib/pegboard/container-runner/.dockerignore

This file was deleted.

2 changes: 1 addition & 1 deletion lib/pegboard/container-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ license = "Apache-2.0"

[dependencies]
anyhow = "1.0.79"
portpicker = "0.1.1"
serde = { version = "1.0.195", features = ["derive"] }
serde_json = "1.0.111"
signal-hook = "0.3.17"

[dev-dependencies]
portpicker = "0.1.1"
tempfile = "3.9.0"
uuid = { version = "1.6.1", features = ["v4"] }

4 changes: 2 additions & 2 deletions lib/pegboard/container-runner/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM clux/muslrust:1.81.0-stable

WORKDIR /app
COPY . .
COPY Cargo.toml .
COPY src/ src/
RUN cargo build --release --bin container-runner

2 changes: 0 additions & 2 deletions lib/pegboard/container-runner/Dockerfile.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
!Cargo.lock
!Cargo.toml
!src


4 changes: 2 additions & 2 deletions lib/pegboard/container-runner/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# container-runner

This crate is used to run OCI bundles on the job servers themselves. This takes care of trapping signals, log
shipping, rate limiting logs, and more.
This crate is used to run OCI bundles on the pegboard servers themselves. This takes care of trapping signals,
log shipping, rate limiting logs, and more.

## Deployment

Expand Down
2 changes: 1 addition & 1 deletion lib/pegboard/container-runner/src/log_shipper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl LogShipper {
}

fn run_inner(&self) -> Result<()> {
let vector_socket_addr = var("PEGBOARD_META_vector_socket_addr")?;
let vector_socket_addr = var("VECTOR_SOCKET_ADDR")?;

println!("Connecting log shipper to Vector at {vector_socket_addr}");

Expand Down
32 changes: 16 additions & 16 deletions lib/pegboard/container-runner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ const MAX_BUFFER_BYTES: usize = 1024 * 1024;
/// identify the reasons for program crashes from the container's output.
const MAX_PREVIEW_LINES: usize = 128;

fn main() -> anyhow::Result<()> {
let pegboard_actor_dir = std::env::args()
fn main() -> Result<()> {
let actor_path = std::env::args()
.skip(1)
.next()
.context("`actor_dir` arg required")?;
let pegboard_actor_dir = Path::new(&pegboard_actor_dir);
.context("`actor_path` arg required")?;
let actor_path = Path::new(&actor_path);

// Write PID to file
fs::write(
pegboard_actor_dir.join("pid"),
actor_path.join("pid"),
std::process::id().to_string().as_bytes(),
)?;

let root_user_enabled = var("PEGBOARD_META_root_user_enabled")? == "1";
let stakeholder = match var("PEGBOARD_META_stakeholder").ok() {
let root_user_enabled = var("ROOT_USER_ENABLED")? == "1";
let stakeholder = match var("STAKEHOLDER").ok() {
Some(x) if x == "dynamic_server" => Stakeholder::DynamicServer {
server_id: var("PEGBOARD_META_server_id")?,
server_id: var("SERVER_ID")?,
},
Some(x) => bail!("invalid actor stakeholder: {x}"),
None => bail!("no actor stakeholder specified"),
Expand All @@ -61,7 +61,7 @@ fn main() -> anyhow::Result<()> {
let log_shipper_thread = log_shipper.spawn();

// Run the container
let exit_code = match run_container(msg_tx.clone(), &pegboard_actor_dir, root_user_enabled) {
let exit_code = match run_container(msg_tx.clone(), &actor_path, root_user_enabled) {
Result::Ok(exit_code) => exit_code,
Err(err) => {
eprintln!("run container failed: {err:?}");
Expand Down Expand Up @@ -96,7 +96,7 @@ fn main() -> anyhow::Result<()> {
}

fs::write(
pegboard_actor_dir.join("exit-code"),
actor_path.join("exit-code"),
exit_code.to_string().as_bytes(),
)?;

Expand All @@ -108,17 +108,17 @@ fn main() -> anyhow::Result<()> {
/// Returns the exit code of the container that will be passed to the parent
fn run_container(
msg_tx: mpsc::SyncSender<log_shipper::ReceivedMessage>,
pegboard_actor_dir: &Path,
actor_path: &Path,
root_user_enabled: bool,
) -> anyhow::Result<i32> {
// Extract actor id from dir
let actor_id = pegboard_actor_dir
) -> Result<i32> {
// Extract actor id from path
let actor_id = actor_path
.iter()
.last()
.context("empty `pegboard_actor_dir`")?
.context("empty `actor_path`")?
.to_string_lossy()
.to_string();
let oci_bundle_path = pegboard_actor_dir.join("oci-bundle");
let oci_bundle_path = actor_path.join("oci-bundle");
let oci_bundle_config_json = oci_bundle_path.join("config.json");

// Validate OCI bundle
Expand Down
2 changes: 2 additions & 0 deletions lib/pegboard/echo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ fn main() {
println!("Listening on {addr}");

for mut request in server.incoming_requests() {
println!("req");

let mut content = Vec::new();
request.as_reader().read_to_end(&mut content).unwrap();

Expand Down
9 changes: 6 additions & 3 deletions lib/pegboard/manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ indoc = "2.0"
lazy_static = "1.4"
nix = { version = "0.27", default-features = false, features = ["user", "signal"] }
notify = { version = "6.1.1", default-features = false, features = ["serde", "fsevent-sys"] }
portpicker = "0.1"
prometheus = "0.13"
rand = "0.8"
reqwest = { version = "0.11", features = ["stream"] }
Expand All @@ -35,14 +34,15 @@ tracing-subscriber = { version = "0.3", default-features = false, features = [
"fmt",
"json",
] }
url = "2.5.0"
url = "2.4"
uuid = { version = "1.6.1", features = ["v4"] }

runner-protocol = { path = "../runner-protocol" }
pegboard = { path = "../../../svc/pkg/pegboard" }

[dependencies.sqlx]
git = "https://github.com/rivet-gg/sqlx"
rev = "08d6e61aa0572e7ec557abbedb72cebb96e1ac5b"
rev = "e7120f59b74fb6d83ac9b1d899b166bab31ba1d6"
default-features = false
features = [
"runtime-tokio",
Expand All @@ -52,3 +52,6 @@ features = [
"json",
"ipnetwork"
]

[dev-dependencies]
portpicker = "0.1"
12 changes: 10 additions & 2 deletions lib/pegboard/manager/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ COPY . .
RUN \
--mount=type=cache,target=/root/.cargo/git \
--mount=type=cache,target=/root/.cargo/registry \
--mount=type=cache,target=/app/target \
--mount=type=cache,target=/app/lib/pegboard/target \
cd lib/pegboard/manager && \
RUSTFLAGS="--cfg tokio_unstable" cargo build --release --package pegboard-manager --bin pegboard-manager && \
mkdir -p /app/dist && \
cp /app/target/x86_64-unknown-linux-musl/release/pegboard-manager /app/dist/pegboard-manager
mv /app/target/target/x86_64-unknown-linux-musl/release/pegboard-manager /app/dist/pegboard-manager

# Create an empty image and copy binaries into it to minimize the size of the image
FROM scratch
COPY --from=rust /app/dist /

# Allows `docker create` to work even though this fails
CMD [""]
1 change: 0 additions & 1 deletion lib/pegboard/manager/Dockerfile.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@

sdks/runtime
svc/**/*.md

Loading

0 comments on commit dc4d97e

Please sign in to comment.