Skip to content

Commit

Permalink
fix database director configuration and remove deprecated NodeId
Browse files Browse the repository at this point in the history
  • Loading branch information
larskuhtz committed Jul 30, 2020
1 parent 61d4305 commit af1112f
Show file tree
Hide file tree
Showing 16 changed files with 180 additions and 219 deletions.
2 changes: 1 addition & 1 deletion bench/Chainweb/Pact/Backend/ForkingBench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ withResources trunkLength logLevel f = C.envWithCleanup create destroy unwrap
coinAccounts <- newMVar mempty
nonceCounter <- newIORef 1
txPerBlock <- newIORef 10
sqlEnv <- startSqliteDb testVer cid logger (Just tempDir) Nothing False
sqlEnv <- startSqliteDb cid logger tempDir False
pactService <-
startPact testVer logger blockHeaderDb payloadDb (testMemPoolAccess txPerBlock coinAccounts) sqlEnv
mainTrunkBlocks <-
Expand Down
1 change: 0 additions & 1 deletion chainweb.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ library
, Chainweb.Miner.RestAPI
, Chainweb.Miner.RestAPI.Client
, Chainweb.Miner.RestAPI.Server
, Chainweb.NodeId
, Chainweb.NodeVersion
, Chainweb.Payload
, Chainweb.Payload.PayloadStore
Expand Down
98 changes: 89 additions & 9 deletions node/ChainwebNode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@ pChainwebNodeConfiguration = id
% long "reset-chain-databases"
<> help "Reset the chain databases for all chains on startup"

getRocksDbDir :: ChainwebNodeConfiguration -> IO FilePath
getRocksDbDir conf = (<> "/rocksDb") <$> getDbBaseDir conf

getPactDbDir :: ChainwebNodeConfiguration -> IO FilePath
getPactDbDir conf = (<> "/sqlite") <$> getDbBaseDir conf

getDbBaseDir :: ChainwebNodeConfiguration -> IO FilePath
getDbBaseDir conf = case _nodeConfigDatabaseDirectory conf of
Nothing -> getXdgDirectory XdgData
$ "chainweb-node/" <> sshow v <> "/0"
Just d -> return d
where
v = _configChainwebVersion $ _nodeConfigChainweb conf

-- -------------------------------------------------------------------------- --
-- Monitors

Expand Down Expand Up @@ -300,11 +314,14 @@ runQueueMonitor logger cutDb = L.withLoggerLabel ("component", "queue-monitor")

node :: Logger logger => ChainwebNodeConfiguration -> logger -> IO ()
node conf logger = do
rocksDbDir <- getRocksDbDir
when (_nodeConfigResetChainDbs conf) $ destroyRocksDb rocksDbDir
migrateDbDirectory logger conf
dbBaseDir <- getDbBaseDir conf
when (_nodeConfigResetChainDbs conf) $ removeDirectoryRecursive dbBaseDir
rocksDbDir <- getRocksDbDir conf
pactDbDir <- getPactDbDir conf
withRocksDb rocksDbDir $ \rocksDb -> do
logFunctionText logger Info $ "opened rocksdb in directory " <> sshow rocksDbDir
withChainweb cwConf logger rocksDb (_nodeConfigDatabaseDirectory conf) (_nodeConfigResetChainDbs conf) $ \cw -> mapConcurrently_ id
withChainweb cwConf logger rocksDb pactDbDir (_nodeConfigResetChainDbs conf) $ \cw -> mapConcurrently_ id
[ runChainweb cw
-- we should probably push 'onReady' deeper here but this should be ok
, runCutMonitor (_chainwebLogger cw) (_cutResCutDb $ _chainwebCutResources cw)
Expand All @@ -315,14 +332,10 @@ node conf logger = do
]
where
cwConf = _nodeConfigChainweb conf
nodeText = T.unpack (toText (_configNodeId cwConf))
v = _configChainwebVersion cwConf
getRocksDbDir = case _nodeConfigDatabaseDirectory conf of
Nothing -> getXdgDirectory XdgData
$ "chainweb-node/" <> sshow v <> "/" <> nodeText <> "/rocksDb"
Just d -> return d
amberdataConfig = _logConfigAmberdataBackend . _nodeConfigLog



withNodeLogger
:: LogConfig
-> ChainwebVersion
Expand Down Expand Up @@ -501,3 +514,70 @@ main = do
node conf logger
where
timeFormat = iso8601DateFormat (Just "%H:%M:%SZ")

-- -------------------------------------------------------------------------- --
-- Database Director Migration
--
-- Legacy default locations:
--
-- `$XDGDATA/chainweb-node/$CHAINWEB_VERSION/$NODEID/rocksDb`
-- `$XDGDATA/chainweb-node/$CHAINWEB_VERSION/$NODEID/sqlite`
--
-- New default locations:
--
-- `$XDGDATA/chainweb-node/$CHAINWEB_VERSION/0/rocksDb`
-- `$XDGDATA/chainweb-node/$CHAINWEB_VERSION/0/sqlite`
--
-- Legacy custom locations:
--
-- `$CUSTOM_PATH/rocksDb`
-- `$CUSTOM_PATH/rocksDbsqlite`
--
-- New custom locations:
--
-- `$CUSTOM_PATH/rocksDb`
-- `$CUSTOM_PATH/sqlite`
--
-- Migration scenarios:
--
-- 1. Custom location configured (and directory exists):
--
-- * Log warning
-- * `mv "$CUSTOM_PATH/rocksDbslqite "$CUSTOM_PATH/sqlite"`
-- * fail if target directory already exists
--
-- 2. No custom location configured:
--
-- * Log warning if `$XDGDATA/chainweb-node/$CHAINWEB_VERSION/[1-9]*` exists
--
--
-- Migration code can be removed in the next version. We don't need to support
-- longer backwards compatibility, because in those situations it will be faster
-- and more convenient to start over with a fresh db.
--

migrateDbDirectory :: Logger logger => logger -> ChainwebNodeConfiguration -> IO ()
migrateDbDirectory logger config = case _nodeConfigDatabaseDirectory config of
Just custom -> do
let legacyCustomPact = custom <> "sqlite"
newCustomPact <- getPactDbDir config
whenM (doesDirectoryExist legacyCustomPact) $ do
logg Warn
$ "Updated database directory layout for new chainweb version"
<> ". Old pact db location: " <> T.pack legacyCustomPact
<> ". New pact db location: " <> T.pack newCustomPact
targetExists <- doesDirectoryExist newCustomPact
if targetExists
then logg Error
$ "Can't move old pact database to new location because the database already exists"
<> ". The database at the new location will be used."
else renameDirectory legacyCustomPact newCustomPact
Nothing -> do
defDir <- getXdgDirectory XdgData $ "chainweb-node/" <> sshow v
dirs <- getDirectoryContents defDir
forM_ (filter (/= defDir <> "/0") dirs) $ \i ->
logg Warn $ "ignoring existing database directory " <> T.pack i
where
logg = logFunctionText logger
v = _configChainwebVersion $ _nodeConfigChainweb config

39 changes: 20 additions & 19 deletions src/Chainweb/Chainweb.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ module Chainweb.Chainweb

-- * Chainweb Configuration
, ChainwebConfiguration(..)
, configNodeId
, configChainwebVersion
, configMining
, configHeaderStream
Expand Down Expand Up @@ -127,6 +126,7 @@ import Control.Error.Util (note)
import Control.Lens hiding ((.=), (<.>))
import Control.Monad
import Control.Monad.Catch (throwM)
import Control.Monad.Writer

import Data.Bifunctor (second)
import Data.CAS (casLookupM)
Expand All @@ -135,7 +135,6 @@ import Data.Function (on)
import qualified Data.HashMap.Strict as HM
import Data.List (isPrefixOf, sortBy)
import Data.Maybe
import Data.Monoid
import qualified Data.Text as T
import Data.These (These(..))
import Data.Tuple.Strict (T2(..))
Expand Down Expand Up @@ -178,7 +177,6 @@ import qualified Chainweb.Mempool.InMemTypes as Mempool
import qualified Chainweb.Mempool.Mempool as Mempool
import Chainweb.Mempool.P2pConfig
import Chainweb.Miner.Config
import Chainweb.NodeId
import Chainweb.Pact.RestAPI.Server (PactServerData)
import Chainweb.Pact.Service.Types (PactServiceConfig(..))
import Chainweb.Pact.Types (defaultReorgLimit)
Expand Down Expand Up @@ -304,7 +302,8 @@ defaultCutConfig = CutConfig

data ChainwebConfiguration = ChainwebConfiguration
{ _configChainwebVersion :: !ChainwebVersion
, _configNodeId :: !NodeId
, _configNodeIdDeprecated :: !Int
-- ^ Deprecated, won't show up in --print-config
, _configCuts :: !CutConfig
, _configMining :: !MiningConfig
, _configHeaderStream :: !Bool
Expand All @@ -328,14 +327,18 @@ instance HasChainwebVersion ChainwebConfiguration where
_chainwebVersion = _configChainwebVersion
{-# INLINE _chainwebVersion #-}

validateChainwebConfiguration :: ConfigValidation ChainwebConfiguration l
validateChainwebConfiguration :: ConfigValidation ChainwebConfiguration []
validateChainwebConfiguration c = do
validateMinerConfig (_configMining c)
unless (_configNodeIdDeprecated c == (-1)) $ tell
[ "Usage NodeId is deprecated. This option will be removed in a future version of chainweb-node"
, "The value of NodeId is ignored by chainweb-node. In particular the database path will not depend on it"
]

defaultChainwebConfiguration :: ChainwebVersion -> ChainwebConfiguration
defaultChainwebConfiguration v = ChainwebConfiguration
{ _configChainwebVersion = v
, _configNodeId = NodeId 0 -- FIXME
, _configNodeIdDeprecated = (-1)
, _configCuts = defaultCutConfig
, _configMining = defaultMining
, _configHeaderStream = False
Expand All @@ -355,7 +358,6 @@ defaultChainwebConfiguration v = ChainwebConfiguration
instance ToJSON ChainwebConfiguration where
toJSON o = object
[ "chainwebVersion" .= _configChainwebVersion o
, "nodeId" .= _configNodeId o
, "cuts" .= _configCuts o
, "mining" .= _configMining o
, "headerStream" .= _configHeaderStream o
Expand All @@ -375,7 +377,7 @@ instance ToJSON ChainwebConfiguration where
instance FromJSON (ChainwebConfiguration -> ChainwebConfiguration) where
parseJSON = withObject "ChainwebConfig" $ \o -> id
<$< configChainwebVersion ..: "chainwebVersion" % o
<*< configNodeId ..: "nodeId" % o
<*< configNodeIdDeprecated ..: "nodeId" % o
<*< configCuts %.: "cuts" % o
<*< configMining %.: "mining" % o
<*< configHeaderStream ..: "headerStream" % o
Expand All @@ -397,10 +399,10 @@ pChainwebConfiguration = id
% long "chainweb-version"
<> short 'v'
<> help "the chainweb version that this node is using"
<*< configNodeId .:: textOption
<*< configNodeIdDeprecated .:: option auto
% long "node-id"
<> short 'i'
<> help "unique id of the node that is used as miner id in new blocks"
<> help "DEPRECATED. The value is ignored"
<*< configHeaderStream .:: boolOption_
% long "header-stream"
<> help "whether to enable an endpoint for streaming block updates"
Expand Down Expand Up @@ -470,19 +472,19 @@ withChainweb
=> ChainwebConfiguration
-> logger
-> RocksDb
-> Maybe FilePath
-> FilePath
-- ^ Pact database directory
-> Bool
-> (forall cas' . PayloadCasLookup cas' => Chainweb logger cas' -> IO a)
-> IO a
withChainweb c logger rocksDb dbDir resetDb inner =
withChainweb c logger rocksDb pactDbDir resetDb inner =
withPeerResources v (view configP2p conf) logger $ \logger' peer ->
withChainwebInternal
(set configP2p (_peerResConfig peer) conf)
logger'
peer
rocksDb
dbDir
(Just (_configNodeId c))
pactDbDir
resetDb
inner
where
Expand Down Expand Up @@ -565,20 +567,19 @@ withChainwebInternal
-> logger
-> PeerResources logger
-> RocksDb
-> Maybe FilePath
-> Maybe NodeId
-> FilePath
-> Bool
-> (forall cas' . PayloadCasLookup cas' => Chainweb logger cas' -> IO a)
-> IO a
withChainwebInternal conf logger peer rocksDb dbDir nodeid resetDb inner = do
withChainwebInternal conf logger peer rocksDb pactDbDir resetDb inner = do
initializePayloadDb v payloadDb
concurrentWith
-- initialize chains concurrently
(\cid -> do
let mcfg = validatingMempoolConfig cid v (_configBlockGasLimit conf)
withChainResources v cid rocksDb peer (chainLogger cid)
mcfg payloadDb prune dbDir nodeid
pactConfig)
mcfg payloadDb prune pactDbDir pactConfig
)

-- initialize global resources after all chain resources are initialized
(\cs -> global (HM.fromList $ zip cidsList cs))
Expand Down
8 changes: 3 additions & 5 deletions src/Chainweb/Chainweb/ChainResources.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ import Chainweb.Mempool.Mempool (MempoolBackend)
import qualified Chainweb.Mempool.Mempool as Mempool
import Chainweb.Mempool.P2pConfig
import qualified Chainweb.Mempool.RestAPI.Client as MPC
import Chainweb.NodeId
import Chainweb.Pact.Service.PactInProcApi
import Chainweb.Pact.Service.Types
import Chainweb.Payload.PayloadStore
Expand Down Expand Up @@ -117,21 +116,20 @@ withChainResources
-> PayloadDb cas
-> Bool
-- ^ whether to prune the chain database
-> Maybe FilePath
-> FilePath
-- ^ database directory for checkpointer
-> Maybe NodeId
-> PactServiceConfig
-> (ChainResources logger -> IO a)
-> IO a
withChainResources
v cid rdb peer logger mempoolCfg0 payloadDb prune dbDir nodeid pactConfig inner =
v cid rdb peer logger mempoolCfg0 payloadDb prune pactDbDir pactConfig inner =
withBlockHeaderDb rdb v cid $ \cdb -> do
pexMv <- newEmptyMVar
let mempoolCfg = mempoolCfg0 pexMv
Mempool.withInMemoryMempool_ (setComponent "mempool" logger) mempoolCfg v $ \mempool -> do
mpc <- MPCon.mkMempoolConsensus mempool cdb $ Just payloadDb
withPactService v cid (setComponent "pact" logger) mpc cdb
payloadDb dbDir nodeid pactConfig $ \requestQ -> do
payloadDb pactDbDir pactConfig $ \requestQ -> do
-- prune block header db
when prune $ do
logg Info "start pruning block header database"
Expand Down
Loading

0 comments on commit af1112f

Please sign in to comment.