{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE RecordWildCards #-}
module Ouroboros.Consensus.Storage.LedgerDB.DiskPolicy (
DiskPolicy (..)
, SnapshotInterval (..)
, TimeSinceLast (..)
, defaultDiskPolicy
) where
import Data.Time.Clock (secondsToDiffTime)
import Data.Word
import GHC.Generics
import NoThunks.Class (NoThunks, OnlyCheckWhnf (..))
import Control.Monad.Class.MonadTime
import Ouroboros.Consensus.Config.SecurityParam
data SnapshotInterval =
DefaultSnapshotInterval
| RequestedSnapshotInterval DiffTime
deriving stock (SnapshotInterval -> SnapshotInterval -> Bool
(SnapshotInterval -> SnapshotInterval -> Bool)
-> (SnapshotInterval -> SnapshotInterval -> Bool)
-> Eq SnapshotInterval
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SnapshotInterval -> SnapshotInterval -> Bool
$c/= :: SnapshotInterval -> SnapshotInterval -> Bool
== :: SnapshotInterval -> SnapshotInterval -> Bool
$c== :: SnapshotInterval -> SnapshotInterval -> Bool
Eq, (forall x. SnapshotInterval -> Rep SnapshotInterval x)
-> (forall x. Rep SnapshotInterval x -> SnapshotInterval)
-> Generic SnapshotInterval
forall x. Rep SnapshotInterval x -> SnapshotInterval
forall x. SnapshotInterval -> Rep SnapshotInterval x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep SnapshotInterval x -> SnapshotInterval
$cfrom :: forall x. SnapshotInterval -> Rep SnapshotInterval x
Generic, Int -> SnapshotInterval -> ShowS
[SnapshotInterval] -> ShowS
SnapshotInterval -> String
(Int -> SnapshotInterval -> ShowS)
-> (SnapshotInterval -> String)
-> ([SnapshotInterval] -> ShowS)
-> Show SnapshotInterval
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SnapshotInterval] -> ShowS
$cshowList :: [SnapshotInterval] -> ShowS
show :: SnapshotInterval -> String
$cshow :: SnapshotInterval -> String
showsPrec :: Int -> SnapshotInterval -> ShowS
$cshowsPrec :: Int -> SnapshotInterval -> ShowS
Show)
data DiskPolicy = DiskPolicy {
DiskPolicy -> Word
onDiskNumSnapshots :: Word
, DiskPolicy -> TimeSinceLast DiffTime -> Word64 -> Bool
onDiskShouldTakeSnapshot :: TimeSinceLast DiffTime -> Word64 -> Bool
}
deriving Context -> DiskPolicy -> IO (Maybe ThunkInfo)
Proxy DiskPolicy -> String
(Context -> DiskPolicy -> IO (Maybe ThunkInfo))
-> (Context -> DiskPolicy -> IO (Maybe ThunkInfo))
-> (Proxy DiskPolicy -> String)
-> NoThunks DiskPolicy
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy DiskPolicy -> String
$cshowTypeOf :: Proxy DiskPolicy -> String
wNoThunks :: Context -> DiskPolicy -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> DiskPolicy -> IO (Maybe ThunkInfo)
noThunks :: Context -> DiskPolicy -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> DiskPolicy -> IO (Maybe ThunkInfo)
NoThunks via OnlyCheckWhnf DiskPolicy
data TimeSinceLast time = NoSnapshotTakenYet | TimeSinceLast time
deriving (a -> TimeSinceLast b -> TimeSinceLast a
(a -> b) -> TimeSinceLast a -> TimeSinceLast b
(forall a b. (a -> b) -> TimeSinceLast a -> TimeSinceLast b)
-> (forall a b. a -> TimeSinceLast b -> TimeSinceLast a)
-> Functor TimeSinceLast
forall a b. a -> TimeSinceLast b -> TimeSinceLast a
forall a b. (a -> b) -> TimeSinceLast a -> TimeSinceLast b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> TimeSinceLast b -> TimeSinceLast a
$c<$ :: forall a b. a -> TimeSinceLast b -> TimeSinceLast a
fmap :: (a -> b) -> TimeSinceLast a -> TimeSinceLast b
$cfmap :: forall a b. (a -> b) -> TimeSinceLast a -> TimeSinceLast b
Functor, Int -> TimeSinceLast time -> ShowS
[TimeSinceLast time] -> ShowS
TimeSinceLast time -> String
(Int -> TimeSinceLast time -> ShowS)
-> (TimeSinceLast time -> String)
-> ([TimeSinceLast time] -> ShowS)
-> Show (TimeSinceLast time)
forall time. Show time => Int -> TimeSinceLast time -> ShowS
forall time. Show time => [TimeSinceLast time] -> ShowS
forall time. Show time => TimeSinceLast time -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TimeSinceLast time] -> ShowS
$cshowList :: forall time. Show time => [TimeSinceLast time] -> ShowS
show :: TimeSinceLast time -> String
$cshow :: forall time. Show time => TimeSinceLast time -> String
showsPrec :: Int -> TimeSinceLast time -> ShowS
$cshowsPrec :: forall time. Show time => Int -> TimeSinceLast time -> ShowS
Show)
defaultDiskPolicy :: SecurityParam -> SnapshotInterval -> DiskPolicy
defaultDiskPolicy :: SecurityParam -> SnapshotInterval -> DiskPolicy
defaultDiskPolicy (SecurityParam Word64
k) SnapshotInterval
requestedInterval = DiskPolicy :: Word -> (TimeSinceLast DiffTime -> Word64 -> Bool) -> DiskPolicy
DiskPolicy {Word
TimeSinceLast DiffTime -> Word64 -> Bool
onDiskShouldTakeSnapshot :: TimeSinceLast DiffTime -> Word64 -> Bool
onDiskNumSnapshots :: Word
onDiskShouldTakeSnapshot :: TimeSinceLast DiffTime -> Word64 -> Bool
onDiskNumSnapshots :: Word
..}
where
onDiskNumSnapshots :: Word
onDiskNumSnapshots :: Word
onDiskNumSnapshots = Word
2
onDiskShouldTakeSnapshot ::
TimeSinceLast DiffTime
-> Word64
-> Bool
onDiskShouldTakeSnapshot :: TimeSinceLast DiffTime -> Word64 -> Bool
onDiskShouldTakeSnapshot TimeSinceLast DiffTime
NoSnapshotTakenYet Word64
blocksSinceLast =
Word64
blocksSinceLast Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word64
k
onDiskShouldTakeSnapshot (TimeSinceLast DiffTime
timeSinceLast) Word64
blocksSinceLast =
DiffTime
timeSinceLast DiffTime -> DiffTime -> Bool
forall a. Ord a => a -> a -> Bool
>= DiffTime
snapshotInterval
Bool -> Bool -> Bool
|| Word64 -> DiffTime -> Bool
forall a. (Ord a, Num a) => a -> DiffTime -> Bool
substantialAmountOfBlocksWereProcessed Word64
blocksSinceLast DiffTime
timeSinceLast
substantialAmountOfBlocksWereProcessed :: a -> DiffTime -> Bool
substantialAmountOfBlocksWereProcessed a
blocksSinceLast DiffTime
timeSinceLast =
let minBlocksBeforeSnapshot :: a
minBlocksBeforeSnapshot = a
50_000
minTimeBeforeSnapshot :: DiffTime
minTimeBeforeSnapshot = DiffTime
6 DiffTime -> DiffTime -> DiffTime
forall a. Num a => a -> a -> a
* Integer -> DiffTime
secondsToDiffTime Integer
60
in a
blocksSinceLast a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
minBlocksBeforeSnapshot
Bool -> Bool -> Bool
&& DiffTime
timeSinceLast DiffTime -> DiffTime -> Bool
forall a. Ord a => a -> a -> Bool
>= DiffTime
minTimeBeforeSnapshot
snapshotInterval :: DiffTime
snapshotInterval = case SnapshotInterval
requestedInterval of
RequestedSnapshotInterval DiffTime
value -> DiffTime
value
SnapshotInterval
DefaultSnapshotInterval -> Integer -> DiffTime
secondsToDiffTime (Integer -> DiffTime) -> Integer -> DiffTime
forall a b. (a -> b) -> a -> b
$ Word64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Integer) -> Word64 -> Integer
forall a b. (a -> b) -> a -> b
$ Word64
k Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
* Word64
2