Skip to content

Commit

Permalink
chore: dont destroy anything
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterPtato committed Apr 15, 2024
1 parent ab22174 commit 02ff43c
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 41 deletions.
39 changes: 32 additions & 7 deletions svc/pkg/cluster/db/cluster/migrations/20231201000927_init.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,34 @@ WHERE cloud_destroy_ts IS NULL;

-- Stores data for destroying linode resources
CREATE TABLE servers_linode (
server_id UUID PRIMARY KEY REFERENCES servers (server_id),
server_id UUID NOT NULL REFERENCES servers (server_id),
ssh_key_id INT NOT NULL,
linode_id INT,
firewall_id INT
firewall_id INT,

destroy_ts INT
);

-- Effectively a conditional primary key
CREATE UNIQUE INDEX idx_servers_linode_pkey
ON servers_linode (server_id)
WHERE destroy_ts IS NULL;

-- Stores data for destroying cloudflare resources
CREATE TABLE servers_cloudflare (
server_id UUID PRIMARY KEY REFERENCES servers (server_id),
server_id UUID NOT NULL REFERENCES servers (server_id),
dns_record_id TEXT NOT NULL,
-- Secondary DNS route which doesn't have a wildcard. Used for discord activities.
secondary_dns_record_id TEXT
secondary_dns_record_id TEXT,

destroy_ts INT
);

-- Effectively a conditional primary key
CREATE UNIQUE INDEX idx_servers_cloudflare_pkey
ON servers_cloudflare (server_id)
WHERE destroy_ts IS NULL;

CREATE TABLE server_images (
provider INT,
install_hash TEXT,
Expand All @@ -108,11 +122,22 @@ CREATE TABLE server_images_linode (
public_ip INET,
image_id TEXT,

PRIMARY KEY (install_hash, datacenter_id, pool_type),
INDEX (public_ip),
INDEX (image_id)
destroy_ts INT
);

-- Effectively a conditional primary key
CREATE UNIQUE INDEX idx_server_images_linode_pkey
ON server_images_linode (install_hash, datacenter_id, pool_type)
WHERE destroy_ts IS NULL;

CREATE INDEX idx_server_images_linode_public_ip
ON server_images_linode (public_ip)
WHERE destroy_ts IS NULL;

CREATE INDEX idx_server_images_linode_image_id
ON server_images_linode (image_id)
WHERE destroy_ts IS NULL;

-- Dictates which cluster a game's lobbies will be created in
CREATE TABLE games (
game_id UUID PRIMARY KEY,
Expand Down
12 changes: 9 additions & 3 deletions svc/pkg/cluster/worker/src/workers/server_dns_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ async fn worker(
"
SELECT dns_record_id, secondary_dns_record_id
FROM db_cluster.servers_cloudflare
WHERE server_id = $1
WHERE
server_id = $1 AND
destroy_ts IS NULL
",
&server_id,
util::timestamp::now(),
Expand Down Expand Up @@ -61,10 +63,14 @@ async fn worker(
sql_execute!(
[ctx, &crdb]
"
DELETE FROM db_cluster.servers_cloudflare
WHERE server_id = $1
UPDATE db_cluster.servers_cloudflare
SET destroy_ts = $2
WHERE
server_id = $1 AND
destroy_ts IS NULL
",
server_id,
util::timestamp::now(),
)
.await?;

Expand Down
5 changes: 4 additions & 1 deletion svc/pkg/cluster/worker/src/workers/server_provision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ async fn worker(
if let Some(provision_res) = provision_res {
// Install components
if !provision_res.already_installed {
msg!([ctx] cluster::msg::server_install(&provision_res.public_ip) {
let request_id = Uuid::new_v4();

msg!([ctx] cluster::msg::server_install(request_id) {
request_id: Some(request_id.into()),
public_ip: provision_res.public_ip,
datacenter_id: ctx.datacenter_id,
server_id: ctx.server_id,
Expand Down
4 changes: 3 additions & 1 deletion svc/pkg/cluster/worker/tests/server_dns_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ async fn server_dns_create(ctx: TestCtx) {
SELECT EXISTS (
SELECT 1
FROM db_cluster.servers_cloudflare
WHERE server_id = $1
WHERE
server_id = $1 AND
destroy_ts IS NULL
)
",
server_id,
Expand Down
4 changes: 3 additions & 1 deletion svc/pkg/cluster/worker/tests/server_dns_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ async fn server_dns_delete(ctx: TestCtx) {
SELECT EXISTS (
SELECT 1
FROM db_cluster.servers_cloudflare
WHERE server_id = $1
WHERE
server_id = $1 AND
destroy_ts IS NULL
)
",
server_id,
Expand Down
4 changes: 3 additions & 1 deletion svc/pkg/cluster/worker/tests/server_drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ async fn gg_server_drain(ctx: TestCtx) {
SELECT EXISTS (
SELECT 1
FROM db_cluster.servers_cloudflare
WHERE server_id = $1
WHERE
server_id = $1 AND
destroy_ts IS NULL
)
",
server_id,
Expand Down
16 changes: 10 additions & 6 deletions svc/pkg/linode/ops/server-destroy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ struct LinodeData {
pub async fn handle(
ctx: OperationContext<linode::server_destroy::Request>,
) -> GlobalResult<linode::server_destroy::Response> {
let crdb = ctx.crdb().await?;
let server_id = unwrap_ref!(ctx.server_id).as_uuid();
let datacenter_id = unwrap!(ctx.datacenter_id);

Expand All @@ -24,11 +23,13 @@ pub async fn handle(
let datacenter = unwrap!(datacenter_res.datacenters.first());

let data = sql_fetch_optional!(
[ctx, LinodeData, &crdb]
[ctx, LinodeData]
"
SELECT ssh_key_id, linode_id, firewall_id
FROM db_cluster.servers_linode
WHERE server_id = $1
WHERE
server_id = $1 AND
destroy_ts IS NULL
",
server_id,
)
Expand All @@ -54,10 +55,13 @@ pub async fn handle(

// Remove record
sql_execute!(
[ctx, &crdb]
[ctx]
"
DELETE FROM db_cluster.servers_linode
WHERE server_id = $1
UPDATE db_cluster.servers_linode
SET destroy_ts = $2
WHERE
server_id = $1 AND
destroy_ts IS NULL
",
server_id,
)
Expand Down
8 changes: 6 additions & 2 deletions svc/pkg/linode/ops/server-provision/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ pub async fn handle(
"
UPDATE db_cluster.servers_linode
SET linode_id = $2
WHERE server_id = $1
WHERE
server_id = $1 AND
destroy_ts IS NULL
",
server_id,
linode_id as i64,
Expand Down Expand Up @@ -130,7 +132,9 @@ pub async fn handle(
"
UPDATE db_cluster.servers_linode
SET firewall_id = $2
WHERE server_id = $1
WHERE
server_id = $1 AND
destroy_ts IS NULL
",
server_id,
firewall_res.id as i64,
Expand Down
17 changes: 11 additions & 6 deletions svc/pkg/linode/standalone/gc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ async fn get_prebake_servers(
install_hash, datacenter_id, pool_type,
ssh_key_id, linode_id, firewall_id
FROM db_cluster.server_images_linode
WHERE image_id = ANY($1)
FOR UPDATE
WHERE
image_id = ANY($1) AND
destroy_ts IS NULL
FOR UPDATE
",
image_ids,
)
Expand Down Expand Up @@ -164,6 +166,7 @@ async fn get_prebake_servers(
s.install_hash = (q->>0)::TEXT AND
s.datacenter_id = (q->>1)::UUID AND
s.pool_type = (q->>2)::INT
WHERE destroy_ts IS NULL
) AS m
WHERE
i.provider = $2 AND
Expand All @@ -180,15 +183,17 @@ async fn get_prebake_servers(
sql_execute!(
[ctx, @tx tx]
"
DELETE FROM db_cluster.server_images_linode AS s
USING jsonb_array_elements($1::JSONB) AS q
UPDATE db_cluster.server_images_linode AS s
SET destroy_ts = $2
FROM jsonb_array_elements($1::JSONB) AS q
WHERE
s.install_hash = (q->>0)::TEXT AND
s.datacenter_id = (q->>1)::UUID AND
s.pool_type = (q->>2)::INT
s.pool_type = (q->>2)::INT AND
destroy_ts IS NULL
",
&primary_keys,
backend::cluster::Provider::Linode as i64,
util::timestamp::now(),
)
.await?;

Expand Down
6 changes: 4 additions & 2 deletions svc/pkg/linode/worker/src/workers/prebake_install_complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ async fn worker(
install_hash, datacenter_id, pool_type, linode_id, disk_id
FROM db_cluster.server_images_linode
WHERE
public_ip = $1
public_ip = $1 AND
destroy_ts IS NULL
",
public_ip,
)
Expand Down Expand Up @@ -62,7 +63,8 @@ async fn worker(
WHERE
install_hash = $1 AND
datacenter_id = $2 AND
pool_type = $3
pool_type = $3 AND
destroy_ts IS NULL
",
prebake_server.install_hash,
prebake_server.datacenter_id,
Expand Down
26 changes: 18 additions & 8 deletions svc/pkg/linode/worker/src/workers/prebake_provision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ async fn worker(

match provision(ctx, &crdb, &client, datacenter_id, &prebake_server).await {
Ok(public_ip) => {
let request_id = Uuid::new_v4();

// Continue to install
msg!([ctx] cluster::msg::server_install(&public_ip) {
msg!([ctx] cluster::msg::server_install(request_id) {
request_id: Some(request_id.into()),
public_ip: public_ip,
pool_type: ctx.pool_type,
server_id: None,
Expand Down Expand Up @@ -123,7 +126,8 @@ async fn provision(
WHERE
install_hash = $1 AND
datacenter_id = $2 AND
pool_type = $3
pool_type = $3 AND
destroy_ts IS NULL
",
util_cluster::INSTALL_SCRIPT_HASH,
datacenter_id,
Expand Down Expand Up @@ -156,7 +160,8 @@ async fn provision(
WHERE
install_hash = $1 AND
datacenter_id = $2 AND
pool_type = $3
pool_type = $3 AND
destroy_ts IS NULL
",
util_cluster::INSTALL_SCRIPT_HASH,
datacenter_id,
Expand All @@ -180,7 +185,8 @@ async fn provision(
WHERE
install_hash = $1 AND
datacenter_id = $2 AND
pool_type = $3
pool_type = $3 AND
destroy_ts IS NULL
",
util_cluster::INSTALL_SCRIPT_HASH,
datacenter_id,
Expand Down Expand Up @@ -214,7 +220,8 @@ async fn destroy(
WHERE
install_hash = $1 AND
datacenter_id = $2 AND
pool_type = $3
pool_type = $3 AND
destroy_ts IS NULL
",
util_cluster::INSTALL_SCRIPT_HASH,
datacenter_id,
Expand All @@ -239,17 +246,20 @@ async fn destroy(

// Remove record
sql_execute!(
[ctx]
[ctx, &crdb]
"
DELETE FROM db_cluster.server_images_linode
UPDATE db_cluster.server_images_linode
SET destroy_ts = $4
WHERE
install_hash = $1 AND
datacenter_id = $2 AND
pool_type = $3
pool_type = $3 AND
destroy_ts IS NULL
",
util_cluster::INSTALL_SCRIPT_HASH,
datacenter_id,
ctx.pool_type as i64,
util::timestamp::now(),
)
.await?;

Expand Down
6 changes: 4 additions & 2 deletions svc/pkg/linode/worker/tests/prebake_install_complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ async fn prebake_install_complete(ctx: TestCtx) {
install_hash = $2 AND
datacenter_id = $3 AND
pool_type = $4 AND
public_ip IS NOT NULL
public_ip IS NOT NULL AND
destroy_ts IS NULL
",
backend::cluster::Provider::Linode as i64,
util_cluster::INSTALL_SCRIPT_HASH,
Expand Down Expand Up @@ -89,7 +90,8 @@ async fn prebake_install_complete(ctx: TestCtx) {
install_hash = $2 AND
datacenter_id = $3 AND
pool_type = $4 AND
image_id IS NOT NULL
image_id IS NOT NULL AND
destroy_ts IS NULL
)
",
backend::cluster::Provider::Linode as i64,
Expand Down
3 changes: 2 additions & 1 deletion svc/pkg/linode/worker/tests/prebake_provision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ async fn prebake_provision(ctx: TestCtx) {
install_hash = $2 AND
datacenter_id = $3 AND
pool_type = $4 AND
public_ip IS NOT NULL
public_ip IS NOT NULL AND
destroy_ts IS NULL
)
",
backend::cluster::Provider::Linode as i64,
Expand Down

0 comments on commit 02ff43c

Please sign in to comment.