-
Notifications
You must be signed in to change notification settings - Fork 12
fix: use threads directive for vCPU and platform-aware resource validation #38
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
Open
nh13
wants to merge
4
commits into
snakemake:main
Choose a base branch
from
nh13:fix-vcpu-threads-and-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
404b0d1
fix: issue with resource validation assuming Fargate #34
radusuciu 0bbd741
fix: use threads directive for vCPU allocation
nh13 92d7a34
fix: handle invalid vCPU for Fargate with proper error
nh13 6d75117
fix(deps): widen snakemake-storage-plugin-s3 to >=0.2,<0.4
nh13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -30,43 +30,141 @@ def __init__( | |||||||||
| self.job_command = job_command | ||||||||||
| self.batch_client = batch_client | ||||||||||
| self.created_job_defs = [] | ||||||||||
| # Determine platform from job queue | ||||||||||
| self.platform = self._get_platform_from_queue() | ||||||||||
|
|
||||||||||
| def _make_container_command(self, remote_command: str) -> List[str]: | ||||||||||
| """ | ||||||||||
| Return docker CMD form of the command | ||||||||||
| """ | ||||||||||
| return ["/bin/bash", "-c", remote_command] | ||||||||||
|
|
||||||||||
| def _validate_resources(self, vcpu: str, mem: str) -> tuple[str, str]: | ||||||||||
| """Validates vcpu and meme conform to Batch EC2 cpu/mem relationship | ||||||||||
| def _get_platform_from_queue(self) -> str: | ||||||||||
| """ | ||||||||||
| Determine the platform (EC2 or FARGATE) from the job queue's compute environments. | ||||||||||
|
|
||||||||||
| https://docs.aws.amazon.com/batch/latest/APIReference/API_ResourceRequirement.html | ||||||||||
| :return: Platform capability string (EC2 or FARGATE) | ||||||||||
| """ | ||||||||||
| vcpu = int(vcpu) | ||||||||||
| mem = int(mem) | ||||||||||
| try: | ||||||||||
| # Query the job queue | ||||||||||
| queue_response = self.batch_client.describe_job_queues( | ||||||||||
| jobQueues=[self.settings.job_queue] | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| if not queue_response.get("jobQueues"): | ||||||||||
| self.logger.warning( | ||||||||||
| f"Job queue {self.settings.job_queue} not found. Defaulting to EC2." | ||||||||||
| ) | ||||||||||
| return BATCH_JOB_PLATFORM_CAPABILITIES.EC2.value | ||||||||||
|
|
||||||||||
| job_queue = queue_response["jobQueues"][0] | ||||||||||
| compute_env_order = job_queue.get("computeEnvironmentOrder", []) | ||||||||||
|
|
||||||||||
| if not compute_env_order: | ||||||||||
| self.logger.warning( | ||||||||||
| f"No compute environments found for queue {self.settings.job_queue}. " | ||||||||||
| "Defaulting to EC2." | ||||||||||
| ) | ||||||||||
| return BATCH_JOB_PLATFORM_CAPABILITIES.EC2.value | ||||||||||
|
|
||||||||||
| # Get the first compute environment ARN | ||||||||||
| compute_env_arn = compute_env_order[0]["computeEnvironment"] | ||||||||||
|
|
||||||||||
| # Query the compute environment to get its type | ||||||||||
| env_response = self.batch_client.describe_compute_environments( | ||||||||||
| computeEnvironments=[compute_env_arn] | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| if not env_response.get("computeEnvironments"): | ||||||||||
| self.logger.warning( | ||||||||||
| f"Compute environment {compute_env_arn} not found. Defaulting to EC2." | ||||||||||
| ) | ||||||||||
| return BATCH_JOB_PLATFORM_CAPABILITIES.EC2.value | ||||||||||
|
|
||||||||||
| compute_env = env_response["computeEnvironments"][0] | ||||||||||
|
|
||||||||||
| # Check if it's a Fargate environment | ||||||||||
| # Fargate environments have computeResources.type == "FARGATE" or "FARGATE_SPOT" | ||||||||||
| compute_resources = compute_env.get("computeResources", {}) | ||||||||||
| resource_type = compute_resources.get("type", "") | ||||||||||
|
|
||||||||||
| if resource_type in ["FARGATE", "FARGATE_SPOT"]: | ||||||||||
| self.logger.info( | ||||||||||
| f"Detected FARGATE platform from queue {self.settings.job_queue}" | ||||||||||
| ) | ||||||||||
| return BATCH_JOB_PLATFORM_CAPABILITIES.FARGATE.value | ||||||||||
| else: | ||||||||||
| self.logger.info( | ||||||||||
| f"Detected EC2 platform from queue {self.settings.job_queue}" | ||||||||||
| ) | ||||||||||
| return BATCH_JOB_PLATFORM_CAPABILITIES.EC2.value | ||||||||||
|
|
||||||||||
| except Exception as e: | ||||||||||
| self.logger.warning( | ||||||||||
| f"Failed to determine platform from queue: {e}. Defaulting to EC2." | ||||||||||
| ) | ||||||||||
| return BATCH_JOB_PLATFORM_CAPABILITIES.EC2.value | ||||||||||
|
|
||||||||||
| def _validate_fargate_resources(self, vcpu: int, mem: int) -> tuple[str, str]: | ||||||||||
| """Validates vcpu and memory conform to Fargate requirements. | ||||||||||
|
|
||||||||||
| Fargate requires strict memory/vCPU combinations. | ||||||||||
| https://docs.aws.amazon.com/batch/latest/userguide/fargate.html | ||||||||||
| """ | ||||||||||
| if mem in VALID_RESOURCES_MAPPING: | ||||||||||
| if vcpu in VALID_RESOURCES_MAPPING[mem]: | ||||||||||
| return str(vcpu), str(mem) | ||||||||||
| else: | ||||||||||
| raise WorkflowError(f"Invalid vCPU value {vcpu} for memory {mem} MB") | ||||||||||
| raise WorkflowError(f"Invalid vCPU value {vcpu} for memory {mem} MB on Fargate") | ||||||||||
| else: | ||||||||||
| min_mem = min([m for m, v in VALID_RESOURCES_MAPPING.items() if vcpu in v]) | ||||||||||
| valid_mems = [m for m, v in VALID_RESOURCES_MAPPING.items() if vcpu in v] | ||||||||||
| if not valid_mems: | ||||||||||
| raise WorkflowError( | ||||||||||
| f"Invalid vCPU value {vcpu} for Fargate. " | ||||||||||
| f"Check valid Fargate resource configurations." | ||||||||||
| ) | ||||||||||
| min_mem = min(valid_mems) | ||||||||||
| self.logger.warning( | ||||||||||
| f"Memory value {mem} MB is invalid for vCPU {vcpu}." | ||||||||||
| f"Memory value {mem} MB is invalid for vCPU {vcpu} on Fargate. " | ||||||||||
| f"Setting memory to minimum allowed value {min_mem} MB." | ||||||||||
| ) | ||||||||||
| return str(vcpu), str(min_mem) | ||||||||||
|
|
||||||||||
| def _validate_ec2_resources(self, vcpu: int, mem: int) -> tuple[str, str]: | ||||||||||
| """Validates vcpu and memory for EC2 compute environments. | ||||||||||
|
|
||||||||||
| EC2 allows flexible resource allocation - just basic sanity checks. | ||||||||||
| https://docs.aws.amazon.com/batch/latest/userguide/compute_environment_parameters.html | ||||||||||
| """ | ||||||||||
| if vcpu < 1: | ||||||||||
| raise WorkflowError(f"vCPU must be at least 1, got {vcpu}") | ||||||||||
| if mem < 1024: | ||||||||||
| raise WorkflowError(f"Memory must be at least 1024 MiB, got {mem} MiB") | ||||||||||
|
Comment on lines
+141
to
+142
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
If no |
||||||||||
| return str(vcpu), str(mem) | ||||||||||
|
|
||||||||||
| def _validate_resources(self, vcpu: str, mem: str) -> tuple[str, str]: | ||||||||||
| """Validates vcpu and memory based on platform requirements. | ||||||||||
|
|
||||||||||
| https://docs.aws.amazon.com/batch/latest/APIReference/API_ResourceRequirement.html | ||||||||||
| """ | ||||||||||
| vcpu_int = int(vcpu) | ||||||||||
| mem_int = int(mem) | ||||||||||
|
|
||||||||||
| if self.platform == BATCH_JOB_PLATFORM_CAPABILITIES.FARGATE.value: | ||||||||||
| return self._validate_fargate_resources(vcpu_int, mem_int) | ||||||||||
| else: | ||||||||||
| return self._validate_ec2_resources(vcpu_int, mem_int) | ||||||||||
|
|
||||||||||
| def build_job_definition(self): | ||||||||||
| job_uuid = str(uuid.uuid4()) | ||||||||||
| job_name = f"snakejob-{self.job.name}-{job_uuid}" | ||||||||||
| job_definition_name = f"snakejob-def-{self.job.name}-{job_uuid}" | ||||||||||
|
|
||||||||||
| # Validate and convert resources | ||||||||||
| gpu = max(0, int(self.job.resources.get("_gpus", 0))) | ||||||||||
| vcpu = max(1, int(self.job.resources.get("_cores", 1))) # Default to 1 vCPU | ||||||||||
| mem = max(1, int(self.job.resources.get("mem_mb", 2048))) # Default to 2048 MiB | ||||||||||
| # Use threads directive, fall back to _cores for backward compatibility | ||||||||||
| vcpu = max(1, self.job.threads if self.job.threads > 0 else int(self.job.resources.get("_cores", 1))) | ||||||||||
| mem = max(1, int(self.job.resources.get("mem_mb", 1024))) # Default to 1024 MiB | ||||||||||
|
|
||||||||||
| vcpu_str, mem_str = self._validate_resources(str(vcpu), str(mem)) | ||||||||||
| gpu_str = str(gpu) | ||||||||||
|
|
@@ -111,7 +209,7 @@ def build_job_definition(self): | |||||||||
| containerProperties=container_properties, | ||||||||||
| timeout=timeout, | ||||||||||
| tags=tags, | ||||||||||
| platformCapabilities=[BATCH_JOB_PLATFORM_CAPABILITIES.EC2.value], | ||||||||||
| platformCapabilities=[self.platform], | ||||||||||
| ) | ||||||||||
| self.created_job_defs.append(job_def) | ||||||||||
| return job_def, job_name | ||||||||||
|
|
||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.