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

fix: restructure datacenter table #599

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions proto/backend/cluster.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ message Datacenter {
rivet.common.Uuid cluster_id = 2;
string name_id = 3;
string display_name = 4;

Provider provider = 5;
string provider_datacenter_id = 6;

repeated Pool pools = 7;
BuildDeliveryMethod build_delivery_method = 8;
// Nomad drain time in seconds.
Expand Down Expand Up @@ -63,5 +63,5 @@ message Server {

optional int64 cloud_destroy_ts = 7;

// TODO: Add the rest
// TODO: Add the rest of the sql columns
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ CREATE TABLE clusters (
CREATE TABLE datacenters (
datacenter_id UUID PRIMARY KEY,
cluster_id UUID NOT NULL REFERENCES clusters (cluster_id),
config BYTES NOT NULL,
name_id TEXT NOT NULL,
display_name TEXT NOT NULL,
provider INT NOT NULL,
provider_datacenter_id TEXT NOT NULL,
provider_api_token TEXT,
pools BYTES NOT NULL,
build_delivery_method INT NOT NULL,
drain_timeout INT NOT NULL,

UNIQUE (cluster_id, name_id),
INDEX (cluster_id)
Expand Down
54 changes: 48 additions & 6 deletions svc/pkg/cluster/ops/datacenter-get/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
use std::convert::{TryFrom, TryInto};

use proto::backend::{self, pkg::*};
use rivet_operation::prelude::*;

#[derive(sqlx::FromRow)]
struct Datacenter {
datacenter_id: Uuid,
cluster_id: Uuid,
name_id: String,
display_name: String,
provider: i64,
provider_datacenter_id: String,
pools: Vec<u8>,
build_delivery_method: i64,
drain_timeout: i64,
}

impl TryFrom<Datacenter> for backend::cluster::Datacenter {
type Error = GlobalError;

fn try_from(value: Datacenter) -> GlobalResult<Self> {
let pools = cluster::msg::datacenter_create::Pools::decode(value.pools.as_slice())?.pools;

Ok(backend::cluster::Datacenter {
datacenter_id: Some(value.datacenter_id.into()),
cluster_id: Some(value.cluster_id.into()),
name_id: value.name_id,
display_name: value.display_name,
provider: value.provider as i32,
provider_datacenter_id: value.provider_datacenter_id,
pools,
build_delivery_method: value.build_delivery_method as i32,
drain_timeout: value.drain_timeout as u64,
})
}
}

#[operation(name = "cluster-datacenter-get")]
pub async fn handle(
ctx: OperationContext<cluster::datacenter_get::Request>,
Expand All @@ -12,23 +47,30 @@ pub async fn handle(
.collect::<Vec<_>>();

let configs = sql_fetch_all!(
[ctx, (Vec<u8>,)]
[ctx, Datacenter]
"
SELECT
config
datacenter_id,
cluster_id,
name_id,
display_name,
provider,
provider_datacenter_id,
provider_api_token,
pools,
build_delivery_method,
drain_timeout
FROM db_cluster.datacenters
WHERE datacenter_id = ANY($1)
",
datacenter_ids
datacenter_ids,
)
.await?;

Ok(cluster::datacenter_get::Response {
datacenters: configs
.into_iter()
.map(|(config_bytes,)| {
backend::cluster::Datacenter::decode(config_bytes.as_slice()).map_err(Into::into)
})
.map(TryInto::try_into)
.collect::<GlobalResult<Vec<_>>>()?,
})
}
5 changes: 5 additions & 0 deletions svc/pkg/cluster/types/msg/datacenter-create.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ import "proto/backend/cluster.proto";
message Message {
rivet.backend.cluster.Datacenter config = 1;
}

// Helper proto for writing to sql
message Pools {
repeated rivet.backend.cluster.Pool pools = 1;
}
28 changes: 21 additions & 7 deletions svc/pkg/cluster/worker/src/workers/datacenter_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,39 @@ async fn worker(
}
}

let mut config_buf = Vec::with_capacity(config.encoded_len());
config.encode(&mut config_buf)?;
// Copy pools config to write to db
let pools = cluster::msg::datacenter_create::Pools {
pools: config.pools.clone(),
};

let mut pools_buf = Vec::with_capacity(pools.encoded_len());
pools.encode(&mut pools_buf)?;

sql_execute!(
[ctx]
"
INSERT INTO db_cluster.datacenters (
datacenter_id,
cluster_id,
config,
name_id
name_id,
display_name,
provider,
provider_datacenter_id,
pools,
build_delivery_method,
drain_timeout
)
VALUES ($1, $2, $3, $4)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
",
datacenter_id,
cluster_id,
config_buf,
// Datacenters have a unique constraint on name ids
&config.name_id,
&config.display_name,
config.provider as i64,
&config.provider_datacenter_id,
pools_buf,
config.build_delivery_method as i64,
config.drain_timeout as i64
)
.await?;

Expand Down
Loading