Skip to content

Commit d220b87

Browse files
committed
multi: Add decayedlog db migration code
This commit adds the migration code for the decayedlog db which is optional and will default to true.
1 parent c7e7731 commit d220b87

File tree

7 files changed

+62
-5
lines changed

7 files changed

+62
-5
lines changed

channeldb/db.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/lightningnetwork/lnd/channeldb/migration31"
3030
"github.com/lightningnetwork/lnd/channeldb/migration32"
3131
"github.com/lightningnetwork/lnd/channeldb/migration33"
32+
"github.com/lightningnetwork/lnd/channeldb/migration34"
3233
"github.com/lightningnetwork/lnd/channeldb/migration_01_to_11"
3334
"github.com/lightningnetwork/lnd/clock"
3435
graphdb "github.com/lightningnetwork/lnd/graph/db"
@@ -74,12 +75,14 @@ type mandatoryVersion struct {
7475
// optional migrations.
7576
type MigrationConfig interface {
7677
migration30.MigrateRevLogConfig
78+
migration34.MigrationConfig
7779
}
7880

7981
// MigrationConfigImpl is a super set of all the various migration configs and
8082
// an implementation of MigrationConfig.
8183
type MigrationConfigImpl struct {
8284
migration30.MigrateRevLogConfigImpl
85+
migration34.MigrationConfigImpl
8386
}
8487

8588
// optionalMigration defines an optional migration function. When a migration
@@ -315,6 +318,16 @@ var (
315318
return migration30.MigrateRevocationLog(db, cfg)
316319
},
317320
},
321+
{
322+
name: "gc_decayed_log",
323+
migration: func(db kvdb.Backend,
324+
cfg MigrationConfig) error {
325+
326+
return migration34.MigrateDecayedLog(
327+
db, cfg,
328+
)
329+
},
330+
},
318331
}
319332

320333
// Big endian is the preferred byte order, due to cursor scans over
@@ -1731,10 +1744,8 @@ func (d *DB) syncVersions(versions []mandatoryVersion) error {
17311744
}, func() {})
17321745
}
17331746

1734-
// applyOptionalVersions takes a config to determine whether the optional
1735-
// migrations will be applied.
1736-
//
1737-
// NOTE: only support the prune_revocation_log optional migration atm.
1747+
// applyOptionalVersions applies the optional migrations to the database if
1748+
// specified in the config.
17381749
func (d *DB) applyOptionalVersions(cfg OptionalMiragtionConfig) error {
17391750
// TODO(yy): need to design the db to support dry run for optional
17401751
// migrations.
@@ -1762,6 +1773,9 @@ func (d *DB) applyOptionalVersions(cfg OptionalMiragtionConfig) error {
17621773
migration30.MigrateRevLogConfigImpl{
17631774
NoAmountData: d.noRevLogAmtData,
17641775
},
1776+
migration34.MigrationConfigImpl{
1777+
DecayedLog: cfg.DecayedLog,
1778+
},
17651779
}
17661780

17671781
log.Infof("Applying %d optional migrations", len(optionalVersions))

channeldb/log.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/lightningnetwork/lnd/channeldb/migration31"
1313
"github.com/lightningnetwork/lnd/channeldb/migration32"
1414
"github.com/lightningnetwork/lnd/channeldb/migration33"
15+
"github.com/lightningnetwork/lnd/channeldb/migration34"
1516
"github.com/lightningnetwork/lnd/channeldb/migration_01_to_11"
1617
"github.com/lightningnetwork/lnd/kvdb"
1718
)
@@ -46,5 +47,6 @@ func UseLogger(logger btclog.Logger) {
4647
migration31.UseLogger(logger)
4748
migration32.UseLogger(logger)
4849
migration33.UseLogger(logger)
50+
migration34.UseLogger(logger)
4951
kvdb.UseLogger(logger)
5052
}

channeldb/meta_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ func TestOptionalMeta(t *testing.T) {
498498
om = &OptionalMeta{
499499
Versions: map[uint64]string{
500500
0: optionalVersions[0].name,
501+
1: optionalVersions[1].name,
501502
},
502503
}
503504
err = db.putOptionalMeta(om)
@@ -506,7 +507,10 @@ func TestOptionalMeta(t *testing.T) {
506507
om1, err := db.fetchOptionalMeta()
507508
require.NoError(t, err, "error getting optional meta")
508509
require.Equal(t, om, om1, "unexpected empty versions")
509-
require.Equal(t, "0: prune_revocation_log", om.String())
510+
require.Equal(
511+
t, "0: prune_revocation_log, 1: gc_decayed_log",
512+
om1.String(),
513+
)
510514
}
511515

512516
// TestApplyOptionalVersions checks that the optional migration is applied as
@@ -565,6 +569,7 @@ func TestApplyOptionalVersions(t *testing.T) {
565569
omExpected := &OptionalMeta{
566570
Versions: map[uint64]string{
567571
0: optionalVersions[0].name,
572+
1: optionalVersions[1].name,
568573
},
569574
}
570575
require.Equal(t, omExpected, om, "unexpected empty versions")

channeldb/options.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package channeldb
22

33
import (
44
"github.com/lightningnetwork/lnd/clock"
5+
"github.com/lightningnetwork/lnd/kvdb"
56
)
67

78
const (
@@ -29,6 +30,14 @@ type OptionalMiragtionConfig struct {
2930
// migrations should be run. The index in the array corresponds to the
3031
// migration number in optionalVersions.
3132
MigrationFlags []bool
33+
34+
// DecayedLog is a reference to the decayed log database. The channeldb
35+
// is inherently part of the optional migration flow so there is no need
36+
// to specify it here. The DecayedLog is a separate database in case the
37+
// kvdb backend is set to `bbolt`. And also for the kvdb SQL backend
38+
// case it is a separate table therefore we need to reference it here
39+
// as well to use the right query to access the decayed log.
40+
DecayedLog kvdb.Backend
3241
}
3342

3443
// NewOptionalMiragtionConfig creates a new OptionalMiragtionConfig with the
@@ -136,3 +145,21 @@ func OptionPruneRevocationLog(prune bool) OptionModifier {
136145
o.OptionalMiragtionConfig.MigrationFlags[0] = prune
137146
}
138147
}
148+
149+
// OptionWithDecayedLogDB sets the decayed log database reference which might
150+
// be used for some migrations because generally we only touch the channeldb
151+
// databases in the migrations, this is a way to allow also access to the
152+
// decayed log database.
153+
func OptionWithDecayedLogDB(decayedLog kvdb.Backend) OptionModifier {
154+
return func(o *Options) {
155+
o.OptionalMiragtionConfig.DecayedLog = decayedLog
156+
}
157+
}
158+
159+
// OptionGcDecayedLog specifies whether the decayed log migration has to
160+
// take place.
161+
func OptionGcDecayedLog(noGc bool) OptionModifier {
162+
return func(o *Options) {
163+
o.OptionalMiragtionConfig.MigrationFlags[1] = !noGc
164+
}
165+
}

config_builder.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,8 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
10731073
),
10741074
channeldb.OptionPruneRevocationLog(cfg.DB.PruneRevocation),
10751075
channeldb.OptionNoRevLogAmtData(cfg.DB.NoRevLogAmtData),
1076+
channeldb.OptionGcDecayedLog(cfg.DB.NoGcDecayedLog),
1077+
channeldb.OptionWithDecayedLogDB(dbs.DecayedLogDB),
10761078
}
10771079

10781080
// Otherwise, we'll open two instances, one for the state we only need

lncfg/db.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ type DB struct {
9595
PruneRevocation bool `long:"prune-revocation" description:"Run the optional migration that prunes the revocation logs to save disk space."`
9696

9797
NoRevLogAmtData bool `long:"no-rev-log-amt-data" description:"If set, the to-local and to-remote output amounts of revoked commitment transactions will not be stored in the revocation log. Note that once this data is lost, a watchtower client will not be able to back up the revoked state."`
98+
99+
NoGcDecayedLog bool `long:"no-gc-decayed-log" description:"Do not run the optional migration that garbage collects the decayed log to save disk space."`
98100
}
99101

100102
// DefaultDB creates and returns a new default DB config.

sample-lnd.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,11 @@
14761476
; channels prior to lnd@v0.15.0.
14771477
; db.prune-revocation=false
14781478

1479+
; Specify whether the optional migration for garbage collecting the decayed
1480+
; sphinx logs should be applied. By default, the decayed log will be garbage
1481+
; collected.
1482+
; db.no-gc-decayed-log=false
1483+
14791484
; If set to true, then the to-local and to-remote output amount data of revoked
14801485
; commitment transactions will not be stored in the revocation log. Note that
14811486
; this flag can only be set if --wtclient.active is not set. It is not

0 commit comments

Comments
 (0)