Skip to content

Commit

Permalink
This is an automated cherry-pick of tikv#8324
Browse files Browse the repository at this point in the history
close tikv#8323

Signed-off-by: ti-chi-bot <[email protected]>
  • Loading branch information
JmPotato authored and ti-chi-bot committed Jun 25, 2024
1 parent 1d2b891 commit ee24057
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 4 deletions.
75 changes: 75 additions & 0 deletions pkg/schedule/config/store_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2022 TiKV Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package config

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestMergeCheck(t *testing.T) {
re := require.New(t)
testdata := []struct {
size uint64
mergeSize uint64
keys uint64
mergeKeys uint64
pass bool
}{{
// case 1: the merged region size is smaller than the max region size
size: 96 + 20,
mergeSize: 20,
keys: 1440000 + 200000,
mergeKeys: 200000,
pass: true,
}, {
// case 2: the smallest region is 68MiB, it can't be merged again.
size: 144 + 20,
mergeSize: 20,
keys: 1440000 + 200000,
mergeKeys: 200000,
pass: true,
}, {
// case 3: the smallest region is 50MiB, it can be merged again.
size: 144 + 2,
mergeSize: 50,
keys: 1440000 + 20000,
mergeKeys: 500000,
pass: false,
}, {
// case4: the smallest region is 51MiB, it can't be merged again.
size: 144 + 3,
mergeSize: 50,
keys: 1440000 + 30000,
mergeKeys: 500000,
pass: true,
}}
config := &StoreConfig{}
for _, v := range testdata {
if v.pass {
re.NoError(config.CheckRegionSize(v.size, v.mergeSize))
re.NoError(config.CheckRegionKeys(v.keys, v.mergeKeys))
} else {
re.Error(config.CheckRegionSize(v.size, v.mergeSize))
re.Error(config.CheckRegionKeys(v.keys, v.mergeKeys))
}
}
// Test CheckRegionSize when the region split size is 0.
config.RegionSplitSize = "100KiB"
config.Adjust()
re.Empty(config.GetRegionSplitSize())
re.NoError(config.CheckRegionSize(defaultRegionMaxSize, 50))
}
28 changes: 26 additions & 2 deletions pkg/typeutil/size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,48 @@ func (s *testSizeSuite) TestJSON(c *C) {
c.Assert(string(o), Equals, `"1.598TiB"`)
}

<<<<<<< HEAD:pkg/typeutil/size_test.go

Check failure on line 43 in pkg/typeutil/size_test.go

View workflow job for this annotation

GitHub Actions / statics

expected declaration, found '<<'
func (s *testSizeSuite) TestParseMbFromText(c *C) {
testdata := []struct {
=======
func TestParseMbFromText(t *testing.T) {
const defaultValue = 2

re := require.New(t)

Check failure on line 50 in pkg/typeutil/size_test.go

View workflow job for this annotation

GitHub Actions / statics

expected declaration, found re
testCases := []struct {
>>>>>>> 6fbe73796 (config: fix the panic caused by zero RegionSplitSizeMB (#8324)):pkg/utils/typeutil/size_test.go

Check failure on line 52 in pkg/typeutil/size_test.go

View workflow job for this annotation

GitHub Actions / statics

illegal character U+0023 '#'
body []string
size uint64
}{{
body: []string{"10Mib", "10MiB", "10M", "10MB"},
size: uint64(10),
size: 10,
}, {
body: []string{"10GiB", "10Gib", "10G", "10GB"},
<<<<<<< HEAD:pkg/typeutil/size_test.go
size: uint64(10 * 1024),
=======
size: 10 * units.GiB / units.MiB,
}, {
body: []string{"1024KiB", "1048576"},
size: 1,
}, {
body: []string{"100KiB", "1023KiB", "1048575", "0"},
size: 0,
>>>>>>> 6fbe73796 (config: fix the panic caused by zero RegionSplitSizeMB (#8324)):pkg/utils/typeutil/size_test.go

Check failure on line 70 in pkg/typeutil/size_test.go

View workflow job for this annotation

GitHub Actions / statics

illegal character U+0023 '#'
}, {
body: []string{"10yiB", "10aib"},
size: uint64(1),
size: defaultValue,
}}

<<<<<<< HEAD:pkg/typeutil/size_test.go
for _, t := range testdata {
for _, b := range t.body {
c.Assert(int(ParseMBFromText(b, 1)), Equals, int(t.size))
=======
for _, testCase := range testCases {
for _, b := range testCase.body {
re.Equal(testCase.size, ParseMBFromText(b, defaultValue))
>>>>>>> 6fbe73796 (config: fix the panic caused by zero RegionSplitSizeMB (#8324)):pkg/utils/typeutil/size_test.go

Check failure on line 84 in pkg/typeutil/size_test.go

View workflow job for this annotation

GitHub Actions / statics

illegal character U+0023 '#'
}
}
}
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

0 comments on commit ee24057

Please sign in to comment.