|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE DerivingStrategies #-} |
| 3 | +{-# LANGUAGE GeneralisedNewtypeDeriving #-} |
| 4 | + |
| 5 | +{-# OPTIONS_GHC -Wno-orphans #-} |
| 6 | + |
| 7 | +module Cardano.Node.Configuration.LedgerDB ( |
| 8 | + DeprecatedOptions (..) |
| 9 | + , LedgerDbConfiguration (..) |
| 10 | + , LedgerDbSelectorFlag(..) |
| 11 | + , Gigabytes |
| 12 | + , noDeprecatedOptions |
| 13 | + , selectorToArgs |
| 14 | + ) where |
| 15 | + |
| 16 | +import Ouroboros.Consensus.Storage.LedgerDB.Args |
| 17 | +import Ouroboros.Consensus.Storage.LedgerDB.Snapshots |
| 18 | +import qualified Ouroboros.Consensus.Storage.LedgerDB.V1.Args as V1 |
| 19 | +import Ouroboros.Consensus.Storage.LedgerDB.V1.BackingStore.Impl.LMDB (LMDBLimits (..)) |
| 20 | +import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.Args as V2 |
| 21 | +import Ouroboros.Consensus.Util.Args |
| 22 | + |
| 23 | +import qualified Data.Aeson.Types as Aeson (FromJSON) |
| 24 | +import Data.Maybe (fromMaybe) |
| 25 | +import Data.SOP.Dict |
| 26 | + |
| 27 | +-- | Choose the LedgerDB Backend |
| 28 | +-- |
| 29 | +-- As of UTxO-HD, the LedgerDB now uses either an in-memory backend or LMDB to |
| 30 | +-- keep track of differences in the UTxO set. |
| 31 | +-- |
| 32 | +-- - 'V2InMemory': uses more memory than the minimum requirements but is somewhat |
| 33 | +-- faster. |
| 34 | +-- |
| 35 | +-- - 'V1LMDB': uses less memory but is somewhat slower. |
| 36 | +-- |
| 37 | +-- - 'V1InMemory': Not intended for production. It is an in-memory reproduction |
| 38 | +-- of the LMDB implementation. |
| 39 | +data LedgerDbSelectorFlag = |
| 40 | + V1LMDB |
| 41 | + V1.FlushFrequency |
| 42 | + -- ^ The frequency at which changes are flushed to the disk. |
| 43 | + (Maybe FilePath) |
| 44 | + -- ^ Path for the live tables. |
| 45 | + (Maybe Gigabytes) |
| 46 | + -- ^ A map size can be specified, this is the maximum disk space the LMDB |
| 47 | + -- database can fill. If not provided, the default of 16GB will be used. |
| 48 | + | V1InMemory V1.FlushFrequency |
| 49 | + | V2InMemory |
| 50 | + deriving (Eq, Show) |
| 51 | + |
| 52 | +-- | Some options that existed in the TopLevel were now moved to a |
| 53 | +-- subsection. We use this field to propagate the results from parsing those |
| 54 | +-- into the monadic part of the node so that we can emit warnings. |
| 55 | +newtype DeprecatedOptions = DeprecatedOptions [String] |
| 56 | + deriving (Eq, Show) |
| 57 | + |
| 58 | +noDeprecatedOptions :: DeprecatedOptions |
| 59 | +noDeprecatedOptions = DeprecatedOptions [] |
| 60 | + |
| 61 | +data LedgerDbConfiguration = |
| 62 | + LedgerDbConfiguration |
| 63 | + NumOfDiskSnapshots |
| 64 | + SnapshotInterval |
| 65 | + QueryBatchSize |
| 66 | + LedgerDbSelectorFlag |
| 67 | + DeprecatedOptions |
| 68 | + deriving (Eq, Show) |
| 69 | + |
| 70 | +-- | A number of gigabytes. |
| 71 | +newtype Gigabytes = Gigabytes Int |
| 72 | + deriving stock (Eq, Show) |
| 73 | + deriving newtype (Read, Aeson.FromJSON) |
| 74 | + |
| 75 | +-- | Convert a number of Gigabytes to the equivalent number of bytes. |
| 76 | +toBytes :: Gigabytes -> Int |
| 77 | +toBytes (Gigabytes x) = x * 1024 * 1024 * 1024 |
| 78 | + |
| 79 | +-- | Recommended settings for the LMDB backing store. |
| 80 | +-- |
| 81 | +-- === @'lmdbMapSize'@ |
| 82 | +-- The default @'LMDBLimits'@ uses an @'lmdbMapSize'@ of @1024 * 1024 * 1024 * 16@ |
| 83 | +-- bytes, or 16 Gigabytes. @'lmdbMapSize'@ sets the size of the memory map |
| 84 | +-- that is used internally by the LMDB backing store, and is also the |
| 85 | +-- maximum size of the on-disk database. 16 GB should be sufficient for the |
| 86 | +-- medium term, i.e., it is sufficient until a more performant alternative to |
| 87 | +-- the LMDB backing store is implemented, which will probably replace the LMDB |
| 88 | +-- backing store altogether. |
| 89 | +-- |
| 90 | +-- Note(jdral): It is recommended not to set the @'lmdbMapSize'@ to a value |
| 91 | +-- that is much smaller than 16 GB through manual configuration: the node will |
| 92 | +-- die with a fatal error as soon as the database size exceeds the |
| 93 | +-- @'lmdbMapSize'@. If this fatal error were to occur, we would expect that |
| 94 | +-- the node can continue normal operation if it is restarted with a higher |
| 95 | +-- @'lmdbMapSize'@ configured. Nonetheless, this situation should be avoided. |
| 96 | +-- |
| 97 | +-- === @'lmdbMaxDatabases'@ |
| 98 | +-- The @'lmdbMaxDatabases'@ is set to 10, which means that the LMDB backing |
| 99 | +-- store will allow up @<= 10@ internal databases. We say /internal/ |
| 100 | +-- databases, since they are not exposed outside the backing store interface, |
| 101 | +-- such that from the outside view there is just one /logical/ database. |
| 102 | +-- Two of these internal databases are reserved for normal operation of the |
| 103 | +-- backing store, while the remaining databases will be used to store ledger |
| 104 | +-- tables. At the moment, there is at most one ledger table that will be |
| 105 | +-- stored in an internal database: the UTxO. Nonetheless, we set |
| 106 | +-- @'lmdbMaxDatabases'@ to @10@ in order to future-proof these limits. |
| 107 | +-- |
| 108 | +-- === @'lmdbMaxReaders'@ |
| 109 | +-- The @'lmdbMaxReaders'@ limit sets the maximum number of threads that can |
| 110 | +-- read from the LMDB database. Currently, there should only be a single reader |
| 111 | +-- active. Again, we set @'lmdbMaxReaders'@ to @16@ in order to future-proof |
| 112 | +-- these limits. |
| 113 | +-- |
| 114 | +-- === References |
| 115 | +-- For more information about LMDB limits, one should inspect: |
| 116 | +-- * The @lmdb-simple@ and @haskell-lmdb@ forked repositories. |
| 117 | +-- * The official LMDB API documentation at |
| 118 | +-- <http://www.lmdb.tech/doc/group__mdb.html>. |
| 119 | +defaultLMDBLimits :: LMDBLimits |
| 120 | +defaultLMDBLimits = LMDBLimits { |
| 121 | + lmdbMapSize = 16 * 1024 * 1024 * 1024 |
| 122 | + , lmdbMaxDatabases = 10 |
| 123 | + , lmdbMaxReaders = 16 |
| 124 | + } |
| 125 | + |
| 126 | +defaultLMDBPath :: FilePath |
| 127 | +defaultLMDBPath = "mainnet/db/lmdb" |
| 128 | + |
| 129 | +selectorToArgs :: LedgerDbSelectorFlag -> Complete LedgerDbFlavorArgs IO |
| 130 | +selectorToArgs (V1InMemory ff) = LedgerDbFlavorArgsV1 $ V1.V1Args ff V1.InMemoryBackingStoreArgs |
| 131 | +selectorToArgs V2InMemory = LedgerDbFlavorArgsV2 $ V2.V2Args V2.InMemoryHandleArgs |
| 132 | +selectorToArgs (V1LMDB ff fp l) = |
| 133 | + LedgerDbFlavorArgsV1 |
| 134 | + $ V1.V1Args ff |
| 135 | + $ V1.LMDBBackingStoreArgs |
| 136 | + (fromMaybe defaultLMDBPath fp) |
| 137 | + (maybe id (\ll lim -> lim { lmdbMapSize = toBytes ll }) l defaultLMDBLimits) |
| 138 | + Dict |
0 commit comments