missioncontrolstore: skip work when there are no new entries

This modifies the mission control store to avoid doing any work when no
new payment result entries are in the queue to be processed.

The mission control store maintains keeps the latest N (in production:
1000) entries in its DB, evicting older entries when new ones are added.
Currently, its implementation is somewhat less performant than it could
be.

This commit adds an early return to the storeResults function to avoid
doing any DB or memory operations when its outstanding queue is empty,
improving the performance during quiescent periods of the LN node's
execution.
This commit is contained in:
Matheus Degiovani 2024-03-13 12:46:42 -03:00
parent 6a27bc29ba
commit 637ac85d1d
No known key found for this signature in database
GPG Key ID: EA98A1DCB4D97D1C

View File

@ -96,6 +96,8 @@ func newMissionControlStore(db kvdb.Backend, maxRecords int,
return nil, err
}
log.Infof("Loaded %d mission control entries", len(keysMap))
return &missionControlStore{
done: make(chan struct{}),
db: db,
@ -302,14 +304,24 @@ func (b *missionControlStore) run() {
// storeResults stores all accumulated results.
func (b *missionControlStore) storeResults() error {
// We copy a reference to the queue and clear the original queue to be
// able to release the lock.
b.queueMx.Lock()
l := b.queue
if l.Len() == 0 {
b.queueMx.Unlock()
return nil
}
b.queue = list.New()
b.queueMx.Unlock()
var (
keys *list.List
keysMap map[string]struct{}
storeCount int
pruneCount int
)
err := kvdb.Update(b.db, func(tx kvdb.RwTx) error {
@ -337,6 +349,7 @@ func (b *missionControlStore) storeResults() error {
keys.PushBack(string(k))
keysMap[string(k)] = struct{}{}
storeCount++
}
// Prune oldest entries.
@ -354,6 +367,7 @@ func (b *missionControlStore) storeResults() error {
keys.Remove(front)
delete(keysMap, key)
pruneCount++
}
return nil
@ -365,12 +379,17 @@ func (b *missionControlStore) storeResults() error {
for k := range b.keysMap {
keysMap[k] = struct{}{}
}
storeCount, pruneCount = 0, 0
})
if err != nil {
return err
}
log.Debugf("Stored mission control results: %d added, %d deleted",
storeCount, pruneCount)
b.keys = keys
b.keysMap = keysMap