Example #1
0
int fdfs_load_storage_ids(char *content, const char *pStorageIdsFilename)
{
	char **lines;
	char *line;
	char *id;
	char *group_name;
	char *pHost;
	char *pIpAddr;
	char *pPort;
	FDFSStorageIdInfo *pStorageIdInfo;
	FDFSStorageIdInfo **ppStorageIdInfo;
	FDFSStorageIdInfo **ppStorageIdEnd;
	int alloc_bytes;
	int result;
	int line_count;
	int i;

	lines = split(content, '\n', 0, &line_count);
	if (lines == NULL)
	{
		return ENOMEM;
	}

	result = 0;
	do
	{
		g_storage_id_count = 0;
		for (i=0; i<line_count; i++)
		{
			trim(lines[i]);
			if (*lines[i] == '\0' || *lines[i] == '#')
			{
				continue;
			}
			g_storage_id_count++;
		}

		if (g_storage_id_count == 0)
		{
			logError("file: "__FILE__", line: %d, " \
				"config file: %s, no storage id!", \
				__LINE__, pStorageIdsFilename);
			result = ENOENT;
			break;
		}

		alloc_bytes = sizeof(FDFSStorageIdInfo) * g_storage_id_count;
		g_storage_ids_by_ip = (FDFSStorageIdInfo *)malloc(alloc_bytes);
		if (g_storage_ids_by_ip == NULL)
		{
			result = errno != 0 ? errno : ENOMEM;
			logError("file: "__FILE__", line: %d, " \
				"malloc %d bytes fail, " \
				"errno: %d, error info: %s", __LINE__, \
				alloc_bytes, result, STRERROR(result));
			break;
		}
		memset(g_storage_ids_by_ip, 0, alloc_bytes);

		alloc_bytes = sizeof(FDFSStorageIdInfo *) * g_storage_id_count;
		g_storage_ids_by_id = (FDFSStorageIdInfo **)malloc(alloc_bytes);
		if (g_storage_ids_by_id == NULL)
		{
			result = errno != 0 ? errno : ENOMEM;
			logError("file: "__FILE__", line: %d, " \
				"malloc %d bytes fail, " \
				"errno: %d, error info: %s", __LINE__, \
				alloc_bytes, result, STRERROR(result));
			free(g_storage_ids_by_ip);
			break;
		}
		memset(g_storage_ids_by_id, 0, alloc_bytes);

		pStorageIdInfo = g_storage_ids_by_ip;
		for (i=0; i<line_count; i++)
		{
			line = lines[i];
			if (*line == '\0' || *line == '#')
			{
				continue;
			}

			id = line;
			group_name = line;
			while (!(*group_name == ' ' || *group_name == '\t' \
				|| *group_name == '\0'))
			{
				group_name++;
			}

			if (*group_name == '\0')
			{
				logError("file: "__FILE__", line: %d, " \
					"config file: %s, line no: %d, " \
					"content: %s, invalid format, " \
					"expect group name and ip address!", \
					__LINE__, pStorageIdsFilename, \
					i + 1, line);
				result = EINVAL;
				break;
			}

			*group_name = '\0';
			group_name++;  //skip space char
			while (*group_name == ' ' || *group_name == '\t')
			{
				group_name++;
			}
		
			pHost = group_name;
			while (!(*pHost == ' ' || *pHost == '\t' \
				|| *pHost == '\0'))
			{
				pHost++;
			}

			if (*pHost == '\0')
			{
				logError("file: "__FILE__", line: %d, " \
					"config file: %s, line no: %d, " \
					"content: %s, invalid format, " \
					"expect ip address!", __LINE__, \
					pStorageIdsFilename, i + 1, line);
				result = EINVAL;
				break;
			}

			*pHost = '\0';
			pHost++;  //skip space char
			while (*pHost == ' ' || *pHost == '\t')
			{
				pHost++;
			}

            pIpAddr = pHost;
            pPort = strchr(pHost, ':');
            if (pPort != NULL)
            {
                *pPort = '\0';
                pStorageIdInfo->port = atoi(pPort + 1);
            }
            else
            {
                pStorageIdInfo->port = 0;
            }
			if (getIpaddrByName(pIpAddr, pStorageIdInfo->ip_addr, \
				sizeof(pStorageIdInfo->ip_addr)) == INADDR_NONE)
			{
				logError("file: "__FILE__", line: %d, " \
					"invalid host name: %s", __LINE__, pIpAddr);
				result = EINVAL;
				break;
			}

			if (!fdfs_is_server_id_valid(id))
			{
				logError("file: "__FILE__", line: %d, " \
					"invalid server id: \"%s\", " \
					"which must be a none zero start " \
					"integer, such as 100001", __LINE__, id);
				result = EINVAL;
				break;
			}

			snprintf(pStorageIdInfo->id, \
				sizeof(pStorageIdInfo->id), "%s", id);
			snprintf(pStorageIdInfo->group_name, \
				sizeof(pStorageIdInfo->group_name), \
				"%s", group_name);
			pStorageIdInfo++;
		}
	} while (0);

	freeSplit(lines);
	if (result != 0)
	{
		return result;
	}

	logDebug("file: "__FILE__", line: %d, " \
		"g_storage_id_count: %d", __LINE__, g_storage_id_count);
	pStorageIdInfo = g_storage_ids_by_ip;
	for (i=0; i<g_storage_id_count; i++)
	{
        char szPortPart[16];
        if (pStorageIdInfo->port > 0)
        {
            sprintf(szPortPart, ":%d", pStorageIdInfo->port);
        }
        else
        {
            *szPortPart = '\0';
        }
		logDebug("%s  %s  %s%s", pStorageIdInfo->id,
			pStorageIdInfo->group_name,
			pStorageIdInfo->ip_addr, szPortPart);

		pStorageIdInfo++;
	}
	
	ppStorageIdEnd = g_storage_ids_by_id + g_storage_id_count;
	pStorageIdInfo = g_storage_ids_by_ip;
	for (ppStorageIdInfo=g_storage_ids_by_id; ppStorageIdInfo < \
		ppStorageIdEnd; ppStorageIdInfo++)
	{
		*ppStorageIdInfo = pStorageIdInfo++;
	}

	qsort(g_storage_ids_by_ip, g_storage_id_count, \
		sizeof(FDFSStorageIdInfo), fdfs_cmp_group_name_and_ip);
	qsort(g_storage_ids_by_id, g_storage_id_count, \
		sizeof(FDFSStorageIdInfo *), fdfs_cmp_server_id);

	return result;
}
int storage_get_params_from_tracker()
{
	IniContext iniContext;
	int result;
	bool use_trunk_file;
	char reserved_space_str[32];
	char *pIdType;

	if ((result=fdfs_get_ini_context_from_tracker(&g_tracker_group, \
		&iniContext, (bool * volatile)&g_continue_flag, \
		g_client_bind_addr, g_bind_addr)) != 0)
	{
		return result;
	}

	g_storage_ip_changed_auto_adjust = iniGetBoolValue(NULL, \
			"storage_ip_changed_auto_adjust", \
			&iniContext, false);

	g_store_path_mode = iniGetIntValue(NULL, "store_path", &iniContext, \
				FDFS_STORE_PATH_ROUND_ROBIN);

	if ((result=fdfs_parse_storage_reserved_space(&iniContext, \
		&g_storage_reserved_space)) != 0)
	{
		iniFreeContext(&iniContext);
		return result;
	}
	if (g_storage_reserved_space.flag == \
		TRACKER_STORAGE_RESERVED_SPACE_FLAG_MB)
	{
		g_avg_storage_reserved_mb = g_storage_reserved_space.rs.mb \
						/ g_fdfs_store_paths.count;
	}
	else
	{
		g_avg_storage_reserved_mb = 0;
	}

	g_use_storage_id = iniGetBoolValue(NULL, "use_storage_id", \
				&iniContext, false);
	use_trunk_file = iniGetBoolValue(NULL, "use_trunk_file", \
				&iniContext, false);
	g_slot_min_size = iniGetIntValue(NULL, "slot_min_size", \
				&iniContext, 256);
	g_trunk_file_size = iniGetIntValue(NULL, "trunk_file_size", \
				&iniContext, 64 * 1024 * 1024);
	g_slot_max_size = iniGetIntValue(NULL, "slot_max_size", \
				&iniContext, g_trunk_file_size / 2);

	g_trunk_create_file_advance = iniGetBoolValue(NULL, \
			"trunk_create_file_advance", &iniContext, false);
	if ((result=get_time_item_from_conf(&iniContext, \
               	"trunk_create_file_time_base", \
		&g_trunk_create_file_time_base, 2, 0)) != 0)
	{
		iniFreeContext(&iniContext);
		return result;
	}
	g_trunk_create_file_interval = iniGetIntValue(NULL, \
			"trunk_create_file_interval", &iniContext, \
			86400);
	g_trunk_create_file_space_threshold = iniGetInt64Value(NULL, \
			"trunk_create_file_space_threshold", \
			&iniContext, 0);

	g_trunk_init_check_occupying = iniGetBoolValue(NULL, \
			"trunk_init_check_occupying", &iniContext, false);
	g_trunk_init_reload_from_binlog = iniGetBoolValue(NULL, \
			"trunk_init_reload_from_binlog", &iniContext, false);
	g_trunk_compress_binlog_min_interval = iniGetIntValue(NULL, \
			"trunk_compress_binlog_min_interval", &iniContext, 0);

	g_store_slave_file_use_link = iniGetBoolValue(NULL, \
			"store_slave_file_use_link", &iniContext, false);

	pIdType = iniGetStrValue(NULL, "id_type_in_filename", &iniContext);
	if (pIdType != NULL && strcasecmp(pIdType, "id") == 0)
	{
		if (g_use_storage_id)
		{
			g_id_type_in_filename = FDFS_ID_TYPE_SERVER_ID;
		}
		else
		{
			g_id_type_in_filename = FDFS_ID_TYPE_IP_ADDRESS;
		}
	}
	else 
	{
		g_id_type_in_filename = FDFS_ID_TYPE_IP_ADDRESS;
	}

	iniFreeContext(&iniContext);

	if (use_trunk_file && !g_if_use_trunk_file)
	{
		if ((result=trunk_sync_init()) != 0)
		{
			return result;
		}
	}
	g_if_use_trunk_file = use_trunk_file;

	logInfo("file: "__FILE__", line: %d, " \
		"use_storage_id=%d, " \
		"id_type_in_filename=%s, " \
		"storage_ip_changed_auto_adjust=%d, " \
		"store_path=%d, " \
		"reserved_storage_space=%s, " \
		"use_trunk_file=%d, " \
		"slot_min_size=%d, " \
		"slot_max_size=%d MB, " \
		"trunk_file_size=%d MB, " \
		"trunk_create_file_advance=%d, " \
		"trunk_create_file_time_base=%02d:%02d, " \
		"trunk_create_file_interval=%d, " \
		"trunk_create_file_space_threshold=%d GB, " \
		"trunk_init_check_occupying=%d, "   \
		"trunk_init_reload_from_binlog=%d, " \
		"trunk_compress_binlog_min_interval=%d, " \
		"store_slave_file_use_link=%d", \
		__LINE__, g_use_storage_id, \
		g_id_type_in_filename == FDFS_ID_TYPE_SERVER_ID ? "id" : "ip", \
		g_storage_ip_changed_auto_adjust, \
		g_store_path_mode, fdfs_storage_reserved_space_to_string( \
			&g_storage_reserved_space, reserved_space_str), \
		g_if_use_trunk_file, g_slot_min_size, \
		g_slot_max_size / FDFS_ONE_MB, \
		g_trunk_file_size / FDFS_ONE_MB, \
		g_trunk_create_file_advance, \
		g_trunk_create_file_time_base.hour, \
		g_trunk_create_file_time_base.minute, \
		g_trunk_create_file_interval, \
		(int)(g_trunk_create_file_space_threshold / \
		(FDFS_ONE_MB * 1024)), g_trunk_init_check_occupying, \
		g_trunk_init_reload_from_binlog, \
		g_trunk_compress_binlog_min_interval, \
		g_store_slave_file_use_link);

	if (g_use_storage_id && *g_sync_src_id != '\0' && \
		!fdfs_is_server_id_valid(g_sync_src_id))
	{
		if ((result=storage_convert_src_server_id()) == 0)
		{
			storage_write_to_sync_ini_file();
		}
		else
		{
			if (result == ENOENT)
			{
				*g_sync_src_id = '\0';
				storage_write_to_sync_ini_file();
			}
			else
			{
				return result;
			}
		}
	}

	return 0;
}