⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tracker_mem.c

📁 文件系统源代码!!!!! 文件系统源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
				__LINE__, data_path, \				STORAGE_SYNC_TIMESTAMP_FILENAME, \				group_name, cols, pGroup->count + 2);			result = errno != 0 ? errno : EINVAL;			break;		}		for (dest_index=0; dest_index<cols-2; dest_index++)		{			pGroup->last_sync_timestamps[src_index][dest_index] = \				atoi(trim_left(fields[2 + dest_index]));		}		src_index++;	}	fclose(fp);	if (result != 0)	{		return result;	}	pEnd = g_groups.groups + g_groups.count;	for (pGroup=g_groups.groups; pGroup<pEnd; pGroup++)	{		if (pGroup->count <= 1)		{			continue;		}		for (dest_index=0; dest_index<pGroup->count; dest_index++)		{			if (g_groups.store_server == FDFS_STORE_SERVER_FIRST)			{				int max_synced_timestamp;				max_synced_timestamp = 0;				for (src_index=0; src_index<pGroup->count; \					src_index++)				{					if (src_index == dest_index)					{						continue;					}					curr_synced_timestamp = \						pGroup->last_sync_timestamps \							[src_index][dest_index];					if (curr_synced_timestamp > \						max_synced_timestamp)					{						max_synced_timestamp = \							curr_synced_timestamp;					}				}				pGroup->all_servers[dest_index].stat. \					last_synced_timestamp = max_synced_timestamp;			}			else //round robin			{				int min_synced_timestamp;				min_synced_timestamp = 0;				for (src_index=0; src_index<pGroup->count; \					src_index++)				{					if (src_index == dest_index)					{						continue;					}					curr_synced_timestamp = \						pGroup->last_sync_timestamps \							[src_index][dest_index];					if (curr_synced_timestamp == 0)					{						continue;					}					if (min_synced_timestamp == 0)					{						min_synced_timestamp = \							curr_synced_timestamp;					}					else if (curr_synced_timestamp < \						min_synced_timestamp)					{						min_synced_timestamp = \							curr_synced_timestamp;					}				}				pGroup->all_servers[dest_index].stat. \					last_synced_timestamp = min_synced_timestamp;			}		}	}	return result;}static int tracker_load_data(){	char data_path[MAX_PATH_SIZE];	int result;	snprintf(data_path, sizeof(data_path), "%s/data", g_base_path);	if (!fileExists(data_path))	{		if (mkdir(data_path, 0755) != 0)		{			logError("file: "__FILE__", line: %d, " \				"mkdir \"%s\" fail, " \				"errno: %d, error info: %s", \				__LINE__, data_path, errno, strerror(errno));			return errno != 0 ? errno : ENOENT;		}	}	if (chdir(data_path) != 0)	{		logError("file: "__FILE__", line: %d, " \			"chdir \"%s\" fail, " \			"errno: %d, error info: %s", \			__LINE__, data_path, errno, strerror(errno));		return errno != 0 ? errno : ENOENT;	}	if (!fileExists(STORAGE_GROUPS_LIST_FILENAME))	{		return 0;	}	if ((result=tracker_load_groups(data_path)) != 0)	{		return result;	}	if ((result=tracker_load_storages(data_path)) != 0)	{		return result;	}	if ((result=tracker_malloc_all_group_path_mbs()) != 0)	{		return result;	}	if ((result=tracker_load_sync_timestamps(data_path)) != 0)	{		return result;	}	return 0;}static int tracker_save_groups(){	char filname[MAX_PATH_SIZE];	char buff[FDFS_GROUP_NAME_MAX_LEN + 64];	int fd;	FDFSGroupInfo **ppGroup;	FDFSGroupInfo **ppEnd;	int result;	int len;	snprintf(filname, sizeof(filname), "%s/data/%s", \		g_base_path, STORAGE_GROUPS_LIST_FILENAME);	if ((fd=open(filname, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0)	{		logError("file: "__FILE__", line: %d, " \			"open \"%s\" fail, " \			"errno: %d, error info: %s", \			__LINE__, filname, errno, strerror(errno));		return errno != 0 ? errno : ENOENT;	}	result = 0;	ppEnd = g_groups.sorted_groups + g_groups.count;	for (ppGroup=g_groups.sorted_groups; ppGroup<ppEnd; ppGroup++)	{		len = sprintf(buff, "%s%c%d%c%d%c%d\n", \				(*ppGroup)->group_name, \				STORAGE_DATA_FIELD_SEPERATOR, \				(*ppGroup)->storage_port, \				STORAGE_DATA_FIELD_SEPERATOR, \				(*ppGroup)->store_path_count, \				STORAGE_DATA_FIELD_SEPERATOR, \				(*ppGroup)->subdir_count_per_path);		if (write(fd, buff, len) != len)		{			logError("file: "__FILE__", line: %d, " \				"write to file \"%s\" fail, " \				"errno: %d, error info: %s", \				__LINE__, filname, errno, strerror(errno));			result = errno != 0 ? errno : EIO;			break;		}	}	close(fd);	return result;}int tracker_save_storages(){	char filname[MAX_PATH_SIZE];	char buff[256];	int fd;	int len;	FDFSGroupInfo **ppGroup;	FDFSGroupInfo **ppGroupEnd;	FDFSStorageDetail *pStorage;	FDFSStorageDetail *pStorageEnd;	int result;	snprintf(filname, sizeof(filname), "%s/data/%s", \		g_base_path, STORAGE_SERVERS_LIST_FILENAME);	if ((fd=open(filname, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0)	{		logError("file: "__FILE__", line: %d, " \			"open \"%s\" fail, " \			"errno: %d, error info: %s", \			__LINE__, filname, errno, strerror(errno));		return errno != 0 ? errno : ENOENT;	}	result = 0;	ppGroupEnd = g_groups.sorted_groups + g_groups.count;	for (ppGroup=g_groups.sorted_groups; \		(ppGroup < ppGroupEnd) && (result == 0); ppGroup++)	{		pStorageEnd = (*ppGroup)->all_servers + (*ppGroup)->count;		for (pStorage=(*ppGroup)->all_servers; \			pStorage<pStorageEnd; pStorage++)		{			if (pStorage->status == FDFS_STORAGE_STATUS_DELETED)			{				continue;			}			len = sprintf(buff, \				"%s%c" \				"%s%c" \				"%d%c" \				"%s%c" \				"%d%c" \				INT64_PRINTF_FORMAT"%c" \				INT64_PRINTF_FORMAT"%c" \				INT64_PRINTF_FORMAT"%c" \				INT64_PRINTF_FORMAT"%c" \				INT64_PRINTF_FORMAT"%c" \				INT64_PRINTF_FORMAT"%c" \				INT64_PRINTF_FORMAT"%c" \				INT64_PRINTF_FORMAT"%c" \				INT64_PRINTF_FORMAT"%c" \				INT64_PRINTF_FORMAT"%c" \				"%d%c" \				"%d\n", \				(*ppGroup)->group_name, \				STORAGE_DATA_FIELD_SEPERATOR, \				pStorage->ip_addr, \				STORAGE_DATA_FIELD_SEPERATOR, \				pStorage->status, \				STORAGE_DATA_FIELD_SEPERATOR, \				(pStorage->psync_src_server != NULL ? \				pStorage->psync_src_server->ip_addr : ""), 					STORAGE_DATA_FIELD_SEPERATOR, \				(int)pStorage->sync_until_timestamp, \				STORAGE_DATA_FIELD_SEPERATOR, \				pStorage->stat.total_upload_count, \				STORAGE_DATA_FIELD_SEPERATOR, \				pStorage->stat.success_upload_count, \				STORAGE_DATA_FIELD_SEPERATOR, \				pStorage->stat.total_set_meta_count, \				STORAGE_DATA_FIELD_SEPERATOR, \				pStorage->stat.success_set_meta_count, \				STORAGE_DATA_FIELD_SEPERATOR, \				pStorage->stat.total_delete_count, \				STORAGE_DATA_FIELD_SEPERATOR, \				pStorage->stat.success_delete_count, \				STORAGE_DATA_FIELD_SEPERATOR, \				pStorage->stat.total_download_count, \				STORAGE_DATA_FIELD_SEPERATOR, \				pStorage->stat.success_download_count, \				STORAGE_DATA_FIELD_SEPERATOR, \				pStorage->stat.total_get_meta_count, \				STORAGE_DATA_FIELD_SEPERATOR, \				pStorage->stat.success_get_meta_count, \				STORAGE_DATA_FIELD_SEPERATOR, \				(int)(pStorage->stat.last_source_update), \				STORAGE_DATA_FIELD_SEPERATOR, \				(int)(pStorage->stat.last_sync_update) \	 		    );			if (write(fd, buff, len) != len)			{				logError("file: "__FILE__", line: %d, " \					"write to file \"%s\" fail, " \					"errno: %d, error info: %s", \					__LINE__, filname, \					errno, strerror(errno));				result = errno != 0 ? errno : EIO;				break;			}		}	}	close(fd);	return result;}int tracker_save_sync_timestamps(){	char filname[MAX_PATH_SIZE];	char buff[512];	int fd;	int len;	FDFSGroupInfo **ppGroup;	FDFSGroupInfo **ppGroupEnd;	int **last_sync_timestamps;	int i;	int k;	int result;	snprintf(filname, sizeof(filname), "%s/data/%s", \		g_base_path, STORAGE_SYNC_TIMESTAMP_FILENAME);	if ((fd=open(filname, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0)	{		logError("file: "__FILE__", line: %d, " \			"open \"%s\" fail, " \			"errno: %d, error info: %s", \			__LINE__, filname, errno, strerror(errno));		return errno != 0 ? errno : ENOENT;	}	result = 0;	ppGroupEnd = g_groups.sorted_groups + g_groups.count;	for (ppGroup=g_groups.sorted_groups; \		(ppGroup < ppGroupEnd) && (result == 0); ppGroup++)	{		last_sync_timestamps = (*ppGroup)->last_sync_timestamps;		for (i=0; i<(*ppGroup)->count; i++)		{			if ((*ppGroup)->all_servers[i].status == \				FDFS_STORAGE_STATUS_DELETED)			{				continue;			}			len = sprintf(buff, "%s%c%s", (*ppGroup)->group_name, \				STORAGE_DATA_FIELD_SEPERATOR, \				(*ppGroup)->all_servers[i].ip_addr);			for (k=0; k<(*ppGroup)->count; k++)			{				if ((*ppGroup)->all_servers[k].status == \					FDFS_STORAGE_STATUS_DELETED)				{					continue;				}				len += sprintf(buff + len, "%c%d", \					STORAGE_DATA_FIELD_SEPERATOR, \					last_sync_timestamps[i][k]);			}			*(buff + len) = '\n';			len++;			if (write(fd, buff, len) != len)			{				logError("file: "__FILE__", line: %d, " \					"write to file \"%s\" fail, " \					"errno: %d, error info: %s", \					__LINE__, filname, \					errno, strerror(errno));				result = errno != 0 ? errno : EIO;				break;			}		}	}	close(fd);	return result;}int tracker_mem_init(){	FDFSGroupInfo *pGroup;	FDFSGroupInfo *pEnd;	int *ref_count;	int result;	if ((result=init_pthread_lock(&mem_thread_lock)) != 0)	{		return result;	}	g_groups.alloc_size = TRACKER_MEM_ALLOC_ONCE;	g_groups.count = 0;	g_groups.current_write_group = 0;	g_groups.pStoreGroup = NULL;	g_groups.groups = (FDFSGroupInfo *)malloc( \			sizeof(FDFSGroupInfo) * g_groups.alloc_size);	if (g_groups.groups == NULL)	{		logCrit("file: "__FILE__", line: %d, " \			"malloc %d bytes fail, program exit!", \			__LINE__, sizeof(FDFSGroupInfo) * g_groups.alloc_size);		return errno != 0 ? errno : ENOMEM;	}	memset(g_groups.groups, 0, \		sizeof(FDFSGroupInfo) * g_groups.alloc_size);	ref_count = (int *)malloc(sizeof(int));	if (ref_count == NULL)	{		free(g_groups.groups);		g_groups.groups = NULL;		logCrit("file: "__FILE__", line: %d, " \			"malloc %d bytes fail, program exit!", \			__LINE__, sizeof(int));		return errno != 0 ? errno : ENOMEM;	}	*ref_count = 0;	pEnd = g_groups.groups + g_groups.alloc_size;	for (pGroup=g_groups.groups; pGroup<pEnd; pGroup++)	{		pGroup->ref_count = ref_count;	}	g_groups.sorted_groups = (FDFSGroupInfo **) \			malloc(sizeof(FDFSGroupInfo *) * g_groups.alloc_size);	if (g_groups.sorted_groups == NULL)	{		free(ref_count);		free(g_groups.groups);		g_groups.groups = NULL;		logCrit("file: "__FILE__", line: %d, " \			"malloc %d bytes fail, program exit!", __LINE__, \			sizeof(FDFSGroupInfo *) * g_groups.alloc_size);		return errno != 0 ? errno : ENOMEM;	}	memset(g_groups.sorted_groups, 0, \		sizeof(FDFSGroupInfo *) * g_groups.alloc_size);	if ((result=tracker_load_data()) != 0)	{		return result;	}	/*	if (g_groups.store_lookup == FDFS_STORE_LOOKUP_SPEC_GROUP)	{		g_groups.pStoreGroup = tracker_mem_get_group( \					g_groups.store_group);	}	*/	return 0;}static void tracker_free_last_sync_timestamps(int **last_sync_timestamps, \		const int alloc_size){	int i;	if (last_sync_timestamps != NULL)	{		for (i=0; i<alloc_size; i++)		{			if (last_sync_timestamps[i] != NULL)			{				free(last_sync_timestamps[i]);				last_sync_timestamps[i] = NULL;			}		}		free(last_sync_timestamps);	}}static int **tracker_malloc_last_sync_timestamps(const int alloc_size, \		int *err_no){	int **results;	int i;	results = (int **)malloc(sizeof(int *) * alloc_size);	if (results == NULL)	{		*err_no = errno != 0 ? errno : ENOMEM;		logError("file: "__FILE__", line: %d, " \			"malloc %d bytes fail", __LINE__, \			sizeof(int *) * alloc_size);		return NULL;	}	memset(results, 0, sizeof(int *) * alloc_size);	for (i=0; i<alloc_size; i++)	{		results[i] = (int *)malloc(sizeof(int) * alloc_size);		if (results[i] == NULL)		{			*err_no = errno != 0 ? errno : ENOMEM;			logError("file: "__FILE__", line: %d, " \				"malloc %d bytes fail", __LINE__, \				sizeof(int) * alloc_size);			tracker_free_last_sync_timestamps(results, alloc_size);			return NULL;		}		memset(results[i], 0, sizeof(int) * alloc_size);	}	*err_no = 0;	return results;}static int tracker_mem_init_group(FDFSGroupInfo *pGroup){	int *ref_count;	FDFSStorageDetail *pServer;	FDFSStorageDetail *pEnd;	int err_no;	pGroup->alloc_size = TRACKER_MEM_ALLOC_ONCE;	pGroup->count = 0;	pGroup->all_servers = (FDFSStorageDetail *) \			malloc(sizeof(FDFSStorageDetail) * pGroup->alloc_size);	if (pGroup->all_servers == NULL)	{		logError("file: "__FILE__", line: %d, " \			"malloc %d bytes fail", __LINE__, \			sizeof(FDFSStorageDetail) * pGroup->alloc_size);		return errno != 0 ? errno : ENOMEM;	}	memset(pGroup->all_servers, 0, \		sizeof(FDFSStorageDetail) * pGroup->alloc_size);	pGroup->sorted_servers = (FDFSStorageDetail **) \		malloc(sizeof(FDFSStorageDetail *) * pGroup->alloc_size);	if (pGroup->sorted_servers == NULL)	{		logError("file: "__FILE__", line: %d, " \			"malloc %d bytes fail", __LINE__, \			sizeof(FDFSStorageDetail *) * pGroup->alloc_size);		return errno != 0 ? errno : ENOMEM;	}	memset(pGroup->sorted_servers, 0, \		sizeof(FDFSStorageDetail *) * pGroup->alloc_size);	pGroup->active_servers = (FDFSStorageDetail **) \		malloc(sizeof(FDFSStorageDetail *) * pGroup->alloc_size);	if (pGroup->active_servers == NULL)	{		logError("file: "__FILE__", line: %d, " \			"malloc %d bytes fail", __LINE__, \			sizeof(FDFSStorageDetail *) * pGroup->alloc_size);		return errno != 0 ? errno : ENOMEM;	}	memset(pGroup->active_servers, 0, \		sizeof(FDFSStorageDetail *) * pGroup->alloc_size);	ref_count = (int *)malloc(sizeof(int));	if (ref_count == NULL)	{		logError("file: "__FILE__", line: %d, " \			"malloc %d bytes fail", \			__LINE__, sizeof(int));		return errno != 0 ? errno : ENOMEM;	}	*ref_count = 0;	pEnd = pGroup->all_servers + pGroup->alloc_size;	for (pServer=pGroup->all_servers; pServer<pEnd; pServer++)	{		pServer->ref_count = ref_count;	}	pGroup->last_sync_timestamps = tracker_malloc_last_sync_timestamps( \			pGroup->alloc_size, &err_no);	return err_no;}static void tracker_mem_free_group(FDFSGroupInfo *pGroup){	if (pGroup->sorted_servers != NULL)	{		free(pGroup->sorted_servers);		pGroup->sorted_servers = NULL;	}	if (pGroup->active_servers != NULL)	{		free(pGroup->active_servers);		pGroup->active_servers = NULL;	}	if (pGroup->all_servers != NULL)	{		free(pGroup->all_servers[0].ref_count);		free(pGroup->all_servers);		pGroup->all_servers = NULL;	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -