Skip to content

Commit 476f84e

Browse files
committed
First integration of LSM trees
1 parent 4568bfb commit 476f84e

File tree

10 files changed

+324
-18
lines changed

10 files changed

+324
-18
lines changed

cabal.project

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ packages:
2525
ouroboros-consensus-diffusion
2626
sop-extras
2727
strict-sop-core
28+
../lsm-tree
2829

2930
-- We want to always build the test-suites and benchmarks
3031
tests: true
@@ -57,6 +58,9 @@ if impl (ghc >= 9.12)
5758
-- https://github.com/kapralVV/Unique/issues/11
5859
, Unique:hashable
5960

61+
-- https://github.com/phadej/regression-simple/pull/14
62+
, regression-simple:base
63+
6064
source-repository-package
6165
type: git
6266
location: https://github.com/IntersectMBO/cardano-ledger
@@ -95,7 +99,7 @@ source-repository-package
9599
location: https://github.com/IntersectMBO/ouroboros-network
96100
tag: dd45510b6c5a935e0f330d7b180800a27ebe2fd8
97101
--sha256: sha256-Xk+DbtqQwadtmRoZNVzDUWM8tnV+RECXYRe74IOkL0s=
98-
subdir:
102+
subdir:
99103
network-mux
100104
ouroboros-network
101105
ouroboros-network-api
@@ -111,3 +115,11 @@ source-repository-package
111115
--sha256: 0ss4n302khl13fj5f4l6cxfj5vn558s2wk533ikmxhgigf9qas0q
112116
subdir: fs-api
113117
fs-sim
118+
119+
source-repository-package
120+
type: git
121+
location: https://github.com/jasagredo/lsm-tree
122+
tag: fa0d862b7a609473538a0007bd0df314cd2fa635
123+
subdir:
124+
125+
blockio

ouroboros-consensus/ouroboros-consensus.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ library
253253
Ouroboros.Consensus.Storage.LedgerDB.V2.Args
254254
Ouroboros.Consensus.Storage.LedgerDB.V2.Forker
255255
Ouroboros.Consensus.Storage.LedgerDB.V2.InMemory
256+
Ouroboros.Consensus.Storage.LedgerDB.V2.LSM
256257
Ouroboros.Consensus.Storage.LedgerDB.V2.LedgerSeq
257258
Ouroboros.Consensus.Storage.Serialisation
258259
Ouroboros.Consensus.Storage.VolatileDB
@@ -350,6 +351,7 @@ library
350351
transformers-base,
351352
typed-protocols ^>=1.0,
352353
vector ^>=0.13,
354+
lsm-tree,
353355

354356
x-docspec-extra-packages:
355357
directory
@@ -542,7 +544,6 @@ library unstable-mempool-test-utils
542544
io-classes:strict-stm,
543545
ouroboros-consensus,
544546
resource-registry,
545-
strict-stm,
546547

547548
library unstable-tutorials
548549
import: common-lib

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module Ouroboros.Consensus.Storage.ChainDB.Impl
3838
, openDBInternal
3939
) where
4040

41+
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.LSM as LSM
4142
import Control.Monad (void, when)
4243
import Control.Monad.Trans.Class (lift)
4344
import Control.ResourceRegistry
@@ -60,6 +61,7 @@ import Ouroboros.Consensus.HardFork.Abstract
6061
import Ouroboros.Consensus.HeaderValidation (mkHeaderWithTime)
6162
import Ouroboros.Consensus.Ledger.Extended (ledgerState)
6263
import Ouroboros.Consensus.Ledger.Inspect
64+
import Ouroboros.Consensus.Ledger.Basics
6365
import Ouroboros.Consensus.Ledger.SupportsProtocol
6466
import Ouroboros.Consensus.Storage.ChainDB.API (ChainDB)
6567
import qualified Ouroboros.Consensus.Storage.ChainDB.API as API
@@ -105,6 +107,7 @@ withDB ::
105107
, ConvertRawHash blk
106108
, SerialiseDiskConstraints blk
107109
, LedgerSupportsLedgerDB blk
110+
, LSM.GoodForLSM (LedgerState blk)
108111
) =>
109112
Complete Args.ChainDbArgs m blk ->
110113
(ChainDB m blk -> m a) ->
@@ -121,6 +124,7 @@ openDB ::
121124
, ConvertRawHash blk
122125
, SerialiseDiskConstraints blk
123126
, LedgerSupportsLedgerDB blk
127+
, LSM.GoodForLSM (LedgerState blk)
124128
) =>
125129
Complete Args.ChainDbArgs m blk ->
126130
m (ChainDB m blk)
@@ -137,6 +141,7 @@ openDBInternal ::
137141
, SerialiseDiskConstraints blk
138142
, HasCallStack
139143
, LedgerSupportsLedgerDB blk
144+
, LSM.GoodForLSM (LedgerState blk)
140145
) =>
141146
Complete Args.ChainDbArgs m blk ->
142147
-- | 'True' = Launch background tasks

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Data.Word
2222
import Ouroboros.Consensus.Block
2323
import Ouroboros.Consensus.HardFork.Abstract
2424
import Ouroboros.Consensus.Ledger.Inspect
25+
import Ouroboros.Consensus.Ledger.Basics
2526
import Ouroboros.Consensus.Ledger.SupportsProtocol
2627
import Ouroboros.Consensus.Storage.ImmutableDB.Stream
2728
import Ouroboros.Consensus.Storage.LedgerDB.API
@@ -30,6 +31,7 @@ import Ouroboros.Consensus.Storage.LedgerDB.Forker
3031
import Ouroboros.Consensus.Storage.LedgerDB.TraceEvent
3132
import qualified Ouroboros.Consensus.Storage.LedgerDB.V1 as V1
3233
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2 as V2
34+
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.LSM as LSM
3335
import Ouroboros.Consensus.Util.Args
3436
import Ouroboros.Consensus.Util.CallStack
3537
import Ouroboros.Consensus.Util.IOLike
@@ -44,6 +46,8 @@ openDB ::
4446
, HasCallStack
4547
, HasHardForkHistory blk
4648
, LedgerSupportsLedgerDB blk
49+
, LSM.GoodForLSM (LedgerState blk)
50+
4751
) =>
4852
-- | Stateless initializaton arguments
4953
Complete LedgerDbArgs m blk ->

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,20 @@ instance FromJSON SnapshotMetadata where
183183
data SnapshotBackend
184184
= UTxOHDMemSnapshot
185185
| UTxOHDLMDBSnapshot
186+
| UTxOHDLSMSnapshot
186187
deriving (Eq, Show)
187188

188189
instance ToJSON SnapshotBackend where
189190
toJSON = \case
190191
UTxOHDMemSnapshot -> "utxohd-mem"
191192
UTxOHDLMDBSnapshot -> "utxohd-lmdb"
193+
UTxOHDLSMSnapshot -> "utxohd-lsm"
192194

193195
instance FromJSON SnapshotBackend where
194196
parseJSON = Aeson.withText "SnapshotBackend" $ \case
195197
"utxohd-mem" -> pure UTxOHDMemSnapshot
196198
"utxohd-lmdb" -> pure UTxOHDLMDBSnapshot
199+
"utxohd-lsm" -> pure UTxOHDLSMSnapshot
197200
_ -> fail "unknown SnapshotBackend"
198201

199202
data MetadataErr

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import Ouroboros.Consensus.Storage.LedgerDB.TraceEvent
5656
import Ouroboros.Consensus.Storage.LedgerDB.V2.Args as V2
5757
import Ouroboros.Consensus.Storage.LedgerDB.V2.Forker
5858
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.InMemory as InMemory
59+
import qualified Ouroboros.Consensus.Storage.LedgerDB.V2.LSM as LSM
5960
import Ouroboros.Consensus.Storage.LedgerDB.V2.LedgerSeq
6061
import Ouroboros.Consensus.Util.Args
6162
import Ouroboros.Consensus.Util.CallStack
@@ -72,6 +73,7 @@ mkInitDb ::
7273
, IOLike m
7374
, LedgerDbSerialiseConstraints blk
7475
, HasHardForkHistory blk
76+
, LSM.GoodForLSM (LedgerState blk)
7577
, LedgerSupportsInMemoryLedgerDB blk
7678
) =>
7779
Complete LedgerDbArgs m blk ->
@@ -138,7 +140,10 @@ mkInitDb args flavArgs getBlock =
138140
emptyF st =
139141
empty' st $ case bss of
140142
InMemoryHandleArgs -> InMemory.newInMemoryLedgerTablesHandle v2Tracer lgrHasFS
141-
LSMHandleArgs x -> absurd x
143+
LSMHandleArgs session ->
144+
\values -> do
145+
table <- LSM.tableFromValuesMK lgrRegistry session values
146+
LSM.newLSMLedgerTablesHandle v2Tracer lgrRegistry session table
142147

143148
loadSnapshot ::
144149
CodecConfig blk ->
@@ -147,7 +152,7 @@ mkInitDb args flavArgs getBlock =
147152
m (Either (SnapshotFailure blk) (LedgerSeq' m blk, RealPoint blk))
148153
loadSnapshot ccfg fs ds = case bss of
149154
InMemoryHandleArgs -> runExceptT $ InMemory.loadSnapshot v2Tracer lgrRegistry ccfg fs ds
150-
LSMHandleArgs x -> absurd x
155+
LSMHandleArgs session -> runExceptT $ LSM.loadSnapshot v2Tracer lgrRegistry ccfg fs session ds
151156

152157
implMkLedgerDb ::
153158
forall m l blk.
@@ -162,7 +167,7 @@ implMkLedgerDb ::
162167
, HasHardForkHistory blk
163168
) =>
164169
LedgerDBHandle m l blk ->
165-
HandleArgs ->
170+
HandleArgs m ->
166171
(LedgerDB m l blk, TestInternals m l blk)
167172
implMkLedgerDb h bss =
168173
( LedgerDB
@@ -188,7 +193,7 @@ mkInternals ::
188193
, LedgerSupportsProtocol blk
189194
, ApplyBlock (ExtLedgerState blk) blk
190195
) =>
191-
HandleArgs ->
196+
HandleArgs m ->
192197
LedgerDBHandle m (ExtLedgerState blk) blk ->
193198
TestInternals' m blk
194199
mkInternals bss h =
@@ -245,7 +250,7 @@ mkInternals bss h =
245250
m (Maybe (DiskSnapshot, RealPoint blk))
246251
takeSnapshot = case bss of
247252
InMemoryHandleArgs -> InMemory.takeSnapshot
248-
LSMHandleArgs x -> absurd x
253+
LSMHandleArgs{} -> LSM.takeSnapshot
249254

250255
-- | Testing only! Truncate all snapshots in the DB.
251256
implIntTruncateSnapshots :: MonadThrow m => SomeHasFS m -> m ()
@@ -359,7 +364,7 @@ implTryTakeSnapshot ::
359364
, LedgerSupportsProtocol blk
360365
, LedgerDbSerialiseConstraints blk
361366
) =>
362-
HandleArgs ->
367+
HandleArgs m ->
363368
LedgerDBEnv m l blk ->
364369
Maybe (Time, Time) ->
365370
Word64 ->
@@ -397,7 +402,13 @@ implTryTakeSnapshot bss env mTime nrBlocks =
397402
fs
398403
Nothing
399404
ref
400-
LSMHandleArgs x -> absurd x
405+
LSMHandleArgs{} ->
406+
LSM.takeSnapshot
407+
config
408+
trcr
409+
fs
410+
Nothing
411+
ref
401412

402413
-- In the first version of the LedgerDB for UTxO-HD, there is a need to
403414
-- periodically flush the accumulated differences to the disk. However, in the

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ module Ouroboros.Consensus.Storage.LedgerDB.V2.Args
1717
import Data.Void (Void)
1818
import GHC.Generics
1919
import NoThunks.Class
20+
import Database.LSMTree (Session)
2021

21-
data LedgerDbFlavorArgs f m = V2Args HandleArgs
22+
data LedgerDbFlavorArgs f m = V2Args (HandleArgs m)
2223

23-
data HandleArgs
24+
data HandleArgs m
2425
= InMemoryHandleArgs
25-
| LSMHandleArgs Void
26-
deriving (Generic, NoThunks)
26+
| LSMHandleArgs (Session m)
2727

2828
data FlavorImplSpecificTrace
2929
= -- | Created a new 'LedgerTablesHandle', potentially by duplicating an

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ newInMemoryLedgerTablesHandle tracer someFS@(SomeHasFS hasFS) l = do
137137
guardClosed h $
138138
\values ->
139139
withFile hasFS (mkFsPath [snapshotName, "tables", "tvar"]) (WriteMode MustBeNew) $ \hf ->
140-
fmap snd $
140+
fmap (Just . snd) $
141141
hPutAllCRC hasFS hf $
142142
CBOR.toLazyByteString $
143143
valuesMKEncoder hint values
@@ -172,7 +172,7 @@ writeSnapshot fs@(SomeHasFS hasFs) encLedger ds st = do
172172
writeSnapshotMetadata fs ds $
173173
SnapshotMetadata
174174
{ snapshotBackend = UTxOHDMemSnapshot
175-
, snapshotChecksum = crcOfConcat crc1 crc2
175+
, snapshotChecksum = maybe crc1 (crcOfConcat crc1) crc2
176176
}
177177

178178
takeSnapshot ::

0 commit comments

Comments
 (0)