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

sqldb+kvdb: unify SQL error mapping and transaction retry for between the kvdb and sqldb packages #8611

Merged
merged 11 commits into from
Apr 11, 2024

Conversation

bhandras
Copy link
Collaborator

@bhandras bhandras commented Apr 2, 2024

Previously we had very similar error mapping and retry functions in the affected modules diverging over time. To avoid bugs resulting from individual updates this PR consolidates the retry and error mapping to live in the newly carved out sqldb package.

Based on: #8603
Added commits from : #8529

Copy link
Contributor

coderabbitai bot commented Apr 2, 2024

Important

Auto Review Skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository.

To trigger a single review, invoke the @coderabbitai review command.

Note

Reviews Paused

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Walkthrough

This update focuses on enhancing error handling across various components by adopting %w for error wrapping in fmt.Errorf calls. It improves error context and propagation, ensuring more precise debugging and error management. Additionally, the update includes a dependency version bump and adjustments in database handling logic and module separation for better transaction management.

Changes

Files Summary
.github/workflows/main.yml Updated google.golang.org/grpc version; github.com/golang/protobuf version unchanged.
autopilot/..., brontide/listener.go, cert/selfsigned.go, etc. Updated error message formatting to use %w for error wrapping.
chainntnfs/..., channeldb/..., cmd/lncli/..., etc. Enhanced error reporting by including error type using %w.
docs/release-notes/release-notes-0.18.0.md Documented significant changes in database handling and module separation.
invoices/..., kvdb/..., lnd.go, lnrpc/..., etc. Error handling improvements for better context and propagation.

Possibly related issues

🐇✨
In a land of code and wire,
A rabbit hopped, with an aim so dire.
To wrap errors tight, and versions higher,
Through lines of code, it leaped with fire.
With every hop, bugs retire,
"To improve," it whispers, "is to aspire."
🌟🐾


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:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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.

@bhandras bhandras force-pushed the sql-tx-retry branch 3 times, most recently from 944f6dc to 726c127 Compare April 2, 2024 14:48
@bhandras bhandras changed the title wip: unify SQL error mapping and transaction retry between sqldb and kvdb sqldb+kvdb: unify SQL error mapping and transaction retry between sqldb and kvdb Apr 2, 2024
@bhandras bhandras changed the title sqldb+kvdb: unify SQL error mapping and transaction retry between sqldb and kvdb sqldb+kvdb: unify SQL error mapping and transaction retry for between the kvdb and sqldb packages Apr 2, 2024
@saubyk saubyk added this to the v0.18.0 milestone Apr 2, 2024
@saubyk saubyk requested review from Roasbeef and guggero April 4, 2024 15:19
@bhandras bhandras marked this pull request as ready for review April 4, 2024 15:37
@guggero guggero mentioned this pull request Apr 5, 2024
8 tasks
Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Looks very good! Just a few minor things, I think we can get this merged pretty soon 🎉

sqldb/interfaces.go Outdated Show resolved Hide resolved
kvdb/sqlbase/db.go Show resolved Hide resolved
go.mod Outdated Show resolved Hide resolved
channeldb/payment_control.go Show resolved Hide resolved
@bhandras bhandras requested a review from guggero April 9, 2024 18:36
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: 43

Actionable comments outside the diff hunks (1)
channeldb/graph.go (1)

Line range hint 14-14: TODO: Add tests.

Do you want me to generate the unit testing code or open a GitHub issue to track this task?

cmd/lncli/commands.go Show resolved Hide resolved
channeldb/payment_control_test.go Show resolved Hide resolved
cmd/lncli/walletrpc_active.go Show resolved Hide resolved
channeldb/payment_control_test.go Show resolved Hide resolved
channeldb/payment_control_test.go Show resolved Hide resolved
invoices/sql_store.go Outdated Show resolved Hide resolved
invoices/sql_store.go Outdated Show resolved Hide resolved
invoices/sql_store.go Outdated Show resolved Hide resolved
invoices/sql_store.go Outdated Show resolved Hide resolved
invoices/sql_store.go Outdated Show resolved Hide resolved
kvdb/sqlbase/sqlerrors_sqlite.go Outdated Show resolved Hide resolved
kvdb/sqlbase/db.go Show resolved Hide resolved
kvdb/sqlbase/sqlerrors.go Show resolved Hide resolved
sqldb/interfaces.go Show resolved Hide resolved
invoices/sql_store.go Show resolved Hide resolved
sqldb/sqlc/queries/invoices.sql Outdated Show resolved Hide resolved
sqldb/sqlc/queries/invoices.sql Outdated Show resolved Hide resolved
invoices/sql_store.go Show resolved Hide resolved
@@ -334,6 +334,8 @@ func (t *TransactionExecutor[Q]) ExecTx(ctx context.Context,
if !ok {
return fmt.Errorf("expected *sql.Tx, got %T", tx)
}

reset()
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if missing this partially exacerbated the issues w/ the async payment benchmark?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it's possible, although from my experience most of the issues came from PaymentControl's InitPayment method.

@guggero
Copy link
Collaborator

guggero commented Apr 10, 2024

Can be rebased. And the module sqldb/v1.0.0 tag can now be used instead of the local replace.

@bhandras bhandras force-pushed the sql-tx-retry branch 4 times, most recently from c16dc0c to 1e86c2b Compare April 10, 2024 13:16
@bhandras
Copy link
Collaborator Author

Can be rebased. And the module sqldb/v1.0.0 tag can now be used instead of the local replace.

Can't fully use the tag yet as this PR also changes both sqldb and kvdb. We can tag and update in a follow up though.

@bhandras
Copy link
Collaborator Author

@coderabbitai pause

go.mod Show resolved Hide resolved
kvdb/go.mod Show resolved Hide resolved
kvdb/go.mod Show resolved Hide resolved
bhandras and others added 11 commits April 11, 2024 15:04
Some sub-systems like btcwallet will return an error from the database,
but they won't properly wrap it. As a result, we were unable to actually
catch the serialization errors in the first place. To work around this,
we'll now attempt to parse the error string directly.
With the new postgres concurrency control, an error may come from a
bucket function that's actually a postgres error. In this case, we need
to return early so we can retry the txn. Otherwise, we'll be working
with an aborted tx, and never actually return the error so we don't auto
retry.
In this commit, we fix a bug that would cause the entire db to shutdown
if hit a panic (since db operations in the main buckets exit with a
panic) while executing a txn call back. This might be a postgres error
we need to check, so we don't want to bail out, and instead want to pass
up the error to the caller so we can retry if needed.
For SQL transactions, we often accumulate results in variables declared
outside the closure's scope. To eliminate the need for manually clearing
these containers, we introduce a reset function to ExecTx, mirroring the
approach already adopted in kvdb.
Previously if the `reverse` named arg was unset (value of NULL), then
SQL would order by NULL instead of ID causing undifined ordering of the
returned rows. To fix that we check for NULL and also make sure to set
the `reverse` arg in the code explicitly as it in the generated code it
is an `interface{}` instead of `bool`.
Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

LGTM 🪡

@Roasbeef Roasbeef merged commit 1c229e4 into lightningnetwork:master Apr 11, 2024
25 of 27 checks passed
Roasbeef added a commit to Roasbeef/lnd that referenced this pull request Apr 12, 2024
Post merge module clean up after
lightningnetwork#8611.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
4 participants