Skip to content

Commit

Permalink
repalce generate
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp committed Jun 27, 2024
1 parent 6ae99b8 commit c646720
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 169 deletions.
2 changes: 1 addition & 1 deletion tests/integrations/realcluster/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tools/pd-simulator/simulator/cases/region_split.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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}
Expand Down
33 changes: 10 additions & 23 deletions tools/pd-simulator/simulator/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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]),
Expand Down
78 changes: 4 additions & 74 deletions tools/pd-simulator/simulator/simutil/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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 {
Expand Down
75 changes: 6 additions & 69 deletions tools/pd-simulator/simulator/simutil/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit c646720

Please sign in to comment.