Skip to content

feat: Remove long standing connections on sql state and catalog backends#675

Closed
Ben Wilen (benjaminwilen) wants to merge 1 commit into
airbytehq:mainfrom
benjaminwilen:ben.wilen/feat_Remove_long_standing_connections_on_sql_state_and_catalog_backends
Closed

feat: Remove long standing connections on sql state and catalog backends#675
Ben Wilen (benjaminwilen) wants to merge 1 commit into
airbytehq:mainfrom
benjaminwilen:ben.wilen/feat_Remove_long_standing_connections_on_sql_state_and_catalog_backends

Conversation

@benjaminwilen

@benjaminwilen Ben Wilen (benjaminwilen) commented May 20, 2025

Copy link
Copy Markdown
Contributor

What does this PR do?

This PR modifies how SqlCatalogBackend and SqlStateBackend connect to an sql engine to do so lazily and per query execution instead of eagerly and in a single connection. This allows long running syncs to complete without receiving the following error:

(snowflake.connector.errors.ProgrammingError) 390114 (08001): None: Authentication token has expired.  The user must authenticate again.

Testing

  • tbd (want an initial review of approach before testing)

Summary by CodeRabbit

  • Refactor
    • Updated backend initialization to use a configuration object for SQL connections instead of a direct engine reference. This change affects how internal catalog and state management components are set up, with no impact on user-facing features or workflows.

@coderabbitai

coderabbitai Bot commented May 20, 2025

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The changes refactor backend classes to accept a SqlConfig object instead of a direct SQLAlchemy Engine instance. Constructors and internal references are updated to retrieve the engine dynamically from SqlConfig. The CacheBase class now passes itself as sql_config to its backend components, aligning with the new initialization pattern.

Changes

File(s) Change Summary
airbyte/caches/_catalog_backend.py Updated SqlCatalogBackend to accept SqlConfig instead of Engine. All engine usages now call sql_config.get_sql_engine(). Imports and type hints adjusted accordingly.
airbyte/caches/_state_backend.py Updated SqlStateBackend and SqlStateWriter to accept SqlConfig instead of Engine. Internal engine references replaced with dynamic retrieval from SqlConfig. Related imports and type hints updated.
airbyte/caches/base.py Modified CacheBase to instantiate backends with sql_config=self instead of passing the engine directly. This aligns backend initialization with the new configuration approach.

Sequence Diagram(s)

sequenceDiagram
    participant CacheBase
    participant SqlCatalogBackend
    participant SqlStateBackend
    participant SqlConfig
    participant SqlEngine

    CacheBase->>SqlCatalogBackend: __init__(sql_config=self, table_prefix)
    CacheBase->>SqlStateBackend: __init__(sql_config=self, table_prefix)
    SqlCatalogBackend->>SqlConfig: get_sql_engine()
    SqlConfig->>SqlEngine: returns engine
    SqlStateBackend->>SqlConfig: get_sql_engine()
    SqlConfig->>SqlEngine: returns engine
Loading

Would you like me to help draft a more detailed sequence diagram showing specific method calls inside the backends, or does this high-level overview work for your review, wdyt?

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 30th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

📥 Commits

Reviewing files that changed from the base of the PR and between 8bfa5a2 and 872f893.

📒 Files selected for processing (3)
  • airbyte/caches/_catalog_backend.py (5 hunks)
  • airbyte/caches/_state_backend.py (4 hunks)
  • airbyte/caches/base.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • airbyte/caches/_state_backend.py
  • airbyte/caches/base.py
  • airbyte/caches/_catalog_backend.py
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Pytest (Fast)
  • GitHub Check: Pytest (No Creds)
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Commit Unit Tests in branch ben.wilen/feat_Remove_long_standing_connections_on_sql_state_and_catalog_backends
  • Post Copyable Unit Tests in Comment

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
🪧 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this 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 or @coderabbitai title 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.

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 2

🧹 Nitpick comments (2)
airbyte/caches/_catalog_backend.py (1)

179-226: Consider connection pooling implications.

While creating a new engine for each operation solves the token expiration issue, have you considered the performance implications? SQLAlchemy engines typically use connection pooling, so this might not be as expensive as it seems, but it's worth considering whether setting appropriate pool options (like pool_recycle) might be a more efficient alternative in some cases, wdyt?

airbyte/caches/_state_backend.py (1)

1-276: Consider adding tests for this change.

The PR objectives mention that testing is yet to be completed. Would you consider adding specific tests that simulate long-running operations to verify this fix actually resolves the token expiration issue? This could be particularly valuable to prevent regression in the future.

🧰 Tools
🪛 Ruff (0.11.9)

23-23: Move application import airbyte.shared.sql_processor.SqlConfig into a type-checking block

Move into type-checking block

(TC001)

🪛 GitHub Actions: Run Linters

[error] 23-23: Ruff TC001: Move application import airbyte.shared.sql_processor.SqlConfig into a type-checking block.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

📥 Commits

Reviewing files that changed from the base of the PR and between 0296295 and ae39eff.

📒 Files selected for processing (3)
  • airbyte/caches/_catalog_backend.py (5 hunks)
  • airbyte/caches/_state_backend.py (4 hunks)
  • airbyte/caches/base.py (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
airbyte/caches/_catalog_backend.py (4)
airbyte/shared/sql_processor.py (3)
  • SqlConfig (86-151)
  • get_sql_engine (131-140)
  • get_sql_engine (384-386)
airbyte/cloud/sync_results.py (1)
  • get_sql_engine (279-281)
airbyte/_processors/sql/duckdb.py (1)
  • get_sql_engine (78-87)
airbyte/results.py (1)
  • get_sql_engine (80-82)
🪛 Ruff (0.11.9)
airbyte/caches/_state_backend.py

23-23: Move application import airbyte.shared.sql_processor.SqlConfig into a type-checking block

Move into type-checking block

(TC001)

airbyte/caches/_catalog_backend.py

26-26: Move application import airbyte.shared.sql_processor.SqlConfig into a type-checking block

Move into type-checking block

(TC001)

🪛 GitHub Actions: Run Linters
airbyte/caches/_state_backend.py

[error] 23-23: Ruff TC001: Move application import airbyte.shared.sql_processor.SqlConfig into a type-checking block.

airbyte/caches/_catalog_backend.py

[error] 26-26: Ruff TC001: Move application import airbyte.shared.sql_processor.SqlConfig into a type-checking block.

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Pytest (No Creds)
  • GitHub Check: Pytest (Fast)
🔇 Additional comments (10)
airbyte/caches/base.py (2)

91-94: LGTM! Dynamic SQL engine retrieval instead of reusing a single connection.

This change looks good - passing sql_config=self to the SqlCatalogBackend instead of directly injecting an engine instance. This allows the backend to create a fresh connection each time it needs one, which should help address the Snowflake token expiration issue mentioned in the PR description.


95-98: LGTM! Consistent approach for both backend components.

Perfect consistency - applying the same pattern to the state backend as well. This ensures all SQL operations will now use fresh connections, preventing authentication token expirations during long-running syncs.

airbyte/caches/_catalog_backend.py (4)

107-113: LGTM! Improved connection handling with SqlConfig.

Great refactoring to use sql_config instead of a direct engine instance. This changes the backend from eagerly connecting and reusing a single connection to lazily connecting for each database operation.

This addresses the Snowflake token expiration issue mentioned in the PR objectives.


123-125: LGTM! Dynamic retrieval of SQL engine.

Perfect - now retrieving a fresh engine instance when needed rather than reusing a stored engine.


179-181: LGTM! Consistent engine retrieval pattern.

Correctly using the dynamic engine retrieval pattern here as well.


215-217: LGTM! Consistent implementation throughout the class.

Final instance of the pattern applied correctly.

airbyte/caches/_state_backend.py (4)

137-139: LGTM! Dynamic SQL engine retrieval in the state writer.

Perfect implementation - this change ensures that the state writer creates a fresh connection each time it writes state, avoiding the token expiration issue.


187-194: LGTM! Consistent SqlConfig pattern in state backend.

Excellent implementation of the same pattern in the state backend constructor. This change keeps the pattern consistent across both catalog and state backends.


197-201: LGTM! Dynamic engine retrieval when ensuring tables exist.

Good implementation - retrieving a fresh engine when creating internal tables.


224-226: LGTM! Final instance of the dynamic engine retrieval pattern.

Consistently applied the pattern throughout the codebase.

Comment thread airbyte/caches/_catalog_backend.py Outdated
Comment thread airbyte/caches/_state_backend.py
@benjaminwilen Ben Wilen (benjaminwilen) force-pushed the ben.wilen/feat_Remove_long_standing_connections_on_sql_state_and_catalog_backends branch 2 times, most recently from 66ed0d1 to 2f3b247 Compare May 20, 2025 19:55
@benjaminwilen Ben Wilen (benjaminwilen) force-pushed the ben.wilen/feat_Remove_long_standing_connections_on_sql_state_and_catalog_backends branch 2 times, most recently from c0d9e0d to 8bfa5a2 Compare May 21, 2025 14:22
@benjaminwilen Ben Wilen (benjaminwilen) force-pushed the ben.wilen/feat_Remove_long_standing_connections_on_sql_state_and_catalog_backends branch from 8bfa5a2 to 872f893 Compare May 21, 2025 14:24
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