redis中hash扩容过程

Redis在hash扩容时采用渐进式rehash策略,避免一次性迁移大量数据导致服务器暂停服务。这一过程涉及ht[1]的分配、rehashindex初始化、分步迁移ht[0]的键值对至ht[1],直到完成rehash并释放ht[0]。在rehash期间,增删改查操作会同时处理ht[0]和ht[1],确保数据一致性。
摘要由CSDN通过智能技术生成

数据结构

Redis一共支持5种数据结构,hash是其中的一种,在hash扩容的时候采用的是渐进式rehash的方式。要想深入理解渐进式rehash,首先要了解以下Redis中hash的数据结构。

哈希表节点

typedef struct dictEntry {
    void *key; // 键
    union {
        void *val;
        uint64_t u64;
        int64_t s64;
        double d;
    } v; // 值
    struct dictEntry *next; // 下一个节点
} dictEntry;

哈希表

/* This is our hash table structure. Every dictionary has two of this as we
 * implement incremental rehashing, for the old to the new table. */
typedef struct dictht {
    dictEntry **table; // 哈希表数组
    unsigned long size; // 哈希表大小
    unsigned long sizemask; // 掩码,计算索引值,size-1
    unsigned long used; // 哈希表已有节点的数量
} dictht;

字典

typedef struct dict {
    dictType *type; // 类型特定函数
    void *privdata; // 私有数据
    dictht ht[2]; // 哈希表
    // rehash索引
    long rehashidx; /* reha
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值