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

全部博文(31)

文章存档

2014年(31)

我的朋友

分类: C/C++

2014-07-28 10:04:16


|------tracker_load_data        
    |-----tracker_load_groups_new
        |-----tracker_load_groups_new
        |-----tracker_save_groups
        |-----iniloadFormFile
        |-----tracker_mem_add_group_ex
        |-----iniFreeContext

    |-----tracker_load_storage_new
    |-----tracker_malloc_all_group_path_mbs
    |-----tracker_load_sync_timestamps

tracker_load_groups_new函数:

点击(此处)折叠或打开

  1. //从storage_groups_new.dat获取group数据
  2. static int tracker_load_groups_new(FDFSGroups *pGroups, const char *data_path)
  3. {
  4.     IniContext iniContext; //保存从配置文件中读取信息
  5.     FDFSGroupInfo *pGroup;
  6.     char *group_name;
  7.     int group_count;
  8.     int result;
  9.     int i;
  10.     char section_name[64];
  11.     TrackerClientInfo clientInfo;
  12.     bool bInserted;

  13.     if (!fileExists(STORAGE_GROUPS_LIST_FILENAME_NEW) && \//若_new不存在,并且_old存在
  14.      fileExists(STORAGE_GROUPS_LIST_FILENAME_OLD))
  15.     {
  16.         logDebug("file: "__FILE__", line: %d, " \
  17.             "convert old data file %s to new data file %s", \
  18.             __LINE__, STORAGE_GROUPS_LIST_FILENAME_OLD, \
  19.             STORAGE_GROUPS_LIST_FILENAME_NEW);
  20.         if ((result=tracker_load_groups_old(pGroups, data_path)) == 0)//从_old文件中读取配置
  21.         {
  22.             if ((result=tracker_save_groups()) == 0)//把_old文件变成_new文件
  23.             {
  24.                 unlink(STORAGE_GROUPS_LIST_FILENAME_OLD); //删除_old文件
  25.             }
  26.         }

  27.         return result;
  28.     }

  29.     if ((result=iniLoadFromFile(STORAGE_GROUPS_LIST_FILENAME_NEW, \
  30.             &iniContext)) != 0)//从新的group文件中获取配置信息,并保存到inicontext结构中
  31.     {
  32.         return result;
  33.     }

  34.     group_count = iniGetIntValue(GROUP_SECTION_NAME_GLOBAL, \//获取集群中group的数目
  35.                 GROUP_ITEM_GROUP_COUNT, \
  36.                         &iniContext, -1);
  37.     if (group_count < 0)
  38.     {
  39.         iniFreeContext(&iniContext);
  40.         logError("file: "__FILE__", line: %d, " \
  41.             "in the file \"%s/%s\", " \
  42.             "item \"%s\" is not found", \
  43.             __LINE__, data_path, \
  44.             STORAGE_GROUPS_LIST_FILENAME_NEW, \
  45.             GROUP_ITEM_GROUP_COUNT);
  46.         return ENOENT;
  47.     }

  48.     for (i=1; i<=group_count; i++)//依次获取section name,如Group001
  49.     {
  50.         sprintf(section_name, "%s"GROUP_SECTION_NO_FORMAT, \
  51.             GROUP_SECTION_NAME_PREFIX, i);

  52.         group_name = iniGetStrValue(section_name, \//从section name中获取group name
  53.                 GROUP_ITEM_GROUP_NAME, &iniContext);
  54.         if (group_name == NULL)
  55.         {
  56.             logError("file: "__FILE__", line: %d, " \
  57.                 "in the file \"%s/%s\", " \
  58.                 "item \"%s\" is not found", \
  59.                 __LINE__, data_path, \
  60.                 STORAGE_GROUPS_LIST_FILENAME_NEW, \
  61.                 GROUP_ITEM_GROUP_NAME);
  62.             result = ENOENT;
  63.             break;
  64.         }

  65.         memset(&clientInfo, 0, sizeof(TrackerClientInfo));
  66.         if ((result=tracker_mem_add_group_ex(pGroups, &clientInfo, \//从pGroups中找到一个group,若没有找到,
  67.                 group_name, false, &bInserted)) != 0) //则,创建一个group,放入到pGroups中
  68.         {
  69.             break;
  70.         }

  71.         if (!bInserted)
  72.         {
  73.             logError("file: "__FILE__", line: %d, " \
  74.                 "in the file \"%s/%s\", " \
  75.                 "group \"%s\" is duplicate", \
  76.                 __LINE__, data_path, \
  77.                 STORAGE_GROUPS_LIST_FILENAME_NEW, \
  78.                 group_name);
  79.             result = errno != 0 ? errno : EEXIST;
  80.             break;
  81.         }

  82.         pGroup = clientInfo.pGroup;
  83.         pGroup->storage_port = iniGetIntValue(section_name, \
  84.             GROUP_ITEM_STORAGE_PORT, &iniContext, 0);
  85.         pGroup->storage_http_port = iniGetIntValue(section_name, \
  86.             GROUP_ITEM_STORAGE_HTTP_PORT, &iniContext, 0);
  87.         pGroup->store_path_count = iniGetIntValue(section_name, \
  88.             GROUP_ITEM_STORE_PATH_COUNT, &iniContext, 0);
  89.         pGroup->subdir_count_per_path = iniGetIntValue(section_name, \
  90.             GROUP_ITEM_SUBDIR_COUNT_PER_PATH, &iniContext, 0);
  91.     }

  92.     iniFreeContext(&iniContext);  //释放iniContext空间

  93.     return result;
  94. }

tracker_load_groups_new函数执行逻辑:
(1) 从配置文件中读取group配置信息并写入到group对象中。
(2) 为了兼容老的版本,先调用tracker_load_groups_old函数读取老的配置文件,并把数据写入新的配置文件。
(3) 读取新配置文件中的数据到iniContext结构中,再从iniContext中读取数据。
(4) 调用tracker_mem_add_group_ex函数把读取的数据写入到group对象中,tracker_mem_add_group_ex函数将在下一节详细分析。





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