Skip to content

Commit

Permalink
Change RequestedInterval to be a data type representing the default a…
Browse files Browse the repository at this point in the history
…nd set value

This adds a bit more readability on the caller side of the
defaultDiskPolicy function by introducing a meaningful data constructor
  • Loading branch information
EncodePanda committed Mar 2, 2021
1 parent ab9e03b commit bb1db87
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 18 deletions.
4 changes: 2 additions & 2 deletions ouroboros-consensus-cardano/tools/db-analyser/Main.hs
Expand Up @@ -24,7 +24,7 @@ import qualified Ouroboros.Consensus.Storage.ChainDB as ChainDB
import Ouroboros.Consensus.Storage.ChainDB.Impl.Args (fromChainDbArgs)
import qualified Ouroboros.Consensus.Storage.ImmutableDB as ImmutableDB
import Ouroboros.Consensus.Storage.LedgerDB.DiskPolicy
(defaultDiskPolicy)
(RequestedInterval (..), defaultDiskPolicy)
import qualified Ouroboros.Consensus.Storage.VolatileDB as VolatileDB

import Analysis
Expand Down Expand Up @@ -168,7 +168,7 @@ analyse CmdLine {..} args =
mkProtocolInfo args
let chunkInfo = Node.nodeImmutableDbChunkInfo (configStorage cfg)
k = configSecurityParam cfg
diskPolicy = defaultDiskPolicy k Nothing
diskPolicy = defaultDiskPolicy k DefaultRequestInterval
args' =
Node.mkChainDbArgs
registry InFuture.dontCheck cfg initLedger chunkInfo $
Expand Down
2 changes: 1 addition & 1 deletion ouroboros-consensus-test/src/Test/ThreadNet/Network.hs
Expand Up @@ -703,7 +703,7 @@ runThreadNetwork systemTime ThreadNetworkArgs
, cdbImmutableDbValidation = ImmutableDB.ValidateAllChunks
, cdbVolatileDbValidation = VolatileDB.ValidateAll
, cdbMaxBlocksPerFile = VolatileDB.mkBlocksPerFile 4
, cdbDiskPolicy = LgrDB.defaultDiskPolicy (configSecurityParam cfg) Nothing
, cdbDiskPolicy = LgrDB.defaultDiskPolicy (configSecurityParam cfg) LgrDB.DefaultRequestInterval
-- Integration
, cdbTopLevelConfig = cfg
, cdbChunkInfo = ImmutableDB.simpleChunkInfo epochSize0
Expand Down
Expand Up @@ -40,7 +40,7 @@ import Ouroboros.Consensus.Storage.ChainDB.Impl.LgrDB (LgrDB,
import qualified Ouroboros.Consensus.Storage.ChainDB.Impl.LgrDB as LgrDB
import Ouroboros.Consensus.Storage.FS.API (HasFS, SomeHasFS (..))
import Ouroboros.Consensus.Storage.LedgerDB.DiskPolicy
(defaultDiskPolicy)
(RequestedInterval (..), defaultDiskPolicy)
import qualified Ouroboros.Consensus.Storage.LedgerDB.InMemory as LgrDB
(ledgerDbPast, ledgerDbTip, ledgerDbWithAnchor)

Expand Down Expand Up @@ -210,7 +210,7 @@ initLgrDB k chain = do
args = LgrDbArgs
{ lgrTopLevelConfig = cfg
, lgrHasFS = SomeHasFS (error "lgrHasFS" :: HasFS m ())
, lgrDiskPolicy = defaultDiskPolicy k Nothing
, lgrDiskPolicy = defaultDiskPolicy k DefaultRequestInterval
, lgrGenesis = return testInitExtLedger
, lgrTracer = nullTracer
, lgrTraceLedger = nullTracer
Expand Down
Expand Up @@ -95,7 +95,7 @@ import Ouroboros.Consensus.Storage.ImmutableDB.Chunks.Internal
(unsafeChunkNoToEpochNo)
import qualified Ouroboros.Consensus.Storage.ImmutableDB.Impl.Index as Index
import Ouroboros.Consensus.Storage.LedgerDB.DiskPolicy
(defaultDiskPolicy)
(RequestedInterval (..), defaultDiskPolicy)
import Ouroboros.Consensus.Storage.LedgerDB.InMemory (LedgerDB)
import qualified Ouroboros.Consensus.Storage.LedgerDB.OnDisk as LedgerDB
import qualified Ouroboros.Consensus.Storage.VolatileDB as VolatileDB
Expand Down Expand Up @@ -1580,7 +1580,7 @@ mkArgs cfg (MaxClockSkew maxClockSkew) chunkInfo initLedger tracer registry varC
, cdbImmutableDbValidation = ValidateAllChunks
, cdbVolatileDbValidation = VolatileDB.ValidateAll
, cdbMaxBlocksPerFile = VolatileDB.mkBlocksPerFile 4
, cdbDiskPolicy = defaultDiskPolicy (configSecurityParam cfg) Nothing
, cdbDiskPolicy = defaultDiskPolicy (configSecurityParam cfg) DefaultRequestInterval

-- Integration
, cdbTopLevelConfig = cfg
Expand Down
4 changes: 2 additions & 2 deletions ouroboros-consensus/src/Ouroboros/Consensus/Node.hs
Expand Up @@ -110,7 +110,7 @@ import Ouroboros.Consensus.Storage.FS.IO (ioHasFS)
import Ouroboros.Consensus.Storage.ImmutableDB (ChunkInfo,
ValidationPolicy (..))
import Ouroboros.Consensus.Storage.LedgerDB.DiskPolicy
(RequestedInterval, defaultDiskPolicy)
(RequestedInterval (..), defaultDiskPolicy)
import Ouroboros.Consensus.Storage.VolatileDB
(BlockValidationPolicy (..))

Expand Down Expand Up @@ -614,7 +614,7 @@ data StdRunNodeArgs m blk = StdRunNodeArgs
, srnBfcMaxConcurrencyDeadline :: Maybe Word
, srcChainDbValidateOverride :: Bool
-- ^ If @True@, validate the ChainDB on init no matter what
, srnRequestedInterval :: Maybe RequestedInterval
, srnRequestedInterval :: RequestedInterval
, srnDatabasePath :: FilePath
-- ^ Location of the DBs
, srnDiffusionArguments :: DiffusionArguments
Expand Down
Expand Up @@ -19,9 +19,9 @@ import Control.Monad.Class.MonadTime
import Ouroboros.Consensus.Config.SecurityParam


newtype RequestedInterval = RequestedInterval
{ unRequestedInterval :: Word64
}
data RequestedInterval =
DefaultRequestInterval
| RequestedInterval Word64

-- | On-disk policy
--
Expand Down Expand Up @@ -85,8 +85,8 @@ data DiskPolicy = DiskPolicy {
-- take a snapshot roughly every @k@ blocks. It does mean the possibility of
-- an extra unnecessary snapshot during syncing (if the node is restarted), but
-- that is not a big deal.
defaultDiskPolicy :: SecurityParam -> Maybe RequestedInterval -> DiskPolicy
defaultDiskPolicy (SecurityParam k) maybeRequestedInterval = DiskPolicy {..}
defaultDiskPolicy :: SecurityParam -> RequestedInterval -> DiskPolicy
defaultDiskPolicy (SecurityParam k) requestedInterval = DiskPolicy {..}
where
onDiskNumSnapshots :: Word
onDiskNumSnapshots = 2
Expand All @@ -97,12 +97,13 @@ defaultDiskPolicy (SecurityParam k) maybeRequestedInterval = DiskPolicy {..}
-> Bool
onDiskShouldTakeSnapshot Nothing blocksSinceLast = blocksSinceLast >= k
onDiskShouldTakeSnapshot (Just timeSinceLast) blocksSinceLast =
let snapshotIntervalSeconds =
maybe (k * 2) unRequestedInterval maybeRequestedInterval
snapshotInterval =
secondsToDiffTime (fromIntegral snapshotIntervalSeconds)
let snapshotIntervalSeconds = fromIntegral $ interval requestedInterval
snapshotInterval = secondsToDiffTime snapshotIntervalSeconds
itsBeen50kBlocks = blocksSinceLast >= 50_000
itsBeen6MinSinceLastSnapshot = timeSinceLast > 6 * secondsToDiffTime 60
in
timeSinceLast >= snapshotInterval
|| itsBeen50kBlocks && itsBeen6MinSinceLastSnapshot

interval (RequestedInterval value) = value
interval DefaultRequestInterval = k * 2

0 comments on commit bb1db87

Please sign in to comment.