Skip to content

Commit 7d3b5a1

Browse files
authored
Merge pull request lightningnetwork#9714 from bhandras/invoice-migration-log-rate
invoices: reduce log spam when migrating invoices to SQL
2 parents bec84e1 + 7020edb commit 7d3b5a1

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

invoices/sql_migration.go

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

2223
var (
@@ -433,25 +434,29 @@ func MigrateInvoicesToSQL(ctx context.Context, db kvdb.Backend,
433434
}
434435
log.Debugf("Created SQL invoice hash index in %v", time.Since(t0))
435436

437+
s := rate.Sometimes{
438+
Interval: 30 * time.Second,
439+
}
440+
441+
t0 = time.Now()
442+
chunk := 0
436443
total := 0
444+
437445
// Now we can start migrating the invoices. We'll do this in
438446
// batches to reduce memory usage.
439447
for {
440-
t0 = time.Now()
441448
query := InvoiceQuery{
442449
IndexOffset: offset,
443450
NumMaxInvoices: uint64(batchSize),
444451
}
445452

446453
queryResult, err := kvStore.QueryInvoices(ctx, query)
447454
if err != nil && !errors.Is(err, ErrNoInvoicesCreated) {
448-
return fmt.Errorf("unable to query invoices: "+
449-
"%w", err)
455+
return fmt.Errorf("unable to query invoices: %w", err)
450456
}
451457

452458
if len(queryResult.Invoices) == 0 {
453-
log.Infof("All invoices migrated")
454-
459+
log.Infof("All invoices migrated. Total: %d", total)
455460
break
456461
}
457462

@@ -461,9 +466,19 @@ func MigrateInvoicesToSQL(ctx context.Context, db kvdb.Backend,
461466
}
462467

463468
offset = queryResult.LastIndexOffset
464-
total += len(queryResult.Invoices)
465-
log.Debugf("Migrated %d KV invoices to SQL in %v\n", total,
466-
time.Since(t0))
469+
resultCnt := len(queryResult.Invoices)
470+
total += resultCnt
471+
chunk += resultCnt
472+
473+
s.Do(func() {
474+
elapsed := time.Since(t0).Seconds()
475+
ratePerSec := float64(chunk) / elapsed
476+
log.Debugf("Migrated %d invoices (%.2f invoices/sec)",
477+
total, ratePerSec)
478+
479+
t0 = time.Now()
480+
chunk = 0
481+
})
467482
}
468483

469484
// Clean up the hash index as it's no longer needed.

0 commit comments

Comments
 (0)