Skip to content

Commit

Permalink
feat: add fdb pool
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterPtato authored and NathanFlurry committed Nov 21, 2024
1 parent 4eedc91 commit 56d5348
Show file tree
Hide file tree
Showing 37 changed files with 1,895 additions and 141 deletions.
2 changes: 1 addition & 1 deletion packages/api/traefik-provider/src/route/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub async fn config(
let config = types::TraefikConfigResponse::default();

// Fetch configs and catch any errors
// build_cdn(&ctx, &mut config).await?;
build_cdn(&ctx, &mut config).await?;

// tracing::info!(
// http_services = ?config.http.services.len(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct ClusterPools {
pub pegboard: ClusterPoolPegboard,
pub gg: ClusterPoolGg,
pub ats: ClusterPoolAts,
pub fdb: ClusterPoolFdb,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -218,6 +219,27 @@ impl ClusterPoolAts {
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub struct ClusterPoolFdb {
pub vlan_ip_net: Option<Ipv4Net>,
pub firewall_rules: Option<Vec<FirewallRule>>,
}

impl ClusterPoolFdb {
pub fn vlan_ip_net(&self) -> Ipv4Net {
Ipv4Net::new(Ipv4Addr::new(10, 0, 2, 0), 26).unwrap()
}

pub fn vlan_addr_range(&self) -> Ipv4AddrRange {
self.vlan_ip_net().hosts()
}

pub fn firewall_rules(&self) -> Vec<FirewallRule> {
FirewallRule::base_rules()
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct FirewallRule {
pub label: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub enum PoolType {
Ats,
Pegboard,
PegboardIsolate,
Fdb,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down
92 changes: 91 additions & 1 deletion packages/infra/client/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/infra/client/container-runner/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ FROM clux/muslrust:1.81.0-stable
WORKDIR /app
COPY Cargo.toml .
COPY src/ src/
RUN cargo build --release --bin container-runner
RUN cargo build --release --bin rivet-container-runner
6 changes: 3 additions & 3 deletions packages/infra/client/echo/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM clux/muslrust:1.81.0-stable AS rust

WORKDIR /app
COPY . .
RUN cargo build --release --bin echo-server
RUN cargo build --release --bin pegboard-echo-server

# Create non-root user and group
RUN useradd -m -d /home/nonroot -s /bin/sh nonroot
Expand All @@ -18,9 +18,9 @@ FROM scratch
COPY --from=rust /passwd /etc/passwd
COPY --from=rust /group /etc/group

COPY --from=rust /app/target/x86_64-unknown-linux-musl/release/echo-server /echo-server
COPY --from=rust /app/target/x86_64-unknown-linux-musl/release/pegboard-echo-server /pegboard-echo-server

# Switch to the non-root user
USER nonroot

CMD ["/echo-server"]
CMD ["/pegboard-echo-server"]
1 change: 1 addition & 0 deletions packages/infra/client/isolate-v8-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ path = "src/main.rs"
anyhow = "1.0.79"
deno_ast = "0.42.1"
deno_core = "0.312.0"
foundationdb = {version = "0.9.1", features = [ "fdb-7_1", "embedded-fdb-include" ] }
futures-util = { version = "0.3" }
netif = "0.1.6"
nix = { version = "0.27", default-features = false, features = ["user", "signal"] }
Expand Down
6 changes: 5 additions & 1 deletion packages/infra/client/isolate-v8-runner/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ WORKDIR /app
COPY packages/infra/client/runner-protocol/ runner-protocol/
COPY packages/infra/client/isolate-v8-runner/Cargo.toml isolate-v8-runner/Cargo.toml
COPY packages/infra/client/isolate-v8-runner/src/ isolate-v8-runner/src/

# Installs shared libs
RUN apt-get update && apt-get install -y libclang-dev

RUN \
--mount=type=cache,target=/root/.cargo/git \
--mount=type=cache,target=/root/.cargo/registry \
--mount=type=cache,target=/app/isolate-v8-runner/target \
cd isolate-v8-runner && \
cargo build --release --target x86_64-unknown-linux-gnu && \
mkdir -p /app/dist && \
mv /app/isolate-v8-runner/target/x86_64-unknown-linux-gnu/release/isolate-v8-runner /app/dist/isolate-v8-runner
mv /app/isolate-v8-runner/target/x86_64-unknown-linux-gnu/release/rivet-isolate-v8-runner /app/dist/rivet-isolate-v8-runner

# Create an empty image and copy binaries into it to minimize the size of the image
FROM scratch
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
*

!packages/infra/pegboard/runner-protocol
!packages/infra/pegboard/v8-isolate-runner/Cargo.toml
!packages/infra/pegboard/v8-isolate-runner/src
!packages/infra/client/runner-protocol
!packages/infra/client/isolate-v8-runner/Cargo.toml
!packages/infra/client/isolate-v8-runner/src

18 changes: 17 additions & 1 deletion packages/infra/client/isolate-v8-runner/src/isolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
os::fd::FromRawFd,
path::{Path, PathBuf},
rc::Rc,
result::Result::{Err, Ok},
sync::{mpsc, Arc},
};

Expand All @@ -20,7 +21,7 @@ use nix::{libc, unistd::pipe};
use tokio::{fs, sync::watch};
use uuid::Uuid;

use crate::{config::Config, log_shipper};
use crate::{config::Config, utils, log_shipper};

pub fn run(actors_path: PathBuf, actor_id: Uuid, stop_rx: watch::Receiver<()>) -> Result<()> {
let actor_path = actors_path.join(actor_id.to_string());
Expand Down Expand Up @@ -121,6 +122,21 @@ async fn run_inner(
) -> Result<i32> {
println!("{actor_id}: Starting isolate");

// let db = utils::fdb_handle()?;

// db.run(|trx, _maybe_committed| async move {
// trx.set(b"hello", b"world");

// Ok(())
// })
// .await?;

// let res = db
// .run(|trx, _maybe_committed| async move { Ok(trx.get(b"hello", false).await?) })
// .await?;

// println!("{actor_id}: hello {:?}", std::str::from_utf8(&res.unwrap())?);

// Load script into a static module loader. No dynamic scripts can be loaded this way.
let script_content = fs::read_to_string(actor_path.join("index.js"))
.await
Expand Down
6 changes: 6 additions & 0 deletions packages/infra/client/isolate-v8-runner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::{

use anyhow::*;
use deno_runtime::deno_core::{v8_set_flags, JsRuntime};
use deno_runtime::deno_core::JsRuntime;
use foundationdb as fdb;
use futures_util::{stream::SplitStream, StreamExt};
use tokio::{
fs,
Expand Down Expand Up @@ -36,6 +38,10 @@ async fn main() -> Result<()> {

redirect_logs(working_path.join("log")).await?;

// Start FDB network thread
let _network = unsafe { fdb::boot() };
tokio::spawn(utils::fdb_health_check());

// Write PID to file
fs::write(
working_path.join("pid"),
Expand Down
31 changes: 31 additions & 0 deletions packages/infra/client/isolate-v8-runner/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
use std::{path::Path, result::Result::Ok, time::Duration};

use anyhow::*;
use foundationdb::{self as fdb, options::DatabaseOption};

pub fn var(name: &str) -> Result<String> {
std::env::var(name).context(name.to_string())
}

pub fn fdb_handle() -> fdb::FdbResult<fdb::Database> {
let cluster_file_path = Path::new("/etc/foundationdb/fdb.cluster");
let db = fdb::Database::from_path(&cluster_file_path.display().to_string())?;
db.set_option(DatabaseOption::TransactionRetryLimit(10))?;

Ok(db)
}

pub async fn fdb_health_check() -> Result<()> {
let db = fdb_handle()?;

loop {
match tokio::time::timeout(
Duration::from_secs(3),
db.run(|trx, _maybe_committed| async move { Ok(trx.get(b"", false).await?) }),
)
.await
{
Ok(res) => {
res?;
}
Err(_) => eprintln!("db missed ping"),
}

tokio::time::sleep(Duration::from_secs(3)).await;
}
}
Loading

0 comments on commit 56d5348

Please sign in to comment.