1515 , PackageImports
1616 , ScopedTypeVariables
1717 , TypeApplications
18+ , RecordWildCards
19+ , ApplicativeDo
1820#-}
1921
2022module Chainweb.Pact.Backend.Compaction
@@ -23,8 +25,8 @@ module Chainweb.Pact.Backend.Compaction
2325 main
2426
2527 -- * Exported for testing
26- , compactPactState
27- , compactRocksDb
28+ , doCompactPactState
29+ , doCompactRocksDb
2830 , Retainment (.. )
2931 , defaultRetainment
3032
@@ -147,23 +149,24 @@ data Config = Config
147149 , toDir :: FilePath
148150 , concurrent :: ConcurrentChains
149151 , logDir :: FilePath
150- , noRocksDb :: Bool
151- -- ^ Don't produce a new RocksDB at all.
152- , noPactState :: Bool
153- -- ^ Don't produce a new Pact State at all.
154- , keepFullTransactionIndex :: Bool
155- -- ^ Whether or not to keep the entire TransactionIndex table. Some APIs rely on this table.
152+ , compactRocksDb :: Bool
153+ -- ^ Compact the RocksDB at all?
154+ , compactPactState :: Bool
155+ -- ^ Compact the Pact State at all?
156+ , compactTransactionIndex :: Bool
157+ -- ^ Compact the TransactionIndex table in the Pact state?
158+ -- Some APIs (e.g. /poll) rely on this table, so the default is False.
156159 }
157160
158161data Retainment = Retainment
159- { keepFullTransactionIndex :: Bool
162+ { compactTransactionIndex :: Bool
160163 , compactThese :: CompactThese
161164 }
162165
163166defaultRetainment :: Retainment
164167defaultRetainment = Retainment
165- { keepFullTransactionIndex = False
166- , compactThese = CompactBoth
168+ { compactTransactionIndex = False
169+ , compactThese = CompactOnlyPactState
167170 }
168171
169172data CompactThese = CompactOnlyRocksDb | CompactOnlyPactState | CompactBoth | CompactNeither
@@ -180,16 +183,17 @@ getConfig = do
180183 (O. fullDesc <> O. progDesc " Pact DB Compaction Tool - create a compacted copy of the source database directory Pact DB into the target directory." )
181184
182185 parser :: O. Parser Config
183- parser = Config
184- <$> (parseVersion <$> O. strOption (O. long " chainweb-version" <> O. value " mainnet01" ))
185- <*> O. strOption (O. long " from" <> O. help " Directory containing SQLite Pact state and RocksDB block data to compact (expected to be in $DIR/0/{sqlite,rocksDb}" )
186- <*> O. strOption (O. long " to" <> O. help " Directory where to place the compacted Pact state and block data. It will place them in $DIR/0/{sqlite,rocksDb}, respectively." )
187- <*> O. flag SingleChain ManyChainsAtOnce (O. long " parallel" <> O. help " Turn on multi-threaded compaction. The threads are per-chain." )
188- <*> O. strOption (O. long " log-dir" <> O. help " Directory where compaction logs will be placed." )
186+ parser = do
187+ chainwebVersion <- (parseVersion <$> O. strOption (O. long " chainweb-version" <> O. value " mainnet01" ))
188+ fromDir <- O. strOption (O. long " from" <> O. help " Directory containing SQLite Pact state and RocksDB block data to compact (expected to be in $DIR/0/{sqlite,rocksDb}" )
189+ toDir <- O. strOption (O. long " to" <> O. help " Directory where to place the compacted Pact state and block data. It will place them in $DIR/0/{sqlite,rocksDb}, respectively." )
190+ concurrent <- O. flag SingleChain ManyChainsAtOnce (O. long " parallel" <> O. help " Turn on multi-threaded compaction. The threads are per-chain." )
191+ logDir <- O. strOption (O. long " log-dir" <> O. help " Directory where compaction logs will be placed." )
189192 -- Hidden options
190- <*> O. switch (O. long " keep-full-rocksdb" <> O. hidden)
191- <*> O. switch (O. long " no-rocksdb" <> O. hidden)
192- <*> O. switch (O. long " no-pact" <> O. hidden)
193+ compactRocksDb <- O. switch (O. long " compact-rocksdb" <> O. hidden <> O. internal)
194+ compactPactState <- not <$> O. switch (O. long " no-compact-pact" <> O. hidden <> O. internal)
195+ compactTransactionIndex <- O. switch (O. long " compact-transaction-index" <> O. hidden <> O. internal)
196+ return Config {.. }
193197
194198 parseVersion :: Text -> ChainwebVersion
195199 parseVersion =
@@ -201,8 +205,8 @@ main :: IO ()
201205main = do
202206 compact =<< getConfig
203207
204- compactPactState :: (Logger logger ) => logger -> Retainment -> BlockHeight -> SQLiteEnv -> SQLiteEnv -> IO ()
205- compactPactState logger rt targetBlockHeight srcDb targetDb = do
208+ doCompactPactState :: (Logger logger ) => logger -> Retainment -> BlockHeight -> SQLiteEnv -> SQLiteEnv -> IO ()
209+ doCompactPactState logger rt targetBlockHeight srcDb targetDb = do
206210 let log = logFunctionText logger
207211
208212 -- These pragmas are tuned for fast insertion on systems with a wide range
@@ -262,8 +266,7 @@ compactPactState logger rt targetBlockHeight srcDb targetDb = do
262266
263267 -- Copy over TransactionIndex.
264268 --
265- -- If the user specifies that they want to keep the entire table, then we do so, otherwise,
266- -- we compact this based on the RocksDB 'blockHeightKeepDepth'.
269+ -- If the user specifies that they want to compact the table, then we do so based on the RocksDB 'blockHeightKeepDepth'.
267270 --
268271 -- /poll and SPV rely on having this table synchronised with RocksDB.
269272 -- We need to document APIs which need TransactionIndex.
@@ -272,7 +275,7 @@ compactPactState logger rt targetBlockHeight srcDb targetDb = do
272275 -- https://tableplus.com/blog/2018/07/sqlite-how-to-copy-table-to-another-database.html
273276 do
274277 (qry, args) <-
275- if rt. keepFullTransactionIndex
278+ if not rt. compactTransactionIndex
276279 then do
277280 log LL. Info " Copying over entire TransactionIndex table. This could take a while"
278281 let wholeTableQuery = " SELECT txhash, blockheight FROM TransactionIndex ORDER BY blockheight"
@@ -338,11 +341,11 @@ compact :: Config -> IO ()
338341compact cfg = do
339342 let cids = allChains cfg. chainwebVersion
340343
341- let _compactThese = case (cfg. noRocksDb , cfg. noPactState ) of
342- (True , True ) -> CompactNeither
343- (True , False ) -> CompactOnlyPactState
344- (False , True ) -> CompactOnlyRocksDb
345- (False , False ) -> CompactBoth
344+ let _compactThese = case (cfg. compactRocksDb , cfg. compactPactState ) of
345+ (False , False ) -> CompactNeither
346+ (False , True ) -> CompactOnlyPactState
347+ (True , False ) -> CompactOnlyRocksDb
348+ (True , True ) -> CompactBoth
346349
347350 -- Get the target blockheight.
348351 targetBlockHeight <- withDefaultLogger LL. Debug $ \ logger -> do
@@ -378,23 +381,23 @@ compact cfg = do
378381 pure targetBlockHeight
379382
380383 -- Compact RocksDB.
381- when ( not cfg. noRocksDb) $ do
384+ when cfg. compactRocksDb $ do
382385 withRocksDbFileLogger cfg. logDir LL. Debug $ \ logger -> do
383386 withReadOnlyRocksDb (rocksDir cfg. fromDir) modernDefaultOptions $ \ srcRocksDb -> do
384387 withRocksDb (rocksDir cfg. toDir) (modernDefaultOptions { compression = NoCompression }) $ \ targetRocksDb -> do
385- compactRocksDb (set setLoggerLevel (l2l LL. Info ) logger) cfg. chainwebVersion cids (targetBlockHeight - blockHeightKeepDepth) srcRocksDb targetRocksDb
388+ doCompactRocksDb (set setLoggerLevel (l2l LL. Info ) logger) cfg. chainwebVersion cids (targetBlockHeight - blockHeightKeepDepth) srcRocksDb targetRocksDb
386389
387390 -- Compact the pact state.
388391 let retainment = Retainment
389- { keepFullTransactionIndex = cfg. keepFullTransactionIndex
392+ { compactTransactionIndex = cfg. compactTransactionIndex
390393 , compactThese = _compactThese
391394 }
392- when ( not cfg. noPactState) $ do
395+ when cfg. compactPactState $ do
393396 forChains_ cfg. concurrent cids $ \ cid -> do
394397 withPerChainFileLogger cfg. logDir cid LL. Debug $ \ logger -> do
395398 withChainDb cid logger (pactDir cfg. fromDir) $ \ _ srcDb -> do
396399 withChainDb cid logger (pactDir cfg. toDir) $ \ _ targetDb -> do
397- compactPactState logger retainment targetBlockHeight srcDb targetDb
400+ doCompactPactState logger retainment targetBlockHeight srcDb targetDb
398401
399402compactTable :: (Logger logger )
400403 => logger -- ^ logger
@@ -701,15 +704,15 @@ rocksDir :: FilePath -> FilePath
701704rocksDir db = db </> " 0/rocksDb"
702705
703706-- | Copy over all CutHashes, all BlockHeaders, and only some Payloads.
704- compactRocksDb :: (Logger logger )
707+ doCompactRocksDb :: (Logger logger )
705708 => logger
706709 -> ChainwebVersion -- ^ cw version
707710 -> [ChainId ] -- ^ ChainIds
708711 -> BlockHeight -- ^ minBlockHeight for payload copying
709712 -> RocksDb -- ^ source db, should be opened read-only
710713 -> RocksDb -- ^ target db
711714 -> IO ()
712- compactRocksDb logger cwVersion cids minBlockHeight srcDb targetDb = do
715+ doCompactRocksDb logger cwVersion cids minBlockHeight srcDb targetDb = do
713716 let log = logFunctionText logger
714717
715718 -- Copy over entirety of CutHashes table
@@ -768,7 +771,7 @@ compactRocksDb logger cwVersion cids minBlockHeight srcDb targetDb = do
768771 -- them does not.
769772 Nothing -> do
770773 iterFirst it
771-
774+
772775 Just minBlockHeaderHistory -> do
773776 let runBack =
774777 let x = int latestHeader
0 commit comments