From d9d6a94751407e389f01c622116d0fd6bc8c83a3 Mon Sep 17 00:00:00 2001 From: ut003460 Date: Fri, 29 Mar 2024 15:11:49 +0800 Subject: [PATCH] Multipath: Optimization method for obtaining multipath states 1. Early return: When a matching path is found, the result can be returned immediately without continuing to traverse other paths. This can improve efficiency. 2. Use a dictionary to store path information: Path information can be stored in the dictionary with the path as the key, which can quickly find the corresponding path's status information. 3. Exception handling: Add appropriate exception handling to deal with situations where a matching path is not found Signed-off-by: ut003460 --- avocado/utils/multipath.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/avocado/utils/multipath.py b/avocado/utils/multipath.py index c1b43a7ac1..cf8da593a7 100644 --- a/avocado/utils/multipath.py +++ b/avocado/utils/multipath.py @@ -224,11 +224,18 @@ def get_path_status(disk_path): mpath_op = get_multipath_details() if not mpath_op: return ("", "", "") - for maps in mpath_op["maps"]: - for path_groups in maps["path_groups"]: - for paths in path_groups["paths"]: - if paths["dev"] == disk_path: - return (paths["dm_st"], paths["dev_st"], paths["chk_st"]) + + paths_info = {} + for maps in mpath_op.get("maps", []): + for path_groups in maps.get("path_groups", []): + for paths in path_groups.get("paths", []): + paths_info[paths["dev"]] = ( + paths["dm_st"], + paths["dev_st"], + paths["chk_st"], + ) + + return paths_info.get(disk_path, ("", "", "")) def get_mpath_paths_status(wwid):