From a2f9f0f9fdcbb889ec1b14503f50582c364c09b3 Mon Sep 17 00:00:00 2001 From: nolouch Date: Fri, 12 Oct 2018 16:55:14 +0800 Subject: [PATCH] fix generate key --- tools/pd-simulator/simulator/raft.go | 18 ++++++++++-------- tools/pd-simulator/simulator/raft_test.go | 13 ++++++++----- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/tools/pd-simulator/simulator/raft.go b/tools/pd-simulator/simulator/raft.go index 8cb36fa74e66..fb38e8eb4094 100644 --- a/tools/pd-simulator/simulator/raft.go +++ b/tools/pd-simulator/simulator/raft.go @@ -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) } @@ -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 { @@ -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-- { @@ -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.Compare(end, start) == 0 { + end = append(end, 0xFF) + } return table.EncodeBytes(end) } diff --git a/tools/pd-simulator/simulator/raft_test.go b/tools/pd-simulator/simulator/raft_test.go index 64a44c1ddab0..544d04590d4f 100644 --- a/tools/pd-simulator/simulator/raft_test.go +++ b/tools/pd-simulator/simulator/raft_test.go @@ -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 + } }