Skip to content

Conversation

@fatih-acar
Copy link
Contributor

@fatih-acar fatih-acar commented Nov 6, 2025

Summary by CodeRabbit

  • Refactor
    • Simplified concurrent execution control in batch processing with streamlined parameters.
    • Unified async and sync batch construction mechanisms for improved consistency and maintainability.
    • Enhanced internal batch handling architecture across client operations.

@coderabbitai
Copy link

coderabbitai bot commented Nov 6, 2025

Walkthrough

The changes refactor concurrent execution control in batch operations across the SDK. The initialization of asyncio.Semaphore was removed from the async client. Both async and sync batch creation methods in the client now instantiate batch objects with a max_concurrent_execution parameter instead of passing a semaphore. The relationship manager classes were updated to obtain batch objects through the client's create_batch method rather than instantiating them directly, and the direct import of InfrahubBatch was removed.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly relates to the main changes: fixing a hang issue with batches by refactoring batch construction from using semaphores to max_concurrent_execution parameter.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fac-fix-create-batch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Nov 6, 2025

Deploying infrahub-sdk-python with  Cloudflare Pages  Cloudflare Pages

Latest commit: dd4cb6e
Status: ✅  Deploy successful!
Preview URL: https://5923ec79.infrahub-sdk-python.pages.dev
Branch Preview URL: https://fac-fix-create-batch.infrahub-sdk-python.pages.dev

View logs

It would make clients hang when using batches within batches.

Signed-off-by: Fatih Acar <fatih@opsmill.com>
@fatih-acar fatih-acar force-pushed the fac-fix-create-batch branch from 8f0af3b to dd4cb6e Compare November 6, 2025 10:11
@codecov
Copy link

codecov bot commented Nov 6, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

@@            Coverage Diff             @@
##           stable     #601      +/-   ##
==========================================
- Coverage   75.80%   75.79%   -0.01%     
==========================================
  Files         100      100              
  Lines        8918     8916       -2     
  Branches     1758     1758              
==========================================
- Hits         6760     6758       -2     
  Misses       1677     1677              
  Partials      481      481              
Flag Coverage Δ
integration-tests 34.67% <100.00%> (-0.01%) ⬇️
python-3.10 48.34% <100.00%> (-0.03%) ⬇️
python-3.11 48.36% <100.00%> (-0.01%) ⬇️
python-3.12 48.31% <100.00%> (-0.01%) ⬇️
python-3.13 48.34% <100.00%> (-0.01%) ⬇️
python-3.9 46.98% <100.00%> (+0.02%) ⬆️
python-filler-3.12 25.06% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
infrahub_sdk/client.py 67.34% <100.00%> (-0.04%) ⬇️
infrahub_sdk/node/relationship.py 73.41% <100.00%> (-0.16%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

@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: 0

🧹 Nitpick comments (1)
infrahub_sdk/client.py (1)

1517-1520: Add docstring to document batch behavior and concurrency semantics.

The synchronous version (InfrahubClientSync.create_batch, lines 2258-2266) includes a helpful docstring explaining thread pool execution and dependency constraints. The async version should have equivalent documentation explaining:

  • That each batch creates its own semaphore with max_concurrent_execution limit
  • Concurrency implications when multiple batches execute simultaneously
  • Any constraints around nesting batches or using batches with parallel operations

This maintains API consistency between async and sync clients per the dual client pattern.

Apply this diff to add documentation:

 async def create_batch(self, return_exceptions: bool = False) -> InfrahubBatch:
+    """Create a batch to execute multiple queries concurrently.
+
+    Each batch manages its own concurrency via a semaphore limited to max_concurrent_execution.
+    Multiple batches can run simultaneously, each respecting its own concurrency limit.
+    
+    Args:
+        return_exceptions: If True, exceptions from tasks are returned rather than raised.
+        
+    Returns:
+        InfrahubBatch: A batch object for scheduling and executing tasks concurrently.
+    """
     return InfrahubBatch(
         max_concurrent_execution=self.max_concurrent_execution, return_exceptions=return_exceptions
     )

Based on learnings

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0d9f66c and dd4cb6e.

📒 Files selected for processing (2)
  • infrahub_sdk/client.py (1 hunks)
  • infrahub_sdk/node/relationship.py (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.py

📄 CodeRabbit inference engine (CLAUDE.md)

When implementing Infrahub checks, subclass InfrahubCheck and override validate(data); do not implement or rely on a check() method

Files:

  • infrahub_sdk/node/relationship.py
  • infrahub_sdk/client.py
infrahub_sdk/client.py

📄 CodeRabbit inference engine (CLAUDE.md)

infrahub_sdk/client.py: Use HTTPX for transport with proxy support (single proxy or HTTP/HTTPS mounts)
Support authentication via API tokens or JWT with automatic refresh

Files:

  • infrahub_sdk/client.py
🧠 Learnings (1)
📚 Learning: 2025-08-24T13:35:12.998Z
Learnt from: CR
Repo: opsmill/infrahub-sdk-python PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-08-24T13:35:12.998Z
Learning: Maintain a dual client pattern: InfrahubClient (async) and InfrahubClientSync (sync) must provide identical interfaces

Applied to files:

  • infrahub_sdk/client.py
🧬 Code graph analysis (1)
infrahub_sdk/client.py (1)
infrahub_sdk/batch.py (1)
  • InfrahubBatch (55-89)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: unit-tests (3.12)
  • GitHub Check: unit-tests (3.13)
  • GitHub Check: integration-tests-latest-infrahub
  • GitHub Check: Cloudflare Pages
🔇 Additional comments (2)
infrahub_sdk/client.py (1)

2258-2266: LGTM!

The sync batch creation correctly instantiates InfrahubBatchSync with the max_concurrent_execution parameter, and the docstring provides helpful guidance about thread pool execution and dependency constraints.

infrahub_sdk/node/relationship.py (1)

168-178: Verify whether nested batching provides measurable benefit; consider making parallel=True optional based on relationship cardinality.

The concern raised is valid: parallel=True does create nested batches. The filters() implementation confirms this—when parallel=True, it internally creates its own batch (batch_process = await self.client.create_batch()) to parallelize pagination across result pages (see process_batch() function).

This results in:

  • Outer batch (lines 168–178): parallelizes filters calls across node kinds
  • Inner batches (from parallel=True): parallelizes pagination within each kind

While this works correctly with per-batch semaphores, the nested structure may create more concurrent tasks than beneficial for typical relationship cardinalities. The trade-off between pagination parallelism and concurrent task overhead is unclear without profiling.

Consider:

  1. Profiling to measure if inner batch parallelization provides meaningful improvement for relationship sizes in typical use
  2. If benefit is marginal, making parallel=True conditional (e.g., only when expected results exceed threshold)
  3. Adding inline comment explaining the nested batching design choice

@fatih-acar fatih-acar merged commit 8bce2a8 into stable Nov 7, 2025
20 checks passed
@fatih-acar fatih-acar deleted the fac-fix-create-batch branch November 7, 2025 14:49
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.

2 participants