Skip to content

Commit 9d53617

Browse files
authored
Merge pull request lightningnetwork#9734 from ziggie1984/improve-logging-for-invoices-payments
Improve logging when fetching invoices and payments
2 parents 825ee3d + 440ed31 commit 9d53617

File tree

8 files changed

+137
-6
lines changed

8 files changed

+137
-6
lines changed

channeldb/invoices.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ const (
142142
ampStateSettleDateType tlv.Type = 3
143143
ampStateCircuitKeysType tlv.Type = 4
144144
ampStateAmtPaidType tlv.Type = 5
145+
146+
// invoiceScanBatchSize is the number we use limiting the logging output
147+
// of invoice processing.
148+
invoiceScanBatchSize = 1000
145149
)
146150

147151
// AddInvoice inserts the targeted invoice into the database. If the invoice has
@@ -241,7 +245,11 @@ func (d *DB) AddInvoice(_ context.Context, newInvoice *invpkg.Invoice,
241245
func (d *DB) InvoicesAddedSince(_ context.Context, sinceAddIndex uint64) (
242246
[]invpkg.Invoice, error) {
243247

244-
var newInvoices []invpkg.Invoice
248+
var (
249+
newInvoices []invpkg.Invoice
250+
start = time.Now()
251+
processedCount int
252+
)
245253

246254
// If an index of zero was specified, then in order to maintain
247255
// backwards compat, we won't send out any new invoices.
@@ -274,7 +282,6 @@ func (d *DB) InvoicesAddedSince(_ context.Context, sinceAddIndex uint64) (
274282
addSeqNo, invoiceKey := invoiceCursor.Next()
275283

276284
for ; addSeqNo != nil && bytes.Compare(addSeqNo, startIndex[:]) > 0; addSeqNo, invoiceKey = invoiceCursor.Next() {
277-
278285
// For each key found, we'll look up the actual
279286
// invoice, then accumulate it into our return value.
280287
invoice, err := fetchInvoice(
@@ -285,6 +292,13 @@ func (d *DB) InvoicesAddedSince(_ context.Context, sinceAddIndex uint64) (
285292
}
286293

287294
newInvoices = append(newInvoices, invoice)
295+
296+
processedCount++
297+
if processedCount%invoiceScanBatchSize == 0 {
298+
log.Debugf("Processed %d invoices since "+
299+
"invoice with add index %v",
300+
processedCount, sinceAddIndex)
301+
}
288302
}
289303

290304
return nil
@@ -295,6 +309,12 @@ func (d *DB) InvoicesAddedSince(_ context.Context, sinceAddIndex uint64) (
295309
return nil, err
296310
}
297311

312+
elapsed := time.Since(start)
313+
log.Debugf("Completed scanning invoices added since index %v: "+
314+
"total_processed=%d, found_invoices=%d, elapsed=%v",
315+
sinceAddIndex, processedCount, len(newInvoices),
316+
elapsed.Round(time.Millisecond))
317+
298318
return newInvoices, nil
299319
}
300320

@@ -1045,7 +1065,11 @@ func (k *kvInvoiceUpdater) serializeAndStoreInvoice() error {
10451065
func (d *DB) InvoicesSettledSince(_ context.Context, sinceSettleIndex uint64) (
10461066
[]invpkg.Invoice, error) {
10471067

1048-
var settledInvoices []invpkg.Invoice
1068+
var (
1069+
settledInvoices []invpkg.Invoice
1070+
start = time.Now()
1071+
processedCount int
1072+
)
10491073

10501074
// If an index of zero was specified, then in order to maintain
10511075
// backwards compat, we won't send out any new invoices.
@@ -1104,6 +1128,13 @@ func (d *DB) InvoicesSettledSince(_ context.Context, sinceSettleIndex uint64) (
11041128
}
11051129

11061130
settledInvoices = append(settledInvoices, invoice)
1131+
1132+
processedCount++
1133+
if processedCount%invoiceScanBatchSize == 0 {
1134+
log.Debugf("Processed %d settled invoices "+
1135+
"since invoice with settle index %v",
1136+
processedCount, sinceSettleIndex)
1137+
}
11071138
}
11081139

11091140
return nil
@@ -1114,6 +1145,12 @@ func (d *DB) InvoicesSettledSince(_ context.Context, sinceSettleIndex uint64) (
11141145
return nil, err
11151146
}
11161147

1148+
elapsed := time.Since(start)
1149+
log.Debugf("Completed scanning invoices settled since index %v: "+
1150+
"total_processed=%d, found_invoices=%d, elapsed=%v",
1151+
sinceSettleIndex, processedCount, len(settledInvoices),
1152+
elapsed.Round(time.Millisecond))
1153+
11171154
return settledInvoices, nil
11181155
}
11191156

channeldb/payment_control.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99
"sync"
10+
"time"
1011

1112
"github.com/lightningnetwork/lnd/kvdb"
1213
"github.com/lightningnetwork/lnd/lntypes"
@@ -16,6 +17,10 @@ const (
1617
// paymentSeqBlockSize is the block size used when we batch allocate
1718
// payment sequences for future payments.
1819
paymentSeqBlockSize = 1000
20+
21+
// paymentBatchSize is the number we use limiting the logging output
22+
// of payment processing.
23+
paymentBatchSize = 1000
1924
)
2025

2126
var (
@@ -770,7 +775,12 @@ func fetchPaymentStatus(bucket kvdb.RBucket) (PaymentStatus, error) {
770775

771776
// FetchInFlightPayments returns all payments with status InFlight.
772777
func (p *PaymentControl) FetchInFlightPayments() ([]*MPPayment, error) {
773-
var inFlights []*MPPayment
778+
var (
779+
inFlights []*MPPayment
780+
start = time.Now()
781+
processedCount int
782+
)
783+
774784
err := kvdb.View(p.db, func(tx kvdb.RTx) error {
775785
payments := tx.ReadBucket(paymentsRootBucket)
776786
if payments == nil {
@@ -788,6 +798,14 @@ func (p *PaymentControl) FetchInFlightPayments() ([]*MPPayment, error) {
788798
return err
789799
}
790800

801+
processedCount++
802+
if processedCount%paymentBatchSize == 0 {
803+
log.Debugf("Scanning inflight payments "+
804+
"(in progress), processed %d, last "+
805+
"processed payment: %v", processedCount,
806+
p.Info)
807+
}
808+
791809
// Skip the payment if it's terminated.
792810
if p.Terminated() {
793811
return nil
@@ -803,5 +821,10 @@ func (p *PaymentControl) FetchInFlightPayments() ([]*MPPayment, error) {
803821
return nil, err
804822
}
805823

824+
elapsed := time.Since(start)
825+
log.Debugf("Completed scanning inflight payments: total_processed=%d, "+
826+
"found_inflight=%d, elapsed=%v", processedCount, len(inFlights),
827+
elapsed.Round(time.Millisecond))
828+
806829
return inFlights, nil
807830
}

channeldb/payments.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,12 @@ type PaymentCreationInfo struct {
202202
FirstHopCustomRecords lnwire.CustomRecords
203203
}
204204

205+
func (p *PaymentCreationInfo) String() string {
206+
return fmt.Sprintf("payment_id=%v, amount=%v, created_at=%v, "+
207+
"payment_request=%v", p.PaymentIdentifier, p.Value,
208+
p.CreationTime, p.PaymentRequest)
209+
}
210+
205211
// htlcBucketKey creates a composite key from prefix and id where the result is
206212
// simply the two concatenated.
207213
func htlcBucketKey(prefix, id []byte) []byte {

cmd/commands/cmd_payments.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,6 +1842,9 @@ func deletePayments(ctx *cli.Context) error {
18421842
if err != nil {
18431843
return fmt.Errorf("error deleting payments: %w", err)
18441844
}
1845+
1846+
default:
1847+
return fmt.Errorf("either --all or --payment_hash must be set")
18451848
}
18461849

18471850
printJSON(resp)

invoices/invoiceregistry.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,16 +484,25 @@ func (i *InvoiceRegistry) dispatchToClients(event *invoiceEvent) {
484484
func (i *InvoiceRegistry) deliverBacklogEvents(ctx context.Context,
485485
client *InvoiceSubscription) error {
486486

487+
log.Debugf("Collecting added invoices since %v for client %v",
488+
client.addIndex, client.id)
489+
487490
addEvents, err := i.idb.InvoicesAddedSince(ctx, client.addIndex)
488491
if err != nil {
489492
return err
490493
}
491494

495+
log.Debugf("Collecting settled invoices since %v for client %v",
496+
client.settleIndex, client.id)
497+
492498
settleEvents, err := i.idb.InvoicesSettledSince(ctx, client.settleIndex)
493499
if err != nil {
494500
return err
495501
}
496502

503+
log.Debugf("Delivering %d added invoices and %d settled invoices "+
504+
"for client %v", len(addEvents), len(settleEvents), client.id)
505+
497506
// If we have any to deliver, then we'll append them to the end of the
498507
// notification queue in order to catch up the client before delivering
499508
// any new notifications.

invoices/sql_store.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ const (
2424
// defaultQueryPaginationLimit is used in the LIMIT clause of the SQL
2525
// queries to limit the number of rows returned.
2626
defaultQueryPaginationLimit = 100
27+
28+
// invoiceScanBatchSize is the number we use limiting the logging output
29+
// when scanning invoices.
30+
invoiceScanBatchSize = 1000
2731
)
2832

2933
// SQLInvoiceQueries is an interface that defines the set of operations that can
@@ -785,7 +789,11 @@ func (i *SQLStore) FetchPendingInvoices(ctx context.Context) (
785789
func (i *SQLStore) InvoicesSettledSince(ctx context.Context, idx uint64) (
786790
[]Invoice, error) {
787791

788-
var invoices []Invoice
792+
var (
793+
invoices []Invoice
794+
start = time.Now()
795+
processedCount int
796+
)
789797

790798
if idx == 0 {
791799
return invoices, nil
@@ -819,6 +827,14 @@ func (i *SQLStore) InvoicesSettledSince(ctx context.Context, idx uint64) (
819827
}
820828

821829
invoices = append(invoices, *invoice)
830+
831+
processedCount++
832+
if processedCount%invoiceScanBatchSize == 0 {
833+
log.Debugf("Processed %d settled "+
834+
"invoices since invoice with "+
835+
"settle index %v",
836+
processedCount, idx)
837+
}
822838
}
823839

824840
return len(rows), nil
@@ -873,6 +889,13 @@ func (i *SQLStore) InvoicesSettledSince(ctx context.Context, idx uint64) (
873889
}
874890

875891
invoices = append(invoices, *invoice)
892+
893+
processedCount++
894+
if processedCount%invoiceScanBatchSize == 0 {
895+
log.Debugf("Processed %d settled invoices "+
896+
"since invoice with settle index %v",
897+
processedCount, idx)
898+
}
876899
}
877900

878901
return nil
@@ -884,6 +907,12 @@ func (i *SQLStore) InvoicesSettledSince(ctx context.Context, idx uint64) (
884907
"index (excluding) %d: %w", idx, err)
885908
}
886909

910+
elapsed := time.Since(start)
911+
log.Debugf("Completed scanning invoices settled since index %v: "+
912+
"total_processed=%d, found_invoices=%d, elapsed=%v",
913+
idx, processedCount, len(invoices),
914+
elapsed.Round(time.Millisecond))
915+
887916
return invoices, nil
888917
}
889918

@@ -896,7 +925,11 @@ func (i *SQLStore) InvoicesSettledSince(ctx context.Context, idx uint64) (
896925
func (i *SQLStore) InvoicesAddedSince(ctx context.Context, idx uint64) (
897926
[]Invoice, error) {
898927

899-
var result []Invoice
928+
var (
929+
result []Invoice
930+
start = time.Now()
931+
processedCount int
932+
)
900933

901934
if idx == 0 {
902935
return result, nil
@@ -928,6 +961,13 @@ func (i *SQLStore) InvoicesAddedSince(ctx context.Context, idx uint64) (
928961
}
929962

930963
result = append(result, *invoice)
964+
965+
processedCount++
966+
if processedCount%invoiceScanBatchSize == 0 {
967+
log.Debugf("Processed %d invoices "+
968+
"added since invoice with add "+
969+
"index %v", processedCount, idx)
970+
}
931971
}
932972

933973
return len(rows), nil
@@ -941,6 +981,12 @@ func (i *SQLStore) InvoicesAddedSince(ctx context.Context, idx uint64) (
941981
"index %d: %w", idx, err)
942982
}
943983

984+
elapsed := time.Since(start)
985+
log.Debugf("Completed scanning invoices added since index %v: "+
986+
"total_processed=%d, found_invoices=%d, elapsed=%v",
987+
idx, processedCount, len(result),
988+
elapsed.Round(time.Millisecond))
989+
944990
return result, nil
945991
}
946992

routing/control_tower.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,13 @@ func (p *controlTower) SubscribeAllPayments() (ControlTowerSubscriber, error) {
365365
p.subscriberIndex++
366366
p.subscribersMtx.Unlock()
367367

368+
log.Debugf("Scanning for inflight payments")
368369
inflightPayments, err := p.db.FetchInFlightPayments()
369370
if err != nil {
370371
return nil, err
371372
}
373+
log.Debugf("Scanning for inflight payments finished",
374+
len(inflightPayments))
372375

373376
for index := range inflightPayments {
374377
// Always write current payment state to the channel.

routing/router.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,11 +1400,15 @@ func (r *ChannelRouter) BuildRoute(amt fn.Option[lnwire.MilliSatoshi],
14001400
// lifecycles.
14011401
func (r *ChannelRouter) resumePayments() error {
14021402
// Get all payments that are inflight.
1403+
log.Debugf("Scanning for inflight payments")
14031404
payments, err := r.cfg.Control.FetchInFlightPayments()
14041405
if err != nil {
14051406
return err
14061407
}
14071408

1409+
log.Debugf("Scanning finished, found %d inflight payments",
1410+
len(payments))
1411+
14081412
// Before we restart existing payments and start accepting more
14091413
// payments to be made, we clean the network result store of the
14101414
// Switch. We do this here at startup to ensure no more payments can be

0 commit comments

Comments
 (0)