|
| 1 | +package migration34 |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/lightningnetwork/lnd/kvdb" |
| 8 | +) |
| 9 | + |
| 10 | +// Migration34 is an optional migration that garbage collects the decayed log |
| 11 | +// in particular the `batch-replay` bucket. However we did choose to use an |
| 12 | +// optional migration which defaults to true because the decayed log db is |
| 13 | +// separate from the channeldb and if we would have implemented it as a |
| 14 | +// required migration, then it would have required a bigger change to the |
| 15 | +// codebase. |
| 16 | +// |
| 17 | +// Most of the decayed log db will shrink significantly after this migration |
| 18 | +// because the other bucket called `shared-secrets` is garbage collected |
| 19 | +// continuously and the `batch-replay` bucket will be deleted. |
| 20 | + |
| 21 | +var ( |
| 22 | + // batchReplayBucket is a bucket that maps batch identifiers to |
| 23 | + // serialized ReplaySets. This is used to give idempotency in the event |
| 24 | + // that a batch is processed more than once. |
| 25 | + batchReplayBucket = []byte("batch-replay") |
| 26 | +) |
| 27 | + |
| 28 | +// MigrationConfig is the interface for the migration configuration. |
| 29 | +type MigrationConfig interface { |
| 30 | + GetDecayedLog() kvdb.Backend |
| 31 | +} |
| 32 | + |
| 33 | +// MigrationConfigImpl is the implementation of the migration configuration. |
| 34 | +type MigrationConfigImpl struct { |
| 35 | + DecayedLog kvdb.Backend |
| 36 | +} |
| 37 | + |
| 38 | +// GetDecayedLog returns the decayed log backend. |
| 39 | +func (c *MigrationConfigImpl) GetDecayedLog() kvdb.Backend { |
| 40 | + return c.DecayedLog |
| 41 | +} |
| 42 | + |
| 43 | +// MigrateDecayedLog migrates the decayed log. The migration deletes the |
| 44 | +// `batch-replay` bucket, which is no longer used. |
| 45 | +// |
| 46 | +// NOTE: This migration is idempotent. If the bucket does not exist, then this |
| 47 | +// migration is a no-op. |
| 48 | +func MigrateDecayedLog(db kvdb.Backend, cfg MigrationConfig) error { |
| 49 | + decayedLog := cfg.GetDecayedLog() |
| 50 | + |
| 51 | + // Make sure we have a reference to the decayed log. |
| 52 | + if decayedLog == nil { |
| 53 | + return fmt.Errorf("decayed log backend is not available") |
| 54 | + } |
| 55 | + |
| 56 | + log.Info("Migrating decayed log...") |
| 57 | + err := decayedLog.Update(func(tx kvdb.RwTx) error { |
| 58 | + err := tx.DeleteTopLevelBucket(batchReplayBucket) |
| 59 | + if err != nil && !errors.Is(err, kvdb.ErrBucketNotFound) { |
| 60 | + return fmt.Errorf("deleting top level bucket %s: %w", |
| 61 | + batchReplayBucket, err) |
| 62 | + } |
| 63 | + |
| 64 | + log.Debugf("top level bucket %s deleted", batchReplayBucket) |
| 65 | + |
| 66 | + return nil |
| 67 | + }, func() {}) |
| 68 | + |
| 69 | + if err != nil { |
| 70 | + return fmt.Errorf("failed to migrate decayed log: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + log.Info("Decayed log migrated successfully") |
| 74 | + |
| 75 | + return nil |
| 76 | +} |
0 commit comments