Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ launch_*
gen/
cardano-chain-gen/test/testfiles/temp/
/secp256k1/
monitoring/data/

# Vim
*.swp
Expand All @@ -31,4 +32,4 @@ result*
/.vscode

# MacOS
.DS_Store
.DS_Store
3 changes: 3 additions & 0 deletions cardano-chain-gen/test/Test/Cardano/Db/Mock/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,9 @@ emptyMetricsSetters =
, metricsSetDbSlotHeight = \_ -> pure ()
, metricsSetDbEpochSyncDuration = \_ -> pure ()
, metricsSetDbEpochSyncNumber = \_ -> pure ()
, metricsSetDbBlocksPerSecond = \_ -> pure ()
, metricsSetInsertDuration = \_ -> pure ()
, metricsSetCacheHitRate = \_ _ -> pure ()
}

withFullConfig ::
Expand Down
15 changes: 15 additions & 0 deletions cardano-db-sync/src/Cardano/DbSync/Database.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Cardano.Prelude hiding (atomically)
import Cardano.Slotting.Slot (SlotNo (..), WithOrigin (..))
import Control.Concurrent.Class.MonadSTM.Strict
import Control.Monad.Extra (whenJust)
import Data.Time.Clock (diffUTCTime, getCurrentTime)
import Ouroboros.Network.Block (BlockNo (..), Point (..))
import Ouroboros.Network.Point (blockPointHash, blockPointSlot)

Expand Down Expand Up @@ -126,7 +127,21 @@ runActions syncEnv actions = do
lift $ atomically $ putTMVar resultVar (points, blockNo)
dbEvent Continue ys
(ys, zs) -> do
-- Record start time and block count for performance metrics
startTime <- liftIO getCurrentTime
let blockCount = length ys

-- Process blocks
ExceptT $ insertListBlocks syncEnv ys

-- Calculate and record blocks per second
when (blockCount > 0) $ do
endTime <- liftIO getCurrentTime
let duration = realToFrac $ diffUTCTime endTime startTime
when (duration > 0) $ do
let blocksPerSecond = fromIntegral blockCount / duration
liftIO $ setDbBlocksPerSecond (envMetricSetters syncEnv) blocksPerSecond

if null zs
then pure Continue
else dbEvent Continue zs
Expand Down
10 changes: 10 additions & 0 deletions cardano-db-sync/src/Cardano/DbSync/Default.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import qualified Data.ByteString.Short as SBS
import Data.List (span)
import qualified Data.Set as Set
import qualified Data.Strict.Maybe as Strict
import Data.Time.Clock (diffUTCTime, getCurrentTime)
import Ouroboros.Consensus.Cardano.Block (HardForkBlock (..))
import qualified Ouroboros.Consensus.HardFork.Combinator as Consensus
import Ouroboros.Network.Block (blockHash, blockNo, getHeaderFields, headerFieldBlockNo, unBlockNo)
Expand All @@ -42,6 +43,7 @@ import Cardano.DbSync.Error (SyncNodeError (..), mkSyncNodeCallStack)
import Cardano.DbSync.Ledger.State (applyBlockAndSnapshot, defaultApplyResult)
import Cardano.DbSync.Ledger.Types (ApplyResult (..))
import Cardano.DbSync.LocalStateQuery
import Cardano.DbSync.Metrics (setInsertDuration)
import Cardano.DbSync.Rollback
import Cardano.DbSync.Types
import Cardano.DbSync.Util
Expand Down Expand Up @@ -151,6 +153,9 @@ insertBlock ::
Bool ->
ExceptT SyncNodeError DB.DbM ()
insertBlock syncEnv cblk applyRes firstAfterRollback tookSnapshot = do
-- Start timing for insert duration metric
startTime <- liftIO getCurrentTime

!epochEvents <- liftIO $ atomically $ generateNewEpochEvents syncEnv (apSlotDetails applyRes)
let !applyResult = applyRes {apEvents = sort $ epochEvents <> apEvents applyRes}
let !details = apSlotDetails applyResult
Expand Down Expand Up @@ -206,6 +211,11 @@ insertBlock syncEnv cblk applyRes firstAfterRollback tookSnapshot = do
do
lift $ DB.deleteConsumedTxOut tracer txOutVariantType (getSafeBlockNoDiff syncEnv)
commitOrIndexes withinTwoMin withinHalfHour

-- Record insert duration metric
endTime <- liftIO getCurrentTime
let duration = realToFrac $ diffUTCTime endTime startTime
liftIO $ setInsertDuration (envMetricSetters syncEnv) duration
where
tracer = getTrace syncEnv
txOutVariantType = getTxOutVariantType syncEnv
Expand Down
2 changes: 2 additions & 0 deletions cardano-db-sync/src/Cardano/DbSync/Era/Cardano/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ insertEpochSyncTime ::
UTCTime ->
ExceptT SyncNodeError DB.DbM ()
insertEpochSyncTime epochNo syncState epochStats endTime = do
currentTime <- liftIO Time.getCurrentTime
void
. lift
$ DB.insertEpochSyncTime
$ DB.EpochSyncTime
{ DB.epochSyncTimeNo = unEpochNo epochNo - 1
, DB.epochSyncTimeSeconds = ceiling (realToFrac (Time.diffUTCTime endTime (elsStartTime epochStats)) :: Double)
, DB.epochSyncTimeState = syncState
, DB.epochSyncTimeSyncedAt = Just currentTime
}

initEpochStatistics :: MonadIO m => m (StrictTVar IO EpochStatistics)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Cardano.Slotting.Slot (EpochNo (..))

import Cardano.DbSync.Api
import Cardano.DbSync.Api.Types (EpochStatistics (..), InsertOptions (..), SyncEnv (..), UnicodeNullSource, formatUnicodeNullSource)
import Cardano.DbSync.Cache.Types (textShowCacheStats)
import Cardano.DbSync.Cache.Types (CacheStatistics (..), textShowCacheStats)
import Cardano.DbSync.Era.Cardano.Util (insertEpochSyncTime, resetEpochStatistics)
import qualified Cardano.DbSync.Era.Shelley.Generic as Generic
import Cardano.DbSync.Era.Universal.Adjust (adjustEpochRewards)
Expand All @@ -31,7 +31,7 @@ import Cardano.DbSync.Types

import Cardano.DbSync.Error (SyncNodeError)
import Cardano.DbSync.Ledger.Types
import Cardano.DbSync.Metrics (setDbEpochSyncDuration, setDbEpochSyncNumber)
import Cardano.DbSync.Metrics (setCacheHitRate, setDbEpochSyncDuration, setDbEpochSyncNumber)
import Control.Concurrent.Class.MonadSTM.Strict (readTVarIO, writeTVar)
import Control.Monad.Extra (whenJust)
import qualified Data.Map.Strict as Map
Expand Down Expand Up @@ -96,6 +96,17 @@ insertNewEpochLedgerEvents syncEnv applyRes currentEpochNo@(EpochNo curEpoch) =
liftIO $ setDbEpochSyncDuration metricSetters (epochDurationSeconds (elsStartTime epochStats) currentTime)
liftIO $ setDbEpochSyncNumber metricSetters (fromIntegral $ unEpochNo en - 1)

-- Calculate and set cache hit rates
let cacheStats = elsCaches epochStats
hitRate hits queries = if queries == 0 then 0.0 else fromIntegral hits / fromIntegral queries
liftIO $ setCacheHitRate metricSetters CacheStake (hitRate (credsHits cacheStats) (credsQueries cacheStats))
liftIO $ setCacheHitRate metricSetters CachePools (hitRate (poolsHits cacheStats) (poolsQueries cacheStats))
liftIO $ setCacheHitRate metricSetters CacheDatum (hitRate (datumHits cacheStats) (datumQueries cacheStats))
liftIO $ setCacheHitRate metricSetters CacheMultiAssets (hitRate (multiAssetsHits cacheStats) (multiAssetsQueries cacheStats))
liftIO $ setCacheHitRate metricSetters CachePrevBlock (hitRate (prevBlockHits cacheStats) (prevBlockQueries cacheStats))
liftIO $ setCacheHitRate metricSetters CacheAddress (hitRate (addressHits cacheStats) (addressQueries cacheStats))
liftIO $ setCacheHitRate metricSetters CacheTxIds (hitRate (txIdsHits cacheStats) (txIdsQueries cacheStats))

-- Log comprehensive epoch statistics
liftIO . logInfo tracer $
mconcat
Expand Down
56 changes: 54 additions & 2 deletions cardano-db-sync/src/Cardano/DbSync/Metrics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ module Cardano.DbSync.Metrics (
setDbSlotHeight,
setDbEpochSyncDuration,
setDbEpochSyncNumber,
setDbBlocksPerSecond,
setInsertDuration,
setCacheHitRate,
makeMetrics,
withMetricSetters,
withMetricsServer,
) where

import Cardano.DbSync.Types (MetricSetters (..))
import Cardano.DbSync.Types (MetricSetters (..), CacheType(..))
import Cardano.Prelude
import Cardano.Slotting.Slot (SlotNo (..), WithOrigin (..), fromWithOrigin)
import Ouroboros.Network.Block (BlockNo (..))
Expand All @@ -41,6 +44,24 @@ data Metrics = Metrics
-- ^ The duration of the last epoch sync in seconds.
, mDbEpochSyncNumber :: !Gauge
-- ^ The number of the last epoch that was synced.
, mDbBlocksPerSecond :: !Gauge
-- ^ The number of blocks being processes per second.
, mInsertDuration :: !Gauge
-- ^ The duration of the last insert operation in seconds.
, mCacheStakeHitRate :: !Gauge
-- ^ Cache hit rate for stake cache.
, mCachePoolsHitRate :: !Gauge
-- ^ Cache hit rate for pools cache.
, mCacheDatumHitRate :: !Gauge
-- ^ Cache hit rate for datum cache.
, mCacheMultiAssetsHitRate :: !Gauge
-- ^ Cache hit rate for multi_assets cache.
, mCachePrevBlockHitRate :: !Gauge
-- ^ Cache hit rate for prev_block cache.
, mCacheAddressHitRate :: !Gauge
-- ^ Cache hit rate for address cache.
, mCacheTxIdsHitRate :: !Gauge
-- ^ Cache hit rate for tx_ids cache.
}

-- This enables us to be much more flexibile with what we actually measure.
Expand All @@ -61,8 +82,21 @@ withMetricSetters prometheusPort action =
Gauge.set duration $ mDbEpochSyncDuration metrics
, metricsSetDbEpochSyncNumber = \epochNo ->
Gauge.set (fromIntegral epochNo) $ mDbEpochSyncNumber metrics
, metricsSetDbBlocksPerSecond = \bps ->
Gauge.set bps $ mDbBlocksPerSecond metrics
, metricsSetInsertDuration = \duration ->
Gauge.set duration $ mInsertDuration metrics
, metricsSetCacheHitRate = \cacheName hitRate ->
case cacheName of
CacheStake -> Gauge.set hitRate $ mCacheStakeHitRate metrics
CachePools -> Gauge.set hitRate $ mCachePoolsHitRate metrics
CacheDatum -> Gauge.set hitRate $ mCacheDatumHitRate metrics
CacheMultiAssets -> Gauge.set hitRate $ mCacheMultiAssetsHitRate metrics
CachePrevBlock -> Gauge.set hitRate $ mCachePrevBlockHitRate metrics
CacheAddress -> Gauge.set hitRate $ mCacheAddressHitRate metrics
CacheTxIds -> Gauge.set hitRate $ mCacheTxIdsHitRate metrics
}

withMetricsServer :: Int -> (Metrics -> IO a) -> IO a
withMetricsServer port action = do
-- Using both `RegistryT` and `bracket` here is overkill. Unfortunately the
Expand All @@ -83,6 +117,15 @@ makeMetrics =
<*> registerGauge "cardano_db_sync_db_slot_height" mempty
<*> registerGauge "cardano_db_sync_db_epoch_sync_duration_seconds" mempty
<*> registerGauge "cardano_db_sync_db_epoch_sync_number" mempty
<*> registerGauge "cardano_db_sync_blocks_per_second" mempty
<*> registerGauge "cardano_db_sync_insert_duration_seconds" mempty
<*> registerGauge "cardano_db_sync_cache_stake_hit_rate" mempty
<*> registerGauge "cardano_db_sync_cache_pools_hit_rate" mempty
<*> registerGauge "cardano_db_sync_cache_datum_hit_rate" mempty
<*> registerGauge "cardano_db_sync_cache_multi_assets_hit_rate" mempty
<*> registerGauge "cardano_db_sync_cache_prev_block_hit_rate" mempty
<*> registerGauge "cardano_db_sync_cache_address_hit_rate" mempty
<*> registerGauge "cardano_db_sync_cache_tx_ids_hit_rate" mempty

setNodeBlockHeight :: MetricSetters -> WithOrigin BlockNo -> IO ()
setNodeBlockHeight setters woBlkNo =
Expand All @@ -102,3 +145,12 @@ setDbEpochSyncDuration = metricsSetDbEpochSyncDuration

setDbEpochSyncNumber :: MetricSetters -> Word64 -> IO ()
setDbEpochSyncNumber = metricsSetDbEpochSyncNumber

setDbBlocksPerSecond :: MetricSetters -> Double -> IO ()
setDbBlocksPerSecond = metricsSetDbBlocksPerSecond

setInsertDuration :: MetricSetters -> Double -> IO ()
setInsertDuration = metricsSetInsertDuration

setCacheHitRate :: MetricSetters -> CacheType -> Double -> IO ()
setCacheHitRate = metricsSetCacheHitRate
14 changes: 14 additions & 0 deletions cardano-db-sync/src/Cardano/DbSync/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Cardano.DbSync.Types (
SyncState (..),
TPraosStandard,
MetricSetters (..),
CacheType (..),
OffChainPoolWorkQueue (..),
OffChainVoteWorkQueue (..),
SimplifiedOffChainPoolData (..),
Expand Down Expand Up @@ -139,11 +140,24 @@ data MetricSetters = MetricSetters
, metricsSetDbSlotHeight :: SlotNo -> IO ()
, metricsSetDbEpochSyncDuration :: Double -> IO ()
, metricsSetDbEpochSyncNumber :: Word64 -> IO ()
, metricsSetDbBlocksPerSecond :: Double -> IO ()
, metricsSetInsertDuration :: Double -> IO ()
, metricsSetCacheHitRate :: CacheType -> Double -> IO ()
}

data SyncState = SyncLagging | SyncFollowing
deriving (Eq, Show)

data CacheType
= CacheStake
| CachePools
| CacheDatum
| CacheMultiAssets
| CachePrevBlock
| CacheAddress
| CacheTxIds
deriving (Eq, Show)

-------------------------------------------------------------------------------------
-- OffChain
-------------------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions cardano-db/src/Cardano/Db/Schema/Core/EpochAndProtocol.hs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ data EpochSyncTime = EpochSyncTime
{ epochSyncTimeNo :: !Word64 -- sqltype=word31type
, epochSyncTimeSeconds :: !Word64 -- sqltype=word63type
, epochSyncTimeState :: !SyncState -- sqltype=syncstatetype
, epochSyncTimeSyncedAt :: !(Maybe UTCTime)
}
deriving (Show, Eq, Generic)

Expand All @@ -338,6 +339,7 @@ epochSyncTimeEncoder =
[ epochSyncTimeNo >$< E.param (E.nonNullable $ fromIntegral >$< E.int8)
, epochSyncTimeSeconds >$< E.param (E.nonNullable $ fromIntegral >$< E.int8)
, epochSyncTimeState >$< E.param (E.nonNullable syncStateEncoder)
, epochSyncTimeSyncedAt >$< E.param (E.nullable E.timestamptz)
]

-----------------------------------------------------------------------------------------------------------------------------------
Expand Down
110 changes: 0 additions & 110 deletions dev-tools/README.md

This file was deleted.

Loading
Loading