/root/doris/be/src/olap/snapshot_manager.h
| Line | Count | Source | 
| 1 |  | // Licensed to the Apache Software Foundation (ASF) under one | 
| 2 |  | // or more contributor license agreements.  See the NOTICE file | 
| 3 |  | // distributed with this work for additional information | 
| 4 |  | // regarding copyright ownership.  The ASF licenses this file | 
| 5 |  | // to you under the Apache License, Version 2.0 (the | 
| 6 |  | // "License"); you may not use this file except in compliance | 
| 7 |  | // with the License.  You may obtain a copy of the License at | 
| 8 |  | // | 
| 9 |  | //   http://www.apache.org/licenses/LICENSE-2.0 | 
| 10 |  | // | 
| 11 |  | // Unless required by applicable law or agreed to in writing, | 
| 12 |  | // software distributed under the License is distributed on an | 
| 13 |  | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
| 14 |  | // KIND, either express or implied.  See the License for the | 
| 15 |  | // specific language governing permissions and limitations | 
| 16 |  | // under the License. | 
| 17 |  |  | 
| 18 |  | #pragma once | 
| 19 |  |  | 
| 20 |  | #include <condition_variable> | 
| 21 |  | #include <memory> | 
| 22 |  | #include <mutex> | 
| 23 |  | #include <string> | 
| 24 |  | #include <vector> | 
| 25 |  |  | 
| 26 |  | #include "common/status.h" | 
| 27 |  | #include "olap/rowset/pending_rowset_helper.h" | 
| 28 |  | #include "olap/rowset/rowset_fwd.h" | 
| 29 |  | #include "olap/tablet_fwd.h" | 
| 30 |  |  | 
| 31 |  | namespace doris { | 
| 32 |  | class RowsetMetaPB; | 
| 33 |  | class TSnapshotRequest; | 
| 34 |  | struct RowsetId; | 
| 35 |  | class StorageEngine; | 
| 36 |  | class MemTrackerLimiter; | 
| 37 |  |  | 
| 38 |  | class LocalSnapshotLockGuard; | 
| 39 |  |  | 
| 40 |  | // A simple lock to protect the local snapshot path. | 
| 41 |  | class LocalSnapshotLock { | 
| 42 |  |     friend class LocalSnapshotLockGuard; | 
| 43 |  |  | 
| 44 |  | public: | 
| 45 | 1 |     LocalSnapshotLock() = default; | 
| 46 | 1 |     ~LocalSnapshotLock() = default; | 
| 47 |  |     LocalSnapshotLock(const LocalSnapshotLock&) = delete; | 
| 48 |  |     LocalSnapshotLock& operator=(const LocalSnapshotLock&) = delete; | 
| 49 |  |  | 
| 50 | 6 |     static LocalSnapshotLock& instance() { | 
| 51 | 6 |         static LocalSnapshotLock instance; | 
| 52 | 6 |         return instance; | 
| 53 | 6 |     } | 
| 54 |  |  | 
| 55 |  |     // Acquire the lock for the specified path. It will block if the lock is already held by another. | 
| 56 |  |     LocalSnapshotLockGuard acquire(const std::string& path); | 
| 57 |  |  | 
| 58 |  | private: | 
| 59 |  |     void release(const std::string& path); | 
| 60 |  |  | 
| 61 |  |     class LocalSnapshotContext { | 
| 62 |  |     public: | 
| 63 |  |         bool _is_locked = false; | 
| 64 |  |         size_t _waiting_count = 0; | 
| 65 |  |         std::condition_variable _cv; | 
| 66 |  |  | 
| 67 | 3 |         LocalSnapshotContext() = default; | 
| 68 |  |         LocalSnapshotContext(const LocalSnapshotContext&) = delete; | 
| 69 |  |         LocalSnapshotContext& operator=(const LocalSnapshotContext&) = delete; | 
| 70 |  |     }; | 
| 71 |  |  | 
| 72 |  |     std::mutex _lock; | 
| 73 |  |     std::unordered_map<std::string, LocalSnapshotContext> _local_snapshot_contexts; | 
| 74 |  | }; | 
| 75 |  |  | 
| 76 |  | class LocalSnapshotLockGuard { | 
| 77 |  | public: | 
| 78 | 3 |     LocalSnapshotLockGuard(std::string path) : _snapshot_path(std::move(path)) {} | 
| 79 |  |     LocalSnapshotLockGuard(const LocalSnapshotLockGuard&) = delete; | 
| 80 |  |     LocalSnapshotLockGuard& operator=(const LocalSnapshotLockGuard&) = delete; | 
| 81 | 3 |     ~LocalSnapshotLockGuard() { LocalSnapshotLock::instance().release(_snapshot_path); } | 
| 82 |  |  | 
| 83 |  | private: | 
| 84 |  |     std::string _snapshot_path; | 
| 85 |  | }; | 
| 86 |  |  | 
| 87 |  | class SnapshotManager { | 
| 88 |  | public: | 
| 89 |  |     SnapshotManager(StorageEngine& engine); | 
| 90 |  |     ~SnapshotManager(); | 
| 91 |  |  | 
| 92 |  |     /// Create a snapshot | 
| 93 |  |     /// snapshot_path: out param, the dir of snapshot | 
| 94 |  |     /// allow_incremental_clone: out param, true if it is an incremental clone | 
| 95 |  |     Status make_snapshot(const TSnapshotRequest& request, std::string* snapshot_path, | 
| 96 |  |                          bool* allow_incremental_clone); | 
| 97 |  |  | 
| 98 |  |     std::string static get_schema_hash_full_path(const TabletSharedPtr& ref_tablet, | 
| 99 |  |                                                  const std::string& prefix); | 
| 100 |  |  | 
| 101 |  |     // @brief 释放snapshot | 
| 102 |  |     // @param snapshot_path [in] 要被释放的snapshot的路径,只包含到ID | 
| 103 |  |     Status release_snapshot(const std::string& snapshot_path); | 
| 104 |  |  | 
| 105 |  |     Result<std::vector<PendingRowsetGuard>> convert_rowset_ids(const std::string& clone_dir, | 
| 106 |  |                                                                int64_t tablet_id, | 
| 107 |  |                                                                int64_t replica_id, int64_t table_id, | 
| 108 |  |                                                                int64_t partition_id, | 
| 109 |  |                                                                int32_t schema_hash); | 
| 110 |  |  | 
| 111 |  | private: | 
| 112 |  |     Status _calc_snapshot_id_path(const TabletSharedPtr& tablet, int64_t timeout_s, | 
| 113 |  |                                   std::string* out_path); | 
| 114 |  |  | 
| 115 |  |     std::string _get_header_full_path(const TabletSharedPtr& ref_tablet, | 
| 116 |  |                                       const std::string& schema_hash_path) const; | 
| 117 |  |  | 
| 118 |  |     std::string _get_json_header_full_path(const TabletSharedPtr& ref_tablet, | 
| 119 |  |                                            const std::string& schema_hash_path) const; | 
| 120 |  |  | 
| 121 |  |     Status _link_index_and_data_files(const std::string& header_path, | 
| 122 |  |                                       const TabletSharedPtr& ref_tablet, | 
| 123 |  |                                       const std::vector<RowsetSharedPtr>& consistent_rowsets); | 
| 124 |  |  | 
| 125 |  |     Status _create_snapshot_files(const TabletSharedPtr& ref_tablet, | 
| 126 |  |                                   const TabletSharedPtr& target_tablet, | 
| 127 |  |                                   const TSnapshotRequest& request, std::string* snapshot_path, | 
| 128 |  |                                   bool* allow_incremental_clone); | 
| 129 |  |  | 
| 130 |  |     Status _prepare_snapshot_dir(const TabletSharedPtr& ref_tablet, std::string* snapshot_id_path); | 
| 131 |  |  | 
| 132 |  |     Status _rename_rowset_id(const RowsetMetaPB& rs_meta_pb, const std::string& new_tablet_path, | 
| 133 |  |                              TabletSchemaSPtr tablet_schema, const RowsetId& next_id, | 
| 134 |  |                              RowsetMetaPB* new_rs_meta_pb); | 
| 135 |  |  | 
| 136 |  |     StorageEngine& _engine; | 
| 137 |  |     std::atomic<uint64_t> _snapshot_base_id {0}; | 
| 138 |  |  | 
| 139 |  |     std::shared_ptr<MemTrackerLimiter> _mem_tracker; | 
| 140 |  | }; // SnapshotManager | 
| 141 |  |  | 
| 142 |  | } // namespace doris |