Skip to content

Commit 049349c

Browse files
Cmdvkderme
authored andcommitted
LRU updated on newly inserted stake addresses
stuff
1 parent b5e867f commit 049349c

File tree

17 files changed

+279
-283
lines changed

17 files changed

+279
-283
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import qualified Cardano.Chain.Genesis as Byron
5555
import Cardano.Crypto.ProtocolMagic (ProtocolMagicId (..))
5656
import qualified Cardano.Db as DB
5757
import Cardano.DbSync.Api.Types
58+
import Cardano.DbSync.Cache.LRU (LRUCacheCapacity (..))
5859
import Cardano.DbSync.Cache.Types (newEmptyCache, useNoCache)
5960
import Cardano.DbSync.Config.Cardano
6061
import Cardano.DbSync.Config.Shelley
@@ -97,7 +98,6 @@ import Ouroboros.Consensus.Protocol.Abstract (ConsensusProtocol)
9798
import Ouroboros.Network.Block (BlockNo (..), Point (..))
9899
import Ouroboros.Network.Magic (NetworkMagic (..))
99100
import qualified Ouroboros.Network.Point as Point
100-
import Cardano.DbSync.Cache.LRU (LRUCacheCapacity(..))
101101

102102
setConsistentLevel :: SyncEnv -> ConsistentLevel -> IO ()
103103
setConsistentLevel env cst = do
@@ -385,13 +385,14 @@ mkSyncEnv trce backend connectionString syncOptions protoInfo nw nwMagic systemS
385385
dbCNamesVar <- newTVarIO =<< dbConstraintNamesExists backend
386386
cache <-
387387
if soptCache syncOptions
388-
then newEmptyCache
389-
LRUCacheCapacity
390-
{ lirCapacityStakeHashRaw = 300000,
391-
lruCapacityDatum = 250000,
392-
lruCapacityMultiAsset = 50000
388+
then
389+
newEmptyCache
390+
LRUCacheCapacity
391+
{ lirCapacityStakeHashRaw = 1600000
392+
, lruCapacityDatum = 250000
393+
, lruCapacityMultiAsset = 250000
393394
}
394-
else pure cacheStatusNoCache
395+
else pure useNoCache
395396
consistentLevelVar <- newTVarIO Unchecked
396397
fixDataVar <- newTVarIO $ if ranMigrations then DataFixRan else NoneFixRan
397398
indexesVar <- newTVarIO $ enpForceIndexes syncNP

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

Lines changed: 74 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import Cardano.BM.Trace
3030
import qualified Cardano.Db as DB
3131
import Cardano.DbSync.Cache.Epoch (rollbackMapEpochInCache)
3232
import qualified Cardano.DbSync.Cache.LRU as LRU
33-
import Cardano.DbSync.Cache.Types (CacheInternal (..), CacheStatistics (..), CacheStatus (..), CacheUpdateAction (..), StakeAddrCache, initCacheStatistics)
33+
import Cardano.DbSync.Cache.Types (CacheAction (..), CacheInternal (..), CacheStatistics (..), CacheStatus (..), initCacheStatistics, isCacheActionUpdate)
3434
import qualified Cardano.DbSync.Era.Shelley.Generic.Util as Generic
3535
import Cardano.DbSync.Era.Shelley.Query
3636
import Cardano.DbSync.Era.Util
@@ -85,47 +85,40 @@ queryOrInsertRewardAccount ::
8585
(MonadBaseControl IO m, MonadIO m) =>
8686
Trace IO Text ->
8787
CacheStatus ->
88-
CacheUpdateAction ->
88+
CacheAction ->
8989
Ledger.RewardAccount StandardCrypto ->
9090
ReaderT SqlBackend m DB.StakeAddressId
91-
queryOrInsertRewardAccount trce cacheStatus cacheUA rewardAddr = do
92-
eiAddrId <- queryRewardAccountWithCacheRetBs trce cacheStatus rewardAddr
91+
queryOrInsertRewardAccount trce cache cacheUA rewardAddr = do
92+
eiAddrId <- queryRewardAccountWithCacheRetBs trce cache cacheUA rewardAddr
9393
case eiAddrId of
94-
Left (_err, bs) -> insertStakeAddress cacheStatus rewardAddr (Just bs)
94+
Left (_err, bs) -> insertStakeAddress rewardAddr (Just bs)
9595
Right addrId -> pure addrId
9696

9797
queryOrInsertStakeAddress ::
9898
(MonadBaseControl IO m, MonadIO m) =>
9999
Trace IO Text ->
100100
CacheStatus ->
101-
CacheUpdateAction ->
101+
CacheAction ->
102102
Network ->
103103
StakeCred ->
104104
ReaderT SqlBackend m DB.StakeAddressId
105-
queryOrInsertStakeAddress trce cacheStatus cacheUA nw cred =
106-
queryOrInsertRewardAccount trce cacheStatus cacheUA $ Ledger.RewardAccount nw cred
105+
queryOrInsertStakeAddress trce cache cacheUA nw cred =
106+
queryOrInsertRewardAccount trce cache cacheUA $ Ledger.RewardAccount nw cred
107107

108108
-- If the address already exists in the table, it will not be inserted again (due to
109109
-- the uniqueness constraint) but the function will return the 'StakeAddressId'.
110110
insertStakeAddress ::
111111
(MonadBaseControl IO m, MonadIO m) =>
112-
CacheStatus ->
113112
Ledger.RewardAccount StandardCrypto ->
114113
Maybe ByteString ->
115114
ReaderT SqlBackend m DB.StakeAddressId
116-
insertStakeAddress cacheStatus rewardAddr stakeCredBs = do
117-
addrId <- DB.insertStakeAddress $
118-
DB.StakeAddress
119-
{ DB.stakeAddressHashRaw = addrBs
120-
, DB.stakeAddressView = Generic.renderRewardAccount rewardAddr
121-
, DB.stakeAddressScriptHash = Generic.getCredentialScriptHash $ Ledger.raCredential rewardAddr
122-
}
123-
case cacheStatus of
124-
NoCache -> pure addrId
125-
CacheActive ci -> do
126-
liftIO $ atomically $ modifyTVar (cStakeRawHashes ci) $
127-
LRU.insert addrBs addrId
128-
pure addrId
115+
insertStakeAddress rewardAddr stakeCredBs = do
116+
DB.insertStakeAddress $
117+
DB.StakeAddress
118+
{ DB.stakeAddressHashRaw = addrBs
119+
, DB.stakeAddressView = Generic.renderRewardAccount rewardAddr
120+
, DB.stakeAddressScriptHash = Generic.getCredentialScriptHash $ Ledger.raCredential rewardAddr
121+
}
129122
where
130123
addrBs = fromMaybe (Ledger.serialiseRewardAccount rewardAddr) stakeCredBs
131124

@@ -134,97 +127,83 @@ queryRewardAccountWithCacheRetBs ::
134127
MonadIO m =>
135128
Trace IO Text ->
136129
CacheStatus ->
137-
CacheUpdateAction ->
130+
CacheAction ->
138131
Ledger.RewardAccount StandardCrypto ->
139132
ReaderT SqlBackend m (Either (DB.LookupFail, ByteString) DB.StakeAddressId)
140-
queryRewardAccountWithCacheRetBs trce cacheStatus cacheUA rwdAcc =
141-
queryStakeAddrWithCacheRetBs trce cacheStatus cacheUA (Ledger.raNetwork rwdAcc) (Ledger.raCredential rwdAcc)
133+
queryRewardAccountWithCacheRetBs trce cache cacheUA rwdAcc =
134+
queryStakeAddrWithCacheRetBs trce cache cacheUA (Ledger.raNetwork rwdAcc) (Ledger.raCredential rwdAcc)
142135

143136
queryStakeAddrWithCache ::
144137
forall m.
145138
MonadIO m =>
146139
Trace IO Text ->
147140
CacheStatus ->
148-
CacheUpdateAction ->
141+
CacheAction ->
149142
Network ->
150143
StakeCred ->
151144
ReaderT SqlBackend m (Either DB.LookupFail DB.StakeAddressId)
152-
queryStakeAddrWithCache trce cacheStatus cacheUA nw cred =
153-
mapLeft fst <$> queryStakeAddrWithCacheRetBs trce cacheStatus cacheUA nw cred
145+
queryStakeAddrWithCache trce cache cacheUA nw cred =
146+
mapLeft fst <$> queryStakeAddrWithCacheRetBs trce cache cacheUA nw cred
154147

155148
queryStakeAddrWithCacheRetBs ::
156149
forall m.
157150
MonadIO m =>
158151
Trace IO Text ->
159152
CacheStatus ->
160-
CacheUpdateAction ->
153+
CacheAction ->
161154
Network ->
162155
StakeCred ->
163156
ReaderT SqlBackend m (Either (DB.LookupFail, ByteString) DB.StakeAddressId)
164-
queryStakeAddrWithCacheRetBs trce cacheStatus cacheUA nw cred = do
157+
queryStakeAddrWithCacheRetBs trce cache cacheUA nw cred = do
165158
let !bs = Ledger.serialiseRewardAccount (Ledger.RewardAccount nw cred)
166-
case cacheStatus of
159+
case cache of
167160
NoCache -> do
168161
mapLeft (,bs) <$> queryStakeAddress bs
169-
CacheActive ci -> do
170-
currentCache <- liftIO $ readTVarIO (cStakeRawHashes ci)
171-
let cacheSize = LRU.getSize currentCache
172-
newCache <-
173-
if cacheSize < 1
174-
then do
175-
liftIO $ logInfo trce "----------------- Cache is empty. Querying all addresses. ---------"
176-
queryRes <- DB.queryLatestAddresses cacheSize
177-
pure $ LRU.fromList queryRes currentCache
178-
-- convert the results into the cache
179-
else pure currentCache
180-
case LRU.lookup bs newCache of
181-
Just (addrId, mp') -> do
162+
ActiveCache ci -> do
163+
prevCache <- liftIO $ readTVarIO (cStakeRawHashes ci)
164+
let isNewCache = LRU.getSize prevCache < 1
165+
-- populate from db if the cache is empty
166+
currentCache <-
167+
if isNewCache
168+
then do
169+
liftIO $ logInfo trce "Stake Raw Hashes cache is new and empty. Populating with addresses from db..."
170+
queryRes <- DB.queryAddressWithReward (fromIntegral $ LRU.getCapacity prevCache)
171+
liftIO $ atomically $ writeTVar (cStakeRawHashes ci) $ LRU.fromList queryRes prevCache
172+
liftIO $ logInfo trce "Population of cache complete."
173+
liftIO $ readTVarIO (cStakeRawHashes ci)
174+
else pure prevCache
175+
176+
case LRU.lookup bs currentCache of
177+
Just (addrId, lruCache) -> do
182178
liftIO $ hitCreds (cStats ci)
183-
liftIO $ atomically $ writeTVar (cStakeRawHashes ci) mp'
184-
pure $ Right addrId
179+
case cacheUA of
180+
EvictAndUpdateCache -> do
181+
liftIO $ atomically $ writeTVar (cStakeRawHashes ci) $ LRU.delete bs lruCache
182+
pure $ Right addrId
183+
_other -> do
184+
liftIO $ atomically $ writeTVar (cStakeRawHashes ci) lruCache
185+
pure $ Right addrId
185186
Nothing -> do
186-
liftIO $ missCreds (cStats ci)
187-
liftIO $ atomically $ writeTVar (cStakeRawHashes ci) newCache
188187
queryRes <- mapLeft (,bs) <$> queryStakeAddress bs
188+
liftIO $ missCreds (cStats ci)
189189
case queryRes of
190190
Left _ -> pure queryRes
191191
Right stakeAddrsId -> do
192-
liftIO $ atomically $ modifyTVar (cStakeRawHashes ci) $
193-
LRU.insert bs stakeAddrsId
192+
when (isCacheActionUpdate cacheUA) $
193+
liftIO $
194+
atomically $
195+
modifyTVar (cStakeRawHashes ci) $
196+
LRU.insert bs stakeAddrsId
194197
pure $ Right stakeAddrsId
195198

196-
-- queryStakeAddrAux ::
197-
-- MonadIO m =>
198-
-- CacheNew ->
199-
-- StakeAddrCache ->
200-
-- StrictTVar IO CacheStatistics ->
201-
-- Network ->
202-
-- StakeCred ->
203-
-- ReaderT SqlBackend m (Either (DB.LookupFail, ByteString) DB.StakeAddressId, StakeAddrCache)
204-
-- queryStakeAddrAux cacheNew mp sts nw cred =
205-
-- case Map.lookup cred mp of
206-
-- Just addrId -> do
207-
-- liftIO $ hitCreds sts
208-
-- case cacheNew of
209-
-- EvictAndReturn -> pure (Right addrId, Map.delete cred mp)
210-
-- _ -> pure (Right addrId, mp)
211-
-- Nothing -> do
212-
-- liftIO $ missCreds sts
213-
-- let !bs = Ledger.serialiseRewardAccount (Ledger.RewardAccount nw cred)
214-
-- mAddrId <- mapLeft (,bs) <$> queryStakeAddress bs
215-
-- case (mAddrId, cacheNew) of
216-
-- (Right addrId, CacheNew) -> pure (Right addrId, Map.insert cred addrId mp)
217-
-- (Right addrId, _) -> pure (Right addrId, mp)
218-
-- (err, _) -> pure (err, mp)
219-
220199
queryPoolKeyWithCache ::
221200
MonadIO m =>
222201
CacheStatus ->
223-
CacheUpdateAction ->
202+
CacheAction ->
224203
PoolKeyHash ->
225204
ReaderT SqlBackend m (Either DB.LookupFail DB.PoolHashId)
226-
queryPoolKeyWithCache cacheStatus cacheUA hsh =
227-
case cacheStatus of
205+
queryPoolKeyWithCache cache cacheUA hsh =
206+
case cache of
228207
NoCache -> do
229208
mPhId <- queryPoolHashId (Generic.unKeyHashRaw hsh)
230209
case mPhId of
@@ -259,11 +238,11 @@ queryPoolKeyWithCache cacheStatus cacheUA hsh =
259238
insertPoolKeyWithCache ::
260239
(MonadBaseControl IO m, MonadIO m) =>
261240
CacheStatus ->
262-
CacheUpdateAction ->
241+
CacheAction ->
263242
PoolKeyHash ->
264243
ReaderT SqlBackend m DB.PoolHashId
265-
insertPoolKeyWithCache cacheStatus cacheUA pHash =
266-
case cacheStatus of
244+
insertPoolKeyWithCache cache cacheUA pHash =
245+
case cache of
267246
NoCache ->
268247
DB.insertPoolHash $
269248
DB.PoolHash
@@ -301,12 +280,12 @@ queryPoolKeyOrInsert ::
301280
Text ->
302281
Trace IO Text ->
303282
CacheStatus ->
304-
CacheUpdateAction ->
283+
CacheAction ->
305284
Bool ->
306285
PoolKeyHash ->
307286
ReaderT SqlBackend m DB.PoolHashId
308-
queryPoolKeyOrInsert txt trce cacheStatus cacheUA logsWarning hsh = do
309-
pk <- queryPoolKeyWithCache cacheStatus cacheUA hsh
287+
queryPoolKeyOrInsert txt trce cache cacheUA logsWarning hsh = do
288+
pk <- queryPoolKeyWithCache cache cacheUA hsh
310289
case pk of
311290
Right poolHashId -> pure poolHashId
312291
Left err -> do
@@ -322,16 +301,16 @@ queryPoolKeyOrInsert txt trce cacheStatus cacheUA logsWarning hsh = do
322301
, txt
323302
, ". We will assume that the pool exists and move on."
324303
]
325-
insertPoolKeyWithCache cacheStatus cacheUA hsh
304+
insertPoolKeyWithCache cache cacheUA hsh
326305

327306
queryMAWithCache ::
328307
MonadIO m =>
329308
CacheStatus ->
330309
PolicyID StandardCrypto ->
331310
AssetName ->
332311
ReaderT SqlBackend m (Either (ByteString, ByteString) DB.MultiAssetId)
333-
queryMAWithCache cacheStatus policyId asset =
334-
case cacheStatus of
312+
queryMAWithCache cache policyId asset =
313+
case cache of
335314
NoCache -> do
336315
let !policyBs = Generic.unScriptHash $ policyID policyId
337316
let !assetNameBs = Generic.unAssetName asset
@@ -359,8 +338,8 @@ queryPrevBlockWithCache ::
359338
CacheStatus ->
360339
ByteString ->
361340
ExceptT SyncNodeError (ReaderT SqlBackend m) DB.BlockId
362-
queryPrevBlockWithCache msg cacheStatus hsh =
363-
case cacheStatus of
341+
queryPrevBlockWithCache msg cache hsh =
342+
case cache of
364343
NoCache -> liftLookupFail msg $ DB.queryBlockId hsh
365344
ActiveCache ci -> do
366345
mCachedPrev <- liftIO $ readTVarIO (cPrevBlock ci)
@@ -387,8 +366,8 @@ insertBlockAndCache ::
387366
CacheStatus ->
388367
DB.Block ->
389368
ReaderT SqlBackend m DB.BlockId
390-
insertBlockAndCache cacheStatus block =
391-
case cacheStatus of
369+
insertBlockAndCache cache block =
370+
case cache of
392371
NoCache -> DB.insertBlock block
393372
ActiveCache ci -> do
394373
bid <- DB.insertBlock block
@@ -402,10 +381,10 @@ queryDatum ::
402381
CacheStatus ->
403382
DataHash ->
404383
ReaderT SqlBackend m (Maybe DB.DatumId)
405-
queryDatum cacheStatus hsh = do
406-
case cacheStatus of
384+
queryDatum cache hsh = do
385+
case cache of
407386
NoCache -> DB.queryDatum $ Generic.dataHashToBytes hsh
408-
CacheActive ci -> do
387+
ActiveCache ci -> do
409388
mp <- liftIO $ readTVarIO (cDatum ci)
410389
case LRU.lookup hsh mp of
411390
Just (datumId, mp') -> do
@@ -424,9 +403,9 @@ insertDatumAndCache ::
424403
DataHash ->
425404
DB.Datum ->
426405
ReaderT SqlBackend m DB.DatumId
427-
insertDatumAndCache cacheStatus hsh dt = do
406+
insertDatumAndCache cache hsh dt = do
428407
datumId <- DB.insertDatum dt
429-
case cacheStatus of
408+
case cache of
430409
NoCache -> pure datumId
431410
ActiveCache ci -> do
432411
liftIO $

0 commit comments

Comments
 (0)