Skip to content

Commit aeda2bc

Browse files
committed
Inventory: Implement display for CockroachStatus
This is used by omdb, reconfigurator-cli
1 parent 356e0b3 commit aeda2bc

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

nexus/types/src/inventory.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,9 @@ impl IdOrdItem for SledAgent {
618618
id_upcast!();
619619
}
620620

621-
#[derive(Clone, Default, Debug, PartialEq, Eq, Deserialize, Serialize)]
621+
#[derive(
622+
Clone, Default, Debug, Hash, PartialEq, Eq, Deserialize, Serialize,
623+
)]
622624
pub struct CockroachStatus {
623625
pub ranges_underreplicated: Option<u64>,
624626
pub liveness_live_nodes: Option<u64>,

nexus/types/src/inventory/display.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use nexus_sled_agent_shared::inventory::{
2626
use omicron_uuid_kinds::{
2727
DatasetUuid, OmicronZoneUuid, PhysicalDiskUuid, ZpoolUuid,
2828
};
29+
use std::collections::HashMap;
2930
use strum::IntoEnumIterator;
3031
use tabled::Tabled;
3132
use tufaceous_artifact::ArtifactHash;
@@ -42,6 +43,7 @@ pub struct CollectionDisplay<'a> {
4243
include_sleds: bool,
4344
include_orphaned_datasets: bool,
4445
include_clickhouse_keeper_membership: bool,
46+
include_cockroach_status: bool,
4547
long_string_formatter: LongStringFormatter,
4648
}
4749

@@ -54,6 +56,7 @@ impl<'a> CollectionDisplay<'a> {
5456
include_sleds: true,
5557
include_orphaned_datasets: true,
5658
include_clickhouse_keeper_membership: true,
59+
include_cockroach_status: true,
5760
long_string_formatter: LongStringFormatter::new(),
5861
}
5962
}
@@ -94,6 +97,15 @@ impl<'a> CollectionDisplay<'a> {
9497
self
9598
}
9699

100+
/// Control display of Cockroach cluster information (defaults to true).
101+
pub fn include_cockroach_status(
102+
&mut self,
103+
include_cockroach_status: bool,
104+
) -> &mut Self {
105+
self.include_cockroach_status = include_cockroach_status;
106+
self
107+
}
108+
97109
/// Show long strings (defaults to false).
98110
pub fn show_long_strings(&mut self, show_long_strings: bool) -> &mut Self {
99111
self.long_string_formatter.show_long_strings = show_long_strings;
@@ -111,6 +123,7 @@ impl<'a> CollectionDisplay<'a> {
111123
.include_clickhouse_keeper_membership(
112124
filter.include_keeper_membership(),
113125
)
126+
.include_cockroach_status(filter.include_cockroach_status())
114127
}
115128
}
116129

@@ -134,6 +147,9 @@ impl fmt::Display for CollectionDisplay<'_> {
134147
if self.include_clickhouse_keeper_membership {
135148
display_keeper_membership(&self.collection, f)?;
136149
}
150+
if self.include_cockroach_status {
151+
display_cockroach_status(&self.collection, f)?;
152+
}
137153

138154
if nerrors > 0 {
139155
writeln!(
@@ -199,6 +215,14 @@ impl CollectionDisplayCliFilter {
199215
Self::OrphanedDatasets => false,
200216
}
201217
}
218+
219+
fn include_cockroach_status(&self) -> bool {
220+
match self {
221+
Self::All => true,
222+
Self::Sp { .. } => false,
223+
Self::OrphanedDatasets => false,
224+
}
225+
}
202226
}
203227

204228
/// Which SPs within a collection to display.
@@ -1002,6 +1026,56 @@ fn display_keeper_membership(
10021026
Ok(())
10031027
}
10041028

1029+
fn display_cockroach_status(
1030+
collection: &Collection,
1031+
f: &mut dyn fmt::Write,
1032+
) -> fmt::Result {
1033+
writeln!(f, "\nCOCKROACH STATUS")?;
1034+
1035+
// Under normal conditions, cockroach nodes will report the same data. For
1036+
// brevity, we will map "status" -> "nodes reporting that status", to avoid
1037+
// emitting the same information repeatedly for each node.
1038+
let mut status_to_node = HashMap::new();
1039+
1040+
for (node, status) in &collection.cockroach_status {
1041+
status_to_node
1042+
.entry(status)
1043+
.and_modify(|nodes: &mut Vec<_>| nodes.push(node))
1044+
.or_insert(vec![node]);
1045+
}
1046+
1047+
for (status, nodes) in &status_to_node {
1048+
writeln!(
1049+
f,
1050+
"\n status from nodes: {}",
1051+
nodes.iter().map(|n| n.to_string()).join(", ")
1052+
)?;
1053+
1054+
writeln!(
1055+
f,
1056+
" \n ranges underreplicated: {}",
1057+
status
1058+
.ranges_underreplicated
1059+
.map(|r| r.to_string())
1060+
.unwrap_or_else(|| "-".to_string())
1061+
)?;
1062+
writeln!(
1063+
f,
1064+
" \n live nodes: {}",
1065+
status
1066+
.liveness_live_nodes
1067+
.map(|r| r.to_string())
1068+
.unwrap_or_else(|| "-".to_string())
1069+
)?;
1070+
}
1071+
if status_to_node.is_empty() {
1072+
writeln!(f, " no cockroach status retrieved")?;
1073+
}
1074+
writeln!(f, "")?;
1075+
1076+
Ok(())
1077+
}
1078+
10051079
#[derive(Debug)]
10061080
struct LongStringFormatter {
10071081
show_long_strings: bool,

0 commit comments

Comments
 (0)