简易流程:
看完这两篇,对Redis渐进式rehash有一个疑问:如果Redis 里的 渐进式rehash 是根据访问老字典的数据来做数据迁移到新字典,那么老字典的某些很少访问到的数据应该怎么迁移到新数据?
源码级别详细:
/* Our hash table implementation performs rehashing incrementally while
* we write/read from the hash table. Still if the server is idle, the hash
* table will use two tables for a long time. So we try to use 1 millisecond
* of CPU time at every call of this function to perform some rehahsing.
*
* 虽然服务器在对数据库执行读取/写入命令时会对数据库进行渐进式 rehash ,
* 但如果服务器长期没有执行命令的话,数据库字典的 rehash 就可能一直没办法完成,
* 为了防止出现这种情况,我们需要对数据库执行主动 rehash 。
*
* The function returns 1 if some rehashing was performed, otherwise 0
* is returned.
*
* 函数在执行了主动 rehash 时返回 1 ,否则返回 0 。
*/
int incrementallyRehash(int dbid) {
/* Keys dictionary */
if (dictIsRehashing(server.db[dbid].dict)) {
dictRehashMilliseconds(server.db[dbid].dict,1);
return 1; /* already used our millisecond for this loop... */
}
/* Expires */
if (dictIsRehashing(server.db[dbid].expires)) {
dictRehashMilliseconds(server.db[dbid].expires,1);
return 1; /* already used our millisecond for this loop... */
}
return 0;
}
除了在渐进式rehash中,通过改查的方式不断地将命中的老字典元素迁移到新字典之外,redis还有一种主动rehash(active rehashing)的方式,主动去做这种迁移。并且该方式是可以通过配置文件来配置的,相关conf如下表所示:(大致意思是:默认1秒做十次,如果你有严格的延迟要求,你可以把主动rehash关掉)
Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in
order to help rehashing the main Redis hash table (the one mapping top-level
keys to values). The hash table implementation Redis uses (see dict.c)
performs a lazy rehashing: the more operation you run into a hash table
that is rehashing, the more rehashing "steps" are performed, so if the
server is idle the rehashing is never complete and some more memory is used
by the hash table.
The default is to use this millisecond 10 times every second in order to
actively rehash the main dictionaries, freeing memory when possible.
If unsure:
use "activerehashing no" if you have hard latency requirements and it is
not a good thing in your environment that Redis can reply from time to time
to queries with 2 milliseconds delay.
use "activerehashing yes" if you don't have such hard requirements but
want to free memory asap when possible.
activerehashing yes