void PeerAdmin::run() { try { while (m_enabled) { if (m_selector.select() && m_enabled) { auto list = m_selector.selectedKeys(); for(auto it = list.begin(); it != list.end(); it++) { SelectionKeyPtr key = *it; if (!key->isValid()) { continue; } if (key->channel().instanceof<ServerSocketChannel>()) { processServer(key->channel(), key->attachment()); } else if (key->channel().instanceof<SocketChannel>()) { processPeer(key->attachment()); } } } } } catch (ParserException &e) { if (m_enabled) { logFine() << __demangle(typeid(*this).name()) << " leaving main loop normally"; } else { logCrit() << __demangle(typeid(*this).name()) << " leaving main loop after throwing an instance of '" << __demangle(typeid(e).name()) << "'"; if (!e.message().isEmpty()) logCrit() << " what(): " << e.message(); } } catch (Exception &e) { logCrit() << __demangle(typeid(*this).name()) << " leaving main loop after throwing an instance of '" << __demangle(typeid(e).name()) << "'"; if (!e.message().isEmpty()) logCrit() << " what(): " << e.message(); } cancel(); }
static void usr1_handler (int signum) { int rv; logInfo("Re-reading config file"); rv = equus_load_config(); if (rv < 0) { logCrit("Failed loading config file, exiting"); exit(EXIT_FAILURE); } equus_restart_set(1); }
static void sigQuitHandler(int sig) { if (!bTerminateFlag) { set_timer(1, 1, sigAlarmHandler); bTerminateFlag = true; g_continue_flag = false; logCrit("file: "__FILE__", line: %d, " \ "catch signal %d, program exiting...", \ __LINE__, sig); } }
int tracker_http_check_start() { int result; if (g_http_check_interval <= 0) { return 0; } if ((result=pthread_create(&http_check_tid, NULL, \ http_check_entrance, NULL)) != 0) { logCrit("file: "__FILE__", line: %d, " \ "create thread failed, errno: %d, error info: %s", \ __LINE__, result, STRERROR(result)); return result; } return 0; }
virgo_error_t* virgo__log_rotate(virgo_t *v) { FILE *old = v->log_fp; FILE *nxt = stderr; if (v->log_path != NULL) { nxt = fopen(v->log_path, "ab"); if (nxt == NULL) { char buf[256]; int err = errno; #ifdef _WIN32 strncpy(&buf[0], strerror(err), sizeof(buf)); #else strerror_r(err, &buf[0], sizeof(buf)); #endif logCrit(v, "Failed to open log file: %s (errno=%d,%s)", v->log_path, err, &buf[0]); return virgo_error_createf(VIRGO_EIO, "Failed to open log file: %s (errno=%d,%s)", v->log_path, err, &buf[0]); } } v->log_fp = nxt; if (old != NULL && old != stderr) { fclose(old); } if (v->log_path) { virgo_log_infof(v, "Log file started (path=%s)", v->log_path); } return VIRGO_SUCCESS; }
int storage_func_init(const char *filename, \ char *bind_addr, const int addr_size) { char *pBindAddr; char *pGroupName; char *pRunByGroup; char *pRunByUser; char *pFsyncAfterWrittenBytes; char *pThreadStackSize; char *pBuffSize; char *pIfAliasPrefix; char *pHttpDomain; IniContext iniContext; int result; int64_t fsync_after_written_bytes; int64_t thread_stack_size; int64_t buff_size; TrackerServerInfo *pServer; TrackerServerInfo *pEnd; /* while (nThreadCount > 0) { sleep(1); } */ if ((result=iniLoadFromFile(filename, &iniContext)) != 0) { logError("file: "__FILE__", line: %d, " \ "load conf file \"%s\" fail, ret code: %d", \ __LINE__, filename, result); return result; } do { if (iniGetBoolValue(NULL, "disabled", &iniContext, false)) { logError("file: "__FILE__", line: %d, " \ "conf file \"%s\" disabled=true, exit", \ __LINE__, filename); result = ECANCELED; break; } g_subdir_count_per_path=iniGetIntValue(NULL, \ "subdir_count_per_path", &iniContext, \ DEFAULT_DATA_DIR_COUNT_PER_PATH); if (g_subdir_count_per_path <= 0 || \ g_subdir_count_per_path > 256) { logError("file: "__FILE__", line: %d, " \ "conf file \"%s\", invalid subdir_count: %d", \ __LINE__, filename, g_subdir_count_per_path); result = EINVAL; break; } if ((result=storage_load_paths(&iniContext)) != 0) { break; } load_log_level(&iniContext); if ((result=log_set_prefix(g_fdfs_base_path, \ STORAGE_ERROR_LOG_FILENAME)) != 0) { break; } g_fdfs_connect_timeout = iniGetIntValue(NULL, "connect_timeout", \ &iniContext, DEFAULT_CONNECT_TIMEOUT); if (g_fdfs_connect_timeout <= 0) { g_fdfs_connect_timeout = DEFAULT_CONNECT_TIMEOUT; } g_fdfs_network_timeout = iniGetIntValue(NULL, "network_timeout", \ &iniContext, DEFAULT_NETWORK_TIMEOUT); if (g_fdfs_network_timeout <= 0) { g_fdfs_network_timeout = DEFAULT_NETWORK_TIMEOUT; } g_network_tv.tv_sec = g_fdfs_network_timeout; g_server_port = iniGetIntValue(NULL, "port", &iniContext, \ FDFS_STORAGE_SERVER_DEF_PORT); if (g_server_port <= 0) { g_server_port = FDFS_STORAGE_SERVER_DEF_PORT; } g_heart_beat_interval = iniGetIntValue(NULL, \ "heart_beat_interval", &iniContext, \ STORAGE_BEAT_DEF_INTERVAL); if (g_heart_beat_interval <= 0) { g_heart_beat_interval = STORAGE_BEAT_DEF_INTERVAL; } g_stat_report_interval = iniGetIntValue(NULL, \ "stat_report_interval", &iniContext, \ STORAGE_REPORT_DEF_INTERVAL); if (g_stat_report_interval <= 0) { g_stat_report_interval = STORAGE_REPORT_DEF_INTERVAL; } pBindAddr = iniGetStrValue(NULL, "bind_addr", &iniContext); if (pBindAddr == NULL) { *bind_addr = '\0'; } else { snprintf(bind_addr, addr_size, "%s", pBindAddr); } g_client_bind_addr = iniGetBoolValue(NULL, "client_bind", \ &iniContext, true); pGroupName = iniGetStrValue(NULL, "group_name", &iniContext); if (pGroupName == NULL) { logError("file: "__FILE__", line: %d, " \ "conf file \"%s\" must have item " \ "\"group_name\"!", \ __LINE__, filename); result = ENOENT; break; } if (pGroupName[0] == '\0') { logError("file: "__FILE__", line: %d, " \ "conf file \"%s\", " \ "group_name is empty!", \ __LINE__, filename); result = EINVAL; break; } snprintf(g_group_name, sizeof(g_group_name), "%s", pGroupName); if ((result=fdfs_validate_group_name(g_group_name)) != 0) \ { logError("file: "__FILE__", line: %d, " \ "conf file \"%s\", " \ "the group name \"%s\" is invalid!", \ __LINE__, filename, g_group_name); result = EINVAL; break; } result = fdfs_load_tracker_group_ex(&g_tracker_group, \ filename, &iniContext); if (result != 0) { break; } pEnd = g_tracker_group.servers + g_tracker_group.server_count; for (pServer=g_tracker_group.servers; pServer<pEnd; pServer++) { //printf("server=%s:%d\n", pServer->ip_addr, pServer->port); if (strcmp(pServer->ip_addr, "127.0.0.1") == 0) { logError("file: "__FILE__", line: %d, " \ "conf file \"%s\", " \ "tracker: \"%s:%d\" is invalid, " \ "tracker server ip can't be 127.0.0.1",\ __LINE__, filename, pServer->ip_addr, \ pServer->port); result = EINVAL; break; } } if (result != 0) { break; } g_sync_wait_usec = iniGetIntValue(NULL, "sync_wait_msec",\ &iniContext, STORAGE_DEF_SYNC_WAIT_MSEC); if (g_sync_wait_usec <= 0) { g_sync_wait_usec = STORAGE_DEF_SYNC_WAIT_MSEC; } g_sync_wait_usec *= 1000; g_sync_interval = iniGetIntValue(NULL, "sync_interval",\ &iniContext, 0); if (g_sync_interval < 0) { g_sync_interval = 0; } g_sync_interval *= 1000; if ((result=get_time_item_from_conf(&iniContext, \ "sync_start_time", &g_sync_start_time, 0, 0)) != 0) { break; } if ((result=get_time_item_from_conf(&iniContext, \ "sync_end_time", &g_sync_end_time, 23, 59)) != 0) { break; } g_sync_part_time = !((g_sync_start_time.hour == 0 && \ g_sync_start_time.minute == 0) && \ (g_sync_end_time.hour == 23 && \ g_sync_end_time.minute == 59)); g_max_connections = iniGetIntValue(NULL, "max_connections", \ &iniContext, DEFAULT_MAX_CONNECTONS); if (g_max_connections <= 0) { g_max_connections = DEFAULT_MAX_CONNECTONS; } if ((result=set_rlimit(RLIMIT_NOFILE, g_max_connections)) != 0) { break; } g_work_threads = iniGetIntValue(NULL, "work_threads", \ &iniContext, DEFAULT_WORK_THREADS); if (g_work_threads <= 0) { logError("file: "__FILE__", line: %d, " \ "item \"work_threads\" is invalid, " \ "value: %d <= 0!", __LINE__, g_work_threads); result = EINVAL; break; } pBuffSize = iniGetStrValue(NULL, \ "buff_size", &iniContext); if (pBuffSize == NULL) { buff_size = STORAGE_DEFAULT_BUFF_SIZE; } else if ((result=parse_bytes(pBuffSize, 1, &buff_size)) != 0) { return result; } g_buff_size = buff_size; if (g_buff_size < 4 * 1024 || \ g_buff_size < sizeof(TrackerHeader) + \ TRUNK_BINLOG_BUFFER_SIZE) { logError("file: "__FILE__", line: %d, " \ "item \"buff_size\" is too small, " \ "value: %d < %d or < %d!", __LINE__, \ g_buff_size, 4 * 1024, \ (int)sizeof(TrackerHeader) + \ TRUNK_BINLOG_BUFFER_SIZE); result = EINVAL; break; } g_disk_rw_separated = iniGetBoolValue(NULL, \ "disk_rw_separated", &iniContext, true); g_disk_reader_threads = iniGetIntValue(NULL, \ "disk_reader_threads", \ &iniContext, DEFAULT_DISK_READER_THREADS); if (g_disk_reader_threads < 0) { logError("file: "__FILE__", line: %d, " \ "item \"disk_reader_threads\" is invalid, " \ "value: %d < 0!", __LINE__, \ g_disk_reader_threads); result = EINVAL; break; } g_disk_writer_threads = iniGetIntValue(NULL, \ "disk_writer_threads", \ &iniContext, DEFAULT_DISK_WRITER_THREADS); if (g_disk_writer_threads < 0) { logError("file: "__FILE__", line: %d, " \ "item \"disk_writer_threads\" is invalid, " \ "value: %d < 0!", __LINE__, \ g_disk_writer_threads); result = EINVAL; break; } if (g_disk_rw_separated) { if (g_disk_reader_threads == 0) { logError("file: "__FILE__", line: %d, " \ "item \"disk_reader_threads\" is " \ "invalid, value = 0!", __LINE__); result = EINVAL; break; } if (g_disk_writer_threads == 0) { logError("file: "__FILE__", line: %d, " \ "item \"disk_writer_threads\" is " \ "invalid, value = 0!", __LINE__); result = EINVAL; break; } } else if (g_disk_reader_threads + g_disk_writer_threads == 0) { logError("file: "__FILE__", line: %d, " \ "item \"disk_reader_threads\" and " \ "\"disk_writer_threads\" are " \ "invalid, both value = 0!", __LINE__); result = EINVAL; break; } /* g_disk_rw_direct = iniGetBoolValue(NULL, \ "disk_rw_direct", &iniContext, false); */ pRunByGroup = iniGetStrValue(NULL, "run_by_group", &iniContext); pRunByUser = iniGetStrValue(NULL, "run_by_user", &iniContext); if (pRunByGroup == NULL) { *g_run_by_group = '\0'; } else { snprintf(g_run_by_group, sizeof(g_run_by_group), \ "%s", pRunByGroup); } if (*g_run_by_group == '\0') { g_run_by_gid = getegid(); } else { struct group *pGroup; pGroup = getgrnam(g_run_by_group); if (pGroup == NULL) { result = errno != 0 ? errno : ENOENT; logError("file: "__FILE__", line: %d, " \ "getgrnam fail, errno: %d, " \ "error info: %s", __LINE__, \ result, STRERROR(result)); return result; } g_run_by_gid = pGroup->gr_gid; } if (pRunByUser == NULL) { *g_run_by_user = '******'; } else { snprintf(g_run_by_user, sizeof(g_run_by_user), \ "%s", pRunByUser); } if (*g_run_by_user == '\0') { g_run_by_uid = geteuid(); } else { struct passwd *pUser; pUser = getpwnam(g_run_by_user); if (pUser == NULL) { result = errno != 0 ? errno : ENOENT; logError("file: "__FILE__", line: %d, " \ "getpwnam fail, errno: %d, " \ "error info: %s", __LINE__, \ result, STRERROR(result)); return result; } g_run_by_uid = pUser->pw_uid; } if ((result=load_allow_hosts(&iniContext, \ &g_allow_ip_addrs, &g_allow_ip_count)) != 0) { return result; } g_file_distribute_path_mode = iniGetIntValue(NULL, \ "file_distribute_path_mode", &iniContext, \ FDFS_FILE_DIST_PATH_ROUND_ROBIN); g_file_distribute_rotate_count = iniGetIntValue(NULL, \ "file_distribute_rotate_count", &iniContext, \ FDFS_FILE_DIST_DEFAULT_ROTATE_COUNT); if (g_file_distribute_rotate_count <= 0) { g_file_distribute_rotate_count = \ FDFS_FILE_DIST_DEFAULT_ROTATE_COUNT; } pFsyncAfterWrittenBytes = iniGetStrValue(NULL, \ "fsync_after_written_bytes", &iniContext); if (pFsyncAfterWrittenBytes == NULL) { fsync_after_written_bytes = 0; } else if ((result=parse_bytes(pFsyncAfterWrittenBytes, 1, \ &fsync_after_written_bytes)) != 0) { return result; } g_fsync_after_written_bytes = fsync_after_written_bytes; g_sync_log_buff_interval = iniGetIntValue(NULL, \ "sync_log_buff_interval", &iniContext, \ SYNC_LOG_BUFF_DEF_INTERVAL); if (g_sync_log_buff_interval <= 0) { g_sync_log_buff_interval = SYNC_LOG_BUFF_DEF_INTERVAL; } g_sync_binlog_buff_interval = iniGetIntValue(NULL, \ "sync_binlog_buff_interval", &iniContext,\ SYNC_BINLOG_BUFF_DEF_INTERVAL); if (g_sync_binlog_buff_interval <= 0) { g_sync_binlog_buff_interval=SYNC_BINLOG_BUFF_DEF_INTERVAL; } g_write_mark_file_freq = iniGetIntValue(NULL, \ "write_mark_file_freq", &iniContext, \ FDFS_DEFAULT_SYNC_MARK_FILE_FREQ); if (g_write_mark_file_freq <= 0) { g_write_mark_file_freq = FDFS_DEFAULT_SYNC_MARK_FILE_FREQ; } g_sync_stat_file_interval = iniGetIntValue(NULL, \ "sync_stat_file_interval", &iniContext, \ DEFAULT_SYNC_STAT_FILE_INTERVAL); if (g_sync_stat_file_interval <= 0) { g_sync_stat_file_interval=DEFAULT_SYNC_STAT_FILE_INTERVAL; } pThreadStackSize = iniGetStrValue(NULL, \ "thread_stack_size", &iniContext); if (pThreadStackSize == NULL) { thread_stack_size = 512 * 1024; } else if ((result=parse_bytes(pThreadStackSize, 1, \ &thread_stack_size)) != 0) { return result; } g_thread_stack_size = (int)thread_stack_size; if (g_thread_stack_size < 64 * 1024) { logError("file: "__FILE__", line: %d, " \ "item \"thread_stack_size\" %d is invalid, " \ "which < %d", __LINE__, g_thread_stack_size, \ 64 * 1024); result = EINVAL; break; } g_upload_priority = iniGetIntValue(NULL, \ "upload_priority", &iniContext, \ DEFAULT_UPLOAD_PRIORITY); pIfAliasPrefix = iniGetStrValue(NULL, \ "if_alias_prefix", &iniContext); if (pIfAliasPrefix == NULL) { *g_if_alias_prefix = '\0'; } else { snprintf(g_if_alias_prefix, sizeof(g_if_alias_prefix), "%s", pIfAliasPrefix); } g_check_file_duplicate = iniGetBoolValue(NULL, \ "check_file_duplicate", &iniContext, false); if (g_check_file_duplicate) { char *pKeyNamespace; strcpy(g_fdht_base_path, g_fdfs_base_path); g_fdht_connect_timeout = g_fdfs_connect_timeout; g_fdht_network_timeout = g_fdfs_network_timeout; pKeyNamespace = iniGetStrValue(NULL, \ "key_namespace", &iniContext); if (pKeyNamespace == NULL || *pKeyNamespace == '\0') { logError("file: "__FILE__", line: %d, " \ "item \"key_namespace\" does not " \ "exist or is empty", __LINE__); result = EINVAL; break; } g_namespace_len = strlen(pKeyNamespace); if (g_namespace_len >= sizeof(g_key_namespace)) { g_namespace_len = sizeof(g_key_namespace) - 1; } memcpy(g_key_namespace, pKeyNamespace, g_namespace_len); *(g_key_namespace + g_namespace_len) = '\0'; if ((result=fdht_load_groups(&iniContext, \ &g_group_array)) != 0) { break; } g_keep_alive = iniGetBoolValue(NULL, "keep_alive", \ &iniContext, false); } g_http_port = iniGetIntValue(NULL, "http.server_port", \ &iniContext, 80); if (g_http_port <= 0) { logError("file: "__FILE__", line: %d, " \ "invalid param \"http.server_port\": %d", \ __LINE__, g_http_port); return EINVAL; } pHttpDomain = iniGetStrValue(NULL, \ "http.domain_name", &iniContext); if (pHttpDomain == NULL) { *g_http_domain = '\0'; } else { snprintf(g_http_domain, sizeof(g_http_domain), \ "%s", pHttpDomain); } #ifdef WITH_HTTPD { char *pHttpTrunkSize; int64_t http_trunk_size; if ((result=fdfs_http_params_load(&iniContext, \ filename, &g_http_params)) != 0) { break; } pHttpTrunkSize = iniGetStrValue(NULL, \ "http.trunk_size", &iniContext); if (pHttpTrunkSize == NULL) { http_trunk_size = 64 * 1024; } else if ((result=parse_bytes(pHttpTrunkSize, 1, \ &http_trunk_size)) != 0) { break; } g_http_trunk_size = (int)http_trunk_size; } #endif logInfo("FastDFS v%d.%02d, base_path=%s, store_path_count=%d, " \ "subdir_count_per_path=%d, group_name=%s, " \ "run_by_group=%s, run_by_user=%s, " \ "connect_timeout=%ds, network_timeout=%ds, "\ "port=%d, bind_addr=%s, client_bind=%d, " \ "max_connections=%d, work_threads=%d, " \ "disk_rw_separated=%d, disk_reader_threads=%d, " \ "disk_writer_threads=%d, " \ "buff_size=%dKB, heart_beat_interval=%ds, " \ "stat_report_interval=%ds, tracker_server_count=%d, " \ "sync_wait_msec=%dms, sync_interval=%dms, " \ "sync_start_time=%02d:%02d, sync_end_time=%02d:%02d, "\ "write_mark_file_freq=%d, " \ "allow_ip_count=%d, " \ "file_distribute_path_mode=%d, " \ "file_distribute_rotate_count=%d, " \ "fsync_after_written_bytes=%d, " \ "sync_log_buff_interval=%ds, " \ "sync_binlog_buff_interval=%ds, " \ "sync_stat_file_interval=%ds, " \ "thread_stack_size=%d KB, upload_priority=%d, " \ "if_alias_prefix=%s, " \ "check_file_duplicate=%d, FDHT group count=%d, " \ "FDHT server count=%d, FDHT key_namespace=%s, " \ "FDHT keep_alive=%d, HTTP server port=%d, " \ "domain name=%s", \ g_fdfs_version.major, g_fdfs_version.minor, \ g_fdfs_base_path, g_fdfs_path_count, g_subdir_count_per_path,\ g_group_name, g_run_by_group, g_run_by_user, \ g_fdfs_connect_timeout, \ g_fdfs_network_timeout, g_server_port, bind_addr, \ g_client_bind_addr, g_max_connections, \ g_work_threads, g_disk_rw_separated, \ g_disk_reader_threads, g_disk_writer_threads, \ g_buff_size / 1024, \ g_heart_beat_interval, g_stat_report_interval, \ g_tracker_group.server_count, g_sync_wait_usec / 1000, \ g_sync_interval / 1000, \ g_sync_start_time.hour, g_sync_start_time.minute, \ g_sync_end_time.hour, g_sync_end_time.minute, \ g_write_mark_file_freq, \ g_allow_ip_count, g_file_distribute_path_mode, \ g_file_distribute_rotate_count, \ g_fsync_after_written_bytes, g_sync_log_buff_interval, \ g_sync_binlog_buff_interval, g_sync_stat_file_interval, \ g_thread_stack_size/1024, g_upload_priority, \ g_if_alias_prefix, g_check_file_duplicate, \ g_group_array.group_count, g_group_array.server_count, \ g_key_namespace, g_keep_alive, \ g_http_port, g_http_domain); #ifdef WITH_HTTPD if (!g_http_params.disabled) { logInfo("HTTP supported: " \ "server_port=%d, " \ "http_trunk_size=%d, " \ "default_content_type=%s, " \ "anti_steal_token=%d, " \ "token_ttl=%ds, " \ "anti_steal_secret_key length=%d, " \ "token_check_fail content_type=%s, " \ "token_check_fail buff length=%d", \ g_http_params.server_port, \ g_http_trunk_size, \ g_http_params.default_content_type, \ g_http_params.anti_steal_token, \ g_http_params.token_ttl, \ g_http_params.anti_steal_secret_key.length, \ g_http_params.token_check_fail_content_type, \ g_http_params.token_check_fail_buff.length); } #endif } while (0); iniFreeContext(&iniContext); if (result != 0) { return result; } if ((result=storage_get_my_tracker_client_ip()) != 0) { return result; } if ((result=storage_check_and_make_data_dirs()) != 0) { logCrit("file: "__FILE__", line: %d, " \ "storage_check_and_make_data_dirs fail, " \ "program exit!", __LINE__); return result; } if ((result=storage_get_params_from_tracker()) != 0) { return result; } if ((result=storage_check_ip_changed()) != 0) { return result; } if ((result=init_pthread_lock(&sync_stat_file_lock)) != 0) { return result; } return storage_open_stat_file(); }
int ioevent_loop(struct nio_thread_data *pThreadData, IOEventCallback recv_notify_callback, TaskCleanUpCallback clean_up_callback, volatile bool *continue_flag) { int result; IOEventEntry ev_notify; FastTimerEntry head; struct fast_task_info *pTask; time_t last_check_time; int count; memset(&ev_notify, 0, sizeof(ev_notify)); ev_notify.fd = pThreadData->pipe_fds[0]; ev_notify.callback = recv_notify_callback; if (ioevent_attach(&pThreadData->ev_puller, pThreadData->pipe_fds[0], IOEVENT_READ, &ev_notify) != 0) { result = errno != 0 ? errno : ENOMEM; logCrit("file: "__FILE__", line: %d, " \ "ioevent_attach fail, " \ "errno: %d, error info: %s", \ __LINE__, result, STRERROR(result)); return result; } pThreadData->deleted_list = NULL; last_check_time = g_current_time; while (*continue_flag) { pThreadData->ev_puller.iterator.count = ioevent_poll(&pThreadData->ev_puller); if (pThreadData->ev_puller.iterator.count > 0) { deal_ioevents(&pThreadData->ev_puller); } else if (pThreadData->ev_puller.iterator.count < 0) { result = errno != 0 ? errno : EINVAL; if (result != EINTR) { logError("file: "__FILE__", line: %d, " \ "ioevent_poll fail, " \ "errno: %d, error info: %s", \ __LINE__, result, STRERROR(result)); return result; } } if (pThreadData->deleted_list != NULL) { count = 0; while (pThreadData->deleted_list != NULL) { pTask = pThreadData->deleted_list; pThreadData->deleted_list = pTask->next; clean_up_callback(pTask); count++; } logDebug("cleanup task count: %d", count); } if (g_current_time - last_check_time > 0) { last_check_time = g_current_time; count = fast_timer_timeouts_get( &pThreadData->timer, g_current_time, &head); if (count > 0) { deal_timeouts(&head); } } if (pThreadData->thread_loop_callback != NULL) { pThreadData->thread_loop_callback(pThreadData); } } return 0; }
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; }
int main_core(int argc,char* argv[]) { const char *lua_err; int status; const char *p = NULL; size_t sz = 0; int rv = 0; #ifdef EQ_TEST_HARNESS #define BOOTSTRAP_MODULE "bootstrap-test" #else #ifndef LOCAL_DEV #define BOOTSTRAP_MODULE "bootstrap-http" #else #define BOOTSTRAP_MODULE "bootstrap-local" #endif #endif equus_set_argv(argc, argv); rv = equus_util_init(); if (rv < 0) { return EXIT_FAILURE; } #ifndef _WIN32 if (signal (SIGHUP, hup_handler) == SIG_IGN) { signal (SIGHUP, SIG_IGN); } if (signal (SIGUSR1, usr1_handler) == SIG_IGN) { signal (SIGUSR1, SIG_IGN); } #endif status = equus_bootstrap_get_module(BOOTSTRAP_MODULE, &p, &sz); if (status != 0) { logCrit("Errorcode: bootstrap broken.\n"); return EXIT_FAILURE; } do { lua_State* L; equus_restart_set(0); g_equus_run_count++; L = equus_lua_vm(); status = luaL_loadbuffer(L, p, sz, "bootstrap.lua"); if (status != 0) { lua_err = lua_tostring(L, -1); logCrit("Load Buffer Error: %s\n\n", lua_err); return EXIT_FAILURE; } status = lua_pcall(L, 0, 0, 0); if (status != 0) { lua_err = lua_tostring(L, -1); logCrit("Runtime Error: %s", lua_err); return EXIT_FAILURE; } lua_close(L); equus_local_cache_clear(); } while (equus_restart_get() == 1); return 0; }
int main(int argc, char *argv[]) { char *conf_filename; int result; int sock; int wait_count; pthread_t schedule_tid; struct sigaction act; ScheduleEntry scheduleEntries[SCHEDULE_ENTRIES_MAX_COUNT]; ScheduleArray scheduleArray; char pidFilename[MAX_PATH_SIZE]; bool stop; if (argc < 2) { usage(argv[0]); return 1; } g_current_time = time(NULL); g_up_time = g_current_time; log_init2(); trunk_shared_init(); conf_filename = argv[1]; if ((result=get_base_path_from_conf_file(conf_filename, g_fdfs_base_path, sizeof(g_fdfs_base_path))) != 0) { log_destroy(); return result; } snprintf(pidFilename, sizeof(pidFilename), "%s/data/fdfs_storaged.pid", g_fdfs_base_path); if ((result=process_action(pidFilename, argv[2], &stop)) != 0) { if (result == EINVAL) { usage(argv[0]); } log_destroy(); return result; } if (stop) { log_destroy(); return 0; } #if defined(DEBUG_FLAG) && defined(OS_LINUX) if (getExeAbsoluteFilename(argv[0], g_exe_name, \ sizeof(g_exe_name)) == NULL) { logCrit("exit abnormally!\n"); log_destroy(); return errno != 0 ? errno : ENOENT; } #endif memset(g_bind_addr, 0, sizeof(g_bind_addr)); if ((result=storage_func_init(conf_filename, \ g_bind_addr, sizeof(g_bind_addr))) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } sock = socketServer(g_bind_addr, g_server_port, &result); if (sock < 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } if ((result=tcpsetserveropt(sock, g_fdfs_network_timeout)) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } daemon_init(false); umask(0); if ((result=write_to_pid_file(pidFilename)) != 0) { log_destroy(); return result; } if ((result=storage_sync_init()) != 0) { logCrit("file: "__FILE__", line: %d, " \ "storage_sync_init fail, program exit!", __LINE__); g_continue_flag = false; return result; } if ((result=tracker_report_init()) != 0) { logCrit("file: "__FILE__", line: %d, " \ "tracker_report_init fail, program exit!", __LINE__); g_continue_flag = false; return result; } if ((result=storage_service_init()) != 0) { logCrit("file: "__FILE__", line: %d, " \ "storage_service_init fail, program exit!", __LINE__); g_continue_flag = false; return result; } if ((result=set_rand_seed()) != 0) { logCrit("file: "__FILE__", line: %d, " \ "set_rand_seed fail, program exit!", __LINE__); g_continue_flag = false; return result; } memset(&act, 0, sizeof(act)); sigemptyset(&act.sa_mask); act.sa_handler = sigUsrHandler; if(sigaction(SIGUSR1, &act, NULL) < 0 || \ sigaction(SIGUSR2, &act, NULL) < 0) { logCrit("file: "__FILE__", line: %d, " \ "call sigaction fail, errno: %d, error info: %s", \ __LINE__, errno, STRERROR(errno)); logCrit("exit abnormally!\n"); return errno; } act.sa_handler = sigHupHandler; if(sigaction(SIGHUP, &act, NULL) < 0) { logCrit("file: "__FILE__", line: %d, " \ "call sigaction fail, errno: %d, error info: %s", \ __LINE__, errno, STRERROR(errno)); logCrit("exit abnormally!\n"); return errno; } act.sa_handler = SIG_IGN; if(sigaction(SIGPIPE, &act, NULL) < 0) { logCrit("file: "__FILE__", line: %d, " \ "call sigaction fail, errno: %d, error info: %s", \ __LINE__, errno, STRERROR(errno)); logCrit("exit abnormally!\n"); return errno; } act.sa_handler = sigQuitHandler; if(sigaction(SIGINT, &act, NULL) < 0 || \ sigaction(SIGTERM, &act, NULL) < 0 || \ sigaction(SIGQUIT, &act, NULL) < 0) { logCrit("file: "__FILE__", line: %d, " \ "call sigaction fail, errno: %d, error info: %s", \ __LINE__, errno, STRERROR(errno)); logCrit("exit abnormally!\n"); return errno; } #if defined(DEBUG_FLAG) /* #if defined(OS_LINUX) memset(&act, 0, sizeof(act)); act.sa_sigaction = sigSegvHandler; act.sa_flags = SA_SIGINFO; if (sigaction(SIGSEGV, &act, NULL) < 0 || \ sigaction(SIGABRT, &act, NULL) < 0) { logCrit("file: "__FILE__", line: %d, " \ "call sigaction fail, errno: %d, error info: %s", \ __LINE__, errno, STRERROR(errno)); logCrit("exit abnormally!\n"); return errno; } #endif */ memset(&act, 0, sizeof(act)); sigemptyset(&act.sa_mask); act.sa_handler = sigDumpHandler; if(sigaction(SIGUSR1, &act, NULL) < 0 || \ sigaction(SIGUSR2, &act, NULL) < 0) { logCrit("file: "__FILE__", line: %d, " \ "call sigaction fail, errno: %d, error info: %s", \ __LINE__, errno, STRERROR(errno)); logCrit("exit abnormally!\n"); return errno; } #endif #ifdef WITH_HTTPD if (!g_http_params.disabled) { if ((result=storage_httpd_start(g_bind_addr)) != 0) { logCrit("file: "__FILE__", line: %d, " \ "storage_httpd_start fail, " \ "program exit!", __LINE__); return result; } } #endif if ((result=tracker_report_thread_start()) != 0) { logCrit("file: "__FILE__", line: %d, " \ "tracker_report_thread_start fail, " \ "program exit!", __LINE__); g_continue_flag = false; storage_func_destroy(); log_destroy(); return result; } scheduleArray.entries = scheduleEntries; memset(scheduleEntries, 0, sizeof(scheduleEntries)); scheduleEntries[0].id = 1; scheduleEntries[0].time_base.hour = TIME_NONE; scheduleEntries[0].time_base.minute = TIME_NONE; scheduleEntries[0].interval = g_sync_log_buff_interval; scheduleEntries[0].task_func = log_sync_func; scheduleEntries[0].func_args = &g_log_context; scheduleEntries[1].id = 2; scheduleEntries[1].time_base.hour = TIME_NONE; scheduleEntries[1].time_base.minute = TIME_NONE; scheduleEntries[1].interval = g_sync_binlog_buff_interval; scheduleEntries[1].task_func = fdfs_binlog_sync_func; scheduleEntries[1].func_args = NULL; scheduleEntries[2].id = 3; scheduleEntries[2].time_base.hour = TIME_NONE; scheduleEntries[2].time_base.minute = TIME_NONE; scheduleEntries[2].interval = g_sync_stat_file_interval; scheduleEntries[2].task_func = fdfs_stat_file_sync_func; scheduleEntries[2].func_args = NULL; scheduleArray.count = 3; if (g_if_use_trunk_file) { scheduleEntries[scheduleArray.count].id = 4; scheduleEntries[scheduleArray.count].time_base.hour = TIME_NONE; scheduleEntries[scheduleArray.count].time_base.minute=TIME_NONE; scheduleEntries[scheduleArray.count].interval = 1; scheduleEntries[scheduleArray.count].task_func = \ trunk_binlog_sync_func; scheduleEntries[scheduleArray.count].func_args = NULL; scheduleArray.count++; } if (g_use_access_log) { scheduleEntries[scheduleArray.count].id = 5; scheduleEntries[scheduleArray.count].time_base.hour = TIME_NONE; scheduleEntries[scheduleArray.count].time_base.minute=TIME_NONE; scheduleEntries[scheduleArray.count].interval = \ g_sync_log_buff_interval; scheduleEntries[scheduleArray.count].task_func = log_sync_func; scheduleEntries[scheduleArray.count].func_args = \ &g_access_log_context; scheduleArray.count++; if (g_rotate_access_log) { scheduleEntries[scheduleArray.count].id = 6; scheduleEntries[scheduleArray.count].time_base = \ g_access_log_rotate_time; scheduleEntries[scheduleArray.count].interval = \ 24 * 3600; scheduleEntries[scheduleArray.count].task_func = \ log_notify_rotate; scheduleEntries[scheduleArray.count].func_args = \ &g_access_log_context; scheduleArray.count++; } } if (g_rotate_error_log) { scheduleEntries[scheduleArray.count].id = 7; scheduleEntries[scheduleArray.count].time_base = \ g_error_log_rotate_time; scheduleEntries[scheduleArray.count].interval = \ 24 * 3600; scheduleEntries[scheduleArray.count].task_func = \ log_notify_rotate; scheduleEntries[scheduleArray.count].func_args = \ &g_log_context; scheduleArray.count++; } if ((result=sched_start(&scheduleArray, &schedule_tid, \ g_thread_stack_size, (bool * volatile)&g_continue_flag)) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } if ((result=set_run_by(g_run_by_group, g_run_by_user)) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } if ((result=storage_dio_init()) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } log_set_cache(true); bTerminateFlag = false; bAcceptEndFlag = false; storage_accept_loop(sock); bAcceptEndFlag = true; fdfs_binlog_sync_func(NULL); //binlog fsync if (g_schedule_flag) { pthread_kill(schedule_tid, SIGINT); } storage_terminate_threads(); storage_dio_terminate(); kill_tracker_report_threads(); kill_storage_sync_threads(); wait_count = 0; while (g_storage_thread_count != 0 || \ g_dio_thread_count != 0 || \ g_tracker_reporter_count > 0 || \ g_schedule_flag) { /* #if defined(DEBUG_FLAG) && defined(OS_LINUX) if (bSegmentFault) { sleep(5); break; } #endif */ usleep(10000); if (++wait_count > 6000) { logWarning("waiting timeout, exit!"); break; } } tracker_report_destroy(); storage_service_destroy(); storage_sync_destroy(); storage_func_destroy(); if (g_if_use_trunk_file) { trunk_sync_destroy(); storage_trunk_destroy(); } logInfo("exit normally.\n"); log_destroy(); delete_pid_file(pidFilename); return 0; }
int main(int argc, char *argv[]) { char *conf_filename; int result; int wait_count; int sock; pthread_t schedule_tid; struct sigaction act; ScheduleEntry scheduleEntries[SCHEDULE_ENTRIES_COUNT]; ScheduleArray scheduleArray; char pidFilename[MAX_PATH_SIZE]; bool stop; if (argc < 2) { usage(argv[0]); return 1; } g_current_time = time(NULL); g_up_time = g_current_time; srand(g_up_time); log_init2(); //通过指定的conf文件,获取base path conf_filename = argv[1]; if ((result=get_base_path_from_conf_file(conf_filename, g_fdfs_base_path, sizeof(g_fdfs_base_path))) != 0) { log_destroy(); return result; } //启动程序,或者通过文件中的pid来kill掉之前的进程 snprintf(pidFilename, sizeof(pidFilename), "%s/data/fdfs_trackerd.pid", g_fdfs_base_path); if ((result=process_action(pidFilename, argv[2], &stop)) != 0) { if (result == EINVAL) { usage(argv[0]); } log_destroy(); return result; } if (stop) { log_destroy(); return 0; } #if defined(DEBUG_FLAG) && defined(OS_LINUX) if (getExeAbsoluteFilename(argv[0], g_exe_name, \ sizeof(g_exe_name)) == NULL) { logCrit("exit abnormally!\n"); log_destroy(); return errno != 0 ? errno : ENOENT; } #endif memset(bind_addr, 0, sizeof(bind_addr)); if ((result=tracker_load_from_conf_file(conf_filename, \ bind_addr, sizeof(bind_addr))) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } //g_tracker_last_status只记录了uptime和last check time作用未知 if ((result=tracker_load_status_from_file(&g_tracker_last_status)) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } base64_init_ex(&g_base64_context, 0, '-', '_', '.'); if ((result=set_rand_seed()) != 0) { logCrit("file: "__FILE__", line: %d, " \ "set_rand_seed fail, program exit!", __LINE__); return result; } //内存分配及历史group和storage载入 if ((result=tracker_mem_init()) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } sock = socketServer(bind_addr, g_server_port, &result); if (sock < 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } if ((result=tcpsetserveropt(sock, g_fdfs_network_timeout)) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } //后台运行 daemon_init(false); umask(0); //将pid记录到文件中 if ((result=write_to_pid_file(pidFilename)) != 0) { log_destroy(); return result; } //启动工作线程,这一块需要重点看 if ((result=tracker_service_init()) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } //注册信号处理函数 memset(&act, 0, sizeof(act)); sigemptyset(&act.sa_mask); act.sa_handler = sigUsrHandler; if(sigaction(SIGUSR1, &act, NULL) < 0 || \ sigaction(SIGUSR2, &act, NULL) < 0) { logCrit("file: "__FILE__", line: %d, " \ "call sigaction fail, errno: %d, error info: %s", \ __LINE__, errno, STRERROR(errno)); logCrit("exit abnormally!\n"); return errno; } act.sa_handler = sigHupHandler; if(sigaction(SIGHUP, &act, NULL) < 0) { logCrit("file: "__FILE__", line: %d, " \ "call sigaction fail, errno: %d, error info: %s", \ __LINE__, errno, STRERROR(errno)); logCrit("exit abnormally!\n"); return errno; } act.sa_handler = SIG_IGN; if(sigaction(SIGPIPE, &act, NULL) < 0) { logCrit("file: "__FILE__", line: %d, " \ "call sigaction fail, errno: %d, error info: %s", \ __LINE__, errno, STRERROR(errno)); logCrit("exit abnormally!\n"); return errno; } act.sa_handler = sigQuitHandler; if(sigaction(SIGINT, &act, NULL) < 0 || \ sigaction(SIGTERM, &act, NULL) < 0 || \ sigaction(SIGQUIT, &act, NULL) < 0) { logCrit("file: "__FILE__", line: %d, " \ "call sigaction fail, errno: %d, error info: %s", \ __LINE__, errno, STRERROR(errno)); logCrit("exit abnormally!\n"); return errno; } #if defined(DEBUG_FLAG) /* #if defined(OS_LINUX) memset(&act, 0, sizeof(act)); sigemptyset(&act.sa_mask); act.sa_sigaction = sigSegvHandler; act.sa_flags = SA_SIGINFO; if (sigaction(SIGSEGV, &act, NULL) < 0 || \ sigaction(SIGABRT, &act, NULL) < 0) { logCrit("file: "__FILE__", line: %d, " \ "call sigaction fail, errno: %d, error info: %s", \ __LINE__, errno, STRERROR(errno)); logCrit("exit abnormally!\n"); return errno; } #endif */ memset(&act, 0, sizeof(act)); sigemptyset(&act.sa_mask); act.sa_handler = sigDumpHandler; if(sigaction(SIGUSR1, &act, NULL) < 0 || \ sigaction(SIGUSR2, &act, NULL) < 0) { logCrit("file: "__FILE__", line: %d, " \ "call sigaction fail, errno: %d, error info: %s", \ __LINE__, errno, STRERROR(errno)); logCrit("exit abnormally!\n"); return errno; } #endif #ifdef WITH_HTTPD if (!g_http_params.disabled) { if ((result=tracker_httpd_start(bind_addr)) != 0) { logCrit("file: "__FILE__", line: %d, " \ "tracker_httpd_start fail, program exit!", \ __LINE__); return result; } } if ((result=tracker_http_check_start()) != 0) { logCrit("file: "__FILE__", line: %d, " \ "tracker_http_check_start fail, " \ "program exit!", __LINE__); return result; } #endif if ((result=set_run_by(g_run_by_group, g_run_by_user)) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } //schedule 调度线程,主要作用待细看 scheduleArray.entries = scheduleEntries; scheduleArray.count = 0; memset(scheduleEntries, 0, sizeof(scheduleEntries)); //日志处理,日志是先存在cache中的,需要定时存储 INIT_SCHEDULE_ENTRY(scheduleEntries[scheduleArray.count], scheduleArray.count + 1, TIME_NONE, TIME_NONE, TIME_NONE, g_sync_log_buff_interval, log_sync_func, &g_log_context); scheduleArray.count++; //用户检测trunk server服务器是否可用(trunk server是选定的一个storage) INIT_SCHEDULE_ENTRY(scheduleEntries[scheduleArray.count], scheduleArray.count + 1, TIME_NONE, TIME_NONE, TIME_NONE, g_check_active_interval, tracker_mem_check_alive, NULL); scheduleArray.count++; //每5分钟刷新一下status INIT_SCHEDULE_ENTRY(scheduleEntries[scheduleArray.count], scheduleArray.count + 1, 0, 0, 0, TRACKER_SYNC_STATUS_FILE_INTERVAL, tracker_write_status_to_file, NULL); scheduleArray.count++; if (g_rotate_error_log) { INIT_SCHEDULE_ENTRY_EX(scheduleEntries[scheduleArray.count], scheduleArray.count + 1, g_error_log_rotate_time, 24 * 3600, log_notify_rotate, &g_log_context); scheduleArray.count++; if (g_log_file_keep_days > 0) { log_set_keep_days(&g_log_context, g_log_file_keep_days); INIT_SCHEDULE_ENTRY(scheduleEntries[scheduleArray.count], scheduleArray.count + 1, 1, 0, 0, 24 * 3600, log_delete_old_files, &g_log_context); scheduleArray.count++; } } if ((result=sched_start(&scheduleArray, &schedule_tid, \ g_thread_stack_size, (bool * volatile)&g_continue_flag)) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } //选主机制,tracker默认不会把自己加入队列 if ((result=tracker_relationship_init()) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } log_set_cache(true); bTerminateFlag = false; bAcceptEndFlag = false; //accept线程,工作线程和消息接收线程是分开的 tracker_accept_loop(sock); bAcceptEndFlag = true; if (g_schedule_flag) { pthread_kill(schedule_tid, SIGINT); } tracker_terminate_threads(); #ifdef WITH_HTTPD if (g_http_check_flag) { tracker_http_check_stop(); } while (g_http_check_flag) { usleep(50000); } #endif wait_count = 0; //线程数和调度flag还在,则继续运行程序 while ((g_tracker_thread_count != 0) || g_schedule_flag) { /* #if defined(DEBUG_FLAG) && defined(OS_LINUX) if (bSegmentFault) { sleep(5); break; } #endif */ usleep(10000); if (++wait_count > 3000) { logWarning("waiting timeout, exit!"); break; } } tracker_mem_destroy(); tracker_service_destroy(); tracker_relationship_destroy(); logInfo("exit normally.\n"); log_destroy(); delete_pid_file(pidFilename); return 0; }
int main(int argc, char *argv[]) { char *conf_filename; int result; int sock; pthread_t schedule_tid; struct sigaction act; ScheduleEntry scheduleEntries[SCHEDULE_ENTRIES_COUNT]; ScheduleArray scheduleArray; if (argc < 2) { printf("Usage: %s <config_file>\n", argv[0]); return 1; } g_current_time = time(NULL); g_up_time = g_current_time; srand(g_up_time); log_init(); #if defined(DEBUG_FLAG) && defined(OS_LINUX) if (getExeAbsoluteFilename(argv[0], g_exe_name, \ sizeof(g_exe_name)) == NULL) { logCrit("exit abnormally!\n"); log_destroy(); return errno != 0 ? errno : ENOENT; } #endif conf_filename = argv[1]; memset(bind_addr, 0, sizeof(bind_addr)); if ((result=tracker_load_from_conf_file(conf_filename, \ bind_addr, sizeof(bind_addr))) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } if ((result=tracker_load_status_from_file(&g_tracker_last_status)) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } base64_init_ex(&g_base64_context, 0, '-', '_', '.'); if ((result=set_rand_seed()) != 0) { logCrit("file: "__FILE__", line: %d, " \ "set_rand_seed fail, program exit!", __LINE__); return result; } if ((result=tracker_mem_init()) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } sock = socketServer(bind_addr, g_server_port, &result); if (sock < 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } if ((result=tcpsetserveropt(sock, g_fdfs_network_timeout)) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } daemon_init(true); umask(0); if (dup2(g_log_context.log_fd, STDOUT_FILENO) < 0 || \ dup2(g_log_context.log_fd, STDERR_FILENO) < 0) { logCrit("file: "__FILE__", line: %d, " \ "call dup2 fail, errno: %d, error info: %s, " \ "program exit!", __LINE__, errno, STRERROR(errno)); g_continue_flag = false; return errno; } if ((result=tracker_service_init()) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } memset(&act, 0, sizeof(act)); sigemptyset(&act.sa_mask); act.sa_handler = sigUsrHandler; if(sigaction(SIGUSR1, &act, NULL) < 0 || \ sigaction(SIGUSR2, &act, NULL) < 0) { logCrit("file: "__FILE__", line: %d, " \ "call sigaction fail, errno: %d, error info: %s", \ __LINE__, errno, STRERROR(errno)); logCrit("exit abnormally!\n"); return errno; } act.sa_handler = sigHupHandler; if(sigaction(SIGHUP, &act, NULL) < 0) { logCrit("file: "__FILE__", line: %d, " \ "call sigaction fail, errno: %d, error info: %s", \ __LINE__, errno, STRERROR(errno)); logCrit("exit abnormally!\n"); return errno; } act.sa_handler = SIG_IGN; if(sigaction(SIGPIPE, &act, NULL) < 0) { logCrit("file: "__FILE__", line: %d, " \ "call sigaction fail, errno: %d, error info: %s", \ __LINE__, errno, STRERROR(errno)); logCrit("exit abnormally!\n"); return errno; } act.sa_handler = sigQuitHandler; if(sigaction(SIGINT, &act, NULL) < 0 || \ sigaction(SIGTERM, &act, NULL) < 0 || \ sigaction(SIGQUIT, &act, NULL) < 0) { logCrit("file: "__FILE__", line: %d, " \ "call sigaction fail, errno: %d, error info: %s", \ __LINE__, errno, STRERROR(errno)); logCrit("exit abnormally!\n"); return errno; } #if defined(DEBUG_FLAG) /* #if defined(OS_LINUX) memset(&act, 0, sizeof(act)); sigemptyset(&act.sa_mask); act.sa_sigaction = sigSegvHandler; act.sa_flags = SA_SIGINFO; if (sigaction(SIGSEGV, &act, NULL) < 0 || \ sigaction(SIGABRT, &act, NULL) < 0) { logCrit("file: "__FILE__", line: %d, " \ "call sigaction fail, errno: %d, error info: %s", \ __LINE__, errno, STRERROR(errno)); logCrit("exit abnormally!\n"); return errno; } #endif */ memset(&act, 0, sizeof(act)); sigemptyset(&act.sa_mask); act.sa_handler = sigDumpHandler; if(sigaction(SIGUSR1, &act, NULL) < 0 || \ sigaction(SIGUSR2, &act, NULL) < 0) { logCrit("file: "__FILE__", line: %d, " \ "call sigaction fail, errno: %d, error info: %s", \ __LINE__, errno, STRERROR(errno)); logCrit("exit abnormally!\n"); return errno; } #endif #ifdef WITH_HTTPD if (!g_http_params.disabled) { if ((result=tracker_httpd_start(bind_addr)) != 0) { logCrit("file: "__FILE__", line: %d, " \ "tracker_httpd_start fail, program exit!", \ __LINE__); return result; } } if ((result=tracker_http_check_start()) != 0) { logCrit("file: "__FILE__", line: %d, " \ "tracker_http_check_start fail, " \ "program exit!", __LINE__); return result; } #endif if ((result=set_run_by(g_run_by_group, g_run_by_user)) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } scheduleArray.entries = scheduleEntries; memset(scheduleEntries, 0, sizeof(scheduleEntries)); scheduleEntries[0].id = 1; scheduleEntries[0].time_base.hour = TIME_NONE; scheduleEntries[0].time_base.minute = TIME_NONE; scheduleEntries[0].interval = g_sync_log_buff_interval; scheduleEntries[0].task_func = log_sync_func; scheduleEntries[0].func_args = &g_log_context; scheduleEntries[1].id = 2; scheduleEntries[1].time_base.hour = TIME_NONE; scheduleEntries[1].time_base.minute = TIME_NONE; scheduleEntries[1].interval = g_check_active_interval; scheduleEntries[1].task_func = tracker_mem_check_alive; scheduleEntries[1].func_args = NULL; scheduleEntries[2].id = 3; scheduleEntries[2].time_base.hour = 0; scheduleEntries[2].time_base.minute = 0; scheduleEntries[2].interval = TRACKER_SYNC_STATUS_FILE_INTERVAL; scheduleEntries[2].task_func = tracker_write_status_to_file; scheduleEntries[2].func_args = NULL; scheduleArray.count = 3; if (g_rotate_error_log) { scheduleEntries[scheduleArray.count].id = 4; scheduleEntries[scheduleArray.count].time_base = \ g_error_log_rotate_time; scheduleEntries[scheduleArray.count].interval = \ 24 * 3600; scheduleEntries[scheduleArray.count].task_func = \ log_notify_rotate; scheduleEntries[scheduleArray.count].func_args = \ &g_log_context; scheduleArray.count++; } if ((result=sched_start(&scheduleArray, &schedule_tid, \ g_thread_stack_size, (bool * volatile)&g_continue_flag)) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } if ((result=tracker_relationship_init()) != 0) { logCrit("exit abnormally!\n"); log_destroy(); return result; } log_set_cache(true); bTerminateFlag = false; bAcceptEndFlag = false; tracker_accept_loop(sock); bAcceptEndFlag = true; if (g_schedule_flag) { pthread_kill(schedule_tid, SIGINT); } tracker_terminate_threads(); #ifdef WITH_HTTPD if (g_http_check_flag) { tracker_http_check_stop(); } while (g_http_check_flag) { usleep(50000); } #endif while ((g_tracker_thread_count != 0) || g_schedule_flag) { /* #if defined(DEBUG_FLAG) && defined(OS_LINUX) if (bSegmentFault) { sleep(5); break; } #endif */ usleep(50000); } tracker_mem_destroy(); tracker_service_destroy(); tracker_relationship_destroy(); logInfo("exit normally.\n"); log_destroy(); return 0; }