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 committed 2024-06-19 07:34:34 -03:00
1 parent 6a27bc29ba
commit 637ac85d1d
1 file changed
+21 -2
+21 -2
View File
@@ -96,6 +96,8 @@ func newMissionControlStore(db kvdb.Backend, maxRecords int,
return nil, err return nil, err
} }
log.Infof("Loaded %d mission control entries", len(keysMap))
return &missionControlStore{ return &missionControlStore{
done: make(chan struct{}), done: make(chan struct{}),
db: db, db: db,
@@ -302,14 +304,24 @@ func (b *missionControlStore) run() {
// storeResults stores all accumulated results. // storeResults stores all accumulated results.
func (b *missionControlStore) storeResults() error { 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() b.queueMx.Lock()
l := b.queue l := b.queue
if l.Len() == 0 {
b.queueMx.Unlock()
return nil
}
b.queue = list.New() b.queue = list.New()
b.queueMx.Unlock() b.queueMx.Unlock()
var ( var (
keys *list.List keys *list.List
keysMap map[string]struct{} keysMap map[string]struct{}
storeCount int
pruneCount int
) )
err := kvdb.Update(b.db, func(tx kvdb.RwTx) error { err := kvdb.Update(b.db, func(tx kvdb.RwTx) error {
@@ -337,6 +349,7 @@ func (b *missionControlStore) storeResults() error {
keys.PushBack(string(k)) keys.PushBack(string(k))
keysMap[string(k)] = struct{}{} keysMap[string(k)] = struct{}{}
storeCount++
} }
// Prune oldest entries. // Prune oldest entries.
@@ -354,6 +367,7 @@ func (b *missionControlStore) storeResults() error {
keys.Remove(front) keys.Remove(front)
delete(keysMap, key) delete(keysMap, key)
pruneCount++
} }
return nil return nil
@@ -365,12 +379,17 @@ func (b *missionControlStore) storeResults() error {
for k := range b.keysMap { for k := range b.keysMap {
keysMap[k] = struct{}{} keysMap[k] = struct{}{}
} }
storeCount, pruneCount = 0, 0
}) })
if err != nil { if err != nil {
return err return err
} }
log.Debugf("Stored mission control results: %d added, %d deleted",
storeCount, pruneCount)
b.keys = keys b.keys = keys
b.keysMap = keysMap b.keysMap = keysMap