Skip to content

Commit ca46034

Browse files
authored
Adds more generic item-allocation queries (#1055)
* Adds more generic item-allocation queries - Creates the `omicron_nexus::db::queries` submodule, for custom / complex queries, organized around specific resources - Adds a general-purpose `NextItem` query, which looks for the first available item in a database column, with a defined starting point and maximum offset / scan size. This can be used to implement a linear search over a defined space. One of the benefits is that for randomly-selected items, we can avoid a retry loop, by instead starting from the random base item and inserting the first successful value. There are several places where we use a similar query, or use a retry loop that could be replaced by this query. - Use the `NextGuestIpv4Address` query when creating NICs. - Use the `NextNicSlot` query when creating guest NICs * Review feedback
1 parent 4c933a1 commit ca46034

File tree

11 files changed

+1171
-715
lines changed

11 files changed

+1171
-715
lines changed

nexus/src/app/instance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::db;
1313
use crate::db::identity::Resource;
1414
use crate::db::lookup::LookupPath;
1515
use crate::db::model::Name;
16-
use crate::db::subnet_allocation::NetworkInterfaceError;
16+
use crate::db::queries::network_interface::NetworkInterfaceError;
1717
use crate::external_api::params;
1818
use omicron_common::api::external;
1919
use omicron_common::api::external::CreateResult;

nexus/src/app/sagas/instance_create.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::app::{MAX_DISKS_PER_INSTANCE, MAX_NICS_PER_INSTANCE};
99
use crate::context::OpContext;
1010
use crate::db::identity::Resource;
1111
use crate::db::lookup::LookupPath;
12+
use crate::db::queries::network_interface::NetworkInterfaceError;
1213
use crate::external_api::params;
1314
use crate::saga_interface::SagaContext;
1415
use crate::{authn, authz, db};
@@ -331,7 +332,6 @@ async fn sic_create_custom_network_interfaces(
331332
)
332333
.await;
333334

334-
use crate::db::subnet_allocation::NetworkInterfaceError;
335335
match result {
336336
Ok(_) => Ok(()),
337337

@@ -432,7 +432,7 @@ async fn sic_create_default_network_interface(
432432
interface,
433433
)
434434
.await
435-
.map_err(db::subnet_allocation::NetworkInterfaceError::into_external)
435+
.map_err(NetworkInterfaceError::into_external)
436436
.map_err(ActionError::action_failed)?;
437437
Ok(())
438438
}

nexus/src/app/vpc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::db;
1010
use crate::db::lookup::LookupPath;
1111
use crate::db::model::Name;
1212
use crate::db::model::VpcRouterKind;
13-
use crate::db::subnet_allocation::SubnetError;
13+
use crate::db::queries::vpc_subnet::SubnetError;
1414
use crate::defaults;
1515
use crate::external_api::params;
1616
use omicron_common::api::external;

nexus/src/app/vpc_subnet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::db::identity::Resource;
1111
use crate::db::lookup::LookupPath;
1212
use crate::db::model::Name;
1313
use crate::db::model::VpcSubnet;
14-
use crate::db::subnet_allocation::SubnetError;
14+
use crate::db::queries::vpc_subnet::SubnetError;
1515
use crate::defaults;
1616
use crate::external_api::params;
1717
use omicron_common::api::external;

nexus/src/db/datastore.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ use crate::db::fixed_data::role_builtin::BUILTIN_ROLES;
3333
use crate::db::fixed_data::silo::{DEFAULT_SILO, SILO_ID};
3434
use crate::db::lookup::LookupPath;
3535
use crate::db::model::DatabaseString;
36+
use crate::db::queries::network_interface::InsertNetworkInterfaceQuery;
37+
use crate::db::queries::network_interface::NetworkInterfaceError;
38+
use crate::db::queries::vpc_subnet::FilterConflictingVpcSubnetRangesQuery;
39+
use crate::db::queries::vpc_subnet::SubnetError;
3640
use crate::db::{
3741
self,
3842
error::{public_error_from_diesel_pool, ErrorHandler, TransactionError},
@@ -49,10 +53,6 @@ use crate::db::{
4953
},
5054
pagination::paginated,
5155
pagination::paginated_multicolumn,
52-
subnet_allocation::FilterConflictingVpcSubnetRangesQuery,
53-
subnet_allocation::InsertNetworkInterfaceQuery,
54-
subnet_allocation::NetworkInterfaceError,
55-
subnet_allocation::SubnetError,
5656
update_and_check::{UpdateAndCheck, UpdateStatus},
5757
};
5858
use crate::external_api::{params, shared};

nexus/src/db/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ pub mod ipv6;
1818
pub mod lookup;
1919
mod pagination;
2020
mod pool;
21+
// This is marked public because the error types are used elsewhere, e.g., in
22+
// sagas.
23+
pub(crate) mod queries;
2124
mod saga_recovery;
2225
mod saga_types;
2326
mod sec_store;
24-
pub(crate) mod subnet_allocation;
2527
mod update_and_check;
2628

2729
#[cfg(test)]

nexus/src/db/queries/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
//! Specialized queries for inserting database records, usually to maintain
6+
//! complex invariants that are most accurately expressed in a single query.
7+
8+
#[macro_use]
9+
mod next_item;
10+
pub mod network_interface;
11+
pub mod vni;
12+
pub mod vpc_subnet;

0 commit comments

Comments
 (0)