Skip to content

Commit 2635277

Browse files
committed
Release LedgerDB handles in the LedgerDB
1 parent 6df1871 commit 2635277

File tree

5 files changed

+22
-18
lines changed

5 files changed

+22
-18
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,9 @@ newLSMLedgerTablesHandle tracer (origResKey, t) = do
194194
{-# INLINE implPushDiffs #-}
195195
{-# INLINE implTakeHandleSnapshot #-}
196196

197-
implClose :: (HasCallStack, IOLike m) => StrictTVar m (ResourceKey m) -> Bool -> m ()
198-
implClose tv False =
197+
implClose :: (HasCallStack, IOLike m) => StrictTVar m (ResourceKey m) -> m ()
198+
implClose tv =
199199
Monad.void $ release =<< readTVarIO tv
200-
implClose tv True = do
201-
Monad.void $ unsafeRelease =<< readTVarIO tv
202200

203201
implDuplicate ::
204202
( IOLike m

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ mkInitDb args getBlock snapManager getVolatileSuffix res = do
100100
lock <- RAWLock.new ()
101101
forkers <- newTVarIO Map.empty
102102
nextForkerKey <- newTVarIO (ForkerKey 0)
103+
ldbToClose <- newTVarIO []
103104
let env =
104105
LedgerDBEnv
105106
{ ldbSeq = varDB
@@ -116,6 +117,7 @@ mkInitDb args getBlock snapManager getVolatileSuffix res = do
116117
, ldbOpenHandlesLock = lock
117118
, ldbGetVolatileSuffix = getVolatileSuffix
118119
, ldbResourceKeys = SomeResources res
120+
, ldbToClose
119121
}
120122
h <- LDBHandle <$> newTVarIO (LedgerDBOpen env)
121123
pure $ implMkLedgerDb h snapManager
@@ -331,6 +333,7 @@ implGarbageCollect env slotNo = do
331333
atomically $
332334
modifyTVar (ldbPrevApplied env) $
333335
Set.dropWhileAntitone ((< slotNo) . realPointSlot)
336+
mapM_ closeLedgerSeq =<< readTVarIO (ldbToClose env)
334337
-- It is safe to close the handles outside of the locked region, which reduces
335338
-- contention. See the docs of 'ldbOpenHandlesLock'.
336339
Monad.join $ RAWLock.withWriteAccess (ldbOpenHandlesLock env) $ \() -> do
@@ -430,6 +433,7 @@ data LedgerDBEnv m l blk = LedgerDBEnv
430433
, ldbResolveBlock :: !(ResolveBlock m blk)
431434
, ldbQueryBatchSize :: !QueryBatchSize
432435
, ldbRegistry :: !(ResourceRegistry m)
436+
, ldbToClose :: !(StrictTVar m [LedgerSeq m l])
433437
, ldbOpenHandlesLock :: !(RAWLock m ())
434438
-- ^ While holding a read lock (at least), all handles in the 'ldbSeq' are
435439
-- guaranteed to be open. During this time, the handle can be duplicated and
@@ -758,6 +762,7 @@ newForker h ldbEnv rr (rk, st) = do
758762
, foeInitialHandleKey = rk
759763
, foeCleanup
760764
, foeLedgerDbLock = ldbOpenHandlesLock ldbEnv
765+
, foeLedgerDbToClose = ldbToClose ldbEnv
761766
}
762767
atomically $ modifyTVar (ldbForkers ldbEnv) $ Map.insert forkerKey forkerEnv
763768
pure $

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import Ouroboros.Consensus.Storage.LedgerDB.API
3333
import Ouroboros.Consensus.Storage.LedgerDB.Args
3434
import Ouroboros.Consensus.Storage.LedgerDB.Forker
3535
import Ouroboros.Consensus.Storage.LedgerDB.V2.LedgerSeq
36+
import Ouroboros.Consensus.Util (whenJust)
3637
import Ouroboros.Consensus.Util.CallStack
3738
import Ouroboros.Consensus.Util.IOLike
3839
import Ouroboros.Consensus.Util.NormalForm.StrictTVar ()
@@ -51,6 +52,7 @@ data ForkerEnv m l blk = ForkerEnv
5152
, foeLedgerDbRegistry :: !(ResourceRegistry m)
5253
-- ^ The registry in the LedgerDB to move handles to in case we commit the
5354
-- forker.
55+
, foeLedgerDbToClose :: !(StrictTVar m [LedgerSeq m l])
5456
, foeTracer :: !(Tracer m TraceForkerEvent)
5557
-- ^ Config
5658
, foeResourceRegistry :: !(ResourceRegistry m)
@@ -154,7 +156,7 @@ implForkerCommit env = do
154156
LedgerSeq lseq <- readTVar foeLedgerSeq
155157
let intersectionSlot = getTipSlot $ state $ AS.anchor lseq
156158
let predicate = (== getTipHash (state (AS.anchor lseq))) . getTipHash . state
157-
transfer <-
159+
(transfer, ldbToClose) <-
158160
stateTVar
159161
foeSwitchVar
160162
( \(LedgerSeq olddb) -> fromMaybe theImpossible $ do
@@ -165,20 +167,21 @@ implForkerCommit env = do
165167
AS.splitAfterMeasure intersectionSlot (either predicate predicate) lseq
166168
-- Join the prefix of the selection with the sequence in the forker
167169
newdb <- AS.join (const $ const True) toKeepBase toKeepTip
168-
let transferCommitted = do
169-
-- Do /not/ close the anchor of @toClose@, as that is also the
170-
-- tip of @olddb'@ which will be used in @newdb@.
171-
case toCloseLdb of
172-
AS.Empty _ -> pure ()
173-
_ AS.:< closeOld' -> closeLedgerSeq (LedgerSeq closeOld')
170+
-- Do /not/ close the anchor of @toClose@, as that is also the
171+
-- tip of @olddb'@ which will be used in @newdb@.
172+
let ldbToClose = case toCloseLdb of
173+
AS.Empty _ -> Nothing
174+
_ AS.:< closeOld' -> Just (LedgerSeq closeOld')
175+
transferCommitted = do
174176
closeLedgerSeq (LedgerSeq toCloseForker)
175177

176178
-- All the other remaining handles are transferred to the LedgerDB registry
177179
keys <- ingestRegistry foeResourceRegistry foeLedgerDbRegistry
178180
mapM_ (\(k, v) -> transfer (tables v) k) $ zip keys (AS.toOldestFirst toKeepTip)
179181

180-
pure (transferCommitted, LedgerSeq newdb)
182+
pure ((transferCommitted, ldbToClose), LedgerSeq newdb)
181183
)
184+
whenJust ldbToClose (modifyTVar foeLedgerDbToClose . (:))
182185
writeTVar foeCleanup transfer
183186
where
184187
ForkerEnv
@@ -187,6 +190,7 @@ implForkerCommit env = do
187190
, foeResourceRegistry
188191
, foeLedgerDbRegistry
189192
, foeCleanup
193+
, foeLedgerDbToClose
190194
} = env
191195

192196
theImpossible =

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,8 @@ implClose ::
132132
IOLike m =>
133133
Tracer m LedgerDBV2Trace ->
134134
StrictTVar m (LedgerTablesHandleState l) ->
135-
Bool ->
136135
m ()
137-
implClose tracer tv _ = do
136+
implClose tracer tv = do
138137
p <- atomically $ swapTVar tv LedgerTablesHandleClosed
139138
case p of
140139
LedgerTablesHandleOpen{} -> traceWith tracer TraceLedgerTablesHandleClose

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ import Prelude hiding (read)
8282

8383
-- | The interface fulfilled by handles on both the InMemory and LSM handles.
8484
data LedgerTablesHandle m l = LedgerTablesHandle
85-
{ close :: !(Bool -> m ())
85+
{ close :: !(m ())
8686
-- ^ Boolean is whether to force release or not.
8787
, transfer :: !(ResourceKey m -> m ())
8888
-- ^ Update the closing action in this handle with a new resource key, as the
@@ -223,9 +223,7 @@ empty' st = empty (forgetLedgerTables st) st
223223
-- the anchor.
224224
closeLedgerSeq :: Monad m => LedgerSeq m l -> m ()
225225
closeLedgerSeq (LedgerSeq l) =
226-
mapM_
227-
(\t -> close (tables t) True)
228-
$ AS.anchor l : AS.toOldestFirst l
226+
mapM_ (close . tables) $ AS.anchor l : AS.toOldestFirst l
229227

230228
{-------------------------------------------------------------------------------
231229
Apply blocks

0 commit comments

Comments
 (0)