Chinaunix首页 | 论坛 | 博客
  • 博客访问: 58262
  • 博文数量: 7
  • 博客积分: 171
  • 博客等级: 入伍新兵
  • 技术积分: 121
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-16 18:48
文章分类

全部博文(7)

文章存档

2012年(7)

我的朋友

分类: 云计算

2012-03-24 12:59:16


首先分析下 tracker , tracker_service.c 3137行:int tracker_deal_task(struct fast_task_info *pTask)

对TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE命令的处理是
  1. case TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE:
  2.             result = tracker_deal_service_query_storage( \
  3.                     pTask, pHeader->cmd);
  4.             break;
我们进入 tracker_deal_service_query_storage, tracker_service.c 1955行:

  1. static int tracker_deal_service_query_storage( \
  2.         struct fast_task_info *pTask, char cmd)
这个函数比较长,由于里面包含了负载均衡相关的逻辑判断,我们暂时关注点只分析cmd为TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE,涉及到负载均衡的部分,之后其他文章会单独分析。

tracker_service.c 2184行: 
  1.     if (cmd == TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ONE
  2.      || cmd == TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE)
  3.     {
  4.         pStorageServer = tracker_get_writable_storage(pStoreGroup);
  5.         if (pStorageServer == NULL)
  6.         {
  7.             pTask->length = sizeof(TrackerHeader);
  8.             return ENOENT;
  9.         }
  10.     }
  11.     else //query store server list, use the first to check
  12.     {
  13.         pStorageServer = *(pStoreGroup->active_servers);
  14.     }
代码分析:
1.通过负载均衡, 获得可利用的pStoreGroup 结构;
2. 调用tracker_get_writable_storage ,找到合适的pStorageServer

tracker_service.c 2199行:

  1. write_path_index = pStorageServer->current_write_path;
  2.     if (write_path_index >= pStoreGroup->store_path_count)
  3.     {
  4.         write_path_index = 0;
  5.     }

  6.     avg_reserved_mb = g_storage_reserved_mb / \
  7.              pStoreGroup->store_path_count;
  8.     if (pStorageServer->path_free_mbs[write_path_index] <= avg_reserved_mb)
  9.     {
  10.         int i;
  11.         for (i=0; i<pStoreGroup->store_path_count; i++)
  12.         {
  13.             if (pStorageServer->path_free_mbs[i] > avg_reserved_mb)
  14.             {
  15.                 pStorageServer->current_write_path = i;
  16.                 write_path_index = i;
  17.                 break;
  18.             }
  19.         }

  20.         if (i == pStoreGroup->store_path_count)
  21.         {
  22.             if (!g_if_use_trunk_file)
  23.             {
  24.                 pTask->length = sizeof(TrackerHeader);
  25.                 return ENOSPC;
  26.             }

  27.             for (i=write_path_index; i<pStoreGroup-> \
  28.                 store_path_count; i++)
  29.             {
  30.                 if (pStorageServer->path_free_mbs[i] + \
  31.                  pStoreGroup->trunk_free_mb > avg_reserved_mb)
  32.                 {
  33.                     pStorageServer->current_write_path = i;
  34.                     write_path_index = i;
  35.                     break;
  36.                 }
  37.             }
  38.             if ( i == pStoreGroup->store_path_count)
  39.             {
  40.                 for (i=0; i<write_path_index; i++)
  41.                 {
  42.                 if (pStorageServer->path_free_mbs[i] + \
  43.                  pStoreGroup->trunk_free_mb > avg_reserved_mb)
  44.                 {
  45.                     pStorageServer->current_write_path = i;
  46.                     write_path_index = i;
  47.                     break;
  48.                 }
  49.                 }

  50.                 if (i == write_path_index)
  51.                 {
  52.                     pTask->length = sizeof(TrackerHeader);
  53.                     return ENOSPC;
  54.                 }
  55.             }
  56.         }
  57.     }

  58.     if (g_groups.store_path == FDFS_STORE_PATH_ROUND_ROBIN)
  59.     {
  60.         pStorageServer->current_write_path++;
  61.         if (pStorageServer->current_write_path >= \
  62.                 pStoreGroup->store_path_count)
  63.         {
  64.             pStorageServer->current_write_path = 0;
  65.         }
  66.     }

  67.     /*
  68.     //printf("pStoreGroup->current_write_server: %d, " \
  69.     "pStoreGroup->active_count=%d\n", \
  70.     pStoreGroup->current_write_server, \
  71.     pStoreGroup->active_count);
  72.     */

  73.     p = pTask->data + sizeof(TrackerHeader);
  74.     memcpy(p, pStoreGroup->group_name, FDFS_GROUP_NAME_MAX_LEN);
  75.     p += FDFS_GROUP_NAME_MAX_LEN;

  76.     if (cmd == TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ALL
  77.      || cmd == TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ALL)
  78.     {
  79.         int active_count;
  80.         FDFSStorageDetail **ppServer;
  81.         FDFSStorageDetail **ppEnd;

  82.         active_count = pStoreGroup->active_count;
  83.         if (active_count == 0)
  84.         {
  85.             pTask->length = sizeof(TrackerHeader);
  86.             return ENOENT;
  87.         }

  88.         ppEnd = pStoreGroup->active_servers + active_count;
  89.         for (ppServer=pStoreGroup->active_servers; ppServer<ppEnd; \
  90.             ppServer++)
  91.         {
  92.             memcpy(p, (*ppServer)->ip_addr, IP_ADDRESS_SIZE - 1);
  93.             p += IP_ADDRESS_SIZE - 1;

  94.             long2buff(pStoreGroup->storage_port, p);
  95.             p += FDFS_PROTO_PKG_LEN_SIZE;
  96.         }
  97.     }
  98.     else
  99.     {
  100.         memcpy(p, pStorageServer->ip_addr, IP_ADDRESS_SIZE - 1);
  101.         p += IP_ADDRESS_SIZE - 1;

  102.         long2buff(pStoreGroup->storage_port, p);
  103.         p += FDFS_PROTO_PKG_LEN_SIZE;
  104.     }

  105.     *p++ = (char)write_path_index;

  106.     pTask->length = p - pTask->data;

  107.     return 0;
代码分析:
1. 根据配置的条件,查找合适的write_path_index,
2. 将找到的storage server 信息,group_name、ip_addr、storage_port 拼接到响应包里面。

最后, tracker_service.c 3287行 :
  1.     pHeader = (TrackerHeader *)pTask->data;
  2.     pHeader->status = result;
  3.     pHeader->cmd = TRACKER_PROTO_CMD_RESP;
  4.     long2buff(pTask->length - sizeof(TrackerHeader), pHeader->pkg_len);

  5.     send_add_event(pTask);
会将应答包通过socket 发送至客户端。

至此,client 与 tracker 的请求storage server 的过程结束。这里只是对TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE 命令进行分析。其余获取store 的命令和它类似,读者可以自行分析。

欢迎感兴趣的朋友一起交流研究,提出意见。
FastDFS技术交流群:164684842





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

malcolm_ok2012-05-23 10:53:28

这个函数比较长,由于里面包含了负载均衡相关的逻辑判断,我们暂时关注点只分析cmd为TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE,涉及到负载均衡的部分,之后其他文章会单独分析。

希望博主 能继续写,你写的文章很不错,给我们的帮助很大。