Skip to content

invoices: migrate KV invoices to native SQL for users of KV SQL backends#8831

Merged
guggero merged 21 commits intolightningnetwork:masterfrom
bhandras:sql-invoice-migration
Jan 23, 2025
Merged

invoices: migrate KV invoices to native SQL for users of KV SQL backends#8831
guggero merged 21 commits intolightningnetwork:masterfrom
bhandras:sql-invoice-migration

Conversation

@bhandras
Copy link
Collaborator

@bhandras bhandras commented Jun 12, 2024

Change Description

This pull request adds the migration of old key-value (KV) invoices to the new native SQL schema when the --db.use-native-sql flag is set, unless the --db.skip-sql-invoice-migration flag is also specified.

Please note that since we currently do not support running on mixed database backends for users of bbolt or etcd, an additional step is required to migrate their KV database to SQL first. For more context, please see lightninglabs/lndinit#21.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 12, 2024

Important

Review skipped

Auto reviews are limited to specific labels.

🏷️ Labels to auto review (1)
  • llm-review

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.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


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>, 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. (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.

@bhandras bhandras self-assigned this Jun 12, 2024
@bhandras bhandras added database Related to the database/storage of LND migration labels Jun 12, 2024
@bhandras bhandras added this to the 0.19.0 milestone Jun 12, 2024
@bhandras bhandras force-pushed the sql-invoice-migration branch 3 times, most recently from 6682b50 to 338e1f0 Compare June 12, 2024 15:30
@bhandras bhandras force-pushed the sql-invoice-migration branch 3 times, most recently from d2a329f to 6379a8b Compare June 21, 2024 17:37
@bhandras bhandras force-pushed the sql-invoice-migration branch 2 times, most recently from 5fe92e2 to a7bf598 Compare August 14, 2024 09:38
@bhandras bhandras force-pushed the sql-invoice-migration branch 5 times, most recently from b6f0ac8 to b983851 Compare September 17, 2024 14:42
@bhandras bhandras changed the title [wip] invoices: migrate KV invoices to native SQL invoices: migrate KV invoices to native SQL for users of KV SQL backends Sep 17, 2024
@bhandras bhandras force-pushed the sql-invoice-migration branch from b983851 to 706b444 Compare September 17, 2024 14:51
@bhandras bhandras marked this pull request as ready for review September 17, 2024 14:51
@bhandras bhandras force-pushed the sql-invoice-migration branch 7 times, most recently from 96f0cbe to bfe4ad5 Compare September 19, 2024 15:08
return s.BaseDB
}

// ApplyAllMigrations applices both the SQLC and custom in-code migrations to
Copy link
Contributor

Choose a reason for hiding this comment

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

spelling: applices -> applies, and 2 other instances where this function was implemented

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thank you! Fixed.

params := sqlc.InsertInvoiceParams{
Hash: paymentHash[:],
AmountMsat: int64(invoice.Terms.Value),
CltvDelta: sqldb.SQLInt32(
Copy link
Contributor

Choose a reason for hiding this comment

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

Just noticed the comment: // Note: BOLT12 invoices don't have a final cltv delta. is no longer there

// which is a monotonically increasing uint32.
invoiceBucket = []byte("invoices")

// paymentHashIndexBucket is the name of the sub-bucket within the
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment references paymentHashIndexBucket, while the variable is actually named invoiceIndexBucket. Is this discrepancy intentional?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

Copy link
Collaborator

@ziggie1984 ziggie1984 left a comment

Choose a reason for hiding this comment

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

LGTM 🫶, massive effort in this PR, and also shout-out to Elle's great ideas here.

Had some minor comments but nothing blocking.

Would be great to open some follow-up issues for the stuff we want to move into another PR.

-- only run once. It tracks a global database version that encompasses both
-- schema migrations handled by golang-migrate and custom in-code migrations
-- for more complex data conversions that cannot be expressed in pure SQL.
CREATE TABLE IF NOT EXISTS migration_tracker (
Copy link
Collaborator

Choose a reason for hiding this comment

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

does this mean, this migration_tracker table is always greater or equal to the schema migration table of the sql table ? Because the in-code migration will lead to the version being greater but the version in this table cannot be lower than the version that the schema migration correct ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Correct! There's a possibility that we could just rely in Golang Migrate in the future once this PR has landed (if ever): golang-migrate/migrate#932

Comment on lines +65 to +80
// version. If a migration includes a non-nil MigrationFn, it is executed after
// the SQL schema has been migrated to the corresponding schema version.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this still hold true if for every code migration will be done within a separate migration so that we do not have schema-migration and normal version in the same version ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, a migration can be of three types: "schema-only", "schema and in-code" or "in-code-only". If a migration function is defined, the schema version acts as a prerequisite for the migration. This means the schema is always updated to the required version first.

type MigrationExecutor interface {
// CurrentSchemaVersion returns the current schema version of the
// database.
CurrentSchemaVersion() (int, error)
Copy link
Collaborator

Choose a reason for hiding this comment

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

haven't seen a code part where this function is used ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Indeed this is not used anymore. Great catch! Removed.


// ExecuteMigrations runs migrations for the database, depending on the
// target given, either all migrations or up to a given version.
ExecuteMigrations(target MigrationTarget) error
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: Maybe add here in the comment that this function handles the schema and in-code migration, and maybe recommend that migrations schema or in-code should be done separately ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done ptal!

migrationConfig = []MigrationConfig{
{
Name: "000001_invoices",
Version: 1,
Copy link
Collaborator

Choose a reason for hiding this comment

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

does the schema version always correspond to the filename here, meaning that would we start with 000002 in the firt place the database version would start at 2 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes the schema version should always correspond to the filename. This is only used to ensure clean ordering.


offset := uint64(0)
t0 := time.Now()
// Create the hash index which we will use to look up invoice
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit NewLine above.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

Copy link
Collaborator

Choose a reason for hiding this comment

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

would it be difficult to inject in at runtime @bhandras

// disabled, the regular native SQL store migrations will still
// run. If the database version is already above this custom
// migration's version (6), it will be skipped permanently,
// regardless of the flag.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm so if the user disables this, and we add a new migration in the future (version 7) he will not be able to migrate the invoice ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The idea is that users won't disable the invoice migration unless the it fails for them. In that case they can continue using their node and report a bug to us and they should be able to migrate later on.

// testNativeSQLNoMigration tests that nodes that have invoices would not start
// up with native SQL enabled, as we don't currently support migration of KV
// invoices to the new SQL schema.
func testNativeSQLNoMigration(ht *lntest.HarnessTest) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Create an issue for this because this will be done in a followu up ?


if len(htlcs) > 0 {
invoice.Htlcs = htlcs
var amountPaid lnwire.MilliSatoshi
Copy link
Collaborator

Choose a reason for hiding this comment

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

Open an issue to investigate this behavior ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Discussed offline, we'll open the issue soon.

invoice.Htlcs = htlcs
var amountPaid lnwire.MilliSatoshi
for _, htlc := range htlcs {
if htlc.State == HtlcStateSettled {
Copy link
Collaborator

@ziggie1984 ziggie1984 Jan 13, 2025

Choose a reason for hiding this comment

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

I think we just need to adopt it and also add it if the htlc is in the accepted state, rather then removing it?

invoiceStateReady := accepted || settled
if !invoiceIsAMP {
// Update the running amount paid to this invoice. We
// don't include accepted htlcs when the invoice is
// still open.
if invoice.State != ContractOpen &&
invoiceStateReady {
amtPaid += htlc.Amt

or maybe remove it because it might be error pruning and we haven't tested every case.

Copy link
Collaborator

Choose a reason for hiding this comment

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

tested it locally it does work with this change:

		for _, htlc := range htlcs {
			if htlc.State == HtlcStateSettled ||
				htlc.State == HtlcStateAccepted {

				amountPaid += htlc.Amt
			}
		}
		invoice.AmtPaid = amountPaid

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Tested it with go test -v -run=TestMigrationWithChannelDB -tags=kvdb_sqlite and i think the only correct fix given your db is to use the stored amount paid value. Also discussed this offline.

@lightninglabs-deploy
Copy link
Collaborator

@Roasbeef: review reminder
@aakselrod: review reminder
@bhandras, remember to re-request review from reviewers when ready

This commit adds the migration_tracker table which we'll use to track if
a custom migration has already been done.
This commit introduces support for custom, in-code migrations, allowing
a specific Go function to be executed at a designated database version
during sqlc migrations. If the current database version surpasses the
specified version, the migration will be skipped.
This commit separates the execution of SQL and in-code migrations
from their construction. This change is necessary because,
currently, the SQL schema is migrated during the construction
phase in the lncfg package. However, migrations are typically
executed when individual stores are constructed within the
configuration builder.
Previously we intentially did not set settled_at and settle_index when
inserting a new invoice as those fields are set when we settle an
invoice through the usual invoice update. As migration requires that we
set these nullable fields, we can safely add them.
Certain invoices may not have a deterministic payment hash. For such
invoices we still store the payment hashes in our KV database, but we do
not have a sufficient index to retrieve them. This PR adds such index to
the SQL database that will be used during migration to retrieve payment
hashes.
…a hash

The current sqlc GetInvoice query experiences incremental slowdowns during
the migration of large invoice databases, primarily due to its complex
predicate set. For this specific use case, a streamlined GetInvoiceByHash
function provides a more efficient solution, maintaining near-constant
lookup times even with extensive table sizes.
This commit runs the invoice migration if the user has a KV SQL backend
configured.
Previously we'd recalculate the paid amount by summing amounts of
settled HTLCs. This approach while correct would stop the SQL migration
process as some KV invoices may have incorrectly stored paid amounts.
Previously, we applied replacements to our schema definitions
to make them compatible with both SQLite and Postgres backends,
as the files were not fully compatible with either.

With this change, the only replacement required for SQLite has
been moved to the generator script. This adjustment ensures
compatibility by enabling auto-incrementing primary keys that
are treated as 64-bit integers by sqlc.
@guggero
Copy link
Collaborator

guggero commented Jan 23, 2025

The "Check commits" CI step succeeded locally, probably just too many commits for GitHub.

@ellemouton
Copy link
Collaborator

reminder that we can remove this replace now:

lnd/go.mod

Line 210 in e403243

// Temporary replace until https://github.com/lightningnetwork/lnd/pull/8831 is

@bhandras
Copy link
Collaborator Author

bhandras commented Feb 1, 2025

reminder that we can remove this replace now:

lnd/go.mod

Line 210 in e403243

// Temporary replace until https://github.com/lightningnetwork/lnd/pull/8831 is

Thanks for the reminder, ptal #9469

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

Labels

database Related to the database/storage of LND migration

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

9 participants