17
17
module Ouroboros.Consensus.Storage.LedgerDB.V2 (mkInitDb ) where
18
18
19
19
import Control.Arrow ((>>>) )
20
- import qualified Control.Monad as Monad (void , (>=>) )
20
+ import qualified Control.Monad as Monad (join , void )
21
21
import Control.Monad.Except
22
22
import Control.RAWLock
23
23
import qualified Control.RAWLock as RAWLock
24
24
import Control.ResourceRegistry
25
25
import Control.Tracer
26
- import Data.Foldable (traverse_ )
27
26
import qualified Data.Foldable as Foldable
28
27
import Data.Functor.Contravariant ((>$<) )
29
28
import Data.Kind (Type )
@@ -573,13 +572,14 @@ getEnvSTM (LDBHandle varState) f =
573
572
getStateRef ::
574
573
(IOLike m , Traversable t ) =>
575
574
LedgerDBEnv m l blk ->
575
+ ResourceRegistry m ->
576
576
(LedgerSeq m l -> t (StateRef m l )) ->
577
577
m (t (StateRef m l ))
578
- getStateRef ldbEnv project =
578
+ getStateRef ldbEnv reg project =
579
579
RAWLock. withReadAccess (ldbOpenHandlesLock ldbEnv) $ \ () -> do
580
580
tst <- project <$> readTVarIO (ldbSeq ldbEnv)
581
581
for tst $ \ st -> do
582
- tables' <- duplicate $ tables st
582
+ (_, tables') <- allocate reg ( \ _ -> duplicate $ tables st) close
583
583
pure st{tables = tables'}
584
584
585
585
-- | Like 'StateRef', but takes care of closing the handle when the given action
@@ -590,8 +590,8 @@ withStateRef ::
590
590
(LedgerSeq m l -> t (StateRef m l )) ->
591
591
(t (StateRef m l ) -> m a ) ->
592
592
m a
593
- withStateRef ldbEnv project =
594
- bracket ( getStateRef ldbEnv project) (traverse_ (close . tables))
593
+ withStateRef ldbEnv project f =
594
+ withRegistry $ \ reg -> getStateRef ldbEnv reg project >>= f
595
595
596
596
acquireAtTarget ::
597
597
( HeaderHash l ~ HeaderHash blk
@@ -602,9 +602,10 @@ acquireAtTarget ::
602
602
) =>
603
603
LedgerDBEnv m l blk ->
604
604
Either Word64 (Target (Point blk )) ->
605
+ ResourceRegistry m ->
605
606
m (Either GetForkerError (StateRef m l ))
606
- acquireAtTarget ldbEnv target =
607
- getStateRef ldbEnv $ \ l -> case target of
607
+ acquireAtTarget ldbEnv target reg =
608
+ getStateRef ldbEnv reg $ \ l -> case target of
608
609
Right VolatileTip -> pure $ currentHandle l
609
610
Right ImmutableTip -> pure $ anchorHandle l
610
611
Right (SpecificPoint pt) -> do
@@ -638,7 +639,7 @@ newForkerAtTarget ::
638
639
Target (Point blk ) ->
639
640
m (Either GetForkerError (Forker m l blk ))
640
641
newForkerAtTarget h rr pt = getEnv h $ \ ldbEnv ->
641
- acquireAtTarget ldbEnv (Right pt) >>= traverse (newForker h ldbEnv rr)
642
+ acquireAtTarget ldbEnv (Right pt) rr >>= traverse (newForker h ldbEnv rr)
642
643
643
644
newForkerByRollback ::
644
645
( HeaderHash l ~ HeaderHash blk
@@ -653,14 +654,14 @@ newForkerByRollback ::
653
654
Word64 ->
654
655
m (Either GetForkerError (Forker m l blk ))
655
656
newForkerByRollback h rr n = getEnv h $ \ ldbEnv ->
656
- acquireAtTarget ldbEnv (Left n) >>= traverse (newForker h ldbEnv rr)
657
+ acquireAtTarget ldbEnv (Left n) rr >>= traverse (newForker h ldbEnv rr)
657
658
658
659
closeForkerEnv ::
659
660
IOLike m => ForkerEnv m l blk -> m ()
660
661
closeForkerEnv ForkerEnv {foeResourcesToRelease = (lock, key, toRelease)} =
661
662
RAWLock. withWriteAccess lock $
662
663
const $ do
663
- id =<< atomically (swapTVar toRelease (pure () ))
664
+ Monad. join $ atomically (swapTVar toRelease (pure () ))
664
665
_ <- release key
665
666
pure (() , () )
666
667
@@ -757,7 +758,12 @@ newForker h ldbEnv rr st = do
757
758
let tr = LedgerDBForkerEvent . TraceForkerEventWithKey forkerKey >$< ldbTracer ldbEnv
758
759
traceWith tr ForkerOpen
759
760
lseqVar <- newTVarIO . LedgerSeq . AS. Empty $ st
760
- (k, toRelease) <- allocate rr (\ _ -> newTVarIO (pure () )) (readTVarIO Monad. >=> id )
761
+ -- The closing action that we allocate in the TVar from the start is not
762
+ -- strictly necessary if the caller uses a short-lived registry like the ones
763
+ -- in Chain selection or the forging loop. Just in case the user passes a
764
+ -- long-lived registry, we store such closing action to make sure the handle
765
+ -- is closed even under @forkerClose@ if the registry outlives the forker.
766
+ (k, toRelease) <- allocate rr (\ _ -> newTVarIO (close (tables st))) (Monad. join . readTVarIO)
761
767
let forkerEnv =
762
768
ForkerEnv
763
769
{ foeLedgerSeq = lseqVar
0 commit comments