Skip to content

Commit cfb9dfc

Browse files
amesgengeo2a
authored andcommitted
[wip] Integrate Predictable Ledger State Snapshots
1 parent d7b2eb8 commit cfb9dfc

6 files changed

Lines changed: 91 additions & 21 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ noDeprecatedOptions = DeprecatedOptions []
7373

7474
data LedgerDbConfiguration =
7575
LedgerDbConfiguration
76-
NumOfDiskSnapshots
77-
SnapshotInterval
76+
SnapshotPolicyArgs
7877
QueryBatchSize
7978
LedgerDbSelectorFlag
8079
DeprecatedOptions

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

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module Cardano.Node.Configuration.POM
2828
where
2929

3030
import Cardano.Crypto (RequiresNetworkMagic (..))
31+
import Cardano.Ledger.BaseTypes
3132
import Cardano.Logging.Types
3233
import Cardano.Network.ConsensusMode (ConsensusMode (..), defaultConsensusMode)
3334
import qualified Cardano.Network.Diffusion.Configuration as Cardano
@@ -48,7 +49,9 @@ import Ouroboros.Consensus.Node.Genesis (GenesisConfig, GenesisConfigF
4849
defaultGenesisConfigFlags, mkGenesisConfig)
4950
import Ouroboros.Consensus.Storage.LedgerDB.Args (QueryBatchSize (..))
5051
import Ouroboros.Consensus.Storage.LedgerDB.Snapshots (NumOfDiskSnapshots (..),
51-
SnapshotInterval (..))
52+
SnapshotDelayRange (..), SnapshotFrequency (..), SnapshotFrequencyArgs (..),
53+
SnapshotPolicyArgs (..), defaultSnapshotPolicyArgs)
54+
import Ouroboros.Consensus.Util.Args (OverrideOrDefault (..))
5255
import Ouroboros.Consensus.Storage.LedgerDB.V1.Args (FlushFrequency (..))
5356
import Ouroboros.Network.Diffusion.Configuration as Configuration
5457
import qualified Ouroboros.Network.Diffusion.Configuration as Ouroboros
@@ -510,8 +513,14 @@ instance FromJSON PartialNodeConfiguration where
510513
Nothing -> return Nothing
511514

512515
parseLedgerDbConfig v = do
513-
let snapInterval x = fmap (RequestedSnapshotInterval . secondsToDiffTime) <$> x .:? "SnapshotInterval"
514-
snapNum x = fmap RequestedNumOfDiskSnapshots <$> x .:? "NumOfDiskSnapshots"
516+
-- TODO maybe don't silently convert old format (which was in seconds)
517+
-- to new format (which is in slots), despite these being the same on
518+
-- mainnet?
519+
let snapInterval x = do
520+
si <- x .:? "SnapshotInterval"
521+
when (any (<= 0) si) $ fail $ "Non-positive SnapshotInterval: " <> show si
522+
pure $ Override . SlotNo <$> si
523+
snapNum x = fmap (Override . NumOfDiskSnapshots) <$> x .:? "NumOfDiskSnapshots"
515524

516525
mTopLevelSnapInterval <- snapInterval v
517526
mTopLevelSnapNum <- snapNum v
@@ -525,12 +534,32 @@ instance FromJSON PartialNodeConfiguration where
525534
mLedgerDB <- v .:? "LedgerDB"
526535
case mLedgerDB of
527536
Nothing -> do
528-
let si = fromMaybe DefaultSnapshotInterval mTopLevelSnapInterval
529-
sn = fromMaybe DefaultNumOfDiskSnapshots mTopLevelSnapNum
530-
return $ Just $ LedgerDbConfiguration sn si DefaultQueryBatchSize V2InMemory deprecatedOpts
537+
let si = fromMaybe UseDefault mTopLevelSnapInterval
538+
sn = fromMaybe UseDefault mTopLevelSnapNum
539+
sf = SnapshotFrequencyArgs {
540+
sfaInterval = unsafeNonZero . unSlotNo <$> si
541+
, sfaOffset = UseDefault
542+
, sfaRateLimit = UseDefault
543+
, sfaDelaySnapshotRange = UseDefault
544+
}
545+
spArgs = SnapshotPolicyArgs (SnapshotFrequency sf) sn
546+
return $ Just $ LedgerDbConfiguration spArgs DefaultQueryBatchSize V2InMemory deprecatedOpts
531547
Just ledgerDB -> flip (withObject "LedgerDB") ledgerDB $ \o -> do
532-
ldbSnapInterval <- (getLast . (Last mTopLevelSnapInterval <>) . Last <$> snapInterval o) .!= DefaultSnapshotInterval
533-
ldbSnapNum <- (getLast . (Last mTopLevelSnapNum <>) . Last <$> snapNum o) .!= DefaultNumOfDiskSnapshots
548+
ldbSnapInterval <- (getLast . (Last mTopLevelSnapInterval <>) . Last <$> snapInterval o) .!= UseDefault
549+
ldbSnapNum <- (getLast . (Last mTopLevelSnapNum <>) . Last <$> snapNum o) .!= UseDefault
550+
ldbSnapOffset <- (fmap Override <$> o .:? "SlotOffset") .!= UseDefault
551+
ldbSnapRateLimit<- (fmap (Override . secondsToDiffTime) <$> o .:? "RateLimit") .!= UseDefault
552+
ldbSnapMinDelay <- o .:? "MinDelay"
553+
ldbSnapMaxDelay <- o .:? "MaxDelay"
554+
ldbSnapDelayRange <-
555+
case (ldbSnapMinDelay, ldbSnapMaxDelay) of
556+
(Just minDelay, Just maxDelay) ->
557+
if minDelay <= maxDelay then
558+
pure (Override (SnapshotDelayRange (secondsToDiffTime minDelay) (secondsToDiffTime maxDelay)))
559+
else fail $ "Invalid ledger snapshot delay range, MinDelay > MaxDelay: "
560+
<> show minDelay <> " > " <> show maxDelay
561+
-- use the default delay range if either min or max is unspecified
562+
_ -> pure UseDefault
534563
qsize <- (fmap RequestedQueryBatchSize <$> o .:? "QueryBatchSize") .!= DefaultQueryBatchSize
535564
backend <- o .:? "Backend" .!= "V2InMemory"
536565
selector <- case backend of
@@ -545,7 +574,14 @@ instance FromJSON PartialNodeConfiguration where
545574
lsmPath :: Maybe FilePath <- o .:? "LSMDatabasePath"
546575
pure $ V2LSM lsmPath
547576
_ -> fail $ "Malformed LedgerDB Backend: " <> backend
548-
pure $ Just $ LedgerDbConfiguration ldbSnapNum ldbSnapInterval qsize selector deprecatedOpts
577+
let sf = SnapshotFrequencyArgs {
578+
sfaInterval = unsafeNonZero . unSlotNo <$> ldbSnapInterval
579+
, sfaOffset = ldbSnapOffset
580+
, sfaRateLimit = ldbSnapRateLimit
581+
, sfaDelaySnapshotRange = ldbSnapDelayRange
582+
}
583+
spArgs = SnapshotPolicyArgs (SnapshotFrequency sf) ldbSnapNum
584+
pure $ Just $ LedgerDbConfiguration spArgs qsize selector deprecatedOpts
549585

550586
parseByronProtocol v = do
551587
primary <- v .:? "ByronGenesisFile"
@@ -712,8 +748,7 @@ defaultPartialNodeConfiguration =
712748
, pncLedgerDbConfig =
713749
Last $ Just $
714750
LedgerDbConfiguration
715-
DefaultNumOfDiskSnapshots
716-
DefaultSnapshotInterval
751+
defaultSnapshotPolicyArgs
717752
DefaultQueryBatchSize
718753
V2InMemory
719754
noDeprecatedOptions

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -656,15 +656,11 @@ handleSimpleNode blockType runP tracers nc networkMagic onKernel = do
656656
Just version_ -> Map.takeWhileAntitone (<= version_)
657657

658658
LedgerDbConfiguration
659-
snapInterval
660-
numSnaps
659+
snapshotPolicyArgs
661660
queryBatchSize
662661
ldbBackend
663662
deprecatedOpts = ncLedgerDbConfig nc
664663

665-
snapshotPolicyArgs :: SnapshotPolicyArgs
666-
snapshotPolicyArgs = SnapshotPolicyArgs numSnaps snapInterval
667-
668664
--------------------------------------------------------------------------------
669665
-- SIGHUP Handlers
670666
--------------------------------------------------------------------------------

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import Ouroboros.Network.Block (MaxSlotNo (..))
6060
import Data.Aeson (Object, Value (String), object, toJSON, (.=))
6161
import qualified Data.ByteString.Base16 as B16
6262
import Data.Int (Int64)
63+
import qualified Data.List.NonEmpty as NonEmpty
6364
import Data.SOP (All, K (..), hcmap, hcollapse)
6465
import Data.Text (Text)
6566
import qualified Data.Text as Text
@@ -1772,6 +1773,14 @@ instance ( StandardHash blk
17721773
LedgerDB.MetadataBackendMismatch ->
17731774
" Snapshot was created for a different backend. Convert it with `snapshot-converter`."
17741775
_ -> ""
1776+
forHuman (LedgerDB.SnapshotRequestDelayed _snapshotRequestTime delayBeforeSnapshotting slots) =
1777+
Text.unwords ["Scheduling to take ledger state snapshots at slots "
1778+
, showT (NonEmpty.toList slots)
1779+
, ", with a randomised delay of"
1780+
, showT delayBeforeSnapshotting
1781+
]
1782+
forHuman (LedgerDB.SnapshotRequestCompleted) = "Completed taking a ledger state snapshot"
1783+
17751784

17761785
forMachine dtals (LedgerDB.TookSnapshot snap pt enclosedTiming) =
17771786
mconcat [ "kind" .= String "TookSnapshot"
@@ -1786,11 +1795,23 @@ instance ( StandardHash blk
17861795
mconcat [ "kind" .= String "InvalidSnapshot"
17871796
, "snapshot" .= forMachine dtals snap
17881797
, "failure" .= show failure ]
1798+
forMachine _dtals (LedgerDB.SnapshotRequestDelayed snapshotRequestTime delayBeforeSnapshotting slots) =
1799+
mconcat [ "kind" .= String "TraceLedgerDBEvent.LedgerDBSnapshotEvent.SnapshotRequestDelayed"
1800+
, "requestTime" .= show snapshotRequestTime
1801+
, "delayBeforeSnapshotting " .= show delayBeforeSnapshotting
1802+
, "slots" .= show slots
1803+
]
1804+
forMachine _dtals (LedgerDB.SnapshotRequestCompleted) =
1805+
mconcat [ "kind" .= String "TraceLedgerDBEvent.LedgerDBSnapshotEvent.SnapshotRequestCompleted"
1806+
]
1807+
17891808

17901809
instance MetaTrace (LedgerDB.TraceSnapshotEvent blk) where
17911810
namespaceFor LedgerDB.TookSnapshot {} = Namespace [] ["TookSnapshot"]
17921811
namespaceFor LedgerDB.DeletedSnapshot {} = Namespace [] ["DeletedSnapshot"]
17931812
namespaceFor LedgerDB.InvalidSnapshot {} = Namespace [] ["InvalidSnapshot"]
1813+
namespaceFor LedgerDB.SnapshotRequestDelayed {} = Namespace [] ["SnapshotRequestDelayed"]
1814+
namespaceFor LedgerDB.SnapshotRequestCompleted {} = Namespace [] ["SnapshotRequestCompleted"]
17941815

17951816
severityFor (Namespace _ ["TookSnapshot"]) _ = Just Info
17961817
severityFor (Namespace _ ["DeletedSnapshot"]) _ = Just Debug
@@ -1809,6 +1830,10 @@ instance MetaTrace (LedgerDB.TraceSnapshotEvent blk) where
18091830
, " seems to be from an old node or different backend, it will"
18101831
, " be deleted"
18111832
]
1833+
documentFor (Namespace _ ["SnapshotRequestDelayed"]) = Just
1834+
"A delayed snapshot requested was issued. The snapshot will be initiated at the specified timestamp, with the specified delay and for the specified slots"
1835+
documentFor (Namespace _ ["SnapshotRequestCompleted"]) = Just
1836+
"The delayed snapshot request was completed"
18121837
documentFor _ = Nothing
18131838

18141839
allNamespaces =

cardano-node/src/Cardano/Tracing/OrphanInstances/Consensus.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ import Data.Aeson (Value (..))
9191
import qualified Data.Aeson as Aeson
9292
import Data.Foldable (Foldable (..))
9393
import Data.Function (on)
94+
import qualified Data.List.NonEmpty as NonEmpty
9495
import Data.Proxy
9596
import Data.Text (Text, pack)
9697
import qualified Data.Text as Text
@@ -185,6 +186,8 @@ instance HasSeverityAnnotation (ChainDB.TraceEvent blk) where
185186
LedgerDB.InitFailureRead (LedgerDB.ReadMetadataError _ LedgerDB.MetadataBackendMismatch) -> Warning
186187
LedgerDB.InitFailureRead (LedgerDB.ReadMetadataError _ LedgerDB.MetadataFileDoesNotExist) -> Warning
187188
_ -> Error
189+
LedgerDB.SnapshotRequestDelayed {} -> Info
190+
LedgerDB.SnapshotRequestCompleted -> Info
188191
LedgerDB.LedgerReplayEvent {} -> Info
189192
LedgerDB.LedgerDBForkerEvent {} -> Debug
190193
LedgerDB.LedgerDBFlavorImplEvent {} -> Debug
@@ -629,6 +632,11 @@ instance ( ConvertRawHash blk
629632
", duration: " <> showT t
630633
LedgerDB.DeletedSnapshot snap ->
631634
"Deleted old snapshot " <> showT snap
635+
LedgerDB.SnapshotRequestDelayed _snapshotRequestTime delayBeforeSnapshotting slots ->
636+
"Scheduling to take ledger state snapshots at slots " <> showT (NonEmpty.toList slots)
637+
<> ", with randomised delay of"
638+
<> showT delayBeforeSnapshotting
639+
LedgerDB.SnapshotRequestCompleted -> "Completed taking a ledger state snapshot"
632640
LedgerDB.LedgerReplayEvent ev' -> case ev' of
633641
LedgerDB.TraceReplayStartEvent ev'' -> case ev'' of
634642
LedgerDB.ReplayFromGenesis ->
@@ -1110,6 +1118,14 @@ instance ( ConvertRawHash blk
11101118
mconcat [ "kind" .= String "TraceLedgerDBEvent.LedgerDBSnapshotEvent.InvalidSnapshot"
11111119
, "snapshot" .= toObject verb snap
11121120
, "failure" .= show failure ]
1121+
LedgerDB.SnapshotRequestDelayed snapshotRequestTime delayBeforeSnapshotting slots ->
1122+
mconcat [ "kind" .= String "TraceLedgerDBEvent.LedgerDBSnapshotEvent.SnapshotRequestDelayed"
1123+
, "requestTime" .= show snapshotRequestTime
1124+
, "delayBeforeSnapshotting " .= show delayBeforeSnapshotting
1125+
, "slots" .= show slots]
1126+
LedgerDB.SnapshotRequestCompleted ->
1127+
mconcat [ "kind" .= String "TraceLedgerDBEvent.LedgerDBSnapshotEvent.SnapshotRequestCompleted"
1128+
]
11131129
LedgerDB.LedgerReplayEvent ev' -> case ev' of
11141130
LedgerDB.TraceReplayStartEvent ev'' -> case ev'' of
11151131
LedgerDB.ReplayFromGenesis ->

cardano-node/test/Test/Cardano/Node/POM.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ import Cardano.Tracing.Config (PartialTraceOptions (..), defaultPartia
2424
import Ouroboros.Consensus.Node (NodeDatabasePaths (..))
2525
import Ouroboros.Consensus.Node.Genesis (disableGenesisConfig)
2626
import Ouroboros.Consensus.Storage.LedgerDB.Args
27-
import Ouroboros.Consensus.Storage.LedgerDB.Snapshots (NumOfDiskSnapshots (..),
28-
SnapshotInterval (..))
27+
import Ouroboros.Consensus.Storage.LedgerDB.Snapshots (defaultSnapshotPolicyArgs)
2928
import Ouroboros.Network.Block (SlotNo (..))
3029
import Ouroboros.Network.PeerSelection.PeerSharing (PeerSharing (..))
3130
import Ouroboros.Network.TxSubmission.Inbound.V2.Types
@@ -297,7 +296,7 @@ eExpectedConfig = do
297296
, ncConsensusMode = PraosMode
298297
, ncGenesisConfig = disableGenesisConfig
299298
, ncResponderCoreAffinityPolicy = NoResponderCoreAffinity
300-
, ncLedgerDbConfig = LedgerDbConfiguration DefaultNumOfDiskSnapshots DefaultSnapshotInterval DefaultQueryBatchSize V2InMemory noDeprecatedOptions
299+
, ncLedgerDbConfig = LedgerDbConfiguration defaultSnapshotPolicyArgs DefaultQueryBatchSize V2InMemory noDeprecatedOptions
301300
, ncRpcConfig
302301
, ncTxSubmissionLogicVersion = TxSubmissionLogicV1
303302
, ncTxSubmissionInitDelay = defaultTxSubmissionInitDelay

0 commit comments

Comments
 (0)