From 112569dffb2339dbcb194221b8afbc8d2311aa65 Mon Sep 17 00:00:00 2001 From: hanwenli Date: Mon, 9 Nov 2026 12:13:50 -0800 Subject: [PATCH] Split launch batches rejected for exceeding the network interfaces allowed per RunInstances request EC2 RunInstances rejects a request when the total number of network interfaces per instance exceeds 5000. With the default launch_max_batch_size of 500, every instance type with more than 10 network interfaces exceeds it. For example, PC configures 17 interfaces on p6-b300.48xlarge. CreateFleet is not affected, because EC2 Fleet splits its own launches internally. When a batch of more than 50 nodes fails with AttachmentLimitExceeded, retry it and the remaining batches of the compute resource at half the size. Tests: * Unit tests with RunInstances mocked to enforce the per-request limit: 600 nodes with 17 network interfaces launch as 500 (rejected), 250, 250, 100 for both best-effort and all-or-nothing, and a single instance over the limit fails all 500 nodes in 20 calls. * Reproduced the failure on PC 3.16.2 (c6g.16xlarge, 15 interfaces through RunInstances overrides, 500 static nodes). --- CHANGELOG.md | 1 + src/slurm_plugin/instance_manager.py | 36 +++++++++-- tests/slurm_plugin/test_instance_manager.py | 69 +++++++++++++++++++++ 3 files changed, 101 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd4f8e3b..58f85316 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ This file is used to list changes made in each version of the aws-parallelcluste **BUG FIXES** - Fix dynamic nodes getting stuck in `COMPLETING` state indefinitely, holding their jobs, when the backing instance of a node with jobs is terminated and its private IP is reused by another compute node. +- Fix compute nodes with more than 10 network interfaces each failing to launch in large numbers at once. 3.16.2 ------ diff --git a/src/slurm_plugin/instance_manager.py b/src/slurm_plugin/instance_manager.py index 6d9c3254..3075b1fe 100644 --- a/src/slurm_plugin/instance_manager.py +++ b/src/slurm_plugin/instance_manager.py @@ -49,6 +49,18 @@ BOTO3_PAGINATION_PAGE_SIZE = 1000 BOTO3_MAX_BATCH_SIZE = 50 +# RunInstances rejects a request launching more network interfaces than allowed (MaxCount multiplied by the network +# interfaces of each instance) with this code, launching nothing. Instance types with many network interfaces +# (e.g. p6-b300 with 17) hit it with the default launch batch size, so the batch is split and launched again. +REQUEST_ATTACHMENT_LIMIT_ERROR_CODE = "AttachmentLimitExceeded" + + +def _get_launch_error_code(exception): + """Return the EC2 error code of a failed launch, None if it is unknown.""" + if isinstance(exception, ClientError): + return exception.response.get("Error", {}).get("Code") + return getattr(exception, "code", None) + class HostnameTableStoreError(Exception): """Raised when error occurs while writing into hostname table.""" @@ -948,7 +960,9 @@ def _launch_instances( # noqa: C901 fleet_manager = self._get_fleet_manager(all_or_nothing_batch, compute_resource, queue) - for batch_nodes in grouper(slurm_node_list, launch_batch_size): + pending_batches = list(grouper(slurm_node_list, launch_batch_size)) + while pending_batches: + batch_nodes = pending_batches.pop(0) try: launched_ec2_instances = self._launch_ec2_instances( batch_nodes, compute_resource, fleet_manager, instances_launched, job, queue @@ -962,16 +976,28 @@ def _launch_instances( # noqa: C901 # This path handle the CreateFleet case, which doesn't fail when no capacity is returned return instances_launched except (ClientError, Exception) as e: + error_code = _get_launch_error_code(e) + if error_code == REQUEST_ATTACHMENT_LIMIT_ERROR_CODE and len(batch_nodes) > 50: + # Nothing was launched: retry this and the remaining batches at half the size + split_batch_size = (len(batch_nodes) + 1) // 2 + logger.warning( + "Launch request for nodes %s exceeded the network interfaces allowed in a single " + "request, retrying in batches of %s nodes", + print_with_count(batch_nodes), + split_batch_size, + ) + remaining_nodes = [node for batch in [batch_nodes, *pending_batches] for node in batch] + pending_batches = list(grouper(remaining_nodes, split_batch_size)) + continue + logger.error( "Encountered exception when launching instances for nodes %s: %s", print_with_count(batch_nodes), e, ) update_failed_nodes_parameters = {"nodeset": set(batch_nodes)} - if isinstance(e, ClientError): - update_failed_nodes_parameters["error_code"] = e.response.get("Error", {}).get("Code") - elif isinstance(e, Exception) and hasattr(e, "code"): - update_failed_nodes_parameters["error_code"] = e.code + if error_code is not None: + update_failed_nodes_parameters["error_code"] = error_code self._update_failed_nodes(**update_failed_nodes_parameters) if job and all_or_nothing_batch: diff --git a/tests/slurm_plugin/test_instance_manager.py b/tests/slurm_plugin/test_instance_manager.py index f127e446..bd473a36 100644 --- a/tests/slurm_plugin/test_instance_manager.py +++ b/tests/slurm_plugin/test_instance_manager.py @@ -21,6 +21,7 @@ import pytest import slurm_plugin from assertpy import assert_that +from botocore.exceptions import ClientError from slurm_plugin.common import ScalingStrategy from slurm_plugin.fleet_manager import EC2Instance, LaunchInstancesError from slurm_plugin.instance_manager import ( @@ -4096,6 +4097,74 @@ def test_launch_instances_reports_blocking_error_instead_of_retrying_throttling( assert_that(instance_manager.failed_nodes).is_equal_to({"VcpuLimitExceeded": {"queue2-dy-fleet-ondemand-1"}}) + @staticmethod + def _mock_run_instances_with_network_interfaces(mocker, network_interfaces_per_instance, max_per_request=5000): + """Mock RunInstances rejecting requests over the network interfaces per request limit, like EC2 does.""" + requested_counts = [] + + def _run_instances(region, boto3_config, run_instances_kwargs): + count = run_instances_kwargs["MaxCount"] + requested_counts.append(count) + if network_interfaces_per_instance > max_per_request or count * network_interfaces_per_instance > ( + max_per_request + ): + raise ClientError( + {"Error": {"Code": "AttachmentLimitExceeded", "Message": "exceeds the maximum allowed limit"}}, + "RunInstances", + ) + first_id = sum(requested_counts[:-1]) + return { + "Instances": [ + { + "InstanceId": f"i-{first_id + index}", + "PrivateIpAddress": f"ip.{first_id + index}", + "PrivateDnsName": f"ip-{first_id + index}", + "NetworkInterfaces": [], + "LaunchTime": datetime(2020, 1, 1, tzinfo=timezone.utc), + } + for index in range(count) + ] + } + + mocker.patch("slurm_plugin.fleet_manager.run_instances", side_effect=_run_instances) + return requested_counts + + @pytest.mark.parametrize("scaling_strategy", [ScalingStrategy.BEST_EFFORT, ScalingStrategy.ALL_OR_NOTHING]) + def test_launch_instances_splits_batch_over_interfaces_limit(self, mocker, instance_manager, scaling_strategy): + """A batch over the network interfaces per request limit is launched again in smaller batches.""" + requested_counts = self._mock_run_instances_with_network_interfaces(mocker, network_interfaces_per_instance=17) + node_list = [f"queue1-st-c5xlarge-{index}" for index in range(1, 601)] + + instances_launched = instance_manager._launch_instances( + job=None, + nodes_to_launch={"queue1": {"c5xlarge": node_list}}, + launch_batch_size=500, + scaling_strategy=scaling_strategy, + ) + + # 500 x 17 = 8500 fails; the batch and the remaining 100 nodes are relaunched in batches of 250 (4250) + assert_that(requested_counts).is_equal_to([500, 250, 250, 100]) + assert_that(instances_launched["queue1"]["c5xlarge"]).is_length(600) + assert_that(instance_manager.failed_nodes).is_empty() + + def test_launch_instances_stops_splitting_when_single_instance_exceeds_limit(self, mocker, instance_manager): + """An instance alone over the limit fails its nodes with the EC2 error code; small batches are not split.""" + requested_counts = self._mock_run_instances_with_network_interfaces( + mocker, network_interfaces_per_instance=17, max_per_request=16 + ) + node_list = [f"queue1-st-c5xlarge-{index}" for index in range(1, 501)] + + instance_manager._launch_instances( + job=None, + nodes_to_launch={"queue1": {"c5xlarge": node_list}}, + launch_batch_size=500, + scaling_strategy=ScalingStrategy.BEST_EFFORT, + ) + + # Batches of 50 nodes or fewer are not split: 4 splits down to 32, then the 500 nodes in batches of 32 + assert_that(requested_counts).is_equal_to([500, 250, 125, 63] + [32] * 15 + [20]) + assert_that(instance_manager.failed_nodes).is_equal_to({"AttachmentLimitExceeded": set(node_list)}) + @pytest.mark.parametrize( "job_list, launch_batch_size, assign_node_batch_size, update_node_address, " "expected_single_nodes_no_oversubscribe, scaling_strategy",