Skip to content

Commit f8d992b

Browse files
committed
LedgerDB: implement predictable snapshotting
1 parent b28d143 commit f8d992b

File tree

9 files changed

+266
-202
lines changed

9 files changed

+266
-202
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
### Breaking
2+
3+
- LedgerDB: implemented *predictable* snapshots, i.e. different nodes with the
4+
same configuration will now create snapshots for the same slots.
5+
6+
See 'SnapshotPolicyArgs' for more details.

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/ChainDB/Impl.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ openDBInternal args launchBgTasks = runWithTempRegistry $ do
160160
(chainDB, testing, env) <- lift $ do
161161
traceWith tracer $ TraceOpenEvent (OpenedVolatileDB maxSlot)
162162
traceWith tracer $ TraceOpenEvent StartedOpeningLgrDB
163-
(lgrDB, replayed) <-
163+
(lgrDB, _replayed) <-
164164
LedgerDB.openDB
165165
argsLgrDb
166166
(ImmutableDB.streamAPI immutableDB)
@@ -279,8 +279,7 @@ openDBInternal args launchBgTasks = runWithTempRegistry $ do
279279
Internal
280280
{ intCopyToImmutableDB = getEnv h (withFuse copyTestFuse . Background.copyToImmutableDB)
281281
, intGarbageCollect = getEnv1 h Background.garbageCollect
282-
, intTryTakeSnapshot = getEnv h $ \env' ->
283-
void $ LedgerDB.tryTakeSnapshot (cdbLedgerDB env') Nothing maxBound
282+
, intTryTakeSnapshot = getEnv h $ LedgerDB.tryTakeSnapshot . cdbLedgerDB
284283
, intAddBlockRunner = getEnv h (Background.addBlockRunner addBlockTestFuse)
285284
, intKillBgThreads = varKillBgThreads
286285
}
@@ -291,7 +290,7 @@ openDBInternal args launchBgTasks = runWithTempRegistry $ do
291290
(castPoint $ AF.anchorPoint chain)
292291
(castPoint $ AF.headPoint chain)
293292

294-
when launchBgTasks $ Background.launchBgTasks env replayed
293+
when launchBgTasks $ Background.launchBgTasks env
295294

296295
return (chainDB, testing, env)
297296

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/ChainDB/Impl/Background.hs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
{-# LANGUAGE NamedFieldPuns #-}
77
{-# LANGUAGE RecordWildCards #-}
88
{-# LANGUAGE ScopedTypeVariables #-}
9-
{-# LANGUAGE TupleSections #-}
109

1110
-- | Background tasks:
1211
--
@@ -53,7 +52,6 @@ import Data.Sequence.Strict (StrictSeq (..))
5352
import qualified Data.Sequence.Strict as Seq
5453
import Data.Time.Clock
5554
import Data.Void (Void)
56-
import Data.Word
5755
import GHC.Generics (Generic)
5856
import GHC.Stack (HasCallStack)
5957
import Ouroboros.Consensus.Block
@@ -93,10 +91,8 @@ launchBgTasks ::
9391
, HasHardForkHistory blk
9492
) =>
9593
ChainDbEnv m blk ->
96-
-- | Number of immutable blocks replayed on ledger DB startup
97-
Word64 ->
9894
m ()
99-
launchBgTasks cdb@CDB{..} replayed = do
95+
launchBgTasks cdb@CDB{..} = do
10096
!addBlockThread <-
10197
launch "ChainDB.addBlockRunner" $
10298
addBlockRunner cdbChainSelFuse cdb
@@ -107,7 +103,7 @@ launchBgTasks cdb@CDB{..} replayed = do
107103
garbageCollect cdb
108104
!copyAndSnapshotThread <-
109105
launch "ChainDB.copyAndSnapshotRunner" $
110-
copyAndSnapshotRunner cdb gcSchedule replayed cdbCopyFuse
106+
copyAndSnapshotRunner cdb gcSchedule cdbCopyFuse
111107
atomically $
112108
writeTVar cdbKillBgThreads $
113109
sequence_ [addBlockThread, gcThread, copyAndSnapshotThread]
@@ -241,30 +237,22 @@ copyAndSnapshotRunner ::
241237
) =>
242238
ChainDbEnv m blk ->
243239
GcSchedule m ->
244-
-- | Number of immutable blocks replayed on ledger DB startup
245-
Word64 ->
246240
Fuse m ->
247241
m Void
248-
copyAndSnapshotRunner cdb@CDB{..} gcSchedule replayed fuse = do
242+
copyAndSnapshotRunner cdb@CDB{..} gcSchedule fuse = do
249243
-- this first flush will persist the differences that come from the initial
250244
-- chain selection.
251245
LedgerDB.tryFlush cdbLedgerDB
252-
loop =<< LedgerDB.tryTakeSnapshot cdbLedgerDB Nothing replayed
246+
forever copyAndSnapshot
253247
where
254248
SecurityParam k = configSecurityParam cdbTopLevelConfig
255249

256-
loop :: LedgerDB.SnapCounters -> m Void
257-
loop counters = do
258-
let LedgerDB.SnapCounters
259-
{ prevSnapshotTime
260-
, ntBlocksSinceLastSnap
261-
} = counters
262-
250+
copyAndSnapshot :: m ()
251+
copyAndSnapshot = do
263252
-- Wait for the chain to grow larger than @k@
264-
numToWrite <- atomically $ do
253+
atomically $ do
265254
curChain <- icWithoutTime <$> readTVar cdbChain
266255
check $ fromIntegral (AF.length curChain) > unNonZero k
267-
return $ fromIntegral (AF.length curChain) - unNonZero k
268256

269257
-- Copy blocks to ImmutableDB
270258
--
@@ -273,16 +261,13 @@ copyAndSnapshotRunner cdb@CDB{..} gcSchedule replayed fuse = do
273261
gcSlotNo <- withFuse fuse (copyToImmutableDB cdb)
274262
scheduleGC' gcSlotNo
275263

264+
LedgerDB.tryTakeSnapshot cdbLedgerDB
265+
276266
-- See the Haddocks above as for why we garbage-collect the LedgerDB already
277267
-- here (instead of as part of the scheduled GC).
278268
whenJust (withOriginToMaybe gcSlotNo) $ LedgerDB.garbageCollect cdbLedgerDB
279269
LedgerDB.tryFlush cdbLedgerDB
280270

281-
now <- getMonotonicTime
282-
let ntBlocksSinceLastSnap' = ntBlocksSinceLastSnap + numToWrite
283-
284-
loop =<< LedgerDB.tryTakeSnapshot cdbLedgerDB ((,now) <$> prevSnapshotTime) ntBlocksSinceLastSnap'
285-
286271
scheduleGC' :: WithOrigin SlotNo -> m ()
287272
scheduleGC' Origin = return ()
288273
scheduleGC' (NotOrigin slotNo) =

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

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,6 @@ module Ouroboros.Consensus.Storage.LedgerDB.API
149149
, withPrivateTipForker
150150
, withTipForker
151151

152-
-- * Snapshots
153-
, SnapCounters (..)
154-
155152
-- * Testing
156153
, TestInternals (..)
157154
, TestInternals'
@@ -160,7 +157,6 @@ module Ouroboros.Consensus.Storage.LedgerDB.API
160157

161158
import Codec.Serialise
162159
import qualified Control.Monad as Monad
163-
import Control.Monad.Class.MonadTime.SI
164160
import Control.Monad.Except
165161
import Control.ResourceRegistry
166162
import Control.Tracer
@@ -262,18 +258,12 @@ data LedgerDB m l blk = LedgerDB
262258
-- * The set of previously applied points.
263259
, tryTakeSnapshot ::
264260
l ~ ExtLedgerState blk =>
265-
Maybe (Time, Time) ->
266-
Word64 ->
267-
m SnapCounters
261+
m ()
268262
-- ^ If the provided arguments indicate so (based on the SnapshotPolicy with
269263
-- which this LedgerDB was opened), take a snapshot and delete stale ones.
270264
--
271-
-- The arguments are:
272-
--
273-
-- - If a snapshot has been taken already, the time at which it was taken
274-
-- and the current time.
275-
--
276-
-- - How many blocks have been processed since the last snapshot.
265+
-- For V1, this must not be called concurrently with 'garbageCollect' and/or
266+
-- 'tryFlush'.
277267
, tryFlush :: m ()
278268
-- ^ Flush V1 in-memory LedgerDB state to disk, if possible. This is a no-op
279269
-- for implementations that do not need an explicit flush function.
@@ -420,18 +410,6 @@ getReadOnlyForker ::
420410
m (Either GetForkerError (ReadOnlyForker m l blk))
421411
getReadOnlyForker ldb rr pt = fmap readOnlyForker <$> getForkerAtTarget ldb rr pt
422412

423-
{-------------------------------------------------------------------------------
424-
Snapshots
425-
-------------------------------------------------------------------------------}
426-
427-
-- | Counters to keep track of when we made the last snapshot.
428-
data SnapCounters = SnapCounters
429-
{ prevSnapshotTime :: !(Maybe Time)
430-
-- ^ When was the last time we made a snapshot
431-
, ntBlocksSinceLastSnap :: !Word64
432-
-- ^ How many blocks have we processed since the last snapshot
433-
}
434-
435413
{-------------------------------------------------------------------------------
436414
Initialization
437415
-------------------------------------------------------------------------------}

0 commit comments

Comments
 (0)