int family_process(const uint64_t ns_id, map<uint64_t, int32_t>& family_map) { const int32_t num = 1000; ShowServerInformationMessage msg; SSMScanParameter& param = msg.get_param(); param.type_ = SSM_TYPE_FAMILY; param.should_actual_count_ = (num << 16); param.end_flag_ = SSM_SCAN_CUTOVER_FLAG_YES; //其余,如param.start_next_position_ 等都初始化为0 while (!((param.end_flag_ >> 4) & SSM_SCAN_END_FLAG_YES)) { param.data_.clear(); tbnet::Packet*ret_msg = NULL; NewClient* client = NewClientManager::get_instance().create_client(); int ret = send_msg_to_server(ns_id, client, &msg, ret_msg, DEFAULT_NETWORK_CALL_TIMEOUT, true); if (TFS_SUCCESS != ret || ret_msg == NULL) { TBSYS_LOG(ERROR, "get block info error, ret: %d", ret); NewClientManager::get_instance().destroy_client(client); return EXIT_TFS_ERROR; } if(ret_msg->getPCode() != SHOW_SERVER_INFORMATION_MESSAGE) { if (ret_msg->getPCode() == STATUS_MESSAGE) { StatusMessage* msg = dynamic_cast<StatusMessage*>(ret_msg); TBSYS_LOG(ERROR, "get invalid message type: error: %s", msg->get_error()); } TBSYS_LOG(ERROR, "get invalid message type, pcode: %d", ret_msg->getPCode()); NewClientManager::get_instance().destroy_client(client); return EXIT_TFS_ERROR; } ShowServerInformationMessage* message = dynamic_cast<ShowServerInformationMessage*>(ret_msg); SSMScanParameter& ret_param = message->get_param(); int32_t data_len = ret_param.data_.getDataLen(); int32_t offset = 0; while (data_len > offset) { FamilyShow family; if (TFS_SUCCESS == family.deserialize(ret_param.data_, data_len, offset)) { family_map.insert(make_pair(family.family_id_, family.family_aid_info_)); } } param.start_next_position_ = (ret_param.start_next_position_ << 16) & 0xffff0000; param.end_flag_ = ret_param.end_flag_; if (param.end_flag_ & SSM_SCAN_CUTOVER_FLAG_NO) { param.addition_param1_ = ret_param.addition_param2_;//next start family_id to scan } NewClientManager::get_instance().destroy_client(client); } return TFS_SUCCESS; }
int ShowInfo::show_family(const int8_t type, const int32_t num, const int64_t family_id, int32_t count, const int32_t interval, const string& filename) { interrupt_ = false; is_loop_ = (count == 0);//count表示循环重复拉取的次数,默认是1,0表示一直按照间间隔循环 FILE* fp = NULL; if (TFS_SUCCESS != get_file_handle(filename, &fp)) { return EXIT_TFS_ERROR; } while ((count > 0 || is_loop_) && !interrupt_) { ShowServerInformationMessage msg; SSMScanParameter& param = msg.get_param(); param.type_ = SSM_TYPE_FAMILY; uint64_t family_count = 0; bool once = false; if (family_id > 0) { param.should_actual_count_ = 0x10000; param.end_flag_ = SSM_SCAN_CUTOVER_FLAG_NO; param.start_next_position_ = ((family_id % nameserver::MAX_FAMILY_CHUNK_NUM) << 16); param.addition_param1_ = family_id; once = true; } else if (family_id == 0) { if (type & BLOCK_TYPE_SERVER_LIST) { fprintf(fp, "para error, -s must be used with -d\n"); put_file_handle(fp); return EXIT_PARAMETER_ERROR; } param.should_actual_count_ = (num << 16); param.end_flag_ = SSM_SCAN_CUTOVER_FLAG_YES; //其余,如param.start_next_position_ 等都初始化为0 } else { TBSYS_LOG(ERROR, "get invalid family_id: %"PRI64_PREFIX"d < 0", family_id); put_file_handle(fp); return EXIT_PARAMETER_ERROR; } while ((!((param.end_flag_ >> 4) & SSM_SCAN_END_FLAG_YES)) && !interrupt_) { param.data_.clear(); tbnet::Packet*ret_msg = NULL; NewClient* client = NewClientManager::get_instance().create_client(); int ret = send_msg_to_server(ns_ip_, client, &msg, ret_msg); if (TFS_SUCCESS != ret || ret_msg == NULL) { TBSYS_LOG(ERROR, "get block info error, ret: %d", ret); NewClientManager::get_instance().destroy_client(client); put_file_handle(fp); return EXIT_TFS_ERROR; } if(ret_msg->getPCode() != SHOW_SERVER_INFORMATION_MESSAGE) { if (ret_msg->getPCode() == STATUS_MESSAGE) { StatusMessage* msg = dynamic_cast<StatusMessage*>(ret_msg); TBSYS_LOG(ERROR, "get invalid message type: error: %s", msg->get_error()); } TBSYS_LOG(ERROR, "get invalid message type, pcode: %d", ret_msg->getPCode()); NewClientManager::get_instance().destroy_client(client); put_file_handle(fp); return EXIT_TFS_ERROR; } ShowServerInformationMessage* message = dynamic_cast<ShowServerInformationMessage*>(ret_msg); SSMScanParameter& ret_param = message->get_param(); int32_t data_len = ret_param.data_.getDataLen(); int32_t offset = 0; if (data_len > 0) { print_header(FAMILY_TYPE, type, fp); } while ((data_len > offset) && !interrupt_) { FamilyShow family; if (TFS_SUCCESS == family.deserialize(ret_param.data_, data_len, offset)) { if (once && (family.family_id_ != family_id)) { //TBSYS_LOG(ERROR, "only get family: %"PRI64_PREFIX"u, but %"PRI64_PREFIX"u not exists", family.family_id_, family_id); break; } if (once && (type & BLOCK_TYPE_SERVER_LIST)) { ret = family.get_members_ds_list(ns_ip_); if (TFS_SUCCESS != ret) { put_file_handle(fp); return ret; } } family.dump(type, fp); ++family_count; } } param.start_next_position_ = (ret_param.start_next_position_ << 16) & 0xffff0000; param.end_flag_ = ret_param.end_flag_; if (param.end_flag_ & SSM_SCAN_CUTOVER_FLAG_NO) { param.addition_param1_ = ret_param.addition_param2_;//next start family_id to scan } NewClientManager::get_instance().destroy_client(client); if (once) { break; } } if (!once) { fprintf(fp, "Total Count: %"PRI64_PREFIX"u\n", family_count); } if (--count) { sleep(interval); } } put_file_handle(fp); return TFS_SUCCESS; }
int ShowInfo::show_block_common(const int32_t num, const uint64_t block_id, int32_t count, const int32_t interval, const string& filename, BaseBlockWorker* worker) { FILE* fp = NULL; if (TFS_SUCCESS != get_file_handle(filename, &fp)) { return EXIT_TFS_ERROR; } interrupt_ = false; is_loop_ = (count == 0); while ((count > 0 || is_loop_) && !interrupt_) { if(TFS_SUCCESS != worker->begin()) { put_file_handle(fp); return EXIT_TFS_ERROR; } ShowServerInformationMessage msg; SSMScanParameter& param = msg.get_param(); param.type_ = SSM_TYPE_BLOCK;//遍历ns上的block数据的类型 param.child_type_ = SSM_CHILD_BLOCK_TYPE_INFO | SSM_CHILD_BLOCK_TYPE_SERVER;//TYPE_INFO只为取block_id, TYPE_SERVER为ds_list bool once = false; if (block_id > 0) { param.should_actual_count_ = 0x10000; param.end_flag_ = SSM_SCAN_CUTOVER_FLAG_NO; param.start_next_position_ = ((block_id % nameserver::MAX_BLOCK_CHUNK_NUMS) << 16);//start是本次在ns上开始扫描blocks的桶的位置 param.addition_param1_ = block_id; once = true; } else { param.should_actual_count_ = (num << 16); param.end_flag_ = SSM_SCAN_CUTOVER_FLAG_YES; } print_header(worker->block_type_, worker->sub_type_, fp); while ((!((param.end_flag_ >> 4) & SSM_SCAN_END_FLAG_YES)) && !interrupt_) { param.data_.clear(); tbnet::Packet*ret_msg = NULL; NewClient* client = NewClientManager::get_instance().create_client(); int ret = send_msg_to_server(ns_ip_, client, &msg, ret_msg); if (TFS_SUCCESS != ret || ret_msg == NULL) { TBSYS_LOG(ERROR, "get block info error, ret: %d", ret); NewClientManager::get_instance().destroy_client(client); put_file_handle(fp); return EXIT_TFS_ERROR; } if(ret_msg->getPCode() != SHOW_SERVER_INFORMATION_MESSAGE) { if (ret_msg->getPCode() == STATUS_MESSAGE) { StatusMessage* msg = dynamic_cast<StatusMessage*>(ret_msg); TBSYS_LOG(ERROR, "get invalid message type: error: %s", msg->get_error()); } TBSYS_LOG(ERROR, "get invalid message type, pcode: %d", ret_msg->getPCode()); NewClientManager::get_instance().destroy_client(client); put_file_handle(fp); return EXIT_TFS_ERROR; } ShowServerInformationMessage* message = dynamic_cast<ShowServerInformationMessage*>(ret_msg); SSMScanParameter& ret_param = message->get_param(); int32_t data_len = ret_param.data_.getDataLen(); int32_t offset = 0; //如果指定的blockid不存在,且block表中[start_next_position_, +00)这些桶内没有block,则拉取block个数为0 while ((data_len > offset) && !interrupt_) { BlockBase* base_block = worker->create_block(); ret = base_block->deserialize(ret_param.data_, data_len, offset, param.child_type_); if (TFS_SUCCESS == ret) { base_block->BlockBase::dump();// log info of each block info if (once && (base_block->info_.block_id_ != block_id)) { //TBSYS_LOG(ERROR, "block: %"PRI64_PREFIX"u,%"PRI64_PREFIX"u not exists", base_block->info_.block_id_, block_id); break; } worker->process(base_block, fp);// not sort by block } else { TBSYS_LOG(ERROR, "Block deserialize error, ret: %d", ret); NewClientManager::get_instance().destroy_client(client); put_file_handle(fp); return ret; } tbsys::gDelete(base_block); } param.start_next_position_ = (ret_param.start_next_position_ << 16) & 0xffff0000; param.end_flag_ = ret_param.end_flag_; if (param.end_flag_ & SSM_SCAN_CUTOVER_FLAG_NO) { param.addition_param1_ = ret_param.addition_param2_; } NewClientManager::get_instance().destroy_client(client); if (once) { break; } } if (!once) { worker->end(fp); } if (--count) { sleep(interval); } } put_file_handle(fp); return TFS_SUCCESS; }
int ShowInfo::show_machine(const int8_t type, const int32_t num, int32_t count, const int32_t interval, const bool need_family, const string& filename) { FILE* fp = NULL; if (TFS_SUCCESS != get_file_handle(filename, &fp)) { return EXIT_TFS_ERROR; } interrupt_ = false; is_loop_ = (count == 0); while ((count > 0 || is_loop_) && !interrupt_) { last_server_map_.clear(); load_last_ds(); int32_t last_server_size = last_server_map_.size(); server_map_.clear(); machine_map_.clear(); StatStruct stat; ShowServerInformationMessage msg; SSMScanParameter& param = msg.get_param(); param.type_ = SSM_TYPE_SERVER; param.child_type_ = SSM_CHILD_SERVER_TYPE_INFO; param.start_next_position_ = 0x0; param.should_actual_count_= (num << 16); param.end_flag_ = SSM_SCAN_CUTOVER_FLAG_YES; while ((!((param.end_flag_ >> 4) & SSM_SCAN_END_FLAG_YES)) && !interrupt_) { param.data_.clear(); tbnet::Packet*ret_msg = NULL; NewClient* client = NewClientManager::get_instance().create_client(); int ret = send_msg_to_server(ns_ip_, client, &msg, ret_msg); if (TFS_SUCCESS != ret || ret_msg == NULL) { TBSYS_LOG(ERROR, "get server info error, ret: %d", ret); NewClientManager::get_instance().destroy_client(client); put_file_handle(fp); return EXIT_TFS_ERROR; } if(ret_msg->getPCode() != SHOW_SERVER_INFORMATION_MESSAGE) { TBSYS_LOG(ERROR, "get invalid message type, pcode: %d", ret_msg->getPCode()); NewClientManager::get_instance().destroy_client(client); put_file_handle(fp); return EXIT_TFS_ERROR; } ShowServerInformationMessage* message = dynamic_cast<ShowServerInformationMessage*>(ret_msg); SSMScanParameter& ret_param = message->get_param(); int32_t data_len = ret_param.data_.getDataLen(); int32_t offset = 0; print_header(MACHINE_TYPE, type, fp); while ((data_len > offset) && !interrupt_) { ServerShow server; if (TFS_SUCCESS == server.ServerBase::deserialize(ret_param.data_, data_len, offset, SERVER_TYPE_SERVER_INFO)) { server_map_[server.server_stat_.id_] = server; ServerShow old_server; old_server = server; if (last_server_size > 0) { map<uint64_t, ServerShow>::iterator iter = last_server_map_.find(server.server_stat_.id_); if (iter != last_server_map_.end()) { old_server = iter->second; } } last_server_map_[server.server_stat_.id_] = server; if (need_family) { ret = server.fetch_family_set(); if (TFS_SUCCESS != ret) { TBSYS_LOG(WARN, "server(%s) fetch family list fail, ret: %d", tbsys::CNetUtil::addrToString(server.server_stat_.id_).c_str(), ret); } } uint64_t machine_id = get_machine_id(server.server_stat_.id_); map<uint64_t, MachineShow>::iterator iter = machine_map_.find(machine_id); if (iter != machine_map_.end()) { (iter->second).add(server, old_server); } else { MachineShow machine; machine.machine_id_ = machine_id; machine.init(server, old_server); machine.add(server, old_server); machine_map_.insert(make_pair<uint64_t, MachineShow> (machine_id, machine)); } } } param.addition_param1_ = ret_param.addition_param1_; param.addition_param2_ = ret_param.addition_param2_; param.end_flag_ = ret_param.end_flag_; NewClientManager::get_instance().destroy_client(client); save_last_ds(); } map<uint64_t, MachineShow>::iterator it = machine_map_.begin(); for (; it != machine_map_.end(); it++) { (it->second).calculate(); (it->second).dump(type, fp); stat.add(it->second); } // first time last_server_size=0, all tps are 0, so not show it for tps monitor if (last_server_size > 0 || !(type & MACHINE_TYPE_FOR_MONITOR)) { stat.dump(MACHINE_TYPE, type, fp); } if (--count || is_loop_) { sleep(interval); } } put_file_handle(fp); return TFS_SUCCESS; }
int ShowInfo::show_server(const int8_t type, const int32_t num, const string& server_ip_port, int32_t count, const int32_t interval, const bool need_family, const string& filename) { FILE* fp = NULL; if (TFS_SUCCESS != get_file_handle(filename, &fp)) { return EXIT_TFS_ERROR; } interrupt_ = false; is_loop_ = (count == 0); while ((count > 0 || is_loop_) && !interrupt_) { last_server_map_.clear(); load_last_ds(); int32_t last_server_size = last_server_map_.size(); server_map_.clear(); StatStruct stat; ShowServerInformationMessage msg; SSMScanParameter& param = msg.get_param(); param.type_ = SSM_TYPE_SERVER; param.child_type_ = SSM_CHILD_SERVER_TYPE_INFO; if (type & SERVER_TYPE_BLOCK_LIST) { param.child_type_ |= SSM_CHILD_SERVER_TYPE_HOLD; } if (type & SERVER_TYPE_BLOCK_WRITABLE) { param.child_type_ |= SSM_CHILD_SERVER_TYPE_WRITABLE; } if (type & SERVER_TYPE_BLOCK_MASTER) { param.child_type_ |= SSM_CHILD_SERVER_TYPE_MASTER; } bool once = false; if (server_ip_port != "") { param.should_actual_count_ = 0x10000; int ret = get_addr(server_ip_port, param.addition_param1_, param.addition_param2_); if (ret != TFS_SUCCESS) { TBSYS_LOG(ERROR, "server(%s) not valid", server_ip_port.c_str()); put_file_handle(fp); return ret; } once = true; } else { param.start_next_position_ = 0x0;//它对SSM_TYPE_SERVER是没用的,因为ns直接使用addition_param param.should_actual_count_= (num << 16); param.end_flag_ = SSM_SCAN_CUTOVER_FLAG_YES; } while ((!((param.end_flag_ >> 4) & SSM_SCAN_END_FLAG_YES)) && !interrupt_) { param.data_.clear(); tbnet::Packet* ret_msg = NULL; NewClient* client = NewClientManager::get_instance().create_client(); int ret = send_msg_to_server(ns_ip_, client, &msg, ret_msg); if (TFS_SUCCESS != ret || ret_msg == NULL) { TBSYS_LOG(ERROR, "get server info error, ret: %d", ret); NewClientManager::get_instance().destroy_client(client); put_file_handle(fp); return EXIT_TFS_ERROR; } if(ret_msg->getPCode() != SHOW_SERVER_INFORMATION_MESSAGE) { TBSYS_LOG(ERROR, "get invalid message type, pcode: %d", ret_msg->getPCode()); NewClientManager::get_instance().destroy_client(client); put_file_handle(fp); return EXIT_TFS_ERROR; } ShowServerInformationMessage* message = dynamic_cast<ShowServerInformationMessage*>(ret_msg); SSMScanParameter& ret_param = message->get_param(); int32_t data_len = ret_param.data_.getDataLen(); int32_t offset = 0; if (data_len > 0) { print_header(SERVER_TYPE, type, fp); } else if (once) { fprintf(fp, "server(%s) not exists\n", server_ip_port.c_str()); break; } while ((data_len > offset) && !interrupt_) { ServerShow server, old_server; if (TFS_SUCCESS == server.ServerBase::deserialize(ret_param.data_, data_len, offset, type)) { if (need_family) { ret = server.fetch_family_set(); if (TFS_SUCCESS != ret) { TBSYS_LOG(WARN, "server(%s) fetch family list fail, ret: %d", tbsys::CNetUtil::addrToString(server.server_stat_.id_).c_str(), ret); } } server.ServerBase::dump(); if (once && ((tbsys::CNetUtil::addrToString(server.server_stat_.id_)) != server_ip_port)) { break; } server_map_[server.server_stat_.id_] = server; old_server = server; if (last_server_size > 0) { map<uint64_t, ServerShow>::iterator iter = last_server_map_.find(server.server_stat_.id_); if (iter != last_server_map_.end()) { old_server = iter->second; //old_server.ServerBase::dump(); } } last_server_map_[server.server_stat_.id_] = server; server.calculate(old_server); stat.add(server); server.dump(type, fp); } } param.addition_param1_ = ret_param.addition_param1_; param.addition_param2_ = ret_param.addition_param2_; param.end_flag_ = ret_param.end_flag_; NewClientManager::get_instance().destroy_client(client); save_last_ds(); if (once) { break; } } // print total info when print info only if (!once) { stat.dump(SERVER_TYPE, type, fp); } if ((--count) > 0 || is_loop_) { sleep(interval); } } put_file_handle(fp); return TFS_SUCCESS; }
int ShowInfo::show_block(const int8_t type, const int32_t num, const uint32_t block_id, const int32_t block_chunk_num, int32_t count, const int32_t interval, const string& filename) { interrupt_ = false; is_loop_ = (count == 0); FILE* fp = NULL; if (TFS_ERROR == get_file_handle(filename, &fp)) { return TFS_ERROR; } while ((count > 0 || is_loop_) && !interrupt_) { StatStruct stat; ShowServerInformationMessage msg; SSMScanParameter& param = msg.get_param(); param.type_ = SSM_TYPE_BLOCK; param.child_type_ = SSM_CHILD_BLOCK_TYPE_INFO | SSM_CHILD_BLOCK_TYPE_SERVER; bool once = false; if (block_id > 0) { param.should_actual_count_ = 0x10000; param.end_flag_ = SSM_SCAN_CUTOVER_FLAG_NO; param.should_actual_count_ = 0x10000; param.start_next_position_ = ((block_id % block_chunk_num) << 16); param.addition_param1_ = block_id; once = true; } else { param.should_actual_count_ = (num << 16); param.end_flag_ = SSM_SCAN_CUTOVER_FLAG_YES; } while ((!((param.end_flag_ >> 4) & SSM_SCAN_END_FLAG_YES)) && !interrupt_) { param.data_.clear(); block_set_.clear(); tbnet::Packet*ret_msg = NULL; NewClient* client = NewClientManager::get_instance().create_client(); int ret = send_msg_to_server(ns_ip_, client, &msg, ret_msg); if (TFS_SUCCESS != ret || ret_msg == NULL) { TBSYS_LOG(ERROR, "get block info error, ret: %d", ret); NewClientManager::get_instance().destroy_client(client); return TFS_ERROR; } //TBSYS_LOG(DEBUG, "pCode: %d", ret_msg->get_message_type()); if(ret_msg->getPCode() != SHOW_SERVER_INFORMATION_MESSAGE) { if (ret_msg->getPCode() == STATUS_MESSAGE) { StatusMessage* msg = dynamic_cast<StatusMessage*>(ret_msg); TBSYS_LOG(ERROR, "get invalid message type: error: %s", msg->get_error()); } TBSYS_LOG(ERROR, "get invalid message type, pcode: %d", ret_msg->getPCode()); NewClientManager::get_instance().destroy_client(client); return TFS_ERROR; } ShowServerInformationMessage* message = dynamic_cast<ShowServerInformationMessage*>(ret_msg); SSMScanParameter& ret_param = message->get_param(); int32_t data_len = ret_param.data_.getDataLen(); int32_t offset = 0; if (data_len > 0) { print_header(BLOCK_TYPE, type, fp); } while ((data_len > offset) && !interrupt_) { BlockShow block; if (TFS_SUCCESS == block.deserialize(ret_param.data_, data_len, offset, param.child_type_)) { //block.BlockBase::dump(); if (once && (block.info_.block_id_ != block_id)) { TBSYS_LOG(ERROR, "block(%u)(%u) not exists", block.info_.block_id_, block_id); break; } stat.add(block); block_set_.insert(block); } } std::set<BlockShow>::iterator iter = block_set_.begin(); for (; iter != block_set_.end(); iter++) { (*iter).dump(type, fp); } param.start_next_position_ = (ret_param.start_next_position_ << 16) & 0xffff0000; param.end_flag_ = ret_param.end_flag_; if (param.end_flag_ & SSM_SCAN_CUTOVER_FLAG_NO) { param.addition_param1_ = ret_param.addition_param2_; } NewClientManager::get_instance().destroy_client(client); if (once) { break; } } if (!once) { stat.dump(BLOCK_TYPE, type, fp); } if (--count) { sleep(interval); } } if (fp != stdout) fclose(fp); return TFS_SUCCESS; }
int block_process(const uint64_t ns_id, const map<uint64_t, int32_t>& family_map, StatInfo& file_stat_info, V_BLOCK_SIZE_RANGE& v_block_size_range, V_DEL_BLOCK_RANGE& v_del_block_range, BLOCK_SIZE_SET& s_big_block, const int32_t top_num, BLOCK_SIZE_SET& s_topn_block) { const int32_t num = 1000; int32_t ret = TFS_ERROR; file_stat_info.set_family_count(family_map.size()); ShowServerInformationMessage msg; SSMScanParameter& param = msg.get_param(); param.type_ = SSM_TYPE_BLOCK; param.child_type_ = SSM_CHILD_BLOCK_TYPE_INFO | SSM_CHILD_BLOCK_TYPE_SERVER; param.should_actual_count_ = (num << 16); param.end_flag_ = SSM_SCAN_CUTOVER_FLAG_YES; while (!((param.end_flag_ >> 4) & SSM_SCAN_END_FLAG_YES)) { param.data_.clear(); tbnet::Packet*ret_msg = NULL; NewClient* client = NewClientManager::get_instance().create_client(); ret = send_msg_to_server(ns_id, client, &msg, ret_msg, DEFAULT_NETWORK_CALL_TIMEOUT, true); if (TFS_SUCCESS != ret || ret_msg == NULL) { TBSYS_LOG(ERROR, "get block info error, ret: %d", ret); NewClientManager::get_instance().destroy_client(client); return TFS_ERROR; } //TBSYS_LOG(DEBUG, "pCode: %d", ret_msg->get_message_type()); if(ret_msg->getPCode() != SHOW_SERVER_INFORMATION_MESSAGE) { if (ret_msg->getPCode() == STATUS_MESSAGE) { StatusMessage* msg = dynamic_cast<StatusMessage*>(ret_msg); TBSYS_LOG(ERROR, "get invalid message type: error: %s", msg->get_error()); } TBSYS_LOG(ERROR, "get invalid message type, pcode: %d", ret_msg->getPCode()); NewClientManager::get_instance().destroy_client(client); return TFS_ERROR; } ShowServerInformationMessage* message = dynamic_cast<ShowServerInformationMessage*>(ret_msg); SSMScanParameter& ret_param = message->get_param(); int32_t data_len = ret_param.data_.getDataLen(); int32_t offset = 0; while (data_len > offset) { BlockBase block; if (TFS_SUCCESS == block.deserialize(ret_param.data_, data_len, offset, param.child_type_)) { //block.dump(); // add to file stat float ratio = static_cast<float>(block.server_list_.size()); map<uint64_t, int32_t>::const_iterator iter; if (INVALID_FAMILY_ID != block.info_.family_id_ && (iter = family_map.find(block.info_.family_id_)) != family_map.end()) { int data_member_num = GET_DATA_MEMBER_NUM(iter->second); int check_member_num = GET_CHECK_MEMBER_NUM(iter->second); ratio = StatInfo::div((data_member_num + check_member_num), data_member_num); } file_stat_info.add(block, ratio); // check block size is invalid if (IS_VERFIFY_BLOCK(block.info_.block_id_)) continue; // do range stat ret = do_stat(block.info_.size_ / M_UNITS, v_block_size_range); if (ret != TFS_SUCCESS) { TBSYS_LOG(ERROR, "some error occurs, blockid: %"PRI64_PREFIX"u, size: %d", block.info_.block_id_, block.info_.size_); } // get big block info if (top_num > 0 && block.info_.size_ >= THRESHOLD) { s_big_block.insert(BlockSize(block.info_.block_id_, block.info_.size_)); } // count del block range if (block.info_.del_size_ < 0) { TBSYS_LOG(ERROR, "some error occurs, blockid: %"PRI64_PREFIX"u, del size: %d, size: %d", block.info_.block_id_, block.info_.del_size_, block.info_.size_); } else { int32_t percent = block.info_.size_ > 0 ? static_cast<int32_t>(static_cast<int64_t>(block.info_.del_size_) * 100 / block.info_.size_) : 0; ret = do_stat(percent, v_del_block_range); if (ret != TFS_SUCCESS) { TBSYS_LOG(ERROR, "some error occurs, blockid: %"PRI64_PREFIX"u, del size: %d, size: %d", block.info_.block_id_, block.info_.del_size_, block.info_.size_); } } if (top_num > 0 && static_cast<int32_t>(s_topn_block.size()) >= top_num) { if (block.info_.size_ > s_topn_block.begin()->file_size_) { s_topn_block.erase(s_topn_block.begin()); s_topn_block.insert(BlockSize(block.info_.block_id_, block.info_.size_)); } } else { s_topn_block.insert(BlockSize(block.info_.block_id_, block.info_.size_)); } } } param.start_next_position_ = (ret_param.start_next_position_ << 16) & 0xffff0000; param.end_flag_ = ret_param.end_flag_; if (param.end_flag_ & SSM_SCAN_CUTOVER_FLAG_NO) { param.addition_param1_ = ret_param.addition_param2_; } NewClientManager::get_instance().destroy_client(client); } return TFS_SUCCESS; }
int ShowInfo::show_rack_block(const int8_t type, string& rack_ip_mask, string& rack_ip_group, const int32_t num, int32_t count, const int32_t interval, const string& filename) { interrupt_ = false; is_loop_ = (count == 0); FILE* fp = NULL; if (TFS_ERROR == get_file_handle(filename, &fp)) { return TFS_ERROR; } while ((count > 0 || is_loop_) && !interrupt_) { RackBlockShow rack_block; ShowServerInformationMessage msg; SSMScanParameter& param = msg.get_param();//参数初始化为0 param.type_ = SSM_TYPE_BLOCK; param.child_type_ = SSM_CHILD_BLOCK_TYPE_INFO | SSM_CHILD_BLOCK_TYPE_SERVER;//可以去掉INFO param.should_actual_count_ = (num << 16);//每发一次消息读取的block数取最大值 param.end_flag_ = SSM_SCAN_CUTOVER_FLAG_YES; print_header(RACK_BLOCK_TYPE, type, fp); while ((!((param.end_flag_ >> 4) & SSM_SCAN_END_FLAG_YES)) && !interrupt_) { param.data_.clear(); tbnet::Packet*ret_msg = NULL; NewClient* client = NewClientManager::get_instance().create_client(); int ret = send_msg_to_server(ns_ip_, client, &msg, ret_msg); if (TFS_SUCCESS != ret || ret_msg == NULL) { TBSYS_LOG(ERROR, "get block info error, ret: %d", ret); NewClientManager::get_instance().destroy_client(client); return TFS_ERROR; } if(ret_msg->getPCode() != SHOW_SERVER_INFORMATION_MESSAGE) { if (ret_msg->getPCode() == STATUS_MESSAGE) { StatusMessage* msg = dynamic_cast<StatusMessage*>(ret_msg); TBSYS_LOG(ERROR, "get invalid message type: error: %s", msg->get_error()); } TBSYS_LOG(ERROR, "get invalid message type, pcode: %d", ret_msg->getPCode()); NewClientManager::get_instance().destroy_client(client); return TFS_ERROR; } ShowServerInformationMessage* message = dynamic_cast<ShowServerInformationMessage*>(ret_msg); SSMScanParameter& ret_param = message->get_param(); int32_t data_len = ret_param.data_.getDataLen(); int32_t offset = 0; if (data_len > 0) { // print_header(RACK_BLOCK_TYPE, type, fp); } while ((data_len > offset) && !interrupt_) { BlockShow block; if (TFS_SUCCESS == block.deserialize(ret_param.data_, data_len, offset, param.child_type_)) { rack_block.add(block, rack_ip_mask); } } param.start_next_position_ = (ret_param.start_next_position_ << 16) & 0xffff0000; param.end_flag_ = ret_param.end_flag_; if (param.end_flag_ & SSM_SCAN_CUTOVER_FLAG_NO) { param.addition_param1_ = ret_param.addition_param2_; } NewClientManager::get_instance().destroy_client(client); } //把所有的block都拉取下来才输出 rack_block.dump(type, rack_ip_group, fp); if (--count) { sleep(interval); } } if (fp != stdout) fclose(fp); return TFS_SUCCESS; }
int ShowInfo::show_block_distribution(const int8_t type, string& rack_ip_mask, const int32_t num, const uint64_t block_id, int32_t count, const int32_t interval, const string& filename) { const int32_t block_chunk_num = nameserver::MAX_BLOCK_CHUNK_NUMS; interrupt_ = false; is_loop_ = (count == 0); FILE* fp = NULL; if (TFS_ERROR == get_file_handle(filename, &fp)) { return TFS_ERROR; } while ((count > 0 || is_loop_) && !interrupt_) { BlockDistributionStruct stat; ShowServerInformationMessage msg; SSMScanParameter& param = msg.get_param(); param.type_ = SSM_TYPE_BLOCK;//遍历ns上的block,这里设置的才是找ns取数据的类型 param.child_type_ = SSM_CHILD_BLOCK_TYPE_SERVER | SSM_CHILD_BLOCK_TYPE_INFO;//TODO:其实只取每个block的server list,但是BlockBase 反序列化接口不一致 bool once = false; if (block_id > 0) { param.should_actual_count_ = 0x10000; param.end_flag_ = SSM_SCAN_CUTOVER_FLAG_NO; param.start_next_position_ = ((block_id % block_chunk_num) << 16); param.addition_param1_ = block_id; once = true; } else { param.should_actual_count_ = (num << 16);//每次should从ns读取的最大block数 param.end_flag_ = SSM_SCAN_CUTOVER_FLAG_YES; } print_header(BLOCK_DISTRIBUTION_TYPE, type, fp); while ((!((param.end_flag_ >> 4) & SSM_SCAN_END_FLAG_YES)) && !interrupt_)//一直把ns上的所有block读取完成(除非终端中断) { param.data_.clear(); tbnet::Packet*ret_msg = NULL; NewClient* client = NewClientManager::get_instance().create_client(); int ret = send_msg_to_server(ns_ip_, client, &msg, ret_msg); if (TFS_SUCCESS != ret || ret_msg == NULL) { TBSYS_LOG(ERROR, "get block info error, ret: %d", ret); NewClientManager::get_instance().destroy_client(client); return TFS_ERROR; } if(ret_msg->getPCode() != SHOW_SERVER_INFORMATION_MESSAGE) { if (ret_msg->getPCode() == STATUS_MESSAGE) { StatusMessage* msg = dynamic_cast<StatusMessage*>(ret_msg); TBSYS_LOG(ERROR, "get invalid message type: error: %s", msg->get_error()); } TBSYS_LOG(ERROR, "get invalid message type, pcode: %d", ret_msg->getPCode()); NewClientManager::get_instance().destroy_client(client); return TFS_ERROR; } ShowServerInformationMessage* message = dynamic_cast<ShowServerInformationMessage*>(ret_msg); SSMScanParameter& ret_param = message->get_param(); int32_t data_len = ret_param.data_.getDataLen(); int32_t offset = 0; // if (data_len > 0) // { // print_header(BLOCK_DISTRIBUTION_TYPE, type, fp); // } //如果指定的blockid都不存在,且block表中>=start_next_position_桶后没有blockid,则拉取block个数为0 if(type & BLOCK_IP_DISTRIBUTION_TYPE) { while ((data_len > offset) && !interrupt_) { BlockDistributionShow block; if (TFS_SUCCESS == block.deserialize(ret_param.data_, data_len, offset, param.child_type_)) { if (once && (block.info_.block_id_ != block_id)) { TBSYS_LOG(ERROR, "block: %"PRI64_PREFIX"u,%"PRI64_PREFIX"u not exists", block.info_.block_id_, block_id); break; } if(block.check_block_ip_distribution()) { block.dump_ip(fp); stat.add(block); } } } } else { assert( (type & BLOCK_RACK_DISTRIBUTION_TYPE) ); while ((data_len > offset) && !interrupt_) { BlockDistributionShow block; if (TFS_SUCCESS == block.deserialize(ret_param.data_, data_len, offset, param.child_type_)) { if (once && (block.info_.block_id_ != block_id)) { TBSYS_LOG(ERROR, "block: %"PRI64_PREFIX"u,%"PRI64_PREFIX"u not exists", block.info_.block_id_, block_id); break; } if(block.check_block_rack_distribution(rack_ip_mask)) { block.dump_rack(fp); stat.add(block); } } } } param.start_next_position_ = (ret_param.start_next_position_ << 16) & 0xffff0000; param.end_flag_ = ret_param.end_flag_; if (param.end_flag_ & SSM_SCAN_CUTOVER_FLAG_NO) { param.addition_param1_ = ret_param.addition_param2_; } NewClientManager::get_instance().destroy_client(client); if (once) { break; } } if (!once) { stat.dump(type, fp); } if (--count) { sleep(interval); } } if (fp != stdout) fclose(fp); return TFS_SUCCESS; }