Skip to content

Commit 690ea16

Browse files
committed
Unify queries for all db_metadata_nexus states
1 parent 70d4ab9 commit 690ea16

File tree

4 files changed

+32
-35
lines changed

4 files changed

+32
-35
lines changed

nexus/db-queries/src/db/datastore/db_metadata.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -300,33 +300,18 @@ impl DatastoreSetupAction {
300300
}
301301

302302
impl DataStore {
303-
// Returns the active Nexuses
304-
pub async fn get_active_db_metadata_nexus(
303+
/// Returns [`DbMetadataNexus`] records in any of the supplied states.
304+
pub async fn get_db_metadata_nexus_in_state(
305305
&self,
306306
opctx: &OpContext,
307+
states: &[DbMetadataNexusState],
307308
) -> Result<Vec<DbMetadataNexus>, Error> {
308309
use nexus_db_schema::schema::db_metadata_nexus::dsl;
309310

310311
opctx.authorize(authz::Action::Read, &authz::FLEET).await?;
311312

312313
dsl::db_metadata_nexus
313-
.filter(dsl::state.eq(DbMetadataNexusState::Active))
314-
.load_async(&*self.pool_connection_authorized(&opctx).await?)
315-
.await
316-
.map_err(|e| public_error_from_diesel(e, ErrorHandler::Server))
317-
}
318-
319-
// Returns the 'not yet' Nexuses
320-
pub async fn get_not_yet_db_metadata_nexus(
321-
&self,
322-
opctx: &OpContext,
323-
) -> Result<Vec<DbMetadataNexus>, Error> {
324-
use nexus_db_schema::schema::db_metadata_nexus::dsl;
325-
326-
opctx.authorize(authz::Action::Read, &authz::FLEET).await?;
327-
328-
dsl::db_metadata_nexus
329-
.filter(dsl::state.eq(DbMetadataNexusState::NotYet))
314+
.filter(dsl::state.eq_any(states.to_vec()))
330315
.load_async(&*self.pool_connection_authorized(&opctx).await?)
331316
.await
332317
.map_err(|e| public_error_from_diesel(e, ErrorHandler::Server))

nexus/reconfigurator/execution/src/database.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! Manages deployment of records into the database.
66
77
use anyhow::anyhow;
8+
use nexus_db_model::DbMetadataNexusState;
89
use nexus_db_queries::context::OpContext;
910
use nexus_db_queries::db::DataStore;
1011
use nexus_types::deployment::Blueprint;
@@ -23,7 +24,7 @@ pub(crate) async fn deploy_db_metadata_nexus_records(
2324
// can lag behind it if we are one of those Nexuses running after quiescing
2425
// has started.
2526
let active_nexus_zones = datastore
26-
.get_active_db_metadata_nexus(opctx)
27+
.get_db_metadata_nexus_in_state(opctx, &[DbMetadataNexusState::Active])
2728
.await?
2829
.into_iter()
2930
.map(|z| z.nexus_id())

nexus/reconfigurator/execution/src/dns.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ mod test {
313313
use internal_dns_types::names::BOUNDARY_NTP_DNS_NAME;
314314
use internal_dns_types::names::DNS_ZONE;
315315
use internal_dns_types::names::ServiceName;
316+
use nexus_db_model::DbMetadataNexusState;
316317
use nexus_db_model::DnsGroup;
317318
use nexus_db_model::Silo;
318319
use nexus_db_queries::authn;
@@ -1503,7 +1504,10 @@ mod test {
15031504
let ip_pool_range_rows =
15041505
fetch_all_service_ip_pool_ranges(&datastore, &opctx).await;
15051506
let active_nexus_zones = datastore
1506-
.get_active_db_metadata_nexus(&opctx)
1507+
.get_db_metadata_nexus_in_state(
1508+
&opctx,
1509+
&[DbMetadataNexusState::Active],
1510+
)
15071511
.await
15081512
.internal_context("fetching active nexuses")
15091513
.unwrap()

nexus/reconfigurator/preparation/src/lib.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
use anyhow::Context;
88
use futures::StreamExt;
9+
use nexus_db_model::DbMetadataNexusState;
910
use nexus_db_model::DnsGroup;
1011
use nexus_db_model::Generation;
1112
use nexus_db_queries::context::OpContext;
@@ -204,20 +205,26 @@ impl PlanningInputFromDb<'_> {
204205
.await
205206
.internal_context("fetching oximeter read policy")?;
206207

207-
let active_nexus_zones = datastore
208-
.get_active_db_metadata_nexus(opctx)
209-
.await
210-
.internal_context("fetching active nexuses")?
211-
.into_iter()
212-
.map(|z| z.nexus_id())
213-
.collect::<Vec<_>>();
214-
let not_yet_nexus_zones = datastore
215-
.get_not_yet_db_metadata_nexus(opctx)
216-
.await
217-
.internal_context("fetching 'not yet' nexuses")?
218-
.into_iter()
219-
.map(|z| z.nexus_id())
220-
.collect::<Vec<_>>();
208+
let (active_nexus_zones, not_yet_nexus_zones): (Vec<_>, Vec<_>) =
209+
datastore
210+
.get_db_metadata_nexus_in_state(
211+
opctx,
212+
&[
213+
DbMetadataNexusState::Active,
214+
DbMetadataNexusState::NotYet,
215+
],
216+
)
217+
.await
218+
.internal_context("fetching db_metdata_nexus records")?
219+
.into_iter()
220+
.partition(|nexus| {
221+
nexus.state() == DbMetadataNexusState::Active
222+
});
223+
224+
let active_nexus_zones =
225+
active_nexus_zones.into_iter().map(|n| n.nexus_id()).collect();
226+
let not_yet_nexus_zones =
227+
not_yet_nexus_zones.into_iter().map(|n| n.nexus_id()).collect();
221228

222229
let planning_input = PlanningInputFromDb {
223230
sled_rows: &sled_rows,

0 commit comments

Comments
 (0)