Redis源码剖析-dict字典

来源:互联网 发布:单词社交网络 编辑:程序博客网 时间:2024/05/17 03:28

改换一下策略,不直接介绍源码,打算先整体介绍一下思路,然后再根据源码解释具体的实现。

dict结构

如图所示,一个dict字典中由两个hashtable组成,分别为ht[0]和ht[1],用到的基本上都是ht[0]。 那么ht[1]什么时候用到呢?因为hash算出来的索引值是有可能重复的,也就是说不同的dictEntry有可能位于同一个hashtable的槽内,如果拥有的dictEntry的数量和slot的数量的比值超过了5,相当于平均每个slot拥有5个以上的dictEntry的时候,就需要重新rehash整个dict。

扩展或者收缩dict的时候,并不是一次性完成的, 因为如果dict中拥有大量数据的时候,一次性的操作有可能会影响正式的服务。

所以redis采取的策略是分布式的rehash。 利用rehashidx来记录当前进行到了哪个索引,下一次的rehash从这个索引开始。那什么时候进行rehash呢,有两种策略, 一种是在指定时间内执行固定步数;另一种是在每次对当前dict进行查询、修改的时候,每一次操作都附带完成一个索引值的rehash。这样就把整体的rehash时间平摊到了各个小操作中。

在rehash的过程中,如果执行查询操作,两个ht都需要查询;修改和删除也需要操作两个ht,插入的时候,只需要插入到ht[1],这样就保证了ht[0]里边的键值对只少不多。

Dict定义

typedef struct dict {    dictType *type;  // 类型特定函数    void *privdata;  // 私有数据    dictht ht[2];    // 2个哈希表    // rehash 索引    // 当 rehash 不在进行时,值为 -1    long rehashidx; /* rehashing not in progress if rehashidx == -1 */    // 目前正在运行的安全迭代器的数量    unsigned long iterators; /* number of iterators currently running */} dict;

dictType保存一些用于操作特定类型键值对的函数,定义如下:

typedef struct dictType {    // 计算哈希值的函数    uint64_t (*hashFunction)(const void *key);    // 复制键的函数    void *(*keyDup)(void *privdata, const void *key);    // 复制值的函数    void *(*valDup)(void *privdata, const void *obj);    // 比较键的函数    int (*keyCompare)(void *privdata, const void *key1, const void *key2);    // 销毁键的函数    void (*keyDestructor)(void *privdata, void *key);    // 销毁值的函数    void (*valDestructor)(void *privdata, void *obj);} dictType;

dicht哈希表,所有的键值对保存在里边,定义如下:

typedef struct dictht {    dictEntry **table;  // 哈希数组    unsigned long size;  // 总的哈希表大小    // 哈希表大小掩码,用于计算索引值    // 总是等于 size - 1    unsigned long sizemask;      unsigned long used;  // 该哈希表已有节点的数量} dictht;

dictEntry则保存了每一个具体的键值对,定义如下:

typedef struct dictEntry {    // 键    void *key;    union {        void *val;        uint64_t u64;        int64_t s64;        double d;    } v; // 值    // 指向下一个结点,因为hash值有可能冲突,冲突的时候链表形式保存在同一个索引后边    struct dictEntry *next;} dictEntry;

Dict操作

判断是否需要进行rehash

/* Resize the table to the minimal size that contains all the elements, * but with the invariant of a USED/BUCKETS ratio near to <= 1 */int dictResize(dict *d){    int minimal;    // 不能在关闭 rehash 或者正在 rehash 的时候调用    if (!dict_can_resize || dictIsRehashing(d)) return DICT_ERR;    // 查看已经使用了多少结点,最少分配DICT_HT_INITIAL_SIZE=4个结点    // 否则,计算让比率接近 1:1 所需要的最少节点数量    minimal = d->ht[0].used;    if (minimal < DICT_HT_INITIAL_SIZE)        minimal = DICT_HT_INITIAL_SIZE;    return dictExpand(d, minimal);}/* Expand or create the hash table *//* * 创建一个新的哈希表,并根据字典的情况,选择以下其中一个动作来进行: * * 1) 如果字典的 0 号哈希表为空,那么将新哈希表设置为 0 号哈希表 * 2) 如果字典的 0 号哈希表非空,那么将新哈希表设置为 1 号哈希表, *    并打开字典的 rehash 标识,使得程序可以开始对字典进行 rehash * * size 参数不够大,或者 rehash 已经在进行时,返回 DICT_ERR 。 * * 成功创建 0 号哈希表,或者 1 号哈希表时,返回 DICT_OK 。 */int dictExpand(dict *d, unsigned long size){    dictht n; /* the new hash table */    // 计算大于size的第一个2的N次方的值,用来当作新的哈希表的大小    unsigned long realsize = _dictNextPower(size);    /* the size is invalid if it is smaller than the number of     * elements already inside the hash table */    if (dictIsRehashing(d) || d->ht[0].used > size)        return DICT_ERR;    /* Rehashing to the same table size is not useful. */    // 如果新表大小跟老表一样, 没有进行expand的需要    if (realsize == d->ht[0].size) return DICT_ERR;    /* Allocate the new hash table and initialize all pointers to NULL */    n.size = realsize;    n.sizemask = realsize-1;    n.table = zcalloc(realsize*sizeof(dictEntry*));    n.used = 0;    /* Is this the first initialization? If so it's not really a rehashing     * we just set the first hash table so that it can accept keys. */     // 如果 0 号哈希表为空,那么这是一次初始化:     // 程序将新哈希表赋给 0 号哈希表的指针,然后字典就可以开始处理键值对了。    if (d->ht[0].table == NULL) {        d->ht[0] = n;        return DICT_OK;    }    /* Prepare a second hash table for incremental rehashing */    // 如果 0 号哈希表非空,那么这是一次 rehash :    // 程序将新哈希表设置为 1 号哈希表,    // 并将字典的 rehash 标识打开,让程序可以开始对字典进行 rehash    d->ht[1] = n;    d->rehashidx = 0;    return DICT_OK;}

rehash算法

/* Performs N steps of incremental rehashing. Returns 1 if there are still * keys to move from the old to the new hash table, otherwise 0 is returned. * 执行 N 步渐进式 rehash 。 * * 返回 1 表示仍有键需要从 0 号哈希表移动到 1 号哈希表, * 返回 0 则表示所有键都已经迁移完毕。 * Note that a rehashing step consists in moving a bucket (that may have more * than one key as we use chaining) from the old to the new hash table, however * since part of the hash table may be composed of empty spaces, it is not * guaranteed that this function will rehash even a single bucket, since it * will visit at max N*10 empty buckets in total, otherwise the amount of * work it does would be unbound and the function may block for a long time. * 每步 rehash 都是以一个哈希表索引(桶)作为单位的, * 一个桶里可能会有多个节点, * 被 rehash 的桶里的所有节点都会被移动到新哈希表。*/int dictRehash(dict *d, int n) {    // 由于hash表中的桶有可能为空,设置最大访问空桶的数量为n*10,否则如果空桶很多的话会等待比较长的一段时间    int empty_visits = n*10; /* Max number of empty buckets to visit. */    // 只可以在 rehash 进行中时执行    if (!dictIsRehashing(d)) return 0;    while(n-- && d->ht[0].used != 0) {        dictEntry *de, *nextde;        /* Note that rehashidx can't overflow as we are sure there are more         * elements because ht[0].used != 0 */        // 确保 rehashidx 没有越界        assert(d->ht[0].size > (unsigned long)d->rehashidx);        // 略过数组中为空的索引,找到下一个非空索引        // 如果访问的空索引达到了n*10, 停止遍历        while(d->ht[0].table[d->rehashidx] == NULL) {            d->rehashidx++;            if (--empty_visits == 0) return 1;        }        // 指向该索引的链表表头节点        de = d->ht[0].table[d->rehashidx];        /* Move all the keys in this bucket from the old to the new hash HT */        // 将链表中的所有节点迁移到新哈希表        while(de) {            unsigned int h;            nextde = de->next;            /* Get the index in the new hash table */            // 根据新表的sizemask计算哈希值            h = dictHashKey(d, de->key) & d->ht[1].sizemask;            // 插入节点到新哈希表            de->next = d->ht[1].table[h];            d->ht[1].table[h] = de;            // 更新计数器            d->ht[0].used--;            d->ht[1].used++;            de = nextde;        }        // 将刚迁移完的哈希表索引的指针设为空        d->ht[0].table[d->rehashidx] = NULL;        // 更新 rehash 索引        d->rehashidx++;    }    /* Check if we already rehashed the whole table... */    // 如果 0 号哈希表为空,那么表示 rehash 执行完毕    if (d->ht[0].used == 0) {        // 释放 0 号哈希表        zfree(d->ht[0].table);        // 将原来的 1 号哈希表设置为新的 0 号哈希表        d->ht[0] = d->ht[1];        // 重置旧的 1 号哈希表        _dictReset(&d->ht[1]);        // 关闭 rehash 标识        d->rehashidx = -1;        return 0;    }    /* More to rehash... */    return 1;}

在两种情况下会调用rehash的操作,一种是在指定时间内执行操作,每次操作进行100步:

/* Rehash for an amount of time between ms milliseconds and ms+1 milliseconds */int dictRehashMilliseconds(dict *d, int ms) {    // 记录开始时间    long long start = timeInMilliseconds();    int rehashes = 0;    while(dictRehash(d,100)) {        // 在给定毫秒数内,以 100 步为单位,对字典进行 rehash        rehashes += 100;        // 如果时间已过,跳出        if (timeInMilliseconds()-start > ms) break;    }    return rehashes;}

另一种是在执行普通的查询或者更新操作的时候,同时执行一次rehash

/* This function is called by common lookup or update operations in the * dictionary so that the hash table automatically migrates from H1 to H2 * while it is actively used. */static void _dictRehashStep(dict *d) {    if (d->iterators == 0) dictRehash(d,1);}

Dict插入键

/* Add an element to the target hash table */int dictAdd(dict *d, void *key, void *val){    // 往字典中添加一个key    dictEntry *entry = dictAddRaw(d,key,NULL);    if (!entry) return DICT_ERR;    // 如果成功返回, 为key是指value    dictSetVal(d, entry, val);    return DICT_OK;}dictEntry *dictAddRaw(dict *d, void *key, dictEntry **existing){    int index;    dictEntry *entry;    dictht *ht;    // 如果在进行rehash操作, 执行一步rehash    if (dictIsRehashing(d)) _dictRehashStep(d);    /* Get the index of the new element, or -1 if     * the element already exists. */     // 获取key的索引,如果索引已经存在,返回NULL    if ((index = _dictKeyIndex(d, key, dictHashKey(d,key), existing)) == -1)        return NULL;    /* Allocate the memory and store the new entry.     * Insert the element in top, with the assumption that in a database     * system it is more likely that recently added entries are accessed     * more frequently. */     // 是否在进行rehash操作,如果在rehash, 放到新表ht[1], 否则放到ht[0]    ht = dictIsRehashing(d) ? &d->ht[1] : &d->ht[0];    entry = zmalloc(sizeof(*entry));    // 插入新结点    entry->next = ht->table[index];    ht->table[index] = entry;    ht->used++;    /* Set the hash entry fields. */    dictSetKey(d, entry, key);    return entry;}

添加或更新元素

  • 如果之前元素不存在,添加成功后返回1
  • 如果之前元素存在,更新元素,同时返回0
  • 如果存在的话,设置新的值,然后释放老的值,这样做能够充分利用引用计数,如果是同一个元素的话,更改计数就行
int dictReplace(dict *d, void *key, void *val){    dictEntry *entry, *existing, auxentry;    /* Try to add the element. If the key     * does not exists dictAdd will suceed. */    // 添加键值对,如果之前不存在的话,添加成功返回1    entry = dictAddRaw(d,key,&existing);    if (entry) {        dictSetVal(d, entry, val);        return 1;    }    /* Set the new value and free the old one. Note that it is important     * to do that in this order, as the value may just be exactly the same     * as the previous one. In this context, think to reference counting,     * you want to increment (set), and then decrement (free), and not the     * reverse. */    // 如果存在的话,设置新的值,然后释放老的值,这样做能够充分利用引用计数,如果是同一个元素的话,更改计数就行    auxentry = *existing;    dictSetVal(d, existing, val);    dictFreeVal(d, &auxentry);    return 0;}

删除键值对

static dictEntry *dictGenericDelete(dict *d, const void *key, int nofree) {    unsigned int h, idx;    dictEntry *he, *prevHe;    int table;    // 如果两个哈希表都没有元素, 返回NULL    if (d->ht[0].used == 0 && d->ht[1].used == 0) return NULL;    // 如果正在进行rehash,先执行一步rehash    if (dictIsRehashing(d)) _dictRehashStep(d);    h = dictHashKey(d, key);    // 两个表都要查看    for (table = 0; table <= 1; table++) {        idx = h & d->ht[table].sizemask;        he = d->ht[table].table[idx];        prevHe = NULL;        while(he) {            if (key==he->key || dictCompareKeys(d, key, he->key)) {                /* Unlink the element from the list */                if (prevHe)                    prevHe->next = he->next;                else                    d->ht[table].table[idx] = he->next;                if (!nofree) {                    dictFreeKey(d, he);                    dictFreeVal(d, he);                    zfree(he);                }                d->ht[table].used--;                return he;            }            prevHe = he;            he = he->next;        }        // 如果没有在进行rehash,说明ht[1]没有值,就不用查看了        if (!dictIsRehashing(d)) break;    }    return NULL; /* not found */}

参数中的nofree用来标记是否真的删除,如果这个值为1的话,只是把这个键值对从table中拿下来,但并不是真的删除;如果想要移除一个键值对,但是在真的删除之前还想使用它的值,这个操作是有用的。

如果没有这个操作,需要先执行find操作找到结点,使用之后再执行删除,就需要进行两次查找;而这个操作只需要进行一次查找,用完之后再调用dictFreeUnlinkedEntry释放这个键值对就行。

查看key的值

dictEntry *dictFind(dict *d, const void *key){    dictEntry *he;    unsigned int h, idx, table;    // 如果没有键值对,返回NULL    if (d->ht[0].used + d->ht[1].used == 0) return NULL; /* dict is empty */    // 如果正在进行rehash,先进行一次rehash    if (dictIsRehashing(d)) _dictRehashStep(d);    // 计算hash值    h = dictHashKey(d, key);    for (table = 0; table <= 1; table++) {        // 计算索引值        idx = h & d->ht[table].sizemask;        he = d->ht[table].table[idx];        while(he) {            if (key==he->key || dictCompareKeys(d, key, he->key))                return he;            he = he->next;        }        // 如果没有在进行rehash,没有查看ht[1]的必要        if (!dictIsRehashing(d)) return NULL;    }    return NULL;}void *dictFetchValue(dict *d, const void *key) {    dictEntry *he;    he = dictFind(d,key);    return he ? dictGetVal(he) : NULL;}

此外还有一个遍历dict的操作,如果dict保持不变,直接按照索引顺序遍历就行,但是由于dict存在扩大和缩小的可能性,如果和做到在扩大或缩小的同时,遍历dict能够不漏掉所有键值对呢,这个算法比较复杂, 单独开一篇研究。