xy720 commented on code in PR #47300: URL: https://github.com/apache/doris/pull/47300#discussion_r2227342609
########## be/src/cloud/cloud_snapshot_mgr.cpp: ########## @@ -0,0 +1,291 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "cloud/cloud_snapshot_mgr.h" + +#include <fmt/format.h> +#include <gen_cpp/olap_file.pb.h> + +#include <map> +#include <unordered_map> + +#include "cloud/cloud_meta_mgr.h" +#include "cloud/cloud_storage_engine.h" +#include "cloud/cloud_tablet_mgr.h" +#include "common/config.h" +#include "common/logging.h" +#include "common/status.h" +#include "io/fs/local_file_system.h" +#include "olap/data_dir.h" +#include "olap/olap_common.h" +#include "olap/olap_define.h" +#include "olap/pb_helper.h" +#include "olap/rowset/rowset.h" +#include "olap/rowset/rowset_factory.h" +#include "olap/rowset/rowset_meta.h" +#include "olap/rowset/rowset_writer.h" +#include "olap/rowset/rowset_writer_context.h" +#include "olap/storage_policy.h" +#include "olap/tablet_meta.h" +#include "olap/tablet_schema.h" +#include "olap/tablet_schema_cache.h" +#include "olap/utils.h" +#include "runtime/memory/mem_tracker_limiter.h" +#include "runtime/thread_context.h" +#include "util/slice.h" +#include "util/uid_util.h" + +namespace doris { +using namespace ErrorCode; + +CloudSnapshotMgr::CloudSnapshotMgr(CloudStorageEngine& engine) : _engine(engine) { + _mem_tracker = + MemTrackerLimiter::create_shared(MemTrackerLimiter::Type::OTHER, "CloudSnapshotMgr"); +} + +Status CloudSnapshotMgr::make_snapshot(int64_t target_tablet_id, StorageResource& storage_resource, + std::unordered_map<std::string, std::string>& file_mapping, + bool is_restore, const Slice* slice) { + SCOPED_ATTACH_TASK(_mem_tracker); + if (is_restore && slice == nullptr) { + return Status::Error<INVALID_ARGUMENT>("slice cannot be null in restore."); + } + + CloudTabletSPtr target_tablet = DORIS_TRY(_engine.tablet_mgr().get_tablet(target_tablet_id)); + if (target_tablet == nullptr) { + return Status::Error<TABLE_NOT_FOUND>("failed to get tablet. tablet={}", target_tablet_id); + } + + TabletMeta tablet_meta; + if (is_restore) { + // 1. deserialize tablet meta from memory + RETURN_IF_ERROR(tablet_meta.create_from_buffer((const uint8_t*)slice->data, slice->size)); + TabletMetaPB tablet_meta_pb; + tablet_meta.to_meta_pb(&tablet_meta_pb); + + tablet_meta_pb.clear_rs_metas(); // copy the rs meta + if (tablet_meta.all_rs_metas().size() > 0) { + tablet_meta_pb.mutable_inc_rs_metas()->Reserve(tablet_meta.all_rs_metas().size()); + for (auto& rs : tablet_meta.all_rs_metas()) { + rs->to_rowset_pb(tablet_meta_pb.add_rs_metas()); + } + } + tablet_meta_pb.clear_stale_rs_metas(); // copy the stale rs meta + if (tablet_meta.all_stale_rs_metas().size() > 0) { + tablet_meta_pb.mutable_stale_rs_metas()->Reserve( + tablet_meta.all_stale_rs_metas().size()); + for (auto& rs : tablet_meta.all_stale_rs_metas()) { + rs->to_rowset_pb(tablet_meta_pb.add_stale_rs_metas()); + } + } + + // 2. convert rowsets + TabletMetaPB new_tablet_meta_pb; + RETURN_IF_ERROR(convert_rowsets(&new_tablet_meta_pb, tablet_meta_pb, target_tablet_id, + target_tablet, storage_resource, file_mapping)); + + // 3. send make snapshot request + RETURN_IF_ERROR(_engine.meta_mgr().make_snapshot(new_tablet_meta_pb, true)); + return Status::OK(); + } + + // backup not implemented + + LOG(INFO) << "success to make snapshot. [tablet_id=" << target_tablet_id << "]"; + return Status::OK(); +} + +Status CloudSnapshotMgr::commit_snapshot(int64_t tablet_id) { + SCOPED_ATTACH_TASK(_mem_tracker); + CloudTabletSPtr tablet = DORIS_TRY(_engine.tablet_mgr().get_tablet(tablet_id)); + if (tablet == nullptr) { + return Status::Error<TABLE_NOT_FOUND>("failed to get tablet. tablet={}", tablet_id); + } + RETURN_IF_ERROR(_engine.meta_mgr().commit_snapshot(tablet_id)); + tablet->clear_cache(); + LOG(INFO) << "success to commit snapshot. [tablet_id=" << tablet_id << "]"; + return Status::OK(); +} + +Status CloudSnapshotMgr::release_snapshot(int64_t tablet_id) { + SCOPED_ATTACH_TASK(_mem_tracker); + RETURN_IF_ERROR(_engine.meta_mgr().release_snapshot(tablet_id)); + LOG(INFO) << "success to release snapshot. [tablet_id=" << tablet_id << "]"; + return Status::OK(); +} + +Status CloudSnapshotMgr::convert_rowsets( + TabletMetaPB* out, const TabletMetaPB& in, int64_t tablet_id, + CloudTabletSPtr& target_tablet, StorageResource& storage_resource, + std::unordered_map<std::string, std::string>& file_mapping) { + SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_mem_tracker); + // deep copy + *out = in; + + out->clear_rs_metas(); + out->clear_inc_rs_metas(); + out->clear_stale_rs_metas(); + // modify id + out->set_tablet_id(tablet_id); + *out->mutable_tablet_uid() = TabletUid::gen_uid().to_proto(); + out->set_table_id(target_tablet->table_id()); + out->set_partition_id(target_tablet->partition_id()); + out->set_index_id(target_tablet->index_id()); + PUniqueId* cooldown_meta_id = out->mutable_cooldown_meta_id(); + cooldown_meta_id->set_hi(0); + cooldown_meta_id->set_lo(0); + + TabletSchemaSPtr tablet_schema = std::make_shared<TabletSchema>(); + tablet_schema->init_from_pb(in.schema()); + + std::unordered_map<Version, RowsetMetaPB*, HashOfVersion> rs_version_map; + std::unordered_map<RowsetId, RowsetId> rowset_id_mapping; + for (auto&& rowset_meta_pb : in.rs_metas()) { + RowsetMetaPB* new_rowset_meta_pb = out->add_rs_metas(); + RETURN_IF_ERROR(_create_rowset_meta(new_rowset_meta_pb, rowset_meta_pb, tablet_id, + target_tablet, storage_resource, tablet_schema, + file_mapping, rowset_id_mapping)); + Version rowset_version = {rowset_meta_pb.start_version(), rowset_meta_pb.end_version()}; + rs_version_map[rowset_version] = new_rowset_meta_pb; + } + + for (auto&& stale_rowset_pb : in.stale_rs_metas()) { + Version rowset_version = {stale_rowset_pb.start_version(), stale_rowset_pb.end_version()}; + auto exist_rs = rs_version_map.find(rowset_version); + if (exist_rs != rs_version_map.end()) { + continue; + } + RowsetMetaPB* new_rowset_meta_pb = out->add_stale_rs_metas(); + RETURN_IF_ERROR(_create_rowset_meta(new_rowset_meta_pb, stale_rowset_pb, tablet_id, + target_tablet, storage_resource, tablet_schema, + file_mapping, rowset_id_mapping)); + } + + if (!rowset_id_mapping.empty() && in.has_delete_bitmap()) { Review Comment: TODO:这里还有问题 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
