Skip to content

Commit 28761a8

Browse files
authored
Merge pull request #2076 from IntersectMBO/2041-fix-isolation-commits
2041 - fix isolation commits
2 parents 6cedcdf + b4b722d commit 28761a8

6 files changed

Lines changed: 26 additions & 24 deletions

File tree

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE ApplicativeDo #-}
12
{-# LANGUAGE BangPatterns #-}
23
{-# LANGUAGE FlexibleContexts #-}
34
{-# LANGUAGE GADTs #-}
@@ -43,6 +44,7 @@ module Cardano.DbSync.Api (
4344
generateNewEpochEvents,
4445
logDbState,
4546
convertToPoint,
47+
determineIsolationLevel,
4648
)
4749
where
4850

@@ -245,7 +247,7 @@ getInsertOptions :: SyncEnv -> InsertOptions
245247
getInsertOptions = soptInsertOptions . envOptions
246248

247249
getSlotHash :: DB.DbEnv -> SlotNo -> IO [(SlotNo, ByteString)]
248-
getSlotHash backend = DB.runDbDirectSilent backend . DB.querySlotHash
250+
getSlotHash backend slotNo = DB.runDbTransSilent backend DB.ReadCommitted (DB.querySlotHash slotNo)
249251

250252
hasLedgerState :: SyncEnv -> Bool
251253
hasLedgerState syncEnv =
@@ -256,7 +258,7 @@ hasLedgerState syncEnv =
256258
getDbLatestBlockInfo :: DB.DbEnv -> IO (Maybe TipInfo)
257259
getDbLatestBlockInfo dbEnv = do
258260
runMaybeT $ do
259-
block <- MaybeT $ DB.runDbDirectSilent dbEnv DB.queryLatestBlock
261+
block <- MaybeT $ DB.runDbTransSilent dbEnv DB.ReadCommitted DB.queryLatestBlock
260262
-- The EpochNo, SlotNo and BlockNo can only be zero for the Byron
261263
-- era, but we need to make the types match, hence `fromMaybe`.
262264
pure $
@@ -267,6 +269,14 @@ getDbLatestBlockInfo dbEnv = do
267269
, bBlockNo = BlockNo . fromMaybe 0 $ DB.blockBlockNo block
268270
}
269271

272+
-- | Determine isolation level based on sync state
273+
determineIsolationLevel :: SyncEnv -> IO (Maybe DB.IsolationLevel)
274+
determineIsolationLevel syncEnv = do
275+
syncState <- readTVarIO (envDbIsolationState syncEnv)
276+
pure $ case syncState of
277+
DB.SyncLagging -> Just DB.ReadCommitted
278+
DB.SyncFollowing -> Nothing
279+
270280
getDbTipBlockNo :: SyncEnv -> IO (Point.WithOrigin BlockNo)
271281
getDbTipBlockNo env = do
272282
mblk <- getDbLatestBlockInfo (envDbEnv env)
@@ -314,7 +324,7 @@ mkSyncEnv ::
314324
Bool ->
315325
IO SyncEnv
316326
mkSyncEnv metricSetters trce dbEnv syncOptions protoInfo nw maxLovelaceSupply nwMagic systemStart syncNodeConfigFromFile syncNP runNearTipMigrationFnc isJsonbInSchema = do
317-
dbCNamesVar <- newTVarIO =<< DB.runDbDirectSilent dbEnv DB.queryRewardAndEpochStakeConstraints
327+
dbCNamesVar <- newTVarIO =<< DB.runDbTransSilent dbEnv DB.ReadCommitted DB.queryRewardAndEpochStakeConstraints
318328
cache <-
319329
if soptCache syncOptions
320330
then
@@ -506,7 +516,7 @@ getBootstrapInProgress ::
506516
DB.DbEnv ->
507517
IO Bool
508518
getBootstrapInProgress trce bootstrapFlag dbEnv = do
509-
DB.runDbDirectSilent dbEnv $ do
519+
DB.runDbTransSilent dbEnv DB.ReadCommitted $ do
510520
ems <- DB.queryAllExtraMigrations
511521
let btsState = DB.bootstrapState ems
512522
case (bootstrapFlag, btsState) of

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ runDbSyncTransactionNoLogging ::
8989
m (Either SyncNodeError a)
9090
runDbSyncTransactionNoLogging dbEnv exceptTAction = do
9191
let dbAction = runExceptT exceptTAction
92-
eResult <- liftIO $ try $ DB.runDbTransSilent dbEnv Nothing dbAction
92+
eResult <- liftIO $ try $ DB.runDbTransSilent dbEnv DB.RepeatableRead dbAction
9393
case eResult of
9494
Left (dbErr :: DB.DbSessionError) -> do
9595
pure $ Left $ SNErrDbSessionErr mkSyncNodeCallStack dbErr

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import Cardano.DbSync.Rollback
4646
import Cardano.DbSync.Types
4747
import Cardano.DbSync.Util
4848
import Cardano.DbSync.Util.Constraint (addConstraintsIfNotExist)
49-
import Control.Concurrent.Class.MonadSTM.Strict (readTVarIO)
5049

5150
insertListBlocks ::
5251
SyncEnv ->
@@ -249,13 +248,6 @@ insertBlock syncEnv cblk applyRes firstAfterRollback tookSnapshot = do
249248
blkNo = headerFieldBlockNo $ getHeaderFields cblk
250249

251250
-- | Determine isolation level based on current sync state
252-
determineIsolationLevel :: SyncEnv -> IO (Maybe DB.IsolationLevel)
253-
determineIsolationLevel syncEnv = do
254-
syncState <- readTVarIO (envDbIsolationState syncEnv)
255-
pure $ case syncState of
256-
DB.SyncLagging -> Just DB.ReadCommitted -- Syncing: use ReadCommitted for performance
257-
DB.SyncFollowing -> Nothing -- Following: use default RepeatableRead for consistency
258-
259251
isWithinTwoMin :: SlotDetails -> Bool
260252
isWithinTwoMin sd = isSyncedWithinSeconds sd 120 == SyncFollowing
261253

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module Cardano.DbSync.OffChain (
2323

2424
import Cardano.BM.Trace (Trace, logInfo)
2525
import qualified Cardano.Db as DB
26-
import Cardano.DbSync.Api
26+
import Cardano.DbSync.Api (determineIsolationLevel, getInsertOptions, getTrace)
2727
import Cardano.DbSync.Api.Types (InsertOptions (..), SyncEnv (..))
2828
import Cardano.DbSync.Config.Types
2929
import Cardano.DbSync.OffChain.Http
@@ -248,9 +248,10 @@ runFetchOffChainPoolThread syncEnv = do
248248
threadSyncEnv = syncEnv {envDbEnv = dbEnv}
249249
forever $ do
250250
tDelay
251-
-- load the offChain vote work queue using the db
251+
-- Use dynamic isolation level based on sync state
252+
mIsolationLevel <- determineIsolationLevel syncEnv
252253
_ <-
253-
DB.runDbDirectLogged trce dbEnv $
254+
DB.runDbTransLogged trce dbEnv mIsolationLevel $
254255
loadOffChainPoolWorkQueue trce (envOffChainPoolWorkQueue threadSyncEnv)
255256
poolq <- atomically $ flushTBQueue (envOffChainPoolWorkQueue threadSyncEnv)
256257
manager <- Http.newManager tlsManagerSettings
@@ -284,9 +285,10 @@ runFetchOffChainVoteThread syncEnv = do
284285
-- Use the thread-specific SyncEnv for all operations
285286
forever $ do
286287
tDelay
287-
-- load the offChain vote work queue using the db
288+
-- Use dynamic isolation level based on sync state
289+
mIsolationLevel <- determineIsolationLevel syncEnv
288290
_ <-
289-
DB.runDbDirectLogged trce dbEnv $
291+
DB.runDbTransLogged trce dbEnv mIsolationLevel $
290292
loadOffChainVoteWorkQueue trce (envOffChainVoteWorkQueue threadSyncEnv)
291293
voteq <- atomically $ flushTBQueue (envOffChainVoteWorkQueue threadSyncEnv)
292294
now <- liftIO Time.getPOSIXTime

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ rollbackFromBlockNo syncEnv blkNo = do
6868

6969
prepareRollback :: SyncEnv -> CardanoPoint -> Tip CardanoBlock -> IO (Either SyncNodeError Bool)
7070
prepareRollback syncEnv point serverTip = do
71-
DB.runDbDirectSilent (envDbEnv syncEnv) $ runExceptT action
71+
DB.runDbTransSilent (envDbEnv syncEnv) DB.ReadCommitted $ runExceptT action
7272
where
7373
trce = getTrace syncEnv
7474

cardano-db/src/Cardano/Db/Run.hs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,19 @@ runDbTransLogged tracer dbEnv mIsolationLevel action = do
9090
runDbTransSilent ::
9191
MonadUnliftIO m =>
9292
DbEnv ->
93-
Maybe IsolationLevel -> -- Optional isolation level
93+
IsolationLevel ->
9494
DbM a ->
9595
m a
96-
runDbTransSilent dbEnv mIsolationLevel action = do
96+
runDbTransSilent dbEnv isolationLevel action = do
9797
runNoLoggingT $ do
9898
result <- liftIO $ HsqlS.run transactionSession (dbConnection dbEnv)
9999
case result of
100100
Left sessionErr ->
101101
throwIO $ DbSessionError mkDbCallStack ("Database transaction error: " <> formatSessionError sessionErr)
102102
Right dbResult -> pure dbResult
103103
where
104-
isolationLevel = fromMaybe RepeatableRead mIsolationLevel
105104
transactionSession = do
106105
HsqlS.statement () (beginTransactionStmt isolationLevel)
107-
108106
result <- liftIO $ try @SomeException $ runReaderT (runDbM action) dbEnv
109107
case result of
110108
Left err -> do
@@ -272,7 +270,7 @@ runDbStandaloneTransSilent source action = do
272270
HsqlCon.release
273271
( \connection -> do
274272
let dbEnv = createDbEnv connection Nothing Nothing
275-
runDbTransSilent dbEnv Nothing action
273+
runDbTransSilent dbEnv RepeatableRead action
276274
)
277275

278276
-- | Standalone runner without transaction management

0 commit comments

Comments
 (0)