Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

txnkv: fix the issue that deleteRange cannot use nil as endkey #429

Merged
merged 1 commit into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion integration_tests/delete_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (s *testDeleteRangeSuite) deleteRange(startKey []byte, endKey []byte) int {
func deleteRangeFromMap(m map[string]string, startKey []byte, endKey []byte) {
for keyStr := range m {
key := []byte(keyStr)
if bytes.Compare(startKey, key) <= 0 && bytes.Compare(key, endKey) < 0 {
if bytes.Compare(startKey, key) <= 0 && (len(endKey) == 0 || bytes.Compare(key, endKey) < 0) {
delete(m, keyStr)
}
}
Expand Down Expand Up @@ -160,4 +160,5 @@ func (s *testDeleteRangeSuite) TestDeleteRange() {
s.mustDeleteRange([]byte("d0\x00"), []byte("d1\x00"), testData, 1)
s.mustDeleteRange([]byte("c5"), []byte("d5"), testData, 2)
s.mustDeleteRange([]byte("a"), []byte("z"), testData, 4)
s.mustDeleteRange(nil, nil, testData, 4)
}
6 changes: 5 additions & 1 deletion internal/mockstore/mocktikv/mvcc_leveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,11 @@ func (mvcc *MVCCLevelDB) GC(startKey, endKey []byte, safePoint uint64) error {

// DeleteRange implements the MVCCStore interface.
func (mvcc *MVCCLevelDB) DeleteRange(startKey, endKey []byte) error {
return mvcc.doRawDeleteRange(codec.EncodeBytes(nil, startKey), codec.EncodeBytes(nil, endKey))
var end []byte
if len(endKey) > 0 {
end = codec.EncodeBytes(nil, endKey)
}
Comment on lines +1541 to +1544
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not do it for start key too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is innecessary. There will be no key located before "" or encode("") so encode or not encode both are fine.

return mvcc.doRawDeleteRange(codec.EncodeBytes(nil, startKey), end)
}

// Close calls leveldb's Close to free resources.
Expand Down
8 changes: 6 additions & 2 deletions txnkv/rangetask/delete_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (t *DeleteRangeTask) sendReqOnRange(ctx context.Context, r kv.KeyRange) (Ta
default:
}

if bytes.Compare(startKey, rangeEndKey) >= 0 {
if len(rangeEndKey) > 0 && bytes.Compare(startKey, rangeEndKey) >= 0 {
break
}

Expand All @@ -135,8 +135,9 @@ func (t *DeleteRangeTask) sendReqOnRange(ctx context.Context, r kv.KeyRange) (Ta

// Delete to the end of the region, except if it's the last region overlapping the range
endKey := loc.EndKey
isLast := len(endKey) == 0 || (len(rangeEndKey) > 0 && bytes.Compare(endKey, rangeEndKey) >= 0)
// If it is the last region
if loc.Contains(rangeEndKey) {
if isLast {
endKey = rangeEndKey
}

Expand Down Expand Up @@ -169,6 +170,9 @@ func (t *DeleteRangeTask) sendReqOnRange(ctx context.Context, r kv.KeyRange) (Ta
return stat, errors.Errorf("unexpected delete range err: %v", err)
}
stat.CompletedRegions++
if isLast {
break
}
startKey = endKey
}

Expand Down