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

Should prefixNext(0xFF) return "" instead of 0xFF00? #12598

Open
disksing opened this issue Oct 10, 2019 · 7 comments
Open

Should prefixNext(0xFF) return "" instead of 0xFF00? #12598

disksing opened this issue Oct 10, 2019 · 7 comments
Labels
component/tikv type/enhancement The issue or PR belongs to an enhancement.

Comments

@disksing
Copy link
Contributor

Typically, we use prefixNext(x) to calculate the upper bound when we want to scan all keys have prefix x.

In other word, the range [x, prefixNext(x)) should contain all keys that start with x.

For the specific key 0xFF, all other keys that are equal or greater than it start with 0xFF, so the upper bound should be +inf. Then it leads to the conclusion that prefixNext(0xFF) should return "" (represents +inf when it is the range end key).

This issue does not affect any usage in tidb. But if someone use txn API and try to scan keys prefixed with 0xFF, it will fail.

Current implementation:

tidb/kv/key.go

Lines 43 to 58 in c0d6185

func (k Key) PrefixNext() Key {
buf := make([]byte, len(k))
copy(buf, []byte(k))
var i int
for i = len(k) - 1; i >= 0; i-- {
buf[i]++
if buf[i] != 0 {
break
}
}
if i == -1 {
copy(buf, k)
buf = append(buf, 0)
}
return buf
}

@disksing disksing added the type/question The issue belongs to a question. label Oct 10, 2019
@disksing
Copy link
Contributor Author

/cc @coocood

@coocood
Copy link
Member

coocood commented Oct 10, 2019

Another option is to return strings.Repeat(0xFF, maxKeyLen).

@coocood
Copy link
Member

coocood commented Oct 10, 2019

In practice set maxKeyLen to 8 can solve 99% of the problems.

@disksing
Copy link
Contributor Author

I think it is a good idea too.

@disksing disksing changed the title Should prefixNext(0xFF) return "" instead of 0xFF0x00? Should prefixNext(0xFF) return "" instead of 0xFF00? Oct 10, 2019
@XuHuaiyu
Copy link
Contributor

@coocood Does this problem still exist?
If so, please assign an assignee for this issue~

@coocood
Copy link
Member

coocood commented Mar 30, 2020

Yes, but we are not sure if it needs to be fixed.

@zz-jason zz-jason added type/enhancement The issue or PR belongs to an enhancement. component/tikv and removed type/question The issue belongs to a question. labels Mar 30, 2020
@shafreeck
Copy link
Contributor

@coocood I think it is a serious bug that needs to be fixed. distributed/titan which is a Redis layer above TiKV uses PrefixNext often, may meet the bug unexpectedly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/tikv type/enhancement The issue or PR belongs to an enhancement.
Projects
None yet
Development

No branches or pull requests

5 participants