|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +//! `omdb db db_metadata` subcommands |
| 6 | +
|
| 7 | +use super::display_option_blank; |
| 8 | +use anyhow::Context; |
| 9 | +use clap::Args; |
| 10 | +use clap::Subcommand; |
| 11 | +use nexus_db_model::DbMetadataNexusState; |
| 12 | +use nexus_db_queries::context::OpContext; |
| 13 | +use nexus_db_queries::db::DataStore; |
| 14 | +use nexus_types::deployment::Blueprint; |
| 15 | +use nexus_types::deployment::BlueprintZoneDisposition; |
| 16 | +use omicron_common::api::external::Generation; |
| 17 | +use omicron_uuid_kinds::BlueprintUuid; |
| 18 | +use omicron_uuid_kinds::OmicronZoneUuid; |
| 19 | +use std::collections::BTreeMap; |
| 20 | +use tabled::Tabled; |
| 21 | + |
| 22 | +#[derive(Debug, Args, Clone)] |
| 23 | +pub struct DbMetadataArgs { |
| 24 | + #[command(subcommand)] |
| 25 | + pub command: DbMetadataCommands, |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Debug, Subcommand, Clone)] |
| 29 | +pub enum DbMetadataCommands { |
| 30 | + #[clap(alias = "ls-nexus")] |
| 31 | + ListNexus, |
| 32 | +} |
| 33 | + |
| 34 | +// DB Metadata |
| 35 | + |
| 36 | +#[derive(Tabled)] |
| 37 | +#[tabled(rename_all = "SCREAMING_SNAKE_CASE")] |
| 38 | +struct DbMetadataNexusRow { |
| 39 | + id: OmicronZoneUuid, |
| 40 | + #[tabled(display_with = "display_option_blank")] |
| 41 | + last_drained_blueprint: Option<BlueprintUuid>, |
| 42 | + |
| 43 | + // Identifies the state we observe in the database |
| 44 | + state: String, |
| 45 | + |
| 46 | + // Identifies the state this Nexus is trying to achieve, based on the target |
| 47 | + // blueprint, if it's different from the current state |
| 48 | + #[tabled(display_with = "display_option_blank")] |
| 49 | + transitioning_to: Option<String>, |
| 50 | +} |
| 51 | + |
| 52 | +fn get_intended_nexus_state( |
| 53 | + bp_nexus_generation: Generation, |
| 54 | + bp_nexus_generation_by_zone: &BTreeMap<OmicronZoneUuid, Generation>, |
| 55 | + id: OmicronZoneUuid, |
| 56 | +) -> Option<DbMetadataNexusState> { |
| 57 | + let Some(gen) = bp_nexus_generation_by_zone.get(&id) else { |
| 58 | + return None; |
| 59 | + }; |
| 60 | + |
| 61 | + Some(if *gen < bp_nexus_generation { |
| 62 | + // This Nexus is either quiescing, or has already quiesced |
| 63 | + DbMetadataNexusState::Quiesced |
| 64 | + } else if *gen == bp_nexus_generation { |
| 65 | + // This Nexus is either active, or will become active once |
| 66 | + // the prior generation has quiesced |
| 67 | + DbMetadataNexusState::Active |
| 68 | + } else { |
| 69 | + // This Nexus is not ready to be run yet |
| 70 | + DbMetadataNexusState::NotYet |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +fn get_nexus_state_transition( |
| 75 | + observed: DbMetadataNexusState, |
| 76 | + intended: Option<DbMetadataNexusState>, |
| 77 | +) -> Option<String> { |
| 78 | + match (observed, intended) { |
| 79 | + (observed, Some(intended)) if observed == intended => None, |
| 80 | + (_, Some(intended)) => Some(intended.to_string()), |
| 81 | + (_, None) => Some("Unknown".to_string()), |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +async fn get_db_metadata_nexus_rows( |
| 86 | + opctx: &OpContext, |
| 87 | + datastore: &DataStore, |
| 88 | + blueprint: &Blueprint, |
| 89 | +) -> Result<Vec<DbMetadataNexusRow>, anyhow::Error> { |
| 90 | + let states = vec![ |
| 91 | + DbMetadataNexusState::Active, |
| 92 | + DbMetadataNexusState::NotYet, |
| 93 | + DbMetadataNexusState::Quiesced, |
| 94 | + ]; |
| 95 | + |
| 96 | + let nexus_generation_by_zone = blueprint |
| 97 | + .all_nexus_zones(BlueprintZoneDisposition::is_in_service) |
| 98 | + .map(|(_, zone, nexus_zone)| (zone.id, nexus_zone.nexus_generation)) |
| 99 | + .collect::<BTreeMap<_, _>>(); |
| 100 | + |
| 101 | + Ok(datastore |
| 102 | + .get_db_metadata_nexus_in_state(opctx, states) |
| 103 | + .await? |
| 104 | + .into_iter() |
| 105 | + .map(|db_metadata_nexus| { |
| 106 | + let id = db_metadata_nexus.nexus_id(); |
| 107 | + let last_drained_blueprint = |
| 108 | + db_metadata_nexus.last_drained_blueprint_id(); |
| 109 | + let state = db_metadata_nexus.state().to_string(); |
| 110 | + let intended_state = get_intended_nexus_state( |
| 111 | + blueprint.nexus_generation, |
| 112 | + &nexus_generation_by_zone, |
| 113 | + id, |
| 114 | + ); |
| 115 | + |
| 116 | + let transitioning_to = get_nexus_state_transition( |
| 117 | + db_metadata_nexus.state(), |
| 118 | + intended_state, |
| 119 | + ); |
| 120 | + |
| 121 | + DbMetadataNexusRow { |
| 122 | + id, |
| 123 | + last_drained_blueprint, |
| 124 | + state, |
| 125 | + transitioning_to, |
| 126 | + } |
| 127 | + }) |
| 128 | + .collect()) |
| 129 | +} |
| 130 | + |
| 131 | +pub async fn cmd_db_metadata_list_nexus( |
| 132 | + opctx: &OpContext, |
| 133 | + datastore: &DataStore, |
| 134 | +) -> Result<(), anyhow::Error> { |
| 135 | + let (_, current_target_blueprint) = datastore |
| 136 | + .blueprint_target_get_current_full(opctx) |
| 137 | + .await |
| 138 | + .context("loading current target blueprint")?; |
| 139 | + println!( |
| 140 | + "Target Blueprint {} @ nexus_generation: {}", |
| 141 | + current_target_blueprint.id, current_target_blueprint.nexus_generation |
| 142 | + ); |
| 143 | + |
| 144 | + let rows: Vec<_> = |
| 145 | + get_db_metadata_nexus_rows(opctx, datastore, ¤t_target_blueprint) |
| 146 | + .await?; |
| 147 | + let table = tabled::Table::new(rows) |
| 148 | + .with(tabled::settings::Style::psql()) |
| 149 | + .with(tabled::settings::Padding::new(0, 1, 0, 0)) |
| 150 | + .to_string(); |
| 151 | + println!("{}", table); |
| 152 | + |
| 153 | + Ok(()) |
| 154 | +} |
0 commit comments