Skip to content

Commit

Permalink
refactor: replica listing based on query
Browse files Browse the repository at this point in the history
Signed-off-by: Abhinandan Purkait <[email protected]>
  • Loading branch information
Abhinandan-Purkait committed Aug 24, 2023
1 parent 9ddc016 commit 357a9fd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 42 deletions.
4 changes: 2 additions & 2 deletions io-engine-tests/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub use super::compose::rpc::v1::pool::Pool;
use super::{
compose::rpc::v1::{
pool::{CreatePoolRequest, ListPoolOptions},
replica::{ListReplicaOptions, Replica, ReplicaType},
replica::{ListReplicaOptions, Replica},
SharedRpcHandle,
Status,
},
Expand Down Expand Up @@ -104,7 +104,7 @@ impl PoolBuilder {
poolname: None,
uuid: None,
pooluuid: self.uuid.clone(),
replicatype: ReplicaType::AllReplicas as i32,
query: None,
})
.await
.map(|r| r.into_inner().replicas)
Expand Down
5 changes: 2 additions & 3 deletions io-engine-tests/src/replica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use mayastor_api::v1::replica::{
DestroyReplicaRequest,
ListReplicaOptions,
Replica,
ReplicaType,
ShareReplicaRequest,
};

Expand Down Expand Up @@ -211,7 +210,7 @@ pub async fn list_replicas(
poolname: None,
uuid: None,
pooluuid: None,
replicatype: ReplicaType::AllReplicas as i32,
query: None,
})
.await
.map(|r| r.into_inner().replicas)
Expand All @@ -229,7 +228,7 @@ pub async fn find_replica_by_uuid(
poolname: None,
uuid: Some(uuid.to_owned()),
pooluuid: None,
replicatype: ReplicaType::AllReplicas as i32,
query: None,
})
.await
.map(|r| r.into_inner().replicas)?
Expand Down
2 changes: 1 addition & 1 deletion io-engine/src/bin/io-engine-client/v1/replica_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ async fn replica_list(
poolname: None,
uuid: None,
pooluuid: None,
replicatype: v1_rpc::replica::ReplicaType::AllReplicas as i32,
query: None,
})
.await
.context(GrpcStatus)?;
Expand Down
58 changes: 23 additions & 35 deletions io-engine/src/grpc/v1/replica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,41 +125,31 @@ impl ReplicaService {
}
}
fn filter_replicas_by_replica_type(
replicas: Vec<Replica>,
replica_type: Option<ReplicaType>,
replica_list: Vec<Replica>,
query: Option<list_replica_options::Query>,
) -> Vec<Replica> {
let Some(replica_type) = replica_type else {
return replicas
let query = match query {
None => return replica_list,
Some(query) => query,
};
replicas
replica_list
.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
.filter(|replica| {
let query = &query;

let query_fields = vec![
(query.snapshot, replica.is_snapshot),
(query.clone, replica.is_clone),
// ... add other fields here as needed
];

query_fields.iter().all(|(query_field, replica_field)| {
match query_field {
Some(true) => *replica_field,
Some(false) => !(*replica_field),
None => true,
}
}
})
})
.collect()
}
Expand Down Expand Up @@ -353,10 +343,8 @@ 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),
);
let replicas =
filter_replicas_by_replica_type(replicas, args.query);
Ok(ListReplicasResponse {
replicas,
})
Expand Down
2 changes: 1 addition & 1 deletion rpc/mayastor-api

0 comments on commit 357a9fd

Please sign in to comment.