Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(snapshot/clone): handle different query options for listsnapshot and listreplica #1479

Merged
merged 2 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions io-engine-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub mod nvme;
pub mod nvmf;
pub mod pool;
pub mod replica;
pub mod snapshot;

pub use compose::MayastorTest;

Expand Down
1 change: 1 addition & 0 deletions io-engine-tests/src/replica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use mayastor_api::v1::replica::{
ReplicaType,
ShareReplicaRequest,
};

use tonic::{Code, Status};

#[derive(Clone)]
Expand Down
199 changes: 199 additions & 0 deletions io-engine-tests/src/snapshot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
use super::{compose::rpc::v1::SharedRpcHandle, generate_uuid};

use mayastor_api::v1::snapshot::{
CreateReplicaSnapshotRequest,
CreateReplicaSnapshotResponse,
CreateSnapshotCloneRequest,
ListSnapshotCloneRequest,
// ListSnapshotCloneResponse,
ListSnapshotsRequest,
// ListSnapshotsResponse,
Replica,
SnapshotInfo,
SnapshotQueryType,
};
use tonic::Status;

pub struct ReplicaSnapshotBuilder {
pub rpc: SharedRpcHandle,
pub replica_uuid: Option<String>,
pub snapshot_uuid: Option<String>,
pub snapshot_name: Option<String>,
pub entity_id: Option<String>,
pub txn_id: Option<String>,
}
impl ReplicaSnapshotBuilder {
pub fn new(rpc: SharedRpcHandle) -> Self {
Self {
rpc,
replica_uuid: None,
snapshot_uuid: None,
snapshot_name: None,
entity_id: None,
txn_id: None,
}
}
pub fn with_replica_uuid(mut self, replica_uuid: &str) -> Self {
self.replica_uuid = Some(replica_uuid.to_owned());
self
}
pub fn with_snapshot_uuid(mut self) -> Self {
self.snapshot_uuid = Some(generate_uuid());
self
}
pub fn with_snapshot_name(mut self, snap_name: &str) -> Self {
self.snapshot_name = Some(snap_name.to_owned());
self
}
pub fn with_entity_id(mut self, entity_id: &str) -> Self {
self.entity_id = Some(entity_id.to_owned());
self
}
pub fn with_txn_id(mut self, txn_id: &str) -> Self {
self.txn_id = Some(txn_id.to_owned());
self
}
pub fn snapshot_uuid(&self) -> String {
self.snapshot_uuid
.as_ref()
.expect("Snapshot UUID must be set")
.clone()
}
pub fn replica_uuid(&self) -> String {
self.replica_uuid
.as_ref()
.expect("Replica UUID must be set")
.clone()
}
pub fn snapshot_name(&self) -> String {
self.snapshot_name
.as_ref()
.expect("Snapshot name must be set")
.clone()
}
pub fn rpc(&self) -> SharedRpcHandle {
self.rpc.clone()
}
pub async fn create_replica_snapshot(
&mut self,
) -> Result<CreateReplicaSnapshotResponse, Status> {
self.rpc()
.lock()
.await
.snapshot
.create_replica_snapshot(CreateReplicaSnapshotRequest {
replica_uuid: self.replica_uuid(),
snapshot_uuid: self.snapshot_uuid(),
snapshot_name: self.snapshot_name(),
entity_id: self.entity_id.as_ref().unwrap().to_string(),
txn_id: self.txn_id.as_ref().unwrap().to_string(),
})
.await
.map(|r| r.into_inner())
}
pub async fn get_snapshots(&self) -> Result<Vec<SnapshotInfo>, Status> {
Ok(list_snapshot(self.rpc())
.await
.expect("List Snapshot Failed")
.into_iter()
.filter(|s| s.source_uuid == self.replica_uuid())
.collect::<Vec<_>>())
}
}
pub async fn list_snapshot(
rpc: SharedRpcHandle,
) -> Result<Vec<SnapshotInfo>, Status> {
rpc.lock()
.await
.snapshot
.list_snapshot(ListSnapshotsRequest {
source_uuid: None,
snapshot_uuid: None,
snapshot_query_type: SnapshotQueryType::AllSnapshots as i32,
})
.await
.map(|r| r.into_inner().snapshots)
}

pub struct SnapshotCloneBuilder {
pub rpc: SharedRpcHandle,
pub snapshot_uuid: Option<String>,
pub clone_name: Option<String>,
pub clone_uuid: Option<String>,
}
impl SnapshotCloneBuilder {
pub fn new(rpc: SharedRpcHandle) -> Self {
Self {
rpc,
snapshot_uuid: None,
clone_name: None,
clone_uuid: None,
}
}
pub fn with_snapshot_uuid(mut self, snapshot_uuid: &str) -> Self {
self.snapshot_uuid = Some(snapshot_uuid.to_owned());
self
}
pub fn with_clone_name(mut self, clone_name: &str) -> Self {
self.clone_name = Some(clone_name.to_owned());
self
}
pub fn with_clone_uuid(mut self, clone_uuid: &str) -> Self {
self.clone_uuid = Some(clone_uuid.to_owned());
self
}
pub fn rpc(&self) -> SharedRpcHandle {
self.rpc.clone()
}
pub fn snapshot_uuid(&self) -> String {
self.snapshot_uuid
.as_ref()
.expect("snapshot_uuid must be set")
.clone()
}
pub fn clone_name(&self) -> String {
self.clone_name
.as_ref()
.expect("clone_name must be set")
.clone()
}
pub fn clone_uuid(&self) -> String {
self.clone_uuid
.as_ref()
.expect("clone_uuid must be set")
.clone()
}
pub async fn create_snapshot_clone(&mut self) -> Result<Replica, Status> {
self.rpc()
.lock()
.await
.snapshot
.create_snapshot_clone(CreateSnapshotCloneRequest {
snapshot_uuid: self.snapshot_uuid(),
clone_name: self.clone_name(),
clone_uuid: self.clone_uuid(),
})
.await
.map(|r| r.into_inner())
}
pub async fn get_clones(&self) -> Result<Vec<Replica>, Status> {
Ok(list_snapshot_clone(self.rpc())
.await
.expect("List Clone Failed")
.into_iter()
.filter(|s| s.snapshot_uuid == Some(self.snapshot_uuid()))
.collect::<Vec<_>>())
}
}
pub async fn list_snapshot_clone(
rpc: SharedRpcHandle,
) -> Result<Vec<Replica>, Status> {
rpc.lock()
.await
.snapshot
.list_snapshot_clone(ListSnapshotCloneRequest {
snapshot_uuid: None,
})
.await
.map(|r| r.into_inner().replicas)
}
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 @@ -122,7 +122,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 @@ -313,7 +351,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
Loading