informatikr / hedis

A Redis client library for Haskell.
http://hackage.haskell.org/package/hedis
BSD 3-Clause "New" or "Revised" License
327 stars 121 forks source link

Mismatched HashSlot response from `keyToSlot` #203

Closed shivaraj-bh closed 1 year ago

shivaraj-bh commented 1 year ago

In certain scenarios, the output of keyToSlot does not align with Redis' perception of the hash value for a given key. For instance, consider the bytestring "{\155\247\234j\171\230\210>K". According to keyToSlot, the output is HashSlot 8700, which, in my situation, corresponds to master node 2. However, when attempting to execute the set command on that node, it fails with a MOVED response. Redis identifies this key as belonging to master node 1. This issue appears to arise specifically when working with special characters in a bytestring.

qnikst commented 1 year ago

Ok, seems I've got the case where we have a problem:

hedis ignores symbol { even it does not have a matching one:

ghci> keyToSlot "{\155\247\234j\171\230\210>K"
HashSlot 8700
ghci> keyToSlot "\155\247\234j\171\230\210>K"
HashSlot 8700

While in redis:

127.0.0.1:52922> EVAL "local x = redis.pcall('cluster', 'keyslot', '\x7b\x9b\xf7\xea\x6a\xab\xe6\xd2\x3e\x4b')\nreturn x" 0
(integer) 1529
127.0.0.1:52922> EVAL "local x = redis.pcall('cluster', 'keyslot', '\x9b\xf7\xea\x6a\xab\xe6\xd2\x3e\x4b')\nreturn x" 0
(integer) 8700

Seems there is a bug in condition in keyToSlot that I'll try to fix

qnikst commented 1 year ago

Thanks again for the report!

The issue is fixed feel free to check if it works for you

There are few API breakers comming around to it's theoretically possible to port the fix onto the latest 0.15 release. Please ping me if it's required for your use case.

shivaraj-bh commented 1 year ago

Thanks for the quick response and fix!