Skip to content
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

[minor] Move create_executor() function to separate module #550

Merged
merged 5 commits into from
Feb 1, 2025
Merged

Conversation

jan-janssen
Copy link
Member

@jan-janssen jan-janssen commented Feb 1, 2025

Summary by CodeRabbit

  • New Features
    • Introduced an enhanced executor creation functionality that supports multiple execution backends with flexible resource configurations.
  • Refactor
    • Streamlined the executor instantiation process to improve maintainability and consolidate related logic.
  • Tests
    • Updated tests to align with the revised executor structure.

These updates enhance executor management while preserving existing workflows and overall behavior.

Copy link
Contributor

coderabbitai bot commented Feb 1, 2025

Warning

Rate limit exceeded

@jan-janssen has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 14 minutes and 32 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 1e9bf50 and ba992af.

📒 Files selected for processing (1)
  • executorlib/interactive/create.py (1 hunks)

Walkthrough

This pull request moves the source of the create_executor function from executorlib.interactive.executor to a new module, executorlib.interactive.create. The change updates the import paths in both the package initialization and tests. Additionally, the legacy implementation of create_executor in executorlib/interactive/executor.py has been removed, while its dependent class ExecutorWithDependencies remains unchanged. The new create_executor function supports multiple backends (e.g., "flux", "local", "slurm") with appropriate validations and spawner selections.

Changes

File(s) Change Summary
executorlib/__init__.py, tests/test_dependencies_executor.py Updated import of create_executor from executorlib.interactive.executor to executorlib.interactive.create.
executorlib/interactive/create.py New file added; introduces create_executor with support for multiple backends, resource validation, and spawner selection. Functions for specific executor types also added.
executorlib/interactive/executor.py Removed the create_executor function and related imports; retained ExecutorWithDependencies without changes.

Sequence Diagram(s)

sequenceDiagram
    participant U as User/Client
    participant E as Executor
    participant C as create_executor
    participant B as Backend Executor

    U->>E: Instantiate Executor
    E->>C: Call create_executor(backend, parameters)
    C->>B: Validate backend & resources
    B-->>C: Return appropriate executor instance
    C-->>E: Return executor instance
Loading

Poem

I'm a code rabbit, hopping in delight,
New executor paths now shining bright.
Imports realigned and new logic unfurled,
Multiple backends ready to conquer the world.
I nibble on changes with a twitch and a cheer,
Happy to see our code evolve each year!


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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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
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

🧹 Nitpick comments (5)
executorlib/interactive/create.py (5)

100-100: Use membership testing directly on the dict.

Use if "openmpi_oversubscribe" in resource_dict: instead of if "openmpi_oversubscribe" in resource_dict.keys():.

-if "openmpi_oversubscribe" in resource_dict.keys():
+if "openmpi_oversubscribe" in resource_dict:
🧰 Tools
🪛 Ruff (0.8.2)

100-100: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


102-102: Use membership testing directly on the dict.

Use if "slurm_cmd_args" in resource_dict: instead of if "slurm_cmd_args" in resource_dict.keys():.

-if "slurm_cmd_args" in resource_dict.keys():
+if "slurm_cmd_args" in resource_dict:
🧰 Tools
🪛 Ruff (0.8.2)

102-102: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


170-170: Use membership testing directly on the dict.

Use if "threads_per_core" in resource_dict: instead of if "threads_per_core" in resource_dict.keys():.

-if "threads_per_core" in resource_dict.keys():
+if "threads_per_core" in resource_dict:
🧰 Tools
🪛 Ruff (0.8.2)

170-170: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


172-172: Use membership testing directly on the dict.

Use if "gpus_per_core" in resource_dict: instead of if "gpus_per_core" in resource_dict.keys():.

-if "gpus_per_core" in resource_dict.keys():
+if "gpus_per_core" in resource_dict:
🧰 Tools
🪛 Ruff (0.8.2)

172-172: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


174-174: Use membership testing directly on the dict.

Use if "slurm_cmd_args" in resource_dict: instead of if "slurm_cmd_args" in resource_dict.keys():.

-if "slurm_cmd_args" in resource_dict.keys():
+if "slurm_cmd_args" in resource_dict:
🧰 Tools
🪛 Ruff (0.8.2)

174-174: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7f9448a and 660b811.

📒 Files selected for processing (4)
  • executorlib/__init__.py (1 hunks)
  • executorlib/interactive/create.py (1 hunks)
  • executorlib/interactive/executor.py (1 hunks)
  • tests/test_dependencies_executor.py (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • tests/test_dependencies_executor.py
🧰 Additional context used
🪛 Ruff (0.8.2)
executorlib/interactive/create.py

38-38: Do not use mutable data structures for argument defaults

Replace with None; initialize within function

(B006)


100-100: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


102-102: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


170-170: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


172-172: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


174-174: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)

⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: build (macos-latest, 3.13)
  • GitHub Check: build (macos-latest, 3.13)
  • GitHub Check: build
  • GitHub Check: build
  • GitHub Check: build (windows-latest, 3.13)
  • GitHub Check: build
🔇 Additional comments (5)
executorlib/interactive/executor.py (1)

5-6: Congrats on the modular import.

The new import from executorlib.interactive.create properly references the relocated function. No issues found.

executorlib/interactive/create.py (3)

3-31: Imports and optional dependency management look good.

The try-except block for Flux ensures graceful handling of optional dependencies. No issues found.


47-85: Comprehensive docstring.

The docstring clearly outlines usage, parameters, and resource requirements. Nicely done.


93-99: Well-structured branching logic for different backends.

The code effectively handles Flux, SLURM, and local scenarios with consistent validations. The flow is straightforward and readable.

Also applies to: 101-101, 103-169, 171-171, 173-173, 175-195

executorlib/__init__.py (1)

4-4: Correct relocation of the import statement.

The import reference to _create_executor from executorlib.interactive.create now correctly reflects the new file structure.

Comment on lines 33 to 46
def create_executor(
max_workers: Optional[int] = None,
backend: str = "local",
max_cores: Optional[int] = None,
cache_directory: Optional[str] = None,
resource_dict: dict = {},
flux_executor=None,
flux_executor_pmi_mode: Optional[str] = None,
flux_executor_nesting: bool = False,
flux_log_files: bool = False,
hostname_localhost: Optional[bool] = None,
block_allocation: bool = False,
init_function: Optional[Callable] = None,
):
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Avoid mutable default argument for resource_dict.

It's safer to use a default value of None instead of {} to avoid unintended side effects from mutable default arguments.

Here's a suggested fix:

 def create_executor(
     max_workers: Optional[int] = None,
     backend: str = "local",
     max_cores: Optional[int] = None,
     cache_directory: Optional[str] = None,
-    resource_dict: dict = {},
+    resource_dict: Optional[dict] = None,
     flux_executor=None,
     ...
 ):
     if resource_dict is None:
         resource_dict = {}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def create_executor(
max_workers: Optional[int] = None,
backend: str = "local",
max_cores: Optional[int] = None,
cache_directory: Optional[str] = None,
resource_dict: dict = {},
flux_executor=None,
flux_executor_pmi_mode: Optional[str] = None,
flux_executor_nesting: bool = False,
flux_log_files: bool = False,
hostname_localhost: Optional[bool] = None,
block_allocation: bool = False,
init_function: Optional[Callable] = None,
):
def create_executor(
max_workers: Optional[int] = None,
backend: str = "local",
max_cores: Optional[int] = None,
cache_directory: Optional[str] = None,
resource_dict: Optional[dict] = None,
flux_executor=None,
flux_executor_pmi_mode: Optional[str] = None,
flux_executor_nesting: bool = False,
flux_log_files: bool = False,
hostname_localhost: Optional[bool] = None,
block_allocation: bool = False,
init_function: Optional[Callable] = None,
):
if resource_dict is None:
resource_dict = {}
# ... rest of the function implementation ...
🧰 Tools
🪛 Ruff (0.8.2)

38-38: Do not use mutable data structures for argument defaults

Replace with None; initialize within function

(B006)

Copy link
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: 2

🧹 Nitpick comments (1)
executorlib/interactive/create.py (1)

158-161: Simplify dictionary key checks.

Using .keys() is unnecessary when checking for key existence.

Apply this diff to simplify the checks:

-    if "openmpi_oversubscribe" in resource_dict.keys():
+    if "openmpi_oversubscribe" in resource_dict:
         del resource_dict["openmpi_oversubscribe"]
-    if "slurm_cmd_args" in resource_dict.keys():
+    if "slurm_cmd_args" in resource_dict:
         del resource_dict["slurm_cmd_args"]

Apply similar changes to all other .keys() usage.

Also applies to: 236-241

🧰 Tools
🪛 Ruff (0.8.2)

158-158: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


160-160: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 660b811 and 1e9bf50.

📒 Files selected for processing (1)
  • executorlib/interactive/create.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
executorlib/interactive/create.py

38-38: Do not use mutable data structures for argument defaults

Replace with None; initialize within function

(B006)


150-150: Do not use mutable data structures for argument defaults

Replace with None; initialize within function

(B006)


158-158: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


160-160: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


197-197: Do not use mutable data structures for argument defaults

Replace with None; initialize within function

(B006)


232-232: Do not use mutable data structures for argument defaults

Replace with None; initialize within function

(B006)


236-236: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


238-238: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)


240-240: Use key in dict instead of key in dict.keys()

Remove .keys()

(SIM118)

🪛 GitHub Actions: MyPy
executorlib/interactive/create.py

[error] 176-176: Argument "cores" to "validate_max_workers" has incompatible type "int | None"; expected "int"


[error] 211-211: Argument "cores" to "validate_max_workers" has incompatible type "int | None"; expected "int"

⏰ Context from checks skipped due to timeout of 90000ms (14)
  • GitHub Check: build (ubuntu-latest, 3.10)
  • GitHub Check: build (ubuntu-latest, 3.11)
  • GitHub Check: build (ubuntu-latest, 3.12)
  • GitHub Check: build (ubuntu-latest, 3.12)
  • GitHub Check: build (ubuntu-latest, 3.13)
  • GitHub Check: build (ubuntu-latest, 3.13)
  • GitHub Check: build (macos-latest, 3.13)
  • GitHub Check: build (windows-latest, 3.13)
  • GitHub Check: build
  • GitHub Check: build (macos-latest, 3.13)
  • GitHub Check: build (ubuntu-latest, 3.13, .ci_support/environment-mpich.yml)
  • GitHub Check: build (ubuntu-latest, 3.13, .ci_support/environment-openmpi.yml)
  • GitHub Check: build
  • GitHub Check: build
🔇 Additional comments (1)
executorlib/interactive/create.py (1)

38-38: Fix mutable default argument.

Using mutable default arguments can lead to unexpected behavior.

🧰 Tools
🪛 Ruff (0.8.2)

38-38: Do not use mutable data structures for argument defaults

Replace with None; initialize within function

(B006)

Comment on lines +174 to +178
validate_max_workers_flux(
max_workers=max_workers,
cores=cores_per_worker,
threads_per_core=resource_dict.get("threads_per_core", 1),
)
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix type incompatibility in validate_max_workers calls.

The cores parameter in validate_max_workers calls expects a non-optional int, but cores_per_worker is Optional[int].

Add validation before the calls:

+        if cores_per_worker is None:
+            cores_per_worker = 1
         validate_max_workers_flux(
             max_workers=max_workers,
             cores=cores_per_worker,
             threads_per_core=resource_dict.get("threads_per_core", 1),
         )

Apply the same fix for validate_max_workers_slurm.

Also applies to: 209-213

🧰 Tools
🪛 GitHub Actions: MyPy

[error] 176-176: Argument "cores" to "validate_max_workers" has incompatible type "int | None"; expected "int"

Comment on lines +141 to +143
raise ValueError(
"The supported backends are slurm_allocation, slurm_submission, flux_allocation, flux_submission and local."
)
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Update error message to match supported backends.

The error message lists backends that don't match the actual supported values in the code. The message mentions "slurm_submission" and "flux_submission" which aren't handled in the code.

Apply this diff to fix the error message:

-            "The supported backends are slurm_allocation, slurm_submission, flux_allocation, flux_submission and local."
+            "The supported backends are slurm_allocation, flux_allocation and local."
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
raise ValueError(
"The supported backends are slurm_allocation, slurm_submission, flux_allocation, flux_submission and local."
)
raise ValueError(
"The supported backends are slurm_allocation, flux_allocation and local."
)

@jan-janssen jan-janssen merged commit 40c35e3 into main Feb 1, 2025
28 checks passed
@jan-janssen jan-janssen deleted the create branch February 1, 2025 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant