Skip to content

Commit 169406b

Browse files
committed
More LSM tracing
1 parent 182e20a commit 169406b

File tree

4 files changed

+113
-98
lines changed
  • ouroboros-consensus
    • src
      • ouroboros-consensus-lsm/Ouroboros/Consensus/Storage/LedgerDB/V2
      • ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB/V2
    • test/storage-test/Test/Ouroboros/Storage/LedgerDB

4 files changed

+113
-98
lines changed

ouroboros-consensus/src/ouroboros-consensus-lsm/Ouroboros/Consensus/Storage/LedgerDB/V2/LSM.hs

Lines changed: 87 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module Ouroboros.Consensus.Storage.LedgerDB.V2.LSM
2525
LSM
2626
, Backend (..)
2727
, Args (LSMArgs)
28-
, Trace (LSMTreeTrace)
28+
, Trace (..)
2929
, LSM.LSMTreeTrace (..)
3030
, mkLSMArgs
3131
, stdMkBlockIOFS
@@ -172,21 +172,21 @@ newLSMLedgerTablesHandle ::
172172
Tracer m LedgerDBV2Trace ->
173173
(ResourceKey m, UTxOTable m) ->
174174
m (LedgerTablesHandle m l)
175-
newLSMLedgerTablesHandle tracer (origResKey, t) = do
176-
traceWith tracer TraceLedgerTablesHandleCreate
177-
tv <- newTVarIO origResKey
178-
pure
179-
LedgerTablesHandle
180-
{ close = implClose tv
181-
, duplicate = \rr -> implDuplicate rr t tracer
182-
, read = implRead t
183-
, readRange = implReadRange t
184-
, readAll = implReadAll t
185-
, pushDiffs = implPushDiffs t
186-
, takeHandleSnapshot = implTakeHandleSnapshot t
187-
, tablesSize = pure Nothing
188-
, transfer = atomically . writeTVar tv
189-
}
175+
newLSMLedgerTablesHandle tracer (origResKey, t) =
176+
encloseTimedWith (TraceLedgerTablesHandleCreate >$< tracer) $ do
177+
tv <- newTVarIO origResKey
178+
pure
179+
LedgerTablesHandle
180+
{ close = implClose tv
181+
, duplicate = \rr -> implDuplicate rr t tracer
182+
, read = implRead tracer t
183+
, readRange = implReadRange t
184+
, readAll = implReadAll t
185+
, pushDiffs = implPushDiffs tracer t
186+
, takeHandleSnapshot = implTakeHandleSnapshot tracer t
187+
, tablesSize = pure Nothing
188+
, transfer = atomically . writeTVar tv
189+
}
190190

191191
{-# INLINE implClose #-}
192192
{-# INLINE implDuplicate #-}
@@ -213,11 +213,8 @@ implDuplicate rr t tracer = do
213213
(rk, table) <-
214214
allocate
215215
rr
216-
(\_ -> LSM.duplicate t)
217-
( \t' -> do
218-
traceWith tracer TraceLedgerTablesHandleClose
219-
LSM.closeTable t'
220-
)
216+
(\_ -> encloseTimedWith (TraceLedgerTablesHandleDuplicate >$< tracer) $ LSM.duplicate t)
217+
(encloseTimedWith (TraceLedgerTablesHandleClose >$< tracer) . LSM.closeTable)
221218
(rk,) <$> newLSMLedgerTablesHandle tracer (rk, table)
222219

223220
implRead ::
@@ -226,28 +223,34 @@ implRead ::
226223
, HasLedgerTables l
227224
, IndexedMemPack (l EmptyMK) (TxOut l)
228225
) =>
229-
UTxOTable m -> l EmptyMK -> LedgerTables l KeysMK -> m (LedgerTables l ValuesMK)
230-
implRead t st (LedgerTables (KeysMK keys)) = do
231-
let vec' = V.create $ do
232-
vec <- VM.new (Set.size keys)
233-
Monad.foldM_
234-
(\i x -> VM.write vec i (toTxInBytes (Proxy @l) x) >> pure (i + 1))
235-
0
236-
keys
237-
pure vec
238-
res <- LSM.lookups t vec'
239-
pure
240-
. LedgerTables
241-
. ValuesMK
242-
. Foldable.foldl'
243-
( \m (k, item) ->
244-
case item of
245-
LSM.Found v -> Map.insert (fromTxInBytes (Proxy @l) k) (fromTxOutBytes st v) m
246-
LSM.NotFound -> m
247-
LSM.FoundWithBlob{} -> m
248-
)
249-
Map.empty
250-
$ V.zip vec' res
226+
Tracer m LedgerDBV2Trace ->
227+
UTxOTable m ->
228+
l EmptyMK ->
229+
LedgerTables l KeysMK ->
230+
m (LedgerTables l ValuesMK)
231+
implRead tracer t st (LedgerTables (KeysMK keys)) =
232+
encloseTimedWith (TraceLedgerTablesHandleRead >$< tracer) $ do
233+
let vec' = V.create $ do
234+
vec <- VM.new (Set.size keys)
235+
Monad.foldM_
236+
(\i x -> VM.write vec i (toTxInBytes (Proxy @l) x) >> pure (i + 1))
237+
0
238+
keys
239+
pure vec
240+
res <-
241+
encloseTimedWith (BackendTrace . SomeBackendTrace . LSMLookup >$< tracer) $ LSM.lookups t vec'
242+
pure
243+
. LedgerTables
244+
. ValuesMK
245+
. Foldable.foldl'
246+
( \m (k, item) ->
247+
case item of
248+
LSM.Found v -> Map.insert (fromTxInBytes (Proxy @l) k) (fromTxOutBytes st v) m
249+
LSM.NotFound -> m
250+
LSM.FoundWithBlob{} -> m
251+
)
252+
Map.empty
253+
$ V.zip vec' res
251254

252255
implReadRange ::
253256
forall m l.
@@ -300,27 +303,30 @@ implPushDiffs ::
300303
, HasLedgerTables l
301304
, IndexedMemPack (l EmptyMK) (TxOut l)
302305
) =>
303-
UTxOTable m -> l mk -> l DiffMK -> m ()
304-
implPushDiffs t _ !st1 = do
305-
let LedgerTables (DiffMK (Diff.Diff diffs)) = projectLedgerTables st1
306-
let vec = V.create $ do
307-
vec' <- VM.new (Map.size diffs)
308-
Monad.foldM_
309-
(\idx (k, item) -> VM.write vec' idx (toTxInBytes (Proxy @l) k, (f item)) >> pure (idx + 1))
310-
0
311-
$ Map.toList diffs
312-
pure vec'
313-
LSM.updates t vec
306+
Tracer m LedgerDBV2Trace -> UTxOTable m -> l mk -> l DiffMK -> m ()
307+
implPushDiffs tracer t _ !st1 =
308+
encloseTimedWith (TraceLedgerTablesHandleRead >$< tracer) $ do
309+
let LedgerTables (DiffMK (Diff.Diff diffs)) = projectLedgerTables st1
310+
let vec = V.create $ do
311+
vec' <- VM.new (Map.size diffs)
312+
Monad.foldM_
313+
(\idx (k, item) -> VM.write vec' idx (toTxInBytes (Proxy @l) k, (f item)) >> pure (idx + 1))
314+
0
315+
$ Map.toList diffs
316+
pure vec'
317+
encloseTimedWith (BackendTrace . SomeBackendTrace . LSMUpdate >$< tracer) $ LSM.updates t vec
314318
where
315319
f (Diff.Insert v) = LSM.Insert (toTxOutBytes (forgetLedgerTables st1) v) Nothing
316320
f Diff.Delete = LSM.Delete
317321

318-
implTakeHandleSnapshot :: IOLike m => UTxOTable m -> t -> String -> m (Maybe a)
319-
implTakeHandleSnapshot t _ snapshotName = do
320-
LSM.saveSnapshot
321-
(fromString snapshotName)
322-
(LSM.SnapshotLabel $ Text.pack $ "UTxO table")
323-
t
322+
implTakeHandleSnapshot ::
323+
IOLike m => Tracer m LedgerDBV2Trace -> UTxOTable m -> t -> String -> m (Maybe a)
324+
implTakeHandleSnapshot tracer t _ snapshotName = do
325+
encloseTimedWith (BackendTrace . SomeBackendTrace . LSMSnap >$< tracer) $
326+
LSM.saveSnapshot
327+
(fromString snapshotName)
328+
(LSM.SnapshotLabel $ Text.pack $ "UTxO table")
329+
t
324330
pure Nothing
325331

326332
{-------------------------------------------------------------------------------
@@ -473,15 +479,13 @@ loadSnapshot tracer rr ccfg fs@(SomeHasFS hfs) session ds =
473479
allocate
474480
rr
475481
( \_ ->
476-
LSM.openTableFromSnapshot
477-
session
478-
(fromString $ snapshotToDirName ds)
479-
(LSM.SnapshotLabel $ Text.pack $ "UTxO table")
480-
)
481-
( \t -> do
482-
traceWith tracer TraceLedgerTablesHandleClose
483-
LSM.closeTable t
482+
encloseTimedWith (TraceLedgerTablesHandleCreateFirst >$< tracer) $
483+
LSM.openTableFromSnapshot
484+
session
485+
(fromString $ snapshotToDirName ds)
486+
(LSM.SnapshotLabel $ Text.pack $ "UTxO table")
484487
)
488+
(encloseTimedWith (TraceLedgerTablesHandleClose >$< tracer) . LSM.closeTable)
485489
Monad.when
486490
(checksumAsRead /= snapshotChecksum snapshotMeta)
487491
$ throwE
@@ -505,11 +509,11 @@ tableFromValuesMK tracer rr session st (LedgerTables (ValuesMK values)) = do
505509
(rk, table) <-
506510
allocate
507511
rr
508-
(\_ -> LSM.newTable session)
509-
( \tb -> do
510-
traceWith tracer TraceLedgerTablesHandleClose
511-
LSM.closeTable tb
512+
( \_ ->
513+
encloseTimedWith (TraceLedgerTablesHandleCreateFirst >$< tracer) $
514+
LSM.newTable session
512515
)
516+
(encloseTimedWith (TraceLedgerTablesHandleClose >$< tracer) . LSM.closeTable)
513517
mapM_ (go table) $ chunks 1000 $ Map.toList values
514518
pure (rk, table)
515519
where
@@ -577,6 +581,10 @@ instance
577581

578582
data Trace LSM
579583
= LSMTreeTrace !LSM.LSMTreeTrace
584+
| LSMLookup EnclosingTimed
585+
| LSMUpdate EnclosingTimed
586+
| LSMSnap EnclosingTimed
587+
| LSMOpenSession EnclosingTimed
580588
deriving Show
581589

582590
mkResources _ trcr (LSMArgs path salt mkFS) reg _ = do
@@ -586,12 +594,13 @@ instance
586594
allocate
587595
reg
588596
( \_ ->
589-
LSM.openSession
590-
(BackendTrace . SomeBackendTrace . LSMTreeTrace >$< trcr)
591-
fs
592-
blockio
593-
salt
594-
path
597+
encloseTimedWith (BackendTrace . SomeBackendTrace . LSMOpenSession >$< trcr) $
598+
LSM.openSession
599+
(BackendTrace . SomeBackendTrace . LSMTreeTrace >$< trcr)
600+
fs
601+
blockio
602+
salt
603+
path
595604
)
596605
LSM.closeSession
597606
pure (LSMResources (fst session) (snd session) rk1)

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB/V2/Backend.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import Ouroboros.Consensus.Ledger.Abstract
3030
import Ouroboros.Consensus.Ledger.Extended
3131
import Ouroboros.Consensus.Storage.LedgerDB.Snapshots
3232
import Ouroboros.Consensus.Storage.LedgerDB.V2.LedgerSeq
33+
import Ouroboros.Consensus.Util.Enclose (EnclosingTimed)
3334
import System.FS.API
3435

3536
-- | Operations needed to open and operate a LedgerDB V2
@@ -114,9 +115,13 @@ instance NoThunks (SomeResources m blk) where
114115
data LedgerDBV2Trace
115116
= -- | Created a new 'LedgerTablesHandle', potentially by duplicating an
116117
-- existing one.
117-
TraceLedgerTablesHandleCreate
118+
TraceLedgerTablesHandleCreate EnclosingTimed
118119
| -- | Closed a 'LedgerTablesHandle'.
119-
TraceLedgerTablesHandleClose
120+
TraceLedgerTablesHandleClose EnclosingTimed
121+
| TraceLedgerTablesHandleRead EnclosingTimed
122+
| TraceLedgerTablesHandleDuplicate EnclosingTimed
123+
| TraceLedgerTablesHandleCreateFirst EnclosingTimed
124+
| TraceLedgerTablesHandlePush EnclosingTimed
120125
| BackendTrace SomeBackendTrace
121126

122127
deriving instance Show SomeBackendTrace => Show LedgerDBV2Trace

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB/V2/InMemory.hs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,22 @@ newInMemoryLedgerTablesHandle ::
104104
SomeHasFS m ->
105105
LedgerTables l ValuesMK ->
106106
m (LedgerTablesHandle m l)
107-
newInMemoryLedgerTablesHandle tracer someFS@(SomeHasFS hasFS) l = do
108-
!tv <- newTVarIO (LedgerTablesHandleOpen l)
109-
traceWith tracer TraceLedgerTablesHandleCreate
110-
pure
111-
LedgerTablesHandle
112-
{ close = implClose tracer tv
113-
, duplicate = implDuplicate tracer tv someFS
114-
, read = implRead tv
115-
, readRange = implReadRange tv
116-
, readAll = implReadAll tv
117-
, pushDiffs = implPushDiffs tv
118-
, takeHandleSnapshot = implTakeHandleSnapshot tv hasFS
119-
, tablesSize = implTablesSize tv
120-
, transfer = const (pure ())
121-
}
107+
newInMemoryLedgerTablesHandle tracer someFS@(SomeHasFS hasFS) l =
108+
encloseTimedWith (TraceLedgerTablesHandleCreate >$< tracer) $ do
109+
!tv <- newTVarIO (LedgerTablesHandleOpen l)
110+
111+
pure
112+
LedgerTablesHandle
113+
{ close = implClose tracer tv
114+
, duplicate = implDuplicate tracer tv someFS
115+
, read = implRead tv
116+
, readRange = implReadRange tv
117+
, readAll = implReadAll tv
118+
, pushDiffs = implPushDiffs tv
119+
, takeHandleSnapshot = implTakeHandleSnapshot tv hasFS
120+
, tablesSize = implTablesSize tv
121+
, transfer = const (pure ())
122+
}
122123

123124
{-# INLINE implClose #-}
124125
{-# INLINE implDuplicate #-}
@@ -137,7 +138,7 @@ implClose ::
137138
implClose tracer tv = do
138139
p <- atomically $ swapTVar tv LedgerTablesHandleClosed
139140
case p of
140-
LedgerTablesHandleOpen{} -> traceWith tracer TraceLedgerTablesHandleClose
141+
LedgerTablesHandleOpen{} -> encloseTimedWith (TraceLedgerTablesHandleClose >$< tracer) $ pure ()
141142
_ -> pure ()
142143

143144
implDuplicate ::

ouroboros-consensus/test/storage-test/Test/Ouroboros/Storage/LedgerDB/StateMachine.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,8 +718,8 @@ mkTrackOpenHandles = do
718718
let tracer = Tracer $ \case
719719
LedgerDBFlavorImplEvent (FlavorImplSpecificTraceV2 ev) ->
720720
atomically $ modifyTVar varOpen $ case ev of
721-
V2.TraceLedgerTablesHandleCreate -> succ
722-
V2.TraceLedgerTablesHandleClose -> pred
721+
V2.TraceLedgerTablesHandleCreate{} -> succ
722+
V2.TraceLedgerTablesHandleClose{} -> pred
723723
_ -> id
724724
_ -> pure ()
725725
pure (tracer, readTVarIO varOpen)

0 commit comments

Comments
 (0)