Skip to content

Conversation

ChristoGrab
Copy link
Collaborator

@ChristoGrab ChristoGrab commented Jan 17, 2025

What

Although users can define custom backoff logic in the manifest, the CompositeErrorHandler component does not currently access the backoff_strategies, meaning it always defaults to the default exponential behavior. By accessing the backoff_strategies in the child ErrorHandler components, we can handle user-specified strategies.

Resolves: airbytehq/airbyte#42928

Cautionary Note

My understanding of the ErrorHandler flow is that it is not currently configured to respect multiple backoff strategies for a single stream, and will always use the first non-None value as the strategy for both:

  1. matches in response_filters
  2. default retryable errors.

For example, using:

type: CompositeErrorHandler
error_handlers:
 - type: DefaultErrorHandler
   max_retries: 2
   backoff_strategies:
     - type: ConstantBackoffStrategy
       backoff_time_in_seconds: 10
   response_filters:
     - type: HttpResponseFilter
       action: RETRY
       http_codes:
         - 404
 - type: DefaultErrorHandler
   response_filters:
     - type: HttpResponseFilter
       action: RETRY
       http_codes:
         - 500
   max_retries: 3
   backoff_strategies:
     - type: ExponentialBackoffStrategy
       factor: 0.5

We would expect a 500 level response to utilize the Exponential strategy. However, the output seen is in fact:

Retrying. Sleeping for 10.0 seconds (rate_limiting.py:114)
Retrying. Sleeping for 10.0 seconds (rate_limiting.py:114)

This is a function of the errorhandler logic flow and is a known behavior, distinct from the bug this PR tries to address.

Summary by CodeRabbit

  • New Features

    • Enhanced error handling with improved backoff strategy management in the Composite Error Handler.
    • Added ability to aggregate and prioritize backoff strategies across multiple error handlers.
  • Tests

    • Added comprehensive test coverage for backoff strategy implementation.
    • Verified correct behavior of backoff strategies in HTTP request retry scenarios.

@github-actions github-actions bot added bug Something isn't working security labels Jan 17, 2025
Copy link
Contributor

@maxi297 maxi297 left a comment

Choose a reason for hiding this comment

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

Can we link the GitHub issue this change is related to?

@ChristoGrab ChristoGrab self-assigned this Jan 17, 2025
@ChristoGrab ChristoGrab marked this pull request as ready for review January 18, 2025 00:25
Copy link
Contributor

coderabbitai bot commented Jan 18, 2025

📝 Walkthrough

Walkthrough

The pull request introduces an enhancement to the CompositeErrorHandler class in the Airbyte CDK by adding a new backoff_strategies property method. This method aggregates backoff strategies from multiple child error handlers, allowing for a more flexible approach to handling retryable errors. The changes include the implementation of this method in the main class and the addition of corresponding unit tests to validate its behavior across various scenarios.

Changes

File Change Summary
airbyte_cdk/sources/declarative/requesters/error_handlers/composite_error_handler.py Added backoff_strategies property method to aggregate strategies from child error handlers
unit_tests/sources/declarative/requesters/error_handlers/test_composite_error_handler.py Added tests for backoff_strategies method, including scenarios with multiple handlers and strategy prioritization
unit_tests/sources/declarative/requesters/test_http_requester.py Added test to verify backoff strategy configuration from manifest

Assessment against linked issues

Objective Addressed Explanation
Configure Custom Backoff Strategy [#42928]
Support Multiple Error Handlers with Backoff Strategies

The changes directly address the issue of configuring error handlers for backoff strategies, providing more flexibility in handling retryable errors. The implementation allows for custom backoff strategies to be properly configured and used, which was not working in previous versions.

Wdyt about the clarity of the new tests? They seem to cover a range of scenarios effectively! 😊

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

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

🧹 Nitpick comments (2)
airbyte_cdk/sources/declarative/requesters/error_handlers/composite_error_handler.py (1)

82-96: LGTM! The backoff_strategies implementation looks solid.

The implementation correctly aggregates strategies from all handlers and handles edge cases well. The docstring is particularly helpful in explaining the current behavior and limitations.

Just a thought - would it make sense to add a debug log when no strategies are found, to help with troubleshooting? wdyt?

unit_tests/sources/declarative/requesters/test_http_requester.py (1)

908-936: Integration test looks good! Addresses the previous review feedback.

The test effectively validates that backoff strategies from the manifest are respected during HTTP requests. Nice job following up on the previous review comment about manifest integration!

One suggestion - would it be helpful to also verify the actual backoff time used? For example, checking that the 0.1s constant backoff was applied? wdyt?

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 34a978d and 168529d.

📒 Files selected for processing (3)
  • airbyte_cdk/sources/declarative/requesters/error_handlers/composite_error_handler.py (2 hunks)
  • unit_tests/sources/declarative/requesters/error_handlers/test_composite_error_handler.py (2 hunks)
  • unit_tests/sources/declarative/requesters/test_http_requester.py (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: Check: 'source-pokeapi' (skip=false)
  • GitHub Check: Check: 'source-the-guardian-api' (skip=false)
  • GitHub Check: Check: 'source-shopify' (skip=false)
  • GitHub Check: Check: 'source-hardcoded-records' (skip=false)
  • GitHub Check: Pytest (All, Python 3.11, Ubuntu)
  • GitHub Check: Pytest (All, Python 3.10, Ubuntu)
  • GitHub Check: Pytest (Fast)
🔇 Additional comments (2)
unit_tests/sources/declarative/requesters/error_handlers/test_composite_error_handler.py (2)

280-314: Great test coverage! The parameterized tests look comprehensive.

The test cases effectively cover all important scenarios including empty, single, and multiple handler configurations.


317-351: The strategy prioritization test is well thought out.

Good job testing that the first handler's strategy is respected even when the second handler's response filter is triggered. This is crucial for validating the behavior described in the docstring.

Copy link
Contributor

@maxi297 maxi297 left a comment

Choose a reason for hiding this comment

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

LGTM! Can we create an issue to improve on backoff strategies and fix the issue when there are multiple error handlers in the composite error handler?

@ChristoGrab
Copy link
Collaborator Author

Created the issue to improve the handling of backoff strategies, linking here for additional visibility: https://github.com/airbytehq/airbyte-internal-issues/issues/11449

@ChristoGrab ChristoGrab enabled auto-merge (squash) January 22, 2025 20:44
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: 0

🧹 Nitpick comments (2)
airbyte_cdk/sources/declarative/requesters/error_handlers/composite_error_handler.py (2)

82-101: Great implementation! Consider adding a warning for multiple strategies?

The implementation looks solid and the docstring is very informative about the "first strategy wins" behavior. Since subsequent strategies are ignored, what do you think about adding a warning when multiple strategies are present? This could help users understand why their additional strategies aren't being used. wdyt?

Here's a potential enhancement:

     @property
     def backoff_strategies(self) -> Optional[List[BackoffStrategy]]:
         all_strategies = []
         for handler in self.error_handlers:
             if hasattr(handler, "backoff_strategies") and handler.backoff_strategies:
                 all_strategies.extend(handler.backoff_strategies)
+        if len(all_strategies) > 1:
+            import warnings
+            warnings.warn(
+                "Multiple backoff strategies detected. Only the first strategy will be used, subsequent strategies will be ignored.",
+                UserWarning
+            )
         return all_strategies if all_strategies else None

84-96: Enhance return type documentation?

The docstring is excellent in explaining the behavior! Would you consider adding a Returns section that explicitly documents the return type and its structure? Something like:

         """
         Combines backoff strategies from all child error handlers into a single flattened list.

         When used with HttpRequester, note the following behavior:
         - In HttpRequester.__post_init__, the entire list of backoff strategies is assigned to the error handler
         - However, the error handler's backoff_time() method only ever uses the first non-None strategy in the list
         - This means that if any backoff strategies are present, the first non-None strategy becomes the default
         - This applies to both user-defined response filters and errors from DEFAULT_ERROR_MAPPING
         - The list structure is not used to map different strategies to different error conditions
         - Therefore, subsequent strategies in the list will not be used

         Returns None if no handlers have strategies defined, which will result in HttpRequester using its default backoff strategy.
+
+        Returns:
+            Optional[List[BackoffStrategy]]: A list of backoff strategies from all child handlers,
+                                           or None if no strategies are defined.
         """
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 168529d and a55b33d.

📒 Files selected for processing (1)
  • airbyte_cdk/sources/declarative/requesters/error_handlers/composite_error_handler.py (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Pytest (Fast)
  • GitHub Check: Pytest (All, Python 3.11, Ubuntu)
  • GitHub Check: Pytest (All, Python 3.10, Ubuntu)
  • GitHub Check: Analyze (python)
🔇 Additional comments (1)
airbyte_cdk/sources/declarative/requesters/error_handlers/composite_error_handler.py (1)

11-11: LGTM! Clean import addition.

The BackoffStrategy import is well-placed and properly scoped for the new functionality.

@ChristoGrab ChristoGrab merged commit ee49219 into main Jan 22, 2025
16 of 20 checks passed
@ChristoGrab ChristoGrab deleted the christo/composite-error-handler-fix branch January 22, 2025 21:13
rpopov pushed a commit to rpopov/airbyte-python-cdk that referenced this pull request Jan 23, 2025
* remotes/airbyte/main:
  fix(airbyte-cdk): Fix RequestOptionsProvider for PerPartitionWithGlobalCursor (airbytehq#254)
  feat(low-code): add profile assertion flow to oauth authenticator component (airbytehq#236)
  feat(Low-Code Concurrent CDK): Add ConcurrentPerPartitionCursor (airbytehq#111)
  fix: don't mypy unit_tests (airbytehq#241)
  fix: handle backoff_strategies in CompositeErrorHandler (airbytehq#225)
  feat(concurrent cursor): attempt at clamping datetime (airbytehq#234)
  ci: use `ubuntu-24.04` explicitly (resolves CI warnings) (airbytehq#244)
  Fix(sdm): module ref issue in python components import (airbytehq#243)
  feat(source-declarative-manifest): add support for custom Python components from dynamic text input (airbytehq#174)
  chore(deps): bump avro from 1.11.3 to 1.12.0 (airbytehq#133)
  docs: comments on what the `Dockerfile` is for (airbytehq#240)
  chore: move ruff configuration to dedicated ruff.toml file (airbytehq#237)
rpopov added a commit to rpopov/airbyte-python-cdk that referenced this pull request Jan 26, 2025
Created a DPath Enhancing Extractor
Refactored the record enhancement logic - moved to the extracted class
Split the tests of DPathExtractor and DPathEnhancingExtractor

Fix the failing tests:

FAILED unit_tests/sources/declarative/parsers/test_model_to_component_factory.py::test_create_custom_components[test_create_custom_component_with_subcomponent_that_uses_parameters]
FAILED unit_tests/sources/declarative/parsers/test_model_to_component_factory.py::test_custom_components_do_not_contain_extra_fields
FAILED unit_tests/sources/declarative/parsers/test_model_to_component_factory.py::test_parse_custom_component_fields_if_subcomponent
FAILED unit_tests/sources/declarative/parsers/test_model_to_component_factory.py::test_create_page_increment
FAILED unit_tests/sources/declarative/parsers/test_model_to_component_factory.py::test_create_offset_increment
FAILED unit_tests/sources/file_based/test_file_based_scenarios.py::test_file_based_read[simple_unstructured_scenario]
FAILED unit_tests/sources/file_based/test_file_based_scenarios.py::test_file_based_read[no_file_extension_unstructured_scenario]

They faile because of comparing string and int values of the page_size (public) attribute.
Imposed an invariant:
  on construction, page_size can be set to a string or int
  keep only values of one type in page_size for uniform comparison (convert the values of the other type)
  _page_size holds the internal / working value
... unless manipulated directly.

Merged:
feat(low-code concurrent): Allow async job low-code streams that are incremental to be run by the concurrent framework (airbytehq#228)
fix(low-code): Fix declarative low-code state migration in SubstreamPartitionRouter (airbytehq#267)
feat: combine slash command jobs into single job steps (airbytehq#266)
feat(low-code): add items and property mappings to dynamic schemas (airbytehq#256)
feat: add help response for unrecognized slash commands (airbytehq#264)
ci: post direct links to html connector test reports (airbytehq#252) (airbytehq#263)
fix(low-code): Fix legacy state migration in SubstreamPartitionRouter (airbytehq#261)
fix(airbyte-cdk): Fix RequestOptionsProvider for PerPartitionWithGlobalCursor (airbytehq#254)
feat(low-code): add profile assertion flow to oauth authenticator component (airbytehq#236)
feat(Low-Code Concurrent CDK): Add ConcurrentPerPartitionCursor (airbytehq#111)
fix: don't mypy unit_tests (airbytehq#241)
fix: handle backoff_strategies in CompositeErrorHandler (airbytehq#225)
feat(concurrent cursor): attempt at clamping datetime (airbytehq#234)
fix(airbyte-cdk): Fix RequestOptionsProvider for PerPartitionWithGlobalCursor (airbytehq#254)
feat(low-code): add profile assertion flow to oauth authenticator component (airbytehq#236)
feat(Low-Code Concurrent CDK): Add ConcurrentPerPartitionCursor (airbytehq#111)
fix: don't mypy unit_tests (airbytehq#241)
fix: handle backoff_strategies in CompositeErrorHandler (airbytehq#225)
feat(concurrent cursor): attempt at clamping datetime (airbytehq#234)
ci: use `ubuntu-24.04` explicitly (resolves CI warnings) (airbytehq#244)
Fix(sdm): module ref issue in python components import (airbytehq#243)
feat(source-declarative-manifest): add support for custom Python components from dynamic text input (airbytehq#174)
chore(deps): bump avro from 1.11.3 to 1.12.0 (airbytehq#133)
docs: comments on what the `Dockerfile` is for (airbytehq#240)
chore: move ruff configuration to dedicated ruff.toml file (airbytehq#237)
Fix(sdm): module ref issue in python components import (airbytehq#243)
feat(low-code): add DpathFlattenFields (airbytehq#227)
feat(source-declarative-manifest): add support for custom Python components from dynamic text input (airbytehq#174)
chore(deps): bump avro from 1.11.3 to 1.12.0 (airbytehq#133)
docs: comments on what the `Dockerfile` is for (airbytehq#240)
chore: move ruff configuration to dedicated ruff.toml file (airbytehq#237)
rpopov added a commit to rpopov/airbyte-python-cdk that referenced this pull request Feb 8, 2025
At record extraction step, in each record add the service field $root holding a reference to:
* the root response object, when parsing JSON format
* the original record, when parsing JSONL format
that each record to process is extracted from.
More service fields could be added in future.
The service fields are available in the record's filtering and transform steps.

Avoid:
* reusing the maps/dictionaries produced, thus avoid building cyclic structures
* transforming the service fields in the Flatten transformation.

Explicitly cleanup the service field(s) after the transform step, thus making them:
* local for the filter and transform steps
* not visible to the next mapping and store steps (as they should be)
* not visible in the tests beyond the test_record_selector (as they should be)
This allows the record transformation logic to define its "local variables" to reuse
some interim calculations.

The contract of body parsing seems irregular in representing the cases of bad JSON, no JSON and empty JSON.
Cannot be unified as that that irregularity is already used.

Update the development environment setup documentation
* to organize and present the setup steps explicitly
* to avoid misunderstandings and wasted efforts.

Update CONTRIBUTING.md to
* collect and organize the knowledge on running the test locally.
* state the actual testing steps.
* clarify and make explicit the procedures and steps.

The unit, integration, and acceptance tests in this exactly version succeed under Fedora 41, while
one of them fails under Oracle Linux 8.7. not related to the contents of this PR.
The integration tests of the CDK fail due to missing `secrets/config.json` file for the Shopify source.
See airbytehq#197

Polish

Integrate the DpathEnhancingExtractor in the UI of Airbyte.
Created a DPath Enhancing Extractor
Refactored the record enhancement logic - moved to the extracted class
Split the tests of DPathExtractor and DPathEnhancingExtractor

Fix the failing tests:

FAILED unit_tests/sources/declarative/parsers/test_model_to_component_factory.py::test_create_custom_components[test_create_custom_component_with_subcomponent_that_uses_parameters]
FAILED unit_tests/sources/declarative/parsers/test_model_to_component_factory.py::test_custom_components_do_not_contain_extra_fields
FAILED unit_tests/sources/declarative/parsers/test_model_to_component_factory.py::test_parse_custom_component_fields_if_subcomponent
FAILED unit_tests/sources/declarative/parsers/test_model_to_component_factory.py::test_create_page_increment
FAILED unit_tests/sources/declarative/parsers/test_model_to_component_factory.py::test_create_offset_increment
FAILED unit_tests/sources/file_based/test_file_based_scenarios.py::test_file_based_read[simple_unstructured_scenario]
FAILED unit_tests/sources/file_based/test_file_based_scenarios.py::test_file_based_read[no_file_extension_unstructured_scenario]

They faile because of comparing string and int values of the page_size (public) attribute.
Imposed an invariant:
  on construction, page_size can be set to a string or int
  keep only values of one type in page_size for uniform comparison (convert the values of the other type)
  _page_size holds the internal / working value
... unless manipulated directly.

Merged:
feat(low-code concurrent): Allow async job low-code streams that are incremental to be run by the concurrent framework (airbytehq#228)
fix(low-code): Fix declarative low-code state migration in SubstreamPartitionRouter (airbytehq#267)
feat: combine slash command jobs into single job steps (airbytehq#266)
feat(low-code): add items and property mappings to dynamic schemas (airbytehq#256)
feat: add help response for unrecognized slash commands (airbytehq#264)
ci: post direct links to html connector test reports (airbytehq#252) (airbytehq#263)
fix(low-code): Fix legacy state migration in SubstreamPartitionRouter (airbytehq#261)
fix(airbyte-cdk): Fix RequestOptionsProvider for PerPartitionWithGlobalCursor (airbytehq#254)
feat(low-code): add profile assertion flow to oauth authenticator component (airbytehq#236)
feat(Low-Code Concurrent CDK): Add ConcurrentPerPartitionCursor (airbytehq#111)
fix: don't mypy unit_tests (airbytehq#241)
fix: handle backoff_strategies in CompositeErrorHandler (airbytehq#225)
feat(concurrent cursor): attempt at clamping datetime (airbytehq#234)
fix(airbyte-cdk): Fix RequestOptionsProvider for PerPartitionWithGlobalCursor (airbytehq#254)
feat(low-code): add profile assertion flow to oauth authenticator component (airbytehq#236)
feat(Low-Code Concurrent CDK): Add ConcurrentPerPartitionCursor (airbytehq#111)
fix: don't mypy unit_tests (airbytehq#241)
fix: handle backoff_strategies in CompositeErrorHandler (airbytehq#225)
feat(concurrent cursor): attempt at clamping datetime (airbytehq#234)
ci: use `ubuntu-24.04` explicitly (resolves CI warnings) (airbytehq#244)
Fix(sdm): module ref issue in python components import (airbytehq#243)
feat(source-declarative-manifest): add support for custom Python components from dynamic text input (airbytehq#174)
chore(deps): bump avro from 1.11.3 to 1.12.0 (airbytehq#133)
docs: comments on what the `Dockerfile` is for (airbytehq#240)
chore: move ruff configuration to dedicated ruff.toml file (airbytehq#237)
Fix(sdm): module ref issue in python components import (airbytehq#243)
feat(low-code): add DpathFlattenFields (airbytehq#227)
feat(source-declarative-manifest): add support for custom Python components from dynamic text input (airbytehq#174)
chore(deps): bump avro from 1.11.3 to 1.12.0 (airbytehq#133)
docs: comments on what the `Dockerfile` is for (airbytehq#240)
chore: move ruff configuration to dedicated ruff.toml file (airbytehq#237)

formatted

Update record_extractor.py

Trigger a new build. Hopefully, the integration test infrastructure is fixed.

Update CONTRIBUTING.md

Trigger a new build
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working security
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Low code] Can not config Error Handler for Backoff Strategy
2 participants