Skip to content

Commit 78fde37

Browse files
committed
Hook
1 parent 749c390 commit 78fde37

File tree

7 files changed

+64
-11
lines changed

7 files changed

+64
-11
lines changed

cardano-node/app/cardano-node.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ main = do
8181

8282
data Command = RunCmd PartialNodeConfiguration
8383
| TraceDocumentation TraceDocumentationCmd
84-
| CanonicalizeSnapshotsCmd FilePath (Maybe NodeDatabasePaths)
84+
| CanonicalizeSnapshotsCmd (Maybe FilePath) (Maybe NodeDatabasePaths)
8585
| VersionCmd
8686

8787
-- Yes! A --version flag or version command. Either guess is right!

cardano-node/cardano-node.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ library
199199
, ouroboros-network-protocols ^>= 0.15
200200
, prettyprinter
201201
, prettyprinter-ansi-terminal
202+
, process
202203
, psqueues
203204
, random
204205
, resource-registry

cardano-node/src/Cardano/Node/Configuration/POM.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ data NodeConfiguration
174174
, ncGenesisConfig :: GenesisConfig
175175

176176
, ncResponderCoreAffinityPolicy :: ResponderCoreAffinityPolicy
177+
178+
, ncCanonicalSnapshotOutputPath :: Maybe FilePath
177179
} deriving (Eq, Show)
178180

179181
-- | We expose the `Ouroboros.Network.Mux.ForkPolicy` as a `NodeConfiguration` field.
@@ -887,6 +889,7 @@ makeNodeConfiguration pnc = do
887889
, ncConsensusMode
888890
, ncGenesisConfig
889891
, ncResponderCoreAffinityPolicy
892+
, ncCanonicalSnapshotOutputPath = getLast $ pncCanonicalSnapshotOutputPath pnc
890893
}
891894

892895
ncProtocol :: NodeConfiguration -> Protocol

cardano-node/src/Cardano/Node/Parsers.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,13 +437,13 @@ renderHelpDoc :: Int -> OptI.Doc -> String
437437
renderHelpDoc cols =
438438
(`OptI.renderShowS` "") . OptI.layoutPretty (OptI.LayoutOptions (OptI.AvailablePerLine cols 1.0))
439439

440-
parseSnapshotsCmd :: Parser (FilePath, Maybe NodeDatabasePaths)
440+
parseSnapshotsCmd :: Parser (Maybe FilePath, Maybe NodeDatabasePaths)
441441
parseSnapshotsCmd = subparser
442442
( commandGroup "Canonicalize snapshots"
443443
<> metavar "run"
444444
<> command "canonicalize-snapshots"
445445
(info (((,)
446-
<$> parseConfigFile
446+
<$> optional parseConfigFile
447447
<*> optional (parseDbPath <|> fmap OnePathForAllDbs parseImmutableDbPath)
448448
) <**> helper)
449449
(progDesc "Canonicalize all snapshots" ))

cardano-node/src/Cardano/Node/Tracing/API.hs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE LambdaCase #-}
12
{-# LANGUAGE FlexibleContexts #-}
23
{-# LANGUAGE MonoLocalBinds #-}
34
{-# LANGUAGE PackageImports #-}
@@ -37,7 +38,8 @@ import Prelude
3738

3839
import Control.DeepSeq (deepseq)
3940
import Control.Monad (forM_)
40-
import "contra-tracer" Control.Tracer (traceWith)
41+
import "contra-tracer" Control.Tracer (traceWith, Tracer)
42+
import qualified "contra-tracer" Control.Tracer as CT
4143
import "trace-dispatcher" Control.Tracer (nullTracer)
4244
import qualified Data.Map.Strict as Map
4345
import Data.Maybe
@@ -48,7 +50,11 @@ import System.Metrics as EKG
4850

4951
import Trace.Forward.Forwarding (initForwardingDelayed)
5052
import Trace.Forward.Utils.TraceObject (writeToSink)
51-
53+
import qualified Ouroboros.Consensus.Storage.ChainDB as ChainDB
54+
import Ouroboros.Consensus.Util.Enclose
55+
import qualified Ouroboros.Consensus.Storage.LedgerDB.Snapshots as LedgerDB
56+
import qualified Ouroboros.Consensus.Storage.LedgerDB.TraceEvent as LedgerDB
57+
import Cardano.Snapshots.Run
5258

5359
initTraceDispatcher ::
5460
forall blk.
@@ -69,7 +75,16 @@ initTraceDispatcher nc p networkMagic nodeKernel noBlockForging = do
6975
(unConfigPath $ ncConfigFile nc)
7076
defaultCardanoConfig
7177

72-
(kickoffForwarder, kickoffPrometheusSimple, tracers) <- mkTracers trConfig
78+
let onChainDbEvent = if isJust (ncCanonicalSnapshotOutputPath nc)
79+
then CT.Tracer $ \case
80+
(ChainDB.TraceLedgerDBEvent
81+
(LedgerDB.LedgerDBSnapshotEvent
82+
(LedgerDB.TookSnapshot _ _ (FallingEdgeWith _)))
83+
) -> spawnCanonicalizer
84+
_ -> pure ()
85+
else CT.nullTracer
86+
87+
(kickoffForwarder, kickoffPrometheusSimple, tracers) <- mkTracers trConfig onChainDbEvent
7388

7489
-- The NodeInfo DataPoint needs to be fully evaluated and stored
7590
-- before it is queried for the first time by cardano-tracer.
@@ -107,11 +122,12 @@ initTraceDispatcher nc p networkMagic nodeKernel noBlockForging = do
107122

108123
mkTracers
109124
:: TraceConfig
125+
-> Tracer IO (ChainDB.TraceEvent blk)
110126
-> IO ( IO ()
111127
, IO (Maybe String)
112128
, Tracers RemoteAddress LocalAddress blk IO
113129
)
114-
mkTracers trConfig = do
130+
mkTracers trConfig onChainDbEvent = do
115131
ekgStore <- EKG.newStore
116132
EKG.registerGcMetrics ekgStore
117133
ekgTrace <- ekgTracer trConfig ekgStore
@@ -144,6 +160,7 @@ initTraceDispatcher nc p networkMagic nodeKernel noBlockForging = do
144160
nodeKernel
145161
stdoutTrace
146162
fwdTracer
163+
onChainDbEvent
147164
(Just ekgTrace)
148165
dpTracer
149166
trConfig

cardano-node/src/Cardano/Node/Tracing/Tracers.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,14 @@ mkDispatchTracers
7979
=> NodeKernelData blk
8080
-> Trace IO FormattedMessage
8181
-> Trace IO FormattedMessage
82+
-> Tracer IO (ChainDB.TraceEvent blk)
8283
-> Maybe (Trace IO FormattedMessage)
8384
-> Trace IO DataPoint
8485
-> TraceConfig
8586
-> SomeConsensusProtocol
8687
-> IO (Tracers RemoteAddress LocalAddress blk IO)
8788

88-
mkDispatchTracers nodeKernel trBase trForward mbTrEKG trDataPoint trConfig p = do
89+
mkDispatchTracers nodeKernel trBase trForward onChainDBEventTracer mbTrEKG trDataPoint trConfig p = do
8990

9091
configReflection <- emptyConfigReflection
9192

@@ -175,6 +176,7 @@ mkDispatchTracers nodeKernel trBase trForward mbTrEKG trDataPoint trConfig p = d
175176
chainDBTracer = Tracer (traceWith chainDBTr')
176177
<> Tracer (traceWith replayBlockTr')
177178
<> Tracer (SR.traceNodeStateChainDB p nodeStateDP)
179+
<> onChainDBEventTracer
178180
, consensusTracers = consensusTr
179181
, churnModeTracer = Tracer (traceWith churnModeTr)
180182
, nodeToClientTracers = nodeToClientTr

cardano-node/src/Cardano/Snapshots/Run.hs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,56 @@
44

55
module Cardano.Snapshots.Run (
66
canonicalizeSnapshots,
7+
spawnCanonicalizer,
78
NodeDatabasePaths,
89
) where
910

1011
import qualified Cardano.Api.Consensus as Api
1112
import Cardano.Node.Configuration.LedgerDB
1213
import Cardano.Node.Configuration.POM
14+
import Cardano.Node.Parsers (nodeCLIParser)
1315
import Cardano.Node.Protocol
1416
import Cardano.Node.Types (ConfigYamlFilePath (..))
1517
import Control.Exception
16-
import Control.Monad (forM_)
18+
import Control.Monad (forM_, void)
19+
import Control.Monad.Class.MonadFork
1720
import Control.Monad.Except
21+
import Data.Maybe (fromMaybe)
1822
import Data.Monoid (Last (..))
23+
import Options.Applicative
1924
import Ouroboros.Consensus.Cardano.SnapshotConversion
2025
import Ouroboros.Consensus.Node (NodeDatabasePaths (..), immutableDbPath)
2126
import System.Directory (doesFileExist, listDirectory)
27+
import System.Environment
2228
import System.FilePath ((</>))
29+
import System.IO (hPutStrLn, stderr)
30+
import System.Process
2331

24-
canonicalizeSnapshots :: FilePath -> Maybe NodeDatabasePaths -> IO ()
32+
spawnCanonicalizer :: IO ()
33+
spawnCanonicalizer =
34+
void $ forkIO $ do
35+
putStrLn "SPAWNING"
36+
progName <- getExecutablePath
37+
putStrLn progName
38+
mPnc <- execParserPure defaultPrefs (info nodeCLIParser mempty) <$> getArgs
39+
case mPnc of
40+
Success pnc -> do
41+
let cfg = case getLast $ pncConfigFile pnc of
42+
Nothing -> []
43+
Just (ConfigYamlFilePath cfgFile) -> ["--config", cfgFile]
44+
db = case getLast $ pncDatabaseFile pnc of
45+
Nothing -> []
46+
Just (OnePathForAllDbs p) -> ["--database-path", p]
47+
Just (MultipleDbPaths imm _) -> ["--database-path", imm]
48+
(_, out, err) <-
49+
readProcessWithExitCode progName ("canonicalize-snapshots" : cfg ++ db) ""
50+
putStrLn out
51+
hPutStrLn stderr err
52+
_ -> pure ()
53+
54+
canonicalizeSnapshots :: Maybe FilePath -> Maybe NodeDatabasePaths -> IO ()
2555
canonicalizeSnapshots cfg (Last -> db) = do
26-
configYamlPc <- parseNodeConfigurationFP $ Just $ ConfigYamlFilePath cfg
56+
configYamlPc <- parseNodeConfigurationFP $ Just $ ConfigYamlFilePath $ fromMaybe "configuration/cardano/mainnet-config.json" cfg
2757

2858
let cfgFromFile = defaultPartialNodeConfiguration <> configYamlPc
2959

0 commit comments

Comments
 (0)