Skip to content

Commit

Permalink
feat(snapshots, restores): store restores as a part of snap meta, exp…
Browse files Browse the repository at this point in the history
…ose counts on rest

Signed-off-by: Abhinandan Purkait <[email protected]>
  • Loading branch information
Abhinandan-Purkait committed Aug 10, 2023
1 parent 28aad38 commit f15265d
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ use std::{fmt::Debug, ops::Deref, sync::Arc};
use stor_port::{
pstor::{product_v1_key_prefix, API_VERSION},
transport_api::ErrorChain,
types::v0::{store::snapshots::volume::VolumeSnapshot, transport::SnapshotId},
types::v0::{
store::{snapshots::volume::VolumeSnapshot, volume::VolumeContentSource},
transport::SnapshotId,
},
};

#[derive(Debug, Snafu)]
Expand Down Expand Up @@ -939,6 +942,18 @@ impl ResourceSpecsLocked {
}
}

// add runtime information for volume restores
for volume in self.read().volumes.values() {
match volume.immutable_ref().content_source.as_ref() {
None => continue,
Some(VolumeContentSource::Snapshot(snap_uuid, _)) => {
if let Some(snapshot) = self.read().volume_snapshots.get(snap_uuid) {
snapshot.lock().insert_restore(volume.uuid())
}
}
}
}

// Remove all entries of v1 key prefix.
store
.delete_values_prefix(&product_v1_key_prefix())
Expand Down
11 changes: 4 additions & 7 deletions control-plane/agents/src/bin/core/volume/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,11 @@ impl Service {
#[tracing::instrument(level = "info", skip(self), err, fields(volume.uuid = %request.uuid))]
pub(super) async fn destroy_volume(&self, request: &DestroyVolume) -> Result<(), SvcError> {
let mut volume = self.specs().volume(&request.uuid).await?;
let content_source = volume.as_ref().content_source.clone();
let content_source = volume.as_ref().content_source.as_ref();
let snap_guard = match content_source {
None => None,
Some(VolumeContentSource::Snapshot(snap_uuid, _)) => {
match self.specs().volume_snapshot(&snap_uuid).await {
match self.specs().volume_snapshot(snap_uuid).await {
Ok(snap_guard) => Some(snap_guard),
Err(SvcError::VolSnapshotNotFound { .. }) => None,
Err(error) => return Err(error),
Expand All @@ -314,16 +314,13 @@ impl Service {
};

match snap_guard {
None => {
volume.destroy(&self.registry, request).await?;
}
None => volume.destroy(&self.registry, request).await,
Some(mut snap_guard) => {
snap_guard
.destroy_clone(&self.registry, request, volume)
.await?;
.await
}
}
Ok(())
}

/// Destroy the shutdown targets associate with the volume.
Expand Down
2 changes: 1 addition & 1 deletion control-plane/grpc/proto/v1/volume/volume.proto
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ message VolumeSnapshotMeta {
// Size taken by the snapshot and its predecessors.
uint64 total_allocated_size = 7;
// Number of restores done from this snapshot.
uint32 num_restore = 8;
uint32 num_restores = 8;

message ReplicaSnapshots {
repeated ReplicaSnapshot snapshots = 1;
Expand Down
5 changes: 3 additions & 2 deletions control-plane/grpc/src/operations/volume/traits_snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ pub struct VolumeSnapshotMeta {
/// The number of restores done from this snapshot.
num_restores: u32,
}

impl VolumeSnapshotMeta {
/// Get the volume snapshot status.
pub fn status(&self) -> &SpecStatus<()> {
Expand Down Expand Up @@ -475,7 +476,7 @@ impl TryFrom<volume::VolumeSnapshot> for VolumeSnapshot {
snapshots.map(|s| (k, s))
})
.collect::<Result<HashMap<_, _>, _>>()?,
num_restores: meta.num_restore,
num_restores: meta.num_restores,
},
state: VolumeSnapshotState {
info,
Expand Down Expand Up @@ -632,7 +633,7 @@ impl TryFrom<VolumeSnapshot> for volume::VolumeSnapshot {
size: value.meta.size,
spec_size: value.meta.spec_size,
total_allocated_size: value.meta.total_allocated_size,
num_restore: value.meta.num_restores,
num_restores: value.meta.num_restores,
}),
state: Some(volume::VolumeSnapshotState {
state: Some(snapshot::SnapshotState {
Expand Down
2 changes: 1 addition & 1 deletion control-plane/plugin/src/bin/rest-plugin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::Parser;
use openapi::tower::client::Url;
use plugin::{
operations::{
Cordoning, Drain, Get, GetBlockDevices, GetSnapshots, List, ListVolumes, Operations,
Cordoning, Drain, Get, GetBlockDevices, GetSnapshots, List, ListExt, Operations,
RebuildHistory, ReplicaTopology, Scale,
},
resources::{
Expand Down
15 changes: 8 additions & 7 deletions control-plane/plugin/src/operations.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::resources::{
utils, volume::VolumesArgs, CordonResources, DrainResources, GetResources, ScaleResources,
};
use crate::resources::{utils, CordonResources, DrainResources, GetResources, ScaleResources};
use async_trait::async_trait;

/// The types of operations that are supported.
Expand Down Expand Up @@ -43,10 +41,12 @@ pub trait List {
async fn list(output: &utils::OutputFormat);
}

/// ListVolumes trait, to be implemented for list with volume_args.
/// List trait.
/// To be implemented by resources which support the 'list' operation, with context.
#[async_trait(?Send)]
pub trait ListVolumes {
async fn list(output: &utils::OutputFormat, volume_args: &VolumesArgs);
pub trait ListExt {
type Context;
async fn list(output: &utils::OutputFormat, context: &Self::Context);
}

/// Get trait.
Expand All @@ -70,7 +70,8 @@ pub trait Scale {
#[async_trait(?Send)]
pub trait ReplicaTopology {
type ID;
async fn topologies(output: &utils::OutputFormat, volume_args: &VolumesArgs);
type Context;
async fn topologies(output: &utils::OutputFormat, context: &Self::Context);
async fn topology(id: &Self::ID, output: &utils::OutputFormat);
}

Expand Down
15 changes: 8 additions & 7 deletions control-plane/plugin/src/resources/volume.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
operations::{Get, ListVolumes, RebuildHistory, ReplicaTopology, Scale},
operations::{Get, ListExt, RebuildHistory, ReplicaTopology, Scale},
resources::{
utils,
utils::{optional_cell, CreateRow, CreateRows, GetHeaderRow, OutputFormat},
Expand Down Expand Up @@ -72,9 +72,10 @@ impl GetHeaderRow for openapi::models::Volume {
}

#[async_trait(?Send)]
impl ListVolumes for Volumes {
async fn list(output: &utils::OutputFormat, volume_args: &VolumesArgs) {
if let Some(volumes) = get_paginated_volumes(volume_args).await {
impl ListExt for Volumes {
type Context = VolumesArgs;
async fn list(output: &OutputFormat, context: &Self::Context) {
if let Some(volumes) = get_paginated_volumes(context).await {
// Print table, json or yaml based on output format.
utils::print_table(output, volumes);
}
Expand Down Expand Up @@ -171,9 +172,9 @@ impl Scale for Volume {
#[async_trait(?Send)]
impl ReplicaTopology for Volume {
type ID = VolumeId;
async fn topologies(output: &OutputFormat, volume_args: &VolumesArgs) {
let volumes =
VolumeTopologies(get_paginated_volumes(volume_args).await.unwrap_or_default());
type Context = VolumesArgs;
async fn topologies(output: &OutputFormat, context: &Self::Context) {
let volumes = VolumeTopologies(get_paginated_volumes(context).await.unwrap_or_default());
utils::print_table(output, volumes);
}
async fn topology(id: &Self::ID, output: &OutputFormat) {
Expand Down
12 changes: 7 additions & 5 deletions control-plane/stor-port/src/types/v0/store/snapshots/volume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ impl VolumeSnapshot {
self.metadata.transactions.extend(transactions)
}
/// Insert a restore to runtime meta restores.
pub fn insert_restore(&mut self, restored_volume: VolumeId) {
self.metadata.runtime_meta.restores.insert(restored_volume)
pub fn insert_restore(&mut self, restored_volume: &VolumeId) {
self.metadata
.runtime_meta
.restores
.insert(restored_volume.clone())
}
/// Remove a restore from runtime meta restores.
pub fn remove_restore(&mut self, restored_volume: &VolumeId) {
Expand Down Expand Up @@ -195,6 +198,7 @@ struct VolumeSnapshotRuntimeMetadata {
/// List of all volume snapshot restore and related information.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct VolumeRestoreList {
/// Runtime list of all volumes restored from this snapshot.
pub(crate) restores: HashSet<VolumeId>,
}
impl VolumeRestoreList {
Expand Down Expand Up @@ -400,9 +404,7 @@ impl SpecTransaction<VolumeSnapshotOperation> for VolumeSnapshot {
}
}
VolumeSnapshotOperation::CleanupStaleTransactions => {}
VolumeSnapshotOperation::CreateRestore(info) => {
self.insert_restore(info.volume_uuid().clone())
}
VolumeSnapshotOperation::CreateRestore(info) => self.insert_restore(info.volume_uuid()),
VolumeSnapshotOperation::DestroyRestore(info) => {
self.remove_restore(info.volume_uuid())
}
Expand Down
1 change: 1 addition & 0 deletions control-plane/stor-port/src/types/v0/store/volume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ pub struct VolumeSpec {
#[serde(default)]
pub affinity_group: Option<AffinityGroup>,
/// Number of snapshots taken on this volume.
#[serde(skip)]
pub num_snapshots: u32,
/// Volume metadata information.
#[serde(default, skip_serializing_if = "super::is_default")]
Expand Down

0 comments on commit f15265d

Please sign in to comment.