Skip to content

Commit

Permalink
Merge pull request #3 from kadena-io/lars/ipv6
Browse files Browse the repository at this point in the history
support for IPv6 and logging of event counters
  • Loading branch information
larskuhtz authored Mar 19, 2019
2 parents 0600828 + 16cada1 commit 52c7aea
Show file tree
Hide file tree
Showing 10 changed files with 436 additions and 79 deletions.
51 changes: 36 additions & 15 deletions chainweb.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ source-repository head
type: git
location: https://github.com/kadena-io/chainweb.git

common warning-flags
ghc-options:
-Wall
-Wincomplete-record-updates
-Wincomplete-uni-patterns
-Wredundant-constraints

flag tls13
description:
Enable TLSv1.3; depends on the master branch of the tls package.
Expand All @@ -52,12 +45,32 @@ flag ed25519
default: False
manual: True

flag debug
description:
Enable various debugging features
default: False
manual:True

common debugging-flags
if flag(debug)
ghc-options:
-g
cpp-options:
-DDEBUG_MULTINODE_TEST=1

common warning-flags
ghc-options:
-Wall
-Wincomplete-record-updates
-Wincomplete-uni-patterns
-Wredundant-constraints

-- -------------------------------------------------------------------------- --
-- Chainweb Library
-- -------------------------------------------------------------------------- --

library
import: warning-flags
import: warning-flags, debugging-flags
default-language: Haskell2010
hs-source-dirs: src
exposed-modules:
Expand All @@ -76,6 +89,7 @@ library
, Chainweb.Chainweb.CutResources
, Chainweb.Chainweb.MinerResources
, Chainweb.Chainweb.PeerResources
, Chainweb.Counter
, Chainweb.Crypto.MerkleLog
, Chainweb.Cut
, Chainweb.Cut.CutHashes
Expand Down Expand Up @@ -298,7 +312,7 @@ library
-- -------------------------------------------------------------------------- --

test-suite chainweb-tests
import: warning-flags
import: warning-flags, debugging-flags
default-language: Haskell2010
ghc-options:
-threaded
Expand Down Expand Up @@ -400,8 +414,9 @@ test-suite chainweb-tests
-- -------------------------------------------------------------------------- --
-- Chainweb Benchmarking
-- -------------------------------------------------------------------------- --

benchmark chainweb-bench
import: warning-flags
import: warning-flags, debugging-flags
default-language: Haskell2010
type: exitcode-stdio-1.0
main-is: Bench.hs
Expand All @@ -428,7 +443,7 @@ benchmark chainweb-bench
-- The application that runs an chainweb node
--
executable chainweb-node
import: warning-flags
import: warning-flags, debugging-flags
default-language: Haskell2010
ghc-options:
-threaded
Expand Down Expand Up @@ -465,11 +480,16 @@ executable chainweb-node
, warp >= 3.2
, yet-another-logger >= 0.3.1

-- -------------------------------------------------------------------------- --
-- Slow Tests
-- -------------------------------------------------------------------------- --

-- This is not classified as a test suite because that allows us to do a wider
-- variety of testing such as using the network, etc--things that aren't allowed
-- in a nix build sandbox.
--
executable slow-tests
import: warning-flags
import: warning-flags, debugging-flags
default-language: Haskell2010
ghc-options:
-threaded
Expand Down Expand Up @@ -535,6 +555,7 @@ executable slow-tests
, stm
, stm-chans
, streaming >= 0.2.2
, streaming-commons >= 0.2
, strict-tuple >= 0.1
, tasty >= 1.0
, tasty-golden >= 2.3
Expand All @@ -559,7 +580,7 @@ executable slow-tests
-- -------------------------------------------------------------------------- --

executable blockheaderdb-example
import: warning-flags
import: warning-flags, debugging-flags
default-language: Haskell2010
ghc-options:
-threaded
Expand All @@ -584,7 +605,7 @@ executable blockheaderdb-example
-- -------------------------------------------------------------------------- --

executable p2p-example
import: warning-flags
import: warning-flags, debugging-flags
default-language: Haskell2010
ghc-options:
-threaded
Expand Down Expand Up @@ -619,7 +640,7 @@ executable p2p-example
-- -------------------------------------------------------------------------- --

executable transaction-generator
import: warning-flags
import: warning-flags, debugging-flags
default-language: Haskell2010
ghc-options:
-threaded
Expand Down
9 changes: 5 additions & 4 deletions node/ChainwebNode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ import System.LogLevel

import Chainweb.Chainweb
import Chainweb.Chainweb.CutResources
import Chainweb.Chainweb.PeerResources
import Chainweb.Counter
import Chainweb.Cut.CutHashes
import Chainweb.CutDB
import Chainweb.Graph
Expand Down Expand Up @@ -194,17 +194,18 @@ withNodeLogger logConfig f = runManaged $ do
$ mkTelemetryLogger @CutHashes mgr teleLogConfig
p2pInfoBackend <- managed
$ mkTelemetryLogger @P2pSessionInfo mgr teleLogConfig
managerBackend <- managed
$ mkTelemetryLogger @ConnectionManagerStats mgr teleLogConfig
rtsBackend <- managed
$ mkTelemetryLogger @RTSStats mgr teleLogConfig
counterBackend <- managed $ configureHandler
(withJsonHandleBackend @CounterLog "connectioncounters" mgr)
teleLogConfig

logger <- managed
$ L.withLogger (_logConfigLogger logConfig) $ logHandles
[ logHandler monitorBackend
, logHandler p2pInfoBackend
, logHandler managerBackend
, logHandler rtsBackend
, logHandler counterBackend
] baseBackend

liftIO $ f logger
Expand Down
2 changes: 1 addition & 1 deletion src/Chainweb/Chainweb.hs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ runChainweb cw = do
let cutDb = _cutResCutDb $ _chainwebCutResources cw
cutPeerDb = _peerResDb $ _cutResPeer $ _chainwebCutResources cw

-- Startup sequnce:
-- Startup sequence:
--
-- 1. Start serving Rest API
-- 2. Start Clients
Expand Down
8 changes: 5 additions & 3 deletions src/Chainweb/Chainweb/ChainResources.hs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ runChainSyncClient
-> IO ()
runChainSyncClient mgr chain = bracket create destroy go
where
syncLogger = setComponent "sync" $ _chainResLogger chain
syncLogger = setComponent "header-sync" $ _chainResLogger chain
netId = ChainNetwork (_chainId chain)
syncLogg = logFunctionText syncLogger
create = p2pCreateNode
Expand Down Expand Up @@ -193,7 +193,7 @@ runMempoolSyncClient mgr chain = bracket create destroy go
where
create = do
logg Debug "starting mempool p2p sync"
p2pCreateNode v netId peer (logFunction $ _chainResLogger chain) peerDb mgr $
p2pCreateNode v netId peer (logFunction syncLogger) peerDb mgr $
mempoolSyncP2pSession chain
go n = do
-- Run P2P client node
Expand All @@ -207,7 +207,9 @@ runMempoolSyncClient mgr chain = bracket create destroy go
p2pConfig = _peerResConfig $ _chainResPeer chain
peerDb = _peerResDb $ _chainResPeer chain
netId = ChainNetwork $ _chainId chain
logg = logFunctionText (_chainResLogger chain)

logg = logFunctionText syncLogger
syncLogger = setComponent "mempool-sync" $ _chainResLogger chain

mempoolSyncP2pSession :: ChainResources logger -> P2pSession
mempoolSyncP2pSession chain logg0 env = go
Expand Down
45 changes: 16 additions & 29 deletions src/Chainweb/Chainweb/PeerResources.hs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}

-- |
Expand Down Expand Up @@ -35,25 +30,20 @@ module Chainweb.Chainweb.PeerResources
, withSocket
, withPeerDb
, withConnectionManger
, ConnectionManagerStats(..)
) where

import Configuration.Utils hiding (Lens', (<.>), Error)

import Control.Concurrent
import Control.Concurrent.Async
import Control.DeepSeq
import Control.Lens hiding ((.=), (<.>))
import Control.Monad
import Control.Monad.Catch

import qualified Data.ByteString.Char8 as B8
import qualified Data.HashSet as HS
import Data.IORef
import Data.IxSet.Typed (getEQ, getOne)

import GHC.Generics

import qualified Network.HTTP.Client as HTTP
import Network.Socket (Socket, close)
import Network.Wai.Handler.Warp (Settings, defaultSettings, setHost, setPort)
Expand All @@ -64,6 +54,7 @@ import System.LogLevel

-- internal modules

import Chainweb.Counter
import Chainweb.Graph
import Chainweb.HostAddress
import Chainweb.Logger
Expand Down Expand Up @@ -165,12 +156,6 @@ withPeerDb_ v conf = bracket (startPeerDb_ v conf) (stopPeerDb conf)
-- -------------------------------------------------------------------------- --
-- Connection Manager

data ConnectionManagerStats = ConnectionManagerStats
{ _connectionManagerConnectionCount :: !Int
, _connectionManagerRequestCount :: !Int
}
deriving (Show, Eq, Ord, Generic, ToJSON, FromJSON, NFData)

-- Connection Manager
--
withConnectionManger
Expand All @@ -196,18 +181,17 @@ withConnectionManger logger cert key peerDb runInner = do
-- total number of connections to keep alive. 512 is the default
}


connCountRef <- newIORef (0 :: Int)
reqCountRef <- newIORef (0 :: Int)
connCountRef <- newCounter @"connection-count"
reqCountRef <- newCounter @"request-count"
urlStats <- newCounterMap @"url-counts"
mgr <- HTTP.newManager settings'
{ HTTP.managerTlsConnection = do
mk <- HTTP.managerTlsConnection settings'
return $ \a b c -> do
atomicModifyIORef' connCountRef $ (,()) . succ
mk a b c
return $ \a b c -> inc connCountRef >> mk a b c

, HTTP.managerModifyRequest = \req -> do
atomicModifyIORef' reqCountRef $ (,()) . succ
inc reqCountRef
incKey urlStats (sshow $ HTTP.getUri req)
HTTP.managerModifyRequest settings req
{ HTTP.responseTimeout = HTTP.responseTimeoutMicro 1000000
-- overwrite the explicit connection timeout from servant-client
Expand All @@ -218,10 +202,11 @@ withConnectionManger logger cert key peerDb runInner = do

let logClientConnections = forever $ do
threadDelay 60000000 {- 1 minute -}
stats <- ConnectionManagerStats
<$> readIORef connCountRef
<*> readIORef reqCountRef
logFunctionJson logger Info stats
logFunctionCounter logger Info =<< sequence
[ roll connCountRef
, roll reqCountRef
, roll urlStats
]

let runLogClientConnections umask = do
umask logClientConnections `catchAllSynchronous` \e -> do
Expand All @@ -238,5 +223,7 @@ withConnectionManger logger cert key peerDb runInner = do
pe <- getOne . getEQ ha <$> peerDbSnapshot peerDb
return $ pe >>= fmap peerIdToFingerprint . _peerId . _peerEntryInfo

serviceIdToHostAddress (h, p) = readHostAddressBytes $ B8.pack h <> ":" <> p
serviceIdToHostAddress (h, p) = HostAddress
<$> readHostnameBytes (B8.pack h)
<*> readPortBytes p

Loading

0 comments on commit 52c7aea

Please sign in to comment.