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_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_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; }