-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
channeldb: add optional migration to prune revocation logs #6469
Merged
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
674b0ec
migration30: add related revocation log and lnwallet code
yyforyongyu ebbdf78
migration30: add iterator to assist migration
yyforyongyu 75dbbb5
migration30: add supporting functions to help with unit tests
yyforyongyu 2a06145
migration30: add unit tests for iterator
yyforyongyu 61bff60
migration30: add migration to convert old revocation logs
yyforyongyu 5316fcd
migration30+migtest: add unit tests for migration
yyforyongyu 1832a93
migration30: add benchmark test
yyforyongyu afb2f72
channeldb: add optional meta and migration30
yyforyongyu 87f58a2
multi: add the flag `prune-revocation` to perform the optional migration
yyforyongyu 78a73f9
migration30: cover the case where `v0.15.0` is active
yyforyongyu b391503
migration30: validate migration results before deleting old buckets
yyforyongyu ac6e1a8
channeldb: skip dry run mode for optional migrations
yyforyongyu d391514
docs: add release note for optional migration
yyforyongyu 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 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 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 |
---|---|---|
@@ -1,7 +1,11 @@ | ||
package channeldb | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/lightningnetwork/lnd/kvdb" | ||
"github.com/lightningnetwork/lnd/tlv" | ||
) | ||
|
||
var ( | ||
|
@@ -12,6 +16,10 @@ var ( | |
// dbVersionKey is a boltdb key and it's used for storing/retrieving | ||
// current database version. | ||
dbVersionKey = []byte("dbp") | ||
|
||
// dbVersionKey is a boltdb key and it's used for storing/retrieving | ||
// a list of optional migrations that have been applied. | ||
optionalVersionKey = []byte("ovk") | ||
) | ||
|
||
// Meta structure holds the database meta information. | ||
|
@@ -80,3 +88,92 @@ func putDbVersion(metaBucket kvdb.RwBucket, meta *Meta) error { | |
byteOrder.PutUint32(scratch, meta.DbVersionNumber) | ||
return metaBucket.Put(dbVersionKey, scratch) | ||
} | ||
|
||
// OptionalMeta structure holds the database optional migration information. | ||
type OptionalMeta struct { | ||
// Versions is a set that contains the versions that have been applied. | ||
// When saved to disk, only the indexes are stored. | ||
Versions map[uint64]string | ||
} | ||
|
||
func (om *OptionalMeta) String() string { | ||
s := "" | ||
for index, name := range om.Versions { | ||
s += fmt.Sprintf("%d: %s", index, name) | ||
} | ||
if s == "" { | ||
s = "empty" | ||
} | ||
return s | ||
} | ||
|
||
// fetchOptionalMeta reads the optional meta from the database. | ||
func (d *DB) fetchOptionalMeta() (*OptionalMeta, error) { | ||
om := &OptionalMeta{ | ||
Versions: make(map[uint64]string), | ||
} | ||
|
||
err := kvdb.View(d, func(tx kvdb.RTx) error { | ||
metaBucket := tx.ReadBucket(metaBucket) | ||
if metaBucket == nil { | ||
return ErrMetaNotFound | ||
} | ||
|
||
vBytes := metaBucket.Get(optionalVersionKey) | ||
// Exit early if nothing found. | ||
if vBytes == nil { | ||
return nil | ||
} | ||
|
||
// Read the versions' length. | ||
r := bytes.NewReader(vBytes) | ||
vLen, err := tlv.ReadVarInt(r, &[8]byte{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Write the version index. | ||
for i := uint64(0); i < vLen; i++ { | ||
version, err := tlv.ReadVarInt(r, &[8]byte{}) | ||
if err != nil { | ||
return err | ||
} | ||
om.Versions[version] = optionalVersions[i].name | ||
} | ||
|
||
return nil | ||
}, func() {}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even though we don't know of people running nodes on etcd if they do, it's safer if we reset any external state properly, like in this current block we update the |
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return om, nil | ||
} | ||
|
||
// fetchOptionalMeta writes an optional meta to the database. | ||
func (d *DB) putOptionalMeta(om *OptionalMeta) error { | ||
return kvdb.Update(d, func(tx kvdb.RwTx) error { | ||
metaBucket, err := tx.CreateTopLevelBucket(metaBucket) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var b bytes.Buffer | ||
|
||
// Write the total length. | ||
err = tlv.WriteVarInt(&b, uint64(len(om.Versions)), &[8]byte{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Write the version indexes. | ||
for v := range om.Versions { | ||
err := tlv.WriteVarInt(&b, v, &[8]byte{}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return metaBucket.Put(optionalVersionKey, b.Bytes()) | ||
}, func() {}) | ||
} |
This file contains 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.
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.
So we'd thread through that quit channel here to make the migration resumable.
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.
So I guess to achieve this, we'll pass the
ctx
used inBuildDatabase
throughBuildDatabase -> CreateWithBackend -> applyOptionalVersions -> version.migration
and catch the cancel signal there?