Skip to content

Commit 88634cc

Browse files
committed
review: add comment about race condition
1 parent e73b0a1 commit 88634cc

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

prototypes/ScheduledMerges.hs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,8 +1048,37 @@ supplyCreditsLevels nominalDeposit =
10481048
when (physicalDeposit > 0) $ do
10491049
leftoverCredits <- supplyCreditsMergingRun physicalDeposit mr
10501050
-- For merges at ordinary levels (not unions) we expect to hit the
1051-
-- debt limit exactly and never exceed it.
1052-
assert (leftoverCredits == 0) $ return ()
1051+
-- debt limit exactly and not exceed it. However if we had a race
1052+
-- on supplying credit then we could go over (which is not a problem).
1053+
-- We can detect such races if the credit afterwards is not the amount
1054+
-- that we credited. This is all just for sanity checking.
1055+
physicalCredit'' <- suppliedCreditMergingRun mr
1056+
assert (leftoverCredits == 0 || physicalCredit' /= physicalCredit'')
1057+
(return ())
1058+
1059+
-- There is a potential race here in between deciding how much physical
1060+
-- credit to supply, and then supplying it. That's because we read the
1061+
-- "current" (absolute) physical credits, decide how much extra
1062+
-- (relative) credits to supply and then do the transaction to supply
1063+
-- the extra (relative) credits. In between the reading and supplying
1064+
-- the current (absolute) physical credits could have changed due to
1065+
-- another thread doing a merge on a different table handle.
1066+
--
1067+
-- This race is relatively benign. When it happens, we will supply more
1068+
-- credit to the merge than either thread intended, however, next time
1069+
-- either thread comes round they'll find the merge has more physical
1070+
-- credits and will thus supply less or none. The only minor problem is
1071+
-- in asserting that we don't supply more physical credits than the
1072+
-- debt limit.
1073+
1074+
-- There is a trade-off, we could supply absolute physical credit to
1075+
-- the merging run, and let it calculate the relative credit as part
1076+
-- of the credit transaction. However, we would also need to support
1077+
-- relative credit for the union merges, which do not have any notion
1078+
-- of nominal credit and only work in terms of relative physical credit.
1079+
-- So we can have a simple relative physical credit and rare benign
1080+
-- races, or a more complex scheme for contributing physical credits
1081+
-- either as absolute or relative values.
10531082

10541083
scaleNominalToPhysicalCredit ::
10551084
NominalDebt

0 commit comments

Comments
 (0)