Skip to content

fix(low-code): Fix legacy state migration in SubstreamPartitionRouter#261

Merged
Anatolii Yatsuk (tolik0) merged 1 commit into
mainfrom
tolik0/fix-legacy-parent-state-migration
Jan 23, 2025
Merged

fix(low-code): Fix legacy state migration in SubstreamPartitionRouter#261
Anatolii Yatsuk (tolik0) merged 1 commit into
mainfrom
tolik0/fix-legacy-parent-state-migration

Conversation

@tolik0
Copy link
Copy Markdown
Contributor

@tolik0 Anatolii Yatsuk (tolik0) commented Jan 23, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Improved state handling in substream partition routing to prevent incorrect state assignments
    • Enhanced robustness of state filtering mechanism
  • Tests

    • Added comprehensive test cases for substream partition router state management
    • Expanded test coverage for different initial state scenarios

@github-actions github-actions Bot added the bug Something isn't working label Jan 23, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 23, 2025

📝 Walkthrough

Walkthrough

The changes focus on improving the SubstreamPartitionRouter class in the Airbyte CDK, specifically enhancing the set_initial_state method. The modifications involve renaming the substream_state variable to substream_state_values and adding a more robust state filtering mechanism. The update ensures that only valid state formats (non-list, non-dictionary) are processed, preventing potential issues with state assignment in substream partition routing.

Changes

File Change Summary
airbyte_cdk/sources/declarative/partition_routers/substream_partition_router.py - Renamed substream_state to substream_state_values
- Added conditional check to filter state types
unit_tests/sources/declarative/partition_routers/test_substream_partition_router.py - Added new test_set_initial_state function
- Introduced parameterized test cases for state management

Sequence Diagram

sequenceDiagram
    participant SR as SubstreamPartitionRouter
    participant IS as Initial State
    
    SR->>IS: set_initial_state()
    IS-->>SR: Validate state type
    alt Valid State
        SR->SR: Copy state to parent streams
    else Invalid State
        SR->SR: Ignore state
    end
Loading

Looks like you've added some nice defensive programming to the state handling! A couple of quick thoughts:

  1. The renaming from substream_state to substream_state_values makes the intent clearer - good move! 👍
  2. The type filtering adds a nice layer of robustness. Wdyt about adding a log warning when an invalid state type is encountered?

Would you be interested in exploring that logging addition? It could help with debugging unexpected state scenarios. Cheers! 🚀

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

🧹 Nitpick comments (1)
airbyte_cdk/sources/declarative/partition_routers/substream_partition_router.py (1)

299-304: LGTM! The state handling improvements look solid.

The changes make the state migration more robust by filtering out non-scalar values. The variable rename from substream_state to substream_state_values better reflects its purpose.

What do you think about adding a debug log when filtering out a state value? This could help with troubleshooting, wdyt? 🤔

 substream_state_values = list(stream_state.values())
 substream_state = substream_state_values[0] if substream_state_values else {}
 # Filter out per partition state. Because we pass the state to the parent stream in the format {cursor_field: substream_state}
 if isinstance(substream_state, (list, dict)):
+    self.logger.debug(f"Filtering out non-scalar state value: {substream_state}")
     substream_state = {}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ec7e961 and 544effb.

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

405-512: Great test coverage! 🎯

The test cases thoroughly validate the state migration logic with well-structured parameterized tests. The test IDs and docstring provide clear documentation of what's being tested.

@tolik0
Copy link
Copy Markdown
Contributor Author

Anatolii Yatsuk (tolik0) commented Jan 23, 2025

I found this error in CATs for Jira: The parent state was missing, causing the substream partition router to attempt migrating the substream state for the parent stream. However, it should only be migrated if it is in the legacy format. We didn’t encounter errors related to this issue previously because the parent state was updated during the sync. In contrast, with the concurrent per-partition cursor, the parent state is updated at the end of the sync, which created this corner case where the parent state is missing. With the fix, tests are successful.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

APPROVED

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants