Skip to content

Commit

Permalink
Add logic to list instance-types matching Requirement set
Browse files Browse the repository at this point in the history
A set of InstanceRequirements may return a long list
of instance-types (or none) depening on the attributes,
the region and the architecture.

Signed-off-by: Nicola Sirena <[email protected]>
  • Loading branch information
NSsirena committed Jun 21, 2023
1 parent fc97ed6 commit 2a45dac
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
18 changes: 18 additions & 0 deletions cli/src/pcluster/aws/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,3 +487,21 @@ def run_instances(self, **kwargs):
except ClientError as e:
if e.response.get("Error").get("Code") != "DryRunOperation":
raise

@AWSExceptionHandler.handle_client_exception
@Cache.cached
def get_instance_types_from_instance_requirements(
self, instance_requirements: str, architecture: str = "x86_64"
) -> List[str]:
"""Get list of instance types matching a set of instance_requirements."""
config = {
"ArchitectureTypes": [architecture],
"VirtualizationTypes": ["hvm"],
"InstanceRequirements": instance_requirements,
}

response = self._client.get_instance_types_from_instance_requirements(config)
if "InstanceTypes" in response:
return [res["InstanceType"] for res in response["InstanceTypes"]]
else:
return []
55 changes: 52 additions & 3 deletions cli/src/pcluster/config/cluster_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2207,6 +2207,46 @@ def __init__(
self.accelerator_types = None
self.accelerator_manufacturers = None

def _vcpu_config(self):
config = {
"Min": self.min_vcpus,
}
if self.max_vcpus > 0:
config["Max"] = self.max_vcpus

return config

def _mem_config(self):
config = {
"Min": self.min_memory_mib,
}
if self.max_memory_mib > 0:
config["Max"] = self.max_memory_mib

return config

def config(self):
"""Compiles an InstanceRequirement config to retrieve the list of matching instance-types."""
config = {
"VCpuCount": self._vcpu_config(),
"MemoryMiB": self._mem_config(),
"InstanceGenerations": self.instance_generations,
"BareMetal": self.bare_metal,
"MaxPricePercentageOverLowestPrice": self.bare_metal,
}

if self.accelerator_count > 0:
config["AcceleratorCount"] = self.accelerator_count
config["AcceleratorTypes"] = self.accelerator_types
config["AcceleratorManufacturers"] = self.max_price_percentage

if self.allowed_instance_types:
config["AllowedInstanceTypes"] = self.allowed_instance_types
elif self.excluded_instance_types:
config["AllowedInstanceTypes"] = self.allowed_instance_types

return config


class InstanceRequirementsComputeResource(_BaseSlurmComputeResource):
"""Represents a Slurm Compute Resource defined through Instance Requirements."""
Expand All @@ -2218,7 +2258,7 @@ def __init__(
):
super().__init__(**kwargs)
self.instance_requirements = Resource.init_param(instance_requirements)
self.instance_type_list = []
self.instance_type_list = None

@property
def disable_simultaneous_multithreading_manually(self) -> bool:
Expand All @@ -2237,11 +2277,20 @@ def max_network_interface_count(self) -> int:
"""
return 1

def get_matching_instance_type_list(self, architecture):
"""Return the list of instance types matching the Requirements for a given architecture."""
# TODO add a mechanism to discover the architecture at ComputeResource level
# it should get the HeadNode architecture that wins over the CR config
# Currently we receive if from the outside (Validator) and delegate the burden to it
if self.instance_type_list is None:
self.instance_type_list = AWSApi.instance().ec2.get_instance_types_from_instance_requirements(
self.instance_requirements.config(), architecture
)
return self.instance_type_list

@property
def instance_types(self) -> List[str]:
"""Should Return list of instance type names in this compute resource."""
# TODO (singleton) retrieve once the list of instance-types derived from the requirements with
# get-instance-types-from-instance-requirements and fill the list
return self.instance_type_list


Expand Down

0 comments on commit 2a45dac

Please sign in to comment.