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

try to optimize getting all controllers #1659

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
104 changes: 92 additions & 12 deletions rust/agama-lib/src/storage/client/zfcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,71 @@ use crate::{
},
};

pub async fn get_wwpns(controller_id: &str) -> Result<Vec<String>, ServiceError> {
let output = std::process::Command::new("zfcp_san_disc")
.arg("-b")
.arg(controller_id)
.arg("-W")
.output()
.expect("zfcp_san_disc failed"); // TODO: real error handling
Ok(String::from_utf8_lossy(&output.stdout)
.split_whitespace()
.map(|s| s.to_string())
.collect())
}

pub async fn get_luns(controller_id: String, wwpn: String) -> Result<Vec<String>, ServiceError> {
let output = std::process::Command::new("zfcp_san_disc")
.arg("-b")
.arg(controller_id)
.arg("-p")
.arg(wwpn)
.arg("-L")
.output()
.expect("zfcp_san_disc failed"); // TODO: real error handling
Ok(String::from_utf8_lossy(&output.stdout)
.split_whitespace()
.map(|s| s.to_string())
.collect())
}

/// Obtains a LUNs map for the given controller
///
/// Given a controller id it returns a HashMap with each of its WWPNs as keys and the list of
/// LUNS corresponding to that specific WWPN as values.
///
/// Arguments:
///
/// `controller_id`: controller id
pub async fn get_luns_map(
controller_id: String,
) -> Result<HashMap<String, Vec<String>>, ServiceError> {
let wwpns = get_wwpns(controller_id.as_str()).await?;
let mut tasks = vec![];
for wwpn in wwpns {
tasks.push((
wwpn.clone(),
tokio::spawn(get_luns(controller_id.to_string(), wwpn.clone())),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of spawning all those processes, I would suggest giving join_all a try.

Copy link
Contributor

@imobachgs imobachgs Oct 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tokio has JoinSet, which might be interesting too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for suggestion. I plan to have it as experiment first and this tokio spawn is suggested way in that linkedin rust training, so it is the first that I would like to give a try.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and testing shows that my change somehow does not work, but as it is just learning/research project I will put it a bit on hold and focus on planned stuff and return to it probably next week.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I cannot sleep without debugging it...so now it is fixed and time is improved to 6.3 second..so already it goes down from 10.8 to 6.3 and I think it can be even better if I parallel also getting wwpns for controller. And then I can start playing with join_all and JoinSet

));
}
let mut result = HashMap::with_capacity(tasks.len());
for (wwpn, task) in tasks {
result.insert(wwpn.clone(), task.await.expect("thread join failed")?);
}
Ok(result)
}

const ZFCP_CONTROLLER_PREFIX: &'static str = "/org/opensuse/Agama/Storage1/zfcp_controllers";

/// Partial zfcp controller data used for thread processing
/// TODO: for final version move all those thread related methods to this struct to procude final result
struct PartialZFCPController {
pub id: String,
pub channel: String,
pub lun_scan: bool,
pub active: bool
}

/// Client to connect to Agama's D-Bus API for zFCP management.
#[derive(Clone)]
pub struct ZFCPClient<'a> {
Expand Down Expand Up @@ -99,23 +162,40 @@ impl<'a> ZFCPClient<'a> {
) -> Result<Vec<(OwnedObjectPath, ZFCPController)>, ServiceError> {
let managed_objects = self.object_manager_proxy.get_managed_objects().await?;

let mut devices: Vec<(OwnedObjectPath, ZFCPController)> = vec![];
let mut devices: Vec<(OwnedObjectPath, PartialZFCPController)> = vec![];
for (path, ifaces) in managed_objects {
if let Some(properties) = ifaces.get("org.opensuse.Agama.Storage1.ZFCP.Controller") {
let id = extract_id_from_path(&path)?.to_string();
let channel: String = get_property(properties, "Channel")?;
devices.push((
path,
ZFCPController {
id: id.clone(),
channel: get_property(properties, "Channel")?,
PartialZFCPController {
id: id,
channel: channel,
lun_scan: get_property(properties, "LUNScan")?,
active: get_property(properties, "Active")?,
luns_map: self.get_luns_map(id.as_str()).await?,
},
))
}
}
Ok(devices)
let mut tasks = vec![];
for (_path, partial_controller) in &devices {
tasks.push(
tokio::spawn(get_luns_map(partial_controller.channel.clone()))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with this additional paralellization time is reduced to 5.3 seconds. So basically for two controllers with 3 LUNS in total it is gets to half of time. I expect that for bigger systems it will be even more significant.

)
}
let mut result = Vec::with_capacity(devices.len());
for (index, task) in tasks.into_iter().enumerate() {
let (path, partial) = devices.get(index).unwrap();
result.push((path.clone(), ZFCPController {
id: partial.id.clone(),
channel: partial.channel.clone(),
lun_scan: partial.lun_scan,
active: partial.active,
luns_map: task.await.expect("thread join failed")?,
}));
}
Ok(result)
}

async fn get_controller_proxy(
Expand All @@ -129,12 +209,6 @@ impl<'a> ZFCPClient<'a> {
Ok(dbus)
}

pub async fn activate_controller(&self, controller_id: &str) -> Result<(), ServiceError> {
let controller = self.get_controller_proxy(controller_id).await?;
controller.activate().await?;
Ok(())
}

pub async fn get_wwpns(&self, controller_id: &str) -> Result<Vec<String>, ServiceError> {
let controller = self.get_controller_proxy(controller_id).await?;
let result = controller.get_wwpns().await?;
Expand Down Expand Up @@ -176,6 +250,12 @@ impl<'a> ZFCPClient<'a> {
.collect::<Result<HashMap<String, Vec<String>>, _>>()
}

pub async fn activate_controller(&self, controller_id: &str) -> Result<(), ServiceError> {
let controller = self.get_controller_proxy(controller_id).await?;
controller.activate().await?;
Ok(())
}

pub async fn activate_disk(
&self,
controller_id: &str,
Expand Down
Loading