Skip to content

Commit 233109c

Browse files
ti-chi-botJmPotato
andauthored
config: fix the panic caused by zero RegionSplitSizeMB (#8324) (#8326)
close #8323 Bypass the case that `RegionSplitSizeMB` might be zero in `(*StoreConfig) CheckRegionSize`. Signed-off-by: ti-chi-bot <[email protected]> Signed-off-by: JmPotato <[email protected]> Co-authored-by: JmPotato <[email protected]>
1 parent 2807999 commit 233109c

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

pkg/schedule/config/store_config.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,14 @@ func (c *StoreConfig) CheckRegionSize(size, mergeSize uint64) error {
166166
if size < c.GetRegionMaxSize() {
167167
return nil
168168
}
169-
169+
// This could happen when the region split size is set to a value less than 1MiB,
170+
// which is a very extreme case, we just pass the check here to prevent panic.
171+
regionSplitSize := c.GetRegionSplitSize()
172+
if regionSplitSize == 0 {
173+
return nil
174+
}
170175
// the smallest of the split regions can not be merge again, so it's size should less merge size.
171-
if smallSize := size % c.GetRegionSplitSize(); smallSize <= mergeSize && smallSize != 0 {
176+
if smallSize := size % regionSplitSize; smallSize <= mergeSize && smallSize != 0 {
172177
log.Debug("region size is too small", zap.Uint64("size", size), zap.Uint64("merge-size", mergeSize), zap.Uint64("small-size", smallSize))
173178
return errs.ErrCheckerMergeAgain.FastGenByArgs("the smallest region of the split regions is less than max-merge-region-size, " +
174179
"it will be merged again")

pkg/schedule/config/store_config_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,9 @@ func TestMergeCheck(t *testing.T) {
6767
re.Error(config.CheckRegionKeys(v.keys, v.mergeKeys))
6868
}
6969
}
70+
// Test CheckRegionSize when the region split size is 0.
71+
config.RegionSplitSize = "100KiB"
72+
config.Adjust()
73+
re.Empty(config.GetRegionSplitSize())
74+
re.NoError(config.CheckRegionSize(defaultRegionMaxSize, 50))
7075
}

pkg/utils/typeutil/size_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,33 @@ func TestSizeJSON(t *testing.T) {
4040
}
4141

4242
func TestParseMbFromText(t *testing.T) {
43+
const defaultValue = 2
44+
4345
t.Parallel()
4446
re := require.New(t)
4547
testCases := []struct {
4648
body []string
4749
size uint64
4850
}{{
4951
body: []string{"10Mib", "10MiB", "10M", "10MB"},
50-
size: uint64(10),
52+
size: 10,
5153
}, {
5254
body: []string{"10GiB", "10Gib", "10G", "10GB"},
53-
size: uint64(10 * units.GiB / units.MiB),
55+
size: 10 * units.GiB / units.MiB,
56+
}, {
57+
body: []string{"1024KiB", "1048576"},
58+
size: 1,
59+
}, {
60+
body: []string{"100KiB", "1023KiB", "1048575", "0"},
61+
size: 0,
5462
}, {
5563
body: []string{"10yiB", "10aib"},
56-
size: uint64(1),
64+
size: defaultValue,
5765
}}
5866

5967
for _, testCase := range testCases {
6068
for _, b := range testCase.body {
61-
re.Equal(int(testCase.size), int(ParseMBFromText(b, 1)))
69+
re.Equal(testCase.size, ParseMBFromText(b, defaultValue))
6270
}
6371
}
6472
}

0 commit comments

Comments
 (0)