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; }
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; }
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; }
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; }
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; }
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; }
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; }
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; }
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; }
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; }
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; }
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; }
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; }
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; }
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; }