-
Notifications
You must be signed in to change notification settings - Fork 2.3k
channeldb: split channel close into two phases to avoid long DB write-locks #10732
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
Draft
ziggie1984
wants to merge
6
commits into
lightningnetwork:master
Choose a base branch
from
ziggie1984:channeldb-two-phase-close
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
23d0a02
channeldb: introduce ChannelCloser interface for two-phase cleanup
ziggie1984 a8f04cb
channeldb: split CloseChannel into two phases to reduce DB lock pressure
ziggie1984 072d977
channeldb: harden two-phase close summary handling
ziggie1984 9f10631
contractcourt: manage closed-channel Phase 2 cleanup
ziggie1984 ccb369e
server: resume closed-channel cleanup at startup
ziggie1984 383587a
docs: add release note for two-phase channel close
ziggie1984 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package channeldb | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/btcsuite/btcd/wire" | ||
| ) | ||
|
|
||
| // ChannelCloser manages the lifecycle of channel closure and the cleanup of | ||
| // associated bulk historical data. The interface is intentionally backend- | ||
| // agnostic: the current KV implementation on *ChannelStateDB satisfies it, | ||
| // and a future native-SQL implementation will provide a different concrete | ||
| // type that satisfies the same contract without any change to callers. | ||
| // | ||
| // The close operation is deliberately split into two phases to prevent a | ||
| // single large DB transaction from holding the write lock for an unbounded | ||
| // amount of time (which is especially harmful on SQLite and Postgres backends, | ||
| // where bulk cascade-deletes inside one transaction can block all other | ||
| // writers for seconds): | ||
| // | ||
| // - CloseChannel performs a fast, atomic Phase 1: it records the close | ||
| // summary, archives the channel state, updates the outpoint index, and | ||
| // deletes the small per-channel keys — but intentionally leaves the bulk | ||
| // historical data (revocation log, forwarding packages) in place and | ||
| // registers a cleanup task for them. | ||
| // | ||
| // - PurgeClosedChannelData performs the heavy Phase 2 across many small | ||
| // transactions, deleting up to batchSize entries per call so that other | ||
| // writers can interleave. It is idempotent and crash-safe: the cleanup | ||
| // task registered by CloseChannel persists across restarts and is resumed | ||
| // via FetchChannelsPendingCleanup. | ||
| type ChannelCloser interface { | ||
| // CloseChannel atomically records the channel as closed. It writes | ||
| // the close summary to the closed-channel bucket, archives the full | ||
| // channel state to the historical bucket, updates the outpoint index, | ||
| // deletes the small per-channel state keys, and registers a cleanup | ||
| // task for the bulk historical data that is too large to delete here. | ||
| // | ||
| // NOTE: summary.RemotePub and summary.ChainHash must be populated so | ||
| // that the KV implementation can navigate to the channel bucket. | ||
| CloseChannel(ctx context.Context, chanPoint wire.OutPoint, | ||
| summary *ChannelCloseSummary, | ||
| statuses ...ChannelStatus) error | ||
|
|
||
| // PurgeClosedChannelData removes up to batchSize entries of historical | ||
| // data for a closed channel (revocation log entries, forwarding | ||
| // packages). Each call executes in one or more small transactions. | ||
| // Returns done=true once all bulk data has been removed and the cleanup | ||
| // task has been deregistered. Safe to call multiple times; idempotent | ||
| // once done=true has been returned. | ||
| PurgeClosedChannelData(ctx context.Context, chanPoint wire.OutPoint, | ||
| batchSize int) (bool, error) | ||
|
|
||
| // FetchChannelsPendingCleanup returns the outpoints of channels that | ||
| // have been closed (Phase 1 complete) but whose bulk historical data | ||
| // has not yet been fully deleted (Phase 2 incomplete). Used at startup | ||
| // to resume any purges that were interrupted by a crash or restart. | ||
| FetchChannelsPendingCleanup(ctx context.Context) ([]wire.OutPoint, | ||
| error) | ||
| } | ||
|
|
||
| // Ensure the current KV-backed channel state store satisfies the backend- | ||
| // agnostic close/cleanup contract. | ||
| var _ ChannelCloser = (*ChannelStateDB)(nil) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment for
ErrChannelPendingCleanupis quite long and could be more concise to improve readability while still explaining the purpose.