From 7b7723af51b0a2ad9d4dc85e50e3adb891ef3bb5 Mon Sep 17 00:00:00 2001 From: Paul Butler Date: Wed, 31 Jan 2024 15:22:05 -0500 Subject: [PATCH] Report drone name in connect response (#592) * Report drone name in connect response * Print drone in CLI --- ...e79047c2aa3601d71f0c7575e735700b55814.json | 35 +++++++++++++++++++ ...fd09d55795731b9bb7f2a25fab9a6cd8daa12.json | 29 --------------- plane/src/admin.rs | 4 +++ plane/src/database/connect.rs | 2 ++ plane/src/database/drone.rs | 23 ++++++++++-- plane/src/types/mod.rs | 11 +++++- 6 files changed, 71 insertions(+), 33 deletions(-) create mode 100644 plane/.sqlx/query-6b542304a24bdcb7ee2f49df5abe79047c2aa3601d71f0c7575e735700b55814.json delete mode 100644 plane/.sqlx/query-c01d06c89d3c9a1cee70de43ab8fd09d55795731b9bb7f2a25fab9a6cd8daa12.json diff --git a/plane/.sqlx/query-6b542304a24bdcb7ee2f49df5abe79047c2aa3601d71f0c7575e735700b55814.json b/plane/.sqlx/query-6b542304a24bdcb7ee2f49df5abe79047c2aa3601d71f0c7575e735700b55814.json new file mode 100644 index 000000000..37c2a536a --- /dev/null +++ b/plane/.sqlx/query-6b542304a24bdcb7ee2f49df5abe79047c2aa3601d71f0c7575e735700b55814.json @@ -0,0 +1,35 @@ +{ + "db_name": "PostgreSQL", + "query": "\n select\n drone.id,\n node.name,\n drone.last_local_time as \"last_local_time!\"\n from node\n left join drone\n on node.id = drone.id\n left join controller\n on node.controller = controller.id\n where\n drone.ready = true\n and controller is not null\n and cluster = $1\n and now() - drone.last_heartbeat < $2\n and now() - controller.last_heartbeat < $2\n and controller.is_online = true\n and draining = false\n and last_local_time is not null\n order by (\n select\n count(*)\n from backend\n where drone_id = node.id\n and last_status != 'Terminated'\n ) asc, random()\n limit 1\n ", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "id", + "type_info": "Int4" + }, + { + "ordinal": 1, + "name": "name", + "type_info": "Varchar" + }, + { + "ordinal": 2, + "name": "last_local_time!", + "type_info": "Timestamptz" + } + ], + "parameters": { + "Left": [ + "Text", + "Interval" + ] + }, + "nullable": [ + false, + false, + true + ] + }, + "hash": "6b542304a24bdcb7ee2f49df5abe79047c2aa3601d71f0c7575e735700b55814" +} diff --git a/plane/.sqlx/query-c01d06c89d3c9a1cee70de43ab8fd09d55795731b9bb7f2a25fab9a6cd8daa12.json b/plane/.sqlx/query-c01d06c89d3c9a1cee70de43ab8fd09d55795731b9bb7f2a25fab9a6cd8daa12.json deleted file mode 100644 index 593376b73..000000000 --- a/plane/.sqlx/query-c01d06c89d3c9a1cee70de43ab8fd09d55795731b9bb7f2a25fab9a6cd8daa12.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "\n select\n drone.id,\n drone.last_local_time as \"last_local_time!\"\n from node\n left join drone\n on node.id = drone.id\n left join controller\n on node.controller = controller.id\n where\n drone.ready = true\n and controller is not null\n and cluster = $1\n and now() - drone.last_heartbeat < $2\n and now() - controller.last_heartbeat < $2\n and controller.is_online = true\n and draining = false\n and last_local_time is not null\n order by (\n select\n count(*)\n from backend\n where drone_id = node.id\n and last_status != 'Terminated'\n ) asc, random()\n limit 1\n ", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "id", - "type_info": "Int4" - }, - { - "ordinal": 1, - "name": "last_local_time!", - "type_info": "Timestamptz" - } - ], - "parameters": { - "Left": [ - "Text", - "Interval" - ] - }, - "nullable": [ - false, - true - ] - }, - "hash": "c01d06c89d3c9a1cee70de43ab8fd09d55795731b9bb7f2a25fab9a6cd8daa12" -} diff --git a/plane/src/admin.rs b/plane/src/admin.rs index 6f892f629..c72b61f71 100644 --- a/plane/src/admin.rs +++ b/plane/src/admin.rs @@ -187,6 +187,10 @@ pub async fn run_admin_command_inner(opts: AdminOpts) -> Result<(), PlaneClientE println!("URL: {}", response.url.bright_white()); println!("Status URL: {}", response.status_url.bright_white()); + if let Some(drone) = response.drone { + println!("Drone: {}", drone.to_string().bright_green()); + } + if wait { let mut stream = client.backend_status_stream(&response.backend_id).await?; diff --git a/plane/src/database/connect.rs b/plane/src/database/connect.rs index bf700c9df..c2c3946bd 100644 --- a/plane/src/database/connect.rs +++ b/plane/src/database/connect.rs @@ -224,6 +224,7 @@ async fn attempt_connect( token, secret_token, client, + None, ); return Ok(connect_response); @@ -284,6 +285,7 @@ async fn attempt_connect( token, secret_token, client, + Some(drone.drone), ); Ok(connect_response) diff --git a/plane/src/database/drone.rs b/plane/src/database/drone.rs index 98c46dd36..1657e76ec 100644 --- a/plane/src/database/drone.rs +++ b/plane/src/database/drone.rs @@ -1,9 +1,10 @@ use crate::{ heartbeat_consts::UNHEALTHY_SECONDS, + names::DroneName, types::{ClusterName, NodeId}, }; use chrono::{DateTime, Utc}; -use sqlx::{postgres::types::PgInterval, query, query_as, PgPool}; +use sqlx::{postgres::types::PgInterval, query, PgPool}; use std::time::Duration; pub struct DroneDatabase<'a> { @@ -72,11 +73,11 @@ impl<'a> DroneDatabase<'a> { &self, cluster: &ClusterName, ) -> sqlx::Result> { - let result = query_as!( - DroneForSpawn, + let result = query!( r#" select drone.id, + node.name, drone.last_local_time as "last_local_time!" from node left join drone @@ -108,11 +109,27 @@ impl<'a> DroneDatabase<'a> { .fetch_optional(self.pool) .await?; + let result = match result { + Some(result) => { + let id = NodeId::from(result.id); + let drone = DroneName::try_from(result.name).expect("valid drone name"); + let last_local_time = result.last_local_time; + + Some(DroneForSpawn { + id, + drone, + last_local_time, + }) + } + None => return Ok(None), + }; + Ok(result) } } pub struct DroneForSpawn { pub id: NodeId, + pub drone: DroneName, pub last_local_time: DateTime, } diff --git a/plane/src/types/mod.rs b/plane/src/types/mod.rs index a771ef080..472dbcb2d 100644 --- a/plane/src/types/mod.rs +++ b/plane/src/types/mod.rs @@ -1,4 +1,8 @@ -use crate::{client::PlaneClient, names::BackendName, util::random_prefixed_string}; +use crate::{ + client::PlaneClient, + names::{BackendName, DroneName}, + util::random_prefixed_string, +}; pub use backend_state::{BackendState, BackendStatus, TerminationKind, TerminationReason}; use bollard::auth::DockerCredentials; use serde::{Deserialize, Serialize}; @@ -305,6 +309,9 @@ pub struct ConnectResponse { pub secret_token: SecretToken, pub status_url: String, + + /// The drone that spawned this backend, if the request resulted in a spawn. + pub drone: Option, } impl ConnectResponse { @@ -316,6 +323,7 @@ impl ConnectResponse { token: BearerToken, secret_token: SecretToken, client: &PlaneClient, + drone: Option, ) -> Self { let url = if cluster.is_https() { format!("https://{}/{}/", cluster, token) @@ -333,6 +341,7 @@ impl ConnectResponse { url, secret_token, status_url, + drone, } } }