8000 fix: make import not transactional by gfyrag · Pull Request #893 · formancehq/ledger · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: make import not transactional #893

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

Merged
merged 6 commits into from
May 2, 2025
Merged

Conversation

gfyrag
Copy link
Contributor
@gfyrag gfyrag commented Apr 29, 2025
  • test: add import test with error
  • feat: remove sql transaction on imports

@gfyrag gfyrag requested a review from a team as a code owner April 29, 2025 09:25
Copy link
coderabbitai bot commented Apr 29, 2025

Walkthrough

This set of changes introduces a new advisory locking mechanism for ledger operations, replacing previous table-level locks with PostgreSQL advisory locks. The LockLedger method is added or updated across several interfaces and implementations, now returning additional resources (controller/store instance, database interface, cleanup function, and error). The control flow in the system's state tracker is refactored to centralize ledger locking and connection management via a new withLock helper. Mock and test files are updated to reflect the new method signatures. End-to-end tests are enhanced to verify advisory lock behavior and error handling during concurrent and error-prone ledger imports.

Changes

File(s) Change Summary
internal/controller/ledger/controller.go
internal/controller/ledger/store.go
Added LockLedger method to Controller interface and updated Store interface to return additional resources (instance, DB, cleanup function, error).
internal/controller/ledger/controller_default.go Implemented LockLedger in DefaultController; updated Import logic for error wrapping and correct log ID tracking.
internal/controller/ledger/controller_with_traces.go Added lockLedgerHistogram metric and LockLedger method with tracing and metrics in ControllerWithTraces.
internal/controller/ledger/controller_generated_test.go
internal/api/common/mocks_ledger_controller_test.go
internal/api/bulking/mocks_ledger_controller_test.go
internal/api/v1/mocks_ledger_controller_test.go
internal/api/v2/mocks_ledger_controller_test.go
Added mock LockLedger methods and recorders to various test and mock files to support new interface signature.
internal/controller/ledger/store_generated_test.go Updated mock LockLedger method to return four values (store, DB, cleanup, error) instead of just error.
internal/storage/ledger/adapters.go Removed Commit and Rollback from DefaultStoreAdapter; added LockLedger method that wraps underlying store's lock method and returns new adapter, DB, cleanup, error.
internal/storage/ledger/store.go Replaced table-level locking with PostgreSQL advisory locks in LockLedger; returns store copy, DB, release function, and error; supports both DB and transaction contexts.
internal/controller/system/state_tracker.go Refactored state tracker to use new withLock helper for acquiring/releasing ledger locks, removing manual transaction management and direct lock handling in handleState and Import.
test/e2e/api_ledgers_import_test.go Added tests for import error handling and advisory lock verification during concurrent operations; updated tests to check advisory locks instead of active queries.
test/e2e/app_lifecycle_test.go Updated lock verification logic to count advisory locks in PostgreSQL instead of active queries, reflecting new advisory locking mechanism.
internal/controller/ledger/controller_with_cache.go Added LockLedger method wrapping underlying controller call, returning cached controller wrapper, DB handle, release function, and error.
internal/controller/ledger/controller_with_events.go Added LockLedger method wrapping underlying controller call, returning events controller wrapper, DB handle, release function, and error.
internal/controller/ledger/controller_with_too_many_client_handling.go Added LockLedger method wrapping underlying controller call, returning too-many-client-handling controller wrapper, DB handle, release function, and error.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Controller
    participant Store
    participant DB

    Client->>Controller: LockLedger(ctx)
    Controller->>Store: LockLedger(ctx)
    alt Using bun.DB
        Store->>DB: Acquire connection
        Store->>DB: pg_advisory_lock(hash(ledgerID))
        Store-->>Controller: (locked Store, DB, release func, error)
    else Using bun.Tx
        Store->>DB: pg_advisory_xact_lock(hash(ledgerID))
        Store-->>Controller: (locked Store, Tx, no-op release, error)
    end
    Controller-->>Client: (Controller, DB, release func, error)
    Note over Client: Use locked controller and DB, then call release func
Loading

Possibly related PRs

  • fix: make import not transactional #892: Adds the LockLedger method with identical signatures and mock implementations across multiple ledger controller layers and related store interfaces, directly related to introducing and supporting the LockLedger functionality.

Suggested reviewers

  • paul-nicolas

Poem

In the warren where ledgers are kept tight,
Advisory locks now guard through the night.
With a hop and a skip, transactions align,
Mock bunnies test, and all works fine.
No more table locks, just hashes and keys,
Ledger flows smoothly, with carrot-like ease!
🐇🔒✨

✨ Finishing Touches
  • 📝 Generate Docstrings

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:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in 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 anywhere in the PR title to generate the title automatically.

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

🧹 Nitpick comments (4)
internal/storage/ledger/store.go (1)

166-168: Use a 64-bit hash to minimize collision risk

hashtext(text) returns an int4; passing it to the single-arg version of
pg_advisory_lock casts it to bigint, but the value space is still 32 bit.
With many ledgers the probability of collision grows.
Consider hashing to two int4 parts (hashtextext, hashtextext(…||'salt')) and
calling the two-arg lock, or use hashtextextbigint via
hashtext(...)::bigint << 32 | hashtext('…salt…')::bigint.

test/e2e/api_ledgers_import_test.go (3)

418-426: pg_locks count assertion may be flaky on busy CI databases

The test expects exactly one advisory lock, but other tests running in
parallel (or the driver itself) might hold unrelated advisory locks, causing
sporadic failures. Narrow the query by classid/objid or by pid to the
current connection instead of relying on a global count.

-   Where("locktype = 'advisory'").
+   Where("locktype = 'advisory'").
+   Where("objid = hashtext(?)", fmt.Sprintf("ledger:%d", ledgerID)).

447-459: Same flakiness for “two locks” check

For the same reason as above, counting all advisory locks can produce >2 rows
even when the expected behaviour is correct. Filter by the ledger hash and use
granted/waiting columns to assert the precise state (1 granted, 1 waiting).


155-163: Assertion assumes 2 inserted logs, but hard-codes nothing about input order

If the import algorithm ever changes to skip invalid entries instead of
aborting, the test will silently pass while the behaviour has changed. Consider
asserting that the IDs present are exactly [0,1] to document the contract
and improve test robustness.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f02b5d5 and 682f484.

📒 Files selected for processing (15)
  • internal/api/bulking/mocks_ledger_controller_test.go (1 hunks)
  • internal/api/common/mocks_ledger_controller_test.go (1 hunks)
  • internal/api/v1/mocks_ledger_contro 8000 ller_test.go (1 hunks)
  • internal/api/v2/mocks_ledger_controller_test.go (1 hunks)
  • internal/controller/ledger/controller.go (1 hunks)
  • internal/controller/ledger/controller_default.go (2 hunks)
  • internal/controller/ledger/controller_generated_test.go (1 hunks)
  • internal/controller/ledger/controller_with_traces.go (3 hunks)
  • internal/controller/ledger/store.go (1 hunks)
  • internal/controller/ledger/store_generated_test.go (1 hunks)
  • internal/controller/system/state_tracker.go (4 hunks)
  • internal/storage/ledger/adapters.go (1 hunks)
  • internal/storage/ledger/store.go (1 hunks)
  • test/e2e/api_ledgers_import_test.go (3 hunks)
  • test/e2e/app_lifecycle_test.go (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (7)
internal/api/bulking/mocks_ledger_controller_test.go (2)
internal/api/common/mocks_ledger_controller_test.go (2)
  • LedgerController (25-29)
  • LedgerControllerMockRecorder (32-34)
internal/controller/ledger/controller.go (1)
  • Controller (19-80)
internal/controller/ledger/store_generated_test.go (1)
internal/controller/ledger/store.go (1)
  • Store (31-66)
internal/api/v2/mocks_ledger_controller_test.go (2)
internal/api/common/mocks_ledger_controller_test.go (2)
  • LedgerController (25-29)
  • LedgerControllerMockRecorder (32-34)
internal/controller/ledger/controller.go (1)
  • Controller (19-80)
internal/api/v1/mocks_ledger_controller_test.go (2)
internal/api/common/mocks_ledger_controller_test.go (2)
  • LedgerController (25-29)
  • LedgerControllerMockRecorder (32-34)
internal/controller/ledger/controller.go (1)
  • Controller (19-80)
internal/api/common/mocks_ledger_controller_test.go (2)
internal/api/v2/mocks_ledger_controller_test.go (2)
  • LedgerController (25-29)
  • LedgerControllerMockRecorder (32-34)
internal/controller/ledger/controller.go (1)
  • Controller (19-80)
internal/storage/ledger/store.go (1)
internal/controller/ledger/store.go (1)
  • Store (31-66)
internal/controller/ledger/controller_default.go (2)
internal/controller/ledger/controller.go (1)
  • Controller (19-80)
internal/controller/ledger/errors.go (1)
  • NewErrImport (33-37)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Dirty
  • GitHub Check: Tests
🔇 Additional comments (18)
internal/controller/ledger/controller.go (1)

23-23: Looks good - API extension with proper cleanup mechanism.

The addition of LockLedger method to the Controller interface with the return signature (Controller, bun.IDB, func() error, error) is a well-designed improvement. This pattern provides a clean way to acquire a ledger lock and necessary resources while ensuring proper cleanup through the returned function.

internal/controller/ledger/store.go (1)

53-53: Improved locking mechanism with explicit resource management.

The enhanced LockLedger signature aligns with the Controller interface change and provides a more robust approach to ledger locking by returning the locked store, database interface, cleanup function, and error status. This change supports the goal of making imports non-transactional while maintaining data integrity.

internal/controller/ledger/store_generated_test.go (1)

224-231: Correctly updated mock to match the new interface signature.

The mock implementation properly reflects the updated LockLedger method signature, correctly extracting and returning the four expected values: Store, bun.IDB, cleanup function, and error. This ensures tests using this mock will work correctly with the new locking mechanism.

test/e2e/app_lifecycle_test.go (3)

97-97: Comment updated to reflect the new advisory locking mechanism.

The comment has been correctly updated to reference advisory locks instead of table locks, which aligns with the architectural change in the PR.


131-133: Test verification query updated to check advisory locks.

The test has been appropriately modified to check pg_locks table with a filter for advisory locks instead of the previous approach that looked at non-idle queries related to account insertions.


139-140: Updated lock count expectations with clear rationale.

The expectations and comments have been updated to account for the new advisory locking strategy. The explanation about having countTransactions+1 locks (with the +1 for logs sync hashing) is clear and helpful for future maintenance.

internal/api/bulking/mocks_ledger_controller_test.go (1)

332-347: Well-implemented mock for the new LockLedger method.

The implementation correctly mirrors the Controller interface's new LockLedger method that supports the new PostgreSQL advisory locking mechanism. The mock properly returns the expected values (controller, database interface, cleanup function, and error) consistent with the interface definition in controller.go.

internal/api/v1/mocks_ledger_controller_test.go (1)

332-347: Correctly implemented mock for the LockLedger method.

The mock implementation appropriately matches the Controller interface's LockLedger method signature, returning the four required components: controller, database interface, cleanup function, and error. This supports the new advisory locking mechanism for non-transactional imports.

internal/controller/ledger/controller_generated_test.go (1)

331-347: Proper implementation of the MockController's LockLedger method.

The mock method implementation correctly aligns with the Controller interface definition, properly handling type assertions for the return values. This will properly support testing the advisory locking mechanism that replaces the previous table-level locks.

internal/api/common/mocks_ledger_controller_test.go (1)

332-347: Well-structured mock implementation for LockLedger.

The implementation correctly matches the Controller interface signature, properly handling the four return values required for the advisory locking mechanism. This mock will allow effective testing of the new non-transactional approach to imports.

internal/controller/ledger/controller_with_traces.go (3)

43-43: LGTM: New histogram field added for lock ledger metrics

The addition of the lockLedgerHistogram field to the ControllerWithTraces struct is consistent with the existing pattern for other operations in the controller.


141-144: LGTM: Proper initialization of the new histogram

The metric initialization follows the same pattern as other metrics in this file with proper error handling.


464-486: LGTM: Well-implemented method for tracing ledger locks

The LockLedger implementation correctly traces the operation, records metrics, and propagates the return values from the underlying controller. It follows the same pattern as other traced methods in this controller wrapper.

internal/storage/ledger/adapters.go (1)

39-47: LGTM: Proper adapter method implementation

The LockLedger method correctly delegates to the underlying store and wraps the returned store in a new adapter instance. This maintains the adapter pattern consistently throughout the codebase.

internal/api/v2/mocks_ledger_controller_test.go (1)

332-347: LGTM: Mock implementation added for new controller method

The mock implementation of LockLedger follows the standard gomock pattern used throughout the codebase. It correctly handles the return types (Controller, bun.IDB, func() error, error) and provides a method to record expected calls.

internal/controller/ledger/controller_default.go (3)

82-95: LGTM: Clean implementation of ledger locking

The LockLedger implementation correctly copies the controller, delegates to the store's locking mechanism, and returns the appropriate values. The approach is consistent with other similar methods like BeginTX.


203-203: Improvement: Better error handling with specialized error type

Good change to use NewErrImport instead of a generic error. This allows callers to check for specific error types and provides more context about the nature of the failure.


205-205: Bug fix: Correctly updating lastLogID tracking

This change fixes an important bug where the lastLogID was not being properly tracked during import. Now it correctly updates to the current log's ID after validation.

Copy link
codecov bot commented Apr 29, 2025

Codecov Report

Attention: Patch coverage is 67.51592% with 51 lines in your changes missing coverage. Please review.

Project coverage is 81.97%. Comparing base (8e0114d) to head (43a45d8).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
internal/storage/ledger/store.go 53.12% 11 Missing and 4 partials ⚠️
internal/controller/system/state_tracker.go 57.14% 9 Missing and 3 partials ⚠️
...ternal/controller/ledger/controller_with_traces.go 80.76% 3 Missing and 2 partials ⚠️
internal/api/v2/controllers_logs_import.go 73.33% 3 Missing and 1 partial ⚠️
internal/controller/ledger/controller_default.go 78.57% 2 Missing and 1 partial ⚠️
...nternal/controller/ledger/controller_with_cache.go 70.00% 2 Missing and 1 partial ⚠️
...ternal/controller/ledger/controller_with_events.go 72.72% 2 Missing and 1 partial ⚠️
...ledger/controller_with_too_many_client_handling.go 70.00% 2 Missing and 1 partial ⚠️
internal/storage/ledger/adapters.go 62.50% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #893      +/-   ##
==========================================
- Coverage   82.27%   81.97%   -0.30%     
==========================================
  Files         141      141              
  Lines        7640     7746     +106     
==========================================
+ Hits         6286     6350      +64     
- Misses       1039     1070      +31     
- Partials      315      326      +11     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@gfyrag
Copy link
Contributor Author
gfyrag commented Apr 29, 2025

@coderabbitai configuration

Copy link
coderabbitai bot commented Apr 29, 2025
CodeRabbit configuration
language: en-US
tone_instructions: ''
early_access: true
enable_free_tier: true
reviews:
  profile: chill
  request_changes_workflow: false
  high_level_summary: false
  high_level_summary_placeholder: '@coderabbitai summary'
  high_level_summary_in_walkthrough: false
  auto_title_placeholder: '@coderabbitai'
  auto_title_instructions: ''
  review_status: true
  commit_status: true
  fail_commit_status: false
  collapse_walkthrough: false
  changed_files_summary: true
  sequence_diagrams: true
  assess_linked_issues: true
  related_issues: true
  related_prs: true
  suggested_labels: true
  auto_apply_labels: false
  suggested_reviewers: true
  auto_assign_reviewers: false
  poem: true
  labeling_instructions: []
  path_filters:
    - '!dist/**'
    - '!**/*.app'
    - '!**/*.bin'
    - '!**/*.bz2'
    - '!**/*.class'
    - '!**/*.db'
    - '!**/*.csv'
    - '!**/*.tsv'
    - '!**/*.dat'
    - '!**/*.dll'
    - '!**/*.dylib'
    - '!**/*.egg'
    - '!**/*.glif'
    - '!**/*.gz'
    - '!**/*.xz'
    - '!**/*.zip'
    - '!**/*.7z'
    - '!**/*.rar'
    - '!**/*.zst'
    - '!**/*.ico'
    - '!**/*.jar'
    - '!**/*.tar'
    - '!**/*.war'
    - '!**/*.lo'
    - '!**/*.log'
    - '!**/*.mp3'
    - '!**/*.wav'
    - '!**/*.wma'
    - '!**/*.mp4'
    - '!**/*.avi'
    - '!**/*.mkv'
    - '!**/*.wmv'
    - '!**/*.m4a'
    - '!**/*.m4v'
    - '!**/*.3gp'
    - '!**/*.3g2'
    - '!**/*.rm'
    - '!**/*.mov'
    - '!**/*.flv'
    - '!**/*.iso'
    - '!**/*.swf'
    - '!**/*.flac'
    - '!**/*.nar'
    - '!**/*.o'
    - '!**/*.ogg'
    - '!**/*.otf'
    - '!**/*.p'
    - '!**/*.pdf'
    - '!**/*.doc'
    - '!**/*.docx'
    - '!**/*.xls'
    - '!**/*.xlsx'
    - '!**/*.map'
    - '!**/*.out'
    - '!**/*.ppt'
    - '!**/*.pptx'
    - '!**/*.pkl'
    - '!**/*.pickle'
    - '!**/*.pyc'
    - '!**/*.pyd'
    - '!**/*.pyo'
    - '!**/*.pub'
    - '!**/*.pem'
    - '!**/*.rkt'
    - '!**/*.so'
    - '!**/*.ss'
    - '!**/*.eot'
    - '!**/*.exe'
    - '!**/*.pb.go'
    - '!**/*.pb.gw.go'
    - '!**/*.lock'
    - '!**/*.ttf'
    - '!**/*.yaml'
    - '!**/*.yml'
    - '!**/*.xml'
    - '!**/*.cfg'
    - '!**/*.toml'
    - '!**/*.ini'
    - '!**/*.mod'
    - '!**/*.sum'
    - '!**/*.work'
    - '!**/*.json'
    - '!**/*.mmd'
    - '!**/*.svg'
    - '!**/*.jpeg'
    - '!**/*.jpg'
    - '!**/*.png'
    - '!**/*.gif'
    - '!**/*.bmp'
    - '!**/*.tiff'
    - '!**/*.webm'
    - '!**/*.woff'
    - '!**/*.woff2'
    - '!**/*.dot'
    - '!**/*.md5sum'
    - '!**/*.wasm'
    - '!**/*.snap'
    - '!**/*.parquet'
    - '!**/gen/**'
    - '!**/_gen/**'
    - '!**/generated/**'
    - '!**/@generated/**'
    - '!**/vendor/**'
    - '!**/*.min.js'
    - '!**/*.min.js.map'
    - '!**/*.min.js.css'
    - '!**/*.tfstate'
    - '!**/*.tfstate.backup'
  path_instructions: []
  abort_on_close: true
  disable_cache: false
  auto_review:
    enabled: true
    auto_incremental_review: true
    ignore_title_keywords: []
    labels: []
    drafts: false
    base_branches:
      - main
  finishing_touches:
    docstrings:
      enabled: true
  tools:
    ast-grep:
      rule_dirs: []
      util_dirs: []
      essential_rules: true
      packages: []
    shellcheck:
      enabled: true
    ruff:
      enabled: true
    markdownlint:
      enabled: true
    github-checks:
      enabled: true
      timeout_ms: 90000
    languagetool:
      enabled: true
      enabled_rules: []
      disabled_rules: []
      enabled_categories: []
      disabled_categories: []
      enabled_only: false
      level: default
    biome:
      enabled: true
    hadolint:
      enabled: true
    swiftlint:
      enabled: true
    phpstan:
      enabled: true
      level: default
    golangci-lint:
      enabled: true
    yamllint:
      enabled: true
    gitleaks:
      enabled: true
    checkov:
      enabled: true
    detekt:
      enabled: true
    eslint:
      enabled: true
    rubocop:
      enabled: true
    buf:
      enabled: true
    regal:
      enabled: true
    actionlint:
      enabled: true
    pmd:
      enabled: true
    cppcheck:
      enabled: true
    semgrep:
      enabled: true
    circleci:
      enabled: true
    sqlfluff:
      enabled: true
    prismaLint:
      enabled: true
    oxc:
      enabled: true
    shopifyThemeCheck:
      enabled: true
chat:
  auto_reply: true
  create_issues: true
  integrations:
    jira:
      usage: auto
    linear:
      usage: auto
knowledge_base:
  opt_out: false
  web_search:
    enabled: true
  learnings:
    scope: auto
  issues:
    scope: auto
  jira:
    usage: auto
    project_keys: []
  linear:
    usage: auto
    team_keys: []
  pull_requests:
    scope: auto
code_generation:
  docstrings:
    language: en-US
    path_instructions: []

Copy link
8000
@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

♻️ Duplicate comments (1)
internal/controller/system/state_tracker.go (1)

159-176: Well-implemented centralized lock handling

The new withLock helper function centralizes ledger locking logic and properly:

  1. Acquires the lock via LockLedger
  2. Handles lock acquisition errors
  3. Ensures lock release even in error cases
  4. Logs and records telemetry for lock release failures
  5. Correctly passes the locked controller to the callback function

This correctly fixes the issue mentioned in a previous review comment about the locked controller being discarded.

🧹 Nitpick comments (2)
internal/controller/system/state_tracker.go (2)

40-40: Consider creating a technical debt issue for this TODO

This TODO comment indicates that some code should be removed in a later version. Consider creating a technical debt issue to ensure this doesn't get forgotten.


19-51: Consider removing transaction handling from handleState in the future

While the Import method has been updated to be non-transactional, the handleState method still uses both transactions and the new locking mechanism. Consider whether you should fully migrate to the advisory locking approach for consistency, or document why the hybrid approach is needed.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b3a560a and b024123.

📒 Files selected for processing (3)
  • internal/controller/ledger/controller_with_events.go (1 hunks)
  • internal/controller/ledger/controller_with_traces.go (3 hunks)
  • internal/controller/system/state_tracker.go (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • internal/controller/ledger/controller_with_traces.go
  • internal/controller/ledger/controller_with_events.go
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Tests
  • GitHub Check: Dirty
🔇 Additional comments (2)
internal/controller/system/state_tracker.go (2)

32-34: Updated lock handling approach

The refactoring now uses the new withLock helper function to handle ledger locking in a centralized way. This makes the code more maintainable and ensures consistent lock handling across operations.


133-147: Import now uses advisory locking instead of transactions

The Import method has been refactored to use advisory locking instead of SQL transactions, aligning with the PR objective to make imports non-transactional. The code now:

  1. Acquires a lock using the withLock helper
  2. Reloads the ledger state after acquiring the lock
  3. Verifies the ledger is still in the initializing state
  4. Calls the underlying import functionality

This implementation correctly ensures ledger state consistency without requiring a single transaction for the entire import process.

@gfyrag gfyrag force-pushed the fix/import-not-transactional branch from 4c1c0b6 to 43a45d8 Compare April 30, 2025 15:45
@gfyrag gfyrag enabled auto-merge April 30, 2025 15:45
@gfyrag gfyrag added this pull request to the merge queue Apr 30, 2025
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: 1

🧹 Nitpick comments (2)
internal/api/v2/controllers_logs_import.go (2)

28-29: Close the request body as early as possible.

http.Server will eventually close r.Body, but adding an explicit
defer r.Body.Close() documents intent and releases the socket immediately
when the handler finishes.

 	dec := json.NewDecoder(r.Body)
+	defer r.Body.Close()

40-45: Avoid continuing the loop after io.EOF to spare one needless select round.

Once the decoder hits io.EOF the channel has been closed and no further
message can be produced, so jumping straight to the select adds a tiny bit of
unnecessary work and complicates reasoning about the value of l.

-			if errors.Is(err, io.EOF) {
-				close(stream)
-				stream = nil
-			} else {
+			if errors.Is(err, io.EOF) {
+				close(stream)
+				// Nothing left to decode, wait for import result.
+				// Jump directly to the select below.
+			} else {
 				common.InternalServerError(w, r, fmt.Errorf("reading input stream: %w", err))
 				return
 			}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b024123 and 43a45d8.

⛔ Files ignored due to path filters (1)
  • .coderabbit.yaml is excluded by !**/*.yaml
📒 Files selected for processing (20)
  • internal/api/bulking/mocks_ledger_controller_test.go (1 hunks)
  • internal/api/common/middleware_resolver.go (2 hunks)
  • internal/api/common/mocks_ledger_controller_test.go (1 hunks)
  • internal/api/v1/mocks_ledger_controller_test.go (1 hunks)
  • internal/api/v2/controllers_logs_import.go (3 hunks)
  • internal/api/v2/mocks_ledger_controller_test.go (1 hunks)
  • internal/controller/ledger/controller.go (1 hunks)
  • internal/controller/ledger/controller_default.go (2 hunks)
  • internal/controller/ledger/controller_generated_test.go (1 hunks)
  • internal/controller/ledger/controller_with_cache.go (1 hunks)
  • internal/controller/ledger/controller_with_events.go (1 hunks)
  • internal/controller/ledger/controller_with_too_many_client_handling.go (1 hunks)
  • internal/controller/ledger/controller_with_traces.go (3 hunks)
  • internal/controller/ledger/store.go (1 hunks)
  • internal/controller/ledger/store_generated_test.go (1 hunks)
  • internal/controller/system/state_tracker.go (4 hunks)
  • internal/storage/ledger/adapters.go (1 hunks)
  • internal/storage/ledger/store.go (1 hunks)
  • test/e2e/api_ledgers_import_test.go (3 hunks)
  • test/e2e/app_lifecycle_test.go (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • internal/api/common/middleware_resolver.go
🚧 Files skipped from review as they are similar to previous changes (18)
  • internal/controller/ledger/store.go
  • test/e2e/app_lifecycle_test.go
  • internal/api/v1/mocks_ledger_controller_test.go
  • internal/controller/ledger/controller_with_too_many_client_handling.go
  • internal/controller/ledger/controller_with_events.go
  • internal/controller/ledger/controller_with_cache.go
  • internal/controller/ledger/controller.go
  • internal/controller/ledger/controller_with_traces.go
  • internal/controller/ledger/controller_default.go
  • test/e2e/api_ledgers_import_test.go
  • internal/ 8000 api/v2/mocks_ledger_controller_test.go
  • internal/controller/ledger/controller_generated_test.go
  • internal/api/bulking/mocks_ledger_controller_test.go
  • internal/storage/ledger/adapters.go
  • internal/api/common/mocks_ledger_controller_test.go
  • internal/controller/ledger/store_generated_test.go
  • internal/controller/system/state_tracker.go
  • internal/storage/ledger/store.go
🧰 Additional context used
🧬 Code Graph Analysis (1)
internal/api/v2/controllers_logs_import.go (1)
internal/api/common/errors.go (1)
  • InternalServerError (40-44)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Dirty
  • GitHub Check: Tests
🔇 Additional comments (1)
internal/api/v2/controllers_logs_import.go (1)

22-27: 🛠️ Refactor suggestion

Ensure the goroutine cannot leak by closing errChan.

errChan is created with a buffer of one, so the main loop will read at most a single value.
If, for any unforeseen reason, the main handler returns before reading from the
channel (e.g. an early-return path is added later), the goroutine will block
forever on errChan <- err.
Closing the channel after the send protects against that class of leak.

 	go func() {
 		err := common.LedgerFromContext(r.Context()).Import(r.Context(), stream)
 		if err != nil {
 			err = fmt.Errorf("importing logs: %w", err)
 		}
-		errChan <- err
+		errChan <- err
+		close(errChan)
 	}()

Likely an incorrect or invalid review comment.

Comment on lines +55 to +61
if err != nil {
handleError(err)
return
}
if stream != nil {
panic("got nil error while not at the end of the stream")
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Replace server-crashing panic with graceful error handling.

A programming error here would currently bring down the entire server.
Return a 500 and log the inconsistency instead of panicking.

-			if stream != nil {
-				panic("got nil error while not at the end of the stream")
-			}
+			if stream != nil {
+				common.InternalServerError(
+					w, r,
+					fmt.Errorf("no error from import while stream still open – possible logic bug"),
+				)
+				return
+			}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if err != nil {
handleError(err)
return
}
if stream != nil {
panic("got nil error while not at the end of the stream")
}
if err != nil {
handleError(err)
return
}
if stream != nil {
common.InternalServerError(
w, r,
fmt.Errorf("no error from import while stream still open – possible logic bug"),
)
return
}
🤖 Prompt for AI Agents (early access)
In internal/api/v2/controllers_logs_import.go around lines 55 to 61, replace the panic call that triggers when stream is not nil but err is nil with graceful error handling. Instead of panicking, log the inconsistency and return a 500 Internal Server Error response to avoid crashing the server. Ensure the error is properly logged and the client receives an appropriate error status.

@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 30, 2025
@gfyrag gfyrag added this pull request to the merge queue May 2, 2025
Merged via the queue into main with commit 9d7cdb4 May 2, 2025
8 of 10 checks passed
@gfyrag gfyrag deleted the fix/import-not-transactional branch May 2, 2025 07:52
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.

3 participants
0