kvrocks icon indicating copy to clipboard operation
kvrocks copied to clipboard

Prefix scan didn't work well when enabling the cluster mode

Open EasyProgramming opened this issue 2 years ago • 8 comments

Search before asking

  • [X] I had searched in the issues and found no similar issues.

Version

centos 7.9

Kvrocks was running with two cluster nodes and slot distributions are 0-8191 and 8192-16383 (kvrocks 2.6.0 内含两个节点的集群,slot分别为0-8191、8192-16383)

Minimal reproduce step

8d1f97b4c485dbad7b462b4c0b0022f

What did you expect to see?

return keys should start with f character.

返回以f开头的key

What did you see instead?

Anything Else?

1.触发场景可能与大量的key被删除有关,因为当时的场景就是删除大量的key 2.依稀记得当时执行scan的时候,在程序里面超时了;然后我手动在redis-cli里面执行,响应速度也很慢

Are you willing to submit a PR?

  • [ ] I'm willing to submit a PR!

EasyProgramming avatar Oct 19 '23 04:10 EasyProgramming

@EasyProgramming could you provide a complete list of commands execute after a FLUSHDB so that we can reproduce locally?

tisonkun avatar Oct 19 '23 05:10 tisonkun

@EasyProgramming It's a limitation in the scan command when enabling the cluster mode. It'll only look forward N(50) slots when scanning keys, for example:

If we have keys: aaaa(slot 0), bbbb(slot 10), cccc(slot 100), dddd(slot 200) and we scan with:

SCAN "0" MATCH "bb*"

it works well since bbbb is located at slot 10, and Kvrocks try to look forward next 50 slots. But if you scan with cc*:

SCAN "0" MATCH "cc*"

it will return empty array with cursor(_bbbb) since cccc is located at slot 100.

For the implementation, please refer: https://github.com/apache/kvrocks/blob/unstable/src/storage/redis_db.cc#L365

Of course, for this limitation will make the scan command very hard to use. We will try our best to improve this if possible. cc @tisonkun @PragmaTwice

git-hulk avatar Oct 19 '23 13:10 git-hulk

Could we make HASH_SLOTS_MAX_ITERATIONS a configurable constant?

PragmaTwice avatar Oct 19 '23 13:10 PragmaTwice

@git-hulk 我这边使用Java的lettuce连接的kvrocks,看了lettuce对scan、hscan、sscan的实现方式,lettuce是以游标返回的是否为字符0来判断是否扫描完成的,所以想问一下,kvrocks这块关于scan、hscan、sscan扫描完成的标志为是否也是字符0呢,这样的话,对于我这边的程序来讲还是可以接受的

EasyProgramming avatar Oct 20 '23 02:10 EasyProgramming

Could we make HASH_SLOTS_MAX_ITERATIONS a configurable constant?

Yes, but this cannot resolve this problem unless they set it to MAX_HASH_SLOTS.

git-hulk avatar Oct 20 '23 02:10 git-hulk

@git-hulk 我这边使用Java的lettuce连接的kvrocks,看了lettuce对scan、hscan、sscan的实现方式,lettuce是以游标返回的是否为字符0来判断是否扫描完成的,所以想问一下,kvrocks这块关于scan、hscan、sscan扫描完成的标志为是否也是字符0呢,这样的话,对于我这边的程序来讲还是可以接受的

Yes, you can continue iterating if the cursor is NOT "0".

git-hulk avatar Oct 20 '23 02:10 git-hulk

Yes, but this cannot resolve this problem unless they set it to MAX_HASH_SLOTS.

But it is the only workaround I can come up with now, for both being compatible with currently behavior and solving the issue by manually setting the option.

PragmaTwice avatar Oct 20 '23 16:10 PragmaTwice

The user can work around this by continuing the scan if the cursor is not "0", it will start with the new slot id instead of 0.

git-hulk avatar Oct 21 '23 01:10 git-hulk