@@ -74,37 +74,38 @@ import Ouroboros.Consensus.Cardano.Block (StandardCrypto)
7474-- NOTE: Other tables are not cleaned up since they are not rollbacked.
7575rollbackCache :: MonadIO m => CacheStatus -> DB. BlockId -> ReaderT SqlBackend m ()
7676rollbackCache NoCache _ = pure ()
77- rollbackCache (ActiveCache _ cache) blockId = do
77+ rollbackCache (ActiveCache cache) blockId = do
7878 liftIO $ do
7979 atomically $ writeTVar (cPrevBlock cache) Nothing
8080 atomically $ modifyTVar (cDatum cache) LRU. cleanup
8181 atomically $ modifyTVar (cTxIds cache) FIFO. cleanupCache
8282 void $ rollbackMapEpochInCache cache blockId
8383
84- -- When syncing and we get within 2 minutes of the tip, we can optimise the caches
85- -- and set the flag to True on ActiveCache.
86- optimiseCaches :: MonadIO m => CacheStatus -> ReaderT SqlBackend m CacheStatus
84+ -- | When syncing and we get within 2 minutes of the tip, we can optimise the caches
85+ -- and set the flag to True on ActiveCache.leaving the following caches as they are:
86+ -- cPools, cPrevBlock, Cstats, cEpoch
87+ optimiseCaches :: MonadIO m => CacheStatus -> ReaderT SqlBackend m ()
8788optimiseCaches cache =
8889 case cache of
89- NoCache -> pure cache
90- ActiveCache True _ -> pure cache
91- ActiveCache False c -> do
92- liftIO $ do
93- -- empty caches not to be used anymore
94- atomically $ modifyTVar (cTxIds c) FIFO. cleanupCache
95- atomically $ writeTVar (cStake c) (StakeCache Map. empty (LRU. empty 0 ))
96- atomically $ modifyTVar (cDatum c) (LRU. optimise 0 )
97- -- empty then limit the capacity of the cache
98- atomically $ writeTVar (cMultiAssets c) (LRU. empty 50000 )
99- -- leaving the following caches as they are:
100- -- cPools, cPrevBlock, Cstats, cEpoch
101- pure $ ActiveCache True c
90+ NoCache -> pure ()
91+ ActiveCache c ->
92+ withCacheOptimisationCheck c ( pure () ) $
93+ liftIO $ do
94+ -- empty caches not to be used anymore
95+ atomically $ modifyTVar (cTxIds c) FIFO. cleanupCache
96+ atomically $ writeTVar (cStake c) (StakeCache Map. empty (LRU. empty 0 ))
97+ atomically $ modifyTVar (cDatum c) (LRU. optimise 0 )
98+ -- empty then limit the capacity of the cache
99+ atomically $ writeTVar (cMultiAssets c) (LRU. empty 50000 )
100+ -- set the flag to True
101+ atomically $ writeTVar (cIsCacheOptimised c) True
102+ pure ()
102103
103104getCacheStatistics :: CacheStatus -> IO CacheStatistics
104105getCacheStatistics cs =
105106 case cs of
106107 NoCache -> pure initCacheStatistics
107- ActiveCache _ ci -> readTVarIO (cStats ci)
108+ ActiveCache ci -> readTVarIO (cStats ci)
108109
109110queryOrInsertRewardAccount ::
110111 (MonadBaseControl IO m , MonadIO m ) =>
@@ -170,34 +171,36 @@ queryStakeAddrWithCacheRetBs ::
170171queryStakeAddrWithCacheRetBs _trce cache cacheUA ra@ (Ledger. RewardAccount _ cred) = do
171172 let bs = Ledger. serialiseRewardAccount ra
172173 case cache of
173- NoCache -> mapLeft (,bs) <$> resolveStakeAddress bs
174- ActiveCache True _ -> mapLeft (,bs) <$> resolveStakeAddress bs
175- ActiveCache False ci -> do
176- stakeCache <- liftIO $ readTVarIO (cStake ci)
177- case queryStakeCache cred stakeCache of
178- Just (addrId, stakeCache') -> do
179- liftIO $ hitCreds (cStats ci)
180- case cacheUA of
181- EvictAndUpdateCache -> do
182- liftIO $ atomically $ writeTVar (cStake ci) $ deleteStakeCache cred stakeCache'
183- pure $ Right addrId
184- _other -> do
185- liftIO $ atomically $ writeTVar (cStake ci) stakeCache'
186- pure $ Right addrId
187- Nothing -> do
188- queryRes <- mapLeft (,bs) <$> resolveStakeAddress bs
189- liftIO $ missCreds (cStats ci)
190- case queryRes of
191- Left _ -> pure queryRes
192- Right stakeAddrsId -> do
193- let ! stakeCache' = case cacheUA of
194- UpdateCache -> stakeCache {scLruCache = LRU. insert cred stakeAddrsId (scLruCache stakeCache)}
195- UpdateCacheStrong -> stakeCache {scStableCache = Map. insert cred stakeAddrsId (scStableCache stakeCache)}
196- _ -> stakeCache
197- liftIO $
198- atomically $
199- writeTVar (cStake ci) stakeCache'
200- pure $ Right stakeAddrsId
174+ NoCache -> rsStkAdrrs bs
175+ ActiveCache ci -> do
176+ withCacheOptimisationCheck ci (rsStkAdrrs bs) $ do
177+ stakeCache <- liftIO $ readTVarIO (cStake ci)
178+ case queryStakeCache cred stakeCache of
179+ Just (addrId, stakeCache') -> do
180+ liftIO $ hitCreds (cStats ci)
181+ case cacheUA of
182+ EvictAndUpdateCache -> do
183+ liftIO $ atomically $ writeTVar (cStake ci) $ deleteStakeCache cred stakeCache'
184+ pure $ Right addrId
185+ _other -> do
186+ liftIO $ atomically $ writeTVar (cStake ci) stakeCache'
187+ pure $ Right addrId
188+ Nothing -> do
189+ queryRes <- mapLeft (,bs) <$> resolveStakeAddress bs
190+ liftIO $ missCreds (cStats ci)
191+ case queryRes of
192+ Left _ -> pure queryRes
193+ Right stakeAddrsId -> do
194+ let ! stakeCache' = case cacheUA of
195+ UpdateCache -> stakeCache {scLruCache = LRU. insert cred stakeAddrsId (scLruCache stakeCache)}
196+ UpdateCacheStrong -> stakeCache {scStableCache = Map. insert cred stakeAddrsId (scStableCache stakeCache)}
197+ _otherwise -> stakeCache
198+ liftIO $
199+ atomically $
200+ writeTVar (cStake ci) stakeCache'
201+ pure $ Right stakeAddrsId
202+ where
203+ rsStkAdrrs bs = mapLeft (,bs) <$> resolveStakeAddress bs
201204
202205-- | True if it was found in LRU
203206queryStakeCache :: StakeCred -> StakeCache -> Maybe (DB. StakeAddressId , StakeCache )
@@ -224,7 +227,7 @@ queryPoolKeyWithCache cache cacheUA hsh =
224227 case mPhId of
225228 Nothing -> pure $ Left (DB. DbLookupMessage " PoolKeyHash" )
226229 Just phId -> pure $ Right phId
227- ActiveCache _ ci -> do
230+ ActiveCache ci -> do
228231 mp <- liftIO $ readTVarIO (cPools ci)
229232 case Map. lookup hsh mp of
230233 Just phId -> do
@@ -264,7 +267,7 @@ insertPoolKeyWithCache cache cacheUA pHash =
264267 { DB. poolHashHashRaw = Generic. unKeyHashRaw pHash
265268 , DB. poolHashView = Generic. unKeyHashView pHash
266269 }
267- ActiveCache _ ci -> do
270+ ActiveCache ci -> do
268271 mp <- liftIO $ readTVarIO (cPools ci)
269272 case Map. lookup pHash mp of
270273 Just phId -> do
@@ -327,23 +330,23 @@ queryMAWithCache ::
327330queryMAWithCache cache policyId asset =
328331 case cache of
329332 NoCache -> queryDb
330- ActiveCache True _ -> queryDb
331- ActiveCache False ci -> do
332- mp <- liftIO $ readTVarIO (cMultiAssets ci)
333- case LRU. lookup (policyId, asset) mp of
334- Just (maId, mp') -> do
335- liftIO $ hitMAssets (cStats ci)
336- liftIO $ atomically $ writeTVar (cMultiAssets ci) mp'
337- pure $ Right maId
338- Nothing -> do
339- liftIO $ missMAssets (cStats ci)
340- -- miss. The lookup doesn't change the cache on a miss.
341- let ! policyBs = Generic. unScriptHash $ policyID policyId
342- let ! assetNameBs = Generic. unAssetName asset
343- maId <- maybe (Left (policyBs, assetNameBs)) Right <$> DB. queryMultiAssetId policyBs assetNameBs
344- whenRight maId $
345- liftIO . atomically . modifyTVar (cMultiAssets ci) . LRU. insert (policyId, asset)
346- pure maId
333+ ActiveCache ci -> do
334+ withCacheOptimisationCheck ci queryDb $ do
335+ mp <- liftIO $ readTVarIO (cMultiAssets ci)
336+ case LRU. lookup (policyId, asset) mp of
337+ Just (maId, mp') -> do
338+ liftIO $ hitMAssets (cStats ci)
339+ liftIO $ atomically $ writeTVar (cMultiAssets ci) mp'
340+ pure $ Right maId
341+ Nothing -> do
342+ liftIO $ missMAssets (cStats ci)
343+ -- miss. The lookup doesn't change the cache on a miss.
344+ let ! policyBs = Generic. unScriptHash $ policyID policyId
345+ let ! assetNameBs = Generic. unAssetName asset
346+ maId <- maybe (Left (policyBs, assetNameBs)) Right <$> DB. queryMultiAssetId policyBs assetNameBs
347+ whenRight maId $
348+ liftIO . atomically . modifyTVar (cMultiAssets ci) . LRU. insert (policyId, asset)
349+ pure maId
347350 where
348351 queryDb = do
349352 let ! policyBs = Generic. unScriptHash $ policyID policyId
@@ -359,7 +362,7 @@ queryPrevBlockWithCache ::
359362queryPrevBlockWithCache msg cache hsh =
360363 case cache of
361364 NoCache -> liftLookupFail msg $ DB. queryBlockId hsh
362- ActiveCache _ ci -> do
365+ ActiveCache ci -> do
363366 mCachedPrev <- liftIO $ readTVarIO (cPrevBlock ci)
364367 case mCachedPrev of
365368 -- if the cached block matches the requested hash, we return its db id.
@@ -388,28 +391,28 @@ queryTxIdWithCache cache txIdLedger = do
388391 case cache of
389392 -- Direct database query if no cache.
390393 NoCache -> qTxHash
391- ActiveCache True _ -> qTxHash
392- ActiveCache False cacheInternal -> do
393- -- Read current cache state.
394- cacheTx <- liftIO $ readTVarIO (cTxIds cacheInternal )
395-
396- case FIFO. lookup txIdLedger cacheTx of
397- -- Cache hit, return the transaction ID.
398- Just txId -> do
399- liftIO $ hitTxIds (cStats cacheInternal )
400- pure $ Right txId
401- -- Cache miss.
402- Nothing -> do
403- eTxId <- qTxHash
404- liftIO $ missTxIds (cStats cacheInternal )
405- case eTxId of
406- Right txId -> do
407- -- Update cache.
408- liftIO $ atomically $ modifyTVar (cTxIds cacheInternal ) $ FIFO. insert txIdLedger txId
409- -- Return ID after updating cache.
410- pure $ Right txId
411- -- Return lookup failure.
412- Left _ -> pure $ Left $ DB. DbLookupTxHash txHash
394+ ActiveCache ci ->
395+ withCacheOptimisationCheck ci qTxHash $ do
396+ -- Read current cache state.
397+ cacheTx <- liftIO $ readTVarIO (cTxIds ci )
398+
399+ case FIFO. lookup txIdLedger cacheTx of
400+ -- Cache hit, return the transaction ID.
401+ Just txId -> do
402+ liftIO $ hitTxIds (cStats ci )
403+ pure $ Right txId
404+ -- Cache miss.
405+ Nothing -> do
406+ eTxId <- qTxHash
407+ liftIO $ missTxIds (cStats ci )
408+ case eTxId of
409+ Right txId -> do
410+ -- Update cache.
411+ liftIO $ atomically $ modifyTVar (cTxIds ci ) $ FIFO. insert txIdLedger txId
412+ -- Return ID after updating cache.
413+ pure $ Right txId
414+ -- Return lookup failure.
415+ Left _ -> pure $ Left $ DB. DbLookupTxHash txHash
413416 where
414417 txHash = Generic. unTxHash txIdLedger
415418 qTxHash = DB. queryTxId txHash
@@ -420,7 +423,7 @@ tryUpdateCacheTx ::
420423 Ledger. TxId StandardCrypto ->
421424 DB. TxId ->
422425 m ()
423- tryUpdateCacheTx (ActiveCache False ci) ledgerTxId txId =
426+ tryUpdateCacheTx (ActiveCache ci) ledgerTxId txId =
424427 liftIO $ atomically $ modifyTVar (cTxIds ci) $ FIFO. insert ledgerTxId txId
425428tryUpdateCacheTx _ _ _ = pure ()
426429
@@ -432,13 +435,13 @@ insertBlockAndCache ::
432435insertBlockAndCache cache block =
433436 case cache of
434437 NoCache -> insBlck
435- ActiveCache True _ -> insBlck
436- ActiveCache False ci -> do
437- bid <- insBlck
438- liftIO $ do
439- missPrevBlock (cStats ci)
440- atomically $ writeTVar (cPrevBlock ci) $ Just (bid, DB. blockHash block)
441- pure bid
438+ ActiveCache ci ->
439+ withCacheOptimisationCheck ci insBlck $ do
440+ bid <- insBlck
441+ liftIO $ do
442+ missPrevBlock (cStats ci)
443+ atomically $ writeTVar (cPrevBlock ci) $ Just (bid, DB. blockHash block)
444+ pure bid
442445 where
443446 insBlck = DB. insertBlock block
444447
@@ -450,18 +453,18 @@ queryDatum ::
450453queryDatum cache hsh = do
451454 case cache of
452455 NoCache -> queryDtm
453- ActiveCache True _ -> queryDtm
454- ActiveCache False ci -> do
455- mp <- liftIO $ readTVarIO (cDatum ci)
456- case LRU. lookup hsh mp of
457- Just (datumId, mp') -> do
458- liftIO $ hitDatum (cStats ci)
459- liftIO $ atomically $ writeTVar (cDatum ci) mp'
460- pure $ Just datumId
461- Nothing -> do
462- liftIO $ missDatum (cStats ci)
463- -- miss. The lookup doesn't change the cache on a miss.
464- queryDtm
456+ ActiveCache ci -> do
457+ withCacheOptimisationCheck ci queryDtm $ do
458+ mp <- liftIO $ readTVarIO (cDatum ci)
459+ case LRU. lookup hsh mp of
460+ Just (datumId, mp') -> do
461+ liftIO $ hitDatum (cStats ci)
462+ liftIO $ atomically $ writeTVar (cDatum ci) mp'
463+ pure $ Just datumId
464+ Nothing -> do
465+ liftIO $ missDatum (cStats ci)
466+ -- miss. The lookup doesn't change the cache on a miss.
467+ queryDtm
465468 where
466469 queryDtm = DB. queryDatum $ Generic. dataHashToBytes hsh
467470
@@ -476,13 +479,25 @@ insertDatumAndCache cache hsh dt = do
476479 datumId <- DB. insertDatum dt
477480 case cache of
478481 NoCache -> pure datumId
479- ActiveCache True _ -> pure datumId
480- ActiveCache False ci -> do
481- liftIO $
482- atomically $
483- modifyTVar (cDatum ci) $
484- LRU. insert hsh datumId
485- pure datumId
482+ ActiveCache ci ->
483+ withCacheOptimisationCheck ci (pure datumId) $ do
484+ liftIO $
485+ atomically $
486+ modifyTVar (cDatum ci) $
487+ LRU. insert hsh datumId
488+ pure datumId
489+
490+ withCacheOptimisationCheck ::
491+ MonadIO m =>
492+ CacheInternal ->
493+ m a -> -- Action to perform if cache is optimised
494+ m a -> -- Action to perform if cache is not optimised
495+ m a
496+ withCacheOptimisationCheck ci ifOptimised ifNotOptimised = do
497+ isCachedOptimised <- liftIO $ readTVarIO (cIsCacheOptimised ci)
498+ if isCachedOptimised
499+ then ifOptimised
500+ else ifNotOptimised
486501
487502-- Stakes
488503hitCreds :: StrictTVar IO CacheStatistics -> IO ()
0 commit comments