Skip to content

Commit 2d51667

Browse files
committed
graphdb: plug in the cache to sqlstore
In this commit, we add publicNodeCache into the sqlstore. We also add the necessary config for initializing the cache. Signed-off-by: Abdullahi Yunus <[email protected]>
1 parent 37d87e7 commit 2d51667

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

graph/db/options.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ const (
1313
// around 40MB.
1414
DefaultChannelCacheSize = 20000
1515

16+
// DefaultPublicNodeCacheSize is the default number of node public
17+
// status entries to cache. With 15k nodes, this produces a cache of
18+
// around 500KB.
19+
DefaultPublicNodeCacheSize = 15000
20+
1621
// DefaultPreAllocCacheNumNodes is the default number of channels we
1722
// assume for mainnet for pre-allocating the graph cache. As of
1823
// September 2021, there currently are 14k nodes in a strictly pruned
@@ -125,6 +130,10 @@ type StoreOptions struct {
125130
// channel cache.
126131
ChannelCacheSize int
127132

133+
// PublicNodeCacheSize is the maximum number of node public status
134+
// entries to hold in the cache.
135+
PublicNodeCacheSize int
136+
128137
// BatchCommitInterval is the maximum duration the batch schedulers will
129138
// wait before attempting to commit a pending set of updates.
130139
BatchCommitInterval time.Duration
@@ -138,9 +147,10 @@ type StoreOptions struct {
138147
// DefaultOptions returns a StoreOptions populated with default values.
139148
func DefaultOptions() *StoreOptions {
140149
return &StoreOptions{
141-
RejectCacheSize: DefaultRejectCacheSize,
142-
ChannelCacheSize: DefaultChannelCacheSize,
143-
NoMigration: false,
150+
RejectCacheSize: DefaultRejectCacheSize,
151+
ChannelCacheSize: DefaultChannelCacheSize,
152+
PublicNodeCacheSize: DefaultPublicNodeCacheSize,
153+
NoMigration: false,
144154
}
145155
}
146156

@@ -169,3 +179,5 @@ func WithBatchCommitInterval(interval time.Duration) StoreOptionModifier {
169179
o.BatchCommitInterval = interval
170180
}
171181
}
182+
183+
// Todo(abdulkbk) consider adding WithPublicNodeCacheSize.

graph/db/sql_store.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/btcsuite/btcd/btcutil"
2121
"github.com/btcsuite/btcd/chaincfg/chainhash"
2222
"github.com/btcsuite/btcd/wire"
23+
"github.com/lightninglabs/neutrino/cache/lru"
2324
"github.com/lightningnetwork/lnd/aliasmgr"
2425
"github.com/lightningnetwork/lnd/batch"
2526
"github.com/lightningnetwork/lnd/fn/v2"
@@ -181,12 +182,14 @@ type SQLStore struct {
181182
cfg *SQLStoreConfig
182183
db BatchedSQLQueries
183184

184-
// cacheMu guards all caches (rejectCache and chanCache). If
185-
// this mutex will be acquired at the same time as the DB mutex then
186-
// the cacheMu MUST be acquired first to prevent deadlock.
187-
cacheMu sync.RWMutex
188-
rejectCache *rejectCache
189-
chanCache *channelCache
185+
// cacheMu guards all caches (rejectCache, chanCache, and
186+
// publicNodeCache). If this mutex will be acquired at the same time as
187+
// the DB mutex then the cacheMu MUST be acquired first to prevent
188+
// deadlock.
189+
cacheMu sync.RWMutex
190+
rejectCache *rejectCache
191+
chanCache *channelCache
192+
publicNodeCache *lru.Cache[[33]byte, *cachedPublicNode]
190193

191194
chanScheduler batch.Scheduler[SQLQueries]
192195
nodeScheduler batch.Scheduler[SQLQueries]
@@ -241,7 +244,10 @@ func NewSQLStore(cfg *SQLStoreConfig, db BatchedSQLQueries,
241244
db: db,
242245
rejectCache: newRejectCache(opts.RejectCacheSize),
243246
chanCache: newChannelCache(opts.ChannelCacheSize),
244-
srcNodes: make(map[ProtocolVersion]*srcNodeInfo),
247+
publicNodeCache: lru.NewCache[[33]byte, *cachedPublicNode](
248+
uint64(opts.PublicNodeCacheSize),
249+
),
250+
srcNodes: make(map[ProtocolVersion]*srcNodeInfo),
245251
}
246252

247253
s.chanScheduler = batch.NewTimeScheduler(

0 commit comments

Comments
 (0)