Skip to content

Commit 0759a78

Browse files
committed
Use ActionRegistry instead of TempRegistry everywhere
1 parent 840c26a commit 0759a78

File tree

6 files changed

+135
-136
lines changed

6 files changed

+135
-136
lines changed

src-control/Control/ActionRegistry.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ import Data.List.NonEmpty (NonEmpty (..))
3434
import qualified Data.List.NonEmpty as NE
3535
import Data.Primitive.MutVar
3636

37-
-- TODO: replace TempRegistry by ActionRegistry
38-
3937
-- TODO: add tests using fs-sim/io-sim to make sure exception safety is
4038
-- guaranteed.
4139

src/Database/LSMTree/Internal.hs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ import Control.Monad.Class.MonadST (MonadST (..))
8383
import Control.Monad.Class.MonadThrow
8484
import Control.Monad.Primitive
8585
import Control.RefCount
86-
import Control.TempRegistry
8786
import Control.Tracer
8887
import Data.Arena (ArenaManager, newArenaManager)
8988
import Data.Either (fromRight)
@@ -702,12 +701,12 @@ new ::
702701
new sesh conf = do
703702
traceWith (sessionTracer sesh) TraceNewTable
704703
withOpenSession sesh $ \seshEnv ->
705-
withTempRegistry $ \reg -> do
704+
withActionRegistry $ \reg -> do
706705
am <- newArenaManager
707706
blobpath <- Paths.tableBlobPath (sessionRoot seshEnv) <$>
708707
incrUniqCounter (sessionUniqCounter seshEnv)
709708
tableWriteBufferBlobs
710-
<- allocateTemp reg
709+
<- withRollback reg
711710
(WBB.new (sessionHasFS seshEnv) blobpath)
712711
releaseRef
713712
let tableWriteBuffer = WB.empty
@@ -722,16 +721,16 @@ new sesh conf = do
722721
newWith reg sesh seshEnv conf am tc
723722

724723
{-# SPECIALISE newWith ::
725-
TempRegistry IO
724+
ActionRegistry IO
726725
-> Session IO h
727726
-> SessionEnv IO h
728727
-> TableConfig
729728
-> ArenaManager RealWorld
730729
-> TableContent IO h
731730
-> IO (Table IO h) #-}
732731
newWith ::
733-
(MonadSTM m, MonadMVar m)
734-
=> TempRegistry m
732+
(MonadSTM m, MonadMVar m, PrimMonad m)
733+
=> ActionRegistry m
735734
-> Session m h
736735
-> SessionEnv m h
737736
-> TableConfig
@@ -754,8 +753,9 @@ newWith reg sesh seshEnv conf !am !tc = do
754753
let !tid = uniqueToWord64 tableId
755754
!t = Table conf tableVar am tr tid sesh
756755
-- Track the current table
757-
freeTemp reg $ modifyMVar_ (sessionOpenTables seshEnv)
758-
$ pure . Map.insert (uniqueToWord64 tableId) t
756+
delayedCommit reg $
757+
modifyMVar_ (sessionOpenTables seshEnv) $
758+
pure . Map.insert (uniqueToWord64 tableId) t
759759
pure $! t
760760

761761
{-# SPECIALISE close :: Table IO h -> IO () #-}
@@ -766,15 +766,15 @@ close ::
766766
-> m ()
767767
close t = do
768768
traceWith (tableTracer t) TraceCloseTable
769-
modifyWithTempRegistry_
769+
modifyWithActionRegistry_
770770
(RW.unsafeAcquireWriteAccess (tableState t))
771771
(atomically . RW.unsafeReleaseWriteAccess (tableState t)) $ \reg -> \case
772772
TableClosed -> pure TableClosed
773773
TableOpen thEnv -> do
774774
-- Since we have a write lock on the table state, we know that we are the
775775
-- only thread currently closing the table. We can safely make the session
776776
-- forget about this table.
777-
freeTemp reg (tableSessionUntrackTable (tableId t) thEnv)
777+
delayedCommit reg (tableSessionUntrackTable (tableId t) thEnv)
778778
RW.withWriteAccess_ (tableContent thEnv) $ \tc -> do
779779
releaseTableContent reg tc
780780
pure tc
@@ -868,7 +868,7 @@ updates resolve es t = do
868868
let conf = tableConfig t
869869
withOpenTable t $ \thEnv -> do
870870
let hfs = tableHasFS thEnv
871-
modifyWithTempRegistry_
871+
modifyWithActionRegistry_
872872
(RW.unsafeAcquireWriteAccess (tableContent thEnv))
873873
(atomically . RW.unsafeReleaseWriteAccess (tableContent thEnv)) $ \reg -> do
874874
updatesWithInterleavedFlushes
@@ -1005,10 +1005,10 @@ newCursor !offsetKey t = withOpenTable t $ \thEnv -> do
10051005
-- We acquire a read-lock on the session open-state to prevent races, see
10061006
-- 'sessionOpenTables'.
10071007
withOpenSession cursorSession $ \_ -> do
1008-
withTempRegistry $ \reg -> do
1008+
withActionRegistry $ \reg -> do
10091009
(wb, wbblobs, cursorRuns) <- dupTableContent reg (tableContent thEnv)
10101010
cursorReaders <-
1011-
allocateMaybeTemp reg
1011+
withRollbackMaybe reg
10121012
(Readers.new offsetKey (Just (wb, wbblobs)) cursorRuns)
10131013
Readers.close
10141014
let cursorWBB = wbblobs
@@ -1017,9 +1017,9 @@ newCursor !offsetKey t = withOpenTable t $ \thEnv -> do
10171017
-- Track cursor, but careful: If now an exception is raised, all
10181018
-- resources get freed by the registry, so if the session still
10191019
-- tracks 'cursor' (which is 'CursorOpen'), it later double frees.
1020-
-- Therefore, we only track the cursor if 'withTempRegistry' exits
1021-
-- successfully, i.e. using 'freeTemp'.
1022-
freeTemp reg $
1020+
-- Therefore, we only track the cursor if 'withActionRegistry' exits
1021+
-- successfully, i.e. using 'delayedCommit'.
1022+
delayedCommit reg $
10231023
modifyMVar_ (sessionOpenCursors cursorSessionEnv) $
10241024
pure . Map.insert cursorId cursor
10251025
pure $! cursor
@@ -1030,10 +1030,10 @@ newCursor !offsetKey t = withOpenTable t $ \thEnv -> do
10301030
RW.withReadAccess contentVar $ \content -> do
10311031
let !wb = tableWriteBuffer content
10321032
!wbblobs = tableWriteBufferBlobs content
1033-
wbblobs' <- allocateTemp reg (dupRef wbblobs) releaseRef
1033+
wbblobs' <- withRollback reg (dupRef wbblobs) releaseRef
10341034
let runs = cachedRuns (tableCache content)
10351035
runs' <- V.forM runs $ \r ->
1036-
allocateTemp reg (dupRef r) releaseRef
1036+
withRollback reg (dupRef r) releaseRef
10371037
pure (wb, wbblobs', runs')
10381038

10391039
{-# SPECIALISE closeCursor :: Cursor IO h -> IO () #-}
@@ -1044,20 +1044,20 @@ closeCursor ::
10441044
-> m ()
10451045
closeCursor Cursor {..} = do
10461046
traceWith cursorTracer $ TraceCloseCursor
1047-
modifyWithTempRegistry_ (takeMVar cursorState) (putMVar cursorState) $ \reg -> \case
1047+
modifyWithActionRegistry_ (takeMVar cursorState) (putMVar cursorState) $ \reg -> \case
10481048
CursorClosed -> return CursorClosed
10491049
CursorOpen CursorEnv {..} -> do
10501050
-- This should be safe-ish, but it's still not ideal, because it doesn't
10511051
-- rule out sync exceptions in the cleanup operations.
10521052
-- In that case, the cursor ends up closed, but resources might not have
10531053
-- been freed. Probably better than the other way around, though.
1054-
freeTemp reg $
1054+
delayedCommit reg $
10551055
modifyMVar_ (sessionOpenCursors cursorSessionEnv) $
10561056
pure . Map.delete cursorId
10571057

1058-
forM_ cursorReaders $ freeTemp reg . Readers.close
1059-
V.forM_ cursorRuns $ freeTemp reg . releaseRef
1060-
freeTemp reg (releaseRef cursorWBB)
1058+
forM_ cursorReaders $ delayedCommit reg . Readers.close
1059+
V.forM_ cursorRuns $ delayedCommit reg . releaseRef
1060+
delayedCommit reg (releaseRef cursorWBB)
10611061
return CursorClosed
10621062

10631063
{-# SPECIALISE readCursor ::
@@ -1142,7 +1142,7 @@ createSnapshot ::
11421142
createSnapshot snap label tableType t = do
11431143
traceWith (tableTracer t) $ TraceSnapshot snap
11441144
withOpenTable t $ \thEnv ->
1145-
withTempRegistry $ \reg -> do -- TODO: use the temp registry for all side effects
1145+
withActionRegistry $ \reg -> do -- TODO: use the action registry for all side effects
11461146
let hfs = tableHasFS thEnv
11471147
hbio = tableHasBlockIO thEnv
11481148
uc = tableSessionUniqCounter thEnv
@@ -1156,9 +1156,9 @@ createSnapshot snap label tableType t = do
11561156
else
11571157
-- we assume the snapshots directory already exists, so we just have
11581158
-- to create the directory for this specific snapshot.
1159-
allocateTemp reg
1159+
withRollback_ reg
11601160
(FS.createDirectory hfs (Paths.getNamedSnapshotDir snapDir))
1161-
(\_ -> FS.removeDirectoryRecursive hfs (Paths.getNamedSnapshotDir snapDir))
1161+
(FS.removeDirectoryRecursive hfs (Paths.getNamedSnapshotDir snapDir))
11621162

11631163
-- Duplicate references to the table content, so that resources do not disappear
11641164
-- from under our feet while taking a snapshot. These references are released
@@ -1206,7 +1206,7 @@ openSnapshot ::
12061206
openSnapshot sesh label tableType override snap resolve = do
12071207
traceWith (sessionTracer sesh) $ TraceOpenSnapshot snap override
12081208
withOpenSession sesh $ \seshEnv -> do
1209-
withTempRegistry $ \reg -> do
1209+
withActionRegistry $ \reg -> do
12101210
let hfs = sessionHasFS seshEnv
12111211
hbio = sessionHasBlockIO seshEnv
12121212
uc = sessionUniqCounter seshEnv
@@ -1316,7 +1316,7 @@ duplicate t@Table{..} = do
13161316
-- We acquire a read-lock on the session open-state to prevent races, see
13171317
-- 'sessionOpenTables'.
13181318
withOpenSession tableSession $ \_ -> do
1319-
withTempRegistry $ \reg -> do
1319+
withActionRegistry $ \reg -> do
13201320
-- The table contents escape the read access, but we just added references
13211321
-- to each run so it is safe.
13221322
content <- RW.withReadAccess tableContent (duplicateTableContent reg)
@@ -1369,7 +1369,7 @@ unions ts = do
13691369

13701370
-- We acquire a read-lock on the session open-state to prevent races, see
13711371
-- 'sessionOpenTables'.
1372-
modifyWithTempRegistry
1372+
modifyWithActionRegistry
13731373
(atomically $ RW.unsafeAcquireReadAccess (sessionState sesh))
13741374
(\_ -> atomically $ RW.unsafeReleaseReadAccess (sessionState sesh)) $ \reg -> \case
13751375
SessionClosed -> throwIO ErrSessionClosed

0 commit comments

Comments
 (0)