redisはハッシュスロットをどのように割り当てるか
1037 ワード
Redisクラスタには16384個のハシスロットが内蔵されており、Redisクラスタにkey-valueを配置する必要がある場合
場合、redisはkeyに対してcrc 16アルゴリズムを用いて1つの結果を算出し、その後、結果対16384を型取り、
これにより、各keyは0-16383の番号のハシ溝に対応し、redisはノード数に応じて大きくなります.
ハッシュ・スロットを異なるノードに均等にマッピングします.
crc 16アルゴリズム
場合、redisはkeyに対してcrc 16アルゴリズムを用いて1つの結果を算出し、その後、結果対16384を型取り、
これにより、各keyは0-16383の番号のハシ溝に対応し、redisはノード数に応じて大きくなります.
ハッシュ・スロットを異なるノードに均等にマッピングします.
crc 16アルゴリズム
unsigned int keyHashSlot(char *key, int keylen) { #
int s, e; /* start-end indexes of { and } */
for (s = 0; s < keylen; s++)
if (key[s] == '{') break;
/* No '{' ? Hash the whole key. This is the base case. */
if (s == keylen) return crc16(key,keylen) & 0x3FFF;
/* '{' found? Check if we have the corresponding '}'. */
for (e = s+1; e < keylen; e++)
if (key[e] == '}') break;
/* No '}' or nothing betweeen {} ? Hash the whole key. */
if (e == keylen || e == s+1) return crc16(key,keylen) & 0x3FFF;
/* If we are here there is both a { and a } on its right. Hash
* what is in the middle between { and }. */
return crc16(key+s+1,e-s-1) & 0x3FFF;
}