From dc72c1eadd49e22cc9aa4432388c75fdb5962108 Mon Sep 17 00:00:00 2001 From: dynco-nym <173912580+dynco-nym@users.noreply.github.com> Date: Wed, 30 Oct 2024 03:15:11 +0100 Subject: [PATCH] Testrun stores gw identity key instead of gw pk --- Cargo.lock | 2 +- common/models/src/ns_api.rs | 3 +- nym-node-status-api/Cargo.toml | 2 +- nym-node-status-api/migrations/000_init.sql | 2 +- nym-node-status-api/src/db/models.rs | 1 + .../src/db/queries/testruns.rs | 30 +++++++++++++++---- nym-node-status-api/src/http/api/testruns.rs | 10 +------ nym-node-status-api/src/http/models.rs | 10 ------- 8 files changed, 31 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d451b3aaa1..e904d62a2b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6591,7 +6591,7 @@ dependencies = [ [[package]] name = "nym-node-status-api" -version = "0.1.3" +version = "0.1.4" dependencies = [ "anyhow", "axum 0.7.7", diff --git a/common/models/src/ns_api.rs b/common/models/src/ns_api.rs index 9c3373802a..5d875420e2 100644 --- a/common/models/src/ns_api.rs +++ b/common/models/src/ns_api.rs @@ -2,7 +2,6 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Deserialize, Serialize)] pub struct TestrunAssignment { - /// has nothing to do with GW identity key. This is PK from `gateways` table pub testrun_id: i64, - pub gateway_pk_id: i64, + pub gateway_identity_key: String, } diff --git a/nym-node-status-api/Cargo.toml b/nym-node-status-api/Cargo.toml index 7bb419c1a9..907e0daf23 100644 --- a/nym-node-status-api/Cargo.toml +++ b/nym-node-status-api/Cargo.toml @@ -3,7 +3,7 @@ [package] name = "nym-node-status-api" -version = "0.1.3" +version = "0.1.4" authors.workspace = true repository.workspace = true homepage.workspace = true diff --git a/nym-node-status-api/migrations/000_init.sql b/nym-node-status-api/migrations/000_init.sql index 1e5683e2c9..2dcb6be4ad 100644 --- a/nym-node-status-api/migrations/000_init.sql +++ b/nym-node-status-api/migrations/000_init.sql @@ -103,7 +103,7 @@ CREATE TABLE CREATE TABLE testruns ( id INTEGER PRIMARY KEY AUTOINCREMENT, - gateway_id INTEGER, + gateway_id INTEGER NOT NULL, status INTEGER NOT NULL, -- 0=pending, 1=in-progress, 2=complete timestamp_utc INTEGER NOT NULL, ip_address VARCHAR NOT NULL, diff --git a/nym-node-status-api/src/db/models.rs b/nym-node-status-api/src/db/models.rs index ca9baeb381..58a673c076 100644 --- a/nym-node-status-api/src/db/models.rs +++ b/nym-node-status-api/src/db/models.rs @@ -300,6 +300,7 @@ pub(crate) mod gateway { } } +#[allow(dead_code)] // not dead code, this is SQL data model #[derive(Debug, Clone)] pub struct TestRunDto { pub id: i64, diff --git a/nym-node-status-api/src/db/queries/testruns.rs b/nym-node-status-api/src/db/queries/testruns.rs index 4434977584..71ce7e0d0d 100644 --- a/nym-node-status-api/src/db/queries/testruns.rs +++ b/nym-node-status-api/src/db/queries/testruns.rs @@ -72,8 +72,8 @@ pub(crate) async fn update_testruns_older_than(db: &DbPool, age: Duration) -> an pub(crate) async fn get_oldest_testrun_and_make_it_pending( conn: &mut PoolConnection, ) -> anyhow::Result> { - let assignment = sqlx::query_as!( - TestrunAssignment, + // find & mark as "In progress" in the same transaction to avoid race conditions + let returning = sqlx::query!( r#"UPDATE testruns SET status = ? WHERE rowid = @@ -85,8 +85,8 @@ pub(crate) async fn get_oldest_testrun_and_make_it_pending( LIMIT 1 ) RETURNING - id as "testrun_id!", - gateway_id as "gateway_pk_id!" + id as "id!", + gateway_id "#, TestRunStatus::InProgress as i64, TestRunStatus::Queued as i64, @@ -94,7 +94,27 @@ pub(crate) async fn get_oldest_testrun_and_make_it_pending( .fetch_optional(conn.as_mut()) .await?; - Ok(assignment) + if let Some(testrun) = returning { + let gw_identity = sqlx::query!( + r#" + SELECT + id, + gateway_identity_key + FROM gateways + WHERE id = ? + LIMIT 1"#, + testrun.gateway_id + ) + .fetch_one(conn.as_mut()) + .await?; + + return Ok(Some(TestrunAssignment { + testrun_id: testrun.id, + gateway_identity_key: gw_identity.gateway_identity_key, + })); + } else { + return Ok(None); + } } pub(crate) async fn update_testrun_status( diff --git a/nym-node-status-api/src/http/api/testruns.rs b/nym-node-status-api/src/http/api/testruns.rs index 9f3802d747..e55e462110 100644 --- a/nym-node-status-api/src/http/api/testruns.rs +++ b/nym-node-status-api/src/http/api/testruns.rs @@ -42,18 +42,10 @@ async fn request_testrun(State(state): State) -> HttpResult { if let Some(testrun) = res { - let gw_identity = - db::queries::select_gateway_identity(&mut conn, testrun.gateway_pk_id) - .await - .map_err(|_| { - // should never happen: - HttpError::internal_with_logging("No gateway found for testrun") - })?; - // TODO dz consider adding a column to testruns table with agent's public key tracing::debug!( "🏃‍ Assigned testrun row_id {} gateway {} to agent", &testrun.testrun_id, - gw_identity + testrun.gateway_identity_key ); Ok(Json(testrun)) } else { diff --git a/nym-node-status-api/src/http/models.rs b/nym-node-status-api/src/http/models.rs index 315bf085dc..82011fc286 100644 --- a/nym-node-status-api/src/http/models.rs +++ b/nym-node-status-api/src/http/models.rs @@ -1,4 +1,3 @@ -use crate::db::models::TestRunDto; use nym_node_requests::api::v1::node::models::NodeDescription; use serde::{Deserialize, Serialize}; use utoipa::ToSchema; @@ -75,12 +74,3 @@ pub(crate) struct SummaryHistory { pub value_json: serde_json::Value, pub timestamp_utc: String, } - -impl From for TestrunAssignment { - fn from(value: TestRunDto) -> Self { - Self { - gateway_pk_id: value.gateway_id, - testrun_id: value.id, - } - } -}