Skip to content

move cockroach-admin-client NodeId to cockroach-admin-types #8865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions cockroach-admin/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,97 @@ pub enum DecommissionError {
ParseRow(#[from] csv::Error),
}

/// CockroachDB Node ID
///
/// This field is stored internally as a String to avoid questions
/// about size, signedness, etc - it can be treated as an arbitrary
/// unique identifier.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)]
pub struct NodeId(pub String);

impl NodeId {
pub fn new(id: String) -> Self {
Self(id)
}

pub fn as_str(&self) -> &str {
&self.0
}
}

impl std::fmt::Display for NodeId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

impl std::str::FromStr for NodeId {
type Err = std::convert::Infallible;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(s.to_string()))
}
}

// When parsing the underlying NodeId, we force it to be interpreted
// as a String. Without this custom Deserialize implementation, we
// encounter parsing errors when querying endpoints which return the
// NodeId as an integer.
impl<'de> serde::Deserialize<'de> for NodeId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::de::{Error, Visitor};
use std::fmt;

struct NodeIdVisitor;

impl<'de> Visitor<'de> for NodeIdVisitor {
type Value = NodeId;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter
.write_str("a string or integer representing a node ID")
}

fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: Error,
{
Ok(NodeId(value.to_string()))
}

fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
where
E: Error,
{
Ok(NodeId(value))
}

fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
where
E: Error,
{
Ok(NodeId(value.to_string()))
}

fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: Error,
{
Ok(NodeId(value.to_string()))
}
}

deserializer.deserialize_any(NodeIdVisitor)
}
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct NodeStatus {
// TODO use NodeId
pub node_id: String,
pub address: SocketAddr,
pub sql_address: SocketAddr,
Expand Down
1 change: 1 addition & 0 deletions cockroach-metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license = "MPL-2.0"
anyhow.workspace = true
chrono.workspace = true
cockroach-admin-client.workspace = true
cockroach-admin-types.workspace = true
futures.workspace = true
parallel-task-set.workspace = true
reqwest.workspace = true
Expand Down
96 changes: 5 additions & 91 deletions cockroach-metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use cockroach_admin_client::Client;
use cockroach_admin_types::NodeId;
use futures::stream::{FuturesUnordered, StreamExt};
use parallel_task_set::ParallelTaskSet;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -760,93 +761,6 @@ impl PrometheusMetrics {
}
}

/// CockroachDB Node ID
///
/// This field is stored internally as a String to avoid questions
/// about size, signedness, etc - it can be treated as an arbitrary
/// unique identifier.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)]
pub struct NodeId(pub String);

impl NodeId {
pub fn new(id: String) -> Self {
Self(id)
}

pub fn as_str(&self) -> &str {
&self.0
}
}

impl std::fmt::Display for NodeId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

impl std::str::FromStr for NodeId {
type Err = std::convert::Infallible;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(s.to_string()))
}
}

// When parsing the underlying NodeId, we force it to be interpreted
// as a String. Without this custom Deserialize implementation, we
// encounter parsing errors when querying endpoints which return the
// NodeId as an integer.
impl<'de> serde::Deserialize<'de> for NodeId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::de::{Error, Visitor};
use std::fmt;

struct NodeIdVisitor;

impl<'de> Visitor<'de> for NodeIdVisitor {
type Value = NodeId;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter
.write_str("a string or integer representing a node ID")
}

fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: Error,
{
Ok(NodeId(value.to_string()))
}

fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
where
E: Error,
{
Ok(NodeId(value))
}

fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
where
E: Error,
{
Ok(NodeId(value.to_string()))
}

fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: Error,
{
Ok(NodeId(value.to_string()))
}
}

deserializer.deserialize_any(NodeIdVisitor)
}
}

/// CockroachDB node liveness status
///
/// From CockroachDB's [NodeLivenessStatus protobuf enum](https://github.com/cockroachdb/cockroach/blob/release-21.1/pkg/kv/kvserver/liveness/livenesspb/liveness.proto#L107-L138)
Expand Down Expand Up @@ -1281,16 +1195,16 @@ sql_exec_latency_bucket{le="0.01"} 25
assert!(result.unwrap().metrics.is_empty());

// Malformed lines (missing values, extra spaces, etc.)
let malformed_input = r#"
let malformed_input = "
metric_name_no_value
metric_name_with_space
metric_name_with_space\x20
metric_name_multiple spaces here
= value_no_name
leading_space_metric 123
trailing_space_metric 456
trailing_space_metric 456\x20
metric{label=value} 789
metric{malformed=label value} 999
"#;
";
let result = PrometheusMetrics::parse(malformed_input);
assert!(result.is_ok());
// Should ignore malformed lines gracefully
Expand Down
1 change: 1 addition & 0 deletions nexus/db-model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ anyhow.workspace = true
camino.workspace = true
chrono.workspace = true
clickhouse-admin-types.workspace = true
cockroach-admin-types.workspace = true
derive-where.workspace = true
diesel = { workspace = true, features = ["postgres", "r2d2", "chrono", "serde_json", "network-address", "uuid"] }
hex.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion nexus/db-model/src/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2918,7 +2918,7 @@ pub struct InvCockroachStatus {
impl InvCockroachStatus {
pub fn new(
inv_collection_id: CollectionUuid,
node_id: omicron_cockroach_metrics::NodeId,
node_id: cockroach_admin_types::NodeId,
status: &CockroachStatus,
) -> Result<Self, anyhow::Error> {
Ok(Self {
Expand Down
1 change: 1 addition & 0 deletions nexus/db-queries/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async-trait.workspace = true
camino.workspace = true
chrono.workspace = true
clickhouse-admin-types.workspace = true
cockroach-admin-types.workspace = true
const_format.workspace = true
diesel.workspace = true
diesel-dtrace.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion nexus/db-queries/src/db/datastore/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use async_bb8_diesel::AsyncConnection;
use async_bb8_diesel::AsyncRunQueryDsl;
use async_bb8_diesel::AsyncSimpleConnection;
use clickhouse_admin_types::ClickhouseKeeperClusterMembership;
use cockroach_admin_types::NodeId as CockroachNodeId;
use diesel::BoolExpressionMethods;
use diesel::ExpressionMethods;
use diesel::IntoSql;
Expand Down Expand Up @@ -102,7 +103,6 @@ use nexus_types::inventory::InternalDnsGenerationStatus;
use nexus_types::inventory::PhysicalDiskFirmware;
use nexus_types::inventory::SledAgent;
use nexus_types::inventory::TimeSync;
use omicron_cockroach_metrics::NodeId as CockroachNodeId;
use omicron_common::api::external::Error;
use omicron_common::api::external::InternalContext;
use omicron_common::api::external::LookupType;
Expand Down
1 change: 1 addition & 0 deletions nexus/inventory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ clickhouse-admin-keeper-client.workspace = true
dns-service-client.workspace = true
clickhouse-admin-server-client.workspace = true
clickhouse-admin-types.workspace = true
cockroach-admin-types.workspace = true
futures.workspace = true
gateway-client.workspace = true
gateway-messages.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion nexus/inventory/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use anyhow::anyhow;
use chrono::DateTime;
use chrono::Utc;
use clickhouse_admin_types::ClickhouseKeeperClusterMembership;
use cockroach_admin_types::NodeId;
use gateway_client::types::SpComponentCaboose;
use gateway_client::types::SpState;
use gateway_client::types::SpType;
Expand All @@ -37,7 +38,6 @@ use nexus_types::inventory::SledAgent;
use nexus_types::inventory::TimeSync;
use nexus_types::inventory::Zpool;
use omicron_cockroach_metrics::CockroachMetric;
use omicron_cockroach_metrics::NodeId;
use omicron_cockroach_metrics::PrometheusMetrics;
use omicron_common::disk::M2Slot;
use omicron_uuid_kinds::CollectionKind;
Expand Down
2 changes: 1 addition & 1 deletion nexus/inventory/src/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ pub fn representative() -> Representative {
);

builder.found_cockroach_metrics(
omicron_cockroach_metrics::NodeId::new("1".to_string()),
cockroach_admin_types::NodeId::new("1".to_string()),
PrometheusMetrics {
metrics: BTreeMap::from([(
"ranges_underreplicated".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion nexus/reconfigurator/planning/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ workspace = true
[dependencies]
anyhow.workspace = true
clickhouse-admin-types.workspace = true
cockroach-admin-types.workspace = true
chrono.workspace = true
debug-ignore.workspace = true
daft.workspace = true
Expand Down Expand Up @@ -50,7 +51,6 @@ omicron-workspace-hack.workspace = true
dropshot.workspace = true
expectorate.workspace = true
maplit.workspace = true
omicron-cockroach-metrics.workspace = true
omicron-common = { workspace = true, features = ["testing"] }
omicron-test-utils.workspace = true
proptest.workspace = true
Expand Down
Loading
Loading