Skip to content

Commit

Permalink
feat(snapshot/clone): handle different query options for list snapsho…
Browse files Browse the repository at this point in the history
…t and listreplica

Signed-off-by: Hrudaya <[email protected]>
  • Loading branch information
hrudaya21 committed Aug 2, 2023
1 parent efb75c7 commit 4f08ac4
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 33 deletions.
4 changes: 2 additions & 2 deletions io-engine/src/bin/io-engine-client/v1/snapshot_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use colored_json::ToColoredJson;
use mayastor_api::v1 as v1_rpc;
use snafu::ResultExt;
use tonic::Status;
use v1_rpc::snapshot::*;

pub async fn handler(
ctx: Context,
Expand Down Expand Up @@ -412,7 +411,8 @@ async fn list(mut ctx: Context, matches: &ArgMatches<'_>) -> crate::Result<()> {
let request = v1_rpc::snapshot::ListSnapshotsRequest {
source_uuid,
snapshot_uuid,
snapshot_query_type: SnapshotQueryType::AllSnapshots as i32,
snapshot_query_type: v1_rpc::snapshot::SnapshotQueryType::AllSnapshots
as i32,
};

let response = ctx
Expand Down
45 changes: 43 additions & 2 deletions io-engine/src/grpc/v1/replica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,45 @@ impl ReplicaService {
}
}
}

fn filter_replicas_by_replica_type(
replicas: Vec<Replica>,
replica_type: Option<ReplicaType>,
) -> Vec<Replica> {
let Some(replica_type) = replica_type else {
return replicas
};
replicas
.into_iter()
.filter_map(|r| match replica_type {
// AllReplicas
ReplicaType::AllReplicas => Some(r),
// AllReplicasExceptSnapshots
ReplicaType::AllReplicasExceptSnapshots => {
if !r.is_snapshot {
Some(r)
} else {
None
}
}
// OnlySnapshotClones
ReplicaType::OnlySnapshotClones => {
if r.is_clone {
Some(r)
} else {
None
}
}
// OnlyReplicas
ReplicaType::OnlyReplicas => {
if !r.is_snapshot && !r.is_clone {
Some(r)
} else {
None
}
}
})
.collect()
}
#[tonic::async_trait]
impl ReplicaRpc for ReplicaService {
#[named]
Expand Down Expand Up @@ -312,7 +350,10 @@ impl ReplicaRpc for ReplicaService {
} else if let Some(uuid) = args.uuid {
replicas.retain(|r| r.uuid == uuid);
}

let replicas = filter_replicas_by_replica_type(
replicas,
ReplicaType::from_i32(args.replicatype),
);
Ok(ListReplicasResponse {
replicas,
})
Expand Down
101 changes: 73 additions & 28 deletions io-engine/src/grpc/v1/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,52 @@ impl SnapshotService {
}
}

/// Filter snapshots based on snapshot_query_type came in gRPC request.
fn filter_snapshots_by_snapshot_query_type(
snapshot_list: Vec<SnapshotInfo>,
snap_query_type: i32,
) -> Vec<SnapshotInfo> {
snapshot_list
.into_iter()
.filter_map(|s| match snap_query_type {
// AllSnapshots
0 => Some(s),
// AllSnapshotsExceptDiscardedSnapshots
1 => {
if !s.discarded_snapshot {
Some(s)
} else {
None
}
}
// OnlyDiscardedSnapshots
2 => {
if s.discarded_snapshot {
Some(s)
} else {
None
}
}
// OnlyInvalidSnapshots
3 => {
if !s.valid_snapshot {
Some(s)
} else {
None
}
}
// OnlyUsableSnapshots
4 => {
if !s.discarded_snapshot && s.valid_snapshot {
Some(s)
} else {
None
}
}
_ => Some(s),
})
.collect()
}
#[tonic::async_trait]
impl SnapshotRpc for SnapshotService {
#[named]
Expand Down Expand Up @@ -458,10 +504,11 @@ impl SnapshotRpc for SnapshotService {
let args = request.into_inner();
trace!("{:?}", args);
let rx = rpc_submit(async move {
let snapshots: Vec<SnapshotInfo>;
// if snapshot_uuid is input, get specific snapshot result
if let Some(snapshot_uuid) = args.snapshot_uuid {
if let Some(ref snapshot_uuid) = args.snapshot_uuid {
let lvol = match UntypedBdev::lookup_by_uuid_str(
&snapshot_uuid,
snapshot_uuid,
) {
Some(bdev) => Lvol::try_from(bdev)?,
None => {
Expand All @@ -473,49 +520,47 @@ impl SnapshotRpc for SnapshotService {
})
}
};
let snapshots = lvol
snapshots = lvol
.list_snapshot_by_snapshot_uuid()
.into_iter()
.map(SnapshotInfo::from)
.collect();
Ok(ListSnapshotsResponse {
snapshots,
})
} else if let Some(replica_uuid) = args.source_uuid {
} else if let Some(ref replica_uuid) = args.source_uuid {
// if replica_uuid is valid, filter snapshot based
// on source_uuid
let lvol = match UntypedBdev::lookup_by_uuid_str(
&replica_uuid,
) {
Some(bdev) => Lvol::try_from(bdev)?,
None => {
return Err(LvsError::Invalid {
source: Errno::ENOENT,
msg: format!(
"Replica {replica_uuid} not found",
),
})
}
};
let snapshots = lvol
let lvol =
match UntypedBdev::lookup_by_uuid_str(replica_uuid)
{
Some(bdev) => Lvol::try_from(bdev)?,
None => {
return Err(LvsError::Invalid {
source: Errno::ENOENT,
msg: format!(
"Replica {replica_uuid} not found",
),
})
}
};
snapshots = lvol
.list_snapshot_by_source_uuid()
.into_iter()
.map(SnapshotInfo::from)
.collect();
Ok(ListSnapshotsResponse {
snapshots,
})
} else {
// if source_uuid is not input, list all snapshot
// present in system
let snapshots = Lvol::list_all_snapshots()
snapshots = Lvol::list_all_snapshots()
.into_iter()
.map(SnapshotInfo::from)
.collect();
Ok(ListSnapshotsResponse {
snapshots,
})
}
let snapshots = filter_snapshots_by_snapshot_query_type(
snapshots,
args.snapshot_query_type,
);
Ok(ListSnapshotsResponse {
snapshots,
})
})?;
rx.await
.map_err(|_| Status::cancelled("cancelled"))?
Expand Down
2 changes: 1 addition & 1 deletion io-engine/src/lvs/lvs_lvol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ impl LvsLvol for Lvol {

// If destroy replica is a snapshot clone and it is the last
// clone from the snapshot, destroy the snapshot
// if it is already marked as discardedSnapshot.
// if it is already marked as discarded snapshot.
if let Some(snapshot_lvol) = snapshot_lvol {
if snapshot_lvol.list_clones_by_snapshot_uuid().is_empty()
&& snapshot_lvol.is_discarded_snapshot()
Expand Down

0 comments on commit 4f08ac4

Please sign in to comment.