int check_all_block_in_disk(const char* ns_addr, const char* ns_slave_addr, const uint64_t ds_id, const VUINT64& blocks, VUINT64& need_sync_block_list, VUINT64& lost_block_list) { int ret = TFS_SUCCESS; uint64_t ns_id = Func::get_host_ip(ns_addr); uint64_t slave_ns_id = Func::get_host_ip(ns_slave_addr); need_sync_block_list.clear(); lost_block_list.clear(); // check current cluster's block in blocks array ret = get_and_check_all_blocks_copy(ns_id, blocks, need_sync_block_list, ds_id); if (TFS_SUCCESS == ret && need_sync_block_list.size() > 0) { // check slave cluster's block in need_sync_block_list array VUINT64 raw_lost_blocks; ret = get_and_check_all_blocks_copy(slave_ns_id, need_sync_block_list, raw_lost_blocks); // raw_lost_blocks need unique, then remove all blocks which not in need_sync_block_list set<uint64_t> need_sync_block_set, lost_block_set; need_sync_block_set.insert(need_sync_block_list.begin(), need_sync_block_list.end()); // to sort lost_block_set.insert(raw_lost_blocks.begin(), raw_lost_blocks.end()); // to sort & unique lost_block_list.resize(raw_lost_blocks.size()); VUINT64::iterator it = set_intersection(raw_lost_blocks.begin(), raw_lost_blocks.end(), need_sync_block_list.begin(), need_sync_block_list.end(), lost_block_list.begin()); lost_block_list.resize(it - lost_block_list.begin());// set real size } return ret; }
void print_result(const VUINT64& need_sync_block_list, const VUINT64& lost_block_list) { if (lost_block_list.size() > 0) { fprintf(stdout, "BOTH LOST BLOCK COUNT: %zd\n", lost_block_list.size()); for (uint32_t i = 0; i < lost_block_list.size(); i++) { fprintf(stdout, "%" PRI64_PREFIX "u\n", lost_block_list.at(i)); } } int32_t real_need_sync_block_size = need_sync_block_list.size() - lost_block_list.size(); if (real_need_sync_block_size > 0) { fprintf(stdout, "NEED SYNC BLOCK COUNT: %d\n", real_need_sync_block_size); set<uint64_t> lost_block_set; lost_block_set.insert(lost_block_list.begin(), lost_block_list.end()); for (uint32_t i = 0; i < need_sync_block_list.size(); i++) { uint64_t block_id = need_sync_block_list.at(i); if (lost_block_set.find(block_id) == lost_block_set.end()) { fprintf(stdout, "%" PRI64_PREFIX "u\n", block_id);// can sync from slave cluster } } } }
/* * get file info from every replica * return value don't needed, we just need finfos */ int read_file_info(const VUINT64& replicas, const uint64_t block_id, const uint64_t file_id, map<uint64_t, FileInfo>& finfos) { finfos.clear(); int ret = TFS_SUCCESS; VUINT64::const_iterator iter = replicas.begin(); for ( ; iter != replicas.end(); iter++) { int32_t retry = 2; while (retry--) { FileInfo info; ret = ToolUtil::read_file_info(*iter, block_id, file_id, FORCE_STAT, info); if (TFS_SUCCESS == ret) { finfos.insert(make_pair(*iter, info)); break; } else if (EXIT_META_NOT_FOUND_ERROR == ret) // file not exist, just ignore { break; } } } return (finfos.size() > 0) ? TFS_SUCCESS : TFS_ERROR; }
void recover_block_from_slave_cluster(const char* ns_addr, const char* ns_slave_addr, const VUINT64& tmp_fail_block_list, VUINT64& success_block_list, VUINT64& fail_block_list, multimap<uint64_t, uint64_t>& fail_block_file_list) { int ret = TFS_SUCCESS; VUINT64::const_iterator vit = tmp_fail_block_list.begin(); multimap<uint64_t, uint64_t> tmp_fail_block_file_list; vector<FileInfoV2> finfos; for (; vit != tmp_fail_block_list.end(); vit++) { uint64_t block_id = (*vit); finfos.clear(); ret = ToolUtil::read_file_infos_v2(Func::get_host_ip(ns_slave_addr), block_id, finfos); if (ret == TFS_SUCCESS) { bool all_success = true; int32_t copy_file_succ_count = 0; tmp_fail_block_file_list.clear(); vector<FileInfoV2>::const_iterator v_file_iter = finfos.begin(); for (; v_file_iter != finfos.end(); v_file_iter++) { if ((v_file_iter->status_ & FILE_STATUS_DELETE) != 0) continue; uint64_t file_id = v_file_iter->id_; ret = copy_file_from_slave_cluster(ns_slave_addr, ns_addr, block_id, (*v_file_iter)); if (TFS_SUCCESS == ret) { ++copy_file_succ_count; TBSYS_LOG(DEBUG, "recover block_id: %" PRI64_PREFIX "u, file_id: %" PRI64_PREFIX "u successful from slave cluster!", block_id, file_id); } else { TBSYS_LOG(WARN, "recover block_id: %" PRI64_PREFIX "u, file_id: %" PRI64_PREFIX "u failed from slave cluster, ret: %d", block_id, file_id, ret); all_success = false; tmp_fail_block_file_list.insert(pair<uint64_t, uint64_t>(block_id, file_id)); } } if (all_success) { success_block_list.push_back(block_id); } else if (0 == copy_file_succ_count) { fail_block_list.push_back(block_id); } else { fail_block_file_list.insert(tmp_fail_block_file_list.begin(), tmp_fail_block_file_list.end()); } } else { fail_block_list.push_back(block_id); } } }
// if parameter: ds_id != INVALID_SERVER_ID, check from current cluster, need exclude repliates in this ds; // if parameter: ds_id == INVALID_SERVER_ID, check from slave cluster int get_and_check_all_blocks_copy(const uint64_t ns_id, const VUINT64& blocks, VUINT64& no_copy_blocks, const uint64_t ds_id = INVALID_SERVER_ID) { no_copy_blocks.clear(); vector<BlockMeta> blocks_meta; int ret = ToolUtil::get_all_blocks_meta(ns_id, blocks, blocks_meta, true); if (TFS_SUCCESS == ret) { VUINT64 lost_in_family; for (uint32_t block_index = 0; block_index < blocks_meta.size(); ++block_index) { // blk_meta.result_ just be used in trunk(stable2.7) BlockMeta blk_meta = blocks_meta.at(block_index); uint64_t block_id = blk_meta.block_id_; FamilyInfoExt family_info = blk_meta.family_info_; if (INVALID_FAMILY_ID == family_info.family_id_) { // if family can't reinstall, family_id here is also invalid if (INVALID_BLOCK_ID == block_id || IS_VERFIFY_BLOCK(block_id)) continue; // else family data block regard as normal block if (0 == blk_meta.size_) { // maybe not exist the block no_copy_blocks.push_back(block_id); } else if (INVALID_SERVER_ID != ds_id && 1 == blk_meta.size_ && ds_id == blk_meta.ds_[0]) { // only exist one copy and be positioned in this ds no_copy_blocks.push_back(block_id); } } else // block is marshalling { int data_num = GET_DATA_MEMBER_NUM(family_info.family_aid_info_); int member_num = data_num + GET_CHECK_MEMBER_NUM(family_info.family_aid_info_); int32_t alive_num = 0; lost_in_family.clear(); for (int i = 0; i < member_num; ++i) { // pair is <blockid, ds_id> pair<uint64_t, uint64_t>& item = family_info.members_[i]; if (INVALID_SERVER_ID != item.second && item.second != ds_id) // exclude current ds { ++alive_num; } else { // keep all lost(and will lost) data blockid if (!IS_VERFIFY_BLOCK(item.first) && INVALID_BLOCK_ID != item.first) { lost_in_family.push_back(item.first); } } } if (alive_num < data_num) // can't reinstate { no_copy_blocks.insert(no_copy_blocks.end(), lost_in_family.begin(), lost_in_family.end()); } } } } else { // this error need script-calller care TBSYS_LOG(ERROR, "get blockis ds_list fail from ns: %s, ret:%d", tbsys::CNetUtil::addrToString(ns_id).c_str(), ret); } return ret; }