📄 tracker_mem.c
字号:
{ return NULL; }}FDFSStorageDetail *tracker_mem_get_storage(FDFSGroupInfo *pGroup, \ const char *ip_addr){ FDFSStorageDetail target_storage; FDFSStorageDetail *pTargetStorage; FDFSStorageDetail **ppStorageServer; memset(&target_storage, 0, sizeof(target_storage)); strcpy(target_storage.ip_addr, ip_addr); pTargetStorage = &target_storage; ppStorageServer = (FDFSStorageDetail **)bsearch(&pTargetStorage, \ pGroup->sorted_servers, \ pGroup->count, \ sizeof(FDFSStorageDetail *), \ tracker_mem_cmp_by_ip_addr); if (ppStorageServer != NULL) { return *ppStorageServer; } else { return NULL; }}int tracker_mem_delete_storage(FDFSGroupInfo *pGroup, const char *ip_addr){ FDFSStorageDetail *pStorageServer; FDFSStorageDetail *pServer; FDFSStorageDetail *pEnd; pStorageServer = tracker_mem_get_storage(pGroup, ip_addr); if (pStorageServer == NULL) { return ENOENT; } if (pStorageServer->status == FDFS_STORAGE_STATUS_ONLINE || \ pStorageServer->status == FDFS_STORAGE_STATUS_ACTIVE) { return EBUSY; } if (pStorageServer->status == FDFS_STORAGE_STATUS_DELETED) { return EALREADY; } pEnd = pGroup->all_servers + pGroup->count; for (pServer=pGroup->all_servers; pServer<pEnd; pServer++) { if (pServer->psync_src_server != NULL && \ strcmp(pServer->psync_src_server->ip_addr, ip_addr) == 0) { pServer->psync_src_server->ip_addr[0] = '\0'; } } pStorageServer->status = FDFS_STORAGE_STATUS_DELETED; pGroup->version++; return 0;}int tracker_mem_add_storage(TrackerClientInfo *pClientInfo, \ const bool bIncRef, bool *bInserted){ FDFSStorageDetail *pStorageServer; int result; *bInserted = false; pStorageServer = tracker_mem_get_storage(pClientInfo->pGroup, \ pClientInfo->ip_addr); if (pStorageServer != NULL) { if (pStorageServer->status == FDFS_STORAGE_STATUS_DELETED) { logError("file: "__FILE__", line: %d, " \ "storage ip: %s already deleted, you can " \ "restart the tracker servers to reset.", \ __LINE__, pClientInfo->ip_addr); return EAGAIN; } //printf("pGroup->count=%d, found %s\n", pClientInfo->pGroup->count, pClientInfo->ip_addr); } else { //printf("pGroup->count=%d, not found %s\n", pClientInfo->pGroup->count, pClientInfo->ip_addr); if ((result=pthread_mutex_lock(&mem_thread_lock)) != 0) { logError("file: "__FILE__", line: %d, " \ "call pthread_mutex_lock fail, " \ "errno: %d, error info: %s", \ __LINE__, result, strerror(result)); return result; } result = 0; while (1) { if (pClientInfo->pGroup->count >= \ pClientInfo->pGroup->alloc_size) { result = tracker_mem_realloc_store_server( \ pClientInfo->pGroup, 1); if (result != 0) { break; } } pStorageServer = pClientInfo->pGroup->all_servers \ + pClientInfo->pGroup->count; memcpy(pStorageServer->ip_addr, pClientInfo->ip_addr, IP_ADDRESS_SIZE); tracker_mem_insert_into_sorted_servers( \ pStorageServer, \ pClientInfo->pGroup->sorted_servers, \ pClientInfo->pGroup->count); pClientInfo->pGroup->count++; pClientInfo->pGroup->version++; break; } if (pthread_mutex_unlock(&mem_thread_lock) != 0) { logError("file: "__FILE__", line: %d, " \ "call pthread_mutex_unlock fail", \ __LINE__); } if (result != 0) { return result; } *bInserted = true; } if (bIncRef) { ++(*(pStorageServer->ref_count)); //printf("group: %s, pStorageServer->ref_count=%d\n", pClientInfo->pGroup->group_name, *(pStorageServer->ref_count)); } pClientInfo->pStorage = pStorageServer; pClientInfo->pAllocedStorages = pClientInfo->pGroup->all_servers; return 0;}int tracker_mem_add_group_and_storage(TrackerClientInfo *pClientInfo, \ const int store_path_count, const int subdir_count_per_path, \ const bool bIncRef){ int result; bool bGroupInserted; bool bStorageInserted; FDFSStorageDetail *pStorageServer; FDFSStorageDetail *pServer; FDFSStorageDetail *pEnd; if ((result=tracker_mem_add_group(pClientInfo, bIncRef, \ &bGroupInserted)) != 0) { return result; } if (bGroupInserted) { if ((result=tracker_save_groups()) != 0) { return result; } } if (pClientInfo->pGroup->storage_port == 0) { pClientInfo->pGroup->storage_port = pClientInfo->storage_port; if ((result=tracker_save_groups()) != 0) { return result; } } else { if (pClientInfo->pGroup->storage_port != \ pClientInfo->storage_port) { logError("file: "__FILE__", line: %d, " \ "client ip: %s, port %d is not same " \ "in the group \"%s\", group port is %d", \ __LINE__, pClientInfo->ip_addr, \ pClientInfo->storage_port, \ pClientInfo->group_name, \ pClientInfo->pGroup->storage_port); return EINVAL; } } if ((result=tracker_mem_add_storage(pClientInfo, bIncRef, \ &bStorageInserted)) != 0) { return result; } pStorageServer = pClientInfo->pStorage; pStorageServer->store_path_count = store_path_count; pStorageServer->subdir_count_per_path = subdir_count_per_path; if (pClientInfo->pGroup->store_path_count == 0) { pClientInfo->pGroup->store_path_count = store_path_count; if ((result=tracker_malloc_group_path_mbs( \ pClientInfo->pGroup)) != 0) { return result; } if ((result=tracker_save_groups()) != 0) { return result; } } else { if (pClientInfo->pGroup->store_path_count != store_path_count) { pEnd = pClientInfo->pGroup->all_servers + \ pClientInfo->pGroup->count; for (pServer=pClientInfo->pGroup->all_servers; \ pServer<pEnd; pServer++) { if (pServer->store_path_count!=store_path_count) { break; } } if (pServer == pEnd) //all servers are same, adjust { if ((result=tracker_realloc_group_path_mbs( \ pClientInfo->pGroup, store_path_count))!=0) { return result; } if ((result=tracker_save_groups()) != 0) { return result; } } else { logError("file: "__FILE__", line: %d, " \ "client ip: %s, store_path_count %d is not " \ "same in the group \"%s\", " \ "group store_path_count is %d", \ __LINE__, pClientInfo->ip_addr, \ store_path_count, pClientInfo->group_name, \ pClientInfo->pGroup->store_path_count); return EINVAL; } } } if (pClientInfo->pGroup->subdir_count_per_path == 0) { pClientInfo->pGroup->subdir_count_per_path = \ subdir_count_per_path; if ((result=tracker_save_groups()) != 0) { return result; } } else { if (pClientInfo->pGroup->subdir_count_per_path != \ subdir_count_per_path) { pEnd = pClientInfo->pGroup->all_servers + \ pClientInfo->pGroup->count; for (pServer=pClientInfo->pGroup->all_servers; \ pServer<pEnd; pServer++) { if (pServer->subdir_count_per_path != \ subdir_count_per_path) { break; } } if (pServer == pEnd) //all servers are same, adjust { pClientInfo->pGroup->subdir_count_per_path = \ subdir_count_per_path; if ((result=tracker_save_groups()) != 0) { return result; } } else { logError("file: "__FILE__", line: %d, " \ "client ip: %s, subdir_count_per_path %d is " \ "not same in the group \"%s\", " \ "group subdir_count_per_path is %d", \ __LINE__, pClientInfo->ip_addr, \ subdir_count_per_path, pClientInfo->group_name,\ pClientInfo->pGroup->subdir_count_per_path); return EINVAL; } } } if (bStorageInserted) { pStorageServer->status = FDFS_STORAGE_STATUS_INIT; if ((result=tracker_save_storages()) != 0) { return result; } } else if (!((pStorageServer->status == FDFS_STORAGE_STATUS_WAIT_SYNC)||\ (pStorageServer->status == FDFS_STORAGE_STATUS_SYNCING) || \ (pStorageServer->status == FDFS_STORAGE_STATUS_INIT) || \ (pStorageServer->status == FDFS_STORAGE_STATUS_DELETED))) { pStorageServer->status = FDFS_STORAGE_STATUS_ONLINE; } return 0;}int tracker_mem_sync_storages(TrackerClientInfo *pClientInfo, \ FDFSStorageBrief *briefServers, const int server_count){ int result; FDFSStorageBrief *pServer; FDFSStorageBrief *pEnd; FDFSStorageDetail target_storage; FDFSStorageDetail *pTargetStorage; FDFSStorageDetail *pStorageServer; FDFSStorageDetail **ppFound; if ((result=pthread_mutex_lock(&mem_thread_lock)) != 0) { logError("file: "__FILE__", line: %d, " \ "call pthread_mutex_lock fail, " \ "errno: %d, error info: %s", \ __LINE__, result, strerror(result)); return result; } result = 0; while (1) { if (pClientInfo->pGroup->count + server_count >= \ pClientInfo->pGroup->alloc_size) { result = tracker_mem_realloc_store_server( \ pClientInfo->pGroup, server_count); if (result != 0) { break; } } memset(&target_storage, 0, sizeof(target_storage)); pStorageServer = pClientInfo->pGroup->all_servers \ + pClientInfo->pGroup->count; pEnd = briefServers + server_count; for (pServer=briefServers; pServer<pEnd; pServer++) { pServer->ip_addr[IP_ADDRESS_SIZE-1] = '\0'; memcpy(target_storage.ip_addr, pServer->ip_addr, \ IP_ADDRESS_SIZE); pTargetStorage = &target_storage; if ((ppFound=(FDFSStorageDetail **)bsearch( \ &pTargetStorage, \ pClientInfo->pGroup->sorted_servers, \ pClientInfo->pGroup->count, \ sizeof(FDFSStorageDetail *), \ tracker_mem_cmp_by_ip_addr)) != NULL) { if (((pServer->status > (*ppFound)->status) && \ (((*ppFound)->status == \ FDFS_STORAGE_STATUS_INIT) || \ ((*ppFound)->status == \ FDFS_STORAGE_STATUS_WAIT_SYNC) || \ ((*ppFound)->status == \ FDFS_STORAGE_STATUS_SYNCING))) || \ (pServer->status != (*ppFound)->status \ && pServer->status == \ FDFS_STORAGE_STATUS_DELETED)) { (*ppFound)->status = pServer->status; pClientInfo->pGroup->version++; } continue; } pStorageServer->status = pServer->status; memcpy(pStorageServer->ip_addr, pServer->ip_addr, \ IP_ADDRESS_SIZE); tracker_mem_insert_into_sorted_servers( \ pStorageServer, \ pClientInfo->pGroup->sorted_servers, \ pClientInfo->pGroup->count); pStorageServer++; pClientInfo->pGroup->count++; } break; } if (pthread_mutex_unlock(&mem_thread_lock) != 0) { logError("file: "__FILE__", line: %d, " \ "call pthread_mutex_unlock fail", \ __LINE__); } return result;}int tracker_mem_deactive_store_server(FDFSGroupInfo *pGroup, FDFSStorageDetail *pTargetServer) { int result; FDFSStorageDetail **ppStorageServer; FDFSStorageDetail **ppEnd; FDFSStorageDetail **ppServer; if ((result=pthread_mutex_lock(&mem_thread_lock)) != 0) { logError("file: "__FILE__", line: %d, " \ "call pthread_mutex_lock fail, " \ "errno: %d, error info: %s", \ __LINE__, result, strerror(result)); return result; } ppStorageServer = (FDFSStorageDetail **)bsearch( \ &pTargetServer, \ pGroup->active_servers, \ pGroup->active_count, \ sizeof(FDFSStorageDetail *), \ tracker_mem_cmp_by_ip_addr); if (ppStorageServer != NULL) { ppEnd = pGroup->active_servers + pGroup->active_count - 1; for (ppServer=ppStorageServer; ppServer<ppEnd; ppServer++) { *ppServer = *(ppServer+1); } pGroup->active_count--; pGroup->version++; if (pGroup->current_write_server >= pGroup->active_count) { pGroup->current_write_server = 0; } if (pGroup->current_read_server >= pGroup->active_count) { pGroup->current_read_server = 0; } } if ((result=pthread_mutex_unlock(&mem_thread_lock)) != 0) { logError("file: "__FILE__", line: %d, " \ "call pthread_mutex_unlock fail, " \ "errno: %d, error info: %s", \ __LINE__, result, strerror(result)); return result; } return 0;}int tracker_mem_active_store_server(FDFSGroupInfo *pGroup, \ FDFSStorageDetail *pTargetServer) { int result; FDFSStorageDetail **ppStorageServer; if ((pTargetServer->status == FDFS_STORAGE_STATUS_WAIT_SYNC) || \ (pTargetServer->status == FDFS_STORAGE_STATUS_SYNCING) || \ (pTargetServer->status == FDFS_STORAGE_STATUS_INIT)) { return 0; } if (pTargetServer->status == FDFS_STORAGE_STATUS_DELETED) { logError("file: "__FILE__", line: %d, " \ "storage ip: %s already deleted, you can " \ "restart the tracker servers to reset.", \ __LINE__, pTargetServer->ip_addr); return EAGAIN; } pTargetServer->status = FDFS_STORAGE_STATUS_ACTIVE; ppStorageServer = (FDFSStorageDetail **)bsearch(&pTargetServer, \ pGroup->active_servers, \ pGroup->active_count, \ sizeof(FDFSStorageDetail *), \ tracker_mem_cmp_by_ip_addr); if (ppStorageServer != NULL) { return 0; } if ((result=pthread_mutex_lock(&mem_thread_lock)) != 0) { logError("file: "__FILE__", line: %d, " \ "call pthread_mutex_lock fail, " \ "errno: %d, error info: %s", \ __LINE__, result, strerror(result)); return result; } ppStorageServer = (FDFSStorageDetail **)bsearch(&pTargetServer, \ pGroup->active_servers, \ pGroup->active_count, \ sizeof(FDFSStorageDetail *), \ tracker_mem_cmp_by_ip_addr); if (ppStorageServer == NULL) { tracker_mem_insert_into_sorted_servers( \ pTargetServer, pGroup->active_servers, \ pGroup->active_count); pGroup->active_count++; pGroup->version++; } if ((result=pthread_mutex_unlock(&mem_thread_lock)) != 0) { logError("file: "__FILE__", line: %d, " \ "call pthread_mutex_unlock fail, " \ "errno: %d, error info: %s", \ __LINE__, result, strerror(result)); return result; } return 0;}int tracker_mem_offline_store_server(TrackerClientInfo *pClientInfo){ if (pClientInfo->pGroup == NULL || pClientInfo->pStorage == NULL) { return 0; } if ((pClientInfo->pStorage->status == \ FDFS_STORAGE_STATUS_WAIT_SYNC) || \ (pClientInfo->pStorage->status == \ FDFS_STORAGE_STATUS_SYNCING) || \ (pClientInfo->pStorage->status == \ FDFS_STORAGE_STATUS_INIT) || \ (pClientInfo->pStorage->status == \ FDFS_STORAGE_STATUS_DELETED)) { return 0; } pClientInfo->pStorage->status = FDFS_STORAGE_STATUS_OFFLINE; return tracker_mem_deactive_store_server(pClientInfo->pGroup, \ pClientInfo->pStorage);}int tracker_mem_pthread_lock(){ int result; if ((result=pthread_mutex_lock(&mem_thread_lock)) != 0) { logError("file: "__FILE__", line: %d, " \ "call pthread_mutex_lock fail, " \ "errno: %d, error info: %s", \ __LINE__, result, strerror(result)); return result; } return 0;}int tracker_mem_pthread_unlock(){ int result; if ((result=pthread_mutex_unlock(&mem_thread_lock)) != 0) { logError("file: "__FILE__", line: %d, " \ "call pthread_mutex_unlock fail, " \ "errno: %d, error info: %s", \ __LINE__, result, strerror(result)); return result; } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -