|-----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函数被这样调用
-
return tracker_mem_init_groups(&g_groups);//初始化g_groups全局变量
tracker_mem_init_groups函数被用来初始化全局变量g_groups,g_groups是一个FDFSGroups结构的变量,该结构构造如下:
-
typedef struct
-
{
-
int alloc_size; //alloc group count //记录一次分配的group个数
-
int count; //group count //记录现在的groups中的group个数
-
FDFSGroupInfo **groups; //指向group指针数组
-
FDFSGroupInfo **sorted_groups; //groups order by group_name //指向sorted_group指针数组
-
FDFSGroupInfo *pStoreGroup; //the group to store uploaded files //上传文件的store group
-
int current_write_group; //current group index to upload file //上传文件的store group的index
-
byte store_lookup; //store to which group, from conf file //
-
byte store_server; //store to which storage server, from conf file
-
byte download_server; //download from which storage server, from conf file
-
byte store_path; //store to which path, from conf file
-
char store_group[FDFS_GROUP_NAME_MAX_LEN + 1];
-
} FDFSGroups
FDFSGroups结构维护着分布式系统所有group的信息,FDFSGroups结构包含FDFSGroupInfo结构的指针,该结构构造如下:
-
typedef struct
-
{
-
char group_name[FDFS_GROUP_NAME_MAX_LEN + 1];
-
int64_t free_mb; //free disk storage in MB
-
int alloc_size; //alloc storage count //一次增加个数
-
int count; //total server count //一个group中,总共的server的数目
-
int active_count; //active server count //活动的server数目
-
int storage_port; //storage server port //端口
-
int storage_http_port; //storage http server port //http端口
-
FDFSStorageDetail **all_servers; //all storage servers
-
FDFSStorageDetail **sorted_servers; //storages order by ip addr
-
FDFSStorageDetail **active_servers; //storages order by ip addr
-
FDFSStorageDetail *pStoreServer; //for upload priority mode
-
-
#ifdef WITH_HTTPD
-
FDFSStorageDetail **http_servers; //storages order by ip addr
-
int http_server_count; //http server count
-
int current_http_server; //current http server index
-
#endif
-
-
int current_read_server; //current read storage server index
-
int current_write_server; //current write storage server index
-
-
int store_path_count; //store base path count of each storage server
-
-
/* subdir_count * subdir_count directories will be auto created
-
under each store_path (disk) of the storage servers
-
*/
-
int subdir_count_per_path;
-
-
int **last_sync_timestamps;//row for src storage, col for dest storage
-
-
int chg_count; //current group changed count
-
time_t last_source_update; //last source update timestamp
-
time_t last_sync_update; //last synced update timestamp
-
} FDFSGroupInfo
FDFSGroupInfo结构表示分布式存储系统中的单个group的信息,FDFSGroupInfo结构又包含了FDFSStorageDetail结构,该结构的构造如下:
-
typedef struct StructFDFSStorageDetail
-
{
-
char status; //当前存储的状态
-
char ip_addr[IP_ADDRESS_SIZE]; //ip地址
-
char domain_name[FDFS_DOMAIN_NAME_MAX_SIZE]; //当前存储的域名
-
char version[FDFS_VERSION_SIZE]; //版本
-
-
struct StructFDFSStorageDetail *psync_src_server;
-
time_t sync_until_timestamp;
-
-
time_t join_time; //storage join timestamp (create timestamp)
-
time_t up_time; //startup timestamp
-
int64_t total_mb; //total disk storage in MB
-
int64_t free_mb; //free disk storage in MB
-
int64_t changelog_offset; //changelog file offset
-
-
int64_t *path_total_mbs; //total disk storage in MB
-
int64_t *path_free_mbs; //free disk storage in MB
-
-
int store_path_count; //store base path count of each storage server
-
int subdir_count_per_path;
-
int upload_priority; //storage upload priority
-
-
int storage_port; //storage server port
-
int storage_http_port; //storage http server port
-
-
int current_write_path; //current write path index
-
-
int chg_count; //current server changed counter
-
FDFSStorageStat stat;
-
-
#ifdef WITH_HTTPD
-
int http_check_last_errno;
-
int http_check_last_status;
-
int http_check_fail_count;
-
char http_check_error_info[256];
-
#endif
-
} 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的空间
-
//初始化group的空间
-
static int tracker_mem_init_groups(FDFSGroups *pGroups)
-
{
-
int result;
-
FDFSGroupInfo **ppGroup;
-
FDFSGroupInfo **ppGroupEnd;
-
-
pGroups->alloc_size = TRACKER_MEM_ALLOC_ONCE; //每次分配2个group的空间
-
pGroups->count = 0;//group数目置0
-
pGroups->current_write_group = 0;//upload group的index置0
-
pGroups->pStoreGroup = NULL;//当前upload group为null
-
pGroups->groups = (FDFSGroupInfo **)malloc( \ //为groups分配空间,groups指向的是一个group的指针数组
-
sizeof(FDFSGroupInfo *) * pGroups->alloc_size);
-
if (pGroups->groups == NULL)
-
{
-
logCrit("file: "__FILE__", line: %d, " \
-
"malloc %d bytes fail!", __LINE__, \
-
(int)sizeof(FDFSGroupInfo *) * pGroups->alloc_size);
-
return errno != 0 ? errno : ENOMEM;
-
}
-
-
memset(pGroups->groups, 0, \ //把groups指向的数组空间置0
-
sizeof(FDFSGroupInfo *) * pGroups->alloc_size);
-
-
ppGroupEnd = pGroups->groups + pGroups->alloc_size;//指向group指针数组末尾的下一个节点
-
for (ppGroup=pGroups->groups; ppGroup<ppGroupEnd; ppGroup++)
-
{
-
*ppGroup = (FDFSGroupInfo *)malloc(sizeof(FDFSGroupInfo));//为group指针数组的每个元素分配一个group的空间
-
if (*ppGroup == NULL)
-
{
-
logCrit("file: "__FILE__", line: %d, " \
-
"malloc %d bytes fail!", \
-
__LINE__, (int)sizeof(FDFSGroupInfo));
-
return errno != 0 ? errno : ENOMEM;
-
}
-
-
memset(*ppGroup, 0, sizeof(FDFSGroupInfo));
-
}
-
-
pGroups->sorted_groups = (FDFSGroupInfo **) \//为sorted_groups分配空间,sorted_groups指向的是一个group的指针数组
-
malloc(sizeof(FDFSGroupInfo *) * pGroups->alloc_size);
-
if (pGroups->sorted_groups == NULL)
-
{
-
free(pGroups->groups);
-
pGroups->groups = NULL;
-
-
logCrit("file: "__FILE__", line: %d, " \
-
"malloc %d bytes fail!", __LINE__, \
-
(int)sizeof(FDFSGroupInfo *) * pGroups->alloc_size);
-
return errno != 0 ? errno : ENOMEM;
-
}
-
-
memset(pGroups->sorted_groups, 0, \ //把sorted_groups指向的数组空间置0
-
sizeof(FDFSGroupInfo *) * pGroups->alloc_size);
-
-
if ((result=tracker_load_data(pGroups)) != 0) //从配置文件中加载group与server的数据
-
{
-
return result;
-
}
-
-
return 0;
-
}
下图展示出了,在执行函数tracker_load_data函数之前,g_groups的初始化状态
程序给groups指针数组分配了空间,并给数组中的每个指针分配了空间,而对sorted_groups指针数组,只分配了数组空间,数组中的每个元素还未分配空间
下一节将详细分析tracker_load_data函数,该函数继续为全局变量g_gruoup中的元素分配空间
阅读(947) | 评论(0) | 转发(0) |