Skip to content

Commit c2a5288

Browse files
committed
Add double cache for stake address
1 parent d6ba241 commit c2a5288

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ mkSyncEnv trce backend connectionString syncOptions protoInfo nw nwMagic systemS
403403
then
404404
newEmptyCache
405405
CacheCapacity
406-
{ cacheCapacityStakeHashRaw = 1600000
406+
{ cacheCapacityStake = 1600000
407407
, cacheCapacityDatum = 250000
408408
, cacheCapacityMultiAsset = 250000
409409
, cacheCapacityTx = 350000

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

Lines changed: 25 additions & 11 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 (..), initCacheStatistics, isCacheActionUpdate)
36+
import Cardano.DbSync.Cache.Types (CacheAction (..), CacheInternal (..), CacheStatistics (..), CacheStatus (..), StakeCache (..), initCacheStatistics)
3737
import qualified Cardano.DbSync.Era.Shelley.Generic.Util as Generic
3838
import Cardano.DbSync.Era.Shelley.Query
3939
import Cardano.DbSync.Era.Util
@@ -165,30 +165,44 @@ queryStakeAddrWithCacheRetBs _trce cache cacheUA nw cred = do
165165
NoCache -> do
166166
mapLeft (,bs) <$> resolveStakeAddress bs
167167
ActiveCache ci -> do
168-
currentCache <- liftIO $ readTVarIO (cStakeRawHashes ci)
169-
case LRU.lookup cred currentCache of
170-
Just (addrId, lruCache) -> do
168+
stakeCache <- liftIO $ readTVarIO (cStake ci)
169+
case queryStakeCache cred stakeCache of
170+
Just (addrId, stakeCache', _) -> do
171171
liftIO $ hitCreds (cStats ci)
172172
case cacheUA of
173173
EvictAndUpdateCache -> do
174-
liftIO $ atomically $ writeTVar (cStakeRawHashes ci) $ LRU.delete cred lruCache
174+
liftIO $ atomically $ writeTVar (cStake ci) $ deleteStakeCache cred stakeCache'
175175
pure $ Right addrId
176176
_other -> do
177-
liftIO $ atomically $ writeTVar (cStakeRawHashes ci) lruCache
177+
liftIO $ atomically $ writeTVar (cStake ci) stakeCache'
178178
pure $ Right addrId
179179
Nothing -> do
180180
queryRes <- mapLeft (,bs) <$> resolveStakeAddress bs
181181
liftIO $ missCreds (cStats ci)
182182
case queryRes of
183183
Left _ -> pure queryRes
184184
Right stakeAddrsId -> do
185-
when (isCacheActionUpdate cacheUA) $
186-
liftIO $
187-
atomically $
188-
modifyTVar (cStakeRawHashes ci) $
189-
LRU.insert cred stakeAddrsId
185+
let !stakeCache' = case cacheUA of
186+
UpdateCache -> stakeCache {scLruCache = LRU.insert cred stakeAddrsId (scLruCache stakeCache)}
187+
UpdateCacheStrong -> stakeCache {scStableCache = Map.insert cred stakeAddrsId (scStableCache stakeCache)}
188+
_ -> stakeCache
189+
liftIO $
190+
atomically $
191+
writeTVar (cStake ci) stakeCache'
190192
pure $ Right stakeAddrsId
191193

194+
-- | True if it was found in LRU
195+
queryStakeCache :: StakeCred -> StakeCache -> Maybe (DB.StakeAddressId, StakeCache, Bool)
196+
queryStakeCache scred scache = case Map.lookup scred (scStableCache scache) of
197+
Just addrId -> Just (addrId, scache, False)
198+
Nothing -> case LRU.lookup scred (scLruCache scache) of
199+
Just (addrId, lru') -> Just (addrId, scache {scLruCache = lru'}, True)
200+
Nothing -> Nothing
201+
202+
deleteStakeCache :: StakeCred -> StakeCache -> StakeCache
203+
deleteStakeCache scred scache =
204+
scache {scStableCache = Map.delete scred (scStableCache scache)}
205+
192206
queryPoolKeyWithCache ::
193207
MonadIO m =>
194208
CacheStatus ->

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,14 @@ module Cardano.DbSync.Cache.Types (
1313
CacheEpoch (..),
1414
CacheInternal (..),
1515
EpochBlockDiff (..),
16+
StakeCache (..),
1617
StakePoolCache,
1718

1819
-- * Inits
1920
useNoCache,
2021
initCacheStatistics,
2122
newEmptyCache,
2223

23-
-- * Helpers
24-
isCacheActionUpdate,
25-
2624
-- * CacheStatistics
2725
CacheStatistics (..),
2826
textShowStats,
@@ -49,6 +47,11 @@ import Ouroboros.Consensus.Cardano.Block (StandardCrypto)
4947

5048
type StakePoolCache = Map PoolKeyHash DB.PoolHashId
5149

50+
data StakeCache = StakeCache
51+
{ scStableCache :: !(Map StakeCred DB.StakeAddressId)
52+
, scLruCache :: !(LRUCache StakeCred DB.StakeAddressId)
53+
}
54+
5255
-- 'CacheStatus' enables functions in this module to be called even if the cache has not been initialized.
5356
-- This is used during genesis insertions, where the cache is not yet initiated, and when the user has disabled the cache functionality.
5457
data CacheStatus
@@ -57,12 +60,13 @@ data CacheStatus
5760

5861
data CacheAction
5962
= UpdateCache
63+
| UpdateCacheStrong
6064
| DoNotUpdateCache
6165
| EvictAndUpdateCache
6266
deriving (Eq)
6367

6468
data CacheInternal = CacheInternal
65-
{ cStakeRawHashes :: !(StrictTVar IO (LRUCache StakeCred DB.StakeAddressId))
69+
{ cStake :: !(StrictTVar IO StakeCache)
6670
, cPools :: !(StrictTVar IO StakePoolCache)
6771
, cDatum :: !(StrictTVar IO (LRUCache DataHash DB.DatumId))
6872
, cMultiAssets :: !(StrictTVar IO (LRUCache (PolicyID StandardCrypto, AssetName) DB.MultiAssetId))
@@ -89,7 +93,7 @@ data CacheStatistics = CacheStatistics
8993

9094
-- CacheCapacity is used to define capacities for different types of cache entries.
9195
data CacheCapacity = CacheCapacity
92-
{ cacheCapacityStakeHashRaw :: !Word64
96+
{ cacheCapacityStake :: !Word64
9397
, cacheCapacityDatum :: !Word64
9498
, cacheCapacityMultiAsset :: !Word64
9599
, cacheCapacityTx :: !Word64
@@ -118,7 +122,7 @@ textShowStats :: CacheStatus -> IO Text
118122
textShowStats NoCache = pure "NoCache"
119123
textShowStats (ActiveCache ic) = do
120124
stats <- readTVarIO $ cStats ic
121-
stakeHashRaws <- readTVarIO (cStakeRawHashes ic)
125+
stakeHashRaws <- readTVarIO (cStake ic)
122126
pools <- readTVarIO (cPools ic)
123127
datums <- readTVarIO (cDatum ic)
124128
mAssets <- readTVarIO (cMultiAssets ic)
@@ -127,8 +131,10 @@ textShowStats (ActiveCache ic) = do
127131
mconcat
128132
[ "\nCache Statistics:"
129133
, "\n Stake Addresses: "
130-
, "cache size: "
131-
, textShow (LRU.getCapacity stakeHashRaws)
134+
, "cache sizes: "
135+
, textShow (Map.size $ scStableCache stakeHashRaws)
136+
, " and "
137+
, textShow (LRU.getSize $ scLruCache stakeHashRaws)
132138
, if credsQueries stats == 0
133139
then ""
134140
else ", hit rate: " <> textShow (100 * credsHits stats `div` credsQueries stats) <> "%"
@@ -197,7 +203,7 @@ useNoCache = NoCache
197203

198204
newEmptyCache :: MonadIO m => CacheCapacity -> m CacheStatus
199205
newEmptyCache CacheCapacity {..} = liftIO $ do
200-
cStakeRawHashes <- newTVarIO (LRU.empty cacheCapacityStakeHashRaw)
206+
cStake <- newTVarIO (StakeCache Map.empty (LRU.empty cacheCapacityStake))
201207
cPools <- newTVarIO Map.empty
202208
cDatum <- newTVarIO (LRU.empty cacheCapacityDatum)
203209
cMultiAssets <- newTVarIO (LRU.empty cacheCapacityMultiAsset)
@@ -208,7 +214,7 @@ newEmptyCache CacheCapacity {..} = liftIO $ do
208214

209215
pure . ActiveCache $
210216
CacheInternal
211-
{ cStakeRawHashes = cStakeRawHashes
217+
{ cStake = cStake
212218
, cPools = cPools
213219
, cDatum = cDatum
214220
, cMultiAssets = cMultiAssets
@@ -223,7 +229,3 @@ initCacheStatistics = CacheStatistics 0 0 0 0 0 0 0 0 0 0 0 0
223229

224230
initCacheEpoch :: CacheEpoch
225231
initCacheEpoch = CacheEpoch mempty Nothing
226-
227-
isCacheActionUpdate :: CacheAction -> Bool
228-
isCacheActionUpdate UpdateCache = True
229-
isCacheActionUpdate _other = False

cardano-db/src/Cardano/Db/Query.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ queryLatestPoints = do
504504
pure (blk ^. BlockSlotNo, blk ^. BlockHash)
505505
pure $ fmap unValue2 res
506506

507-
-- This is an expensive query, but it is only used once when initiating the cStakeRawHashes LRU cache.
507+
-- This is an expensive query, but it is only used once when initiating the cStake LRU cache.
508508
-- The idea being to get a list of current registered stake addresses using rewards.
509509
queryAddressWithReward :: MonadIO m => Int -> ReaderT SqlBackend m [(ByteString, StakeAddressId)]
510510
queryAddressWithReward limitNumber = do

0 commit comments

Comments
 (0)