Skip to content

Commit 50da856

Browse files
authored
Merge pull request #554 from IntersectMBO/dcoutts/merging-tree
Table union porting: change merge credit tracking strategy and return leftovers
2 parents 036f60e + 74d9048 commit 50da856

File tree

9 files changed

+656
-354
lines changed

9 files changed

+656
-354
lines changed

lsm-tree.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ test-suite lsm-tree-test
366366
Test.Database.LSMTree.Internal.Index.Ordinary
367367
Test.Database.LSMTree.Internal.Lookup
368368
Test.Database.LSMTree.Internal.Merge
369+
Test.Database.LSMTree.Internal.MergingRun
369370
Test.Database.LSMTree.Internal.MergingTree
370371
Test.Database.LSMTree.Internal.Monkey
371372
Test.Database.LSMTree.Internal.PageAcc

src-extras/Database/LSMTree/Extras/NoThunks.hs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,8 @@ deriving anyclass instance NoThunks MergePolicyForLevel
320320
deriving stock instance Generic NumRuns
321321
deriving anyclass instance NoThunks NumRuns
322322

323-
deriving stock instance Generic (UnspentCreditsVar s)
324-
deriving anyclass instance Typeable s => NoThunks (UnspentCreditsVar s)
325-
326-
deriving stock instance Generic (TotalStepsVar s)
327-
deriving anyclass instance Typeable s => NoThunks (TotalStepsVar s)
328-
329-
deriving stock instance Generic (SpentCreditsVar s)
330-
deriving anyclass instance Typeable s => NoThunks (SpentCreditsVar s)
323+
deriving stock instance Generic (CreditsVar s)
324+
deriving anyclass instance Typeable s => NoThunks (CreditsVar s)
331325

332326
deriving stock instance Generic MergeKnownCompleted
333327
deriving anyclass instance NoThunks MergeKnownCompleted

src/Database/LSMTree/Internal/MergeSchedule.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ addRunToLevels tr conf@TableConfig{..} resolve hfs hbio root uc r0 reg levels ul
746746
OneShot -> do
747747
let !required = MR.Credits (unNumEntries (V.foldMap' Run.size rs))
748748
let !thresh = creditThresholdForLevel conf ln
749-
MR.supplyCredits required thresh mr
749+
_leftoverCredits <- MR.supplyCredits mr thresh required
750750
-- This ensures the merge is really completed. However, we don't
751751
-- release the merge yet and only briefly inspect the resulting run.
752752
bracket (MR.expectCompleted mr) releaseRef $ \r ->
@@ -877,7 +877,8 @@ supplyCredits conf c levels =
877877
Merging mp mr -> do
878878
let !c' = scaleCreditsForMerge mp mr c
879879
let !thresh = creditThresholdForLevel conf ln
880-
MR.supplyCredits c' thresh mr
880+
_leftoverCredits <- MR.supplyCredits mr thresh c'
881+
return ()
881882

882883
-- | Scale a number of credits to a number of merge steps to be performed, based
883884
-- on the merging run.
@@ -893,7 +894,7 @@ scaleCreditsForMerge LevelTiering _ (Credits c) =
893894
-- runs come in).
894895
MR.Credits (c * (1 + 4))
895896

896-
scaleCreditsForMerge LevelLevelling (DeRef mr) (Credits c) =
897+
scaleCreditsForMerge LevelLevelling mr (Credits c) =
897898
-- A levelling merge has 1 input run and one resident run, which is (up
898899
-- to) 4x bigger than the others. It needs to be completed before
899900
-- another run comes in.
@@ -903,7 +904,7 @@ scaleCreditsForMerge LevelLevelling (DeRef mr) (Credits c) =
903904
-- worst-case upper bound by looking at the sizes of the input runs.
904905
-- As as result, merge work would/could be more evenly distributed over
905906
-- time when the resident run is smaller than the worst case.
906-
let NumRuns n = MR.mergeNumRuns mr
907+
let NumRuns n = MR.numRuns mr
907908
-- same as division rounding up: ceiling (c * n / 4)
908909
in MR.Credits ((c * n + 3) `div` 4)
909910

@@ -912,4 +913,4 @@ scaleCreditsForMerge LevelLevelling (DeRef mr) (Credits c) =
912913
creditThresholdForLevel :: TableConfig -> LevelNo -> MR.CreditThreshold
913914
creditThresholdForLevel conf (LevelNo _i) =
914915
let AllocNumEntries (NumEntries x) = confWriteBufferAlloc conf
915-
in MR.CreditThreshold x
916+
in MR.CreditThreshold (MR.Credits x)

0 commit comments

Comments
 (0)