Skip to content

Commit

Permalink
config: fix the panic caused by zero RegionSplitSizeMB (#8324) (#8329)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ti-chi-bot and JmPotato committed Jun 26, 2024
1 parent 1d2b891 commit ea57f5e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
17 changes: 13 additions & 4 deletions pkg/typeutil/size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package typeutil
import (
"encoding/json"

"github.com/docker/go-units"
. "github.com/pingcap/check"
)

Expand All @@ -41,23 +42,31 @@ func (s *testSizeSuite) TestJSON(c *C) {
}

func (s *testSizeSuite) TestParseMbFromText(c *C) {
const defaultValue = 2

testdata := []struct {
body []string
size uint64
}{{
body: []string{"10Mib", "10MiB", "10M", "10MB"},
size: uint64(10),
size: 10,
}, {
body: []string{"10GiB", "10Gib", "10G", "10GB"},
size: uint64(10 * 1024),
size: 10 * units.GiB / units.MiB,
}, {
body: []string{"1024KiB", "1048576"},
size: 1,
}, {
body: []string{"100KiB", "1023KiB", "1048575", "0"},
size: 0,
}, {
body: []string{"10yiB", "10aib"},
size: uint64(1),
size: defaultValue,
}}

for _, t := range testdata {
for _, b := range t.body {
c.Assert(int(ParseMBFromText(b, 1)), Equals, int(t.size))
c.Assert(ParseMBFromText(b, defaultValue), Equals, t.size)
}
}
}
9 changes: 7 additions & 2 deletions server/config/store_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,14 @@ func (c *StoreConfig) CheckRegionSize(size, mergeSize uint64) error {
if size < c.GetRegionMaxSize() {
return nil
}

// This could happen when the region split size is set to a value less than 1MiB,
// which is a very extreme case, we just pass the check here to prevent panic.
regionSplitSize := c.GetRegionSplitSize()
if regionSplitSize == 0 {
return nil
}
// the smallest of the split regions can not be merge again, so it's size should less merge size.
if smallSize := size % c.GetRegionSplitSize(); smallSize <= mergeSize && smallSize != 0 {
if smallSize := size % regionSplitSize; smallSize <= mergeSize && smallSize != 0 {
log.Debug("region size is too small", zap.Uint64("size", size), zap.Uint64("merge-size", mergeSize), zap.Uint64("small-size", smallSize))
return errs.ErrCheckerMergeAgain.FastGenByArgs("the smallest region of the split regions is less than max-merge-region-size, " +
"it will be merged again")
Expand Down
4 changes: 4 additions & 0 deletions server/config/store_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,8 @@ func (t *testTiKVConfigSuite) TestMergeCheck(c *C) {
c.Assert(config.CheckRegionKeys(v.keys, v.mergeKeys), NotNil)
}
}
// Test CheckRegionSize when the region split size is 0.
config.RegionSplitSize = "100KiB"
c.Assert(config.GetRegionSplitSize(), Equals, uint64(0))
c.Assert(config.CheckRegionSize(defaultRegionMaxSize, 50), IsNil)
}

0 comments on commit ea57f5e

Please sign in to comment.