@@ -1048,8 +1048,37 @@ supplyCreditsLevels nominalDeposit =
1048
1048
when (physicalDeposit > 0 ) $ do
1049
1049
leftoverCredits <- supplyCreditsMergingRun physicalDeposit mr
1050
1050
-- 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.
1053
1082
1054
1083
scaleNominalToPhysicalCredit ::
1055
1084
NominalDebt
0 commit comments