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

Much quieter replays #1713

Merged
merged 13 commits into from
Aug 3, 2023
24 changes: 17 additions & 7 deletions node/ChainwebNode.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
Expand Down Expand Up @@ -93,6 +94,7 @@ import Chainweb.Miner.Coordinator (MiningStats)
import Chainweb.Pact.Backend.DbCache (DbCacheStats)
import Chainweb.Pact.Service.PactQueue (PactQueueStats)
import Chainweb.Pact.RestAPI.Server (PactCmdLog(..))
import Chainweb.Pact.Types(TxFailureLog)
import Chainweb.Payload
import Chainweb.Payload.PayloadStore
import Chainweb.Time
Expand Down Expand Up @@ -363,17 +365,24 @@ node conf logger = do

withNodeLogger
:: LogConfig
-> ChainwebConfiguration
-> ChainwebVersion
-> (L.Logger SomeLogMessage -> IO ())
-> IO ()
withNodeLogger logConfig v f = runManaged $ do
withNodeLogger logCfg chainwebCfg v f = runManaged $ do

-- This manager is used only for logging backends
mgr <- liftIO HTTPS.newTlsManager

-- Base Backend
baseBackend <- managed
$ withBaseHandleBackend "ChainwebApp" mgr pkgInfoScopes (_logConfigBackend logConfig)
$ withBaseHandleBackend "ChainwebApp" mgr pkgInfoScopes (_logConfigBackend logCfg)

-- we don't log tx failures in replay
let !txFailureHandler =
if _configOnlySyncPact chainwebCfg
then dropLogHandler (Proxy :: Proxy TxFailureLog)
else passthroughLogHandler

-- Telemetry Backends
monitorBackend <- managed
Expand Down Expand Up @@ -415,8 +424,9 @@ withNodeLogger logConfig v f = runManaged $ do
$ mkTelemetryLogger @ChainwebStatus mgr teleLogConfig

logger <- managed
$ L.withLogger (_logConfigLogger logConfig) $ logHandles
[ logFilterHandle (_logConfigFilter logConfig)
$ L.withLogger (_logConfigLogger logCfg) $ logHandles
[ logFilterHandle (_logConfigFilter logCfg)
, txFailureHandler
, logHandler monitorBackend
, logHandler p2pInfoBackend
, logHandler rtsBackend
Expand All @@ -438,11 +448,11 @@ withNodeLogger logConfig v f = runManaged $ do
] baseBackend

liftIO $ f
$ maybe id (\x -> addLabel ("cluster", toText x)) (_logConfigClusterId logConfig)
$ maybe id (\x -> addLabel ("cluster", toText x)) (_logConfigClusterId logCfg)
$ addLabel ("chainwebVersion", sshow (_versionName v))
$ logger
where
teleLogConfig = _logConfigTelemetryBackend logConfig
teleLogConfig = _logConfigTelemetryBackend logCfg

mkTelemetryLogger
:: forall a b
Expand Down Expand Up @@ -538,7 +548,7 @@ main = do
let v = _configChainwebVersion $ _nodeConfigChainweb conf
registerVersion v
hSetBuffering stderr LineBuffering
withNodeLogger (_nodeConfigLog conf) v $ \logger -> do
withNodeLogger (_nodeConfigLog conf) (_nodeConfigChainweb conf) v $ \logger -> do
logFunctionJson logger Info ProcessStarted
handles
[ Handler $ \(e :: SomeAsyncException) ->
Expand Down
1 change: 0 additions & 1 deletion src/Chainweb/Chainweb.hs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,6 @@ withChainwebInternal conf logger peer serviceSock rocksDb pactDbDir backupDir re
{ _pactReorgLimit = _configReorgLimit conf
, _pactLocalRewindDepthLimit = _configLocalRewindDepthLimit conf
, _pactPreInsertCheckTimeout = _configPreInsertCheckTimeout conf
, _pactRevalidate = True
, _pactQueueSize = _configPactQueueSize conf
, _pactResetDb = resetDb
, _pactAllowReadsInLocal = _configAllowReadsInLocal conf
Expand Down
1 change: 0 additions & 1 deletion src/Chainweb/Pact/PactService.hs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ withPactService ver cid chainwebLogger bhDb pdb sqlenv config act =
, _psPreInsertCheckTimeout = _pactPreInsertCheckTimeout config
, _psOnFatalError = defaultOnFatalError (logFunctionText chainwebLogger)
, _psVersion = ver
, _psValidateHashesOnReplay = _pactRevalidate config
, _psAllowReadsInLocal = _pactAllowReadsInLocal config
, _psIsBatch = False
, _psCheckpointerDepth = 0
Expand Down
5 changes: 2 additions & 3 deletions src/Chainweb/Pact/PactService/Checkpointer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,7 @@ rewindTo rewindLimit (Just (ParentHeader parent)) = do
withAsync (heightProgress (_blockHeight commonAncestor) heightRef (logInfo_ logger)) $ \_ ->
s
& S.scanM
-- no need to re-validate hashes, because these blocks have already been validated
(\ !p !c -> runPact (local (psValidateHashesOnReplay .~ False) $ fastForward (ParentHeader p, c)) >> writeIORef heightRef (_blockHeight c) >> return c)
(\ !p !c -> runPact (fastForward (ParentHeader p, c)) >> writeIORef heightRef (_blockHeight c) >> return c)
(return h) -- initial parent
return
& S.length_
Expand All @@ -416,7 +415,7 @@ fastForward
. (HasCallStack, CanReadablePayloadCas tbl, Logger logger)
=> (ParentHeader, BlockHeader)
-> PactServiceM logger tbl ()
fastForward (target, block) =
fastForward (target, block) = do
-- This does a restore, i.e. it rewinds the checkpointer back in
-- history, if needed.
withCheckpointerWithoutRewind (Just target) "fastForward" $ \pdbenv -> do
Expand Down
9 changes: 3 additions & 6 deletions src/Chainweb/Pact/PactService/ExecBlock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,8 @@ execBlock currHeader plData pdbenv = do

modify' $ set psStateValidated $ Just currHeader

-- Validate hashes if requested
asks _psValidateHashesOnReplay >>= \x -> when x $
either throwM (void . return) $!
validateHashes currHeader plData miner results
either throwM (void . return) $
validateHashes currHeader plData miner results
Comment on lines +154 to +155
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


return $! T2 miner results

Expand Down Expand Up @@ -356,8 +354,7 @@ runCoinbase False dbEnv miner enfCBFail usePrecomp mc = do

reward <- liftIO $! minerReward v rs bh

(T2 cr upgradedCacheM) <-
liftIO $! applyCoinbase v logger dbEnv miner reward txCtx enfCBFail usePrecomp mc
(T2 cr upgradedCacheM) <- liftIO $ applyCoinbase v logger dbEnv miner reward txCtx enfCBFail usePrecomp mc
mapM_ upgradeInitCache upgradedCacheM
debugResult "runCoinbase" (P.crLogs %~ fmap J.Array $ cr)
return $! cr
Expand Down
2 changes: 0 additions & 2 deletions src/Chainweb/Pact/Service/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ data PactServiceConfig = PactServiceConfig
-- ^ Maximum allowed rewind depth in the local command.
, _pactPreInsertCheckTimeout :: !Micros
-- ^ Maximum allowed execution time for the transactions validation.
, _pactRevalidate :: !Bool
-- ^ Re-validate payload hashes during transaction replay
, _pactAllowReadsInLocal :: !Bool
-- ^ Allow direct database reads in local mode
, _pactQueueSize :: !Natural
Expand Down
35 changes: 19 additions & 16 deletions src/Chainweb/Pact/TransactionExec.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
Expand Down Expand Up @@ -72,6 +76,7 @@ import Data.Maybe
import qualified Data.Set as S
import Data.Text (Text)
import qualified Data.Text as T
import qualified System.LogLevel as L

-- internal Pact modules

Expand Down Expand Up @@ -223,14 +228,14 @@ applyCmd v logger gasLogger pdbenv miner gasModel txCtx spv cmd initialGas mcach
Right _ -> checkTooBigTx initialGas gasLimit applyPayload redeemAllGas

displayPactError e = do
r <- jsonErrorResult e "tx failure for request key when running cmd"
r <- failTxWith e "tx failure for request key when running cmd"
redeemAllGas r

stripPactError e = do
let e' = case callCtx of
ApplyLocal -> e
ApplySend -> toEmptyPactError e
r <- jsonErrorResult e' "tx failure for request key when running cmd"
r <- failTxWith e' "tx failure for request key when running cmd"
redeemAllGas r

applyPayload = do
Expand All @@ -247,7 +252,7 @@ applyCmd v logger gasLogger pdbenv miner gasModel txCtx spv cmd initialGas mcach
| chainweb217Pact' -> stripPactError e
| chainweb213Pact' || not (isOldListErr e) -> displayPactError e
| otherwise -> do
r <- jsonErrorResult (toOldListErr e) "tx failure for request key when running cmd"
r <- failTxWith (toOldListErr e) "tx failure for request key when running cmd"
redeemAllGas r
Right r -> applyRedeem r

Expand Down Expand Up @@ -329,7 +334,7 @@ applyGenesisCmd logger dbEnv spv txCtx cmd =

go = do
-- TODO: fix with version recordification so that this matches the flags at genesis heights.
cr <- catchesPactError logger PrintsUnexpectedError $! runGenesis cmd permissiveNamespacePolicy interp
cr <- catchesPactError logger (onChainErrorPrintingFor txCtx) $! runGenesis cmd permissiveNamespacePolicy interp
case cr of
Left e -> fatal $ "Genesis command failed: " <> sshow e
Right r -> r <$ debug "successful genesis tx for request key"
Expand Down Expand Up @@ -414,7 +419,7 @@ applyCoinbase v logger dbEnv (Miner mid mks@(MinerKeys mk)) reward@(ParsedDecima
case cr of
Left e
| throwCritical -> throwM $ CoinbaseFailure $ sshow e
| otherwise -> (`T2` Nothing) <$> jsonErrorResult e "coinbase tx failure"
| otherwise -> (`T2` Nothing) <$> failTxWith e "coinbase tx failure"
Right er -> do
debug
$! "successful coinbase of "
Expand Down Expand Up @@ -475,7 +480,7 @@ applyLocal logger gasLogger dbEnv gasModel txCtx spv cmdIn mc execConfig =
applyContinuation gas0 interp cm signers chash managedNamespacePolicy

case cr of
Left e -> jsonErrorResult e "applyLocal"
Left e -> failTxWith e "applyLocal"
Right r -> return $! r { _crMetaData = Just (J.toJsonViaEncode $ ctxToPublicData' txCtx) }

go = checkTooBigTx gas0 gasLimit (applyPayload $ _pPayload $ _cmdPayload cmd) return
Expand Down Expand Up @@ -623,21 +628,19 @@ applyUpgrades v cid height
logError $ "Upgrade transaction failed! " <> sshow e
throwM e

jsonErrorResult
failTxWith
:: (Logger logger)
=> PactError
-> Text
-> TransactionM logger p (CommandResult [TxLogJson])
jsonErrorResult err msg = do
failTxWith err msg = do
logs <- use txLogs
gas <- view txGasLimit -- error means all gas was charged
rk <- view txRequestKey
l <- view txLogger

logInfo_ l
$! msg
<> ": " <> sshow rk
<> ": " <> sshow err
liftIO $ logFunction l L.Info
(TxFailureLog rk err msg)

return $! CommandResult rk Nothing (PactResult (Left err))
gas (Just logs) Nothing Nothing []
Expand Down Expand Up @@ -992,7 +995,7 @@ checkTooBigTx initialGas gasLimit next onFail
$ "Tx too big (" <> pretty initialGas <> "), limit "
<> pretty gasLimit

r <- jsonErrorResult pe "Tx too big"
r <- failTxWith pe "Tx too big"
onFail r
| otherwise = next

Expand Down Expand Up @@ -1136,15 +1139,15 @@ gasLog m = do
l <- view txGasLogger
rk <- view txRequestKey
for_ l $ \logger ->
logInfo_ logger $! m <> ": " <> sshow rk
logInfo_ logger $ m <> ": " <> sshow rk

-- | Log request keys at DEBUG when successful
--
debug :: (Logger logger) => Text -> TransactionM logger db ()
debug s = do
l <- view txLogger
rk <- view txRequestKey
logDebug_ l $! s <> ": " <> sshow rk
logDebug_ l $ s <> ": " <> sshow rk


-- | Denotes fatal failure points in the tx exec process
Expand All @@ -1155,7 +1158,7 @@ fatal e = do
rk <- view txRequestKey

logError_ l
$! "critical transaction failure: "
$ "critical transaction failure: "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for undoing the bangs

Copy link
Contributor Author

@edmundnoble edmundnoble Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loggers are always strict, so the bangs aren't necessary... except that they're lazy when they're filtered such that the log message never makes it to the queue. I've seen some pretty big log messages here, so I'd like those not constructed in case that can raise throughput.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, log messages are deep forced before added to the queue.

Avoiding construction when their log level is disabled is a good point.

<> sshow rk <> ": " <> e

throwM $ PactTransactionExecError (fromUntypedHash $ unRequestKey rk) e
Expand Down
36 changes: 27 additions & 9 deletions src/Chainweb/Pact/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module Chainweb.Pact.Types
, GasId(..)
, EnforceCoinbaseFailure(..)
, CoinbaseUsePrecompiled(..)
, cleanModuleCache

-- * Transaction State
, TransactionState(..)
Expand Down Expand Up @@ -81,7 +82,6 @@ module Chainweb.Pact.Types
, psPreInsertCheckTimeout
, psOnFatalError
, psVersion
, psValidateHashesOnReplay
, psLogger
, psGasLogger
, psAllowReadsInLocal
Expand Down Expand Up @@ -147,6 +147,7 @@ module Chainweb.Pact.Types
-- * types
, TxTimeout(..)
, ApplyCmdExecutionContext(..)
, TxFailureLog(..)

-- * miscellaneous
, defaultOnFatalError
Expand Down Expand Up @@ -217,7 +218,6 @@ import Chainweb.Time
import Chainweb.Transaction
import Chainweb.Utils
import Chainweb.Version
import Chainweb.Version.Guards
import Utils.Logging.Trace


Expand Down Expand Up @@ -288,6 +288,15 @@ moduleCacheKeys :: ModuleCache -> [ModuleName]
moduleCacheKeys (ModuleCache a) = fst <$> LHM.toList a
{-# INLINE moduleCacheKeys #-}

-- this can't go in Chainweb.Version.Guards because it causes an import cycle
-- it uses genesisHeight which is from BlockHeader which imports Guards
Comment on lines +291 to +292
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to move genesisHeight to a different place instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably would cause a cycle. I tried it during recordification.

cleanModuleCache :: ChainwebVersion -> ChainId -> BlockHeight -> Bool
cleanModuleCache v cid bh =
case v ^?! versionForks . at Chainweb217Pact . _Just . onChain cid of
ForkAtBlockHeight bh' -> bh == bh'
ForkAtGenesis -> bh == genesisHeight v cid
ForkNever -> False

-- -------------------------------------------------------------------- --
-- Local vs. Send execution context flag

Expand Down Expand Up @@ -414,7 +423,6 @@ data PactServiceEnv logger tbl = PactServiceEnv
-- ^ The limit of checkpointer's rewind in the `execValidationBlock` command.
, _psOnFatalError :: !(forall a. PactException -> Text -> IO a)
, _psVersion :: !ChainwebVersion
, _psValidateHashesOnReplay :: !Bool
, _psAllowReadsInLocal :: !Bool
, _psLogger :: !logger
, _psGasLogger :: !(Maybe logger)
Expand Down Expand Up @@ -464,7 +472,6 @@ testPactServiceConfig = PactServiceConfig
{ _pactReorgLimit = defaultReorgLimit
, _pactLocalRewindDepthLimit = defaultLocalRewindDepthLimit
, _pactPreInsertCheckTimeout = defaultPreInsertCheckTimeout
, _pactRevalidate = True
, _pactQueueSize = 1000
, _pactResetDb = True
, _pactAllowReadsInLocal = False
Expand Down Expand Up @@ -493,6 +500,15 @@ newtype TxTimeout = TxTimeout TransactionHash
deriving Show
instance Exception TxTimeout

data TxFailureLog = TxFailureLog !RequestKey !PactError !Text
deriving stock (Generic)
deriving anyclass (NFData, Typeable)
instance LogMessage TxFailureLog where
logText (TxFailureLog rk err msg) =
msg <> ": " <> sshow rk <> ": " <> sshow err
instance Show TxFailureLog where
show m = unpack (logText m)

defaultOnFatalError :: forall a. (LogLevel -> Text -> IO ()) -> PactException -> Text -> IO a
defaultOnFatalError lf pex t = do
lf Error errMsg
Expand Down Expand Up @@ -728,9 +744,11 @@ catchesPactError :: (MonadCatch m, MonadIO m, Logger logger) => logger -> Unexpe
catchesPactError logger exnPrinting action = catches (Right <$> action)
[ Handler $ \(e :: PactError) -> return $ Left e
, Handler $ \(e :: SomeException) -> do
liftIO $ logWarn_ logger ("catchesPactError: unknown error: " <> sshow e)
return $ Left $ PactError EvalError def def $
case exnPrinting of
PrintsUnexpectedError -> viaShow e
CensorsUnexpectedError -> "unknown error"
!err <- case exnPrinting of
PrintsUnexpectedError ->
return (viaShow e)
CensorsUnexpectedError -> do
liftIO $ logWarn_ logger ("catchesPactError: unknown error: " <> sshow e)
return "unknown error"
return $ Left $ PactError EvalError def def err
]
4 changes: 0 additions & 4 deletions src/Chainweb/Version/Guards.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ module Chainweb.Version.Guards
, chainweb215Pact
, chainweb216Pact
, chainweb217Pact
, cleanModuleCache
, chainweb218Pact
, chainweb219Pact
, chainweb220Pact
Expand Down Expand Up @@ -223,9 +222,6 @@ chainweb216Pact = checkFork after Chainweb216Pact
chainweb217Pact :: ChainwebVersion -> ChainId -> BlockHeight -> Bool
chainweb217Pact = checkFork after Chainweb217Pact

cleanModuleCache :: ChainwebVersion -> ChainId -> BlockHeight -> Bool
cleanModuleCache = checkFork atOrAfter Chainweb217Pact

chainweb218Pact :: ChainwebVersion -> ChainId -> BlockHeight -> Bool
chainweb218Pact = checkFork atOrAfter Chainweb218Pact

Expand Down
Loading
Loading