首页 > 代码库 > glusterfs 中的字典查询

glusterfs 中的字典查询

  glusterfs文件系统是一个分布式的文件系统,但是与很多分布式文件系统不一样,它没有元数服务器,听说swift上也是应用了这个技术的。glusterfs中每个xlator的配置信息都是用dict进行管理的。dict这玩意儿,说白了就是一个hash表,是一个key/value的内存数据库。今天花了点时间慢慢研究了glusterfs中的设计,觉得还是挺有意思的。

  上篇博客介绍了glusterfs文件系统的内存池的设计,而glusterfs的内存池正应用在这项技术上。首先,glusterfsd在程序初始化时,就建立了三个池dict_pool、dict_pair_pool、dict_data_pool。接下来看看它是怎么玩这三个内存池的呢!

  1、在使用dict之前,首先是建立dict对象,这点是面向对象的思想吧。

1 dict_t *2 get_new_dict (void)3 {4         return get_new_dict_full (1);5 }

  glusterfs调用get_new_dict来建立一个dict对象,接下来看看get_new_dict又做了什么呢?

 1 dict_t * 2 get_new_dict_full (int size_hint) 3 { 4         dict_t *dict = mem_get0 (THIS->ctx->dict_pool); 5  6         if (!dict) { 7                 return NULL; 8         } 9 10         dict->hash_size = size_hint;11         if (size_hint == 1) {12                 /*13                  * This is the only case we ever see currently.  If we ever14                  * need to support resizing the hash table, the resize function15                  * will have to take into account the possibility that16                  * "members" is not separately allocated (i.e. don‘t just call17                  * realloc() blindly.18                  */19                 dict->members = &dict->members_internal;20         }21         else {22                 /*23                  * We actually need to allocate space for size_hint *pointers*24                  * but we actually allocate space for one *structure*.  Since25                  * a data_pair_t consists of five pointers, we‘re wasting four26                  * pointers‘ worth for N=1, and will overrun what we allocated27                  * for N>5.  If anybody ever starts using size_hint, we‘ll need28                  * to fix this.29                  */30                 GF_ASSERT (size_hint <=31                            (sizeof(data_pair_t) / sizeof(data_pair_t *)));32                 dict->members = mem_get0 (THIS->ctx->dict_pair_pool);33                 if (!dict->members) {34                         mem_put (dict);35                         return NULL;36                 }37         }38 39         LOCK_INIT (&dict->lock);40 41         return dict;42 }

   size_hint是要分配的字典的大小