@@ -74,37 +74,38 @@ import Ouroboros.Consensus.Cardano.Block (StandardCrypto)
74
74
-- NOTE: Other tables are not cleaned up since they are not rollbacked.
75
75
rollbackCache :: MonadIO m => CacheStatus -> DB. BlockId -> ReaderT SqlBackend m ()
76
76
rollbackCache NoCache _ = pure ()
77
- rollbackCache (ActiveCache _ cache) blockId = do
77
+ rollbackCache (ActiveCache cache) blockId = do
78
78
liftIO $ do
79
79
atomically $ writeTVar (cPrevBlock cache) Nothing
80
80
atomically $ modifyTVar (cDatum cache) LRU. cleanup
81
81
atomically $ modifyTVar (cTxIds cache) FIFO. cleanupCache
82
82
void $ rollbackMapEpochInCache cache blockId
83
83
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 ()
87
88
optimiseCaches cache =
88
89
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 ()
102
103
103
104
getCacheStatistics :: CacheStatus -> IO CacheStatistics
104
105
getCacheStatistics cs =
105
106
case cs of
106
107
NoCache -> pure initCacheStatistics
107
- ActiveCache _ ci -> readTVarIO (cStats ci)
108
+ ActiveCache ci -> readTVarIO (cStats ci)
108
109
109
110
queryOrInsertRewardAccount ::
110
111
(MonadBaseControl IO m , MonadIO m ) =>
@@ -170,34 +171,36 @@ queryStakeAddrWithCacheRetBs ::
170
171
queryStakeAddrWithCacheRetBs _trce cache cacheUA ra@ (Ledger. RewardAccount _ cred) = do
171
172
let bs = Ledger. serialiseRewardAccount ra
172
173
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
201
204
202
205
-- | True if it was found in LRU
203
206
queryStakeCache :: StakeCred -> StakeCache -> Maybe (DB. StakeAddressId , StakeCache )
@@ -224,7 +227,7 @@ queryPoolKeyWithCache cache cacheUA hsh =
224
227
case mPhId of
225
228
Nothing -> pure $ Left (DB. DbLookupMessage " PoolKeyHash" )
226
229
Just phId -> pure $ Right phId
227
- ActiveCache _ ci -> do
230
+ ActiveCache ci -> do
228
231
mp <- liftIO $ readTVarIO (cPools ci)
229
232
case Map. lookup hsh mp of
230
233
Just phId -> do
@@ -264,7 +267,7 @@ insertPoolKeyWithCache cache cacheUA pHash =
264
267
{ DB. poolHashHashRaw = Generic. unKeyHashRaw pHash
265
268
, DB. poolHashView = Generic. unKeyHashView pHash
266
269
}
267
- ActiveCache _ ci -> do
270
+ ActiveCache ci -> do
268
271
mp <- liftIO $ readTVarIO (cPools ci)
269
272
case Map. lookup pHash mp of
270
273
Just phId -> do
@@ -327,23 +330,23 @@ queryMAWithCache ::
327
330
queryMAWithCache cache policyId asset =
328
331
case cache of
329
332
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
347
350
where
348
351
queryDb = do
349
352
let ! policyBs = Generic. unScriptHash $ policyID policyId
@@ -359,7 +362,7 @@ queryPrevBlockWithCache ::
359
362
queryPrevBlockWithCache msg cache hsh =
360
363
case cache of
361
364
NoCache -> liftLookupFail msg $ DB. queryBlockId hsh
362
- ActiveCache _ ci -> do
365
+ ActiveCache ci -> do
363
366
mCachedPrev <- liftIO $ readTVarIO (cPrevBlock ci)
364
367
case mCachedPrev of
365
368
-- if the cached block matches the requested hash, we return its db id.
@@ -388,28 +391,28 @@ queryTxIdWithCache cache txIdLedger = do
388
391
case cache of
389
392
-- Direct database query if no cache.
390
393
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
413
416
where
414
417
txHash = Generic. unTxHash txIdLedger
415
418
qTxHash = DB. queryTxId txHash
@@ -420,7 +423,7 @@ tryUpdateCacheTx ::
420
423
Ledger. TxId StandardCrypto ->
421
424
DB. TxId ->
422
425
m ()
423
- tryUpdateCacheTx (ActiveCache False ci) ledgerTxId txId =
426
+ tryUpdateCacheTx (ActiveCache ci) ledgerTxId txId =
424
427
liftIO $ atomically $ modifyTVar (cTxIds ci) $ FIFO. insert ledgerTxId txId
425
428
tryUpdateCacheTx _ _ _ = pure ()
426
429
@@ -432,13 +435,13 @@ insertBlockAndCache ::
432
435
insertBlockAndCache cache block =
433
436
case cache of
434
437
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
442
445
where
443
446
insBlck = DB. insertBlock block
444
447
@@ -450,18 +453,18 @@ queryDatum ::
450
453
queryDatum cache hsh = do
451
454
case cache of
452
455
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
465
468
where
466
469
queryDtm = DB. queryDatum $ Generic. dataHashToBytes hsh
467
470
@@ -476,13 +479,25 @@ insertDatumAndCache cache hsh dt = do
476
479
datumId <- DB. insertDatum dt
477
480
case cache of
478
481
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
486
501
487
502
-- Stakes
488
503
hitCreds :: StrictTVar IO CacheStatistics -> IO ()
0 commit comments