Skip to content

Commit

Permalink
fix generate key
Browse files Browse the repository at this point in the history
  • Loading branch information
nolouch committed Oct 12, 2018
1 parent 5f1e4ef commit 63a2c52
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
18 changes: 10 additions & 8 deletions tools/pd-simulator/simulator/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,8 @@ func generateKeys(size int) []string {

func generateTableKey(tableID, rowID int64) []byte {
key := table.GenerateRowKey(tableID, rowID)
// To try not to split the same keys as much as possible
for i := 0; i <= keyLen; i++ {
key = append(key, byte(rand.Intn(0xFF)))
}
// append 0xFF use to split
key = append(key, 0xFF)

return table.EncodeBytes(key)
}
Expand Down Expand Up @@ -373,6 +371,7 @@ func generateTiDBEncodedSplitKey(start, end []byte) []byte {

start = mustDecodeMvccKey(start)
end = mustDecodeMvccKey(end)
originStartLen := len(start)

// make the start key and end key in same length.
if len(end) == 0 {
Expand All @@ -389,10 +388,8 @@ func generateTiDBEncodedSplitKey(start, end []byte) []byte {
}

switch bytes.Compare(start, end) {
case 0:
return table.EncodeBytes(start)
case 1:
simutil.Logger.Fatalf("invalid start key: %v end key: %v", start, end)
case 0, 1:
simutil.Logger.Fatalf("invalid start key(decode): %v end key(decode): %v", start[:originStartLen], end)
case -1:
}
for i := len(end) - 1; i >= 0; i-- {
Expand All @@ -406,5 +403,10 @@ func generateTiDBEncodedSplitKey(start, end []byte) []byte {
break
}
}
// if endKey equal to startKey after reduce 1.
// we append 0xFF to the split key
if bytes.Equal(end, start) {
end = append(end, 0xFF)
}
return table.EncodeBytes(end)
}
19 changes: 11 additions & 8 deletions tools/pd-simulator/simulator/raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ func (t *testTableKeySuite) TestGenerateTableKeys(c *C) {
func (t *testTableKeySuite) TestGenerateSplitKey(c *C) {
s := []byte(table.EncodeBytes([]byte("a")))
e := []byte(table.EncodeBytes([]byte("ab")))
for i := 0; i <= 100; i++ {
for i := 0; i <= 1000; i++ {
cc := generateTiDBEncodedSplitKey(s, e)
c.Assert(s, LessEqual, cc)
c.Assert(cc, LessEqual, e)
c.Assert(s, Less, cc)
c.Assert(cc, Less, e)
e = cc
}

Expand All @@ -67,10 +67,13 @@ func (t *testTableKeySuite) TestGenerateSplitKey(c *C) {

// split equal key
s = table.EncodeBytes([]byte{116, 128, 0, 0, 0, 0, 0, 0, 1})
e = table.EncodeBytes([]byte{116, 128, 0, 0, 0, 0, 0, 0, 1, 0})
c.Assert(s, Less, e)
splitKey = generateTiDBEncodedSplitKey(s, e)
c.Assert(s, Less, splitKey)
c.Assert(splitKey, LessEqual, e)
e = table.EncodeBytes([]byte{116, 128, 0, 0, 0, 0, 0, 0, 1, 1})
for i := 0; i <= 1000; i++ {
c.Assert(s, Less, e)
splitKey = generateTiDBEncodedSplitKey(s, e)
c.Assert(s, Less, splitKey)
c.Assert(splitKey, Less, e)
e = splitKey
}

}

0 comments on commit 63a2c52

Please sign in to comment.