Skip to content

Commit 5749666

Browse files
authored
db-truncater: make --truncate-after-slot more lenient again (#1203)
Closes #1202 This PR reverts the behavioral change of #1143, specifically 5747d3c. Concretely, `--truncate-after-slot slotNo` will now remove all blocks with a slot number higher than `slotNo` in the ImmutableDB, but does not require that a block with exactly that slot number exists. This is convenient eg for truncating all blocks after an epoch without having to find out the exact slot of the last block in the epoch just before. At the same time, the run time is still much faster than before #1143: We iteratively check all slot numbers descending from the given one, and truncate to the first point that is in the ImmutableDB. As realistic ImmutableDBs are only somewhat sparse (active slot coefficient is `f = 1/20`), this should be very fast (ie still constant time in the length of the chain if we consider the slot distance between any two adjacent blocks to be bounded). In addition, we explicitly check whether the given argument is beyond the tip of the ImmutableDB, and immediately exit (successfully) in that case.
2 parents 7a3b7bb + c862d34 commit 5749666

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

ouroboros-consensus-cardano/app/DBTruncater/Parsers.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ slotNoOption =
3232
mods = mconcat
3333
[ long "truncate-after-slot"
3434
, metavar "SLOT_NUMBER"
35-
, help "The slot number of the intended new tip of the chain after truncation"
35+
, help "Remove all blocks with a higher slot number"
3636
]
3737

3838
blockNoOption :: Parser BlockNo

ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBTruncater/Run.hs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ import Cardano.Slotting.Slot (WithOrigin (..))
1111
import Cardano.Tools.DBAnalyser.HasAnalysis
1212
import Cardano.Tools.DBTruncater.Types
1313
import Control.Monad
14+
import Control.Monad.Trans.Class (lift)
15+
import Control.Monad.Trans.Maybe (MaybeT (..))
1416
import Control.Tracer
17+
import Data.Foldable (asum)
18+
import Data.Functor ((<&>))
1519
import Data.Functor.Identity
16-
import Data.Traversable (for)
1720
import Ouroboros.Consensus.Block
1821
import Ouroboros.Consensus.Config
1922
import Ouroboros.Consensus.Node as Node
2023
import Ouroboros.Consensus.Node.InitStorage as Node
2124
import Ouroboros.Consensus.Storage.Common
2225
import Ouroboros.Consensus.Storage.ImmutableDB (ImmutableDB, Iterator,
23-
IteratorResult (..))
26+
IteratorResult (..), Tip (..))
2427
import qualified Ouroboros.Consensus.Storage.ImmutableDB as ImmutableDB
2528
import Ouroboros.Consensus.Storage.ImmutableDB.Impl
2629
import Ouroboros.Consensus.Util.IOLike
@@ -56,33 +59,35 @@ truncate DBTruncaterConfig{ dbDir, truncateAfter, verbose } args = do
5659
}
5760

5861
withDB immutableDBArgs $ \(immutableDB, internal) -> do
59-
mLastHdr :: Maybe (Header block) <- case truncateAfter of
60-
TruncateAfterSlot slotNo -> do
61-
mHash <- getHashForSlot internal slotNo
62-
for (RealPoint slotNo <$> mHash) $
63-
ImmutableDB.getKnownBlockComponent immutableDB GetHeader
62+
tip <- atomically $ ImmutableDB.getTip immutableDB
63+
let truncationBeyondTip = case truncateAfter of
64+
TruncateAfterSlot slotNo -> (tipSlotNo <$> tip) <= NotOrigin slotNo
65+
TruncateAfterBlock bno -> (tipBlockNo <$> tip) <= NotOrigin bno
66+
if truncationBeyondTip
67+
then putStrLn $ "Nothing to truncate, tip stays at " <> show tip
68+
else do
69+
mLastHdr :: Maybe (Header block) <- case truncateAfter of
70+
TruncateAfterSlot slotNo -> runMaybeT $ asum $
71+
[slotNo, slotNo - 1 .. 0] <&> \s -> do
72+
pt <- RealPoint s <$> MaybeT (getHashForSlot internal s)
73+
lift $ ImmutableDB.getKnownBlockComponent immutableDB GetHeader pt
6474

65-
TruncateAfterBlock bno -> do
66-
-- At the moment, we're just running a linear search with streamAll to
67-
-- find the correct block to truncate from, but we could in theory do this
68-
-- more quickly by binary searching the chunks of the ImmutableDB.
69-
iterator <- ImmutableDB.streamAll immutableDB registry GetHeader
70-
findLast ((<= bno) . blockNo) iterator
75+
TruncateAfterBlock bno -> do
76+
-- At the moment, we're just running a linear search with streamAll to
77+
-- find the correct block to truncate from, but we could in theory do this
78+
-- more quickly by binary searching the chunks of the ImmutableDB.
79+
iterator <- ImmutableDB.streamAll immutableDB registry GetHeader
80+
findLast ((<= bno) . blockNo) iterator
7181

72-
case ImmutableDB.headerToTip <$> mLastHdr of
73-
Nothing ->
74-
putStrLn $ mconcat
75-
[ "Unable to find a truncate point. This is because the ImmutableDB"
76-
, "does not contain a block with the given slot or block number."
77-
]
78-
Just newTip -> do
79-
when verbose $ do
82+
case ImmutableDB.headerToTip <$> mLastHdr of
83+
Nothing -> fail "Couldn't find a point to truncate to!"
84+
Just newTip -> do
8085
putStrLn $ mconcat
8186
[ "Truncating the ImmutableDB using the following block as the "
8287
, "new tip:\n"
8388
, " ", show newTip
8489
]
85-
deleteAfter internal (At newTip)
90+
deleteAfter internal (At newTip)
8691

8792
-- | Given a predicate, and an iterator, find the last item for which
8893
-- the predicate passes.

ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBTruncater/Types.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ data DBTruncaterConfig = DBTruncaterConfig {
1313

1414
-- | Where to truncate the ImmutableDB.
1515
data TruncateAfter
16-
-- | Truncate after the given slot number, such that the new tip has this
17-
-- exact slot number. Fail if this is not possible, ie no block has this
18-
-- slot number. If there are two blocks with the same slot number (due to
19-
-- EBBs), the tip will be the non-EBB.
16+
-- | Truncate after the given slot number, deleting all blocks with a higher
17+
-- slot number.
2018
= TruncateAfterSlot SlotNo
2119
-- | Truncate after the given block number (such that the new tip has this
2220
-- block number).

0 commit comments

Comments
 (0)