int conn_pool_connect_server(ConnectionInfo *pConnection, \ const int connect_timeout) { int result; if (pConnection->sock >= 0) { close(pConnection->sock); } pConnection->sock = socket(AF_INET, SOCK_STREAM, 0); if(pConnection->sock < 0) { logError("file: "__FILE__", line: %d, " \ "socket create failed, errno: %d, " \ "error info: %s", __LINE__, errno, STRERROR(errno)); return errno != 0 ? errno : EPERM; } if ((result=tcpsetnonblockopt(pConnection->sock)) != 0) { close(pConnection->sock); pConnection->sock = -1; return result; } if ((result=connectserverbyip_nb(pConnection->sock, \ pConnection->ip_addr, pConnection->port, \ connect_timeout)) != 0) { logError("file: "__FILE__", line: %d, " \ "connect to %s:%d fail, errno: %d, " \ "error info: %s", __LINE__, pConnection->ip_addr, \ pConnection->port, result, STRERROR(result)); close(pConnection->sock); pConnection->sock = -1; return result; } return 0; }
int fdfs_get_ini_context_from_tracker(TrackerServerGroup *pTrackerGroup, \ IniContext *iniContext, bool * volatile continue_flag, \ const bool client_bind_addr, const char *bind_addr) { ConnectionInfo *pGlobalServer; ConnectionInfo *pTServer; ConnectionInfo *pServerStart; ConnectionInfo *pServerEnd; ConnectionInfo trackerServer; char in_buff[1024]; int result; int leader_index; int i; result = 0; pTServer = &trackerServer; pServerEnd = pTrackerGroup->servers + pTrackerGroup->server_count; leader_index = pTrackerGroup->leader_index; if (leader_index >= 0) { pServerStart = pTrackerGroup->servers + leader_index; } else { pServerStart = pTrackerGroup->servers; } do { for (pGlobalServer=pServerStart; pGlobalServer<pServerEnd; \ pGlobalServer++) { memcpy(pTServer, pGlobalServer, sizeof(ConnectionInfo)); for (i=0; i < 3; i++) { pTServer->sock = socket(AF_INET, SOCK_STREAM, 0); if(pTServer->sock < 0) { result = errno != 0 ? errno : EPERM; logError("file: "__FILE__", line: %d, " \ "socket create failed, errno: %d, " \ "error info: %s.", \ __LINE__, result, STRERROR(result)); sleep(5); break; } if (client_bind_addr && (bind_addr != NULL && \ *bind_addr != '\0')) { socketBind(pTServer->sock, bind_addr, 0); } if (tcpsetnonblockopt(pTServer->sock) != 0) { close(pTServer->sock); pTServer->sock = -1; sleep(1); continue; } if ((result=connectserverbyip_nb(pTServer->sock, \ pTServer->ip_addr, pTServer->port, \ g_fdfs_connect_timeout)) == 0) { break; } close(pTServer->sock); pTServer->sock = -1; sleep(1); } if (pTServer->sock < 0) { logError("file: "__FILE__", line: %d, " \ "connect to tracker server %s:%d fail, " \ "errno: %d, error info: %s", \ __LINE__, pTServer->ip_addr, pTServer->port, \ result, STRERROR(result)); continue; } result = fdfs_do_parameter_req(pTServer, in_buff, \ sizeof(in_buff)); if (result == 0) { result = iniLoadFromBuffer(in_buff, iniContext); close(pTServer->sock); return result; } fdfs_quit(pTServer); close(pTServer->sock); sleep(1); } if (pServerStart != pTrackerGroup->servers) { pServerStart = pTrackerGroup->servers; } } while (*continue_flag); return EINTR; }
static void* trunk_sync_thread_entrance(void* arg) { FDFSStorageBrief *pStorage; TrunkBinLogReader reader; ConnectionInfo storage_server; 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; memset(local_ip_addr, 0, sizeof(local_ip_addr)); memset(&reader, 0, sizeof(reader)); reader.mark_fd = -1; reader.binlog_fd = -1; current_time = g_current_time; last_keep_alive_time = 0; pStorage = (FDFSStorageBrief *)arg; strcpy(storage_server.ip_addr, pStorage->ip_addr); storage_server.port = g_server_port; storage_server.sock = -1; logInfo("file: "__FILE__", line: %d, " \ "trunk sync thread to storage server %s:%d started", \ __LINE__, storage_server.ip_addr, storage_server.port); 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) { 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 = \ socket(AF_INET, SOCK_STREAM, 0); if(storage_server.sock < 0) { logCrit("file: "__FILE__", line: %d," \ " socket create fail, " \ "errno: %d, error info: %s. " \ "program exit!", __LINE__, \ errno, STRERROR(errno)); g_continue_flag = false; break; } if (g_client_bind_addr && *g_bind_addr != '\0') { socketBind(storage_server.sock, g_bind_addr, 0); } if (tcpsetnonblockopt(storage_server.sock) != 0) { nContinuousFail++; close(storage_server.sock); storage_server.sock = -1; sleep(1); continue; } 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)); } if ((!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) { logError("file: "__FILE__", line: %d, break loop." \ "g_continue_flag: %d, g_if_trunker_self: %d, " \ "dest storage status: %d", __LINE__, \ g_continue_flag, g_if_trunker_self, \ pStorage->status); break; } if ((result=trunk_reader_init(pStorage, &reader)) != 0) { logCrit("file: "__FILE__", line: %d, " \ "trunk_reader_init fail, errno=%d, " \ "program exit!", \ __LINE__, result); g_continue_flag = false; break; } getSockIpaddr(storage_server.sock, \ local_ip_addr, IP_ADDRESS_SIZE); insert_into_local_host_ip(local_ip_addr); /* //printf("file: "__FILE__", line: %d, " \ "storage_server.ip_addr=%s, " \ "local_ip_addr: %s\n", \ __LINE__, pStorage->ip_addr, local_ip_addr); */ if (is_local_host_ip(pStorage->ip_addr)) { //can't self sync to self logError("file: "__FILE__", line: %d, " \ "ip_addr %s belong to the local host," \ " trunk sync thread exit.", \ __LINE__, pStorage->ip_addr); fdfs_quit(&storage_server); close(storage_server.sock); break; } if (reader.binlog_offset == 0) { if ((result=fdfs_deal_no_body_cmd(&storage_server, \ STORAGE_PROTO_CMD_TRUNK_TRUNCATE_BINLOG_FILE)) != 0) { logError("file: "__FILE__", line: %d, " "fdfs_deal_no_body_cmd fail, result: %d", __LINE__, result); close(storage_server.sock); trunk_reader_destroy(&reader); sleep(5); continue; } } sync_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) { read_result = trunk_binlog_preread(&reader); if (read_result == ENOENT) { if (reader.last_binlog_offset != \ reader.binlog_offset) { if (trunk_write_to_mark_file(&reader)!=0) { logCrit("file: "__FILE__", line: %d, " \ "trunk_write_to_mark_file fail, " \ "program exit!", __LINE__); g_continue_flag = false; break; } } current_time = g_current_time; if (current_time - last_keep_alive_time >= \ g_heart_beat_interval) { if (fdfs_active_test(&storage_server)!=0) { break; } last_keep_alive_time = current_time; } if (!g_if_trunker_self) { break; } usleep(g_sync_wait_usec); continue; } if (read_result != 0) { sleep(5); continue; } if ((sync_result=trunk_sync_data(&reader, \ &storage_server)) != 0) { break; } if (g_sync_interval > 0) { usleep(g_sync_interval); } } if (reader.last_binlog_offset != reader.binlog_offset) { if (trunk_write_to_mark_file(&reader) != 0) { logCrit("file: "__FILE__", line: %d, " \ "trunk_write_to_mark_file fail, " \ "program exit!", __LINE__); g_continue_flag = false; break; } } close(storage_server.sock); storage_server.sock = -1; trunk_reader_destroy(&reader); if (!g_continue_flag) { break; } if (!(sync_result == ENOTCONN || sync_result == EIO)) { sleep(1); } } if (storage_server.sock >= 0) { close(storage_server.sock); } trunk_reader_destroy(&reader); trunk_sync_thread_exit(&storage_server); return NULL; }