Skip to content

Commit f3b215c

Browse files
committed
add custom buffer sizes & additional pipelines
1 parent 018a0bf commit f3b215c

File tree

30 files changed

+296
-245
lines changed

30 files changed

+296
-245
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,19 @@ runSyncNode metricsSetters trce iomgr dbConnSetting runNearTipMigrationFnc syncN
203203
HsqlC.release
204204
( \dbConn -> do
205205
runOrThrowIO $ runExceptT $ do
206-
let isLogingEnabled = dncEnableDbLogging syncNodeConfigFromFile
207206
-- Create connection pool for parallel operations
208207
pool <- liftIO $ DB.createHasqlConnectionPool [dbConnSetting] 4 -- 4 connections for reasonable parallelism
209-
let dbEnv =
210-
if isLogingEnabled
211-
then DB.createDbEnv dbConn (Just pool) (Just trce)
212-
else DB.createDbEnv dbConn (Just pool) Nothing
208+
let dbEnv = DB.createDbEnv dbConn (Just pool) (Just trce)
213209
genCfg <- readCardanoGenesisConfig syncNodeConfigFromFile
214210
isJsonbInSchema <- liftSessionIO mkSyncNodeCallStack $ DB.queryJsonbInSchemaExists dbConn
215211
logProtocolMagicId trce $ genesisProtocolMagicId genCfg
212+
213+
-- Determine the final JSONB state after any schema migrations
214+
let finalJsonbInSchema = case (isJsonbInSchema, removeJsonbFromSchemaConfig) of
215+
(True, True) -> False -- Will be removed
216+
(False, False) -> True -- Will be added
217+
(s, _) -> s -- No change
218+
216219
syncEnv <-
217220
ExceptT $
218221
mkSyncEnvFromConfig
@@ -224,6 +227,7 @@ runSyncNode metricsSetters trce iomgr dbConnSetting runNearTipMigrationFnc syncN
224227
syncNodeConfigFromFile
225228
syncNodeParams
226229
runNearTipMigrationFnc
230+
finalJsonbInSchema
227231

228232
-- Warn the user that jsonb datatypes are being removed from the database schema.
229233
when (isJsonbInSchema && removeJsonbFromSchemaConfig) $ do
@@ -250,8 +254,8 @@ runSyncNode metricsSetters trce iomgr dbConnSetting runNearTipMigrationFnc syncN
250254
id
251255
[ runDbThread syncEnv threadChannels
252256
, runSyncNodeClient metricsSetters syncEnv iomgr trce threadChannels (enpSocketPath syncNodeParams)
253-
, runFetchOffChainPoolThread syncEnv syncNodeConfigFromFile
254-
, runFetchOffChainVoteThread syncEnv syncNodeConfigFromFile
257+
, runFetchOffChainPoolThread syncEnv
258+
, runFetchOffChainVoteThread syncEnv
255259
, runLedgerStateWriteThread (getTrace syncEnv) (envLedgerEnv syncEnv)
256260
]
257261
)

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ isConsistent env = do
106106
cst <- getConsistentLevel env
107107
case cst of
108108
Consistent -> pure True
109-
_otherwise -> pure False
109+
_ -> pure False
110110

111111
getDisableInOutState :: SyncEnv -> IO Bool
112112
getDisableInOutState syncEnv = do
@@ -142,11 +142,13 @@ runConsumedTxOutMigrationsMaybe :: SyncEnv -> IO ()
142142
runConsumedTxOutMigrationsMaybe syncEnv = do
143143
let pcm = getPruneConsume syncEnv
144144
txOutVariantType = getTxOutVariantType syncEnv
145+
bulkSize = DB.getTxOutBulkSize txOutVariantType
146+
145147
logInfo (getTrace syncEnv) $ "runConsumedTxOutMigrationsMaybe: " <> textShow pcm
146148
DB.runDbDirectSilent (envDbEnv syncEnv) $
147149
DB.runConsumedTxOutMigrations
148150
(getTrace syncEnv)
149-
maxBulkSize
151+
bulkSize
150152
txOutVariantType
151153
(getSafeBlockNoDiff syncEnv)
152154
pcm
@@ -308,8 +310,9 @@ mkSyncEnv ::
308310
SyncNodeConfig ->
309311
SyncNodeParams ->
310312
RunMigration ->
313+
Bool ->
311314
IO SyncEnv
312-
mkSyncEnv metricSetters trce dbEnv syncOptions protoInfo nw nwMagic systemStart syncNodeConfigFromFile syncNP runNearTipMigrationFnc = do
315+
mkSyncEnv metricSetters trce dbEnv syncOptions protoInfo nw nwMagic systemStart syncNodeConfigFromFile syncNP runNearTipMigrationFnc isJsonbInSchema = do
313316
dbCNamesVar <- newTVarIO =<< DB.runDbDirectSilent dbEnv DB.queryRewardAndEpochStakeConstraints
314317
cache <-
315318
if soptCache syncOptions
@@ -376,6 +379,7 @@ mkSyncEnv metricSetters trce dbEnv syncOptions protoInfo nw nwMagic systemStart
376379
, envOptions = syncOptions
377380
, envRunNearTipMigration = runNearTipMigrationFnc
378381
, envSyncNodeConfig = syncNodeConfigFromFile
382+
, envIsJsonbInSchema = isJsonbInSchema
379383
, envSystemStart = systemStart
380384
}
381385
where
@@ -392,8 +396,9 @@ mkSyncEnvFromConfig ::
392396
SyncNodeParams ->
393397
-- | run migration function
394398
RunMigration ->
399+
Bool ->
395400
IO (Either SyncNodeError SyncEnv)
396-
mkSyncEnvFromConfig metricsSetters trce dbEnv syncOptions genCfg syncNodeConfigFromFile syncNodeParams runNearTipMigrationFnc =
401+
mkSyncEnvFromConfig metricsSetters trce dbEnv syncOptions genCfg syncNodeConfigFromFile syncNodeParams runNearTipMigrationFnc isJsonbInSchema =
397402
case genCfg of
398403
GenesisCardano _ bCfg sCfg _ _
399404
| unProtocolMagicId (Byron.configProtocolMagicId bCfg) /= Shelley.sgNetworkMagic (scConfig sCfg) ->
@@ -430,6 +435,7 @@ mkSyncEnvFromConfig metricsSetters trce dbEnv syncOptions genCfg syncNodeConfigF
430435
syncNodeConfigFromFile
431436
syncNodeParams
432437
runNearTipMigrationFnc
438+
isJsonbInSchema
433439

434440
-- | 'True' is for in memory points and 'False' for on disk
435441
getLatestPoints :: SyncEnv -> IO [(CardanoPoint, Bool)]

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import Cardano.DbSync.Era.Universal.Insert.Tx (insertTxOut)
4343
import Cardano.DbSync.Error (SyncNodeError, mkSyncNodeCallStack)
4444
import Cardano.DbSync.Ledger.State
4545
import Cardano.DbSync.Types
46-
import Cardano.DbSync.Util (maxBulkSize)
4746

4847
bootStrapMaybe ::
4948
SyncEnv ->
@@ -105,12 +104,13 @@ storeUTxO env mp = do
105104
[ "Inserting "
106105
, textShow size
107106
, " tx_out as pages of "
108-
, textShow maxBulkSize
107+
, textShow bulkSize
109108
]
110-
mapM_ (storePage env pagePerc) . zip [0 ..] . chunksOf maxBulkSize . Map.toList $ mp
109+
mapM_ (storePage env pagePerc) . zip [0 ..] . chunksOf bulkSize . Map.toList $ mp
111110
where
112111
trce = getTrace env
113-
npages = size `div` maxBulkSize
112+
bulkSize = DB.getTxOutBulkSize (getTxOutVariantType env)
113+
npages = size `div` bulkSize
114114
pagePerc :: Float = if npages == 0 then 100.0 else 100.0 / fromIntegral npages
115115
size = Map.size mp
116116

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ data SyncEnv = SyncEnv
6060
, envSyncNodeConfig :: !SyncNodeConfig
6161
, envRunNearTipMigration :: RunMigration
6262
, envSystemStart :: !SystemStart
63+
, envIsJsonbInSchema :: !Bool
6364
}
6465

6566
data SyncOptions = SyncOptions

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ cleanCachesForTip cache =
8989
case cache of
9090
NoCache -> pure ()
9191
ActiveCache c ->
92-
withCacheCleanedForTipCheck c (pure ()) $
92+
withCacheCleanedCheck c (pure ()) $
9393
liftIO $ do
9494
-- empty caches not to be used anymore
9595
atomically $ modifyTVar (cTxIds c) FIFO.cleanupCache
@@ -174,7 +174,7 @@ queryStakeAddrWithCacheRetBs syncEnv cacheUA ra@(Ledger.RewardAccount _ cred) =
174174
case envCache syncEnv of
175175
NoCache -> (,bs) <$> resolveStakeAddress bs
176176
ActiveCache ci -> do
177-
result <- withCacheCleanedForTipCheck ci (resolveStakeAddress bs) $ do
177+
result <- withCacheCleanedCheck ci (resolveStakeAddress bs) $ do
178178
stakeCache <- liftIO $ readTVarIO (cStake ci)
179179
case queryStakeCache cred stakeCache of
180180
Just (addrId, stakeCache') -> do
@@ -379,7 +379,7 @@ queryMAWithCache syncEnv policyId asset =
379379
case envCache syncEnv of
380380
NoCache -> lift queryDb
381381
ActiveCache ci -> do
382-
withCacheCleanedForTipCheck ci (lift queryDb) $ do
382+
withCacheCleanedCheck ci (lift queryDb) $ do
383383
mp <- liftIO $ readTVarIO (cMultiAssets ci)
384384
case LRU.lookup (policyId, asset) mp of
385385
Just (maId, mp') -> do
@@ -437,7 +437,7 @@ queryTxIdWithCache syncEnv txIdLedger = do
437437
-- Direct database query if no cache.
438438
NoCache -> lift qTxHash
439439
ActiveCache ci ->
440-
withCacheCleanedForTipCheck ci (lift qTxHash) $ do
440+
withCacheCleanedCheck ci (lift qTxHash) $ do
441441
-- Read current cache state.
442442
cacheTx <- liftIO $ readTVarIO (cTxIds ci)
443443

@@ -485,7 +485,7 @@ insertBlockAndCache syncEnv block =
485485
case envCache syncEnv of
486486
NoCache -> lift insBlck
487487
ActiveCache ci ->
488-
withCacheCleanedForTipCheck ci (lift insBlck) $ do
488+
withCacheCleanedCheck ci (lift insBlck) $ do
489489
bid <- lift insBlck
490490
liftIO $ do
491491
missPrevBlock syncEnv
@@ -502,7 +502,7 @@ queryDatum syncEnv hsh = do
502502
case envCache syncEnv of
503503
NoCache -> lift queryDtm
504504
ActiveCache ci -> do
505-
withCacheCleanedForTipCheck ci (lift queryDtm) $ do
505+
withCacheCleanedCheck ci (lift queryDtm) $ do
506506
mp <- liftIO $ readTVarIO (cDatum ci)
507507
case LRU.lookup hsh mp of
508508
Just (datumId, mp') -> do
@@ -527,24 +527,24 @@ insertDatumAndCache cache hsh dt = do
527527
case cache of
528528
NoCache -> pure datumId
529529
ActiveCache ci ->
530-
withCacheCleanedForTipCheck ci (pure datumId) $ do
530+
withCacheCleanedCheck ci (pure datumId) $ do
531531
liftIO $
532532
atomically $
533533
modifyTVar (cDatum ci) $
534534
LRU.insert hsh datumId
535535
pure datumId
536536

537-
withCacheCleanedForTipCheck ::
537+
withCacheCleanedCheck ::
538538
MonadIO m =>
539539
CacheInternal ->
540540
m a -> -- Action to perform if cache is cleaned for tip
541541
m a -> -- Action to perform if cache is not cleaned for tip
542542
m a
543-
withCacheCleanedForTipCheck ci ifCleanedForTip ifNotCleanedForTip = do
543+
withCacheCleanedCheck ci actionIfCleaned actionIfNotCleaned = do
544544
isCacheCleanedForTip <- liftIO $ readTVarIO (cIsCacheCleanedForTip ci)
545545
if isCacheCleanedForTip
546-
then ifCleanedForTip
547-
else ifNotCleanedForTip
546+
then actionIfCleaned
547+
else actionIfNotCleaned
548548

549549
-- Creds
550550
hitCreds :: SyncEnv -> IO ()

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ data CacheCapacity = CacheCapacity
111111
, cacheCapacityDatum :: !Word64
112112
, cacheCapacityMultiAsset :: !Word64
113113
, cacheCapacityTx :: !Word64
114-
, -- Optimisation target sizes for Map-based caches (used every 100k blocks)
114+
, -- Used by optimiseCaches function to trim Map-based caches that can grow without bounds,
115+
-- unlike LRU caches which have built-in capacity limits. Trimming keeps most recent entries.
115116
cacheOptimisePools :: !Word64
116117
, cacheOptimiseStake :: !Word64
117118
}
@@ -145,16 +146,16 @@ textShowCacheStats stats (ActiveCache ic) = do
145146
mAssets <- readTVarIO (cMultiAssets ic)
146147
txIds <- readTVarIO (cTxIds ic)
147148
address <- readTVarIO (cAddress ic)
148-
pure $
149-
mconcat
149+
pure
150+
$ mconcat
150151
[ "\n\nEpoch Cache Statistics: "
151152
, "\n Caches Cleaned For Tip: " <> textShow isCacheCleanedForTip
152153
, textCacheSection " Stake Addresses" (scLruCache stakeHashRaws) (scStableCache stakeHashRaws) (credsHits stats) (credsQueries stats)
153154
, textMapSection " Pools" pools (poolsHits stats) (poolsQueries stats)
154155
, textLruSection " Datums" datums (datumHits stats) (datumQueries stats)
155156
, textLruSection " Addresses" address (addressHits stats) (addressQueries stats)
156157
, textLruSection " Multi Assets" mAssets (multiAssetsHits stats) (multiAssetsQueries stats)
157-
, textPrevBlockSection
158+
, textPrevBlockSection " Previous Block"
158159
, textFifoSection " TxId" txIds (txIdsHits stats) (txIdsQueries stats)
159160
]
160161
where
@@ -196,9 +197,9 @@ textShowCacheStats stats (ActiveCache ic) = do
196197
, hitMissStats hits queries
197198
]
198199

199-
textPrevBlockSection =
200+
textPrevBlockSection title =
200201
mconcat
201-
[ "\nPrevious Block: "
202+
[ "\n" <> title <> ": "
202203
, hitMissStats (prevBlockHits stats) (prevBlockQueries stats)
203204
]
204205

@@ -231,8 +232,9 @@ newEmptyCache CacheCapacity {..} = liftIO $ do
231232
cEpoch <- newTVarIO initCacheEpoch
232233
cTxIds <- newTVarIO (FIFO.empty cacheCapacityTx)
233234

234-
pure . ActiveCache $
235-
CacheInternal
235+
pure
236+
. ActiveCache
237+
$ CacheInternal
236238
{ cIsCacheCleanedForTip = cIsCacheCleanedForTip
237239
, cStake = cStake
238240
, cPools = cPools

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ coalesceConfig pcfg ncfg adjustGenesisPath = do
6666
, dncProtocol = ncProtocol ncfg
6767
, dncRequiresNetworkMagic = ncRequiresNetworkMagic ncfg
6868
, dncEnableLogging = pcEnableLogging pcfg
69-
, dncEnableDbLogging = pcEnableDbLogging pcfg
7069
, dncEnableMetrics = pcEnableMetrics pcfg
7170
, dncPrometheusPort = pcPrometheusPort pcfg
7271
, dncPBftSignatureThreshold = ncPBftSignatureThreshold ncfg

cardano-db-sync/src/Cardano/DbSync/Config/Byron.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{-# LANGUAGE FlexibleInstances #-}
22
{-# LANGUAGE MultiParamTypeClasses #-}
3-
{-# LANGUAGE OverloadedStrings #-}
43
{-# LANGUAGE RankNTypes #-}
54

65
module Cardano.DbSync.Config.Byron (

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ data SyncNodeConfig = SyncNodeConfig
127127
, dncProtocol :: !SyncProtocol
128128
, dncRequiresNetworkMagic :: !RequiresNetworkMagic
129129
, dncEnableLogging :: !Bool
130-
, dncEnableDbLogging :: !Bool
131130
, dncEnableMetrics :: !Bool
132131
, dncPrometheusPort :: !Int
133132
, dncPBftSignatureThreshold :: !(Maybe Double)
@@ -156,7 +155,6 @@ data SyncPreConfig = SyncPreConfig
156155
, pcNodeConfigFile :: !NodeConfigFile
157156
, pcEnableFutureGenesis :: !Bool
158157
, pcEnableLogging :: !Bool
159-
, pcEnableDbLogging :: !Bool
160158
, pcEnableMetrics :: !Bool
161159
, pcPrometheusPort :: !Int
162160
, pcInsertConfig :: !SyncInsertConfig
@@ -405,7 +403,6 @@ parseGenSyncNodeConfig o =
405403
<*> fmap NodeConfigFile (o .: "NodeConfigFile")
406404
<*> fmap (fromMaybe True) (o .:? "EnableFutureGenesis")
407405
<*> o .: "EnableLogging"
408-
<*> fmap (fromMaybe False) (o .:? "EnableDbLogging")
409406
<*> o .: "EnableLogMetrics"
410407
<*> fmap (fromMaybe 8080) (o .:? "PrometheusPort")
411408
<*> o .:? "insert_options" .!= def

0 commit comments

Comments
 (0)