From c64672007ceaad78fe61b095a518cd8abad71111 Mon Sep 17 00:00:00 2001 From: husharp Date: Thu, 27 Jun 2024 12:48:47 +0800 Subject: [PATCH] repalce generate Signed-off-by: husharp --- .../realcluster/scheduler_test.go | 2 +- .../simulator/cases/region_split.go | 4 +- tools/pd-simulator/simulator/raft.go | 33 +++----- tools/pd-simulator/simulator/simutil/key.go | 78 +------------------ .../simulator/simutil/key_test.go | 75 ++---------------- 5 files changed, 23 insertions(+), 169 deletions(-) diff --git a/tests/integrations/realcluster/scheduler_test.go b/tests/integrations/realcluster/scheduler_test.go index 0ed6f6c6b76..3b75e6c8c88 100644 --- a/tests/integrations/realcluster/scheduler_test.go +++ b/tests/integrations/realcluster/scheduler_test.go @@ -86,7 +86,7 @@ func TestRegionLabelDenyScheduler(t *testing.T) { regions, err := pdHTTPCli.GetRegions(ctx) re.NoError(err) - re.GreaterOrEqual(len(regions.Regions), 1) + re.NotEmpty(regions.Regions) region1 := regions.Regions[0] err = pdHTTPCli.DeleteScheduler(ctx, schedulers.BalanceLeaderName) diff --git a/tools/pd-simulator/simulator/cases/region_split.go b/tools/pd-simulator/simulator/cases/region_split.go index 8548a6eb619..264d9d9f442 100644 --- a/tools/pd-simulator/simulator/cases/region_split.go +++ b/tools/pd-simulator/simulator/cases/region_split.go @@ -38,7 +38,7 @@ func newRegionSplit(config *sc.SimConfig) *Case { } replica := int(config.ServerConfig.Replication.MaxReplicas) peers := make([]*metapb.Peer, 0, replica) - for j := 1; j < replica; j++ { + for j := 0; j < replica; j++ { peers = append(peers, &metapb.Peer{ Id: simutil.IDAllocator.NextID(), StoreId: uint64((j)%(totalStore-1) + 1), @@ -63,7 +63,7 @@ func newRegionSplit(config *sc.SimConfig) *Case { e := &WriteFlowOnSpotDescriptor{} e.Step = func(int64) map[string]int64 { return map[string]int64{ - "a": 8 * units.MiB, + "foobar": 8 * units.MiB, } } simCase.Events = []EventDescriptor{e} diff --git a/tools/pd-simulator/simulator/raft.go b/tools/pd-simulator/simulator/raft.go index fdd24a2b056..4f890392ca0 100644 --- a/tools/pd-simulator/simulator/raft.go +++ b/tools/pd-simulator/simulator/raft.go @@ -30,13 +30,12 @@ import ( // RaftEngine records all raft information. type RaftEngine struct { syncutil.RWMutex - regionsInfo *core.RegionsInfo - conn *Connection - regionChange map[uint64][]uint64 - regionSplitSize int64 - regionSplitKeys int64 - storeConfig *config.SimConfig - useTiDBEncodedKey bool + regionsInfo *core.RegionsInfo + conn *Connection + regionChange map[uint64][]uint64 + regionSplitSize int64 + regionSplitKeys int64 + storeConfig *config.SimConfig } // NewRaftEngine creates the initialized raft with the configuration. @@ -49,14 +48,7 @@ func NewRaftEngine(conf *cases.Case, conn *Connection, storeConfig *config.SimCo regionSplitKeys: conf.RegionSplitKeys, storeConfig: storeConfig, } - var splitKeys []string - if conf.TableNumber > 0 { - splitKeys = simutil.GenerateTableKeys(conf.TableNumber, len(conf.Regions)-1) - r.useTiDBEncodedKey = true - } else { - splitKeys = simutil.GenerateKeys(len(conf.Regions) - 1) - } - + splitKeys := simutil.GenerateTableKeys(conf.TableNumber, len(conf.Regions)-1) for i, region := range conf.Regions { meta := &metapb.Region{ Id: region.ID, @@ -133,14 +125,9 @@ func (r *RaftEngine) stepSplit(region *core.RegionInfo) { } } - var splitKey []byte - if r.useTiDBEncodedKey { - splitKey, err = simutil.GenerateTiDBEncodedSplitKey(region.GetStartKey(), region.GetEndKey()) - if err != nil { - simutil.Logger.Fatal("Generate TiDB encoded split key failed", zap.Error(err)) - } - } else { - splitKey = simutil.GenerateSplitKey(region.GetStartKey(), region.GetEndKey()) + splitKey, err := simutil.GenerateTiDBEncodedSplitKey(region.GetStartKey(), region.GetEndKey()) + if err != nil { + simutil.Logger.Fatal("Generate TiDB encoded split key failed", zap.Error(err)) } left := region.Clone( core.WithNewRegionID(ids[len(ids)-1]), diff --git a/tools/pd-simulator/simulator/simutil/key.go b/tools/pd-simulator/simulator/simutil/key.go index bda1dbdaddb..a095f9d567b 100644 --- a/tools/pd-simulator/simulator/simutil/key.go +++ b/tools/pd-simulator/simulator/simutil/key.go @@ -16,38 +16,11 @@ package simutil import ( "bytes" - "math/rand" - "sort" "github.com/pingcap/errors" "github.com/tikv/pd/pkg/codec" ) -const ( - // 26^10 ~= 1.4e+14, should be enough. - keyChars = "abcdefghijklmnopqrstuvwxyz" - keyLen = 10 -) - -// GenerateKeys generates ordered, unique strings. -func GenerateKeys(size int) []string { - m := make(map[string]struct{}, size) - for len(m) < size { - k := make([]byte, keyLen) - for i := range k { - k[i] = keyChars[rand.Intn(len(keyChars))] - } - m[string(k)] = struct{}{} - } - - v := make([]string, 0, size) - for k := range m { - v = append(v, k) - } - sort.Strings(v) - return v -} - // GenerateTableKey generates the table key according to the table ID and row ID. func GenerateTableKey(tableID, rowID int64) []byte { key := codec.GenerateRowKey(tableID, rowID) @@ -59,6 +32,10 @@ func GenerateTableKey(tableID, rowID int64) []byte { // GenerateTableKeys generates the table keys according to the table count and size. func GenerateTableKeys(tableCount, size int) []string { + if tableCount <= 0 { + // set default tableCount as 1 + tableCount = 1 + } v := make([]string, 0, size) groupNumber := size / tableCount tableID := 0 @@ -74,53 +51,6 @@ func GenerateTableKeys(tableCount, size int) []string { return v } -// GenerateSplitKey generate the split key. -func GenerateSplitKey(start, end []byte) []byte { - const maxLength = 16 // Define a maximum length for the key - - // If both start and end are empty, return a default middle key - if len(start) == 0 && len(end) == 0 { - key := make([]byte, maxLength) - mid := (byte('a') + byte('z')) / 2 - for i := 0; i < maxLength; i++ { - key[i] = mid - } - return key - } - - maxLen := len(start) - if len(end) > maxLen { - maxLen = len(end) - } - - key := make([]byte, maxLen) - carry := 0 - for i := 0; i < maxLen; i++ { - s, e := byte('a'), byte('z') - if i < len(start) { - s = start[i] - } - if i < len(end) { - e = end[i] - } - - mid := (int(s) + int(e) + carry) / 2 - if (int(s)+int(e)+carry)%2 == 1 { - carry = 1 - } else { - carry = 0 - } - key[i] = byte(mid) - } - - // Add a character if needed to ensure the key is in the correct range - if carry == 1 && len(key) < maxLength { - key = append(key, (byte('a')+byte('z'))/2) - } - - return key -} - func mustDecodeMvccKey(key []byte) ([]byte, error) { // FIXME: seems nil key not encode to order compare key if len(key) == 0 { diff --git a/tools/pd-simulator/simulator/simutil/key_test.go b/tools/pd-simulator/simulator/simutil/key_test.go index be1c23ce4e8..ae00887afa8 100644 --- a/tools/pd-simulator/simulator/simutil/key_test.go +++ b/tools/pd-simulator/simulator/simutil/key_test.go @@ -17,7 +17,6 @@ package simutil import ( "github.com/pingcap/kvproto/pkg/metapb" "github.com/tikv/pd/pkg/core" - "strings" "testing" "github.com/stretchr/testify/require" @@ -99,83 +98,21 @@ func TestGenerateTiDBEncodedSplitKey(t *testing.T) { re.Error(err) } -func TestGenerateKeys(t *testing.T) { - re := require.New(t) - - numKeys := 10 - actual := GenerateKeys(numKeys) - re.Len(actual, numKeys) - - // make sure every key: - // i. has length `keyLen` - // ii. has only characters from `keyChars` - for _, key := range actual { - re.Len(key, keyLen) - for _, char := range key { - re.True(strings.ContainsRune(keyChars, char)) - } - } -} - -func TestGenerateSplitKey(t *testing.T) { - re := require.New(t) - - // empty key - s := []byte("") - e := []byte{116, 128, 0, 0, 0, 0, 0, 0, 255, 1, 0, 0, 0, 0, 0, 0, 0, 248} - splitKey := GenerateSplitKey(s, e) - re.Less(string(s), string(splitKey)) - re.Less(string(splitKey), string(e)) - re.NotEqual(string(splitKey), string(e)) - - // empty key - s = []byte("") - // a is 97, z is 122, (97+122)/2 = 109.5 = 109 = byte("bQ==") - e = []byte("bQ==") - splitKey = GenerateSplitKey(s, e) - re.Less(string(s), string(splitKey)) - re.NotEqual(string(splitKey), string(e)) - - // empty end key - s = []byte{116, 128, 0, 0, 0, 0, 0, 0, 255, 1, 0, 0, 0, 0, 0, 0, 0, 248} - e = []byte("") - splitKey = GenerateSplitKey(s, e) - re.Less(string(s), string(splitKey)) - re.Less(string(e), string(splitKey)) - - // empty start and end keys - s = []byte{} - e = []byte{} - splitKey = GenerateSplitKey(s, e) - re.Less(string(s), string(splitKey)) - re.Less(string(e), string(splitKey)) - - // same start and end keys - s = codec.EncodeBytes([]byte{116, 128, 0, 0, 0, 0, 0, 0, 255, 1, 0, 0, 0, 0, 0, 0, 0, 248}) - e = codec.EncodeBytes([]byte{116, 128, 0, 0, 0, 0, 0, 0, 255, 1, 0, 0, 0, 0, 0, 0, 0, 248}) - splitKey = GenerateSplitKey(s, e) - re.Equal(string(s), string(splitKey)) - re.Equal(string(e), string(splitKey)) - - s = codec.EncodeBytes([]byte{116, 128, 0, 0, 0, 0, 0, 0, 1}) - e = codec.EncodeBytes([]byte{116, 128, 0, 0, 0, 0, 0, 0, 1, 1}) - splitKey = GenerateSplitKey(s, e) - re.Less(string(s), string(splitKey)) - re.Less(string(splitKey), string(e)) -} - func TestRegionSplitKey(t *testing.T) { re := require.New(t) // empty start and end keys var s []byte var e []byte - splitKey := GenerateSplitKey(s, e) + splitKey, err := GenerateTiDBEncodedSplitKey(s, e) + re.NoError(err) // left - leftSplit := GenerateSplitKey(s, splitKey) + leftSplit, err := GenerateTiDBEncodedSplitKey(s, splitKey) + re.NoError(err) re.Less(string(leftSplit), string(splitKey)) - rightSplit := GenerateSplitKey(splitKey, e) + rightSplit, err := GenerateTiDBEncodedSplitKey(splitKey, e) + re.NoError(err) re.Less(string(splitKey), string(rightSplit)) meta := &metapb.Region{