Skip to content

Commit

Permalink
fix(clusters): query vlan ips per datacenter (#961)
Browse files Browse the repository at this point in the history
<!-- Please make sure there is an issue that this PR is correlated to. -->

## Changes

<!-- If there are frontend changes, please include screenshots. -->
  • Loading branch information
MasterPtato committed Jun 30, 2024
1 parent 9b31345 commit c2a7e3f
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions svc/pkg/cluster/worker/src/workers/server_provision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async fn inner(
ctx: OperationContext<cluster::msg::server_provision::Message>,
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> GlobalResult<()> {
let datacenter_id = unwrap!(ctx.datacenter_id);
let datacenter_id = unwrap!(ctx.datacenter_id).as_uuid();
let server_id = unwrap_ref!(ctx.server_id).as_uuid();
let pool_type = unwrap!(backend::cluster::PoolType::from_i32(ctx.pool_type));
let provider = unwrap!(backend::cluster::Provider::from_i32(ctx.provider));
Expand Down Expand Up @@ -57,7 +57,7 @@ async fn inner(

// Fetch datacenter config
let datacenter_res = op!([ctx] cluster_datacenter_get {
datacenter_ids: vec![datacenter_id],
datacenter_ids: vec![datacenter_id.into()],
})
.await?;
let datacenter = unwrap!(datacenter_res.datacenters.first());
Expand All @@ -70,7 +70,7 @@ async fn inner(
);

// Get a new vlan ip
let vlan_ip = get_vlan_ip(&ctx, tx, server_id, pool_type).await?;
let vlan_ip = get_vlan_ip(&ctx, tx, datacenter_id, server_id, pool_type).await?;

sql_execute!(
[ctx]
Expand Down Expand Up @@ -212,6 +212,7 @@ async fn inner(
async fn get_vlan_ip(
ctx: &OperationContext<cluster::msg::server_provision::Message>,
_tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
datacenter_id: Uuid,
server_id: Uuid,
pool_type: backend::cluster::PoolType,
) -> GlobalResult<Ipv4Addr> {
Expand All @@ -234,6 +235,10 @@ async fn get_vlan_ip(
FROM db_cluster.servers
WHERE
pool_type = $3 AND
-- Technically this should check all servers where their datacenter's provider and
-- provider_datacenter_id are the same because VLAN is separated by irl datacenter
-- but this is good enough
datacenter_id = $4 AND
network_idx = mod(idx + $1, $2) AND
cloud_destroy_ts IS NULL
)
Expand All @@ -242,7 +247,7 @@ async fn get_vlan_ip(
update_network_idx AS (
UPDATE db_cluster.servers
SET network_idx = (SELECT idx FROM get_next_network_idx)
WHERE server_id = $4
WHERE server_id = $5
RETURNING 1
)
SELECT idx FROM get_next_network_idx
Expand All @@ -251,7 +256,8 @@ async fn get_vlan_ip(
rand::thread_rng().gen_range(0i64..max_idx),
max_idx,
pool_type as i64,
server_id
datacenter_id,
server_id,
)
.await?;

Expand Down

0 comments on commit c2a7e3f

Please sign in to comment.