mirror of
https://github.com/lightningnetwork/lnd.git
synced 2024-11-19 01:43:16 +01:00
multi: pass in a MigrationConfig to all optional migrations
Define a MigrationConfig interface that should be used to pass the config of an optional migration function. An implementation of this interface is added for migration30 (the only current optional migration).
This commit is contained in:
parent
33b7b9221a
commit
70e3f8f511
@ -63,12 +63,24 @@ type mandatoryVersion struct {
|
||||
migration migration
|
||||
}
|
||||
|
||||
// MigrationConfig is an interface combines the config interfaces of all
|
||||
// optional migrations.
|
||||
type MigrationConfig interface {
|
||||
migration30.MigrateRevLogConfig
|
||||
}
|
||||
|
||||
// MigrationConfigImpl is a super set of all the various migration configs and
|
||||
// an implementation of MigrationConfig.
|
||||
type MigrationConfigImpl struct {
|
||||
migration30.MigrateRevLogConfigImpl
|
||||
}
|
||||
|
||||
// optionalMigration defines an optional migration function. When a migration
|
||||
// is optional, it usually involves a large scale of changes that might touch
|
||||
// millions of keys. Due to OOM concern, the update cannot be safely done
|
||||
// within one db transaction. Thus, for optional migrations, they must take the
|
||||
// db backend and construct transactions as needed.
|
||||
type optionalMigration func(db kvdb.Backend) error
|
||||
type optionalMigration func(db kvdb.Backend, cfg MigrationConfig) error
|
||||
|
||||
// optionalVersion defines a db version that can be optionally applied. When
|
||||
// applying migrations, we must apply all the mandatory migrations first before
|
||||
@ -273,8 +285,12 @@ var (
|
||||
// to determine its state.
|
||||
optionalVersions = []optionalVersion{
|
||||
{
|
||||
name: "prune revocation log",
|
||||
migration: migration30.MigrateRevocationLog,
|
||||
name: "prune revocation log",
|
||||
migration: func(db kvdb.Backend,
|
||||
cfg MigrationConfig) error {
|
||||
|
||||
return migration30.MigrateRevocationLog(db, cfg)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@ -1545,8 +1561,12 @@ func (d *DB) applyOptionalVersions(cfg OptionalMiragtionConfig) error {
|
||||
version := optionalVersions[0]
|
||||
log.Infof("Performing database optional migration: %s", version.name)
|
||||
|
||||
migrationCfg := &MigrationConfigImpl{
|
||||
migration30.MigrateRevLogConfigImpl{},
|
||||
}
|
||||
|
||||
// Migrate the data.
|
||||
if err := version.migration(d); err != nil {
|
||||
if err := version.migration(d, migrationCfg); err != nil {
|
||||
log.Errorf("Unable to apply optional migration: %s, error: %v",
|
||||
version.name, err)
|
||||
return err
|
||||
|
@ -530,7 +530,9 @@ func TestApplyOptionalVersions(t *testing.T) {
|
||||
// Overwrite the migration function so we can count how many times the
|
||||
// migration has happened.
|
||||
migrateCount := 0
|
||||
optionalVersions[0].migration = func(_ kvdb.Backend) error {
|
||||
optionalVersions[0].migration = func(_ kvdb.Backend,
|
||||
_ MigrationConfig) error {
|
||||
|
||||
migrateCount++
|
||||
return nil
|
||||
}
|
||||
|
@ -23,10 +23,17 @@ import (
|
||||
// indexes.
|
||||
const recordsPerTx = 20_000
|
||||
|
||||
// MigrateRevLogConfig is an interface that defines the config that should be
|
||||
// passed to the MigrateRevocationLog function.
|
||||
type MigrateRevLogConfig interface{}
|
||||
|
||||
// MigrateRevLogConfigImpl implements the MigrationRevLogConfig interface.
|
||||
type MigrateRevLogConfigImpl struct{}
|
||||
|
||||
// MigrateRevocationLog migrates the old revocation logs into the newer format
|
||||
// and deletes them once finished, with the deletion only happens once ALL the
|
||||
// old logs have been migrates.
|
||||
func MigrateRevocationLog(db kvdb.Backend) error {
|
||||
func MigrateRevocationLog(db kvdb.Backend, _ MigrateRevLogConfig) error {
|
||||
log.Infof("Migrating revocation logs, might take a while...")
|
||||
|
||||
var (
|
||||
|
@ -103,11 +103,15 @@ func TestMigrateRevocationLog(t *testing.T) {
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg := &MigrateRevLogConfigImpl{}
|
||||
|
||||
migtest.ApplyMigrationWithDB(
|
||||
t,
|
||||
beforeMigration,
|
||||
afterMigration,
|
||||
MigrateRevocationLog,
|
||||
func(db kvdb.Backend) error {
|
||||
return MigrateRevocationLog(db, cfg)
|
||||
},
|
||||
false,
|
||||
)
|
||||
})
|
||||
@ -559,6 +563,8 @@ func BenchmarkMigration(b *testing.B) {
|
||||
return setupTestLogs(db, c, oldLogs, nil)
|
||||
}
|
||||
|
||||
cfg := &MigrateRevLogConfigImpl{}
|
||||
|
||||
// Run the migration test.
|
||||
migtest.ApplyMigrationWithDB(
|
||||
b,
|
||||
@ -568,7 +574,7 @@ func BenchmarkMigration(b *testing.B) {
|
||||
b.StartTimer()
|
||||
defer b.StopTimer()
|
||||
|
||||
return MigrateRevocationLog(db)
|
||||
return MigrateRevocationLog(db, cfg)
|
||||
},
|
||||
false,
|
||||
)
|
||||
|
@ -89,7 +89,8 @@ func ApplyMigration(t *testing.T,
|
||||
// function. This function differs from ApplyMigration as it requires the
|
||||
// supplied migration functions to take a db instance and construct their own
|
||||
// database transactions.
|
||||
func ApplyMigrationWithDB(t testing.TB, beforeMigration, afterMigration,
|
||||
func ApplyMigrationWithDB(t testing.TB, beforeMigration,
|
||||
afterMigration func(db kvdb.Backend) error,
|
||||
migrationFunc func(db kvdb.Backend) error, shouldFail bool) {
|
||||
|
||||
t.Helper()
|
||||
|
Loading…
Reference in New Issue
Block a user