Skip to content

Commit

Permalink
Add socket option to local-cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Apr 23, 2024
1 parent 62f8c6a commit 3a2650e
Show file tree
Hide file tree
Showing 18 changed files with 300 additions and 191 deletions.
5 changes: 3 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ bench target:

# run a local test cluster
local-cluster:
nix shell '.#local-cluster' '.#cardano-node' '.#cardano-wallet' \
nix shell '.#local-cluster' '.#cardano-node' \
-c "local-cluster" \
control \
--cluster-configs lib/local-cluster/test/data/cluster-configs \
--faucet-funds ${FAUCET_FUNDS_FILE} \
--control-port 12798 \
--pulling-mode not-pulling \
--monitoring-port 12799 \
--cluster-logs ignore-me/cluster.logs
--cluster-logs ignore-me/cluster.logs \
--socket-path ignore-me/cluster.socket

# run unit tests on a match
unit-tests-cabal-match match:
Expand Down
3 changes: 3 additions & 0 deletions lib/benchmarks/cardano-wallet-benchmarks.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ library
, criterion-measurement
, deepseq
, directory
, extra
, faucet
, filepath
, fmt
Expand All @@ -64,6 +65,7 @@ library
, local-cluster
, mtl
, optparse-applicative
, pathtype
, resourcet
, say
, servant-client
Expand Down Expand Up @@ -133,6 +135,7 @@ benchmark latency
, cardano-wallet-primitive
, directory
, exceptions
, extra
, faucet
, filepath
, fmt
Expand Down
57 changes: 32 additions & 25 deletions lib/benchmarks/exe/latency-bench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ import Cardano.Wallet.Launch.Cluster.CommandLine
)
import Cardano.Wallet.Launch.Cluster.FileOf
( DirOf (..)
, FileOf (..)
, mkRelDirOf
, newAbsolutizer
, toFilePath
Expand Down Expand Up @@ -204,12 +205,16 @@ import System.Directory
import System.Environment.Extended
( isEnvSet
)
import System.IO.Extra
( withTempFile
)
import System.IO.Temp.Extra
( SkipCleanup (..)
, withSystemTempDir
)
import System.Path
( absDir
, absFile
, relDir
, (</>)
)
Expand Down Expand Up @@ -655,31 +660,33 @@ withShelleyServer tracers action = withFaucet $ \faucetClientEnv -> do
$ Just
$ mkRelDirOf
$ Cluster.clusterEraToString clusterEra
let clusterConfig =
Cluster.Config
{ cfgStakePools = pure (NE.head defaultPoolConfigs)
, cfgLastHardFork = clusterEra
, cfgNodeLogging
, cfgClusterDir = DirOf testDir
, cfgClusterConfigs = clusterConfigsDir
, cfgTestnetMagic
, cfgShelleyGenesisMods =
[ over #sgSlotLength (const 0.2)
, -- to avoid "PastHorizonException" errors, as wallet
-- doesn't keep up with retrieving fresh time interpreter.
over #sgSecurityParam (const 100)
-- when it low then cluster is not making blocks;
]
, cfgTracer = stdoutTextTracer
, cfgNodeOutputFile = Nothing
, cfgRelayNodePath = mkRelDirOf "relay"
, cfgClusterLogFile = Nothing
}
withCluster
nullTracer
clusterConfig
faucetFunds
(onClusterStart cfgTestnetMagic setupAction db)
withTempFile $ \socket -> do
let clusterConfig =
Cluster.Config
{ cfgStakePools = pure (NE.head defaultPoolConfigs)
, cfgLastHardFork = clusterEra
, cfgNodeLogging
, cfgClusterDir = DirOf testDir
, cfgClusterConfigs = clusterConfigsDir
, cfgTestnetMagic
, cfgShelleyGenesisMods =
[ over #sgSlotLength (const 0.2)
, -- to avoid "PastHorizonException" errors, as wallet
-- doesn't keep up with retrieving fresh time interpreter.
over #sgSecurityParam (const 100)
-- when it low then cluster is not making blocks;
]
, cfgTracer = stdoutTextTracer
, cfgNodeOutputFile = Nothing
, cfgRelayNodePath = mkRelDirOf "relay"
, cfgClusterLogFile = Nothing
, cfgNodeToClientSocket = FileOf $ absFile socket
}
withCluster
nullTracer
clusterConfig
faucetFunds
(onClusterStart cfgTestnetMagic setupAction db)

onClusterStart testnetMagic setupAction db node = do
let (RunningNode conn genesisData vData) = node
Expand Down
54 changes: 34 additions & 20 deletions lib/benchmarks/src/Cardano/Wallet/BenchShared.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE ScopedTypeVariables #-}
Expand Down Expand Up @@ -50,7 +52,9 @@ import Cardano.BM.Trace
import Cardano.Launcher.Node
( CardanoNodeConfig (..)
, CardanoNodeConn
, MaybeK (..)
, NodePort (..)
, Presence (..)
, cardanoNodeConn
, withCardanoNode
)
Expand Down Expand Up @@ -96,6 +100,9 @@ import GHC.Generics
( Generic
)
-- See ADP-1910
import Cardano.Launcher
( ProcessHasExited
)
import "optparse-applicative" Options.Applicative
( HasValue
, Mod
Expand Down Expand Up @@ -132,6 +139,9 @@ import System.Exit
import System.FilePath
( (</>)
)
import System.IO.Extra
( withTempFile
)
import Test.Utils.Startup
( withNoBuffering
)
Expand All @@ -148,9 +158,6 @@ import UnliftIO.Temporary

import qualified Cardano.BM.Configuration.Model as CM
import qualified Cardano.BM.Data.BackendKind as CM
import Cardano.Launcher
( ProcessHasExited
)

{-------------------------------------------------------------------------------
CLI option handling and cardano-node configuration
Expand Down Expand Up @@ -180,15 +187,18 @@ execBenchWithNode networkConfig action = withNoBuffering $ do
Nothing
Nothing
nodeConfig
$ action tr (networkConfig args)
$ \(JustK socket) -> action tr (networkConfig args) socket
case res of
Left (exited :: ProcessHasExited) -> do
sayErr $ "FAIL: cardano-node exited with status "
<> toText exited
pure $ ExitFailure 1
Right _ -> pure ExitSuccess

withNetworkConfiguration :: RestoreBenchArgs -> (CardanoNodeConfig -> IO a) -> IO a
withNetworkConfiguration
:: RestoreBenchArgs
-> (CardanoNodeConfig 'Present -> IO a)
-> IO a
withNetworkConfiguration args action = do
-- Temporary directory for storing socket and node database
let withNodeDir cb = case argNodeDatabaseDir args of
Expand All @@ -199,21 +209,25 @@ withNetworkConfiguration args action = do

let networkDir = argsNetworkDir args
port <- fromIntegral <$> getRandomPort
withNodeDir $ \dir -> action CardanoNodeConfig
{ nodeDir = dir
, nodeConfigFile = networkDir </> "config.json"
, nodeDatabaseDir = fromMaybe "db" (argNodeDatabaseDir args)
, nodeDlgCertFile = Nothing
, nodeSignKeyFile = Nothing
, nodeTopologyFile = networkDir </> "topology.json"
, nodeOpCertFile = Nothing
, nodeKesKeyFile = Nothing
, nodeVrfKeyFile = Nothing
, nodePort = Just (NodePort port)
, nodeLoggingHostname = Nothing
, nodeExecutable = Nothing
, nodeOutputFile = Nothing
}
withNodeDir $ \dir ->
withTempFile $ \socket -> do
action
CardanoNodeConfig
{ nodeDir = dir
, nodeConfigFile = networkDir </> "config.json"
, nodeDatabaseDir = fromMaybe "db" (argNodeDatabaseDir args)
, nodeDlgCertFile = Nothing
, nodeSignKeyFile = Nothing
, nodeTopologyFile = networkDir </> "topology.json"
, nodeOpCertFile = Nothing
, nodeKesKeyFile = Nothing
, nodeVrfKeyFile = Nothing
, nodePort = Just (NodePort port)
, nodeLoggingHostname = Nothing
, nodeExecutable = Nothing
, nodeOutputFile = Nothing
, nodeSocketPathFile = JustK socket
}

argsNetworkDir :: RestoreBenchArgs -> FilePath
argsNetworkDir args = argConfigsDir args </> argNetworkName args
Expand Down
2 changes: 0 additions & 2 deletions lib/integration/cardano-wallet-integration.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ library framework
, microstache
, monad-loops
, mtl
, path
, path-io
, network-uri
, resourcet
, retry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import Cardano.Wallet.Launch.Cluster.FileOf
( DirOf (..)
, FileOf (..)
, absFilePathOf
, relDirOf
, toFilePath
)
import Cardano.Wallet.Launch.Cluster.Monitoring.Http.Client
Expand Down Expand Up @@ -101,7 +100,6 @@ import System.Environment
)
import System.IO.Extra
( IOMode (..)
, withFile
, withTempFile
)
import System.Path
Expand All @@ -119,13 +117,24 @@ import System.Process.Extra
, proc
)
import UnliftIO
( SomeException
( Handle
, SomeException
, finally
, hClose
, openFile
, try
)

import qualified Data.Aeson as Aeson
import qualified Data.ByteString as BS

-- | The withFile from base intercept exceptions and rethrows them after messing
-- up the exception informations. :(
withFile :: FilePath -> IOMode -> (Handle -> IO a) -> IO a
withFile fp mode f = do
h <- openFile fp mode
f h `finally` hClose h

withLocalClusterProcess
:: HasCallStack
=> CommandLineOptions
Expand Down Expand Up @@ -166,6 +175,8 @@ withLocalClusterProcess CommandLineOptions{..} cfgTracer era = do
, toFilePath $ absDirOf clusterConfigsDir
, "--faucet-funds"
, toFilePath $ absFileOf faucetFundsFile
, "--socket-path"
, toFilePath $ absFileOf nodeToClientSocket
]
<> case clusterDir of
Nothing -> []
Expand All @@ -186,13 +197,10 @@ withFaucetFunds faucetFunds = ContT $ \action ->

withSocketPath
:: HasCallStack
=> DirOf s
=> FileOf s
-> ContT r m CardanoNodeConn
withSocketPath cfgClusterDir = ContT $ \f ->
case cardanoNodeConn
$ nodeSocketPath
$ toFilePath
$ absDirOf cfgClusterDir of
withSocketPath socketFilePath = ContT $ \f ->
case cardanoNodeConn $ nodeSocketPath $ absFilePathOf socketFilePath of
Left err -> error $ "Failed to get socket path: " ++ err
Right socketPath -> f socketPath

Expand Down Expand Up @@ -238,15 +246,12 @@ withLocalCluster
clusterDir = Just cfgClusterDir
clusterLogs = cfgClusterLogFile
clusterControl = Nothing
nodeToClientSocket = cfgNodeToClientSocket
evalContT $ do
(monitoring, RunQuery queryMonitor) <-
withHttpMonitoring $ MsgHttpMonitoring >$< cfgTracer
faucetFundsFile <- withFaucetFunds faucetFunds
socketPath <-
withSocketPath
$ DirOf
$ absDirOf cfgClusterDir
</> relDirOf cfgRelayNodePath
socketPath <- withSocketPath nodeToClientSocket
withLocalClusterProcess
CommandLineOptions{monitoring = Just monitoring, ..}
cfgTracer
Expand Down

0 comments on commit 3a2650e

Please sign in to comment.