Skip to content

Commit

Permalink
Use a typed UUID for Volume
Browse files Browse the repository at this point in the history
wip fix build
  • Loading branch information
jmpesp committed Dec 20, 2024
1 parent 460f038 commit 7b48343
Show file tree
Hide file tree
Showing 56 changed files with 913 additions and 718 deletions.
1 change: 1 addition & 0 deletions clients/nexus-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ progenitor::generate_api!(
TypedUuidForUpstairsKind = omicron_uuid_kinds::TypedUuid<omicron_uuid_kinds::UpstairsKind>,
TypedUuidForUpstairsRepairKind = omicron_uuid_kinds::TypedUuid<omicron_uuid_kinds::UpstairsRepairKind>,
TypedUuidForUpstairsSessionKind = omicron_uuid_kinds::TypedUuid<omicron_uuid_kinds::UpstairsSessionKind>,
TypedUuidForVolumeKind = omicron_uuid_kinds::TypedUuid<omicron_uuid_kinds::VolumeKind>,
TypedUuidForZpoolKind = omicron_uuid_kinds::TypedUuid<omicron_uuid_kinds::ZpoolKind>,
},
patch = {
Expand Down
44 changes: 23 additions & 21 deletions dev-tools/omdb/src/bin/omdb/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ use omicron_uuid_kinds::InstanceUuid;
use omicron_uuid_kinds::PhysicalDiskUuid;
use omicron_uuid_kinds::PropolisUuid;
use omicron_uuid_kinds::SledUuid;
use omicron_uuid_kinds::VolumeUuid;
use sled_agent_client::types::VolumeConstructionRequest;
use std::borrow::Cow;
use std::cmp::Ordering;
Expand Down Expand Up @@ -1536,7 +1537,7 @@ async fn cmd_db_disk_info(
disk_name,
instance_name,
propolis_zone: format!("oxz_propolis-server_{}", propolis_id),
volume_id: disk.volume_id.to_string(),
volume_id: disk.volume_id().to_string(),
disk_state: disk.runtime_state.disk_state.to_string(),
}
} else {
Expand All @@ -1545,7 +1546,7 @@ async fn cmd_db_disk_info(
disk_name,
instance_name,
propolis_zone: NO_ACTIVE_PROPOLIS_MSG.to_string(),
volume_id: disk.volume_id.to_string(),
volume_id: disk.volume_id().to_string(),
disk_state: disk.runtime_state.disk_state.to_string(),
}
}
Expand All @@ -1557,7 +1558,7 @@ async fn cmd_db_disk_info(
disk_name: disk.name().to_string(),
instance_name: "-".to_string(),
propolis_zone: "-".to_string(),
volume_id: disk.volume_id.to_string(),
volume_id: disk.volume_id().to_string(),
disk_state: disk.runtime_state.disk_state.to_string(),
}
};
Expand All @@ -1571,7 +1572,7 @@ async fn cmd_db_disk_info(
println!("{}", table);

// Get the dataset backing this volume.
let regions = datastore.get_allocated_regions(disk.volume_id).await?;
let regions = datastore.get_allocated_regions(disk.volume_id()).await?;

let mut rows = Vec::with_capacity(3);
for (dataset, region) in regions {
Expand Down Expand Up @@ -1605,21 +1606,21 @@ async fn cmd_db_disk_info(

println!("{}", table);

get_and_display_vcr(disk.volume_id, datastore).await?;
get_and_display_vcr(disk.volume_id(), datastore).await?;
Ok(())
}

// Given a UUID, search the database for a volume with that ID
// If found, attempt to parse the .data field into a VolumeConstructionRequest
// and display it if successful.
async fn get_and_display_vcr(
volume_id: Uuid,
volume_id: VolumeUuid,
datastore: &DataStore,
) -> Result<(), anyhow::Error> {
// Get the VCR from the volume and display selected parts.
use db::schema::volume::dsl as volume_dsl;
let volumes = volume_dsl::volume
.filter(volume_dsl::id.eq(volume_id))
.filter(volume_dsl::id.eq(to_db_typed_uuid(volume_id)))
.limit(1)
.select(Volume::as_select())
.load_async(&*datastore.pool_connection_for_tests().await?)
Expand Down Expand Up @@ -1729,7 +1730,7 @@ async fn cmd_db_disk_physical(
.context("loading region")?;

for rs in regions {
volume_ids.insert(rs.volume_id());
volume_ids.insert(rs.volume_id().into_untyped_uuid());
}
}

Expand Down Expand Up @@ -1977,8 +1978,8 @@ impl From<Snapshot> for SnapshotRow {
state: format_snapshot(&s.state).to_string(),
size: s.size.to_string(),
source_disk_id: s.disk_id.to_string(),
source_volume_id: s.volume_id.to_string(),
destination_volume_id: s.destination_volume_id.to_string(),
source_volume_id: s.volume_id().to_string(),
destination_volume_id: s.destination_volume_id().to_string(),
}
}
}
Expand Down Expand Up @@ -2046,8 +2047,8 @@ async fn cmd_db_snapshot_info(
let mut dest_volume_ids = Vec::new();
let mut source_volume_ids = Vec::new();
let rows = snapshots.into_iter().map(|snapshot| {
dest_volume_ids.push(snapshot.destination_volume_id);
source_volume_ids.push(snapshot.volume_id);
dest_volume_ids.push(snapshot.destination_volume_id());
source_volume_ids.push(snapshot.volume_id());
SnapshotRow::from(snapshot)
});
if rows.len() == 0 {
Expand Down Expand Up @@ -2362,7 +2363,7 @@ async fn cmd_db_region_list(
struct RegionRow {
id: Uuid,
dataset_id: DatasetUuid,
volume_id: Uuid,
volume_id: VolumeUuid,
block_size: i64,
blocks_per_extent: u64,
extent_count: u64,
Expand Down Expand Up @@ -2414,7 +2415,8 @@ async fn cmd_db_region_used_by(
String::from("listing regions")
});

let volumes: Vec<Uuid> = regions.iter().map(|x| x.volume_id()).collect();
let volumes: Vec<Uuid> =
regions.iter().map(|x| x.volume_id().into_untyped_uuid()).collect();

let disks_used: Vec<Disk> = {
let volumes = volumes.clone();
Expand Down Expand Up @@ -2504,7 +2506,7 @@ async fn cmd_db_region_used_by(
#[derive(Tabled)]
struct RegionRow {
id: Uuid,
volume_id: Uuid,
volume_id: VolumeUuid,
usage_type: String,
usage_id: String,
usage_name: String,
Expand All @@ -2515,7 +2517,7 @@ async fn cmd_db_region_used_by(
.into_iter()
.map(|region: Region| {
if let Some(image) =
images_used.iter().find(|x| x.volume_id == region.volume_id())
images_used.iter().find(|x| x.volume_id() == region.volume_id())
{
RegionRow {
id: region.id(),
Expand All @@ -2528,7 +2530,7 @@ async fn cmd_db_region_used_by(
}
} else if let Some(snapshot) = snapshots_used
.iter()
.find(|x| x.volume_id == region.volume_id())
.find(|x| x.volume_id() == region.volume_id())
{
RegionRow {
id: region.id(),
Expand All @@ -2541,7 +2543,7 @@ async fn cmd_db_region_used_by(
}
} else if let Some(snapshot) = snapshots_used
.iter()
.find(|x| x.destination_volume_id == region.volume_id())
.find(|x| x.destination_volume_id() == region.volume_id())
{
RegionRow {
id: region.id(),
Expand All @@ -2553,7 +2555,7 @@ async fn cmd_db_region_used_by(
deleted: snapshot.time_deleted().is_some(),
}
} else if let Some(disk) =
disks_used.iter().find(|x| x.volume_id == region.volume_id())
disks_used.iter().find(|x| x.volume_id() == region.volume_id())
{
RegionRow {
id: region.id(),
Expand Down Expand Up @@ -2602,7 +2604,7 @@ async fn cmd_db_region_find_deleted(

#[derive(Tabled)]
struct VolumeRow {
volume_id: Uuid,
volume_id: VolumeUuid,
}

let region_rows: Vec<RegionRow> = freed_crucible_resources
Expand Down Expand Up @@ -4423,7 +4425,7 @@ async fn cmd_db_region_snapshot_replacement_request(
.insert_region_snapshot_replacement_request_with_volume_id(
opctx,
request,
db_snapshots[0].volume_id,
db_snapshots[0].volume_id(),
)
.await?;

Expand Down
13 changes: 10 additions & 3 deletions nexus/db-model/src/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use super::{BlockSize, ByteCount, DiskState, Generation};
use crate::typed_uuid::DbTypedUuid;
use crate::{schema::disk, unsigned::SqlU8};
use chrono::{DateTime, Utc};
use db_macros::Resource;
use nexus_types::external_api::params;
use nexus_types::identity::Resource;
use omicron_common::api::external;
use omicron_common::api::internal;
use omicron_uuid_kinds::VolumeKind;
use omicron_uuid_kinds::VolumeUuid;
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
use std::net::SocketAddrV6;
Expand Down Expand Up @@ -38,7 +41,7 @@ pub struct Disk {
pub project_id: Uuid,

/// Root volume of the disk
pub volume_id: Uuid,
volume_id: DbTypedUuid<VolumeKind>,

/// runtime state of the Disk
#[diesel(embed)]
Expand Down Expand Up @@ -81,7 +84,7 @@ impl Disk {
pub fn new(
disk_id: Uuid,
project_id: Uuid,
volume_id: Uuid,
volume_id: VolumeUuid,
params: params::DiskCreate,
block_size: BlockSize,
runtime_initial: DiskRuntimeState,
Expand All @@ -103,7 +106,7 @@ impl Disk {
identity,
rcgen: external::Generation::new().into(),
project_id,
volume_id,
volume_id: volume_id.into(),
runtime_state: runtime_initial,
slot: None,
size: params.size.into(),
Expand All @@ -129,6 +132,10 @@ impl Disk {
pub fn pantry_address(&self) -> Option<SocketAddrV6> {
self.pantry_address.as_ref().map(|x| x.parse().unwrap())
}

pub fn volume_id(&self) -> VolumeUuid {
self.volume_id.into()
}
}

/// Conversion to the external API type.
Expand Down
27 changes: 24 additions & 3 deletions nexus/db-model/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
use super::{BlockSize, ByteCount, Digest};
use crate::schema::{image, project_image, silo_image};
use crate::typed_uuid::DbTypedUuid;
use db_macros::Resource;
use nexus_types::external_api::views;
use nexus_types::identity::Resource;
use omicron_common::api::external::Error;
use omicron_uuid_kinds::VolumeKind;
use omicron_uuid_kinds::VolumeUuid;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

Expand All @@ -36,7 +39,7 @@ pub struct Image {
pub silo_id: Uuid,
pub project_id: Option<Uuid>,

pub volume_id: Uuid,
pub volume_id: DbTypedUuid<VolumeKind>,
pub url: Option<String>,
pub os: String,
pub version: String,
Expand All @@ -47,6 +50,12 @@ pub struct Image {
pub size: ByteCount,
}

impl Image {
pub fn volume_id(&self) -> VolumeUuid {
self.volume_id.into()
}
}

#[derive(
Queryable, Selectable, Clone, Debug, Resource, Serialize, Deserialize,
)]
Expand All @@ -57,7 +66,7 @@ pub struct ProjectImage {

pub silo_id: Uuid,
pub project_id: Uuid,
pub volume_id: Uuid,
pub volume_id: DbTypedUuid<VolumeKind>,
pub url: Option<String>,
pub os: String,
pub version: String,
Expand All @@ -68,6 +77,12 @@ pub struct ProjectImage {
pub size: ByteCount,
}

impl ProjectImage {
pub fn volume_id(&self) -> VolumeUuid {
self.volume_id.into()
}
}

#[derive(
Queryable, Selectable, Clone, Debug, Resource, Serialize, Deserialize,
)]
Expand All @@ -77,7 +92,7 @@ pub struct SiloImage {
pub identity: SiloImageIdentity,

pub silo_id: Uuid,
pub volume_id: Uuid,
pub volume_id: DbTypedUuid<VolumeKind>,
pub url: Option<String>,
pub os: String,
pub version: String,
Expand All @@ -88,6 +103,12 @@ pub struct SiloImage {
pub size: ByteCount,
}

impl SiloImage {
pub fn volume_id(&self) -> VolumeUuid {
self.volume_id.into()
}
}

impl TryFrom<Image> for ProjectImage {
type Error = Error;

Expand Down
12 changes: 7 additions & 5 deletions nexus/db-model/src/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use db_macros::Asset;
use omicron_common::api::external;
use omicron_uuid_kinds::DatasetKind;
use omicron_uuid_kinds::DatasetUuid;
use omicron_uuid_kinds::VolumeKind;
use omicron_uuid_kinds::VolumeUuid;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

Expand All @@ -34,7 +36,7 @@ pub struct Region {
identity: RegionIdentity,

dataset_id: DbTypedUuid<DatasetKind>,
volume_id: Uuid,
volume_id: DbTypedUuid<VolumeKind>,

block_size: ByteCount,

Expand All @@ -58,7 +60,7 @@ pub struct Region {
impl Region {
pub fn new(
dataset_id: DatasetUuid,
volume_id: Uuid,
volume_id: VolumeUuid,
block_size: ByteCount,
blocks_per_extent: u64,
extent_count: u64,
Expand All @@ -68,7 +70,7 @@ impl Region {
Self {
identity: RegionIdentity::new(Uuid::new_v4()),
dataset_id: dataset_id.into(),
volume_id,
volume_id: volume_id.into(),
block_size,
blocks_per_extent: blocks_per_extent as i64,
extent_count: extent_count as i64,
Expand All @@ -81,8 +83,8 @@ impl Region {
pub fn id(&self) -> Uuid {
self.identity.id
}
pub fn volume_id(&self) -> Uuid {
self.volume_id
pub fn volume_id(&self) -> VolumeUuid {
self.volume_id.into()
}
pub fn dataset_id(&self) -> DatasetUuid {
self.dataset_id.into()
Expand Down
Loading

0 comments on commit 7b48343

Please sign in to comment.