Skip to content

Add reconfigurator chicken-switch-history to omdb #8465

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 1 commit into
base: ajs/chicken-switch-bg-task
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
74 changes: 74 additions & 0 deletions dev-tools/omdb/src/bin/omdb/reconfigurator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@ use diesel::ExpressionMethods;
use diesel::QueryDsl;
use diesel::SelectableHelper;
use nexus_db_model::BpTarget;
use nexus_db_model::SqlU32;
use nexus_db_queries::authz;
use nexus_db_queries::context::OpContext;
use nexus_db_queries::db::DataStore;
use nexus_db_queries::db::datastore::SQL_BATCH_SIZE;
use nexus_db_queries::db::pagination::Paginator;
use nexus_types::deployment::Blueprint;
use nexus_types::deployment::BlueprintMetadata;
use nexus_types::deployment::ReconfiguratorChickenSwitches;
use nexus_types::deployment::UnstableReconfiguratorState;
use omicron_common::api::external::Error;
use omicron_common::api::external::LookupType;
use omicron_uuid_kinds::BlueprintUuid;
use omicron_uuid_kinds::GenericUuid;
use slog::Logger;
use std::collections::BTreeMap;
use std::num::NonZeroU32;
use tabled::Tabled;

/// Arguments to the "omdb reconfigurator" subcommand
#[derive(Debug, Args)]
Expand All @@ -53,6 +57,8 @@ enum ReconfiguratorCommands {
Archive(ExportArgs),
/// Show recent history of blueprints
History(HistoryArgs),
/// Show the recent history of chicken switch settings
ChickenSwitchesHistory(ChickenSwitchesHistoryArgs),
}

#[derive(Debug, Args, Clone)]
Expand All @@ -61,6 +67,13 @@ struct ExportArgs {
output_file: Utf8PathBuf,
}

#[derive(Debug, Args, Clone)]
struct ChickenSwitchesHistoryArgs {
/// how far back in the history to show (number of targets)
#[clap(long, default_value_t = 128)]
limit: u32,
}

#[derive(Debug, Args, Clone)]
struct HistoryArgs {
/// how far back in the history to show (number of targets)
Expand Down Expand Up @@ -111,6 +124,12 @@ impl ReconfiguratorArgs {
)
.await
}
ReconfiguratorCommands::ChickenSwitchesHistory(args) => {
cmd_reconfigurator_chicken_switches_history(
&opctx, &datastore, args,
)
.await
}
},
)
.await
Expand Down Expand Up @@ -351,6 +370,61 @@ async fn cmd_reconfigurator_history(

Ok(())
}
/// Show recent history of chicken switches
async fn cmd_reconfigurator_chicken_switches_history(
opctx: &OpContext,
datastore: &DataStore,
history_args: &ChickenSwitchesHistoryArgs,
) -> anyhow::Result<()> {
let mut history = vec![];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually has to be a set or a map to prevent duplicates.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't follow this comment - how are there duplicates if each row has a unique version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proposed solution is likely wrong. I was just noting it as I was running out the door. The bug I'm hitting is related to asking for more than the SQL_BATCH_SIZE and getting back that many items, but with repeats in the Vec, which is not what I would expect. I think, this is likely due to not passing an offset. I'm going to dig back into it shortly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue was actually with pagination direction. The PaginatorHelper hardcodes ascending order in current_pagparams. When changing to Descending order everything works. I can do this by adding a separate method or modifying the returned DataPageParams.

What I don't get is how we never ran into this before. I don't see any code that changes the returned DataPageparams, although admittedly I've only looked through part of the search results for uses of current_pagparams().

let limit = history_args.limit;
let batch_size = NonZeroU32::min(limit.try_into().unwrap(), SQL_BATCH_SIZE);
let mut paginator = Paginator::<SqlU32>::new(batch_size);
while let Some(p) = paginator.next() {
if history.len() >= limit as usize {
break;
}
let batch = datastore
.reconfigurator_chicken_switches_list(opctx, &p.current_pagparams())
.await
.context("batch of chicken switches")?;
paginator = p.found_batch(&batch, &|b| SqlU32::new(b.version));
history.extend(batch.into_iter());
}

#[derive(Tabled)]
#[tabled(rename_all = "SCREAMING_SNAKE_CASE")]
struct SwitchesRow {
version: String,
planner_enabled: String,
time_modified: String,
}

let rows: Vec<_> = history
.into_iter()
.map(|s| {
let ReconfiguratorChickenSwitches {
version,
planner_enabled,
time_modified,
} = s;
SwitchesRow {
version: version.to_string(),
planner_enabled: planner_enabled.to_string(),
time_modified: time_modified.to_string(),
}
})
.collect();

let table = tabled::Table::new(rows)
.with(tabled::settings::Style::empty())
.with(tabled::settings::Padding::new(0, 1, 0, 0))
.to_string();

println!("{}", table);

Ok(())
}

async fn blueprint_load(
opctx: &OpContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl DataStore {
reconfigurator_chicken_switches::version,
pagparams,
)
.order_by(reconfigurator_chicken_switches::dsl::version.desc())
.select(DbReconfiguratorChickenSwitches::as_select())
.get_results_async(&*self.pool_connection_authorized(opctx).await?)
.await
Expand Down
2 changes: 1 addition & 1 deletion nexus/db-queries/src/db/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ impl<N> PaginatorHelper<N> {
PaginatorState::Done
} else {
// self.batch_size is non-zero, so if we got at least that many
// items, then there's at least one.
// items, then there's at least one left.
let last = batch.iter().last().unwrap();
let marker = item2marker(last);
PaginatorState::Middle { marker }
Expand Down
Loading