Skip to content

fix: do not limit resources (e.g. threads) before job submission in case of remote execution#3204

Closed
johanneskoester wants to merge 4 commits intomainfrom
fix/cores-cluster
Closed

fix: do not limit resources (e.g. threads) before job submission in case of remote execution#3204
johanneskoester wants to merge 4 commits intomainfrom
fix/cores-cluster

Conversation

@johanneskoester
Copy link
Copy Markdown
Contributor

@johanneskoester johanneskoester commented Nov 12, 2024

fixes #3191
fixes #2997

QC

  • The PR contains a test case for the changes or the changes are already covered by an existing test case.
  • The documentation (docs/) is updated to reflect the changes or this is not necessary (e.g. if the change does neither modify the language nor the behavior or functionalities of Snakemake).

Summary by CodeRabbit

  • New Features
    • Introduced a new rule in the workflow for creating an output file with specified thread allocation.
    • Added a shell script for logging and job execution simulation.
  • Bug Fixes
    • Enhanced resource handling logic to better respect local execution settings.
  • Tests
    • Added a new test to validate cluster executor behavior in the Snakemake workflow.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Nov 12, 2024

📝 Walkthrough
📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes in this pull request involve modifications to the Rule class's expand_resources method in snakemake/rules.py, enhancing the handling of resource constraints for local execution scenarios. A new rule is added in tests/test_cores_cluster/Snakefile, which allocates threads and creates an output file. Additionally, a new shell script qsub is introduced for job submission logging. A corresponding test function test_cores_cluster is added to tests/tests.py to validate the workflow's behavior with cluster execution settings.

Changes

File Change Summary
snakemake/rules.py Modified expand_resources method to improve handling of _cores resource for local execution.
tests/test_cores_cluster/Snakefile Added new rule a that creates test.txt with thread allocation.
tests/test_cores_cluster/qsub Introduced new shell script for logging and job execution.
tests/tests.py Added new test function test_cores_cluster to validate cluster executor behavior.

Assessment against linked issues

Objective Addressed Explanation
Locally detected cores determine remote max thread level (#3191)

Possibly related PRs


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (5)
tests/test_cores_cluster/qsub (3)

2-3: Add error handling for log file operations

The logging operations could fail silently if there are permission issues or if the log file becomes too large.

-echo `date` >> qsub.log
-tail -n1 $1 >> qsub.log
+LOG_FILE="qsub.log"
+if ! echo `date` >> "$LOG_FILE"; then
+    echo "Error: Failed to write to $LOG_FILE" >&2
+    exit 1
+fi
+if ! tail -n1 "$1" >> "$LOG_FILE"; then
+    echo "Error: Failed to append input file content to $LOG_FILE" >&2
+    exit 1
+fi

6-6: Consider safer script execution method

Direct execution of input files could be a security risk. Since this is a test script, it's acceptable, but consider adding a comment explaining the security implications.

-sh $1
+# Note: Direct script execution is acceptable in test environment
+# but should be avoided in production for security reasons
+sh "$1"

4-5: Document the random job ID simulation

The random job ID generation should be documented to explain its purpose in the test environment.

-# simulate printing of job id by a random number
-echo $RANDOM
+# Simulate cluster job ID generation (1-32767) for testing purposes
+# Real cluster systems would return their own job ID format
+echo $RANDOM
tests/test_cores_cluster/Snakefile (1)

5-6: Consider making the shell command more robust and explicit.

While the current command works, it could be more explicit about the test's purpose and provide better error reporting.

Consider this improved version:

-    shell:
-        "if [ {threads} -gt 1 ]; then touch {output}; else exit 1; fi"
+    shell:
+        """
+        if [ {threads} -eq 32 ]; then
+            echo "Success: Received expected 32 threads"
+            touch {output}
+        else
+            echo "Error: Expected 32 threads, but got {threads}" >&2
+            exit 1
+        fi
+        """

This version:

  1. Explicitly checks for exactly 32 threads
  2. Provides clear success/failure messages
  3. Properly redirects error message to stderr
snakemake/rules.py (1)

1052-1055: LGTM! The fix correctly handles resource limitations for remote execution.

The changes ensure that resource limits are only applied for local execution or resources explicitly marked as local, which fixes the issue of remote jobs being throttled by local core counts.

Consider adding a comment to explain the rationale:

 if isinstance(res, int) and (
+    # Only apply resource limits for local execution or explicitly local resources
     self.workflow.is_local(self)
     or name in self.workflow.resource_scopes.locals
 ):
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 76d5329 and 449cfb0.

📒 Files selected for processing (4)
  • snakemake/rules.py (1 hunks)
  • tests/test_cores_cluster/Snakefile (1 hunks)
  • tests/test_cores_cluster/qsub (1 hunks)
  • tests/tests.py (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
snakemake/rules.py (1)

Pattern **/*.py: Do not try to improve formatting.
Do not suggest type annotations for functions that are defined inside of functions or methods.
Do not suggest type annotation of the self argument of methods.
Do not suggest type annotation of the cls argument of classmethods.
Do not suggest return type annotation if a function or method does not contain a return statement.

tests/tests.py (1)

Pattern **/*.py: Do not try to improve formatting.
Do not suggest type annotations for functions that are defined inside of functions or methods.
Do not suggest type annotation of the self argument of methods.
Do not suggest type annotation of the cls argument of classmethods.
Do not suggest return type annotation if a function or method does not contain a return statement.

🔇 Additional comments (5)
tests/test_cores_cluster/qsub (2)

1-6: Verify test suite integration

Let's verify how this script integrates with the test workflow and cluster submission.

#!/bin/bash
# Check for related test files and their configuration
echo "Checking related test files:"
echo "1. Snakefile configuration:"
find tests/test_cores_cluster -name "Snakefile" -exec cat {} \;
echo "2. Test implementation:"
rg -A 5 "test_cores_cluster" tests/tests.py

1-1: Ensure script has execute permissions

The script needs execute permissions to function in the test environment.

✅ Verification successful

Script has execute permissions

The tests/test_cores_cluster/qsub script has execute permissions set correctly.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if the script has execute permissions
ls -l tests/test_cores_cluster/qsub

Length of output: 636


Script:

#!/bin/bash
# Check execute permissions of the qsub script
stat -c '%A' tests/test_cores_cluster/qsub

Length of output: 114


Script:

#!/bin/bash
# Check if the qsub script has execute permissions using find
find tests/test_cores_cluster/qsub -perm /u=x,g=x,o=x -ls

Length of output: 161

tests/test_cores_cluster/Snakefile (1)

1-6: LGTM! Well-designed test case for validating thread allocation.

The test case effectively validates the PR's objective by:

  1. Requesting 32 threads (likely exceeding local cores)
  2. Failing if thread count is incorrectly limited
  3. Providing a clear pass/fail outcome
snakemake/rules.py (1)

1052-1055: Verify test coverage for resource handling in remote execution.

Let's ensure that the test case mentioned in the PR objectives is properly implemented.

tests/tests.py (1)

930-936: LGTM! Test case appropriately validates the fix.

The test case correctly validates that remote job execution is not limited by locally detected cores by:

  1. Using the cluster-generic executor
  2. Providing a custom job submission command
  3. Limiting concurrent jobs to 1 to ensure proper validation

@sonarqubecloud
Copy link
Copy Markdown

@cmeesters
Copy link
Copy Markdown
Member

@johanneskoester there seem to be a few issues to be fixed. Just merged main, so that this is at least done, before fixing the little things. I wonder: Is it related to #2896, too?

@cmeesters
Copy link
Copy Markdown
Member

@johanneskoester & @fgvieira just screening the hackathon project space and I wonder: does this issue still persist?

@fgvieira
Copy link
Copy Markdown
Contributor

fgvieira commented Jun 20, 2025

As far as I can see, I think this issue is fixed.

@fgvieira fgvieira closed this Sep 25, 2025
@github-project-automation github-project-automation bot moved this from In progress to Done in Snakemake Hackathon March 2025 Sep 25, 2025
jonasfreimuth pushed a commit to TRON-Bioinformatics/snakemake that referenced this pull request Mar 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

3 participants