Skip to content

Commit 1ad6513

Browse files
committed
Change the MergingRun credit tracking strategy
The intention is to simplify things and make them more obviously correct in the presence of concurrency. This should have the bonus of making it easier to reliably determine leftover/excess supplied credits (which is something we want for supplying credits to trees of merging runs). The approach is to change the counters from three independent counters, to just two, which are modified together as a pair atomically. Previously we tracked the credits spent and unspent, and the steps performed. We did not explicitly keep track of credits that were in the process of being spent. Now we track spent and unspent (and not steps performed), but the spent credits includes those that are in the process of being spent. We keep these together in a single atomic variable, and so all operations on the pair are atomic. This makes the concurrency story much simpler because all credit tracking changes are atomic. We avoid having to track steps performed by accounting differently for the difference between credits used for merging and steps performed: we simply borrow more credits from the unspent pot, allowing the pot to become negative.
1 parent c59cd57 commit 1ad6513

File tree

6 files changed

+537
-370
lines changed

6 files changed

+537
-370
lines changed

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

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

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

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

src/Database/LSMTree/Internal/MergeSchedule.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ addRunToLevels tr conf@TableConfig{..} resolve hfs hbio root uc r0 reg levels =
673673
OneShot -> do
674674
let !required = MR.Credits (unNumEntries (V.foldMap' Run.size rs))
675675
let !thresh = creditThresholdForLevel conf ln
676-
MR.supplyCredits required thresh mr
676+
MR.supplyCredits mr thresh required
677677
-- This ensures the merge is really completed. However, we don't
678678
-- release the merge yet and only briefly inspect the resulting run.
679679
bracket (MR.expectCompleted mr) releaseRef $ \r ->
@@ -802,7 +802,7 @@ supplyCredits conf c levels =
802802
Merging mp mr -> do
803803
let !c' = scaleCreditsForMerge mp mr c
804804
let !thresh = creditThresholdForLevel conf ln
805-
MR.supplyCredits c' thresh mr
805+
MR.supplyCredits mr thresh c'
806806

807807
-- | Scale a number of credits to a number of merge steps to be performed, based
808808
-- on the merging run.
@@ -837,4 +837,4 @@ scaleCreditsForMerge LevelLevelling (DeRef mr) (Credits c) =
837837
creditThresholdForLevel :: TableConfig -> LevelNo -> MR.CreditThreshold
838838
creditThresholdForLevel conf (LevelNo _i) =
839839
let AllocNumEntries (NumEntries x) = confWriteBufferAlloc conf
840-
in MR.CreditThreshold x
840+
in MR.CreditThreshold (MR.Credits x)

0 commit comments

Comments
 (0)