Chinaunix首页 | 论坛 | 博客
  • 博客访问: 80315
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 380
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-24 22:04
文章分类

全部博文(31)

文章存档

2014年(31)

我的朋友

分类: C/C++

2014-07-28 09:21:03


|-----tracker_mem_init
        |-----init_pthread_lock(mem_thread_lock)
        |-----init_pthread_lock(mem_file_lock)
        |-----tracker_open_changlog_file
        |-----tracker_mem_init_groups

在上一节提到的tracker_mem_init函数中,tracker_mem_init_groups函数被这样调用

点击(此处)折叠或打开

  1. return tracker_mem_init_groups(&g_groups);//初始化g_groups全局变量
tracker_mem_init_groups函数被用来初始化全局变量g_groups,g_groups是一个FDFSGroups结构的变量,该结构构造如下:


点击(此处)折叠或打开

  1. typedef struct
  2. {
  3.     int alloc_size; //alloc group count //记录一次分配的group个数
  4.     int count; //group count //记录现在的groups中的group个数
  5.     FDFSGroupInfo **groups; //指向group指针数组
  6.     FDFSGroupInfo **sorted_groups; //groups order by group_name //指向sorted_group指针数组
  7.     FDFSGroupInfo *pStoreGroup; //the group to store uploaded files //上传文件的store group
  8.     int current_write_group; //current group index to upload file //上传文件的store group的index
  9.     byte store_lookup; //store to which group, from conf file //
  10.     byte store_server; //store to which storage server, from conf file
  11.     byte download_server; //download from which storage server, from conf file
  12.     byte store_path; //store to which path, from conf file
  13.     char store_group[FDFS_GROUP_NAME_MAX_LEN + 1];
  14. } FDFSGroups

FDFSGroups结构维护着分布式系统所有group的信息,FDFSGroups结构包含FDFSGroupInfo结构的指针,该结构构造如下:


点击(此处)折叠或打开

  1. typedef struct
  2. {
  3.     char group_name[FDFS_GROUP_NAME_MAX_LEN + 1];
  4.     int64_t free_mb; //free disk storage in MB
  5.     int alloc_size; //alloc storage count //一次增加个数
  6.     int count; //total server count //一个group中,总共的server的数目
  7.     int active_count; //active server count //活动的server数目
  8.     int storage_port; //storage server port //端口
  9.     int storage_http_port; //storage http server port //http端口
  10.     FDFSStorageDetail **all_servers; //all storage servers
  11.     FDFSStorageDetail **sorted_servers; //storages order by ip addr
  12.     FDFSStorageDetail **active_servers; //storages order by ip addr
  13.     FDFSStorageDetail *pStoreServer; //for upload priority mode

  14. #ifdef WITH_HTTPD
  15.     FDFSStorageDetail **http_servers; //storages order by ip addr
  16.     int http_server_count; //http server count
  17.     int current_http_server; //current http server index
  18. #endif

  19.     int current_read_server; //current read storage server index
  20.     int current_write_server; //current write storage server index

  21.     int store_path_count; //store base path count of each storage server

  22.     /* subdir_count * subdir_count directories will be auto created
  23.      under each store_path (disk) of the storage servers
  24.     */
  25.     int subdir_count_per_path;

  26.     int **last_sync_timestamps;//row for src storage, col for dest storage

  27.     int chg_count; //current group changed count
  28.     time_t last_source_update; //last source update timestamp
  29.     time_t last_sync_update; //last synced update timestamp
  30. } FDFSGroupInfo

FDFSGroupInfo结构表示分布式存储系统中的单个group的信息,FDFSGroupInfo结构又包含了FDFSStorageDetail结构,该结构的构造如下:

点击(此处)折叠或打开

  1. typedef struct StructFDFSStorageDetail
  2. {
  3.     char status;  //当前存储的状态
  4.     char ip_addr[IP_ADDRESS_SIZE];  //ip地址
  5.     char domain_name[FDFS_DOMAIN_NAME_MAX_SIZE];  //当前存储的域名
  6.     char version[FDFS_VERSION_SIZE];  //版本

  7.     struct StructFDFSStorageDetail *psync_src_server;
  8.     time_t sync_until_timestamp;

  9.     time_t join_time; //storage join timestamp (create timestamp)
  10.     time_t up_time; //startup timestamp
  11.     int64_t total_mb; //total disk storage in MB
  12.     int64_t free_mb; //free disk storage in MB
  13.     int64_t changelog_offset; //changelog file offset

  14.     int64_t *path_total_mbs; //total disk storage in MB
  15.     int64_t *path_free_mbs; //free disk storage in MB

  16.     int store_path_count; //store base path count of each storage server
  17.     int subdir_count_per_path;
  18.     int upload_priority; //storage upload priority

  19.     int storage_port; //storage server port
  20.     int storage_http_port; //storage http server port

  21.     int current_write_path; //current write path index

  22.     int chg_count; //current server changed counter
  23.     FDFSStorageStat stat;

  24. #ifdef WITH_HTTPD
  25.     int http_check_last_errno;
  26.     int http_check_last_status;
  27.     int http_check_fail_count;
  28.     char http_check_error_info[256];
  29. #endif
  30. } FDFSStorageDetail

FDFSStorageDetail表示分布式系统中的一个storage的结构,通过对上面三个结构的分析,对理清groups、group、storage的三者之间的关系很重要

tracker_mem_init_groups内部的函数调用结构如下:
tracker_mem_init_groups
    |-----tracker_load_data
tracker_mem_init_groups在内部先分配了全局变量g_groups的groups的空间,再调用tracker_load_data函数分配相关group与storage的空间


点击(此处)折叠或打开

  1. //初始化group的空间
  2. static int tracker_mem_init_groups(FDFSGroups *pGroups)
  3. {
  4.     int result;
  5.     FDFSGroupInfo **ppGroup;
  6.     FDFSGroupInfo **ppGroupEnd;

  7.     pGroups->alloc_size = TRACKER_MEM_ALLOC_ONCE; //每次分配2个group的空间
  8.     pGroups->count = 0;//group数目置0
  9.     pGroups->current_write_group = 0;//upload group的index置0
  10.     pGroups->pStoreGroup = NULL;//当前upload group为null
  11.     pGroups->groups = (FDFSGroupInfo **)malloc( \ //为groups分配空间,groups指向的是一个group的指针数组
  12.             sizeof(FDFSGroupInfo *) * pGroups->alloc_size);
  13.     if (pGroups->groups == NULL)
  14.     {
  15.         logCrit("file: "__FILE__", line: %d, " \
  16.             "malloc %d bytes fail!", __LINE__, \
  17.             (int)sizeof(FDFSGroupInfo *) * pGroups->alloc_size);
  18.         return errno != 0 ? errno : ENOMEM;
  19.     }

  20.     memset(pGroups->groups, 0, \ //把groups指向的数组空间置0
  21.         sizeof(FDFSGroupInfo *) * pGroups->alloc_size);

  22.     ppGroupEnd = pGroups->groups + pGroups->alloc_size;//指向group指针数组末尾的下一个节点
  23.     for (ppGroup=pGroups->groups; ppGroup<ppGroupEnd; ppGroup++)
  24.     {
  25.         *ppGroup = (FDFSGroupInfo *)malloc(sizeof(FDFSGroupInfo));//为group指针数组的每个元素分配一个group的空间
  26.         if (*ppGroup == NULL)
  27.         {
  28.             logCrit("file: "__FILE__", line: %d, " \
  29.                 "malloc %d bytes fail!", \
  30.                 __LINE__, (int)sizeof(FDFSGroupInfo));
  31.             return errno != 0 ? errno : ENOMEM;
  32.         }

  33.         memset(*ppGroup, 0, sizeof(FDFSGroupInfo));
  34.     }

  35.     pGroups->sorted_groups = (FDFSGroupInfo **) \//为sorted_groups分配空间,sorted_groups指向的是一个group的指针数组
  36.             malloc(sizeof(FDFSGroupInfo *) * pGroups->alloc_size);
  37.     if (pGroups->sorted_groups == NULL)
  38.     {
  39.         free(pGroups->groups);
  40.         pGroups->groups = NULL;

  41.         logCrit("file: "__FILE__", line: %d, " \
  42.             "malloc %d bytes fail!", __LINE__, \
  43.             (int)sizeof(FDFSGroupInfo *) * pGroups->alloc_size);
  44.         return errno != 0 ? errno : ENOMEM;
  45.     }

  46.     memset(pGroups->sorted_groups, 0, \ //把sorted_groups指向的数组空间置0
  47.         sizeof(FDFSGroupInfo *) * pGroups->alloc_size);

  48.     if ((result=tracker_load_data(pGroups)) != 0) //从配置文件中加载group与server的数据
  49.     {
  50.         return result;
  51.     }

  52.     return 0;
  53. }

下图展示出了,在执行函数tracker_load_data函数之前,g_groups的初始化状态


程序给groups指针数组分配了空间,并给数组中的每个指针分配了空间,而对sorted_groups指针数组,只分配了数组空间,数组中的每个元素还未分配空间
下一节将详细分析tracker_load_data函数,该函数继续为全局变量g_gruoup中的元素分配空间





阅读(649) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~