Skip to content

Commit 42a6bfe

Browse files
committed
convert DB.DbAction to DB.DbM
1 parent 7738702 commit 42a6bfe

File tree

104 files changed

+3308
-4999
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+3308
-4999
lines changed

cardano-chain-gen/cardano-chain-gen.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ test-suite cardano-chain-gen
192192
, transformers-except
193193
, tree-diff
194194
, tasty-hunit
195-
, monad-logger
196195
, ouroboros-consensus
197196
, ouroboros-consensus-cardano
198197
, ouroboros-network-api

cardano-chain-gen/test/Test/Cardano/Db/Mock/Config.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ import Control.Concurrent.STM.TMVar (
7474
import Control.Exception (SomeException, bracket)
7575
import Control.Monad (void)
7676
import Control.Monad.Extra (eitherM)
77-
import Control.Monad.Logger (NoLoggingT)
7877
import Control.Monad.Trans.Except.Extra (runExceptT)
7978
import Control.Tracer (nullTracer)
8079
import Data.Text (Text)
@@ -230,9 +229,9 @@ withDBSyncEnv mkEnv = bracket mkEnv stopDBSyncIfRunning
230229
getDBSyncPGPass :: DBSyncEnv -> DB.PGPassSource
231230
getDBSyncPGPass = enpPGPassSource . dbSyncParams
232231

233-
queryDBSync :: DBSyncEnv -> DB.DbAction (NoLoggingT IO) a -> IO a
232+
queryDBSync :: DBSyncEnv -> DB.DbM a -> IO a
234233
queryDBSync env = do
235-
DB.runWithConnectionNoLogging (getDBSyncPGPass env)
234+
DB.runDbStandaloneTransSilent (getDBSyncPGPass env)
236235

237236
getPoolLayer :: DBSyncEnv -> IO PoolDataLayer
238237
getPoolLayer env = do
@@ -385,6 +384,8 @@ emptyMetricsSetters =
385384
, metricsSetDbQueueLength = \_ -> pure ()
386385
, metricsSetDbBlockHeight = \_ -> pure ()
387386
, metricsSetDbSlotHeight = \_ -> pure ()
387+
, metricsSetDbEpochSyncDuration = \_ -> pure ()
388+
, metricsSetDbEpochSyncNumber = \_ -> pure ()
388389
}
389390

390391
withFullConfig ::

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Alonzo/Config.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ insertConfig = do
3434
, sioPoolStats = PoolStatsConfig False
3535
, sioJsonType = JsonTypeDisable
3636
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
37+
, sioStopAtBlock = Nothing
3738
}
3839

3940
dncInsertOptions cfg @?= expected

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Config/Parse.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ insertConfig = do
104104
, sioPoolStats = PoolStatsConfig False
105105
, sioJsonType = JsonTypeDisable
106106
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
107+
, sioStopAtBlock = Nothing
107108
}
108109

109110
dncInsertOptions cfg @?= expected

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Config/Schema.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ validateSchemaColumns =
7979
validateCall dbSync (Proxy @DB.MultiAsset)
8080
validateCall dbSync (Proxy @DB.MaTxMint)
8181

82-
-- Cardano.Db.Schema.Core.StakeDeligation
82+
-- Cardano.Db.Schema.Core.StakeDelegation
8383
validateCall dbSync (Proxy @DB.StakeAddress)
8484
validateCall dbSync (Proxy @DB.StakeRegistration)
8585
validateCall dbSync (Proxy @DB.StakeDeregistration)

cardano-chain-gen/test/Test/Cardano/Db/Mock/Validate.hs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ module Test.Cardano.Db.Mock.Validate (
4646
import Control.Concurrent
4747
import Control.Exception
4848
import Control.Monad (forM_)
49-
import Control.Monad.Logger (NoLoggingT)
5049
import Data.Bifunctor (first)
5150
import Data.ByteString (ByteString)
5251
import Data.Either (isRight)
@@ -69,7 +68,6 @@ import qualified Cardano.Ledger.Core as Core
6968
import Cardano.Ledger.Shelley.LedgerState (EraCertState)
7069
import Cardano.Mock.Forging.Tx.Generic
7170
import Cardano.Mock.Forging.Types
72-
import Cardano.Prelude (MonadIO)
7371
import Cardano.SMASH.Server.PoolDataLayer
7472
import Cardano.SMASH.Server.Types
7573
import Test.Cardano.Db.Mock.Config
@@ -129,16 +127,16 @@ assertUnspentTx dbSyncEnv = do
129127
defaultDelays :: [Int]
130128
defaultDelays = [1, 2, 4, 8, 16, 32, 64, 128, 256]
131129

132-
assertEqQuery :: (Eq a, Show a) => DBSyncEnv -> DB.DbAction (NoLoggingT IO) a -> a -> String -> IO ()
130+
assertEqQuery :: (Eq a, Show a) => DBSyncEnv -> DB.DbM a -> a -> String -> IO ()
133131
assertEqQuery env query a msg = do
134132
assertEqBackoff env query a defaultDelays msg
135133

136-
assertEqBackoff :: (Eq a, Show a) => DBSyncEnv -> DB.DbAction (NoLoggingT IO) a -> a -> [Int] -> String -> IO ()
134+
assertEqBackoff :: (Eq a, Show a) => DBSyncEnv -> DB.DbM a -> a -> [Int] -> String -> IO ()
137135
assertEqBackoff env query a delays msg = do
138136
checkStillRuns env
139137
assertBackoff env query delays (== a) (\a' -> msg <> ": got " <> show a' <> " expected " <> show a)
140138

141-
assertBackoff :: DBSyncEnv -> DB.DbAction (NoLoggingT IO) a -> [Int] -> (a -> Bool) -> (a -> String) -> IO ()
139+
assertBackoff :: DBSyncEnv -> DB.DbM a -> [Int] -> (a -> Bool) -> (a -> String) -> IO ()
142140
assertBackoff env query delays check errMsg = go delays
143141
where
144142
go ds = do
@@ -150,7 +148,7 @@ assertBackoff env query delays check errMsg = go delays
150148
threadDelay $ dl * 100_000
151149
go rest
152150

153-
assertQuery :: DBSyncEnv -> DB.DbAction (NoLoggingT IO) a -> (a -> Bool) -> (a -> String) -> IO (Maybe String)
151+
assertQuery :: DBSyncEnv -> DB.DbM a -> (a -> Bool) -> (a -> String) -> IO (Maybe String)
154152
assertQuery env query check errMsg = do
155153
ma <- try @DB.DbError $ queryDBSync env query
156154
case ma of
@@ -161,7 +159,7 @@ assertQuery env query check errMsg = do
161159
Right a | not (check a) -> pure $ Just $ errMsg a
162160
_ -> pure Nothing
163161

164-
runQuery :: DBSyncEnv -> DB.DbAction (NoLoggingT IO) a -> IO a
162+
runQuery :: DBSyncEnv -> DB.DbM a -> IO a
165163
runQuery env query = do
166164
ma <- try @DB.DbError $ queryDBSync env query
167165
case ma of
@@ -371,7 +369,7 @@ assertPoolCounters :: DBSyncEnv -> (Word64, Word64, Word64, Word64, Word64, Word
371369
assertPoolCounters env expected =
372370
assertEqBackoff env poolCountersQuery expected defaultDelays "Unexpected Pool counts"
373371

374-
poolCountersQuery :: MonadIO m => DB.DbAction m (Word64, Word64, Word64, Word64, Word64, Word64)
372+
poolCountersQuery :: DB.DbM (Word64, Word64, Word64, Word64, Word64, Word64)
375373
poolCountersQuery = do
376374
poolHash <- DB.queryPoolHashCount
377375
poolMetadataRef <- DB.queryPoolMetadataRefCount

cardano-db-sync/app/test-http-get-json-metadata.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import Cardano.DbSync.Types (
1616
OffChainUrlType (..),
1717
)
1818
import Control.Monad (foldM)
19-
import Control.Monad.IO.Class (MonadIO)
2019
import Control.Monad.Trans.Except.Extra (runExceptT)
2120
import Data.ByteString.Char8 (ByteString)
2221
import qualified Data.List as List
@@ -32,7 +31,7 @@ import Network.HTTP.Client.TLS (tlsManagerSettings)
3231
main :: IO ()
3332
main = do
3433
manager <- Http.newManager tlsManagerSettings
35-
xs <- DB.runDbNoLoggingEnv queryTestOffChainData
34+
xs <- DB.runDbStandaloneTransSilent DB.PGPassDefaultEnv queryTestOffChainData
3635
putStrLn $ "testOffChainPoolDataFetch: " ++ show (length xs) ++ " tests to run."
3736
tfs <- foldM (testOne manager) emptyTestFailure xs
3837
reportTestFailures tfs
@@ -74,7 +73,7 @@ data TestFailure = TestFailure
7473
, tfOtherError :: !Word
7574
}
7675

77-
queryTestOffChainData :: MonadIO m => DB.DbAction m [TestOffChain]
76+
queryTestOffChainData :: DB.DbM [TestOffChain]
7877
queryTestOffChainData = do
7978
res <- DB.queryTestOffChainData
8079
pure . organise $ map convert res

cardano-db-sync/cardano-db-sync.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ library
187187
, lifted-base
188188
, memory
189189
, microlens
190-
, monad-control
190+
-- , monad-control
191191
, network-mux
192192
, ouroboros-consensus
193193
, ouroboros-consensus-cardano
@@ -215,6 +215,7 @@ library
215215
, transformers
216216
, transformers-except
217217
, typed-protocols
218+
, unliftio-core
218219
, vector
219220
, wide-word
220221
, yaml

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import Cardano.DbSync.Era
5555
import Cardano.DbSync.Error
5656
import Cardano.DbSync.Ledger.State
5757
import Cardano.DbSync.OffChain (runFetchOffChainPoolThread, runFetchOffChainVoteThread)
58-
import Cardano.DbSync.Rollback (unsafeRollback)
58+
import Cardano.DbSync.Rollback (handlePostRollbackSnapshots, unsafeRollback)
5959
import Cardano.DbSync.Sync (runSyncNodeClient)
6060
import Cardano.DbSync.Tracing.ToObjectOrphans ()
6161
import Cardano.DbSync.Types
@@ -98,7 +98,7 @@ runMigrationsOnly knownMigrations trce params syncNodeConfigFromFile = do
9898
msg <- DB.getMaintenancePsqlConf pgConfig
9999
logInfo trce $ "Running database migrations in mode " <> textShow mode
100100
logInfo trce msg
101-
DB.runMigrations pgConfig True dbMigrationDir (Just $ DB.LogFileDir "/tmp") mode (txOutConfigToTableType txOutConfig)
101+
DB.runMigrations (Just trce) pgConfig True dbMigrationDir (Just $ DB.LogFileDir "/tmp") mode (txOutConfigToTableType txOutConfig)
102102

103103
-- Always run Initial mode only - never indexes
104104
(ranMigrations, unofficial) <- runMigration DB.Initial
@@ -150,7 +150,7 @@ runDbSync metricsSetters iomgr trce params syncNodeConfigFromFile abortOnPanic =
150150
logInfo trce $ "Running NearTip database migrations in mode " <> textShow mode
151151
logInfo trce msg
152152
when (mode `elem` [DB.NearTip, DB.Full]) $ logWarning trce indexesMsg
153-
DB.runMigrations pgConfig True dbMigrationDir (Just $ DB.LogFileDir "/tmp") mode (txOutConfigToTableType txOutConfig)
153+
DB.runMigrations (Just trce) pgConfig True dbMigrationDir (Just $ DB.LogFileDir "/tmp") mode (txOutConfigToTableType txOutConfig)
154154

155155
runSyncNode
156156
metricsSetters
@@ -199,7 +199,7 @@ runSyncNode metricsSetters trce iomgr dbConnSetting runNearTipMigrationFnc syncN
199199
let useLedger = shouldUseLedger (sioLedger $ dncInsertOptions syncNodeConfigFromFile)
200200
-- The main thread
201201
bracket
202-
(acquireDbConnection [dbConnSetting])
202+
(DB.acquireConnection [dbConnSetting])
203203
HsqlC.release
204204
( \dbConn -> do
205205
runOrThrowIO $ runExceptT $ do
@@ -208,14 +208,15 @@ runSyncNode metricsSetters trce iomgr dbConnSetting runNearTipMigrationFnc syncN
208208
pool <- liftIO $ DB.createHasqlConnectionPool [dbConnSetting] 4 -- 4 connections for reasonable parallelism
209209
let dbEnv =
210210
if isLogingEnabled
211-
then DB.createDbEnv dbConn pool (Just trce)
212-
else DB.createDbEnv dbConn pool Nothing
211+
then DB.createDbEnv dbConn (Just pool) (Just trce)
212+
else DB.createDbEnv dbConn (Just pool) Nothing
213213
genCfg <- readCardanoGenesisConfig syncNodeConfigFromFile
214214
isJsonbInSchema <- liftDbError $ DB.queryJsonbInSchemaExists dbConn
215215
logProtocolMagicId trce $ genesisProtocolMagicId genCfg
216216
syncEnv <-
217217
ExceptT $
218218
mkSyncEnvFromConfig
219+
metricsSetters
219220
trce
220221
dbEnv
221222
syncOptions
@@ -239,12 +240,15 @@ runSyncNode metricsSetters trce iomgr dbConnSetting runNearTipMigrationFnc syncN
239240
DB.noLedgerMigrations dbEnv trce
240241
insertValidateGenesisDist syncEnv (dncNetworkName syncNodeConfigFromFile) genCfg (useShelleyInit syncNodeConfigFromFile)
241242

243+
-- Handle ledger snapshots after rollback to ensure consistency
244+
liftIO $ handlePostRollbackSnapshots syncEnv (enpMaybeRollback syncNodeParams)
245+
242246
-- communication channel between datalayer thread and chainsync-client thread
243247
threadChannels <- liftIO newThreadChannels
244248
liftIO $
245249
race_
246250
-- We split the main thread into two parts to allow for graceful shutdown of the main App db thread.
247-
(runDbThread syncEnv metricsSetters threadChannels)
251+
(runDbThread syncEnv threadChannels)
248252
( mapConcurrently_
249253
id
250254
[ runSyncNodeClient metricsSetters syncEnv iomgr trce threadChannels (enpSocketPath syncNodeParams)

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

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ runConsumedTxOutMigrationsMaybe syncEnv = do
143143
let pcm = getPruneConsume syncEnv
144144
txOutVariantType = getTxOutVariantType syncEnv
145145
logInfo (getTrace syncEnv) $ "runConsumedTxOutMigrationsMaybe: " <> textShow pcm
146-
DB.runDbIohkNoLogging (envDbEnv syncEnv) $
146+
DB.runDbDirectSilent (envDbEnv syncEnv) $
147147
DB.runConsumedTxOutMigrations
148148
(getTrace syncEnv)
149149
maxBulkSize
@@ -153,11 +153,11 @@ runConsumedTxOutMigrationsMaybe syncEnv = do
153153

154154
runAddJsonbToSchema :: SyncEnv -> IO ()
155155
runAddJsonbToSchema syncEnv =
156-
void $ DB.runDbIohkNoLogging (envDbEnv syncEnv) DB.enableJsonbInSchema
156+
void $ DB.runDbDirectSilent (envDbEnv syncEnv) DB.enableJsonbInSchema
157157

158158
runRemoveJsonbFromSchema :: SyncEnv -> IO ()
159159
runRemoveJsonbFromSchema syncEnv =
160-
void $ DB.runDbIohkNoLogging (envDbEnv syncEnv) DB.disableJsonbInSchema
160+
void $ DB.runDbDirectSilent (envDbEnv syncEnv) DB.disableJsonbInSchema
161161

162162
getSafeBlockNoDiff :: SyncEnv -> Word64
163163
getSafeBlockNoDiff syncEnv = 2 * getSecurityParam syncEnv
@@ -243,7 +243,7 @@ getInsertOptions :: SyncEnv -> InsertOptions
243243
getInsertOptions = soptInsertOptions . envOptions
244244

245245
getSlotHash :: DB.DbEnv -> SlotNo -> IO [(SlotNo, ByteString)]
246-
getSlotHash backend = DB.runDbIohkNoLogging backend . DB.querySlotHash
246+
getSlotHash backend = DB.runDbDirectSilent backend . DB.querySlotHash
247247

248248
hasLedgerState :: SyncEnv -> Bool
249249
hasLedgerState syncEnv =
@@ -254,7 +254,7 @@ hasLedgerState syncEnv =
254254
getDbLatestBlockInfo :: DB.DbEnv -> IO (Maybe TipInfo)
255255
getDbLatestBlockInfo dbEnv = do
256256
runMaybeT $ do
257-
block <- MaybeT $ DB.runDbIohkNoLogging dbEnv DB.queryLatestBlock
257+
block <- MaybeT $ DB.runDbDirectSilent dbEnv DB.queryLatestBlock
258258
-- The EpochNo, SlotNo and BlockNo can only be zero for the Byron
259259
-- era, but we need to make the types match, hence `fromMaybe`.
260260
pure $
@@ -297,6 +297,7 @@ getCurrentTipBlockNo env = do
297297
Nothing -> pure Origin
298298

299299
mkSyncEnv ::
300+
MetricSetters ->
300301
Trace IO Text ->
301302
DB.DbEnv ->
302303
SyncOptions ->
@@ -308,18 +309,20 @@ mkSyncEnv ::
308309
SyncNodeParams ->
309310
RunMigration ->
310311
IO SyncEnv
311-
mkSyncEnv trce dbEnv syncOptions protoInfo nw nwMagic systemStart syncNodeConfigFromFile syncNP runNearTipMigrationFnc = do
312-
dbCNamesVar <- newTVarIO =<< DB.runDbActionIO dbEnv DB.queryRewardAndEpochStakeConstraints
312+
mkSyncEnv metricSetters trce dbEnv syncOptions protoInfo nw nwMagic systemStart syncNodeConfigFromFile syncNP runNearTipMigrationFnc = do
313+
dbCNamesVar <- newTVarIO =<< DB.runDbDirectSilent dbEnv DB.queryRewardAndEpochStakeConstraints
313314
cache <-
314315
if soptCache syncOptions
315316
then
316317
newEmptyCache
317318
CacheCapacity
318-
{ cacheCapacityAddress = 100000
319-
, cacheCapacityStake = 100000
320-
, cacheCapacityDatum = 250000
321-
, cacheCapacityMultiAsset = 250000
322-
, cacheCapacityTx = 100000
319+
{ cacheCapacityAddress = 50000
320+
, cacheCapacityStake = 50000
321+
, cacheCapacityDatum = 125000
322+
, cacheCapacityMultiAsset = 125000
323+
, cacheCapacityTx = 50000
324+
, cacheOptimisePools = 50000
325+
, cacheOptimiseStake = 50000
323326
}
324327
else pure useNoCache
325328
consistentLevelVar <- newTVarIO Unchecked
@@ -356,6 +359,7 @@ mkSyncEnv trce dbEnv syncOptions protoInfo nw nwMagic systemStart syncNodeConfig
356359
pure $
357360
SyncEnv
358361
{ envDbEnv = dbEnv
362+
, envMetricSetters = metricSetters
359363
, envBootstrap = bootstrapVar
360364
, envCache = cache
361365
, envEpochStatistics = epochStatistics
@@ -379,6 +383,7 @@ mkSyncEnv trce dbEnv syncOptions protoInfo nw nwMagic systemStart syncNodeConfig
379383
isTxOutConsumedBootstrap' = isTxOutConsumedBootstrap . sioTxOut . dncInsertOptions
380384

381385
mkSyncEnvFromConfig ::
386+
MetricSetters ->
382387
Trace IO Text ->
383388
DB.DbEnv ->
384389
SyncOptions ->
@@ -388,7 +393,7 @@ mkSyncEnvFromConfig ::
388393
-- | run migration function
389394
RunMigration ->
390395
IO (Either SyncNodeError SyncEnv)
391-
mkSyncEnvFromConfig trce dbEnv syncOptions genCfg syncNodeConfigFromFile syncNodeParams runNearTipMigrationFnc =
396+
mkSyncEnvFromConfig metricsSetters trce dbEnv syncOptions genCfg syncNodeConfigFromFile syncNodeParams runNearTipMigrationFnc =
392397
case genCfg of
393398
GenesisCardano _ bCfg sCfg _ _
394399
| unProtocolMagicId (Byron.configProtocolMagicId bCfg) /= Shelley.sgNetworkMagic (scConfig sCfg) ->
@@ -414,6 +419,7 @@ mkSyncEnvFromConfig trce dbEnv syncOptions genCfg syncNodeConfigFromFile syncNod
414419
| otherwise ->
415420
Right
416421
<$> mkSyncEnv
422+
metricsSetters
417423
trce
418424
dbEnv
419425
syncOptions
@@ -434,7 +440,7 @@ getLatestPoints env = do
434440
verifySnapshotPoint env snapshotPoints
435441
NoLedger _ -> do
436442
-- Brings the 5 latest.
437-
lastPoints <- DB.runDbIohkNoLogging (envDbEnv env) DB.queryLatestPoints
443+
lastPoints <- DB.runDbDirectSilent (envDbEnv env) DB.queryLatestPoints
438444
pure $ mapMaybe convert lastPoints
439445
where
440446
convert (Nothing, _) = Nothing
@@ -489,7 +495,7 @@ getBootstrapInProgress ::
489495
DB.DbEnv ->
490496
IO Bool
491497
getBootstrapInProgress trce bootstrapFlag dbEnv = do
492-
DB.runDbIohkNoLogging dbEnv $ do
498+
DB.runDbDirectSilent dbEnv $ do
493499
ems <- DB.queryAllExtraMigrations
494500
let btsState = DB.bootstrapState ems
495501
case (bootstrapFlag, btsState) of

0 commit comments

Comments
 (0)