Skip to content

Commit 1d0234e

Browse files
committed
graph/db: add SQL migration progress logs
1 parent 98fc58b commit 1d0234e

File tree

1 file changed

+98
-3
lines changed

1 file changed

+98
-3
lines changed

graph/db/sql_migration.go

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/lightningnetwork/lnd/lnwire"
1818
"github.com/lightningnetwork/lnd/sqldb"
1919
"github.com/lightningnetwork/lnd/sqldb/sqlc"
20+
"golang.org/x/time/rate"
2021
)
2122

2223
// MigrateGraphToSQL migrates the graph store from a KV backend to a SQL
@@ -115,6 +116,12 @@ func migrateNodes(ctx context.Context, kvBackend kvdb.Backend,
115116
var (
116117
count uint64
117118
skipped uint64
119+
120+
t0 = time.Now()
121+
chunk uint64
122+
s = rate.Sometimes{
123+
Interval: 10 * time.Second,
124+
}
118125
)
119126

120127
// Loop through each node in the KV store and insert it into the SQL
@@ -146,6 +153,7 @@ func migrateNodes(ctx context.Context, kvBackend kvdb.Backend,
146153
}
147154

148155
count++
156+
chunk++
149157

150158
// TODO(elle): At this point, we should check the loaded node
151159
// to see if we should extract any DNS addresses from its
@@ -203,9 +211,25 @@ func migrateNodes(ctx context.Context, kvBackend kvdb.Backend,
203211
},
204212
)
205213

206-
return sqldb.CompareRecords(
214+
err = sqldb.CompareRecords(
207215
node, migratedNode, fmt.Sprintf("node %x", pub),
208216
)
217+
if err != nil {
218+
return fmt.Errorf("node mismatch after migration "+
219+
"for node %x: %w", pub, err)
220+
}
221+
222+
s.Do(func() {
223+
elapsed := time.Since(t0).Seconds()
224+
ratePerSec := float64(chunk) / elapsed
225+
log.Debugf("Migrated %d nodes (%.2f nodes/sec)",
226+
count, ratePerSec)
227+
228+
t0 = time.Now()
229+
chunk = 0
230+
})
231+
232+
return nil
209233
}, func() {
210234
// No reset is needed since if a retry occurs, the entire
211235
// migration will be retried from the start.
@@ -225,6 +249,8 @@ func migrateNodes(ctx context.Context, kvBackend kvdb.Backend,
225249
func migrateSourceNode(ctx context.Context, kvdb kvdb.Backend,
226250
sqlDB SQLQueries) error {
227251

252+
log.Debugf("Migrating source node from KV to SQL")
253+
228254
sourceNode, err := sourceNode(kvdb)
229255
if errors.Is(err, ErrSourceNodeNotSet) {
230256
// If the source node has not been set yet, we can skip this
@@ -302,6 +328,12 @@ func migrateChannelsAndPolicies(ctx context.Context, kvBackend kvdb.Backend,
302328
skippedChanCount uint64
303329
policyCount uint64
304330
skippedPolicyCount uint64
331+
332+
t0 = time.Now()
333+
chunk uint64
334+
s = rate.Sometimes{
335+
Interval: 10 * time.Second,
336+
}
305337
)
306338
migChanPolicy := func(policy *models.ChannelEdgePolicy) error {
307339
// If the policy is nil, we can skip it.
@@ -386,6 +418,16 @@ func migrateChannelsAndPolicies(ctx context.Context, kvBackend kvdb.Backend,
386418
scid, err)
387419
}
388420

421+
s.Do(func() {
422+
elapsed := time.Since(t0).Seconds()
423+
ratePerSec := float64(chunk) / elapsed
424+
log.Debugf("Migrated %d channels (%.2f channels/sec)",
425+
channelCount, ratePerSec)
426+
427+
t0 = time.Now()
428+
chunk = 0
429+
})
430+
389431
return nil
390432
}, func() {
391433
// No reset is needed since if a retry occurs, the entire
@@ -544,6 +586,12 @@ func migratePruneLog(ctx context.Context, kvBackend kvdb.Backend,
544586
count uint64
545587
pruneTipHeight uint32
546588
pruneTipHash chainhash.Hash
589+
590+
t0 = time.Now()
591+
chunk uint64
592+
s = rate.Sometimes{
593+
Interval: 10 * time.Second,
594+
}
547595
)
548596

549597
// migrateSinglePruneEntry is a helper function that inserts a single
@@ -596,6 +644,17 @@ func migratePruneLog(ctx context.Context, kvBackend kvdb.Backend,
596644
height, err)
597645
}
598646

647+
s.Do(func() {
648+
elapsed := time.Since(t0).Seconds()
649+
ratePerSec := float64(chunk) / elapsed
650+
log.Debugf("Migrated %d prune log "+
651+
"entries (%.2f entries/sec)",
652+
count, ratePerSec)
653+
654+
t0 = time.Now()
655+
chunk = 0
656+
})
657+
599658
return nil
600659
},
601660
)
@@ -715,7 +774,15 @@ func forEachPruneLogEntry(db kvdb.Backend, cb func(height uint32,
715774
func migrateClosedSCIDIndex(ctx context.Context, kvBackend kvdb.Backend,
716775
sqlDB SQLQueries) error {
717776

718-
var count uint64
777+
var (
778+
count uint64
779+
780+
t0 = time.Now()
781+
chunk uint64
782+
s = rate.Sometimes{
783+
Interval: 10 * time.Second,
784+
}
785+
)
719786
migrateSingleClosedSCID := func(scid lnwire.ShortChannelID) error {
720787
count++
721788

@@ -739,6 +806,16 @@ func migrateClosedSCIDIndex(ctx context.Context, kvBackend kvdb.Backend,
739806
"but is not", scid)
740807
}
741808

809+
s.Do(func() {
810+
elapsed := time.Since(t0).Seconds()
811+
ratePerSec := float64(chunk) / elapsed
812+
log.Debugf("Migrated %d closed scids "+
813+
"(%.2f entries/sec)", count, ratePerSec)
814+
815+
t0 = time.Now()
816+
chunk = 0
817+
})
818+
742819
return nil
743820
}
744821

@@ -766,7 +843,15 @@ func migrateClosedSCIDIndex(ctx context.Context, kvBackend kvdb.Backend,
766843
func migrateZombieIndex(ctx context.Context, kvBackend kvdb.Backend,
767844
sqlDB SQLQueries) error {
768845

769-
var count uint64
846+
var (
847+
count uint64
848+
849+
t0 = time.Now()
850+
chunk uint64
851+
s = rate.Sometimes{
852+
Interval: 10 * time.Second,
853+
}
854+
)
770855
err := forEachZombieEntry(kvBackend, func(chanID uint64, pubKey1,
771856
pubKey2 [33]byte) error {
772857

@@ -820,6 +905,16 @@ func migrateZombieIndex(ctx context.Context, kvBackend kvdb.Backend,
820905
"a zombie, but is not", chanID)
821906
}
822907

908+
s.Do(func() {
909+
elapsed := time.Since(t0).Seconds()
910+
ratePerSec := float64(chunk) / elapsed
911+
log.Debugf("Migrated %d zombie index entries "+
912+
"(%.2f entries/sec)", count, ratePerSec)
913+
914+
t0 = time.Now()
915+
chunk = 0
916+
})
917+
823918
return nil
824919
})
825920
if err != nil {

0 commit comments

Comments
 (0)