Skip to content

Commit a99247e

Browse files
authored
Merge pull request #658 from Roasbeef/fix-universe-stats-refresh
tapdb: ensure universe asset stats ticker is stopped before refresh
2 parents 5c07cc4 + 8ec03e3 commit a99247e

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

tapdb/multiverse.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,6 @@ func (r *rootNodeCache) fetchRoots(q universe.RootNodesQuery,
275275
defer r.RUnlock()
276276
}
277277

278-
log.Infof("checking cache for roots")
279-
280278
// Attempt to read directly from the root node cache.
281279
rootNodeCache := r.allRoots.Load()
282280
rootNodes, _ := rootNodeCache.Get(newRootPageQuery(q))
@@ -650,7 +648,7 @@ func (b *MultiverseStore) RootNodes(ctx context.Context,
650648
// Attempt to read directly from the root node cache.
651649
rootNodes := b.rootNodeCache.fetchRoots(q, false)
652650
if len(rootNodes) > 0 {
653-
log.Debugf("read %d root nodes from cache", len(rootNodes))
651+
log.Tracef("read %d root nodes from cache", len(rootNodes))
654652
return rootNodes, nil
655653
}
656654

@@ -661,7 +659,7 @@ func (b *MultiverseStore) RootNodes(ctx context.Context,
661659
// the mutex.
662660
rootNodes = b.rootNodeCache.fetchRoots(q, true)
663661
if len(rootNodes) > 0 {
664-
log.Debugf("read %d root nodes from cache", len(rootNodes))
662+
log.Tracef("read %d root nodes from cache", len(rootNodes))
665663
return rootNodes, nil
666664
}
667665

tapdb/universe_stats.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/btcsuite/btcd/btcec/v2"
1414
"github.com/btcsuite/btcd/btcec/v2/schnorr"
15+
"github.com/davecgh/go-spew/spew"
1516
"github.com/lightninglabs/neutrino/cache/lru"
1617
"github.com/lightninglabs/taproot-assets/asset"
1718
"github.com/lightninglabs/taproot-assets/fn"
@@ -152,7 +153,7 @@ type assetEventsCache = *lru.Cache[eventQuery, cachedAssetEvents]
152153

153154
// statsQueryCacheSize is the total number of asset query responses that we'll
154155
// hold inside the cache.
155-
const statsQueryCacheSize = 80_0000
156+
const statsQueryCacheSize = 80_000
156157

157158
// cachedSyncStats is a cached set of sync stats.
158159
type cachedSyncStats []universe.AssetSyncSnapshot
@@ -266,6 +267,8 @@ func (a *atomicSyncStatsCache) storeQuery(q universe.SyncStatsQuery,
266267

267268
statsCache := a.Load()
268269

270+
log.Debugf("Storing asset stats query: %v", spew.Sdump(q))
271+
269272
_, _ = statsCache.Put(query, cachedSyncStats(resp))
270273
}
271274

@@ -509,6 +512,7 @@ func (u *UniverseStats) populateSyncStatsCache() {
509512

510513
// If this is a test, then we'll just purge the items.
511514
if u.opts.cacheDuration == 0 {
515+
log.Debugf("nil state cache duration, wiping cache")
512516
u.statsSnapshot.Store(nil)
513517
return
514518
}
@@ -532,7 +536,10 @@ func (u *UniverseStats) populateSyncStatsCache() {
532536

533537
// Reset the timer so we'll refresh again after the cache duration.
534538
if !u.statsRefresh.Stop() {
535-
<-u.statsRefresh.C
539+
select {
540+
case <-u.statsRefresh.C:
541+
default:
542+
}
536543
}
537544

538545
u.statsRefresh.Reset(u.opts.cacheDuration)
@@ -562,6 +569,8 @@ func (u *UniverseStats) AggregateSyncStats(
562569

563570
u.statsCacheLogger.Miss()
564571

572+
log.Debugf("Populating aggregate sync stats")
573+
565574
dbStats, err := u.querySyncStats(ctx)
566575
if err != nil {
567576
return dbStats, err
@@ -571,6 +580,13 @@ func (u *UniverseStats) AggregateSyncStats(
571580
// the stats pointer so we'll refresh it after a period of time.
572581
u.statsSnapshot.Store(&dbStats)
573582

583+
// Reset the timer so we'll refresh again after the cache duration.
584+
if u.statsRefresh != nil && !u.statsRefresh.Stop() {
585+
select {
586+
case <-u.statsRefresh.C:
587+
default:
588+
}
589+
}
574590
u.statsRefresh = time.AfterFunc(
575591
u.opts.cacheDuration, u.populateSyncStatsCache,
576592
)
@@ -837,6 +853,16 @@ func (u *UniverseStats) QuerySyncStats(ctx context.Context,
837853

838854
// Finally, we'll create the time after function that'll wipe the
839855
// cache, forcing a refresh.
856+
//
857+
// If we already have a timer active, then stop it, so we only have a
858+
// single timer going at any given time.
859+
if u.syncStatsRefresh != nil && !u.syncStatsRefresh.Stop() {
860+
select {
861+
case <-u.syncStatsRefresh.C:
862+
default:
863+
}
864+
}
865+
840866
u.syncStatsRefresh = time.AfterFunc(u.opts.cacheDuration, func() {
841867
log.Infof("Purging sync stats cache, duration=%v",
842868
u.opts.cacheDuration)

0 commit comments

Comments
 (0)