Skip to content

Commit ec7cab8

Browse files
committed
Use list of dict to remove duplicate nodes in _find_active_nodes()
This preserves the insertion order of nodes in the list of active nodes and removed the need to sort nodelists manually to compare them in unit tests. Signed-off-by: Jacopo De Amicis <[email protected]>
1 parent 6ce409b commit ec7cab8

File tree

2 files changed

+10
-27
lines changed

2 files changed

+10
-27
lines changed

src/slurm_plugin/clustermgtd.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,12 +1123,11 @@ def _find_bootstrap_failure_nodes(slurm_nodes):
11231123

11241124
@staticmethod
11251125
def _find_active_nodes(partitions_name_map):
1126-
active_nodes = set()
1126+
active_nodes = []
11271127
for partition in partitions_name_map.values():
11281128
if partition.state != "INACTIVE":
1129-
active_nodes |= set(partition.slurm_nodes)
1130-
# TODO: Returning the set breaks some unit tests (I believe the unit test should be revised though).
1131-
return sorted(list(active_nodes), key=str)
1129+
active_nodes += partition.slurm_nodes
1130+
return list(dict.fromkeys(active_nodes))
11321131

11331132
def _is_node_in_replacement_valid(self, node, check_node_is_valid):
11341133
"""

tests/slurm_plugin/test_clustermgtd.py

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,14 +1314,13 @@ def test_maintain_nodes(
13141314
# Run test
13151315
cluster_manager._maintain_nodes(partitions, {})
13161316
# Check function calls
1317-
active_nodes_sorted = sorted(active_nodes, key=str)
1318-
mock_update_replacement.assert_called_with(active_nodes_sorted)
1317+
mock_update_replacement.assert_called_with(active_nodes)
13191318
mock_handle_dynamic.assert_called_with(expected_unhealthy_dynamic_nodes)
13201319
mock_handle_static.assert_called_with(expected_unhealthy_static_nodes)
1321-
mock_handle_powering_down_nodes.assert_called_with(active_nodes_sorted)
1322-
mock_handle_failed_health_check_nodes_in_replacement.assert_called_with(active_nodes_sorted)
1320+
mock_handle_powering_down_nodes.assert_called_with(active_nodes)
1321+
mock_handle_failed_health_check_nodes_in_replacement.assert_called_with(active_nodes)
13231322
if _is_protected_mode_enabled:
1324-
mock_handle_protected_mode_process.assert_called_with(active_nodes_sorted, partitions)
1323+
mock_handle_protected_mode_process.assert_called_with(active_nodes, partitions)
13251324
else:
13261325
mock_handle_protected_mode_process.assert_not_called()
13271326

@@ -3795,21 +3794,6 @@ def test_find_unhealthy_slurm_nodes(
37953794
],
37963795
)
37973796
def test_find_active_nodes(partitions_name_map, expected_nodelist):
3798-
"""
3799-
Unit test for the `ClusterManager._find_active_nodes()` method.
3800-
3801-
Some context about the way this test is implemented:
3802-
- `ClusterManager._find_active_nodes()` may be implemented to return different types of iterables.
3803-
This test was implemented together with a fix that changed the return type from a list to a set,
3804-
and it was desirable to have the test compatible with both return types.
3805-
- The implementation that returned a list caused a duplication of node entities in the returned iterable
3806-
in case the same node belonged to multiple Slurm partitions (via a customization of the Slurm configuration).
3807-
- Due to the way we implement the `__hash__()` dunder method for the SlurmNode class, two different
3808-
SlurmNode objects with the same node name are squashed into the same entity in a set. Therefore
3809-
we cannot use `set(expected_nodelist)` when trying to test the duplication of the node entities
3810-
in the iterable returned by `ClusterManager._find_active_nodes()`.
3811-
- Sets are unordered, so when transforming them into lists we have to sort them to make them comparable with
3812-
the `expected_nodelist`.
3813-
"""
3814-
result_nodelist = sorted(list(ClusterManager._find_active_nodes(partitions_name_map)), key=str)
3815-
assert_that(result_nodelist).is_equal_to(sorted(expected_nodelist, key=str))
3797+
"""Unit test for the `ClusterManager._find_active_nodes()` method."""
3798+
result_nodelist = ClusterManager._find_active_nodes(partitions_name_map)
3799+
assert_that(result_nodelist).is_equal_to(expected_nodelist)

0 commit comments

Comments
 (0)