Skip to content

Commit f5695a7

Browse files
committed
Use doublew cache for stake address
One small LRU and one bigger where addrs are only explicitely evicted
1 parent c2a5288 commit f5695a7

File tree

7 files changed

+38
-36
lines changed

7 files changed

+38
-36
lines changed

cardano-db-sync/src/Cardano/DbSync/Api.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,10 @@ mkSyncEnv trce backend connectionString syncOptions protoInfo nw nwMagic systemS
403403
then
404404
newEmptyCache
405405
CacheCapacity
406-
{ cacheCapacityStake = 1600000
406+
{ cacheCapacityStake = 100000
407407
, cacheCapacityDatum = 250000
408408
, cacheCapacityMultiAsset = 250000
409-
, cacheCapacityTx = 350000
409+
, cacheCapacityTx = 100000
410410
}
411411
else pure useNoCache
412412
consistentLevelVar <- newTVarIO Unchecked

cardano-db-sync/src/Cardano/DbSync/Cache.hs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import qualified Cardano.Db as DB
3333
import Cardano.DbSync.Cache.Epoch (rollbackMapEpochInCache)
3434
import qualified Cardano.DbSync.Cache.FIFO as FIFO
3535
import qualified Cardano.DbSync.Cache.LRU as LRU
36-
import Cardano.DbSync.Cache.Types (CacheAction (..), CacheInternal (..), CacheStatistics (..), CacheStatus (..), StakeCache (..), initCacheStatistics)
36+
import Cardano.DbSync.Cache.Types (CacheAction (..), CacheInternal (..), CacheStatistics (..), CacheStatus (..), StakeCache (..), initCacheStatistics, shouldCache)
3737
import qualified Cardano.DbSync.Era.Shelley.Generic.Util as Generic
3838
import Cardano.DbSync.Era.Shelley.Query
3939
import Cardano.DbSync.Era.Util
@@ -94,7 +94,7 @@ queryOrInsertRewardAccount ::
9494
Ledger.RewardAccount StandardCrypto ->
9595
ReaderT SqlBackend m DB.StakeAddressId
9696
queryOrInsertRewardAccount trce cache cacheUA rewardAddr = do
97-
eiAddrId <- queryRewardAccountWithCacheRetBs trce cache cacheUA rewardAddr
97+
eiAddrId <- queryStakeAddrWithCacheRetBs trce cache cacheUA rewardAddr
9898
case eiAddrId of
9999
Left (_err, bs) -> insertStakeAddress rewardAddr (Just bs)
100100
Right addrId -> pure addrId
@@ -127,17 +127,6 @@ insertStakeAddress rewardAddr stakeCredBs = do
127127
where
128128
addrBs = fromMaybe (Ledger.serialiseRewardAccount rewardAddr) stakeCredBs
129129

130-
queryRewardAccountWithCacheRetBs ::
131-
forall m.
132-
MonadIO m =>
133-
Trace IO Text ->
134-
CacheStatus ->
135-
CacheAction ->
136-
Ledger.RewardAccount StandardCrypto ->
137-
ReaderT SqlBackend m (Either (DB.LookupFail, ByteString) DB.StakeAddressId)
138-
queryRewardAccountWithCacheRetBs trce cache cacheUA rwdAcc =
139-
queryStakeAddrWithCacheRetBs trce cache cacheUA (Ledger.raNetwork rwdAcc) (Ledger.raCredential rwdAcc)
140-
141130
queryStakeAddrWithCache ::
142131
forall m.
143132
MonadIO m =>
@@ -148,26 +137,25 @@ queryStakeAddrWithCache ::
148137
StakeCred ->
149138
ReaderT SqlBackend m (Either DB.LookupFail DB.StakeAddressId)
150139
queryStakeAddrWithCache trce cache cacheUA nw cred =
151-
mapLeft fst <$> queryStakeAddrWithCacheRetBs trce cache cacheUA nw cred
140+
mapLeft fst <$> queryStakeAddrWithCacheRetBs trce cache cacheUA (Ledger.RewardAccount nw cred)
152141

153142
queryStakeAddrWithCacheRetBs ::
154143
forall m.
155144
MonadIO m =>
156145
Trace IO Text ->
157146
CacheStatus ->
158147
CacheAction ->
159-
Network ->
160-
StakeCred ->
148+
Ledger.RewardAccount StandardCrypto ->
161149
ReaderT SqlBackend m (Either (DB.LookupFail, ByteString) DB.StakeAddressId)
162-
queryStakeAddrWithCacheRetBs _trce cache cacheUA nw cred = do
163-
let bs = Ledger.serialiseRewardAccount (Ledger.RewardAccount nw cred)
150+
queryStakeAddrWithCacheRetBs _trce cache cacheUA ra@(Ledger.RewardAccount _ cred) = do
151+
let bs = Ledger.serialiseRewardAccount ra
164152
case cache of
165153
NoCache -> do
166154
mapLeft (,bs) <$> resolveStakeAddress bs
167155
ActiveCache ci -> do
168156
stakeCache <- liftIO $ readTVarIO (cStake ci)
169157
case queryStakeCache cred stakeCache of
170-
Just (addrId, stakeCache', _) -> do
158+
Just (addrId, stakeCache') -> do
171159
liftIO $ hitCreds (cStats ci)
172160
case cacheUA of
173161
EvictAndUpdateCache -> do
@@ -192,11 +180,11 @@ queryStakeAddrWithCacheRetBs _trce cache cacheUA nw cred = do
192180
pure $ Right stakeAddrsId
193181

194182
-- | True if it was found in LRU
195-
queryStakeCache :: StakeCred -> StakeCache -> Maybe (DB.StakeAddressId, StakeCache, Bool)
183+
queryStakeCache :: StakeCred -> StakeCache -> Maybe (DB.StakeAddressId, StakeCache)
196184
queryStakeCache scred scache = case Map.lookup scred (scStableCache scache) of
197-
Just addrId -> Just (addrId, scache, False)
185+
Just addrId -> Just (addrId, scache)
198186
Nothing -> case LRU.lookup scred (scLruCache scache) of
199-
Just (addrId, lru') -> Just (addrId, scache {scLruCache = lru'}, True)
187+
Just (addrId, lru') -> Just (addrId, scache {scLruCache = lru'})
200188
Nothing -> Nothing
201189

202190
deleteStakeCache :: StakeCred -> StakeCache -> StakeCache
@@ -235,7 +223,7 @@ queryPoolKeyWithCache cache cacheUA hsh =
235223
Nothing -> pure $ Left (DB.DbLookupMessage "PoolKeyHash")
236224
Just phId -> do
237225
-- missed so we can't evict even with 'EvictAndReturn'
238-
when (cacheUA == UpdateCache) $
226+
when (shouldCache cacheUA) $
239227
liftIO $
240228
atomically $
241229
modifyTVar (cPools ci) $
@@ -275,7 +263,7 @@ insertPoolKeyWithCache cache cacheUA pHash =
275263
{ DB.poolHashHashRaw = Generic.unKeyHashRaw pHash
276264
, DB.poolHashView = Generic.unKeyHashView pHash
277265
}
278-
when (cacheUA == UpdateCache) $
266+
when (shouldCache cacheUA) $
279267
liftIO $
280268
atomically $
281269
modifyTVar (cPools ci) $

cardano-db-sync/src/Cardano/DbSync/Cache/Types.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE DataKinds #-}
22
{-# LANGUAGE FlexibleContexts #-}
3+
{-# LANGUAGE LambdaCase #-}
34
{-# LANGUAGE OverloadedStrings #-}
45
{-# LANGUAGE RankNTypes #-}
56
{-# LANGUAGE RecordWildCards #-}
@@ -21,6 +22,9 @@ module Cardano.DbSync.Cache.Types (
2122
initCacheStatistics,
2223
newEmptyCache,
2324

25+
-- * Utils
26+
shouldCache,
27+
2428
-- * CacheStatistics
2529
CacheStatistics (..),
2630
textShowStats,
@@ -47,6 +51,10 @@ import Ouroboros.Consensus.Cardano.Block (StandardCrypto)
4751

4852
type StakePoolCache = Map PoolKeyHash DB.PoolHashId
4953

54+
-- | We use a stable cache for entries that are expected to be reused frequentyl.
55+
-- These are stake addresses that have rewards, delegations etc.
56+
-- They are never removed unless manually eg when it's deregistered
57+
-- The LRU cache is much smaller for the rest stake addresses.
5058
data StakeCache = StakeCache
5159
{ scStableCache :: !(Map StakeCred DB.StakeAddressId)
5260
, scLruCache :: !(LRUCache StakeCred DB.StakeAddressId)
@@ -229,3 +237,9 @@ initCacheStatistics = CacheStatistics 0 0 0 0 0 0 0 0 0 0 0 0
229237

230238
initCacheEpoch :: CacheEpoch
231239
initCacheEpoch = CacheEpoch mempty Nothing
240+
241+
shouldCache :: CacheAction -> Bool
242+
shouldCache = \case
243+
UpdateCache -> True
244+
UpdateCacheStrong -> True
245+
_ -> False

cardano-db-sync/src/Cardano/DbSync/Era/Universal/Epoch.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ insertEpochStake syncEnv nw epochNo stakeChunk = do
215215
(StakeCred, (Shelley.Coin, PoolKeyHash)) ->
216216
ExceptT SyncNodeError (ReaderT SqlBackend m) DB.EpochStake
217217
mkStake cache (saddr, (coin, pool)) = do
218-
saId <- lift $ queryOrInsertStakeAddress trce cache UpdateCache nw saddr
218+
saId <- lift $ queryOrInsertStakeAddress trce cache UpdateCacheStrong nw saddr
219219
poolId <- lift $ queryPoolKeyOrInsert "insertEpochStake" trce cache UpdateCache (ioShelley iopts) pool
220220
pure $
221221
DB.EpochStake
@@ -249,7 +249,7 @@ insertRewards syncEnv nw earnedEpoch spendableEpoch cache rewardsChunk = do
249249
(StakeCred, Set Generic.Reward) ->
250250
ExceptT SyncNodeError (ReaderT SqlBackend m) [DB.Reward]
251251
mkRewards (saddr, rset) = do
252-
saId <- lift $ queryOrInsertStakeAddress trce cache UpdateCache nw saddr
252+
saId <- lift $ queryOrInsertStakeAddress trce cache UpdateCacheStrong nw saddr
253253
mapM (prepareReward saId) (Set.toList rset)
254254

255255
prepareReward ::
@@ -299,7 +299,7 @@ insertRewardRests trce nw earnedEpoch spendableEpoch cache rewardsChunk = do
299299
(StakeCred, Set Generic.RewardRest) ->
300300
ExceptT SyncNodeError (ReaderT SqlBackend m) [DB.RewardRest]
301301
mkRewards (saddr, rset) = do
302-
saId <- lift $ queryOrInsertStakeAddress trce cache UpdateCache nw saddr
302+
saId <- lift $ queryOrInsertStakeAddress trce cache UpdateCacheStrong nw saddr
303303
pure $ map (prepareReward saId) (Set.toList rset)
304304

305305
prepareReward ::
@@ -333,7 +333,7 @@ insertProposalRefunds trce nw earnedEpoch spendableEpoch cache refunds = do
333333
GovActionRefunded ->
334334
ExceptT SyncNodeError (ReaderT SqlBackend m) DB.RewardRest
335335
mkReward refund = do
336-
saId <- lift $ queryOrInsertStakeAddress trce cache UpdateCache nw (raCredential $ garReturnAddr refund)
336+
saId <- lift $ queryOrInsertStakeAddress trce cache UpdateCacheStrong nw (raCredential $ garReturnAddr refund)
337337
pure $
338338
DB.RewardRest
339339
{ DB.rewardRestAddrId = saId

cardano-db-sync/src/Cardano/DbSync/Era/Universal/Insert/Certificate.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ insertMirCert tracer cache network txId idx mcert = do
193193
(StakeCred, Ledger.DeltaCoin) ->
194194
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
195195
insertMirReserves (cred, dcoin) = do
196-
addrId <- lift $ queryOrInsertStakeAddress tracer cache UpdateCache network cred
196+
addrId <- lift $ queryOrInsertStakeAddress tracer cache UpdateCacheStrong network cred
197197
void . lift . DB.insertReserve $
198198
DB.Reserve
199199
{ DB.reserveAddrId = addrId
@@ -207,7 +207,7 @@ insertMirCert tracer cache network txId idx mcert = do
207207
(StakeCred, Ledger.DeltaCoin) ->
208208
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
209209
insertMirTreasury (cred, dcoin) = do
210-
addrId <- lift $ queryOrInsertStakeAddress tracer cache UpdateCache network cred
210+
addrId <- lift $ queryOrInsertStakeAddress tracer cache UpdateCacheStrong network cred
211211
void . lift . DB.insertTreasury $
212212
DB.Treasury
213213
{ DB.treasuryAddrId = addrId
@@ -413,7 +413,7 @@ insertDelegation ::
413413
Ledger.KeyHash 'Ledger.StakePool StandardCrypto ->
414414
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
415415
insertDelegation trce cache network (EpochNo epoch) slotNo txId idx mRedeemerId cred poolkh = do
416-
addrId <- lift $ queryOrInsertStakeAddress trce cache UpdateCache network cred
416+
addrId <- lift $ queryOrInsertStakeAddress trce cache UpdateCacheStrong network cred
417417
poolHashId <- lift $ queryPoolKeyOrInsert "insertDelegation" trce cache UpdateCache True poolkh
418418
void . lift . DB.insertDelegation $
419419
DB.Delegation
@@ -437,7 +437,7 @@ insertDelegationVote ::
437437
DRep StandardCrypto ->
438438
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
439439
insertDelegationVote trce cache network txId idx cred drep = do
440-
addrId <- lift $ queryOrInsertStakeAddress trce cache UpdateCache network cred
440+
addrId <- lift $ queryOrInsertStakeAddress trce cache UpdateCacheStrong network cred
441441
drepId <- lift $ insertDrep drep
442442
void
443443
. lift

cardano-db-sync/src/Cardano/DbSync/Era/Universal/Insert/Other.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ insertStakeAddressRefIfMissing trce cache addr =
155155
Ledger.Addr nw _pcred sref ->
156156
case sref of
157157
Ledger.StakeRefBase cred -> do
158-
Just <$> queryOrInsertStakeAddress trce cache DoNotUpdateCache nw cred
158+
Just <$> queryOrInsertStakeAddress trce cache UpdateCache nw cred
159159
Ledger.StakeRefPtr ptr -> do
160160
DB.queryStakeRefPtr ptr
161161
Ledger.StakeRefNull -> pure Nothing

cardano-db-sync/src/Cardano/DbSync/Era/Universal/Insert/Pool.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ insertPoolOwner ::
150150
Ledger.KeyHash 'Ledger.Staking StandardCrypto ->
151151
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
152152
insertPoolOwner trce cache network poolUpdateId skh = do
153-
saId <- lift $ queryOrInsertStakeAddress trce cache UpdateCache network (Ledger.KeyHashObj skh)
153+
saId <- lift $ queryOrInsertStakeAddress trce cache UpdateCacheStrong network (Ledger.KeyHashObj skh)
154154
void . lift . DB.insertPoolOwner $
155155
DB.PoolOwner
156156
{ DB.poolOwnerAddrId = saId

0 commit comments

Comments
 (0)