Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WORK IN PROGRESS] Add db cleanup logic for checkpointer #1105

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Chainweb/Pact/Backend/InMemoryCheckpointer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ initInMemoryCheckpointEnv loggers logger ver cid = do
, _cpLookupProcessedTx = doLookupSuccessful inmem
, _cpGetBlockHistory = \_ _ -> error "unimplemented"
, _cpGetHistoricalLookup = \_ _ _ -> error "unimplemented"
, _cpCleanupDb = doDiscard inmem
}
, _cpeLogger = logger
})
Expand Down
8 changes: 8 additions & 0 deletions src/Chainweb/Pact/Backend/RelationalCheckpointer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,20 @@ initRelationalCheckpointer' bstate sqlenv loggr v cid = do
, _cpLookupProcessedTx = doLookupSuccessful db
, _cpGetBlockHistory = doGetBlockHistory db
, _cpGetHistoricalLookup = doGetHistoricalLookup db
, _cpCleanupDb = doCleanupDb db
}
, _cpeLogger = loggr
})

type Db = MVar (BlockEnv SQLiteEnv)

-- | Rollback any open transaction or safepoint in the db. Usually this should
-- be executed in masked state from within a handler.
--
doCleanupDb :: Db -> IO ()
doCleanupDb dbenv = runBlockEnv dbenv $ do
clearPendingTxState
rollbackAll

doRestore :: ChainwebVersion -> ChainId -> Db -> Maybe (BlockHeight, ParentHash) -> IO PactDbEnv'
doRestore v cid dbenv (Just (bh, hash)) = runBlockEnv dbenv $ do
Expand Down
1 change: 1 addition & 0 deletions src/Chainweb/Pact/Backend/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ data Checkpointer = Checkpointer
forall k v . (FromJSON v) => BlockHeader -> Domain k v -> IO BlockTxHistory)
, _cpGetHistoricalLookup :: !(
forall k v . (FromJSON v) => BlockHeader -> Domain k v -> RowKey -> IO (Maybe (TxLog Value)))
, _cpCleanupDb :: IO ()
}

data CheckpointEnv = CheckpointEnv
Expand Down
3 changes: 3 additions & 0 deletions src/Chainweb/Pact/Backend/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ rollbackSavepoint :: SavepointName -> BlockHandler SQLiteEnv ()
rollbackSavepoint name =
callDb "rollbackSavepoint" $ \db -> exec_ db $ "ROLLBACK TRANSACTION TO SAVEPOINT [" <> toS (asString name) <> "];"

rollbackAll :: BlockHandler SQLiteEnv ()
rollbackAll = callDb "rollback all open transactions" $ \db -> exec_ db "ROLLBACK;"

data SavepointName = BatchSavepoint | Block | DbTransaction | PreBlock
deriving (Eq, Ord, Enum)

Expand Down
4 changes: 4 additions & 0 deletions src/Chainweb/Pact/PactService.hs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,11 @@ serviceRequests logFn memPoolAccess reqQ = do
]
liftIO $ void $ tryPutMVar mvar $! toPactInternalError e
]
`finally`
-- Rolls back all open transactions and safepoints
getCheckpointer >>= liftIO . _cpCleanupDb
where

-- Pact turns AsyncExceptions into textual exceptions within
-- PactInternalError. So there is no easy way for us to distinguish
-- whether an exception originates from within pact or from the outside.
Expand Down