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

fix: Avoid write empty state #2844

Merged
merged 10 commits into from
Jan 29, 2025

Conversation

joaopamaral
Copy link
Contributor

@joaopamaral joaopamaral commented Jan 28, 2025

Summary

Currently, Meltano sends an empty state message when no state is provided in the --state parameter. The issue arises when the state message is processed and the database is updated. If the Meltano extraction fails before the next state message is sent, the state in the database will be empty, leading to the loss of the last known state.

2025-01-28T18:32:50.566137Z [debug    ] {"type":"STATE","value":{}}    cmd_type=extractor name=tap-zendesk (out)

This issue has been observed across several extractors using PostgreSQL on Cloud SQL (GCP) as the Meltano database, forcing manual updates to restore the state.

Bug Fixes:

  • Avoid writing empty state messages when the state is empty.

📚 Documentation preview 📚: https://meltano-sdk--2844.org.readthedocs.build/en/2844/

Summary by Sourcery

Bug Fixes:

  • Prevent writing empty state messages to the database if the state is empty.

Copy link
Contributor

sourcery-ai bot commented Jan 28, 2025

Reviewer's Guide by Sourcery

This pull request prevents the writing of empty state messages, which could lead to data loss. The changes ensure that state messages are only written if the state is not empty, or if the current state is different from the last emitted state.

Sequence diagram for state message handling in Meltano

sequenceDiagram
    participant Extractor
    participant StateHandler
    participant Database

    Note over Extractor,Database: Before Fix
    Extractor->>StateHandler: Start extraction
    StateHandler->>Database: Write empty state {}
    Note right of Database: State is lost if extraction fails

    Note over Extractor,Database: After Fix
    Extractor->>StateHandler: Start extraction
    alt State is not empty
        StateHandler->>Database: Write state message
    else State is empty
        Note over StateHandler: Skip writing empty state
    end
Loading

State diagram for state message handling logic

stateDiagram-v2
    [*] --> CheckState
    CheckState --> EvaluateConditions: State exists
    CheckState --> [*]: Empty state

    EvaluateConditions --> WriteState: State not flushed AND
State different from last
    EvaluateConditions --> [*]: Conditions not met

    WriteState --> UpdateLastEmitted: Write state message
    UpdateLastEmitted --> [*]: Copy state
Loading

File-Level Changes

Change Details Files
Avoid writing empty state messages.
  • Modified the _write_state_message method in singer_sdk/streams/core.py to check if the state is not empty before writing a state message.
  • Modified the sync_all method in singer_sdk/tap_base.py to check if the state is not empty before writing a state message.
singer_sdk/streams/core.py
singer_sdk/tap_base.py
Updated tests to reflect changes in state message emission.
  • Adjusted assertions in tests/core/test_parent_child.py to account for the change in the order of messages.
  • Added initial state to the SQLiteTap in tests/samples/test_target_sqlite.py.
  • Adjusted the expected number of state messages in tests/samples/test_tap_countries.py.
tests/core/test_parent_child.py
tests/samples/test_target_sqlite.py
tests/samples/test_tap_countries.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

codecov bot commented Jan 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.33%. Comparing base (6ed507e) to head (b713e23).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2844   +/-   ##
=======================================
  Coverage   91.33%   91.33%           
=======================================
  Files          62       62           
  Lines        5205     5206    +1     
  Branches      671      672    +1     
=======================================
+ Hits         4754     4755    +1     
  Misses        319      319           
  Partials      132      132           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

codspeed-hq bot commented Jan 28, 2025

CodSpeed Performance Report

Merging #2844 will not alter performance

Comparing joaopamaral:fix/avoid-write-empty-state (b713e23) with main (6ed507e)

Summary

✅ 7 untouched benchmarks

@edgarrmondragon
Copy link
Collaborator

Hey @joaopamaral, any particular reason for closing this?

@joaopamaral
Copy link
Contributor Author

Hey @joaopamaral, any particular reason for closing this?

Hi @edgarrmondragon, I'll finish adding the test cases before reopen it! Maybe I should just open as draft 😅

@joaopamaral joaopamaral reopened this Jan 29, 2025
@@ -138,6 +140,7 @@ def test_sync_sqlite_to_sqlite(
new_lines = new_stdout.readlines()
assert len(orig_lines) > 0, "Orig tap output should not be empty."
assert len(new_lines) > 0, "(Re-)tapped target output should not be empty."
assert orig_lines[0] == new_lines[0]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test was incorrect before because the state was not included in the provided state to the second call.


  - {"type":"STATE","value":{}}
  + {"type":"STATE","value":{"bookmarks":{"main-t0":{"replication_key":"c1","replication_key_value":55}}}}

@joaopamaral joaopamaral changed the title [Fix] Avoid write empty state fix: Avoid write empty state Jan 29, 2025
@edgarrmondragon edgarrmondragon self-assigned this Jan 29, 2025
@joaopamaral joaopamaral marked this pull request as ready for review January 29, 2025 19:17
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @joaopamaral - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@edgarrmondragon
Copy link
Collaborator

(Doing some manual testing to ensure we don't regress on #1750 / #1753, and then see if I can add unit tests here for that)

@edgarrmondragon
Copy link
Collaborator

(Doing some manual testing to ensure we don't regress on #1750 / #1753, and then see if I can add unit tests here for that)

Ok, I've confirmed that the initial state message is emitted if --state ... is passed and the state is not empty.

Co-authored-by: Edgar Ramírez Mondragón <[email protected]>
@edgarrmondragon edgarrmondragon modified the milestones: v0.45, v0.44 Jan 29, 2025
Copy link
Collaborator

@edgarrmondragon edgarrmondragon left a comment

Choose a reason for hiding this comment

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

Thank you @joaopamaral!

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

Successfully merging this pull request may close these issues.

2 participants