Skip to content

Commit 9e98ae8

Browse files
committed
asim/state: use epsilon in cumulative weights checking
Previously, we asserted that cumulative weights in the weighted random distribution must sum to exactly 1. Due to floating point precision limitations, this is not always achievable and can lead to false failures like total cumulative weights for all stores should sum up to 1 but got 1.00. This commit relaxes the check to allow a small epsilon tolerance in the comparison to avoid such errors.
1 parent f350341 commit 9e98ae8

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

pkg/kv/kvserver/asim/state/new_state.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ func newWeighted(weightedStores []float64) weighted {
102102
prefixSumWeight += item
103103
cumulativeWeights[i] = prefixSumWeight
104104
}
105-
if cumulativeWeights[len(weightedStores)-1] != float64(1) {
105+
const epsilon = 1e-10
106+
if math.Abs(cumulativeWeights[len(weightedStores)-1]-float64(1)) > epsilon {
106107
panic(fmt.Sprintf("total cumulative weights for all stores should sum up to 1 but got %.2f\n",
107108
cumulativeWeights[len(weightedStores)-1]))
108109
}

0 commit comments

Comments
 (0)