From 08baa4fe5c0fb4c20f3e467153fa526a121e131a Mon Sep 17 00:00:00 2001 From: MasterPtato Date: Fri, 23 May 2025 23:22:37 +0000 Subject: [PATCH] fix: disambiguate cluster_id <-> instance_id --- .../config/src/config/server/rivet/mod.rs | 12 ++++--- .../install_scripts/components/rivet/guard.rs | 1 + .../components/rivet/worker.rs | 1 + .../20250523120501_instance_id.down.sql | 0 .../20250523120501_instance_id.up.sql | 2 ++ .../dynamic-config/src/ops/get_config.rs | 32 +++++++++---------- .../telemetry/standalone/beacon/src/lib.rs | 28 ++++++++-------- 7 files changed, 42 insertions(+), 34 deletions(-) create mode 100644 packages/core/services/dynamic-config/db/dynamic-config/migrations/20250523120501_instance_id.down.sql create mode 100644 packages/core/services/dynamic-config/db/dynamic-config/migrations/20250523120501_instance_id.up.sql diff --git a/packages/common/config/src/config/server/rivet/mod.rs b/packages/common/config/src/config/server/rivet/mod.rs index f402531a62..976b857a9b 100644 --- a/packages/common/config/src/config/server/rivet/mod.rs +++ b/packages/common/config/src/config/server/rivet/mod.rs @@ -60,6 +60,11 @@ pub struct Rivet { #[serde(default = "Rivet::default_namespace")] pub namespace: String, + /// If specified, will use this as the default cluster ID. + /// + /// This will have no effect if applied after the cluster has first ran. + pub instance_id: Option, + /// If specified, will use this as the default cluster ID. /// /// This will have no effect if applied after the cluster has first ran. @@ -139,6 +144,7 @@ impl Default for Rivet { fn default() -> Rivet { Self { namespace: Self::default_namespace(), + instance_id: None, default_cluster_id: None, clusters: None, provision: None, @@ -171,7 +177,7 @@ impl Rivet { } } -impl Rivet { +impl Rivet { pub fn default_cluster_id(&self) -> GlobalResult { if let Some(default_cluster_id) = self.default_cluster_id { ensure!( @@ -184,9 +190,7 @@ impl Rivet { // Return default development clusters AccessKind::Development => Ok(default_dev_cluster::CLUSTER_ID), // No cluster configured - AccessKind::Public | AccessKind::Private => { - bail!("`default_cluster_id` not configured") - } + AccessKind::Public | AccessKind::Private => bail!("`default_cluster_id` not configured"), } } } diff --git a/packages/core/services/cluster/src/workflows/server/install/install_scripts/components/rivet/guard.rs b/packages/core/services/cluster/src/workflows/server/install/install_scripts/components/rivet/guard.rs index 4206a079e3..3436c22152 100644 --- a/packages/core/services/cluster/src/workflows/server/install/install_scripts/components/rivet/guard.rs +++ b/packages/core/services/cluster/src/workflows/server/install/install_scripts/components/rivet/guard.rs @@ -36,6 +36,7 @@ pub fn configure(config: &rivet_config::Config) -> GlobalResult { tls: server_config.tls.clone(), rivet: Rivet { namespace: server_config.rivet.namespace.clone(), + instance_id: server_config.rivet.instance_id, auth: server_config.rivet.auth.clone(), api_public: ApiPublic { // NOTE: Templated later diff --git a/packages/core/services/cluster/src/workflows/server/install/install_scripts/components/rivet/worker.rs b/packages/core/services/cluster/src/workflows/server/install/install_scripts/components/rivet/worker.rs index fbb4296026..d9314b72cd 100644 --- a/packages/core/services/cluster/src/workflows/server/install/install_scripts/components/rivet/worker.rs +++ b/packages/core/services/cluster/src/workflows/server/install/install_scripts/components/rivet/worker.rs @@ -36,6 +36,7 @@ pub fn configure(config: &rivet_config::Config) -> GlobalResult { tls: server_config.tls.clone(), rivet: Rivet { namespace: server_config.rivet.namespace.clone(), + instance_id: server_config.rivet.instance_id, clusters: Some({ let mut clusters = HashMap::new(); diff --git a/packages/core/services/dynamic-config/db/dynamic-config/migrations/20250523120501_instance_id.down.sql b/packages/core/services/dynamic-config/db/dynamic-config/migrations/20250523120501_instance_id.down.sql new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/core/services/dynamic-config/db/dynamic-config/migrations/20250523120501_instance_id.up.sql b/packages/core/services/dynamic-config/db/dynamic-config/migrations/20250523120501_instance_id.up.sql new file mode 100644 index 0000000000..b27d85bde1 --- /dev/null +++ b/packages/core/services/dynamic-config/db/dynamic-config/migrations/20250523120501_instance_id.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE config + RENAME COLUMN cluster_id TO rivet_instance_id; diff --git a/packages/core/services/dynamic-config/src/ops/get_config.rs b/packages/core/services/dynamic-config/src/ops/get_config.rs index 1bf83c8486..45025494f8 100644 --- a/packages/core/services/dynamic-config/src/ops/get_config.rs +++ b/packages/core/services/dynamic-config/src/ops/get_config.rs @@ -1,51 +1,51 @@ use chirp_workflow::prelude::*; use tokio::sync::OnceCell; -// The cluster ID will never change, so store it in memory. -static CLUSTER_ID_ONCE: OnceCell = OnceCell::const_new(); +// The instance ID will never change, so store it in memory. +static INSTANCE_ID_ONCE: OnceCell = OnceCell::const_new(); #[derive(Debug, Default)] pub struct Input {} #[derive(Debug)] pub struct Output { - pub cluster_id: Uuid, + pub instance_id: Uuid, } #[operation] -pub async fn get_cluster_id(ctx: &OperationCtx, input: &Input) -> GlobalResult { +pub async fn get_config(ctx: &OperationCtx, input: &Input) -> GlobalResult { // IMPORTANT: This is not the same as the cluster ID from the `cluster` package. This is used - // for unqiuely identifying the entire Rivet cluster. + // for uniquely identifying the entire Rivet cluster. - // Pick a cluster ID to insert if none exists. If this is specified in the config. fall back to + // Pick an instance ID to insert if none exists. If this is specified in the config. fall back to // this. - let default_cluster_id = - if let Some(cluster_id) = ctx.config().server()?.rivet.default_cluster_id { - cluster_id + let default_instance_id = + if let Some(instance_id) = ctx.config().server()?.rivet.instance_id { + instance_id } else { Uuid::new_v4() }; - let cluster_id = CLUSTER_ID_ONCE + let instance_id = INSTANCE_ID_ONCE .get_or_try_init(|| async { sql_fetch_one!( [ctx, (Uuid,)] " WITH new_row AS ( - INSERT INTO db_dynamic_config.config (id, cluster_id) + INSERT INTO db_dynamic_config.config (id, rivet_instance_id) VALUES (1, $1) ON CONFLICT (id) DO NOTHING - RETURNING cluster_id + RETURNING rivet_instance_id ) - SELECT cluster_id + SELECT rivet_instance_id FROM new_row UNION ALL - SELECT cluster_id + SELECT rivet_instance_id FROM db_dynamic_config.config WHERE NOT EXISTS (SELECT 1 FROM new_row) ", - default_cluster_id + default_instance_id ) .await .map(|x| x.0) @@ -53,6 +53,6 @@ pub async fn get_cluster_id(ctx: &OperationCtx, input: &Input) -> GlobalResult) -> GlobalResult { // use pegboard::protocol::ClientFlavor;