开始之前我们提出几个问题:
什么是redis cluster ?
keyHashSlot 函数用来计算给定键应该被分配到哪个槽采用分片取模算法实现节点分配,节点数据存储。
/* --------------cluster.c---------------------------------------------------------------
* Key space handling
* -------------------------------------------------------------------------- */
/* We have 16384 hash slots. The hash slot of a given key is obtained
* as the least significant 14 bits of the crc16 of the key.
*
* However if the key contains the {...} pattern, only the part between
* { and } is hashed. This may be useful in the future to force certain
* keys to be in the same node (assuming no resharding is in progress). */
//我们有16384个哈希槽,将会用crc16得到的最小有效14位作为给定key的哈希槽。
//但是,如果键包含‘{...}’这样的字段,只有在"{“和“}”之间的才会被哈希。这在将来可能有助于将某些键强制放在同一个节点中(假设没有进行重新分片
// 计算给定键应该被分配到哪个槽
unsigned int keyHashSlot(char *key, int keylen) {
int s, e; /* start-end indexes of { and } */
for (s = 0; s < keylen; s++)
if (key[s] == '{')