Skip to content

Commit

Permalink
Merge pull request #1905 from IntersectMBO/kderme/fixes-rollback
Browse files Browse the repository at this point in the history
Fix rollbacks
  • Loading branch information
kderme authored Nov 21, 2024
2 parents 5b131e9 + 8aceaef commit 1c941df
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 36 deletions.
2 changes: 1 addition & 1 deletion cardano-chain-gen/test/Test/Cardano/Db/Mock/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ withFullConfig' WithConfigArgs {..} cmdLineArgs mSyncNodeConfig configFilePath t
-- we dont fork dbsync here. Just prepare it as an action
withDBSyncEnv (mkDBSyncEnv dbsyncParams syncNodeConfig partialDbSyncRun) $ \dbSyncEnv -> do
let pgPass = getDBSyncPGPass dbSyncEnv
tableNames <- DB.getAllTablleNames pgPass
tableNames <- DB.getAllTableNames pgPass
-- We only want to create the table schema once for the tests so here we check
-- if there are any table names.
if null tableNames || shouldDropDB
Expand Down
4 changes: 2 additions & 2 deletions cardano-db-sync/src/Cardano/DbSync/Rollback.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ rollbackFromBlockNo syncEnv blkNo = do
, textShow blkNo
]
lift $ do
deletedBlockCount <- DB.deleteBlocksBlockId trce txOutTableType blockId epochNo (Just (DB.pcmConsumedTxOut $ getPruneConsume syncEnv))
deletedBlockCount <- DB.deleteBlocksBlockId trce txOutTableType blockId epochNo (DB.pcmConsumedTxOut $ getPruneConsume syncEnv)
when (deletedBlockCount > 0) $ do
-- We use custom constraints to improve input speeds when syncing.
-- If they don't already exists we add them here as once a rollback has happened
Expand Down Expand Up @@ -111,4 +111,4 @@ prepareRollback syncEnv point serverTip =
unsafeRollback :: Trace IO Text -> DB.TxOutTableType -> DB.PGConfig -> SlotNo -> IO (Either SyncNodeError ())
unsafeRollback trce txOutTableType config slotNo = do
logWarning trce $ "Starting a forced rollback to slot: " <> textShow (unSlotNo slotNo)
Right <$> DB.runDbNoLogging (DB.PGPassCached config) (void $ DB.deleteBlocksSlotNo trce txOutTableType slotNo Nothing)
Right <$> DB.runDbNoLogging (DB.PGPassCached config) (void $ DB.deleteBlocksSlotNo trce txOutTableType slotNo True)
6 changes: 3 additions & 3 deletions cardano-db/src/Cardano/Db/Migration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Cardano.Db.Migration (
getMigrationScripts,
runMigrations,
recreateDB,
getAllTablleNames,
getAllTableNames,
truncateTables,
dropTables,
getMaintenancePsqlConf,
Expand Down Expand Up @@ -298,8 +298,8 @@ recreateDB pgpass = do
rawExecute "drop schema if exists public cascade" []
rawExecute "create schema public" []

getAllTablleNames :: PGPassSource -> IO [Text]
getAllTablleNames pgpass = do
getAllTableNames :: PGPassSource -> IO [Text]
getAllTableNames pgpass = do
runWithConnectionNoLogging pgpass $ do
fmap unSingle <$> rawSql "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = current_schema()" []

Expand Down
63 changes: 33 additions & 30 deletions cardano-db/src/Cardano/Db/Operations/Delete.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
{-# LANGUAGE TypeOperators #-}

module Cardano.Db.Operations.Delete (
deleteBlocksSlotNo,
deleteBlocksSlotNoNoTrace,
deleteDelistedPool,
deleteBlocksBlockId,
queryDelete,
deleteBlocksSlotNo,
deleteBlocksSlotNoNoTrace,
deleteBlocksForTests,
deleteBlock,
queryDelete,
) where

import Cardano.BM.Trace (Trace, logInfo, logWarning, nullTracer)
Expand Down Expand Up @@ -54,32 +54,25 @@ import Database.Persist (
)
import Database.Persist.Sql (Filter, SqlBackend, delete, deleteWhere, deleteWhereCount, selectKeysList)

deleteBlocksSlotNoNoTrace :: MonadIO m => TxOutTableType -> SlotNo -> ReaderT SqlBackend m Bool
deleteBlocksSlotNoNoTrace txOutTableType slotNo = deleteBlocksSlotNo nullTracer txOutTableType slotNo Nothing

-- | Delete a block if it exists. Returns 'True' if it did exist and has been
-- deleted and 'False' if it did not exist.
deleteBlocksSlotNo ::
MonadIO m =>
Trace IO Text ->
TxOutTableType ->
SlotNo ->
Maybe Bool ->
Bool ->
ReaderT SqlBackend m Bool
deleteBlocksSlotNo trce txOutTableType (SlotNo slotNo) mIsConsumedTxOut = do
deleteBlocksSlotNo trce txOutTableType (SlotNo slotNo) isConsumedTxOut = do
mBlockId <- queryNearestBlockSlotNo slotNo
case mBlockId of
Nothing -> do
liftIO $ logWarning trce $ "deleteBlocksSlotNo: No block contains the the slot: " <> pack (show slotNo)
pure False
Just (blockId, epochN) -> do
void $ deleteBlocksBlockId trce txOutTableType blockId epochN mIsConsumedTxOut
void $ deleteBlocksBlockId trce txOutTableType blockId epochN isConsumedTxOut
pure True

deleteBlocksForTests :: MonadIO m => TxOutTableType -> BlockId -> Word64 -> ReaderT SqlBackend m ()
deleteBlocksForTests txOutTableType blockId epochN = do
void $ deleteBlocksBlockId nullTracer txOutTableType blockId epochN Nothing

-- | Delete starting from a 'BlockId'.
deleteBlocksBlockId ::
MonadIO m =>
Expand All @@ -89,20 +82,19 @@ deleteBlocksBlockId ::
-- | The 'EpochNo' of the block to delete.
Word64 ->
-- | Is ConsumeTxout
Maybe Bool ->
Bool ->
ReaderT SqlBackend m Int64
deleteBlocksBlockId trce txOutTableType blockId epochN mIsConsumedTxOut = do
deleteBlocksBlockId trce txOutTableType blockId epochN isConsumedTxOut = do
mMinIds <- fmap (textToMinIds txOutTableType =<<) <$> queryReverseIndexBlockId blockId
(cminIds, completed) <- findMinIdsRec mMinIds mempty
mTxId <- queryMinRefId TxBlockId blockId
minIds <- if completed then pure cminIds else completeMinId mTxId cminIds
deleteEpochLogs <- deleteUsingEpochNo epochN
(deleteBlockCount, blockDeleteLogs) <- deleteTablesAfterBlockId txOutTableType blockId mTxId minIds
setNullLogs <-
maybe
(pure ("ConsumedTxOut is not active so no Nulls set", 0))
(\_ -> querySetNullTxOut txOutTableType mTxId)
mIsConsumedTxOut
if isConsumedTxOut
then querySetNullTxOut txOutTableType mTxId
else pure ("ConsumedTxOut is not active so no Nulls set", 0)
-- log all the deleted rows in the rollback
liftIO $ logInfo trce $ mkRollbackSummary (deleteEpochLogs <> blockDeleteLogs) setNullLogs
pure deleteBlockCount
Expand Down Expand Up @@ -357,17 +349,6 @@ deleteDelistedPool poolHash = do
mapM_ delete keys
pure $ not (null keys)

-- | Delete a block if it exists. Returns 'True' if it did exist and has been
-- deleted and 'False' if it did not exist.
deleteBlock :: MonadIO m => TxOutTableType -> Block -> ReaderT SqlBackend m Bool
deleteBlock txOutTableType block = do
mBlockId <- queryBlockHash block
case mBlockId of
Nothing -> pure False
Just (blockId, epochN) -> do
void $ deleteBlocksBlockId nullTracer txOutTableType blockId epochN Nothing
pure True

mkRollbackSummary :: [(Text, Int64)] -> (Text, Int64) -> Text
mkRollbackSummary logs setNullLogs =
"\n----------------------- Rollback Summary: ----------------------- \n"
Expand All @@ -392,3 +373,25 @@ mkRollbackSummary logs setNullLogs =
<> if nullCount == 0
then nullMessage
else "\n\nSet Null: " <> nullMessage <> " - Count: " <> pack (show nullCount)

-- Tools

deleteBlocksSlotNoNoTrace :: MonadIO m => TxOutTableType -> SlotNo -> ReaderT SqlBackend m Bool
deleteBlocksSlotNoNoTrace txOutTableType slotNo = deleteBlocksSlotNo nullTracer txOutTableType slotNo True

-- Tests

deleteBlocksForTests :: MonadIO m => TxOutTableType -> BlockId -> Word64 -> ReaderT SqlBackend m ()
deleteBlocksForTests txOutTableType blockId epochN = do
void $ deleteBlocksBlockId nullTracer txOutTableType blockId epochN False

-- | Delete a block if it exists. Returns 'True' if it did exist and has been
-- deleted and 'False' if it did not exist.
deleteBlock :: MonadIO m => TxOutTableType -> Block -> ReaderT SqlBackend m Bool
deleteBlock txOutTableType block = do
mBlockId <- queryBlockHash block
case mBlockId of
Nothing -> pure False
Just (blockId, epochN) -> do
void $ deleteBlocksBlockId nullTracer txOutTableType blockId epochN False
pure True

0 comments on commit 1c941df

Please sign in to comment.