diff --git a/storage/Makefile.in b/storage/Makefile.in index 7815196..cda713b 100644 --- a/storage/Makefile.in +++ b/storage/Makefile.in @@ -9,11 +9,12 @@ CONFIG_PATH = $(TARGET_CONF_PATH) SHARED_OBJS = ../common/fdfs_global.o ../tracker/fdfs_shared_func.o \ ../tracker/fdfs_server_id_func.o ../tracker/tracker_proto.o \ tracker_client_thread.o storage_global.o storage_func.o \ - storage_service.o storage_sync.o storage_nio.o storage_dio.o \ - storage_ip_changed_dealer.o storage_param_getter.o \ - storage_disk_recovery.o trunk_mgr/trunk_mem.o \ - trunk_mgr/trunk_shared.o trunk_mgr/trunk_sync.o \ - trunk_mgr/trunk_client.o trunk_mgr/trunk_free_block_checker.o \ + storage_sync_func.o storage_service.o storage_sync.o \ + storage_nio.o storage_dio.o storage_ip_changed_dealer.o \ + storage_param_getter.o storage_disk_recovery.o \ + trunk_mgr/trunk_mem.o trunk_mgr/trunk_shared.o \ + trunk_mgr/trunk_sync.o trunk_mgr/trunk_client.o \ + trunk_mgr/trunk_free_block_checker.o \ ../client/client_global.o ../client/tracker_client.o \ ../client/storage_client.o ../client/client_func.o \ fdht_client/fdht_proto.o fdht_client/fdht_client.o \ diff --git a/storage/storage_disk_recovery.c b/storage/storage_disk_recovery.c index f3ac8eb..6cd9a11 100644 --- a/storage/storage_disk_recovery.c +++ b/storage/storage_disk_recovery.c @@ -134,15 +134,15 @@ static int recovery_get_src_storage_server(ConnectionInfo *pSrcStorage) __LINE__); while (g_continue_flag) { - result = tracker_get_storage_max_status(&g_tracker_group, \ - g_group_name, g_tracker_client_ip, \ - g_my_server_id_str, &saved_storage_status); + result = tracker_get_storage_max_status(&g_tracker_group, + g_group_name, g_tracker_client_ip.ips[0], + g_my_server_id_str, &saved_storage_status); if (result == ENOENT) { logWarning("file: "__FILE__", line: %d, " \ "current storage: %s does not exist " \ "in tracker server", __LINE__, \ - g_tracker_client_ip); + g_tracker_client_ip.ips[0]); return ENOENT; } @@ -153,7 +153,7 @@ static int recovery_get_src_storage_server(ConnectionInfo *pSrcStorage) logInfo("file: "__FILE__", line: %d, " \ "current storage: %s 's status is %d" \ ", does not need recovery", __LINE__, \ - g_tracker_client_ip, \ + g_tracker_client_ip.ips[0], \ saved_storage_status); return ENOENT; } @@ -164,7 +164,7 @@ static int recovery_get_src_storage_server(ConnectionInfo *pSrcStorage) logWarning("file: "__FILE__", line: %d, " \ "current storage: %s 's status is %d" \ ", does not need recovery", __LINE__, \ - g_tracker_client_ip, saved_storage_status); + g_tracker_client_ip.ips[0], saved_storage_status); return ENOENT; } @@ -817,7 +817,7 @@ int storage_disk_recovery_restore(const char *pBasePath) while (g_continue_flag) { if (storage_report_storage_status(g_my_server_id_str, \ - g_tracker_client_ip, saved_storage_status) == 0) + g_tracker_client_ip.ips[0], saved_storage_status) == 0) { break; } @@ -1103,7 +1103,7 @@ int storage_disk_recovery_start(const int store_path_index) while (g_continue_flag) { if (storage_report_storage_status(g_my_server_id_str, \ - g_tracker_client_ip, FDFS_STORAGE_STATUS_RECOVERY) == 0) + g_tracker_client_ip.ips[0], FDFS_STORAGE_STATUS_RECOVERY) == 0) { break; } diff --git a/storage/storage_dump.c b/storage/storage_dump.c index f8331ba..32a62db 100644 --- a/storage/storage_dump.c +++ b/storage/storage_dump.c @@ -30,9 +30,16 @@ static int fdfs_dump_global_vars(char *buff, const int buffSize) char szSyncUntilTimestamp[32]; char szUptime[32]; char reserved_space_str[32]; + char tracker_client_ip_str[256]; + char last_storage_ip_str[256]; int total_len; int i; + fdfs_multi_ips_to_string(&g_tracker_client_ip, + tracker_client_ip_str, sizeof(tracker_client_ip_str)); + fdfs_multi_ips_to_string(&g_last_storage_ip, + last_storage_ip_str, sizeof(last_storage_ip_str)); + total_len = snprintf(buff, buffSize, "g_fdfs_connect_timeout=%ds\n" "g_fdfs_network_timeout=%ds\n" @@ -177,8 +184,8 @@ static int fdfs_dump_global_vars(char *buff, const int buffSize) , formatDatetime(g_sync_until_timestamp, "%Y-%m-%d %H:%M:%S", szSyncUntilTimestamp, sizeof(szSyncUntilTimestamp)) , g_my_server_id_str - , g_tracker_client_ip - , g_last_storage_ip + , tracker_client_ip_str + , last_storage_ip_str , g_check_file_duplicate , g_key_namespace , g_namespace_len diff --git a/storage/storage_func.c b/storage/storage_func.c index acaa5ed..521cd98 100644 --- a/storage/storage_func.c +++ b/storage/storage_func.c @@ -208,8 +208,9 @@ static int storage_get_group_name_from_tracker() static int tracker_get_my_server_id() { struct in_addr ip_addr; + char ip_str[256]; - if (inet_pton(AF_INET, g_tracker_client_ip, &ip_addr) == 1) + if (inet_pton(AF_INET, g_tracker_client_ip.ips[0], &ip_addr) == 1) { g_server_id_in_filename = ip_addr.s_addr; } @@ -217,7 +218,7 @@ static int tracker_get_my_server_id() { logError("file: "__FILE__", line: %d, " \ "call inet_pton for ip: %s fail", \ - __LINE__,g_tracker_client_ip); + __LINE__, g_tracker_client_ip.ips[0]); g_server_id_in_filename = INADDR_NONE; } @@ -232,8 +233,9 @@ static int tracker_get_my_server_id() return errno != 0 ? errno : ECONNREFUSED; } - result = tracker_get_storage_id(pTrackerServer, \ - g_group_name, g_tracker_client_ip, g_my_server_id_str); + result = tracker_get_storage_id(pTrackerServer, + g_group_name, g_tracker_client_ip.ips[0], + g_my_server_id_str); tracker_close_connection_ex(pTrackerServer, result != 0); if (result != 0) { @@ -247,14 +249,16 @@ static int tracker_get_my_server_id() } else { - snprintf(g_my_server_id_str, sizeof(g_my_server_id_str), "%s", \ - g_tracker_client_ip); + snprintf(g_my_server_id_str, sizeof(g_my_server_id_str), "%s", + g_tracker_client_ip.ips[0]); } - logInfo("file: "__FILE__", line: %d, " \ - "tracker_client_ip: %s, my_server_id_str: %s, " \ - "g_server_id_in_filename: %d", __LINE__, \ - g_tracker_client_ip, g_my_server_id_str, g_server_id_in_filename); + fdfs_multi_ips_to_string(&g_tracker_client_ip, + ip_str, sizeof(ip_str)); + logInfo("file: "__FILE__", line: %d, " + "tracker_client_ip: %s, my_server_id_str: %s, " + "g_server_id_in_filename: %d", __LINE__, + ip_str, g_my_server_id_str, g_server_id_in_filename); return 0; } @@ -625,6 +629,7 @@ int storage_write_to_sync_ini_file() { char full_filename[MAX_PATH_SIZE]; char buff[512]; + char ip_str[256]; int fd; int len; @@ -640,6 +645,8 @@ int storage_write_to_sync_ini_file() return errno != 0 ? errno : ENOENT; } + fdfs_multi_ips_to_string(&g_tracker_client_ip, + ip_str, sizeof(ip_str)); len = sprintf(buff, "%s=%d\n" \ "%s=%d\n" \ "%s=%s\n" \ @@ -653,7 +660,7 @@ int storage_write_to_sync_ini_file() INIT_ITEM_SYNC_OLD_DONE, g_sync_old_done, \ INIT_ITEM_SYNC_SRC_SERVER, g_sync_src_id, \ INIT_ITEM_SYNC_UNTIL_TIMESTAMP, g_sync_until_timestamp, \ - INIT_ITEM_LAST_IP_ADDRESS, g_tracker_client_ip, \ + INIT_ITEM_LAST_IP_ADDRESS, ip_str, \ INIT_ITEM_LAST_SERVER_PORT, g_last_server_port, \ INIT_ITEM_LAST_HTTP_PORT, g_last_http_port, INIT_ITEM_CURRENT_TRUNK_FILE_ID, g_current_trunk_file_id, \ @@ -683,6 +690,7 @@ static int storage_check_and_make_data_dirs() int i; char data_path[MAX_PATH_SIZE]; char full_filename[MAX_PATH_SIZE]; + char error_info[256]; bool pathCreated; snprintf(data_path, sizeof(data_path), "%s/data", \ @@ -752,8 +760,8 @@ static int storage_check_and_make_data_dirs() &iniContext); if (pValue != NULL) { - snprintf(g_last_storage_ip, sizeof(g_last_storage_ip), \ - "%s", pValue); + fdfs_parse_multi_ips(pValue, &g_last_storage_ip, + error_info, sizeof(error_info)); } pValue = iniGetStrValue(NULL, INIT_ITEM_LAST_SERVER_PORT, \ diff --git a/storage/storage_global.c b/storage/storage_global.c index 7fbd313..99065db 100644 --- a/storage/storage_global.c +++ b/storage/storage_global.c @@ -71,8 +71,8 @@ char g_sync_src_id[FDFS_STORAGE_ID_MAX_SIZE] = {0}; char g_group_name[FDFS_GROUP_NAME_MAX_LEN + 1] = {0}; char g_my_server_id_str[FDFS_STORAGE_ID_MAX_SIZE] = {0}; //my server id string -char g_tracker_client_ip[IP_ADDRESS_SIZE] = {0}; //storage ip as tracker client -char g_last_storage_ip[IP_ADDRESS_SIZE] = {0}; //the last storage ip address +FDFSMultiIP g_tracker_client_ip = {0, 0}; //storage ip as tracker client +FDFSMultiIP g_last_storage_ip = {0, 0}; //the last storage ip address LogContext g_access_log_context = {LOG_INFO, STDERR_FILENO, NULL}; @@ -129,3 +129,32 @@ int storage_cmp_by_server_id(const void *p1, const void *p2) (*((FDFSStorageServer **)p2))->server.id); } + +int storage_insert_ip_addr_to_multi_ips(FDFSMultiIP *multi_ip, + const char *ip_addr, const int ips_limit) +{ + int i; + if (multi_ip->count == 0) + { + multi_ip->count = 1; + strcpy(multi_ip->ips[0], ip_addr); + return 0; + } + + for (i = 0; i < multi_ip->count; i++) + { + if (strcmp(multi_ip->ips[i], ip_addr) == 0) + { + return EEXIST; + } + } + + if (i >= ips_limit) + { + return ENOSPC; + } + + strcpy(multi_ip->ips[i], ip_addr); + multi_ip->count++; + return 0; +} diff --git a/storage/storage_global.h b/storage/storage_global.h index 7b30959..4223f58 100644 --- a/storage/storage_global.h +++ b/storage/storage_global.h @@ -116,8 +116,8 @@ extern char g_sync_src_id[FDFS_STORAGE_ID_MAX_SIZE]; //the source storage server extern char g_group_name[FDFS_GROUP_NAME_MAX_LEN + 1]; extern char g_my_server_id_str[FDFS_STORAGE_ID_MAX_SIZE]; //my server id string -extern char g_tracker_client_ip[IP_ADDRESS_SIZE]; //storage ip as tracker client -extern char g_last_storage_ip[IP_ADDRESS_SIZE]; //the last storage ip address +extern FDFSMultiIP g_tracker_client_ip; //storage ip as tracker client +extern FDFSMultiIP g_last_storage_ip; //the last storage ip address extern LogContext g_access_log_context; @@ -173,6 +173,9 @@ extern struct storage_dio_thread_data *g_dio_thread_data; //disk io thread data int storage_cmp_by_server_id(const void *p1, const void *p2); +int storage_insert_ip_addr_to_multi_ips(FDFSMultiIP *multi_ip, + const char *ip_addr, const int ips_limit); + #ifdef __cplusplus } #endif diff --git a/storage/storage_ip_changed_dealer.c b/storage/storage_ip_changed_dealer.c index 5dd5d20..f768e0f 100644 --- a/storage/storage_ip_changed_dealer.c +++ b/storage/storage_ip_changed_dealer.c @@ -76,9 +76,9 @@ static int storage_report_ip_changed(ConnectionInfo *pTrackerServer) pHeader->cmd = TRACKER_PROTO_CMD_STORAGE_REPORT_IP_CHANGED; strcpy(out_buff + sizeof(TrackerHeader), g_group_name); strcpy(out_buff + sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN, \ - g_last_storage_ip); + g_last_storage_ip.ips[0]); strcpy(out_buff + sizeof(TrackerHeader) + FDFS_GROUP_NAME_MAX_LEN + \ - IP_ADDRESS_SIZE, g_tracker_client_ip); + IP_ADDRESS_SIZE, g_tracker_client_ip.ips[0]); if((result=tcpsenddata_nb(pTrackerServer->sock, out_buff, \ sizeof(out_buff), g_fdfs_network_timeout)) != 0) @@ -166,25 +166,26 @@ int storage_get_my_tracker_client_ip() continue; } - //TODO support multi IPs !!! + getSockIpaddr(conn->sock, tracker_client_ip, IP_ADDRESS_SIZE); - getSockIpaddr(conn->sock,tracker_client_ip,IP_ADDRESS_SIZE); - if (*g_tracker_client_ip == '\0') - { - strcpy(g_tracker_client_ip, tracker_client_ip); - } - else if (strcmp(tracker_client_ip, g_tracker_client_ip) != 0) - { - logError("file: "__FILE__", line: %d, " - "as a client of tracker server %s:%d, " - "my ip: %s != client ip: %s of other " - "tracker client", __LINE__, - conn->ip_addr, conn->port, - tracker_client_ip, g_tracker_client_ip); + result = storage_insert_ip_addr_to_multi_ips(&g_tracker_client_ip, + tracker_client_ip, 1); + if (!(result == 0 || result == EEXIST)) + { + char ip_str[256]; + + fdfs_multi_ips_to_string(&g_tracker_client_ip, + ip_str, sizeof(ip_str)); + logError("file: "__FILE__", line: %d, " + "as a client of tracker server %s:%d, " + "my ip: %s not consistent with client ips: %s " + "of other tracker client.", __LINE__, + conn->ip_addr, conn->port, + tracker_client_ip, ip_str); close(conn->sock); return EINVAL; - } + } fdfs_quit(conn); close(conn->sock); @@ -216,15 +217,17 @@ static int storage_report_storage_ip_addr() pTServer = &trackerServer; pTServerEnd = g_tracker_group.servers + g_tracker_group.server_count; - logDebug("file: "__FILE__", line: %d, " \ - "last my ip is %s, current my ip is %s", \ - __LINE__, g_last_storage_ip, g_tracker_client_ip); + logDebug("file: "__FILE__", line: %d, " + "last my ip is %s, current my ip is %s", + __LINE__, g_last_storage_ip.ips[0], + g_tracker_client_ip.ips[0]); - if (*g_last_storage_ip == '\0') + if (g_last_storage_ip.count == 0) { return storage_write_to_sync_ini_file(); } - else if (strcmp(g_tracker_client_ip, g_last_storage_ip) == 0) + else if (strcmp(g_tracker_client_ip.ips[0], + g_last_storage_ip.ips[0]) == 0) { return 0; } @@ -367,7 +370,7 @@ int storage_check_ip_changed() return result; } - if (*g_last_storage_ip == '\0') //first run + if (g_last_storage_ip.count == 0) //first run { return 0; } diff --git a/storage/storage_sync.c b/storage/storage_sync.c index 55e84b2..20e1633 100644 --- a/storage/storage_sync.c +++ b/storage/storage_sync.c @@ -34,8 +34,9 @@ #include "storage_ip_changed_dealer.h" #include "tracker_client_thread.h" #include "storage_client.h" -#include "storage_sync.h" #include "trunk_mem.h" +#include "storage_sync_func.h" +#include "storage_sync.h" #define SYNC_BINLOG_FILE_MAX_SIZE 1024 * 1024 * 1024 #define SYNC_BINLOG_FILE_PREFIX "binlog" @@ -2550,11 +2551,8 @@ static void* storage_sync_thread_entrance(void* arg) char local_ip_addr[IP_ADDRESS_SIZE]; int read_result; int sync_result; - int conn_result; int result; int record_len; - int previousCode; - int nContinuousFail; time_t current_time; time_t start_time; time_t end_time; @@ -2617,83 +2615,7 @@ static void* storage_sync_thread_entrance(void* arg) } } - previousCode = 0; - nContinuousFail = 0; - conn_result = 0; - while (g_continue_flag && \ - pStorage->status != FDFS_STORAGE_STATUS_DELETED && \ - pStorage->status != FDFS_STORAGE_STATUS_IP_CHANGED && \ - pStorage->status != FDFS_STORAGE_STATUS_NONE) - { - strcpy(storage_server.ip_addr, pStorage->ip_addr); - - storage_server.sock = socketCreateExAuto(pStorage->ip_addr, - g_fdfs_connect_timeout, O_NONBLOCK, - g_client_bind_addr ? g_bind_addr : NULL, &result); - if (storage_server.sock < 0) - { - logCrit("file: "__FILE__", line: %d, " - "socket create fail, program exit!", __LINE__); - g_continue_flag = false; - break; - } - - if ((conn_result=connectserverbyip_nb(storage_server.sock, - pStorage->ip_addr, g_server_port, - g_fdfs_connect_timeout)) == 0) - { - char szFailPrompt[64]; - if (nContinuousFail == 0) - { - *szFailPrompt = '\0'; - } - else - { - sprintf(szFailPrompt, \ - ", continuous fail count: %d", \ - nContinuousFail); - } - logInfo("file: "__FILE__", line: %d, " \ - "successfully connect to " \ - "storage server %s:%d%s", __LINE__, \ - pStorage->ip_addr, \ - g_server_port, szFailPrompt); - nContinuousFail = 0; - break; - } - - if (previousCode != conn_result) - { - logError("file: "__FILE__", line: %d, " \ - "connect to storage server %s:%d fail" \ - ", errno: %d, error info: %s", \ - __LINE__, \ - pStorage->ip_addr, g_server_port, \ - conn_result, STRERROR(conn_result)); - previousCode = conn_result; - } - - nContinuousFail++; - close(storage_server.sock); - storage_server.sock = -1; - - if (!g_continue_flag) - { - break; - } - - sleep(1); - } - - if (nContinuousFail > 0) - { - logError("file: "__FILE__", line: %d, " \ - "connect to storage server %s:%d fail, " \ - "try count: %d, errno: %d, error info: %s", \ - __LINE__, pStorage->ip_addr, \ - g_server_port, nContinuousFail, \ - conn_result, STRERROR(conn_result)); - } + storage_sync_connect_storage_server(pStorage, &storage_server); if ((!g_continue_flag) || pStorage->status == FDFS_STORAGE_STATUS_DELETED || \ diff --git a/storage/storage_sync_func.c b/storage/storage_sync_func.c new file mode 100644 index 0000000..6f527e9 --- /dev/null +++ b/storage/storage_sync_func.c @@ -0,0 +1,145 @@ +/** * Copyright (C) 2008 Happy Fish / YuQing +* +* FastDFS may be copied only under the terms of the GNU General +* Public License V3, which may be found in the FastDFS source kit. +* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. +**/ + +//storage_sync_func.c + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "fdfs_define.h" +#include "fdfs_global.h" +#include "fastcommon/logger.h" +#include "fastcommon/sockopt.h" +#include "tracker_types.h" +#include "tracker_proto.h" +#include "storage_global.h" +#include "storage_func.h" +#include "storage_sync_func.h" + +void storage_sync_connect_storage_server_ex(FDFSStorageBrief *pStorage, + ConnectionInfo *conn, bool *check_flag) +{ + int nContinuousFail; + int previousCodes[FDFS_MULTI_IP_MAX_COUNT]; + int conn_results[FDFS_MULTI_IP_MAX_COUNT]; + int result; + int i; + FDFSMultiIP ip_addrs; + + if (g_use_storage_id) + { + FDFSStorageIdInfo *idInfo; + idInfo = fdfs_get_storage_by_id(pStorage->id); + if (idInfo == NULL) + { + logError("file: "__FILE__", line: %d, " + "storage server id: %s not exist " + "in storage_ids.conf, storage ip: %s", + __LINE__, pStorage->id, pStorage->ip_addr); + sleep(5); + return; + } + ip_addrs = idInfo->ip_addrs; + } + else + { + ip_addrs.count = 1; + ip_addrs.index = 0; + strcpy(ip_addrs.ips[0], pStorage->ip_addr); + } + + nContinuousFail = 0; + memset(previousCodes, 0, sizeof(previousCodes)); + memset(conn_results, 0, sizeof(conn_results)); + while (g_continue_flag && *check_flag && + pStorage->status != FDFS_STORAGE_STATUS_DELETED && + pStorage->status != FDFS_STORAGE_STATUS_IP_CHANGED && + pStorage->status != FDFS_STORAGE_STATUS_NONE) + { + for (i=0; iip_addr, ip_addrs.ips[i]); + conn->sock = socketCreateExAuto(conn->ip_addr, + g_fdfs_connect_timeout, O_NONBLOCK, + g_client_bind_addr ? g_bind_addr : NULL, &result); + if (conn->sock < 0) + { + logCrit("file: "__FILE__", line: %d, " + "socket create fail, program exit!", __LINE__); + g_continue_flag = false; + break; + } + + if ((conn_results[i]=connectserverbyip_nb(conn->sock, + conn->ip_addr, g_server_port, + g_fdfs_connect_timeout)) == 0) + { + char szFailPrompt[64]; + if (nContinuousFail == 0) + { + *szFailPrompt = '\0'; + } + else + { + sprintf(szFailPrompt, + ", continuous fail count: %d", + nContinuousFail); + } + logInfo("file: "__FILE__", line: %d, " + "successfully connect to " + "storage server %s:%d%s", __LINE__, + conn->ip_addr, g_server_port, szFailPrompt); + nContinuousFail = 0; + break; + } + + nContinuousFail++; + if (previousCodes[i] != conn_results[i]) + { + logError("file: "__FILE__", line: %d, " + "connect to storage server %s:%d fail, " + "errno: %d, error info: %s", + __LINE__, conn->ip_addr, g_server_port, + conn_results[i], STRERROR(conn_results[i])); + previousCodes[i] = conn_results[i]; + } + + close(conn->sock); + conn->sock = -1; + } + + if (!g_continue_flag) + { + break; + } + + sleep(1); + } + + if (nContinuousFail > 0) + { + int avg_fails; + avg_fails = (nContinuousFail + ip_addrs.count - 1) / ip_addrs.count; + for (i=0; iip_addr, conn->port, - tracker_client_ip, g_tracker_client_ip); + result = storage_insert_ip_addr_to_multi_ips(&g_tracker_client_ip, + tracker_client_ip, ips_limit); + if (!(result == 0 || result == EEXIST)) + { + fdfs_multi_ips_to_string(&g_tracker_client_ip, + ip_str, sizeof(ip_str)); + logCrit("file: "__FILE__", line: %d, " + "as a client of tracker server %s:%d, " + "my ip: %s not consistent with client ips: %s " + "of other tracker client. program exit!", __LINE__, + conn->ip_addr, conn->port, + tracker_client_ip, ip_str); - close(conn->sock); - conn->sock = -1; - break; - } + g_continue_flag = false; + break; + } - insert_into_local_host_ip(tracker_client_ip); + if (result == 0) + { + if (fdfs_check_and_format_ips(&g_tracker_client_ip, + szFailPrompt, sizeof(szFailPrompt)) != 0) + { + logCrit("file: "__FILE__", line: %d, " + "as a client of tracker server %s:%d, " + "my ip: %s not valid, error info: %s. " + "program exit!", __LINE__, + conn->ip_addr, conn->port, + tracker_client_ip, szFailPrompt); + + g_continue_flag = false; + break; + } + + insert_into_local_host_ip(tracker_client_ip); + } /* //printf("file: "__FILE__", line: %d, " \ @@ -421,7 +440,7 @@ static void *tracker_report_thread_entrance(void *arg) int my_status; if (tracker_get_storage_max_status( \ &g_tracker_group, g_group_name, \ - g_tracker_client_ip, my_server_id, \ + tracker_client_ip, my_server_id, \ &my_status) == 0) { tracker_sync_dest_query(conn); @@ -544,6 +563,10 @@ static void *tracker_report_thread_entrance(void *arg) pTrackerServer->connections[0].port, nContinuousFail, result, STRERROR(result)); } + else if (conn != NULL) + { + conn_pool_disconnect_server(conn); + } thracker_report_thread_exit(pTrackerServer); diff --git a/storage/trunk_mgr/trunk_sync.c b/storage/trunk_mgr/trunk_sync.c index a887d6b..e359ac1 100644 --- a/storage/trunk_mgr/trunk_sync.c +++ b/storage/trunk_mgr/trunk_sync.c @@ -35,6 +35,7 @@ #include "storage_ip_changed_dealer.h" #include "tracker_client_thread.h" #include "storage_client.h" +#include "storage_sync_func.h" #include "trunk_sync.h" #define TRUNK_SYNC_BINLOG_FILENAME "binlog" @@ -1405,10 +1406,7 @@ static void* trunk_sync_thread_entrance(void* arg) char local_ip_addr[IP_ADDRESS_SIZE]; int read_result; int sync_result; - int conn_result; int result; - int previousCode; - int nContinuousFail; time_t current_time; time_t last_keep_alive_time; @@ -1435,82 +1433,8 @@ static void* trunk_sync_thread_entrance(void* arg) pStorage->status != FDFS_STORAGE_STATUS_IP_CHANGED && \ pStorage->status != FDFS_STORAGE_STATUS_NONE) { - previousCode = 0; - nContinuousFail = 0; - conn_result = 0; - while (g_continue_flag && g_if_trunker_self && \ - pStorage->status != FDFS_STORAGE_STATUS_DELETED && \ - pStorage->status != FDFS_STORAGE_STATUS_IP_CHANGED && \ - pStorage->status != FDFS_STORAGE_STATUS_NONE) - { - strcpy(storage_server.ip_addr, pStorage->ip_addr); - storage_server.sock = socketCreateExAuto(pStorage->ip_addr, - g_fdfs_connect_timeout, O_NONBLOCK, - g_client_bind_addr ? g_bind_addr : NULL, &result); - if (storage_server.sock < 0) - { - logCrit("file: "__FILE__", line: %d, " - "socket create fail, program exit!", __LINE__); - g_continue_flag = false; - break; - } - - if ((conn_result=connectserverbyip_nb(storage_server.sock, - pStorage->ip_addr, g_server_port, - g_fdfs_connect_timeout)) == 0) - { - char szFailPrompt[64]; - if (nContinuousFail == 0) - { - *szFailPrompt = '\0'; - } - else - { - sprintf(szFailPrompt, \ - ", continuous fail count: %d", \ - nContinuousFail); - } - logInfo("file: "__FILE__", line: %d, " \ - "successfully connect to " \ - "storage server %s:%d%s", __LINE__, \ - pStorage->ip_addr, g_server_port, \ - szFailPrompt); - nContinuousFail = 0; - break; - } - - if (previousCode != conn_result) - { - logError("file: "__FILE__", line: %d, " \ - "connect to storage server %s:%d fail" \ - ", errno: %d, error info: %s", \ - __LINE__, \ - pStorage->ip_addr, g_server_port, \ - conn_result, STRERROR(conn_result)); - previousCode = conn_result; - } - - nContinuousFail++; - close(storage_server.sock); - storage_server.sock = -1; - - if (!g_continue_flag) - { - break; - } - - sleep(1); - } - - if (nContinuousFail > 0) - { - logError("file: "__FILE__", line: %d, " \ - "connect to storage server %s:%d fail, " \ - "try count: %d, errno: %d, error info: %s", \ - __LINE__, pStorage->ip_addr, \ - g_server_port, nContinuousFail, \ - conn_result, STRERROR(conn_result)); - } + storage_sync_connect_storage_server_ex(pStorage, + &storage_server, &g_if_trunker_self); if ((!g_continue_flag) || (!g_if_trunker_self) || \ pStorage->status == FDFS_STORAGE_STATUS_DELETED || \ diff --git a/tracker/fdfs_server_id_func.c b/tracker/fdfs_server_id_func.c index 4f53114..10f2111 100644 --- a/tracker/fdfs_server_id_func.c +++ b/tracker/fdfs_server_id_func.c @@ -194,7 +194,7 @@ static int fdfs_check_id_duplicated() FDFSStorageIdInfo *idEnd; FDFSStorageIdInfo *previous; - current=g_storage_ids_by_id.ids + 0; + previous = g_storage_ids_by_id.ids + 0; idEnd = g_storage_ids_by_id.ids + g_storage_ids_by_id.count; for (current=g_storage_ids_by_id.ids + 1; currentip_addr, conn->port, (int)in_bytes); + "is invalid", __LINE__, conn->ip_addr, + conn->port, (int)in_bytes); result = EINVAL; break; } @@ -649,11 +649,10 @@ int fdfs_get_storage_ids_from_tracker_server(TrackerServerInfo *pTrackerServer) if (list_count == MAX_REQUEST_LOOP) { - logError("file: "__FILE__", line: %d, " \ - "response data from tracker " \ - "server %s:%d is too large", \ - __LINE__, conn->ip_addr,\ - conn->port); + logError("file: "__FILE__", line: %d, " + "response data from tracker " + "server %s:%d is too large", + __LINE__, conn->ip_addr, conn->port); result = ENOSPC; break; } @@ -694,7 +693,7 @@ int fdfs_get_storage_ids_from_tracker_server(TrackerServerInfo *pTrackerServer) } *p = '\0'; - //logInfo("list_count: %d, storage ids:\n%s", list_count, content); + logInfo("list_count: %d, storage ids:\n%s", list_count, content); result = fdfs_load_storage_ids(content, \ "storage-ids-from-tracker"); diff --git a/tracker/tracker_mem.c b/tracker/tracker_mem.c index e366b07..2120bb2 100644 --- a/tracker/tracker_mem.c +++ b/tracker/tracker_mem.c @@ -1807,7 +1807,6 @@ int tracker_save_storages() char trueFilename[MAX_PATH_SIZE]; char buff[4096]; char id_buff[128]; - char ip_buff[256]; int fd; int len; FDFSGroupInfo **ppGroup; @@ -1858,9 +1857,6 @@ int tracker_save_storages() STORAGE_ITEM_SERVER_ID, pStorage->id); } - fdfs_multi_ips_to_string(&pStorage->ip_addrs, - ip_buff, sizeof(ip_buff)); - count++; len = sprintf(buff, \ "# storage %s:%d\n" \ @@ -1918,11 +1914,12 @@ int tracker_save_storages() "\t%s=%d\n" \ "\t%s=%d\n" \ "\t%s=%"PRId64"\n\n", \ - ip_buff, pStorage->storage_port, \ + FDFS_CURRENT_IP_ADDR(pStorage), \ + pStorage->storage_port, \ STORAGE_SECTION_NAME_PREFIX, count, id_buff, \ STORAGE_ITEM_GROUP_NAME, \ (*ppGroup)->group_name, \ - STORAGE_ITEM_IP_ADDR, ip_buff, \ + STORAGE_ITEM_IP_ADDR, FDFS_CURRENT_IP_ADDR(pStorage), \ STORAGE_ITEM_STATUS, pStorage->status, \ STORAGE_ITEM_VERSION, pStorage->version, \ STORAGE_ITEM_JOIN_TIME, \ @@ -3587,8 +3584,6 @@ static int tracker_mem_add_storage_from_file(FDFSGroups *pGroups, const char *data_path, TrackerClientInfo *pClientInfo, const char *group_name, const char *storage_id, char *ip_addr) { - FDFSMultiIP multi_ip; - char error_info[256]; int result; bool bInserted; @@ -3640,19 +3635,8 @@ static int tracker_mem_add_storage_from_file(FDFSGroups *pGroups, return errno != 0 ? errno : ENOENT; } - if ((result=fdfs_parse_multi_ips_ex(ip_addr, &multi_ip, - error_info, sizeof(error_info), false)) != 0) - { - logError("file: "__FILE__", line: %d, " - "in the file \"%s/%s\", invalid ip address, " - "group: %s, error info: %s", __LINE__, data_path, - STORAGE_SERVERS_LIST_FILENAME_NEW, - group_name, error_info); - return result; - } - if ((result=tracker_mem_add_storage(pClientInfo, storage_id, - multi_ip.ips[0], false, false, &bInserted)) != 0) + ip_addr, false, false, &bInserted)) != 0) { return result; } @@ -4772,7 +4756,6 @@ int tracker_mem_sync_storages(FDFSGroupInfo *pGroup, \ FDFSStorageDetail target_storage; FDFSStorageDetail *pTargetStorage; FDFSStorageDetail **ppFound; - char ip_str[256]; if ((result=pthread_mutex_lock(&mem_thread_lock)) != 0) { @@ -4820,12 +4803,11 @@ int tracker_mem_sync_storages(FDFSGroupInfo *pGroup, \ continue; } - fdfs_multi_ips_to_string(&(*ppFound)->ip_addrs, - ip_str, sizeof(ip_str)); logWarning("file: "__FILE__", line: %d, " "storage server: %s:%d, dest status: %d, " "my status: %d, should change my status!", - __LINE__, ip_str, (*ppFound)->storage_port, + __LINE__, FDFS_CURRENT_IP_ADDR(*ppFound), + (*ppFound)->storage_port, pServer->status, (*ppFound)->status); if (pServer->status == \ @@ -5001,7 +4983,6 @@ static int tracker_write_to_trunk_change_log(FDFSGroupInfo *pGroup, \ struct tm tm; time_t current_time; FDFSStorageDetail *pLastTrunk; - char ip_str[256]; tracker_mem_file_lock(); @@ -5038,10 +5019,8 @@ static int tracker_write_to_trunk_change_log(FDFSGroupInfo *pGroup, \ } else { - fdfs_multi_ips_to_string(&pLastTrunk->ip_addrs, - ip_str, sizeof(ip_str)); len += sprintf(buff + len, " %s/%s => ", - pLastTrunk->id, ip_str); + pLastTrunk->id, FDFS_CURRENT_IP_ADDR(pLastTrunk)); } if (pGroup->pTrunkServer == NULL) @@ -5050,10 +5029,9 @@ static int tracker_write_to_trunk_change_log(FDFSGroupInfo *pGroup, \ } else { - fdfs_multi_ips_to_string(&pGroup->pTrunkServer->ip_addrs, - ip_str, sizeof(ip_str)); len += sprintf(buff + len, " %s/%s\n", - pGroup->pTrunkServer->id, ip_str); + pGroup->pTrunkServer->id, + FDFS_CURRENT_IP_ADDR(pGroup->pTrunkServer)); } } else @@ -5066,9 +5044,8 @@ static int tracker_write_to_trunk_change_log(FDFSGroupInfo *pGroup, \ } else { - fdfs_multi_ips_to_string(&pLastTrunk->ip_addrs, - ip_str, sizeof(ip_str)); - len += sprintf(buff + len, " %s => ", ip_str); + len += sprintf(buff + len, " %s => ", + FDFS_CURRENT_IP_ADDR(pLastTrunk)); } if (pGroup->pTrunkServer == NULL) @@ -5077,9 +5054,8 @@ static int tracker_write_to_trunk_change_log(FDFSGroupInfo *pGroup, \ } else { - fdfs_multi_ips_to_string(&pGroup->pTrunkServer->ip_addrs, - ip_str, sizeof(ip_str)); - len += sprintf(buff + len, " %s\n", ip_str); + len += sprintf(buff + len, " %s\n", + FDFS_CURRENT_IP_ADDR(pGroup->pTrunkServer)); } } @@ -5130,7 +5106,6 @@ static int tracker_mem_do_set_trunk_server(FDFSGroupInfo *pGroup, FDFSStorageDetail *pTrunkServer, const bool save) { int result; - char ip_str[256]; if (*(pGroup->last_trunk_server_id) != '\0' && strcmp(pTrunkServer->id, pGroup->last_trunk_server_id) != 0) @@ -5151,12 +5126,11 @@ static int tracker_mem_do_set_trunk_server(FDFSGroupInfo *pGroup, pGroup->trunk_chg_count++; g_trunk_server_chg_count++; - fdfs_multi_ips_to_string(&pGroup->pTrunkServer->ip_addrs, - ip_str, sizeof(ip_str)); logInfo("file: "__FILE__", line: %d, " "group: %s, trunk server set to %s(%s:%d)", __LINE__, pGroup->group_name, pGroup->pTrunkServer->id, - ip_str, pGroup->storage_port); + FDFS_CURRENT_IP_ADDR(pGroup->pTrunkServer), + pGroup->storage_port); if (save) { return tracker_save_groups(); diff --git a/tracker/tracker_proto.c b/tracker/tracker_proto.c index 455aa60..a015cc2 100644 --- a/tracker/tracker_proto.c +++ b/tracker/tracker_proto.c @@ -44,9 +44,9 @@ int fdfs_recv_header(ConnectionInfo *pTrackerServer, int64_t *in_bytes) if (resp.status != 0) { - logError("file: "__FILE__", line: %d, " \ - "server: %s:%d, response status %d != 0", \ - __LINE__, pTrackerServer->ip_addr, \ + logError("file: "__FILE__", line: %d, " + "server: %s:%d, response status %d != 0", + __LINE__, pTrackerServer->ip_addr, pTrackerServer->port, resp.status); *in_bytes = 0;