Skip to content

Commit b2ac78f

Browse files
committed
tapdb+tapcfg: make cache configurable
1 parent 1f4a4c1 commit b2ac78f

File tree

9 files changed

+231
-52
lines changed

9 files changed

+231
-52
lines changed

sample-tapd.conf

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,28 @@
310310
; The burst budget for the universe query rate limiting
311311
; universe.req-burst-budget=10
312312

313+
[multiverse-caches]
314+
315+
; The number of proofs that are cached per universe. (default: 5)
316+
; universe.multiverse-caches.proofs-per-universe=5
317+
318+
; The number of universes that can have a cache of leaf keys. (default: 2000)
319+
; universe.multiverse-caches.leaves-num-cached-universes=2000
320+
321+
; The number of leaf keys that are cached per cached universe. (default: 50)
322+
; universe.multiverse-caches.leaves-per-universe=50
323+
324+
; If the syncer cache is enabled.
325+
; universe.multiverse-caches.syncer-cache-enabled=false
326+
327+
; The pre-allocated size of the syncer cache. (default: 100000)
328+
; universe.multiverse-caches.syncer-cache-pre-alloc-size=100000
329+
330+
; The size of the root node page cache for all requests that aren't served by
331+
; the syncer cache. (default: 10240)
332+
; universe.multiverse-caches.root-node-page-cache-size=10240
333+
334+
313335
[address]
314336

315337
; If true, tapd will not try to sync issuance proofs for unknown assets when

tapcfg/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/jessevdk/go-flags"
2222
"github.com/lightninglabs/lndclient"
2323
tap "github.com/lightninglabs/taproot-assets"
24+
"github.com/lightninglabs/taproot-assets/fn"
2425
"github.com/lightninglabs/taproot-assets/monitoring"
2526
"github.com/lightninglabs/taproot-assets/proof"
2627
"github.com/lightninglabs/taproot-assets/rfq"
@@ -286,6 +287,8 @@ type UniverseConfig struct {
286287
UniverseQueriesPerSecond rate.Limit `long:"max-qps" description:"The maximum number of queries per second across the set of active universe queries that is permitted. Anything above this starts to get rate limited."`
287288

288289
UniverseQueriesBurst int `long:"req-burst-budget" description:"The burst budget for the universe query rate limiting."`
290+
291+
MultiverseCaches *tapdb.MultiverseCacheConfig `group:"multiverse-caches" namespace:"multiverse-caches"`
289292
}
290293

291294
// AddrBookConfig is the config that houses any address Book related config
@@ -431,6 +434,9 @@ func DefaultConfig() Config {
431434
defaultUniverseMaxQps,
432435
),
433436
UniverseQueriesBurst: defaultUniverseQueriesBurst,
437+
MultiverseCaches: fn.Ptr(
438+
tapdb.DefaultMultiverseCacheConfig(),
439+
),
434440
},
435441
AddrBook: &AddrBookConfig{
436442
DisableSyncer: false,

tapcfg/server.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ func genServerConfig(cfg *Config, cfgLogger btclog.Logger,
126126
return db.WithTx(tx)
127127
},
128128
)
129-
multiverse := tapdb.NewMultiverseStore(multiverseDB)
129+
multiverse := tapdb.NewMultiverseStore(
130+
multiverseDB, tapdb.DefaultMultiverseStoreConfig(),
131+
)
130132

131133
uniStatsDB := tapdb.NewTransactionExecutor(
132134
db, func(tx *sql.Tx) tapdb.UniverseStatsStore {

tapdb/multiverse.go

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,29 @@ type BatchedMultiverse interface {
101101
BatchedTx[BaseMultiverseStore]
102102
}
103103

104+
// MultiverseStoreConfig is the set of configuration options for the multiverse
105+
// store.
106+
type MultiverseStoreConfig struct {
107+
// Caches is the set of cache configurations for the multiverse store.
108+
Caches MultiverseCacheConfig
109+
}
110+
111+
// DefaultMultiverseStoreConfig returns the default configuration for the
112+
// multiverse store.
113+
func DefaultMultiverseStoreConfig() *MultiverseStoreConfig {
114+
return &MultiverseStoreConfig{
115+
Caches: DefaultMultiverseCacheConfig(),
116+
}
117+
}
118+
104119
// MultiverseStore implements the persistent storage for a multiverse.
105120
//
106121
// NOTE: This implements the universe.MultiverseArchive interface.
107122
type MultiverseStore struct {
108123
db BatchedMultiverse
109124

125+
cfg *MultiverseStoreConfig
126+
110127
syncerCache *syncerRootNodeCache
111128

112129
rootNodeCache *rootNodeCache
@@ -124,13 +141,26 @@ type MultiverseStore struct {
124141
}
125142

126143
// NewMultiverseStore creates a new multiverse DB store handle.
127-
func NewMultiverseStore(db BatchedMultiverse) *MultiverseStore {
144+
func NewMultiverseStore(db BatchedMultiverse,
145+
cfg *MultiverseStoreConfig) *MultiverseStore {
146+
128147
return &MultiverseStore{
129-
db: db,
130-
syncerCache: newSyncerRootNodeCache(),
131-
rootNodeCache: newRootNodeCache(),
132-
proofCache: newUniverseProofCache(),
133-
leafKeysCache: newUniverseLeafPageCache(),
148+
db: db,
149+
cfg: cfg,
150+
syncerCache: newSyncerRootNodeCache(
151+
cfg.Caches.SyncerCacheEnabled,
152+
cfg.Caches.SyncerCachePreAllocSize,
153+
),
154+
rootNodeCache: newRootNodeCache(
155+
cfg.Caches.RootNodePageCacheSize,
156+
),
157+
proofCache: newUniverseProofCache(
158+
cfg.Caches.ProofsPerUniverse,
159+
),
160+
leafKeysCache: newUniverseLeafPageCache(
161+
cfg.Caches.LeavesNumCachedUniverses,
162+
cfg.Caches.LeavesPerUniverse,
163+
),
134164
transferProofDistributor: fn.NewEventDistributor[proof.Blob](),
135165
}
136166
}
@@ -207,13 +237,16 @@ func (b *MultiverseStore) UniverseRootNode(ctx context.Context,
207237
// syncer cache, as that should have all root nodes that are currently
208238
// known. We never update the syncer cache on a cache miss of a single
209239
// root node, as that shouldn't happen (unless the cache is empty).
240+
// This will always return nil if the cache is disabled, so we don't
241+
// need an extra indentation for that check here.
210242
rootNode := b.syncerCache.fetchRoot(id)
211243
if rootNode != nil {
212244
return *rootNode, nil
213245
}
214246

215-
// If the cache is still empty, we'll populate it now.
216-
if b.syncerCache.isEmpty() {
247+
// If the cache is still empty, we'll populate it now, given it is
248+
// enabled.
249+
if b.syncerCache.isEmpty() && b.cfg.Caches.SyncerCacheEnabled {
217250
// We attempt to acquire the write lock to fill the cache. If
218251
// another goroutine is already filling the cache, we'll wait
219252
// for it to finish that way.
@@ -336,7 +369,7 @@ func (b *MultiverseStore) RootNodes(ctx context.Context,
336369
// WithAmountsById=false)? This cache is complete (all root nodes) and
337370
// can be directly sliced into, but it doesn't have any amounts by ID
338371
// and doesn't support descending order.
339-
if isQueryForSyncerCache(q) {
372+
if isQueryForSyncerCache(q) && b.cfg.Caches.SyncerCacheEnabled {
340373
// First, check to see if we have the root nodes cached in the
341374
// syncer cache.
342375
rootNodes, emptyPage := b.syncerCache.fetchRoots(q, false)
@@ -543,7 +576,9 @@ func (b *MultiverseStore) fillSyncerCache(ctx context.Context) error {
543576
NumLimit: universe.MaxPageSize,
544577
}
545578

546-
allRoots := make([]universe.Root, 0, numCachedProofs)
579+
allRoots := make(
580+
[]universe.Root, 0, b.cfg.Caches.SyncerCachePreAllocSize,
581+
)
547582
for {
548583
newRoots, err := b.queryRootNodes(ctx, params, false)
549584
if err != nil {

0 commit comments

Comments
 (0)