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

chanbackup: archive old channel backup files #9232

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Abdulkbk
Copy link
Contributor

@Abdulkbk Abdulkbk commented Oct 29, 2024

Closes #8906

Change Description

From the feature description:

To ensure that certain classes of recovery are always possible, we should considering adding a mode to never delete channel backups, and only rename or move them to a special folder that stores archived channels. They're relatively small (~300 bytes or so), so it shouldn't take up a significant amount of disk space.

This PR allows for the archiving of channel backups, enabling users to choose whether to permanently delete previous backup files or archive them by moving them to a designated archives folder.

Problem:
LND currently overwrites old channel backup files when creating new ones. This means:

  • If a user accidentally deletes a channel backup file, the backup is irretrievably lost.
  • If a newly created backup file becomes corrupted and no older backups are preserved, recovering the channel through the file becomes impossible.
  • In certain cases, users may want to compare older and newer backups to verify that the evolution of the channel states was valid.

Solution:
We modify the current flow of creating channel.backup. We start by creating a folder in the same directory where we store the channel.backup file (chan-backup-archives). We ensure that the channel.backup file is copied to the archive directory and timestamped before finally replacing it with the new file.

We set the LND's default behavior to archive old channel backups, but we provide a configuration option that can be passed as an argument or specified in lnd.conf to disable this behavior. This option allows LND to delete channel.backup files instead of archiving them. This can be achieved by setting disable-backup-archive=true in lnd.conf or passed as argument:

lnd --disable-backup-archive

Steps to Test

  1. Start LND (make sure disable-backup-archive=true is not set in lnd.conf):
lnd
  1. Trigger the channel backup process. This can be achieved by generating some blocks. If you're using BTCD backend:
btcctl generate 6
  1. Stop LND and check the directory where the channel.backup file is stored. You will find a new folder called chan-backup-archives, where the backup files are archived.

To test disabling this feature

  • Start LND
lnd # if disable-backup-archive=true is set in lnd.conf

# or

lnd --disable-backup-archive # pass as arg
  • Repeat 2 & 3. This time the chan-backup-archives directory will not be created (if it does not already exist). If it already exists a new archive will not be created.

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

📝 Please see our Contribution Guidelines for further guidance.

Copy link
Contributor

coderabbitai bot commented Oct 29, 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.

@Abdulkbk Abdulkbk marked this pull request as ready for review November 1, 2024 11:49
@Abdulkbk Abdulkbk force-pushed the archive-channel-backups branch from bd7fb87 to 71ee590 Compare November 24, 2024 15:38
@Abdulkbk Abdulkbk force-pushed the archive-channel-backups branch from 71ee590 to 375e0fb Compare December 22, 2024 16:46
@lightninglabs-deploy
Copy link

@Abdulkbk, remember to re-request review from reviewers when ready

@MPins
Copy link
Contributor

MPins commented Jan 6, 2025

LGTM 👌

chanbackup/backupfile_test.go Show resolved Hide resolved
@Abdulkbk Abdulkbk changed the title chanbackup: archive old channel backups chanbackup: archive old channel backup files Jan 7, 2025
@yyforyongyu
Copy link
Member

Could you do a rebase and push so the CI can run again, wanna check what's going in the logs but they are expired.

@Abdulkbk Abdulkbk force-pushed the archive-channel-backups branch from 375e0fb to ade9d50 Compare January 7, 2025 04:13
@Abdulkbk
Copy link
Contributor Author

Abdulkbk commented Jan 7, 2025

I've rebased and pushed
@yyforyongyu

@yyforyongyu yyforyongyu added this to the v0.19.0 milestone Jan 8, 2025
@yyforyongyu yyforyongyu added enhancement Improvements to existing features / behaviour SCB Related to static channel backup size/kilo medium, proper context needed, less than 1000 lines labels Jan 8, 2025
@@ -147,3 +173,48 @@ func (b *MultiFile) ExtractMulti(keyChain keychain.KeyRing) (*Multi, error) {
packedMulti := PackedMulti(multiBytes)
return packedMulti.Unpack(keyChain)
}

func createArchiveFile(archiveDir string, fileName string) error {
Copy link
Member

Choose a reason for hiding this comment

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

nit: missing godocs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

wow, nice catch 👍

}

return nil
}
Copy link
Member

Choose a reason for hiding this comment

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

think ade9d50 should be squashed into this commit instead since it's testing the newly introduced method createArchiveFile.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense

@@ -17,6 +19,10 @@ const (
// file that we'll use to atomically update the primary back up file
// when new channel are detected.
DefaultTempBackupFileName = "temp-dont-use.backup"

// DefaultChanBackupDirName is the default name of the directory that
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// DefaultChanBackupDirName is the default name of the directory that
// DefaultChanBackupArchiveDirName is the default name of the directory that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed


// First, we'll create a temporary directory for our test files.
tempDir := t.TempDir()
archiveDir := filepath.Join(tempDir, "chan-backup-archives")
Copy link
Member

Choose a reason for hiding this comment

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

use the var DefaultChanBackupArchiveDirName?

archiveDir := filepath.Join(tempDir, "chan-backup-archives")

// Next, we'll create a test backup file and write some content to it.
backupFile := filepath.Join(tempDir, "channel.backup")
Copy link
Member

Choose a reason for hiding this comment

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

use the var DefaultBackupFileName

@@ -117,6 +130,19 @@ func (b *MultiFile) UpdateAndSwap(newBackup PackedMulti) error {
return fmt.Errorf("unable to close file: %w", err)
}

// We check if the main backup file exists, if it does we archive it
// before replacing it with the new backup.
if _, err := os.Stat(b.fileName); err == nil {
Copy link
Member

Choose a reason for hiding this comment

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

Q: should we return the error here instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we return an error here, LND will fail to start during the first run because channel.backup does not exist then. Should we add a log here instead?

I will also update the comment to capture this.

@@ -360,6 +366,8 @@ type Config struct {
MaxPendingChannels int `long:"maxpendingchannels" description:"The maximum number of incoming pending channels permitted per peer."`
BackupFilePath string `long:"backupfilepath" description:"The target location of the channel backup file"`

DisableBackupArchive bool `long:"disable-backup-archive" description:"If set to true, channel backups will be deleted or replaced rather than being archived to a separate location."`
Copy link
Member

Choose a reason for hiding this comment

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

think it'd be better if we could name it ArchiveBackup - then we don't need to use a double negative to decide whether we wanna create this archive or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem with this approach is when you try to disable this feature (since its enabled by default) using a CLI arg. Running the command lnd --archivebackup=false results in an error although setting archivebackup=false in the lnd.conf file works fine.
imageedit_0_3450861008

I think the alternative is to disable this feature by default and then the user can then run lnd --archivebackup to enable it without getting any errors. In this case there won't be any need for lnd --archivebackup=false

@@ -74,6 +74,9 @@
This is a protocol gadget required for Dynamic Commitments and Splicing that
will be added later.

* Add support for [archiving channel backup](https://github.com/lightningnetwork/lnd/pull/9232)
in a designated folder which allows for easy referencing in the future.
Copy link
Member

Choose a reason for hiding this comment

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

should also mention the newly introduced config value here.

@@ -117,6 +134,21 @@ func (b *MultiFile) UpdateAndSwap(newBackup PackedMulti) error {
return fmt.Errorf("unable to close file: %w", err)
}

if !b.deleteOldBackup {
Copy link
Member

Choose a reason for hiding this comment

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

should also update the docs in UpdateAndSwap about this new behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done 👍

@@ -94,7 +94,7 @@ func TestUpdateAndSwap(t *testing.T) {
},
}
for i, testCase := range testCases {
backupFile := NewMultiFile(testCase.fileName)
backupFile := NewMultiFile(testCase.fileName, false)
Copy link
Member

Choose a reason for hiding this comment

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

think this test should also be updated to check the new behavior, maybe a new test TestUpdateAndSwapWithArchive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The test is in the next commit ade9d50, I will squash it into the first commit.

In this commit, we first check if a previous backup file exists,
if it does we copy it to archive folder before replacing it with
a new backup file.
In this commit, we add the --disable-backup-archive with a default
as false. When set to true then previous channel backup file will
not be archived but replaced.
@Abdulkbk Abdulkbk force-pushed the archive-channel-backups branch from 7dd1be4 to c5a1f13 Compare January 13, 2025 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvements to existing features / behaviour SCB Related to static channel backup size/kilo medium, proper context needed, less than 1000 lines
Projects
None yet
Development

Successfully merging this pull request may close these issues.

chanbackup/[feature]: add archive mode for channel back ups
4 participants