Esempio n. 1
0
static int trunk_client_trunk_do_alloc_space(TrackerServerInfo *pTrunkServer, \
		const int file_size, FDFSTrunkFullInfo *pTrunkInfo)
{
	TrackerHeader *pHeader;
	char *p;
	int result;
	char out_buff[sizeof(TrackerHeader)+FDFS_GROUP_NAME_MAX_LEN+4];
	FDFSTrunkInfoBuff trunkBuff;
	int64_t in_bytes;

	pHeader = (TrackerHeader *)out_buff;
	memset(out_buff, 0, sizeof(out_buff));
	snprintf(out_buff + sizeof(TrackerHeader), sizeof(out_buff) - \
		sizeof(TrackerHeader),  "%s", g_group_name);
	int2buff(file_size, out_buff + sizeof(TrackerHeader) \
		 + FDFS_GROUP_NAME_MAX_LEN);
	long2buff(FDFS_GROUP_NAME_MAX_LEN + 4, pHeader->pkg_len);
	pHeader->cmd = STORAGE_PROTO_CMD_TRUNK_ALLOC_SPACE;

	if ((result=tcpsenddata_nb(pTrunkServer->sock, out_buff, \
			sizeof(out_buff), g_fdfs_network_timeout)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"send data to storage server %s:%d fail, " \
			"errno: %d, error info: %s", __LINE__, \
			pTrunkServer->ip_addr, pTrunkServer->port, \
			result, STRERROR(result));

		return result;
	}

	p = (char *)&trunkBuff;
	if ((result=fdfs_recv_response(pTrunkServer, \
		&p, sizeof(FDFSTrunkInfoBuff), &in_bytes)) != 0)
	{
		return result;
	}

	if (in_bytes != sizeof(FDFSTrunkInfoBuff))
	{
		logError("file: "__FILE__", line: %d, " \
			"storage server %s:%d, recv body length: %d invalid, " \
			"expect body length: %d", __LINE__, \
			pTrunkServer->ip_addr, pTrunkServer->port, \
			(int)in_bytes, (int)sizeof(FDFSTrunkInfoBuff));
		return EINVAL;
	}

	pTrunkInfo->path.store_path_index = trunkBuff.store_path_index;
	pTrunkInfo->path.sub_path_high = trunkBuff.sub_path_high;
	pTrunkInfo->path.sub_path_low = trunkBuff.sub_path_low;
	pTrunkInfo->file.id = buff2int(trunkBuff.id);
	pTrunkInfo->file.offset = buff2int(trunkBuff.offset);
	pTrunkInfo->file.size = buff2int(trunkBuff.size);
	pTrunkInfo->status = FDFS_TRUNK_STATUS_HOLD;

	return 0;
}
Esempio n. 2
0
static int trunk_client_trunk_confirm_or_free(TrackerServerInfo *pTrunkServer,\
		const FDFSTrunkFullInfo *pTrunkInfo, const int cmd, \
		const int status)
{
	TrackerHeader *pHeader;
	FDFSTrunkInfoBuff *pTrunkBuff;
	int64_t in_bytes;
	int result;
	char out_buff[sizeof(TrackerHeader) \
		+ STORAGE_TRUNK_ALLOC_CONFIRM_REQ_BODY_LEN];

	pHeader = (TrackerHeader *)out_buff;
	pTrunkBuff = (FDFSTrunkInfoBuff *)(out_buff + sizeof(TrackerHeader) \
			 + FDFS_GROUP_NAME_MAX_LEN);
	memset(out_buff, 0, sizeof(out_buff));
	snprintf(out_buff + sizeof(TrackerHeader), sizeof(out_buff) - \
		sizeof(TrackerHeader),  "%s", g_group_name);
	long2buff(STORAGE_TRUNK_ALLOC_CONFIRM_REQ_BODY_LEN, pHeader->pkg_len);
	pHeader->cmd = cmd;
	pHeader->status = status;

	pTrunkBuff->store_path_index = pTrunkInfo->path.store_path_index;
	pTrunkBuff->sub_path_high = pTrunkInfo->path.sub_path_high;
	pTrunkBuff->sub_path_low = pTrunkInfo->path.sub_path_low;
	int2buff(pTrunkInfo->file.id, pTrunkBuff->id);
	int2buff(pTrunkInfo->file.offset, pTrunkBuff->offset);
	int2buff(pTrunkInfo->file.size, pTrunkBuff->size);

	if ((result=tcpsenddata_nb(pTrunkServer->sock, out_buff, \
			sizeof(out_buff), g_fdfs_network_timeout)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"send data to storage server %s:%d fail, " \
			"errno: %d, error info: %s", __LINE__, \
			pTrunkServer->ip_addr, pTrunkServer->port, \
			result, STRERROR(result));

		return result;
	}

	if ((result=fdfs_recv_header(pTrunkServer, &in_bytes)) != 0)
	{
		return result;
	}

	if (in_bytes != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"storage server %s:%d response data " \
			"length: "INT64_PRINTF_FORMAT" is invalid, " \
			"should == 0", __LINE__, pTrunkServer->ip_addr, \
			pTrunkServer->port, in_bytes);
		return EINVAL;
	}

	return 0;
}
static int storage_do_fetch_binlog(ConnectionInfo *pSrcStorage, \
		const int store_path_index)
{
	char out_buff[sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + 1];
	char full_binlog_filename[MAX_PATH_SIZE];
	TrackerHeader *pHeader;
	char *pBasePath;
	int64_t in_bytes;
	int64_t file_bytes;
	int result;

	pBasePath = g_fdfs_store_paths.paths[store_path_index];
	recovery_get_binlog_filename(pBasePath, full_binlog_filename);

	memset(out_buff, 0, sizeof(out_buff));
	pHeader = (TrackerHeader *)out_buff;

	long2buff(FDFS_GROUP_NAME_MAX_LEN + 1, pHeader->pkg_len);
	pHeader->cmd = STORAGE_PROTO_CMD_FETCH_ONE_PATH_BINLOG;
	strcpy(out_buff + sizeof(TrackerHeader), g_group_name);
	*(out_buff + sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN) = \
			store_path_index;

	if((result=tcpsenddata_nb(pSrcStorage->sock, out_buff, \
		sizeof(out_buff), g_fdfs_network_timeout)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"tracker server %s:%d, send data fail, " \
			"errno: %d, error info: %s.", \
			__LINE__, pSrcStorage->ip_addr, pSrcStorage->port, \
			result, STRERROR(result));
		return result;
	}

	if ((result=fdfs_recv_header(pSrcStorage, &in_bytes)) != 0)
	{
		return result;
	}

	if ((result=tcprecvfile(pSrcStorage->sock, full_binlog_filename, \
				in_bytes, 0, g_fdfs_network_timeout, \
				&file_bytes)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"tracker server %s:%d, tcprecvfile fail, " \
			"errno: %d, error info: %s.", \
			__LINE__, pSrcStorage->ip_addr, pSrcStorage->port, \
			result, STRERROR(result));
		return result;
	}

	logInfo("file: "__FILE__", line: %d, " \
		"recovery binlog file size: %"PRId64, \
		__LINE__, file_bytes);

	return 0;
}
Esempio n. 4
0
int fdht_client_heart_beat(FDHTServerInfo *pServer)
{
	int result;
	FDHTProtoHeader header;
	int in_bytes;

	memset(&header, 0, sizeof(header));
	header.cmd = FDHT_PROTO_CMD_HEART_BEAT;
	header.keep_alive = 1;

	if ((result=tcpsenddata_nb(pServer->sock, &header, \
		sizeof(header), g_fdht_network_timeout)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"send data to server %s:%d fail, " \
			"errno: %d, error info: %s", __LINE__, \
			pServer->ip_addr, pServer->port, \
			result, STRERROR(result));
		return result;
	}

	if ((result=fdht_recv_header(pServer, &in_bytes)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"recv data from server %s:%d fail, " \
			"errno: %d, error info: %s", __LINE__, \
			pServer->ip_addr, pServer->port, \
			result, STRERROR(result));
		return result;
	}

	if (in_bytes != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"server %s:%d reponse bytes: %d != 0", \
			__LINE__, pServer->ip_addr, \
			pServer->port, in_bytes);
		return EINVAL;
	}

	return 0;
}
Esempio n. 5
0
static int fdfs_do_parameter_req(ConnectionInfo *pTrackerServer, \
	char *buff, const int buff_size)
{
	char out_buff[sizeof(TrackerHeader)];
	TrackerHeader *pHeader;
	int64_t in_bytes;
	int result;

	memset(out_buff, 0, sizeof(out_buff));
	pHeader = (TrackerHeader *)out_buff;
	pHeader->cmd = TRACKER_PROTO_CMD_STORAGE_PARAMETER_REQ;
	if((result=tcpsenddata_nb(pTrackerServer->sock, out_buff, \
		sizeof(TrackerHeader), g_fdfs_network_timeout)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"tracker server %s:%d, send data fail, " \
			"errno: %d, error info: %s.", \
			__LINE__, pTrackerServer->ip_addr, \
			pTrackerServer->port, \
			result, STRERROR(result));
		return result;
	}

	result = fdfs_recv_response(pTrackerServer, &buff, buff_size, &in_bytes);
	if (result != 0)
	{
		return result;
	}

	if (in_bytes >= buff_size)
	{
		logError("file: "__FILE__", line: %d, " \
			"server: %s:%d, recv body bytes: " \
			INT64_PRINTF_FORMAT" exceed max: %d", \
			__LINE__, pTrackerServer->ip_addr, \
			pTrackerServer->port, in_bytes, buff_size);
		return ENOSPC;
	}

	*(buff + in_bytes) = '\0';
	return 0;
}
Esempio n. 6
0
int fdht_quit(FDHTServerInfo *pServer)
{
	FDHTProtoHeader header;
	int result;

	memset(&header, 0, sizeof(header));
	header.cmd = FDHT_PROTO_CMD_QUIT;
	result = tcpsenddata_nb(pServer->sock, &header, sizeof(header), \
				g_fdht_network_timeout);
	if(result != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"server ip: %s, send data fail, " \
			"errno: %d, error info: %s", \
			__LINE__, pServer->ip_addr, \
			result, STRERROR(result));
		return result;
	}

	return 0;
}
Esempio n. 7
0
int fdfs_quit(ConnectionInfo *pTrackerServer)
{
	TrackerHeader header;
	int result;

	memset(&header, 0, sizeof(header));
	header.cmd = FDFS_PROTO_CMD_QUIT;
	result = tcpsenddata_nb(pTrackerServer->sock, &header, \
			sizeof(header), g_fdfs_network_timeout);
	if(result != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"tracker server: %s:%d, send data fail, " \
			"errno: %d, error info: %s", \
			__LINE__, pTrackerServer->ip_addr, \
			pTrackerServer->port, \
			result, STRERROR(result));
		return result;
	}

	return 0;
}
Esempio n. 8
0
int fdfs_deal_no_body_cmd(ConnectionInfo *pTrackerServer, const int cmd)
{
	TrackerHeader header;
	int result;
	int64_t in_bytes;

	memset(&header, 0, sizeof(header));
	header.cmd = cmd;
	result = tcpsenddata_nb(pTrackerServer->sock, &header, \
			sizeof(header), g_fdfs_network_timeout);
	if(result != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"tracker server ip: %s, send data fail, " \
			"errno: %d, error info: %s", \
			__LINE__, pTrackerServer->ip_addr, \
			result, STRERROR(result));
		return result;
	}

	result = fdfs_recv_header(pTrackerServer, &in_bytes);
	if (result != 0)
	{
		return result;
	}

	if (in_bytes == 0)
	{
		return 0;
	}
	else
	{
		logError("file: "__FILE__", line: %d, " \
			"server ip: %s, expect body length 0, " \
			"but received: "INT64_PRINTF_FORMAT, __LINE__, \
			pTrackerServer->ip_addr, in_bytes);
		return EINVAL;
	}
}
Esempio n. 9
0
int tracker_set_trunk_server(TrackerServerGroup *pTrackerGroup, \
		const char *group_name, const char *storage_id, \
		char *new_trunk_server_id)
{
	TrackerHeader *pHeader;
	ConnectionInfo *conn;
	ConnectionInfo *pServer;
	ConnectionInfo *pEnd;
	ConnectionInfo tracker_server;
	char out_buff[sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + \
			FDFS_STORAGE_ID_MAX_SIZE];
	char in_buff[FDFS_STORAGE_ID_MAX_SIZE];
	char *pInBuff;
	int64_t in_bytes;
	int result;
	int storage_id_len;

	*new_trunk_server_id = '\0';
	memset(out_buff, 0, sizeof(out_buff));
	memset(in_buff, 0, sizeof(in_buff));
	pHeader = (TrackerHeader *)out_buff;
	snprintf(out_buff + sizeof(TrackerHeader), sizeof(out_buff) - \
			sizeof(TrackerHeader),  "%s", group_name);
	if (storage_id == NULL)
	{
		storage_id_len = 0;
	}
	else
	{
		storage_id_len = snprintf(out_buff + sizeof(TrackerHeader) + \
				FDFS_GROUP_NAME_MAX_LEN, \
				sizeof(out_buff) - sizeof(TrackerHeader) - \
				FDFS_GROUP_NAME_MAX_LEN,  "%s", storage_id);
	}
	
	long2buff(FDFS_GROUP_NAME_MAX_LEN + storage_id_len, pHeader->pkg_len);
	pHeader->cmd = TRACKER_PROTO_CMD_SERVER_SET_TRUNK_SERVER;

	result = 0;
	pEnd = pTrackerGroup->servers + pTrackerGroup->server_count;
	for (pServer=pTrackerGroup->servers; pServer<pEnd; pServer++)
	{
		memcpy(&tracker_server, pServer, sizeof(ConnectionInfo));
		tracker_server.sock = -1;
		if ((conn=tracker_connect_server(&tracker_server, &result)) == NULL)
		{
			continue;
		}

		if ((result=tcpsenddata_nb(conn->sock, out_buff, \
			sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + 
			storage_id_len, g_fdfs_network_timeout)) != 0)
		{
			logError("file: "__FILE__", line: %d, " \
				"send data to tracker server %s:%d fail, " \
				"errno: %d, error info: %s", __LINE__, \
				tracker_server.ip_addr, tracker_server.port, \
				result, STRERROR(result));

			tracker_disconnect_server_ex(conn, true);
			continue;
		}

		pInBuff = in_buff;
		result = fdfs_recv_response(conn, &pInBuff, \
				sizeof(in_buff) - 1, &in_bytes);

		tracker_disconnect_server_ex(conn, result != 0);
		if (result == 0)
		{
			strcpy(new_trunk_server_id, in_buff);
			return 0;
		}

		if (result == EOPNOTSUPP)
		{
			continue;
		}
		if (result == EALREADY)
		{
			if (storage_id_len > 0)
			{
				strcpy(new_trunk_server_id, storage_id);
			}
			return result;
		}
		else
		{
			return result;
		}
	}

	return result;
}
Esempio n. 10
0
static int do_notify_leader_changed(ConnectionInfo *pTrackerServer, \
		ConnectionInfo *pLeader, const char cmd, bool *bConnectFail)
{
	char out_buff[sizeof(TrackerHeader) + FDFS_PROTO_IP_PORT_SIZE];
	char in_buff[1];
	ConnectionInfo *conn;
	TrackerHeader *pHeader;
	char *pInBuff;
	int64_t in_bytes;
	int result;

	pTrackerServer->sock = -1;
	if ((conn=tracker_connect_server(pTrackerServer, &result)) == NULL)
	{
		*bConnectFail = true;
		return result;
	}
	*bConnectFail = false;

	do
	{
	memset(out_buff, 0, sizeof(out_buff));
	pHeader = (TrackerHeader *)out_buff;
	pHeader->cmd = cmd;
	sprintf(out_buff + sizeof(TrackerHeader), "%s:%d", \
			pLeader->ip_addr, pLeader->port);
	long2buff(FDFS_PROTO_IP_PORT_SIZE, pHeader->pkg_len);
	if ((result=tcpsenddata_nb(conn->sock, out_buff, \
			sizeof(out_buff), g_fdfs_network_timeout)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"send data to tracker server %s:%d fail, " \
			"errno: %d, error info: %s", __LINE__, \
			pTrackerServer->ip_addr, \
			pTrackerServer->port, \
			result, STRERROR(result));

		result = (result == ENOENT ? EACCES : result);
		break;
	}

	pInBuff = in_buff;
	result = fdfs_recv_response(conn, &pInBuff, \
				0, &in_bytes);
	if (result != 0)
	{
		break;
	}

	if (in_bytes != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"tracker server %s:%d response data " \
			"length: %"PRId64" is invalid, " \
			"expect length: %d.", __LINE__, \
			pTrackerServer->ip_addr, pTrackerServer->port, \
			in_bytes, 0);
		result = EINVAL;
		break;
	}
	} while (0);

	if (pTrackerServer->port == g_server_port && \
		is_local_host_ip(pTrackerServer->ip_addr))
	{
		tracker_disconnect_server_ex(conn, true);
	}
	else
	{
		tracker_disconnect_server_ex(conn, result != 0);
	}

	return result;
}
Esempio n. 11
0
int tracker_query_storage_store_list_with_group( \
	ConnectionInfo *pTrackerServer, const char *group_name, \
	ConnectionInfo *storageServers, const int nMaxServerCount, \
	int *storage_count, int *store_path_index)
{
	ConnectionInfo *pStorageServer;
	ConnectionInfo *pServerEnd;
	TrackerHeader *pHeader;
	ConnectionInfo *conn;
	bool new_connection;
	char out_buff[sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN];
	char in_buff[sizeof(TrackerHeader) + FDFS_MAX_SERVERS_EACH_GROUP * \
			TRACKER_QUERY_STORAGE_STORE_BODY_LEN];
	char returned_group_name[FDFS_GROUP_NAME_MAX_LEN + 1];
	char *pInBuff;
	char *p;
	int64_t in_bytes;
	int out_len;
	int ipPortsLen;
	int result;

	*storage_count = 0;
	CHECK_CONNECTION(pTrackerServer, conn, result, new_connection);

	pHeader = (TrackerHeader *)out_buff;
	memset(out_buff, 0, sizeof(out_buff));

	if (group_name == NULL || *group_name == '\0')
	{
	pHeader->cmd = TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ALL;
	out_len = 0;
	}
	else
	{
	pHeader->cmd = TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ALL;
	snprintf(out_buff + sizeof(TrackerHeader), sizeof(out_buff) - \
			sizeof(TrackerHeader),  "%s", group_name);
	out_len = FDFS_GROUP_NAME_MAX_LEN;
	}

	long2buff(out_len, pHeader->pkg_len);
	if ((result=tcpsenddata_nb(conn->sock, out_buff, \
		sizeof(TrackerHeader) + out_len, g_fdfs_network_timeout)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"send data to tracker server %s:%d fail, " \
			"errno: %d, error info: %s", __LINE__, \
			pTrackerServer->ip_addr, \
			pTrackerServer->port, \
			result, STRERROR(result));
	}
	else
	{
		pInBuff = in_buff;
		result = fdfs_recv_response(conn, \
				&pInBuff, sizeof(in_buff), &in_bytes);
	}

	if (new_connection)
	{
		tracker_disconnect_server_ex(conn, result != 0);
	}

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

	if (in_bytes < TRACKER_QUERY_STORAGE_STORE_BODY_LEN)
	{
		logError("file: "__FILE__", line: %d, " \
			"tracker server %s:%d response data " \
			"length: %"PRId64" is invalid, " \
			"expect length >= %d", __LINE__, \
			pTrackerServer->ip_addr, pTrackerServer->port, \
			in_bytes, TRACKER_QUERY_STORAGE_STORE_BODY_LEN);
		return EINVAL;
	}

#define RECORD_LENGTH  (IP_ADDRESS_SIZE - 1 + FDFS_PROTO_PKG_LEN_SIZE)

	ipPortsLen = in_bytes - (FDFS_GROUP_NAME_MAX_LEN + 1);
	if (ipPortsLen % RECORD_LENGTH != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"tracker server %s:%d response data " \
			"length: %"PRId64" is invalid", \
			__LINE__, pTrackerServer->ip_addr, \
			pTrackerServer->port, in_bytes);
		return EINVAL;
	}

	*storage_count = ipPortsLen / RECORD_LENGTH;
	if (nMaxServerCount < *storage_count)
	{
		logError("file: "__FILE__", line: %d, " \
			"tracker server %s:%d response storage server " \
			 "count: %d, exceeds max server count: %d!", \
			__LINE__, pTrackerServer->ip_addr, \
			pTrackerServer->port, *storage_count, nMaxServerCount);
		return ENOSPC;
	}

	memset(storageServers, 0, sizeof(ConnectionInfo) * nMaxServerCount);

	memcpy(returned_group_name, in_buff, FDFS_GROUP_NAME_MAX_LEN);
	p = in_buff + FDFS_GROUP_NAME_MAX_LEN;
	*(returned_group_name + FDFS_GROUP_NAME_MAX_LEN) = '\0';

	pServerEnd = storageServers + (*storage_count);
	for (pStorageServer=storageServers; pStorageServer<pServerEnd; \
		pStorageServer++)
	{
		pStorageServer->sock = -1;
		memcpy(pStorageServer->ip_addr, p, IP_ADDRESS_SIZE - 1);
		p += IP_ADDRESS_SIZE - 1;

		pStorageServer->port = (int)buff2long(p);
		p += FDFS_PROTO_PKG_LEN_SIZE;
	}

	*store_path_index = *p;

	return 0;
}
Esempio n. 12
0
int tracker_query_storage_list(ConnectionInfo *pTrackerServer, \
		ConnectionInfo *pStorageServer, const int nMaxServerCount, \
		int *server_count, char *group_name, const char *filename)
{
	TrackerHeader *pHeader;
	ConnectionInfo *pServer;
	ConnectionInfo *pServerEnd;
	ConnectionInfo *conn;
	bool new_connection;
	char out_buff[sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + 128];
	char in_buff[sizeof(TrackerHeader) + \
		TRACKER_QUERY_STORAGE_FETCH_BODY_LEN + \
		FDFS_MAX_SERVERS_EACH_GROUP * IP_ADDRESS_SIZE];
	char *pInBuff;
	int64_t in_bytes;
	int result;
	int filename_len;

	CHECK_CONNECTION(pTrackerServer, conn, result, new_connection);

	memset(out_buff, 0, sizeof(out_buff));
	pHeader = (TrackerHeader *)out_buff;
	snprintf(out_buff + sizeof(TrackerHeader), sizeof(out_buff) - \
			sizeof(TrackerHeader),  "%s", group_name);
	filename_len = snprintf(out_buff + sizeof(TrackerHeader) + \
			FDFS_GROUP_NAME_MAX_LEN, \
			sizeof(out_buff) - sizeof(TrackerHeader) - \
			FDFS_GROUP_NAME_MAX_LEN,  "%s", filename);
	
	long2buff(FDFS_GROUP_NAME_MAX_LEN + filename_len, pHeader->pkg_len);
	pHeader->cmd = TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ALL;
	if ((result=tcpsenddata_nb(conn->sock, out_buff, \
		sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + 
		filename_len, g_fdfs_network_timeout)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"send data to tracker server %s:%d fail, " \
			"errno: %d, error info: %s", __LINE__, \
			pTrackerServer->ip_addr, \
			pTrackerServer->port, \
			result, STRERROR(result));
	}
	else
	{
		pInBuff = in_buff;
		result = fdfs_recv_response(conn, \
				&pInBuff, sizeof(in_buff), &in_bytes);
	}

	if (new_connection)
	{
		tracker_disconnect_server_ex(conn, result != 0);
	}

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

	if ((in_bytes - TRACKER_QUERY_STORAGE_FETCH_BODY_LEN) % \
		(IP_ADDRESS_SIZE - 1) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"tracker server %s:%d response data " \
			"length: %"PRId64" is invalid", \
			__LINE__, pTrackerServer->ip_addr, \
			pTrackerServer->port, in_bytes);
		return EINVAL;
	}

	*server_count = 1 + (in_bytes - TRACKER_QUERY_STORAGE_FETCH_BODY_LEN) /
			(IP_ADDRESS_SIZE - 1);
	if (nMaxServerCount < *server_count)
	{
		logError("file: "__FILE__", line: %d, " \
			"tracker server %s:%d response storage server " \
			 "count: %d, exceeds max server count: %d!", __LINE__, \
			pTrackerServer->ip_addr, pTrackerServer->port, \
			*server_count, nMaxServerCount);
		return ENOSPC;
	}

	memset(pStorageServer, 0, nMaxServerCount * sizeof(ConnectionInfo));
	pStorageServer->sock = -1;

	memcpy(group_name, pInBuff, FDFS_GROUP_NAME_MAX_LEN);
	*(group_name + FDFS_GROUP_NAME_MAX_LEN) = '\0';
	pInBuff += FDFS_GROUP_NAME_MAX_LEN;
	memcpy(pStorageServer->ip_addr, pInBuff, IP_ADDRESS_SIZE - 1);
	pInBuff += IP_ADDRESS_SIZE - 1;
	pStorageServer->port = (int)buff2long(pInBuff);
	pInBuff += FDFS_PROTO_PKG_LEN_SIZE;

	pServerEnd = pStorageServer + (*server_count);
	for (pServer=pStorageServer+1; pServer<pServerEnd; pServer++)
	{
		pServer->sock = -1;
		pServer->port = pStorageServer->port;
		memcpy(pServer->ip_addr, pInBuff, IP_ADDRESS_SIZE - 1);
		pInBuff += IP_ADDRESS_SIZE - 1;
	}

	return 0;
}
Esempio n. 13
0
int tracker_list_one_group(ConnectionInfo *pTrackerServer, \
		const char *group_name, FDFSGroupStat *pDest)
{
	TrackerHeader *pHeader;
	ConnectionInfo *conn;
	bool new_connection;
	char out_buff[sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN];
	TrackerGroupStat src;
	char *pInBuff;
	int result;
	int64_t in_bytes;

	CHECK_CONNECTION(pTrackerServer, conn, result, new_connection);

	memset(out_buff, 0, sizeof(out_buff));
	pHeader = (TrackerHeader *)out_buff;
	snprintf(out_buff + sizeof(TrackerHeader), sizeof(out_buff) - \
			sizeof(TrackerHeader),  "%s", group_name);
	pHeader->cmd = TRACKER_PROTO_CMD_SERVER_LIST_ONE_GROUP;
	long2buff(FDFS_GROUP_NAME_MAX_LEN, pHeader->pkg_len);
	if ((result=tcpsenddata_nb(conn->sock, out_buff, \
			sizeof(out_buff), g_fdfs_network_timeout)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"send data to tracker server %s:%d fail, " \
			"errno: %d, error info: %s", __LINE__, \
			pTrackerServer->ip_addr, \
			pTrackerServer->port, \
			result, STRERROR(result));
	}
	else
	{
		pInBuff = (char *)&src;
		result = fdfs_recv_response(conn, \
			&pInBuff, sizeof(TrackerGroupStat), &in_bytes);
	}

	if (new_connection)
	{
		tracker_disconnect_server_ex(conn, result != 0);
	}

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

	if (in_bytes != sizeof(TrackerGroupStat))
	{
		logError("file: "__FILE__", line: %d, " \
			"tracker server %s:%d response data " \
			"length: %"PRId64" is invalid", \
			__LINE__, pTrackerServer->ip_addr, \
			pTrackerServer->port, in_bytes);
		return EINVAL;
	}

	memset(pDest, 0, sizeof(FDFSGroupStat));
	memcpy(pDest->group_name, src.group_name, FDFS_GROUP_NAME_MAX_LEN);
	pDest->total_mb = buff2long(src.sz_total_mb);
	pDest->free_mb = buff2long(src.sz_free_mb);
	pDest->trunk_free_mb = buff2long(src.sz_trunk_free_mb);
	pDest->count= buff2long(src.sz_count);
	pDest->storage_port= buff2long(src.sz_storage_port);
	pDest->storage_http_port= buff2long(src.sz_storage_http_port);
	pDest->active_count = buff2long(src.sz_active_count);
	pDest->current_write_server = buff2long(src.sz_current_write_server);
	pDest->store_path_count = buff2long(src.sz_store_path_count);
	pDest->subdir_count_per_path = buff2long(src.sz_subdir_count_per_path);
	pDest->current_trunk_file_id = buff2long(src.sz_current_trunk_file_id);

	return 0;
}
Esempio n. 14
0
/**
* request body format:
*       namespace_len:  4 bytes big endian integer
*       namespace: can be emtpy
*       obj_id_len:  4 bytes big endian integer
*       object_id: the object id (can be empty)
*       key_len:  4 bytes big endian integer
*       key:      key name
* response body format:
*      none
*/
int fdht_client_delete(FDHTServerInfo *pServer, const char keep_alive, \
	const time_t timestamp, const int prot_cmd, \
	const int key_hash_code, FDHTKeyInfo *pKeyInfo)
{
	int result;
	FDHTProtoHeader *pHeader;
	char buff[sizeof(FDHTProtoHeader) + FDHT_MAX_FULL_KEY_LEN + 16];
	int in_bytes;
	char *p;

	memset(buff, 0, sizeof(buff));
	pHeader = (FDHTProtoHeader *)buff;
	pHeader->cmd = prot_cmd;
	pHeader->keep_alive = keep_alive;
	int2buff(timestamp, pHeader->timestamp);
	int2buff(key_hash_code, pHeader->key_hash_code);
	int2buff(12 + pKeyInfo->namespace_len + pKeyInfo->obj_id_len + \
		pKeyInfo->key_len, pHeader->pkg_len);

	p = buff + sizeof(FDHTProtoHeader);
	PACK_BODY_UNTIL_KEY(pKeyInfo, p)

	if ((result=tcpsenddata_nb(pServer->sock, buff, p - buff, \
		g_fdht_network_timeout)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"send data to server %s:%d fail, " \
			"errno: %d, error info: %s", __LINE__, \
			pServer->ip_addr, pServer->port, \
			result, STRERROR(result));
		return result;
	}

	if ((result=fdht_recv_header(pServer, &in_bytes)) != 0)
	{
		if (result == ENOENT)
		{
			logWarning("file: "__FILE__", line: %d, " \
				"recv data from server %s:%d fail, " \
				"errno: %d, error info: %s", __LINE__, \
				pServer->ip_addr, pServer->port, \
				result, STRERROR(result));
		}
		else
		{
			logError("file: "__FILE__", line: %d, " \
				"recv data from server %s:%d fail, " \
				"errno: %d, error info: %s", __LINE__, \
				pServer->ip_addr, pServer->port, \
				result, STRERROR(result));
		}

		return result;
	}

	if (in_bytes != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"server %s:%d reponse bytes: %d != 0", \
			__LINE__, pServer->ip_addr, \
			pServer->port, in_bytes);
		return EINVAL;
	}

	return 0;
}
Esempio n. 15
0
int tracker_get_storage_id(ConnectionInfo *pTrackerServer, \
		const char *group_name, const char *ip_addr, \
		char *storage_id)
{
	TrackerHeader *pHeader;
	ConnectionInfo *conn;
	bool new_connection;
	char out_buff[sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + \
			IP_ADDRESS_SIZE];
	char *p;
	int result;
	int ip_len;
	int64_t in_bytes;

	if (storage_id == NULL)
	{
		return EINVAL;
	}

	CHECK_CONNECTION(pTrackerServer, conn, result, new_connection);
	
	if (ip_addr == NULL)
	{
		ip_len = 0;
	}
	else
	{
		ip_len = strlen(ip_addr);
	}

	memset(out_buff, 0, sizeof(out_buff));
	pHeader = (TrackerHeader *)out_buff;
	p = out_buff + sizeof(TrackerHeader);
	snprintf(p, sizeof(out_buff) - sizeof(TrackerHeader), "%s", group_name);
	p += FDFS_GROUP_NAME_MAX_LEN;
	if (ip_len > 0)
	{
		memcpy(p, ip_addr, ip_len);
		p += ip_len;
	}
	pHeader->cmd = TRACKER_PROTO_CMD_STORAGE_GET_SERVER_ID;
	long2buff(FDFS_GROUP_NAME_MAX_LEN + ip_len, pHeader->pkg_len);
	if ((result=tcpsenddata_nb(conn->sock, out_buff, \
			p - out_buff, g_fdfs_network_timeout)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"send data to tracker server %s:%d fail, " \
			"errno: %d, error info: %s", __LINE__, \
			pTrackerServer->ip_addr, \
			pTrackerServer->port, \
			result, STRERROR(result));
	}
	else
	{
		result = fdfs_recv_response(conn, \
			&storage_id, FDFS_STORAGE_ID_MAX_SIZE, &in_bytes);
	}

	if (new_connection)
	{
		tracker_disconnect_server_ex(conn, result != 0);
	}

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

	if (in_bytes == 0 || in_bytes >= FDFS_STORAGE_ID_MAX_SIZE)
	{
		logError("file: "__FILE__", line: %d, " \
			"tracker server %s:%d response data " \
			"length: %"PRId64" is invalid", \
			__LINE__, pTrackerServer->ip_addr, \
			pTrackerServer->port, in_bytes);
		return EINVAL;
	}

	*(storage_id + in_bytes) = '\0';
	return 0;
}
Esempio n. 16
0
int tracker_list_servers(ConnectionInfo *pTrackerServer, \
		const char *szGroupName, const char *szStorageId, \
		FDFSStorageInfo *storage_infos, const int max_storages, \
		int *storage_count)
{
	char out_buff[sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + \
			IP_ADDRESS_SIZE];
	bool new_connection;
	TrackerHeader *pHeader;
	ConnectionInfo *conn;
	int result;
	int name_len;
	int id_len;
	TrackerStorageStat stats[FDFS_MAX_GROUPS];
	char *pInBuff;
	TrackerStorageStat *pSrc;
	TrackerStorageStat *pEnd;
	FDFSStorageStat *pStorageStat;
	FDFSStorageInfo *pDest;
	FDFSStorageStatBuff *pStatBuff;
	int64_t in_bytes;

	CHECK_CONNECTION(pTrackerServer, conn, result, new_connection);

	memset(out_buff, 0, sizeof(out_buff));
	pHeader = (TrackerHeader *)out_buff;
	name_len = strlen(szGroupName);
	if (name_len > FDFS_GROUP_NAME_MAX_LEN)
	{
		name_len = FDFS_GROUP_NAME_MAX_LEN;
	}
	memcpy(out_buff + sizeof(TrackerHeader), szGroupName, name_len);

	if (szStorageId == NULL)
	{
		id_len = 0;
	}
	else
	{
		id_len = strlen(szStorageId);
		if (id_len >= FDFS_STORAGE_ID_MAX_SIZE)
		{
			id_len = FDFS_STORAGE_ID_MAX_SIZE - 1;
		}

		memcpy(out_buff+sizeof(TrackerHeader)+FDFS_GROUP_NAME_MAX_LEN,\
			szStorageId, id_len);
	}

	long2buff(FDFS_GROUP_NAME_MAX_LEN + id_len, pHeader->pkg_len);
	pHeader->cmd = TRACKER_PROTO_CMD_SERVER_LIST_STORAGE;
	if ((result=tcpsenddata_nb(conn->sock, out_buff, \
		sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + id_len, \
		g_fdfs_network_timeout)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
		        "send data to tracker server %s:%d fail, " \
			"errno: %d, error info: %s", __LINE__, \
			pTrackerServer->ip_addr, \
			pTrackerServer->port, \
			result, STRERROR(result));
	}
	else
	{
		pInBuff = (char *)stats;
		result = fdfs_recv_response(conn, &pInBuff, \
					sizeof(stats), &in_bytes);
	}

	if (new_connection)
	{
		tracker_disconnect_server_ex(conn, result != 0);
	}

	if (result != 0)
	{
		*storage_count = 0;
		return result;
	}

	if (in_bytes % sizeof(TrackerStorageStat) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"tracker server %s:%d response data " \
			"length: %"PRId64" is invalid", \
			__LINE__, pTrackerServer->ip_addr, \
			pTrackerServer->port, in_bytes);
		*storage_count = 0;
		return EINVAL;
	}

	*storage_count = in_bytes / sizeof(TrackerStorageStat);
	if (*storage_count > max_storages)
	{
		logError("file: "__FILE__", line: %d, " \
		 	"tracker server %s:%d insufficent space, " \
			"max storage count: %d, expect count: %d", \
			__LINE__, pTrackerServer->ip_addr, \
			pTrackerServer->port, max_storages, *storage_count);
		*storage_count = 0;
		return ENOSPC;
	}

	memset(storage_infos, 0, sizeof(FDFSStorageInfo) * max_storages);
	pDest = storage_infos;
	pEnd = stats + (*storage_count);
	for (pSrc=stats; pSrc<pEnd; pSrc++)
	{
		pStatBuff = &(pSrc->stat_buff);
		pStorageStat = &(pDest->stat);

		pDest->status = pSrc->status;
		memcpy(pDest->id, pSrc->id, FDFS_STORAGE_ID_MAX_SIZE - 1);
		memcpy(pDest->ip_addr, pSrc->ip_addr, IP_ADDRESS_SIZE - 1);
		memcpy(pDest->src_id, pSrc->src_id, \
			FDFS_STORAGE_ID_MAX_SIZE - 1);
		strcpy(pDest->domain_name, pSrc->domain_name);
		strcpy(pDest->version, pSrc->version);
		pDest->join_time = buff2long(pSrc->sz_join_time);
		pDest->up_time = buff2long(pSrc->sz_up_time);
		pDest->total_mb = buff2long(pSrc->sz_total_mb);
		pDest->free_mb = buff2long(pSrc->sz_free_mb);
		pDest->upload_priority = buff2long(pSrc->sz_upload_priority);
		pDest->store_path_count = buff2long(pSrc->sz_store_path_count);
		pDest->subdir_count_per_path = buff2long( \
					pSrc->sz_subdir_count_per_path);
		pDest->storage_port = buff2long(pSrc->sz_storage_port);
		pDest->storage_http_port = buff2long(pSrc->sz_storage_http_port);
		pDest->current_write_path = buff2long( \
					pSrc->sz_current_write_path);

		pStorageStat->total_upload_count = buff2long( \
			pStatBuff->sz_total_upload_count);
		pStorageStat->success_upload_count = buff2long( \
			pStatBuff->sz_success_upload_count);
		pStorageStat->total_append_count = buff2long( \
			pStatBuff->sz_total_append_count);
		pStorageStat->success_append_count = buff2long( \
			pStatBuff->sz_success_append_count);
		pStorageStat->total_modify_count = buff2long( \
			pStatBuff->sz_total_modify_count);
		pStorageStat->success_modify_count = buff2long( \
			pStatBuff->sz_success_modify_count);
		pStorageStat->total_truncate_count = buff2long( \
			pStatBuff->sz_total_truncate_count);
		pStorageStat->success_truncate_count = buff2long( \
			pStatBuff->sz_success_truncate_count);
		pStorageStat->total_set_meta_count = buff2long( \
			pStatBuff->sz_total_set_meta_count);
		pStorageStat->success_set_meta_count = buff2long( \
			pStatBuff->sz_success_set_meta_count);
		pStorageStat->total_delete_count = buff2long( \
			pStatBuff->sz_total_delete_count);
		pStorageStat->success_delete_count = buff2long( \
			pStatBuff->sz_success_delete_count);
		pStorageStat->total_download_count = buff2long( \
			pStatBuff->sz_total_download_count);
		pStorageStat->success_download_count = buff2long( \
			pStatBuff->sz_success_download_count);
		pStorageStat->total_get_meta_count = buff2long( \
			pStatBuff->sz_total_get_meta_count);
		pStorageStat->success_get_meta_count = buff2long( \
			pStatBuff->sz_success_get_meta_count);
		pStorageStat->last_source_update = buff2long( \
			pStatBuff->sz_last_source_update);
		pStorageStat->last_sync_update = buff2long( \
			pStatBuff->sz_last_sync_update);
		pStorageStat->last_synced_timestamp = buff2long( \
			pStatBuff->sz_last_synced_timestamp);
		pStorageStat->total_create_link_count = buff2long( \
			pStatBuff->sz_total_create_link_count);
		pStorageStat->success_create_link_count = buff2long( \
			pStatBuff->sz_success_create_link_count);
		pStorageStat->total_delete_link_count = buff2long( \
			pStatBuff->sz_total_delete_link_count);
		pStorageStat->success_delete_link_count = buff2long( \
			pStatBuff->sz_success_delete_link_count);
		pStorageStat->total_upload_bytes = buff2long( \
			pStatBuff->sz_total_upload_bytes);
		pStorageStat->success_upload_bytes = buff2long( \
			pStatBuff->sz_success_upload_bytes);
		pStorageStat->total_append_bytes = buff2long( \
			pStatBuff->sz_total_append_bytes);
		pStorageStat->success_append_bytes = buff2long( \
			pStatBuff->sz_success_append_bytes);
		pStorageStat->total_modify_bytes = buff2long( \
			pStatBuff->sz_total_modify_bytes);
		pStorageStat->success_modify_bytes = buff2long( \
			pStatBuff->sz_success_modify_bytes);
		pStorageStat->total_download_bytes = buff2long( \
			pStatBuff->sz_total_download_bytes);
		pStorageStat->success_download_bytes = buff2long( \
			pStatBuff->sz_success_download_bytes);
		pStorageStat->total_sync_in_bytes = buff2long( \
			pStatBuff->sz_total_sync_in_bytes);
		pStorageStat->success_sync_in_bytes = buff2long( \
			pStatBuff->sz_success_sync_in_bytes);
		pStorageStat->total_sync_out_bytes = buff2long( \
			pStatBuff->sz_total_sync_out_bytes);
		pStorageStat->success_sync_out_bytes = buff2long( \
			pStatBuff->sz_success_sync_out_bytes);
		pStorageStat->total_file_open_count = buff2long( \
			pStatBuff->sz_total_file_open_count);
		pStorageStat->success_file_open_count = buff2long( \
			pStatBuff->sz_success_file_open_count);
		pStorageStat->total_file_read_count = buff2long( \
			pStatBuff->sz_total_file_read_count);
		pStorageStat->success_file_read_count = buff2long( \
			pStatBuff->sz_success_file_read_count);
		pStorageStat->total_file_write_count = buff2long( \
			pStatBuff->sz_total_file_write_count);
		pStorageStat->success_file_write_count = buff2long( \
			pStatBuff->sz_success_file_write_count);
		pStorageStat->last_heart_beat_time = buff2long( \
			pStatBuff->sz_last_heart_beat_time);
		pDest->if_trunk_server = pSrc->if_trunk_server;
		pDest++;
	}

	return 0;
}
Esempio n. 17
0
//recv不做重连
UINT32 TrackerMgr::QueryStorageStore(ConnectionInfo *pTrackerServer,
		ServerAddress *pStorageServer,
		TCHAR *szGroupName, UINT32 *nStorePathIndex)
{
	UINT32 nRet;
	UINT64 nInBytes;

	TrackerHeader header;
	memset(&header, 0, sizeof(header));
	header.byCmd = TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE;

	EnterCriticalSection(&pTrackerServer->csSend);
	if ((nRet = tcpsenddata_nb(pTrackerServer->sock, &header, sizeof(header), DEFAULT_NETWORK_TIMEOUT)) != 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageStore tcpsenddata_nb Header Failed, Error Code:%d"), nRet);
		return enumNetworkError_FDFS;
	}

	TrackerHeader resp;
	int nCount = 0;
	EnterCriticalSection(&pTrackerServer->csRecv);
	if ((nRet = tcprecvdata_nb(pTrackerServer->sock, &resp, sizeof(resp), DEFAULT_NETWORK_TIMEOUT, &nCount)) != 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageStore tcprecvdata_nb Header Failed, Error Code:%d"), nRet);
		return enumNetworkError_FDFS;
	}

	if (resp.byStatus != 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageStore Status Error, %d"), resp.byStatus);
		if(resp.byStatus == 28)
			return enumNoEnoughSpace_FDFS;
		return enumFailure_FDFS;
	}

	nInBytes = buff2long64((const char*)(resp.byPkgLen));
	if (nInBytes < 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageStore Packet Len Error"));
		return enumFailure_FDFS;
	}

	//包头接收完毕,接收Body
	BYTE *pbyBuff = NULL;
	pbyBuff = new BYTE[nInBytes];
	if(pbyBuff == NULL)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageStore New Error"));
		delete[] pbyBuff;
		return enumFailure_FDFS;
	}
	if ((nRet = tcprecvdata_nb(pTrackerServer->sock, pbyBuff, nInBytes, DEFAULT_NETWORK_TIMEOUT, &nCount)) != 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageStore tcprecvdata_nb Body Failed, Error Code:%d"), nRet);
		delete[] pbyBuff;
		return enumNetworkError_FDFS;
	}
	LeaveCriticalSection(&pTrackerServer->csRecv);
	LeaveCriticalSection(&pTrackerServer->csSend);
	
	//数据接收完毕,开始解析
	memcpy(szGroupName, pbyBuff, FDFS_GROUP_NAME_MAX_LEN);
	*(szGroupName + FDFS_GROUP_NAME_MAX_LEN) = '\0';
	memcpy(pStorageServer->szIP, pbyBuff + FDFS_GROUP_NAME_MAX_LEN, IP_ADDRESS_SIZE - 1);
	pStorageServer->nPort = buff2long64((const char*)(pbyBuff + FDFS_GROUP_NAME_MAX_LEN + IP_ADDRESS_SIZE - 1));
	*nStorePathIndex = *(pbyBuff + FDFS_GROUP_NAME_MAX_LEN + IP_ADDRESS_SIZE - 1 + FDFS_PROTO_PKG_LEN_SIZE);

	delete[] pbyBuff;

	return enumSuccess_FDFS;
}
Esempio n. 18
0
int tracker_list_groups(ConnectionInfo *pTrackerServer, \
		FDFSGroupStat *group_stats, const int max_groups, \
		int *group_count)
{
	bool new_connection;
	TrackerHeader header;
	TrackerGroupStat stats[FDFS_MAX_GROUPS];
	char *pInBuff;
	ConnectionInfo *conn;
	TrackerGroupStat *pSrc;
	TrackerGroupStat *pEnd;
	FDFSGroupStat *pDest;
	int result;
	int64_t in_bytes;

	CHECK_CONNECTION(pTrackerServer, conn, result, new_connection);

	memset(&header, 0, sizeof(header));
	header.cmd = TRACKER_PROTO_CMD_SERVER_LIST_ALL_GROUPS;
	header.status = 0;
	if ((result=tcpsenddata_nb(conn->sock, &header, \
			sizeof(header), g_fdfs_network_timeout)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"send data to tracker server %s:%d fail, " \
			"errno: %d, error info: %s", __LINE__, \
			pTrackerServer->ip_addr, \
			pTrackerServer->port, \
			result, STRERROR(result));
	}
	else
	{
		pInBuff = (char *)stats;
		result = fdfs_recv_response(conn, \
			&pInBuff, sizeof(stats), &in_bytes);
	}

	if (new_connection)
	{
		tracker_disconnect_server_ex(conn, result != 0);
	}

	if (result != 0)
	{
		*group_count = 0;
		return result;
	}

	if (in_bytes % sizeof(TrackerGroupStat) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"tracker server %s:%d response data " \
			"length: %"PRId64" is invalid", \
			__LINE__, pTrackerServer->ip_addr, \
			pTrackerServer->port, in_bytes);
		*group_count = 0;
		return EINVAL;
	}

	*group_count = in_bytes / sizeof(TrackerGroupStat);
	if (*group_count > max_groups)
	{
		logError("file: "__FILE__", line: %d, " \
			"tracker server %s:%d insufficent space, " \
			"max group count: %d, expect count: %d", \
			__LINE__, pTrackerServer->ip_addr, \
			pTrackerServer->port, max_groups, *group_count);
		*group_count = 0;
		return ENOSPC;
	}

	memset(group_stats, 0, sizeof(FDFSGroupStat) * max_groups);
	pDest = group_stats;
	pEnd = stats + (*group_count);
	for (pSrc=stats; pSrc<pEnd; pSrc++)
	{
		memcpy(pDest->group_name, pSrc->group_name, \
				FDFS_GROUP_NAME_MAX_LEN);
		pDest->total_mb = buff2long(pSrc->sz_total_mb);
		pDest->free_mb = buff2long(pSrc->sz_free_mb);
		pDest->trunk_free_mb = buff2long(pSrc->sz_trunk_free_mb);
		pDest->count= buff2long(pSrc->sz_count);
		pDest->storage_port= buff2long(pSrc->sz_storage_port);
		pDest->storage_http_port= buff2long(pSrc->sz_storage_http_port);
		pDest->active_count = buff2long(pSrc->sz_active_count);
		pDest->current_write_server = buff2long( \
				pSrc->sz_current_write_server);
		pDest->store_path_count = buff2long( \
				pSrc->sz_store_path_count);
		pDest->subdir_count_per_path = buff2long( \
				pSrc->sz_subdir_count_per_path);
		pDest->current_trunk_file_id = buff2long( \
				pSrc->sz_current_trunk_file_id);

		pDest++;
	}

	return 0;
}
Esempio n. 19
0
UINT32 TrackerMgr::QueryUpdateStorageStore(ConnectionInfo *pTrackerServer,
		const BYTE *pbyMasterGroupName, const BYTE *pbyMasterFileName,
		ServerAddress *pStorageServer,
		UINT32 *nStorePathIndex)
{
	UINT32 nRet;
	UINT64 nInBytes;

	BYTE byOutBuff[sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN
	+ FDFS_REMOTE_FILE_NAME_MAX_LEN];
	memset(byOutBuff, 0, sizeof(byOutBuff));

	TrackerHeader *pHeader = (TrackerHeader*)byOutBuff;
	memcpy(byOutBuff + sizeof(TrackerHeader), pbyMasterGroupName, FDFS_GROUP_NAME_MAX_LEN);

	UINT32 nMasterFileNameLen = strlen((const char*)pbyMasterFileName);
	memcpy(byOutBuff + sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN, pbyMasterFileName, nMasterFileNameLen);
	long642buff(FDFS_GROUP_NAME_MAX_LEN + nMasterFileNameLen, (char*)pHeader->byPkgLen);
	pHeader->byCmd = TRACKER_PROTO_CMD_SERVICE_QUERY_UPDATE;

	EnterCriticalSection(&pTrackerServer->csSend);
	if ((nRet = tcpsenddata_nb(pTrackerServer->sock, &byOutBuff, sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + nMasterFileNameLen, DEFAULT_NETWORK_TIMEOUT)) != 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryUpdateStorageStore tcpsenddata_nb Header Failed, Error Code:%d"), nRet);
		return enumNetworkError_FDFS;
	}

	TrackerHeader resp;
	int nCount = 0;
	EnterCriticalSection(&pTrackerServer->csRecv);
	if ((nRet = tcprecvdata_nb(pTrackerServer->sock, &resp, sizeof(resp), DEFAULT_NETWORK_TIMEOUT, &nCount)) != 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryUpdateStorageStore tcprecvdata_nb Header Failed, Error Code:%d"), nRet);
		return enumNetworkError_FDFS;
	}

	if (resp.byStatus != 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryUpdateStorageStore Status Error, %d"), resp.byStatus);
		return enumFailure_FDFS;
	}

	nInBytes = buff2long64((const char*)(resp.byPkgLen));
	if (nInBytes < 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryUpdateStorageStore Packet Len Error"));
		return enumFailure_FDFS;
	}

	//包头接收完毕,接收Body
	BYTE *pbyBuff = NULL;
	pbyBuff = new BYTE[nInBytes];
	if(pbyBuff == NULL)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryUpdateStorageStore New Error"));
		delete[] pbyBuff;
		return enumFailure_FDFS;
	}
	if ((nRet = tcprecvdata_nb(pTrackerServer->sock, pbyBuff, nInBytes, DEFAULT_NETWORK_TIMEOUT, &nCount)) != 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryUpdateStorageStore tcprecvdata_nb Body Failed, Error Code:%d"), nRet);
		delete[] pbyBuff;
		return enumNetworkError_FDFS;
	}
	LeaveCriticalSection(&pTrackerServer->csRecv);
	LeaveCriticalSection(&pTrackerServer->csSend);
	
	//数据接收完毕,开始解析
	memcpy(pStorageServer->szIP, pbyBuff + FDFS_GROUP_NAME_MAX_LEN, IP_ADDRESS_SIZE - 1);
	pStorageServer->nPort = buff2long64((const char*)(pbyBuff + FDFS_GROUP_NAME_MAX_LEN + IP_ADDRESS_SIZE - 1));

	delete[] pbyBuff;

	return enumSuccess_FDFS;
}
Esempio n. 20
0
int tracker_query_storage_store_with_group(ConnectionInfo *pTrackerServer, \
		const char *group_name, ConnectionInfo *pStorageServer, \
		int *store_path_index)
{
	TrackerHeader *pHeader;
	ConnectionInfo *conn;
	bool new_connection;
	char out_buff[sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN];
	char in_buff[sizeof(TrackerHeader) + \
		TRACKER_QUERY_STORAGE_STORE_BODY_LEN];
	char *pInBuff;
	int64_t in_bytes;
	int result;

	CHECK_CONNECTION(pTrackerServer, conn, result, new_connection);

	memset(pStorageServer, 0, sizeof(ConnectionInfo));
	pStorageServer->sock = -1;

	pHeader = (TrackerHeader *)out_buff;
	memset(out_buff, 0, sizeof(out_buff));
	snprintf(out_buff + sizeof(TrackerHeader), sizeof(out_buff) - \
			sizeof(TrackerHeader),  "%s", group_name);
	
	long2buff(FDFS_GROUP_NAME_MAX_LEN, pHeader->pkg_len);
	pHeader->cmd = TRACKER_PROTO_CMD_SERVICE_QUERY_STORE_WITH_GROUP_ONE;
	if ((result=tcpsenddata_nb(conn->sock, out_buff, \
			sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN, \
			g_fdfs_network_timeout)) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"send data to tracker server %s:%d fail, " \
			"errno: %d, error info: %s", __LINE__, \
			pTrackerServer->ip_addr, \
			pTrackerServer->port, \
			result, STRERROR(result));
	}
	else
	{
		pInBuff = in_buff;
		result = fdfs_recv_response(conn, \
				&pInBuff, sizeof(in_buff), &in_bytes);
	}

	if (new_connection)
	{
		tracker_disconnect_server_ex(conn, result != 0);
	}

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

	if (in_bytes != TRACKER_QUERY_STORAGE_STORE_BODY_LEN)
	{
		logError("file: "__FILE__", line: %d, " \
			"tracker server %s:%d response data " \
			"length: %"PRId64" is invalid, " \
			"expect length: %d", __LINE__, \
			pTrackerServer->ip_addr, pTrackerServer->port, \
			in_bytes, TRACKER_QUERY_STORAGE_STORE_BODY_LEN);
		return EINVAL;
	}

	memcpy(pStorageServer->ip_addr, in_buff + \
			FDFS_GROUP_NAME_MAX_LEN, IP_ADDRESS_SIZE-1);
	pStorageServer->port = (int)buff2long(in_buff + \
				FDFS_GROUP_NAME_MAX_LEN + IP_ADDRESS_SIZE - 1);
	*store_path_index = *(in_buff + FDFS_GROUP_NAME_MAX_LEN + \
			 IP_ADDRESS_SIZE - 1 + FDFS_PROTO_PKG_LEN_SIZE);

	return 0;
}
Esempio n. 21
0
UINT32 TrackerMgr::QueryStorageFetch(ConnectionInfo *pTrackerServer,
	const BYTE *szGroupName, const BYTE *szRemoteFileName,
	ServerAddress *pStorageServer)
{
	UINT32 nRemoteFileNameLen;
	UINT32 nGroupNameLen;
	UINT32 nOutBytes = 0, nInBytes = 0;
	UINT32 nRet;
	BYTE *p = NULL;
	BYTE byOutBuff[sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + FDFS_REMOTE_FILE_NAME_MAX_LEN];
	TrackerHeader *pHeader;

	memset(byOutBuff, 0, sizeof(byOutBuff));
	pHeader = (TrackerHeader *)byOutBuff;
	p = byOutBuff + sizeof(TrackerHeader);

	nGroupNameLen = strlen((const char*)szGroupName);
	memcpy_s(p, sizeof(byOutBuff) - (p - byOutBuff), szGroupName, nGroupNameLen);
	p += FDFS_GROUP_NAME_MAX_LEN;

	nRemoteFileNameLen = strlen((const char*)szRemoteFileName);
	memcpy_s(p, sizeof(byOutBuff) - (p - byOutBuff), szRemoteFileName, nRemoteFileNameLen);
	p += nRemoteFileNameLen;

	nOutBytes = p - byOutBuff;
	long642buff(nOutBytes - sizeof(TrackerHeader), (char*)pHeader->byPkgLen);
	pHeader->byCmd = TRACKER_PROTO_CMD_SERVICE_QUERY_FETCH_ONE;

	EnterCriticalSection(&pTrackerServer->csSend);
	if ((nRet = tcpsenddata_nb(pTrackerServer->sock, byOutBuff, nOutBytes, DEFAULT_NETWORK_TIMEOUT)) != 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageFetch tcpsenddata_nb Header Failed, Error Code:%d"), nRet);
		return enumNetworkError_FDFS;
	}

	//发包完毕,准备接收数据
	TrackerHeader resp;
	int nCount = 0;
	EnterCriticalSection(&pTrackerServer->csRecv);
	if ((nRet = tcprecvdata_nb(pTrackerServer->sock, &resp, sizeof(resp), DEFAULT_NETWORK_TIMEOUT, &nCount)) != 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageFetch tcprecvdata_nb Header Failed, Error Code:%d"), nRet);
		return enumNetworkError_FDFS;
	}

	if (resp.byStatus != 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageFetch Status Error, %d"), resp.byStatus);
		return enumFailure_FDFS;
	}

	nInBytes = buff2long64((const char*)(resp.byPkgLen));
	if (nInBytes < 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageFetch Packet Len Error"));
		return enumFailure_FDFS;
	}

	//包头接收完毕,接收Body
	BYTE *pbyBuff = NULL;
	pbyBuff = new BYTE[nInBytes];
	if(pbyBuff == NULL)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageFetch New Error"));
		delete[] pbyBuff;
		return enumFailure_FDFS;
	}
	if ((nRet = tcprecvdata_nb(pTrackerServer->sock, pbyBuff, nInBytes, DEFAULT_NETWORK_TIMEOUT, &nCount)) != 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::QueryStorageFetch tcprecvdata_nb Body Failed, Error Code:%d"), nRet);
		delete[] pbyBuff;
		return enumNetworkError_FDFS;
	}
	LeaveCriticalSection(&pTrackerServer->csRecv);
	LeaveCriticalSection(&pTrackerServer->csSend);

	//数据接收完毕,开始解析
	//前16字节为Group Name,用不着
	//memcpy(szGroupName, pbyBuff, FDFS_GROUP_NAME_MAX_LEN);
	//*(szGroupName + FDFS_GROUP_NAME_MAX_LEN) = '\0';
	memcpy(pStorageServer->szIP, pbyBuff + FDFS_GROUP_NAME_MAX_LEN, IP_ADDRESS_SIZE - 1);
	pStorageServer->nPort = buff2long64((const char*)(pbyBuff + FDFS_GROUP_NAME_MAX_LEN + IP_ADDRESS_SIZE - 1));
	delete[] pbyBuff;
	return enumSuccess_FDFS;
}
Esempio n. 22
0
int fdfs_get_storage_ids_from_tracker_server(ConnectionInfo *pTrackerServer)
{
#define MAX_REQUEST_LOOP   32
	TrackerHeader *pHeader;
	ConnectionInfo *conn;
	char out_buff[sizeof(TrackerHeader) + sizeof(int)];
	char *p;
	char *response;
	struct data_info {
		char *buffer;  //for free
		char *content;
		int length;
	} data_list[MAX_REQUEST_LOOP];
	int list_count;
	int total_count;
	int current_count;
	int result;
	int i;
	int start_index;
	int64_t in_bytes;

	if ((conn=tracker_connect_server(pTrackerServer, &result)) == NULL)
	{
		return result;
	}

	memset(data_list, 0, sizeof(data_list));
	memset(out_buff, 0, sizeof(out_buff));
	pHeader = (TrackerHeader *)out_buff;
	p = out_buff + sizeof(TrackerHeader);
	pHeader->cmd = TRACKER_PROTO_CMD_STORAGE_FETCH_STORAGE_IDS;
	long2buff(sizeof(int), pHeader->pkg_len);

	start_index = 0;
	list_count = 0;
	result = 0;
	while (1)
	{
		int2buff(start_index, p);
		if ((result=tcpsenddata_nb(conn->sock, out_buff, \
			sizeof(out_buff), g_fdfs_network_timeout)) != 0)
		{
			logError("file: "__FILE__", line: %d, " \
				"send data to tracker server %s:%d fail, " \
				"errno: %d, error info: %s", __LINE__, \
				pTrackerServer->ip_addr, \
				pTrackerServer->port, \
				result, STRERROR(result));
		}
		else
		{
			response = NULL;
			result = fdfs_recv_response(conn, \
				&response, 0, &in_bytes);
            if (result != 0)
            {
                logError("file: "__FILE__", line: %d, "
                        "fdfs_recv_response fail, result: %d",
                        __LINE__, result);
            }
		}

		if (result != 0)
		{
			break;
		}

		if (in_bytes < 2 * sizeof(int))
		{
			logError("file: "__FILE__", line: %d, " \
				"tracker server %s:%d, recv data length: %d "\
				"is invalid", __LINE__, 
				pTrackerServer->ip_addr, \
				pTrackerServer->port, (int)in_bytes);
			result = EINVAL;
			break;
		}

		total_count = buff2int(response);
		current_count = buff2int(response + sizeof(int));
		if (total_count <= start_index)
		{
			logError("file: "__FILE__", line: %d, " \
				"tracker server %s:%d, total storage " \
				"count: %d is invalid, which <= start " \
				"index: %d", __LINE__, pTrackerServer->ip_addr,\
				pTrackerServer->port, total_count, start_index);
			result = EINVAL;
			break;
		}

		if (current_count <= 0)
		{
			logError("file: "__FILE__", line: %d, " \
				"tracker server %s:%d, current storage " \
				"count: %d is invalid, which <= 0", \
				__LINE__, pTrackerServer->ip_addr,\
				pTrackerServer->port, current_count);
			result = EINVAL;
			break;
		}

		data_list[list_count].buffer = response;
		data_list[list_count].content = response + 2 * sizeof(int);
		data_list[list_count].length = in_bytes - 2 * sizeof(int);
		list_count++;

		/*
		//logInfo("list_count: %d, total_count: %d, current_count: %d", 
			list_count, total_count, current_count);
		*/

		start_index += current_count;
		if (start_index >= total_count)
		{
			break;
		}

		if (list_count == MAX_REQUEST_LOOP)
		{
			logError("file: "__FILE__", line: %d, " \
				"response data from tracker " \
				"server %s:%d is too large", \
				__LINE__, pTrackerServer->ip_addr,\
				pTrackerServer->port);
			result = ENOSPC;
			break;
		}
	}

	tracker_disconnect_server_ex(conn, result != 0);

	if (result == 0)
	{
		do
		{
			int total_length;
			char *content;

			total_length = 0;
			for (i=0; i<list_count; i++)
			{
				total_length += data_list[i].length;
			}

			content = (char *)malloc(total_length + 1);
			if (content == NULL)
			{
				result = errno != 0 ? errno : ENOMEM;
				logError("file: "__FILE__", line: %d, " \
					"malloc %d bytes fail, " \
					"errno: %d, error info: %s", \
					__LINE__, total_length + 1, \
					result, STRERROR(result));
				break;
			}

			p = content;
			for (i=0; i<list_count; i++)
			{
				memcpy(p, data_list[i].content, data_list[i].length);
				p += data_list[i].length;
			}
			*p = '\0';

			//logInfo("list_count: %d, storage ids:\n%s", list_count, content);

			result = fdfs_load_storage_ids(content, \
					"storage-ids-from-tracker");
			free(content);
		} while (0);
	}

	for (i=0; i<list_count; i++)
	{
		free(data_list[i].buffer);
	}

	return result;
}
Esempio n. 23
0
UINT32 TrackerMgr::GetGroupStat(ConnectionInfo *pTrackerServer,
	FDFSGroupStat *pStat, UINT32 nLen, UINT32 *pnStatCount)
{
	UINT32 nRet;
	UINT64 nInBytes;
	memset(pStat, 0, sizeof(FDFSGroupStat) * nLen);
	GroupStat rawStat[FDFS_MAX_GROUPS];

	TrackerHeader header;
	memset(&header, 0, sizeof(header));
	header.byCmd = TRACKER_PROTO_CMD_SERVER_LIST_ALL_GROUPS;

	EnterCriticalSection(&pTrackerServer->csSend);
	if ((nRet = tcpsenddata_nb(pTrackerServer->sock, &header, sizeof(header), DEFAULT_NETWORK_TIMEOUT)) != 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetGroupStat tcpsenddata_nb Header Failed, Error Code:%d"), nRet);
		return enumNetworkError_FDFS;
	}

	TrackerHeader resp;
	int nCount = 0;
	EnterCriticalSection(&pTrackerServer->csRecv);
	if ((nRet = tcprecvdata_nb(pTrackerServer->sock, &resp, sizeof(resp), DEFAULT_NETWORK_TIMEOUT, &nCount)) != 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetGroupStat tcprecvdata_nb Header Failed, Error Code:%d"), nRet);
		return enumNetworkError_FDFS;
	}

	if (resp.byStatus != 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetGroupStat Status Error, %d"), resp.byStatus);
		return enumFailure_FDFS;
	}

	nInBytes = buff2long64((const char*)(resp.byPkgLen));
	if (nInBytes < 0 || (nInBytes % sizeof(GroupStat)) != 0 || (nInBytes / sizeof(GroupStat)) > FDFS_MAX_GROUPS)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetGroupStat Packet Len Error"));
		return enumFailure_FDFS;
	}
	*pnStatCount = nInBytes / sizeof(GroupStat);

	//包头接收完毕,接收Body
	if ((nRet = tcprecvdata_nb(pTrackerServer->sock, rawStat, nInBytes, DEFAULT_NETWORK_TIMEOUT, &nCount)) != 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetGroupStat tcprecvdata_nb Body Failed, Error Code:%d"), nRet);
		return enumNetworkError_FDFS;
	}
	LeaveCriticalSection(&pTrackerServer->csRecv);
	LeaveCriticalSection(&pTrackerServer->csSend);

	//数据接收完毕,开始处理
	if(nLen < *pnStatCount)
	{
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetGroupStat Groups Is Too Many"));
		return enumFailure_FDFS;
	}
	for(int i = 0; i < *pnStatCount; i++)
	{
		memcpy(pStat[i].szGroupName, rawStat[i].szGroupName, FDFS_GROUP_NAME_MAX_LEN);
		pStat[i].nTotalMB = buff2long64((const char*)rawStat[i].byTotalMB);
		pStat[i].nFreeMB = buff2long64((const char*)rawStat[i].byFreeMB);
		pStat[i].nTrunkFreeMB = buff2long64((const char*)rawStat[i].byTrunkFreeMB);
		pStat[i].nCount = buff2long64((const char*)rawStat[i].byCount);
		pStat[i].nStoragePort = buff2long64((const char*)rawStat[i].byStoragePort);
		pStat[i].nStorageHttpPort = buff2long64((const char*)rawStat[i].byStorageHttpPort);
		pStat[i].nActiveCount = buff2long64((const char*)rawStat[i].byActiveCount);
		pStat[i].nCurrentWriteServer = buff2long64((const char*)rawStat[i].byCurrentWriteServer);
		pStat[i].nStorePathCount = buff2long64((const char*)rawStat[i].byStorePathCount);
		pStat[i].nSubdirCountPerPath = buff2long64((const char*)rawStat[i].bySubdirCountPerPath);
		pStat[i].nCurrentTrunkFileID = buff2long64((const char*)rawStat[i].byCurrentTrunkFileID);
	}
	return enumSuccess_FDFS;
}
Esempio n. 24
0
static int fdfs_ping_leader(ConnectionInfo *pTrackerServer)
{
	TrackerHeader header;
	int result;
	int success_count;
	int64_t in_bytes;
	char in_buff[(FDFS_GROUP_NAME_MAX_LEN + FDFS_STORAGE_ID_MAX_SIZE) * \
			FDFS_MAX_GROUPS];
	char *pInBuff;
	char *p;
	char *pEnd;
	FDFSGroupInfo *pGroup;
	char group_name[FDFS_GROUP_NAME_MAX_LEN + 1];
	char trunk_server_id[FDFS_STORAGE_ID_MAX_SIZE];

	memset(&header, 0, sizeof(header));
	header.cmd = TRACKER_PROTO_CMD_TRACKER_PING_LEADER;
	result = tcpsenddata_nb(pTrackerServer->sock, &header, \
			sizeof(header), g_fdfs_network_timeout);
	if(result != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"tracker server ip: %s, send data fail, " \
			"errno: %d, error info: %s", \
			__LINE__, pTrackerServer->ip_addr, \
			result, STRERROR(result));
		return result;
	}

	pInBuff = in_buff;
	if ((result=fdfs_recv_response(pTrackerServer, &pInBuff, \
			sizeof(in_buff), &in_bytes)) != 0)
	{
		return result;
	}

	if (in_bytes == 0)
	{
		return 0;
	}
	else if (in_bytes % (FDFS_GROUP_NAME_MAX_LEN + \
			FDFS_STORAGE_ID_MAX_SIZE) != 0)
	{
		logError("file: "__FILE__", line: %d, " \
			"tracker server ip: %s, invalid body length: " \
			"%"PRId64, __LINE__, \
			pTrackerServer->ip_addr, in_bytes);
		return EINVAL;
	}

	success_count = 0;
	memset(group_name, 0, sizeof(group_name));
	memset(trunk_server_id, 0, sizeof(trunk_server_id));

	pEnd = in_buff + in_bytes;
	for (p=in_buff; p<pEnd; p += FDFS_GROUP_NAME_MAX_LEN + \
					FDFS_STORAGE_ID_MAX_SIZE)
	{
		memcpy(group_name, p, FDFS_GROUP_NAME_MAX_LEN);
		memcpy(trunk_server_id, p + FDFS_GROUP_NAME_MAX_LEN, \
			FDFS_STORAGE_ID_MAX_SIZE - 1);

		pGroup = tracker_mem_get_group(group_name);
		if (pGroup == NULL)
		{
			logWarning("file: "__FILE__", line: %d, " \
				"tracker server ip: %s, group: %s not exists", \
				__LINE__, pTrackerServer->ip_addr, group_name);
			continue;
		}

		if (*trunk_server_id == '\0')
		{
			*(pGroup->last_trunk_server_id) = '\0';
			pGroup->pTrunkServer = NULL;
			success_count++;
			continue;
		}

		pGroup->pTrunkServer = tracker_mem_get_storage(pGroup, \
							trunk_server_id);
		if (pGroup->pTrunkServer == NULL)
		{
			logWarning("file: "__FILE__", line: %d, " \
				"tracker server ip: %s, group: %s, " \
				"trunk server: %s not exists", \
				__LINE__, pTrackerServer->ip_addr, \
				group_name, trunk_server_id);
		}
		snprintf(pGroup->last_trunk_server_id, sizeof( \
			pGroup->last_trunk_server_id), "%s", trunk_server_id);
		success_count++;
	}

	if (success_count > 0)
	{
		tracker_save_groups();
	}

	return 0;
}
Esempio n. 25
0
UINT32 TrackerMgr::GetStorageStat(ConnectionInfo *pTrackerServer,
	TCHAR *pszGroupName, FDFSStorageStat *pStat, UINT32 nLen,
	UINT32 *pnStatCount)
{
	UINT32 nRet;
	UINT64 nInBytes;
	StorageStat rawStat[FDFS_MAX_GROUPS];
	BYTE byOut[sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN];
	memset(byOut, 0, sizeof(byOut));
	memset(pStat, 0, sizeof(FDFSStorageStat) * nLen);
	UINT32 nNameLen = strlen(pszGroupName);
	if (nNameLen > FDFS_GROUP_NAME_MAX_LEN)
	{
		nNameLen = FDFS_GROUP_NAME_MAX_LEN;
	}

	TrackerHeader *pHeader = (TrackerHeader*)byOut;
	pHeader->byCmd = TRACKER_PROTO_CMD_SERVER_LIST_STORAGE;
	memcpy(byOut + sizeof(TrackerHeader), pszGroupName, nNameLen);
	long642buff(FDFS_GROUP_NAME_MAX_LEN, (char*)pHeader->byPkgLen);

	EnterCriticalSection(&pTrackerServer->csSend);
	if ((nRet = tcpsenddata_nb(pTrackerServer->sock, byOut, sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN, DEFAULT_NETWORK_TIMEOUT)) != 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetStorageStat tcpsenddata_nb Header Failed, Error Code:%d"), nRet);
		return enumNetworkError_FDFS;
	}

	TrackerHeader resp;
	int nCount = 0;
	EnterCriticalSection(&pTrackerServer->csRecv);
	if ((nRet = tcprecvdata_nb(pTrackerServer->sock, &resp, sizeof(resp), DEFAULT_NETWORK_TIMEOUT, &nCount)) != 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetStorageStat tcprecvdata_nb Header Failed, Error Code:%d"), nRet);
		return enumNetworkError_FDFS;
	}

	if (resp.byStatus != 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetStorageStat Status Error, %d"), resp.byStatus);
		return enumFailure_FDFS;
	}

	nInBytes = buff2long64((const char*)(resp.byPkgLen));
	if (nInBytes < 0 || (nInBytes % sizeof(StorageStat)) != 0 || (nInBytes / sizeof(StorageStat)) > FDFS_MAX_SERVERS_EACH_GROUP)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetStorageStat Packet Len Error"));
		return enumFailure_FDFS;
	}
	*pnStatCount = nInBytes / sizeof(StorageStat);

	//包头接收完毕,接收Body
	if ((nRet = tcprecvdata_nb(pTrackerServer->sock, rawStat, nInBytes, DEFAULT_NETWORK_TIMEOUT, &nCount)) != 0)
	{
		closesocket(pTrackerServer->sock);
		pTrackerServer->sock = INVALID_SOCKET;
		LeaveCriticalSection(&pTrackerServer->csRecv);
		LeaveCriticalSection(&pTrackerServer->csSend);
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetStorageStat tcprecvdata_nb Body Failed, Error Code:%d"), nRet);
		return enumNetworkError_FDFS;
	}
	LeaveCriticalSection(&pTrackerServer->csRecv);
	LeaveCriticalSection(&pTrackerServer->csSend);

	//数据接收完毕,开始处理
	if(nLen < *pnStatCount)
	{
		WriteLogInfo(LogFileName, FDFSC_ERROR_MODE, _T("TrackerMgr::GetStorageStat Storages Is Too Many"));
		return enumFailure_FDFS;
	}
	
	StorageStatBuff *pStatBuff = NULL;
	StorageStat *pSrc = NULL;
	FDFSStorageInfo *pStorageInfo = NULL;
	FDFSStorageStat *pDest = NULL;

	for(int i = 0; i < *pnStatCount; i++)
	{
		pDest = &pStat[i];
		pSrc = &rawStat[i];
		pStatBuff = &(pSrc->statBuff);
		pStorageInfo = &(pDest->stat);

		pDest->byStatus = pSrc->byStatus;
		//保留最后一个\0
		memcpy(pDest->szID, pSrc->byID, FDFS_STORAGE_ID_MAX_SIZE - 1);
		memcpy(pDest->szIpAddr, pSrc->byIP, IP_ADDRESS_SIZE - 1);
		memcpy(pDest->szSrcId, pSrc->bySrcID, FDFS_STORAGE_ID_MAX_SIZE - 1);
		strcpy(pDest->szDomainName, (const char*)pSrc->byDomainName);
		strcpy(pDest->szVersion, (const char*)pSrc->byVersion);
		pDest->joinTime = buff2long64((const char*)pSrc->byJoinTime);
		pDest->upTime = buff2long64((const char*)pSrc->byUpTime);
		pDest->nTotalMb = buff2long64((const char*)pSrc->byTotalMB);
		pDest->nFreeMb = buff2long64((const char*)pSrc->byFreeMB);
		pDest->nUploadPriority = buff2long64((const char*)pSrc->byUploadPriority);
		pDest->nStorePathCount = buff2long64((const char*)pSrc->byStorePathCount);
		pDest->nSubdirCountPerPath = buff2long64((const char*)pSrc->bySubdirCountPerPath);
		pDest->nStoragePort = buff2long64((const char*)pSrc->byStoragePort);
		pDest->nStorageHttpPort = buff2long64((const char*)pSrc->byStorageHttpPort);
		pDest->nCurrentWritePath = buff2long64((const char*)pSrc->byCurrentWritePath);

		pStorageInfo->connection.nAllocCount = buff2int((const char*)pStatBuff->connection.byAllocCount);
		pStorageInfo->connection.nCurrentCount = buff2int((const char*)pStatBuff->connection.byCurrentCount);
		pStorageInfo->connection.nMaxCount = buff2int((const char*)pStatBuff->connection.byMaxCount);

		pStorageInfo->nTotalUploadCount = buff2long64((const char*)pStatBuff->byTotalUploadCount);
		pStorageInfo->nSuccessUploadCount = buff2long64((const char*)pStatBuff->bySuccessUploadCount);
		pStorageInfo->nTotalAppendCount = buff2long64((const char*)pStatBuff->byTotalAppendCount);
		pStorageInfo->nSuccessAppendCount = buff2long64((const char*)pStatBuff->bySuccessAppendCount);
		pStorageInfo->nTotalModifyCount = buff2long64((const char*)pStatBuff->byTotalModifyCount);
		pStorageInfo->nSuccessModifyCount = buff2long64((const char*)pStatBuff->bySuccessModifyCount);
		pStorageInfo->nTotalTruncateCount = buff2long64((const char*)pStatBuff->byTotalTruncateCount);
		pStorageInfo->nSuccessTruncateCount = buff2long64((const char*)pStatBuff->bySuccessTruncateCount);
		pStorageInfo->nTotalSetMetaCount = buff2long64((const char*)pStatBuff->byTotalSetMetaCount);
		pStorageInfo->nSuccessSetMetaCount = buff2long64((const char*)pStatBuff->bySuccessSetMetaCount);

		pStorageInfo->nTotalDeleteCount = buff2long64((const char*)pStatBuff->byTotalDeleteCount);
		pStorageInfo->nSuccessDeleteCount = buff2long64((const char*)pStatBuff->bySuccessDeleteCount);
		pStorageInfo->nTotalDownloadCount = buff2long64((const char*)pStatBuff->byTotalDownloadCount);
		pStorageInfo->nSuccessDownloadCount = buff2long64((const char*)pStatBuff->bySuccessDownloadCount);
		pStorageInfo->nTotalGetMetaCount = buff2long64((const char*)pStatBuff->byTotalGetMetaCount);
		pStorageInfo->nSuccessGetMetaCount = buff2long64((const char*)pStatBuff->bySuccessGetMetaCount);
		pStorageInfo->lastSourceUpdate = buff2long64((const char*)pStatBuff->byLastSourceUpdate);
		pStorageInfo->lastSyncUpdate = buff2long64((const char*)pStatBuff->byLastSyncUpdate);
		pStorageInfo->lastSyncedTimestamp = buff2long64((const char*)pStatBuff->byLastSyncedTimestamp);
		pStorageInfo->nTotalCreateLinkCount = buff2long64((const char*)pStatBuff->byTotalCreateLinkCount);
		pStorageInfo->nSuccessCreateLinkCount = buff2long64((const char*)pStatBuff->bySuccessCreateLinkCount);
		pStorageInfo->nTotalDeleteLinkCount = buff2long64((const char*)pStatBuff->byTotalDeleteLinkCount);
		pStorageInfo->nSuccessDeleteLinkCount = buff2long64((const char*)pStatBuff->bySuccessDeleteLinkCount);
		pStorageInfo->nTotalUploadBytes = buff2long64((const char*)pStatBuff->byTotalUploadBytes);
		pStorageInfo->nSuccessUploadBytes = buff2long64((const char*)pStatBuff->bySuccessUploadBytes);
		pStorageInfo->nTotalAppendBytes = buff2long64((const char*)pStatBuff->byTotalAppendBytes);
		pStorageInfo->nSuccessAppendBytes = buff2long64((const char*)pStatBuff->bySuccessAppendBytes);
		pStorageInfo->nTotalModifyBytes = buff2long64((const char*)pStatBuff->byTotalModifyBytes);
		pStorageInfo->nSuccessModifyBytes = buff2long64((const char*)pStatBuff->bySuccessModifyBytes);
		pStorageInfo->nTotalDownloadBytes = buff2long64((const char*)pStatBuff->byTotalDownloadBytes);
		pStorageInfo->nSuccessDownloadBytes = buff2long64((const char*)pStatBuff->bySuccessDownloadBytes);
		pStorageInfo->nTotalSyncInBytes = buff2long64((const char*)pStatBuff->byTotalSyncInBytes);
		pStorageInfo->nSuccessSyncInBytes = buff2long64((const char*)pStatBuff->bySuccessSyncInBytes);
		pStorageInfo->nTotalSyncOutBytes = buff2long64((const char*)pStatBuff->byTotalSyncOutBytes);
		pStorageInfo->nSuccessSyncOutBytes = buff2long64((const char*)pStatBuff->bySuccessSyncOutBytes);
		pStorageInfo->nTotalFileOpenCount = buff2long64((const char*)pStatBuff->byTotalFileOpenCount);
		pStorageInfo->nSuccessFileOpenCount = buff2long64((const char*)pStatBuff->bySuccessFileOpenCount);
		pStorageInfo->nTotalFileReadCount = buff2long64((const char*)pStatBuff->byTotalFileReadCount);
		pStorageInfo->nSuccessFileReadCount = buff2long64((const char*)pStatBuff->bySuccessFileReadCount);
		pStorageInfo->nTotalFileWriteCount = buff2long64((const char*)pStatBuff->byTotalFileWriteCount);
		pStorageInfo->nSuccessFileWriteCount = buff2long64((const char*)pStatBuff->bySuccessFileWriteCount);
		pStorageInfo->lastHeartBeatTime = buff2long64((const char*)pStatBuff->byLastHeartBeatTime);
		pDest->bIfTrunkServer = pSrc->byIfTrunkServer;
	}
	return enumSuccess_FDFS;
}
Esempio n. 26
0
int tracker_delete_storage(TrackerServerGroup *pTrackerGroup, \
		const char *group_name, const char *storage_id)
{
	ConnectionInfo *conn;
	TrackerHeader *pHeader;
	ConnectionInfo tracker_server;
	ConnectionInfo *pServer;
	ConnectionInfo *pEnd;
	FDFSStorageInfo storage_infos[1];
	char out_buff[sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + \
			FDFS_STORAGE_ID_MAX_SIZE];
	char in_buff[1];
	char *pInBuff;
	int64_t in_bytes;
	int result;
	int storage_id_len;
	int storage_count;
	int enoent_count;

	enoent_count = 0;
	pEnd = pTrackerGroup->servers + pTrackerGroup->server_count;
	for (pServer=pTrackerGroup->servers; pServer<pEnd; pServer++)
	{
		memcpy(&tracker_server, pServer, sizeof(ConnectionInfo));
		tracker_server.sock = -1;
		if ((conn=tracker_connect_server(&tracker_server, &result)) == NULL)
		{
			return result;
		}

		result = tracker_list_servers(conn, group_name, storage_id, \
				storage_infos, 1, &storage_count);
		tracker_disconnect_server_ex(conn, result != 0 && result != ENOENT);
		if (result != 0 && result != ENOENT)
		{
			return result;
		}

		if (result == ENOENT || storage_count == 0)
		{
			enoent_count++;
			continue;
		}

		if (storage_infos[0].status == FDFS_STORAGE_STATUS_ONLINE
		   || storage_infos[0].status == FDFS_STORAGE_STATUS_ACTIVE)
		{
			return EBUSY;
		}
	}
	if (enoent_count == pTrackerGroup->server_count)
	{
		return ENOENT;
	}

	memset(out_buff, 0, sizeof(out_buff));
	pHeader = (TrackerHeader *)out_buff;
	snprintf(out_buff + sizeof(TrackerHeader), sizeof(out_buff) - \
			sizeof(TrackerHeader),  "%s", group_name);
	storage_id_len = snprintf(out_buff + sizeof(TrackerHeader) + \
			FDFS_GROUP_NAME_MAX_LEN, \
			sizeof(out_buff) - sizeof(TrackerHeader) - \
			FDFS_GROUP_NAME_MAX_LEN,  "%s", storage_id);
	
	long2buff(FDFS_GROUP_NAME_MAX_LEN + storage_id_len, pHeader->pkg_len);
	pHeader->cmd = TRACKER_PROTO_CMD_SERVER_DELETE_STORAGE;

	enoent_count = 0;
	result = 0;
	for (pServer=pTrackerGroup->servers; pServer<pEnd; pServer++)
	{
		memcpy(&tracker_server, pServer, sizeof(ConnectionInfo));
		tracker_server.sock = -1;
		if ((conn=tracker_connect_server(&tracker_server, &result)) == NULL)
		{
			return result;
		}

		if ((result=tcpsenddata_nb(conn->sock, out_buff, \
			sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + 
			storage_id_len, g_fdfs_network_timeout)) != 0)
		{
			logError("file: "__FILE__", line: %d, " \
				"send data to tracker server %s:%d fail, " \
				"errno: %d, error info: %s", __LINE__, \
				tracker_server.ip_addr, tracker_server.port, \
				result, STRERROR(result));
		}
		else
		{
			pInBuff = in_buff;
			result = fdfs_recv_response(conn, &pInBuff, 0, &in_bytes);
		}

		tracker_disconnect_server_ex(conn, result != 0 && result != ENOENT);
		if (result != 0)
		{
			if (result == ENOENT)
			{
				enoent_count++;
			}
			else if (result == EALREADY)
			{
			}
			else
			{
				return result;
			}
		}
	}

	if (enoent_count == pTrackerGroup->server_count)
	{
		return ENOENT;
	}

	return result == ENOENT ? 0 : result;
}
Esempio n. 27
0
static int trunk_sync_data(TrunkBinLogReader *pReader, \
		ConnectionInfo *pStorage)
{
	int length;
	char *p;
	int result;
	TrackerHeader header;
	char in_buff[1];
	char *pBuff;
	int64_t in_bytes;

	p = pReader->binlog_buff.buffer + pReader->binlog_buff.length - 1;
	while (p != pReader->binlog_buff.buffer && *p != '\n')
	{
		p--;
	}

	length = p - pReader->binlog_buff.buffer;
	if (length == 0)
	{
		logWarning("FILE: "__FILE__", line: %d, " \
			"no buffer to sync, buffer length: %d, " \
			"should try again later", __LINE__, \
			pReader->binlog_buff.length);
		return ENOENT;
	}
	length++;

	memset(&header, 0, sizeof(header));
	long2buff(length, header.pkg_len);
	header.cmd = STORAGE_PROTO_CMD_TRUNK_SYNC_BINLOG;
	if ((result=tcpsenddata_nb(pStorage->sock, &header, \
		sizeof(TrackerHeader), g_fdfs_network_timeout)) != 0)
	{
		logError("FILE: "__FILE__", line: %d, " \
			"send data to storage server %s:%d fail, " \
			"errno: %d, error info: %s", \
			__LINE__, pStorage->ip_addr, pStorage->port, \
			result, STRERROR(result));
		return result;
	}

	if ((result=tcpsenddata_nb(pStorage->sock, pReader->binlog_buff.buffer,\
		length, g_fdfs_network_timeout)) != 0)
	{
		logError("FILE: "__FILE__", line: %d, " \
			"send data to storage server %s:%d fail, " \
			"errno: %d, error info: %s", \
			__LINE__, pStorage->ip_addr, pStorage->port, \
			result, STRERROR(result));
		return result;
	}

	pBuff = in_buff;
	if ((result=fdfs_recv_response(pStorage, &pBuff, 0, &in_bytes)) != 0)
	{
		logError("file: "__FILE__", line: %d, "
                "fdfs_recv_response fail, result: %d",
                __LINE__, result);
		return result;
	}

	pReader->binlog_offset += length;
	pReader->binlog_buff.length -= length;
	if (pReader->binlog_buff.length > 0)
	{
		pReader->binlog_buff.current = pReader->binlog_buff.buffer + length;
	}

	return 0;
}