/root/doris/be/src/olap/snapshot_manager.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | #include "olap/snapshot_manager.h" |
19 | | |
20 | | #include <fmt/format.h> |
21 | | #include <gen_cpp/AgentService_types.h> |
22 | | #include <gen_cpp/Types_constants.h> |
23 | | #include <gen_cpp/olap_file.pb.h> |
24 | | #include <thrift/protocol/TDebugProtocol.h> |
25 | | |
26 | | #include <algorithm> |
27 | | #include <ctime> |
28 | | #include <filesystem> |
29 | | #include <list> |
30 | | #include <map> |
31 | | #include <new> |
32 | | #include <ostream> |
33 | | #include <set> |
34 | | #include <shared_mutex> |
35 | | #include <unordered_map> |
36 | | #include <utility> |
37 | | |
38 | | #include "common/config.h" |
39 | | #include "common/logging.h" |
40 | | #include "common/status.h" |
41 | | #include "io/fs/local_file_system.h" |
42 | | #include "olap/data_dir.h" |
43 | | #include "olap/olap_common.h" |
44 | | #include "olap/olap_define.h" |
45 | | #include "olap/pb_helper.h" |
46 | | #include "olap/rowset/rowset.h" |
47 | | #include "olap/rowset/rowset_factory.h" |
48 | | #include "olap/rowset/rowset_meta.h" |
49 | | #include "olap/rowset/rowset_writer.h" |
50 | | #include "olap/rowset/rowset_writer_context.h" |
51 | | #include "olap/storage_engine.h" |
52 | | #include "olap/tablet_manager.h" |
53 | | #include "olap/tablet_meta.h" |
54 | | #include "olap/tablet_schema.h" |
55 | | #include "olap/tablet_schema_cache.h" |
56 | | #include "olap/utils.h" |
57 | | #include "runtime/thread_context.h" |
58 | | #include "util/uid_util.h" |
59 | | |
60 | | using std::nothrow; |
61 | | using std::string; |
62 | | using std::stringstream; |
63 | | using std::vector; |
64 | | |
65 | | namespace doris { |
66 | | using namespace ErrorCode; |
67 | | |
68 | | SnapshotManager* SnapshotManager::_s_instance = nullptr; |
69 | | std::mutex SnapshotManager::_mlock; |
70 | | |
71 | 0 | LocalSnapshotLockGuard LocalSnapshotLock::acquire(const std::string& path) { |
72 | 0 | std::unique_lock<std::mutex> l(_lock); |
73 | 0 | auto& ctx = _local_snapshot_contexts[path]; |
74 | 0 | while (ctx._is_locked) { |
75 | 0 | ctx._waiting_count++; |
76 | 0 | ctx._cv.wait(l); |
77 | 0 | ctx._waiting_count--; |
78 | 0 | } |
79 | |
|
80 | 0 | ctx._is_locked = true; |
81 | 0 | return {path}; |
82 | 0 | } |
83 | | |
84 | 0 | void LocalSnapshotLock::release(const std::string& path) { |
85 | 0 | std::lock_guard<std::mutex> l(_lock); |
86 | 0 | auto iter = _local_snapshot_contexts.find(path); |
87 | 0 | if (iter == _local_snapshot_contexts.end()) { |
88 | 0 | return; |
89 | 0 | } |
90 | | |
91 | 0 | auto& ctx = iter->second; |
92 | 0 | ctx._is_locked = false; |
93 | 0 | if (ctx._waiting_count > 0) { |
94 | 0 | ctx._cv.notify_one(); |
95 | 0 | } else { |
96 | 0 | _local_snapshot_contexts.erase(iter); |
97 | 0 | } |
98 | 0 | } |
99 | | |
100 | 2 | SnapshotManager* SnapshotManager::instance() { |
101 | 2 | if (_s_instance == nullptr) { |
102 | 1 | std::lock_guard<std::mutex> lock(_mlock); |
103 | 1 | if (_s_instance == nullptr) { |
104 | 1 | _s_instance = new SnapshotManager(); |
105 | 1 | } |
106 | 1 | } |
107 | 2 | return _s_instance; |
108 | 2 | } |
109 | | |
110 | | Status SnapshotManager::make_snapshot(const TSnapshotRequest& request, string* snapshot_path, |
111 | 0 | bool* allow_incremental_clone) { |
112 | 0 | SCOPED_ATTACH_TASK(_mem_tracker); |
113 | 0 | Status res = Status::OK(); |
114 | 0 | if (snapshot_path == nullptr) { |
115 | 0 | return Status::Error<INVALID_ARGUMENT>("output parameter cannot be null"); |
116 | 0 | } |
117 | | |
118 | 0 | TabletSharedPtr target_tablet = |
119 | 0 | StorageEngine::instance()->tablet_manager()->get_tablet(request.tablet_id); |
120 | |
|
121 | 0 | DBUG_EXECUTE_IF("SnapshotManager::make_snapshot.inject_failure", { target_tablet = nullptr; }) |
122 | |
|
123 | 0 | if (target_tablet == nullptr) { |
124 | 0 | return Status::Error<TABLE_NOT_FOUND>("failed to get tablet. tablet={}", request.tablet_id); |
125 | 0 | } |
126 | | |
127 | 0 | TabletSharedPtr ref_tablet = target_tablet; |
128 | 0 | if (request.__isset.ref_tablet_id) { |
129 | 0 | int64_t ref_tablet_id = request.ref_tablet_id; |
130 | 0 | TabletSharedPtr base_tablet = |
131 | 0 | StorageEngine::instance()->tablet_manager()->get_tablet(ref_tablet_id); |
132 | | |
133 | | // Some tasks, like medium migration, cause the target tablet and base tablet to stay on |
134 | | // different disks. In this case, we fall through to the normal restore path. |
135 | | // |
136 | | // Otherwise, we can directly link the rowset files from the base tablet to the target tablet. |
137 | 0 | if (base_tablet != nullptr && |
138 | 0 | base_tablet->data_dir()->path() == target_tablet->data_dir()->path()) { |
139 | 0 | ref_tablet = std::move(base_tablet); |
140 | 0 | } |
141 | 0 | } |
142 | |
|
143 | 0 | res = _create_snapshot_files(ref_tablet, target_tablet, request, snapshot_path, |
144 | 0 | allow_incremental_clone); |
145 | |
|
146 | 0 | if (!res.ok()) { |
147 | 0 | LOG(WARNING) << "failed to make snapshot. res=" << res << " tablet=" << request.tablet_id; |
148 | 0 | return res; |
149 | 0 | } |
150 | | |
151 | 0 | LOG(INFO) << "success to make snapshot. path=['" << *snapshot_path << "']"; |
152 | 0 | return res; |
153 | 0 | } |
154 | | |
155 | 0 | Status SnapshotManager::release_snapshot(const string& snapshot_path) { |
156 | 0 | auto local_snapshot_guard = LocalSnapshotLock::instance().acquire(snapshot_path); |
157 | | |
158 | | // If the requested snapshot_path is located in the root/snapshot folder, it is considered legal and can be deleted. |
159 | | // Otherwise, it is considered an illegal request and returns an error result. |
160 | 0 | SCOPED_ATTACH_TASK(_mem_tracker); |
161 | 0 | auto stores = StorageEngine::instance()->get_stores(); |
162 | 0 | for (auto store : stores) { |
163 | 0 | std::string abs_path; |
164 | 0 | RETURN_IF_ERROR(io::global_local_filesystem()->canonicalize(store->path(), &abs_path)); |
165 | 0 | if (snapshot_path.compare(0, abs_path.size(), abs_path) == 0 && |
166 | 0 | snapshot_path.compare(abs_path.size() + 1, SNAPSHOT_PREFIX.size(), SNAPSHOT_PREFIX) == |
167 | 0 | 0) { |
168 | 0 | RETURN_IF_ERROR(io::global_local_filesystem()->delete_directory(snapshot_path)); |
169 | 0 | LOG(INFO) << "success to release snapshot path. [path='" << snapshot_path << "']"; |
170 | 0 | return Status::OK(); |
171 | 0 | } |
172 | 0 | } |
173 | | |
174 | 0 | return Status::Error<CE_CMD_PARAMS_ERROR>("released snapshot path illegal. [path='{}']", |
175 | 0 | snapshot_path); |
176 | 0 | } |
177 | | |
178 | | Result<std::vector<PendingRowsetGuard>> SnapshotManager::convert_rowset_ids( |
179 | | const std::string& clone_dir, int64_t tablet_id, int64_t replica_id, int64_t partition_id, |
180 | 2 | int32_t schema_hash) { |
181 | 2 | SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_mem_tracker); |
182 | 2 | std::vector<PendingRowsetGuard> guards; |
183 | | // check clone dir existed |
184 | 2 | bool exists = true; |
185 | 2 | RETURN_IF_ERROR_RESULT(io::global_local_filesystem()->exists(clone_dir, &exists)); |
186 | 2 | if (!exists) { |
187 | 0 | return unexpected(Status::Error<DIR_NOT_EXIST>( |
188 | 0 | "clone dir not existed when convert rowsetids. clone_dir={}", clone_dir)); |
189 | 0 | } |
190 | | |
191 | | // load original tablet meta |
192 | 2 | auto cloned_meta_file = fmt::format("{}/{}.hdr", clone_dir, tablet_id); |
193 | 2 | TabletMeta cloned_tablet_meta; |
194 | 2 | RETURN_IF_ERROR_RESULT(cloned_tablet_meta.create_from_file(cloned_meta_file)); |
195 | 2 | TabletMetaPB cloned_tablet_meta_pb; |
196 | 2 | cloned_tablet_meta.to_meta_pb(&cloned_tablet_meta_pb); |
197 | | |
198 | 2 | TabletMetaPB new_tablet_meta_pb; |
199 | 2 | new_tablet_meta_pb = cloned_tablet_meta_pb; |
200 | 2 | new_tablet_meta_pb.clear_rs_metas(); |
201 | | // inc_rs_meta is deprecated since 0.13. |
202 | | // keep this just for safety |
203 | 2 | new_tablet_meta_pb.clear_inc_rs_metas(); |
204 | 2 | new_tablet_meta_pb.clear_stale_rs_metas(); |
205 | | // should modify tablet id and schema hash because in restore process the tablet id is not |
206 | | // equal to tablet id in meta |
207 | 2 | new_tablet_meta_pb.set_tablet_id(tablet_id); |
208 | 2 | *new_tablet_meta_pb.mutable_tablet_uid() = TabletUid::gen_uid().to_proto(); |
209 | 2 | new_tablet_meta_pb.set_replica_id(replica_id); |
210 | 2 | if (partition_id != -1) { |
211 | 2 | new_tablet_meta_pb.set_partition_id(partition_id); |
212 | 2 | } |
213 | 2 | new_tablet_meta_pb.set_schema_hash(schema_hash); |
214 | 2 | TabletSchemaSPtr tablet_schema = std::make_shared<TabletSchema>(); |
215 | 2 | tablet_schema->init_from_pb(new_tablet_meta_pb.schema()); |
216 | | |
217 | 2 | std::unordered_map<Version, RowsetMetaPB*, HashOfVersion> rs_version_map; |
218 | 2 | std::unordered_map<RowsetId, RowsetId> rowset_id_mapping; |
219 | 2 | guards.reserve(cloned_tablet_meta_pb.rs_metas_size() + |
220 | 2 | cloned_tablet_meta_pb.stale_rs_metas_size()); |
221 | 4 | for (auto&& visible_rowset : cloned_tablet_meta_pb.rs_metas()) { |
222 | 4 | RowsetMetaPB* rowset_meta = new_tablet_meta_pb.add_rs_metas(); |
223 | 4 | if (!visible_rowset.has_resource_id()) { |
224 | | // src be local rowset |
225 | 4 | RowsetId rowset_id = StorageEngine::instance()->next_rowset_id(); |
226 | 4 | guards.push_back(StorageEngine::instance()->pending_local_rowsets().add(rowset_id)); |
227 | 4 | RETURN_IF_ERROR_RESULT(_rename_rowset_id(visible_rowset, clone_dir, tablet_schema, |
228 | 4 | rowset_id, rowset_meta)); |
229 | 4 | RowsetId src_rs_id; |
230 | 4 | if (visible_rowset.rowset_id() > 0) { |
231 | 0 | src_rs_id.init(visible_rowset.rowset_id()); |
232 | 4 | } else { |
233 | 4 | src_rs_id.init(visible_rowset.rowset_id_v2()); |
234 | 4 | } |
235 | 4 | rowset_id_mapping[src_rs_id] = rowset_id; |
236 | 4 | } else { |
237 | | // remote rowset |
238 | 0 | *rowset_meta = visible_rowset; |
239 | 0 | } |
240 | | |
241 | 4 | rowset_meta->set_tablet_id(tablet_id); |
242 | 4 | if (partition_id != -1) { |
243 | 4 | rowset_meta->set_partition_id(partition_id); |
244 | 4 | } |
245 | | |
246 | 4 | Version rowset_version = {visible_rowset.start_version(), visible_rowset.end_version()}; |
247 | 4 | rs_version_map[rowset_version] = rowset_meta; |
248 | 4 | } |
249 | | |
250 | 2 | for (auto&& stale_rowset : cloned_tablet_meta_pb.stale_rs_metas()) { |
251 | 0 | Version rowset_version = {stale_rowset.start_version(), stale_rowset.end_version()}; |
252 | 0 | auto exist_rs = rs_version_map.find(rowset_version); |
253 | 0 | if (exist_rs != rs_version_map.end()) { |
254 | 0 | continue; |
255 | 0 | } |
256 | 0 | RowsetMetaPB* rowset_meta = new_tablet_meta_pb.add_stale_rs_metas(); |
257 | |
|
258 | 0 | if (!stale_rowset.has_resource_id()) { |
259 | | // src be local rowset |
260 | 0 | RowsetId rowset_id = StorageEngine::instance()->next_rowset_id(); |
261 | 0 | guards.push_back(StorageEngine::instance()->pending_local_rowsets().add(rowset_id)); |
262 | 0 | RETURN_IF_ERROR_RESULT(_rename_rowset_id(stale_rowset, clone_dir, tablet_schema, |
263 | 0 | rowset_id, rowset_meta)); |
264 | 0 | RowsetId src_rs_id; |
265 | 0 | if (stale_rowset.rowset_id() > 0) { |
266 | 0 | src_rs_id.init(stale_rowset.rowset_id()); |
267 | 0 | } else { |
268 | 0 | src_rs_id.init(stale_rowset.rowset_id_v2()); |
269 | 0 | } |
270 | 0 | rowset_id_mapping[src_rs_id] = rowset_id; |
271 | 0 | } else { |
272 | | // remote rowset |
273 | 0 | *rowset_meta = stale_rowset; |
274 | 0 | } |
275 | | |
276 | 0 | rowset_meta->set_tablet_id(tablet_id); |
277 | 0 | if (partition_id != -1) { |
278 | 0 | rowset_meta->set_partition_id(partition_id); |
279 | 0 | } |
280 | 0 | } |
281 | | |
282 | 2 | if (!rowset_id_mapping.empty() && cloned_tablet_meta_pb.has_delete_bitmap()) { |
283 | 0 | const auto& cloned_del_bitmap_pb = cloned_tablet_meta_pb.delete_bitmap(); |
284 | 0 | DeleteBitmapPB* new_del_bitmap_pb = new_tablet_meta_pb.mutable_delete_bitmap(); |
285 | 0 | int rst_ids_size = cloned_del_bitmap_pb.rowset_ids_size(); |
286 | 0 | for (size_t i = 0; i < rst_ids_size; ++i) { |
287 | 0 | RowsetId rst_id; |
288 | 0 | rst_id.init(cloned_del_bitmap_pb.rowset_ids(i)); |
289 | | // It should not happen, if we can't convert some rowid in delete bitmap, the |
290 | | // data might be inconsist. |
291 | 0 | CHECK(rowset_id_mapping.find(rst_id) != rowset_id_mapping.end()) |
292 | 0 | << "can't find rowset_id " << rst_id.to_string() << " in convert_rowset_ids"; |
293 | 0 | new_del_bitmap_pb->set_rowset_ids(i, rowset_id_mapping[rst_id].to_string()); |
294 | 0 | } |
295 | 0 | } |
296 | | |
297 | 2 | RETURN_IF_ERROR_RESULT(TabletMeta::save(cloned_meta_file, new_tablet_meta_pb)); |
298 | | |
299 | 2 | return guards; |
300 | 2 | } |
301 | | |
302 | | Status SnapshotManager::_rename_rowset_id(const RowsetMetaPB& rs_meta_pb, |
303 | | const std::string& new_tablet_path, |
304 | | TabletSchemaSPtr tablet_schema, const RowsetId& rowset_id, |
305 | 4 | RowsetMetaPB* new_rs_meta_pb) { |
306 | 4 | Status res = Status::OK(); |
307 | 4 | RowsetMetaSharedPtr rowset_meta(new RowsetMeta()); |
308 | 4 | rowset_meta->init_from_pb(rs_meta_pb); |
309 | 4 | RowsetSharedPtr org_rowset; |
310 | 4 | RETURN_IF_ERROR( |
311 | 4 | RowsetFactory::create_rowset(tablet_schema, new_tablet_path, rowset_meta, &org_rowset)); |
312 | | // do not use cache to load index |
313 | | // because the index file may conflict |
314 | | // and the cached fd may be invalid |
315 | 4 | RETURN_IF_ERROR(org_rowset->load(false)); |
316 | 4 | RowsetMetaSharedPtr org_rowset_meta = org_rowset->rowset_meta(); |
317 | 4 | RowsetWriterContext context; |
318 | 4 | context.rowset_id = rowset_id; |
319 | 4 | context.tablet_id = org_rowset_meta->tablet_id(); |
320 | 4 | context.partition_id = org_rowset_meta->partition_id(); |
321 | 4 | context.tablet_schema_hash = org_rowset_meta->tablet_schema_hash(); |
322 | 4 | context.rowset_type = org_rowset_meta->rowset_type(); |
323 | 4 | context.rowset_dir = new_tablet_path; |
324 | 4 | context.tablet_schema = |
325 | 4 | org_rowset_meta->tablet_schema() ? org_rowset_meta->tablet_schema() : tablet_schema; |
326 | 4 | context.rowset_state = org_rowset_meta->rowset_state(); |
327 | 4 | context.version = org_rowset_meta->version(); |
328 | 4 | context.newest_write_timestamp = org_rowset_meta->newest_write_timestamp(); |
329 | | // keep segments_overlap same as origin rowset |
330 | 4 | context.segments_overlap = rowset_meta->segments_overlap(); |
331 | | |
332 | 4 | std::unique_ptr<RowsetWriter> rs_writer; |
333 | 4 | RETURN_IF_ERROR(RowsetFactory::create_rowset_writer(context, false, &rs_writer)); |
334 | | |
335 | 4 | res = rs_writer->add_rowset(org_rowset); |
336 | 4 | if (!res.ok()) { |
337 | 0 | LOG(WARNING) << "failed to add rowset " |
338 | 0 | << " id = " << org_rowset->rowset_id() << " to rowset " << rowset_id; |
339 | 0 | return res; |
340 | 0 | } |
341 | 4 | RowsetSharedPtr new_rowset; |
342 | 4 | RETURN_NOT_OK_STATUS_WITH_WARN(rs_writer->build(new_rowset), |
343 | 4 | "failed to build rowset when rename rowset id"); |
344 | 4 | RETURN_IF_ERROR(new_rowset->load(false)); |
345 | 4 | new_rowset->rowset_meta()->to_rowset_pb(new_rs_meta_pb); |
346 | 4 | RETURN_IF_ERROR(org_rowset->remove()); |
347 | 4 | return Status::OK(); |
348 | 4 | } |
349 | | |
350 | | // get snapshot path: curtime.seq.timeout |
351 | | // eg: 20190819221234.3.86400 |
352 | | Status SnapshotManager::_calc_snapshot_id_path(const TabletSharedPtr& tablet, int64_t timeout_s, |
353 | 0 | std::string* out_path) { |
354 | 0 | Status res = Status::OK(); |
355 | 0 | if (out_path == nullptr) { |
356 | 0 | return Status::Error<INVALID_ARGUMENT>("output parameter cannot be null"); |
357 | 0 | } |
358 | | |
359 | | // get current timestamp string |
360 | 0 | string time_str; |
361 | 0 | if ((res = gen_timestamp_string(&time_str)) != Status::OK()) { |
362 | 0 | LOG(WARNING) << "failed to generate time_string when move file to trash." |
363 | 0 | << "err code=" << res; |
364 | 0 | return res; |
365 | 0 | } |
366 | | |
367 | 0 | std::unique_lock<std::mutex> auto_lock(_snapshot_mutex); |
368 | 0 | uint64_t sid = _snapshot_base_id++; |
369 | 0 | *out_path = fmt::format("{}/{}/{}.{}.{}", tablet->data_dir()->path(), SNAPSHOT_PREFIX, time_str, |
370 | 0 | sid, timeout_s); |
371 | 0 | return res; |
372 | 0 | } |
373 | | |
374 | | // prefix: /path/to/data/DATA_PREFIX/shard_id |
375 | | // return: /path/to/data/DATA_PREFIX/shard_id/tablet_id/schema_hash |
376 | | std::string SnapshotManager::get_schema_hash_full_path(const TabletSharedPtr& ref_tablet, |
377 | 2 | const std::string& prefix) { |
378 | 2 | return fmt::format("{}/{}/{}", prefix, ref_tablet->tablet_id(), ref_tablet->schema_hash()); |
379 | 2 | } |
380 | | |
381 | | std::string SnapshotManager::_get_header_full_path(const TabletSharedPtr& ref_tablet, |
382 | 0 | const std::string& schema_hash_path) const { |
383 | 0 | return fmt::format("{}/{}.hdr", schema_hash_path, ref_tablet->tablet_id()); |
384 | 0 | } |
385 | | |
386 | | std::string SnapshotManager::_get_json_header_full_path(const TabletSharedPtr& ref_tablet, |
387 | 0 | const std::string& schema_hash_path) const { |
388 | 0 | return fmt::format("{}/{}.hdr.json", schema_hash_path, ref_tablet->tablet_id()); |
389 | 0 | } |
390 | | |
391 | | Status SnapshotManager::_link_index_and_data_files( |
392 | | const std::string& schema_hash_path, const TabletSharedPtr& ref_tablet, |
393 | 0 | const std::vector<RowsetSharedPtr>& consistent_rowsets) { |
394 | 0 | Status res = Status::OK(); |
395 | 0 | for (auto& rs : consistent_rowsets) { |
396 | 0 | RETURN_IF_ERROR(rs->link_files_to(schema_hash_path, rs->rowset_id())); |
397 | 0 | } |
398 | 0 | return res; |
399 | 0 | } |
400 | | |
401 | | // `rs_metas` MUST already be sorted by `RowsetMeta::comparator` |
402 | 0 | Status check_version_continuity(const std::vector<RowsetSharedPtr>& rowsets) { |
403 | 0 | if (rowsets.size() < 2) { |
404 | 0 | return Status::OK(); |
405 | 0 | } |
406 | 0 | auto prev = rowsets.begin(); |
407 | 0 | for (auto it = rowsets.begin() + 1; it != rowsets.end(); ++it) { |
408 | 0 | if ((*prev)->end_version() + 1 != (*it)->start_version()) { |
409 | 0 | return Status::InternalError("versions are not continuity: prev={} cur={}", |
410 | 0 | (*prev)->version().to_string(), |
411 | 0 | (*it)->version().to_string()); |
412 | 0 | } |
413 | 0 | prev = it; |
414 | 0 | } |
415 | 0 | return Status::OK(); |
416 | 0 | } |
417 | | |
418 | | Status SnapshotManager::_create_snapshot_files(const TabletSharedPtr& ref_tablet, |
419 | | const TabletSharedPtr& target_tablet, |
420 | | const TSnapshotRequest& request, |
421 | | string* snapshot_path, |
422 | 0 | bool* allow_incremental_clone) { |
423 | 0 | int32_t snapshot_version = request.preferred_snapshot_version; |
424 | 0 | LOG(INFO) << "receive a make snapshot request" |
425 | 0 | << ", request detail is " << apache::thrift::ThriftDebugString(request) |
426 | 0 | << ", snapshot_version is " << snapshot_version; |
427 | 0 | Status res = Status::OK(); |
428 | 0 | if (snapshot_path == nullptr) { |
429 | 0 | return Status::Error<INVALID_ARGUMENT>("output parameter cannot be null"); |
430 | 0 | } |
431 | | |
432 | | // snapshot_id_path: |
433 | | // /data/shard_id/tablet_id/snapshot/time_str/id.timeout/ |
434 | 0 | int64_t timeout_s = config::snapshot_expire_time_sec; |
435 | 0 | if (request.__isset.timeout) { |
436 | 0 | timeout_s = request.timeout; |
437 | 0 | } |
438 | 0 | std::string snapshot_id_path; |
439 | 0 | res = _calc_snapshot_id_path(target_tablet, timeout_s, &snapshot_id_path); |
440 | 0 | if (!res.ok()) { |
441 | 0 | LOG(WARNING) << "failed to calc snapshot_id_path, tablet=" |
442 | 0 | << target_tablet->data_dir()->path(); |
443 | 0 | return res; |
444 | 0 | } |
445 | | |
446 | 0 | bool is_copy_binlog = request.__isset.is_copy_binlog ? request.is_copy_binlog : false; |
447 | | |
448 | | // schema_full_path_desc.filepath: |
449 | | // /snapshot_id_path/tablet_id/schema_hash/ |
450 | 0 | auto schema_full_path = get_schema_hash_full_path(target_tablet, snapshot_id_path); |
451 | | // header_path: |
452 | | // /schema_full_path/tablet_id.hdr |
453 | 0 | auto header_path = _get_header_full_path(target_tablet, schema_full_path); |
454 | | // /schema_full_path/tablet_id.hdr.json |
455 | 0 | auto json_header_path = _get_json_header_full_path(target_tablet, schema_full_path); |
456 | 0 | bool exists = true; |
457 | 0 | RETURN_IF_ERROR(io::global_local_filesystem()->exists(schema_full_path, &exists)); |
458 | 0 | if (exists) { |
459 | 0 | VLOG_TRACE << "remove the old schema_full_path." << schema_full_path; |
460 | 0 | RETURN_IF_ERROR(io::global_local_filesystem()->delete_directory(schema_full_path)); |
461 | 0 | } |
462 | | |
463 | 0 | RETURN_IF_ERROR(io::global_local_filesystem()->create_directory(schema_full_path)); |
464 | 0 | string snapshot_id; |
465 | 0 | RETURN_IF_ERROR(io::global_local_filesystem()->canonicalize(snapshot_id_path, &snapshot_id)); |
466 | | |
467 | 0 | std::vector<RowsetSharedPtr> consistent_rowsets; |
468 | 0 | do { |
469 | 0 | TabletMetaSharedPtr new_tablet_meta(new (nothrow) TabletMeta()); |
470 | 0 | if (new_tablet_meta == nullptr) { |
471 | 0 | res = Status::Error<MEM_ALLOC_FAILED>("fail to malloc TabletMeta."); |
472 | 0 | break; |
473 | 0 | } |
474 | 0 | DeleteBitmap delete_bitmap_snapshot(new_tablet_meta->tablet_id()); |
475 | | |
476 | | /// If set missing_version, try to get all missing version. |
477 | | /// If some of them not exist in tablet, we will fall back to |
478 | | /// make the full snapshot of the tablet. |
479 | 0 | { |
480 | 0 | std::shared_lock rdlock(ref_tablet->get_header_lock()); |
481 | 0 | if (ref_tablet->tablet_state() == TABLET_SHUTDOWN) { |
482 | 0 | return Status::Aborted("tablet has shutdown"); |
483 | 0 | } |
484 | 0 | bool is_single_rowset_clone = |
485 | 0 | (request.__isset.start_version && request.__isset.end_version); |
486 | 0 | if (is_single_rowset_clone) { |
487 | 0 | LOG(INFO) << "handle compaction clone make snapshot, tablet_id: " |
488 | 0 | << ref_tablet->tablet_id(); |
489 | 0 | Version version(request.start_version, request.end_version); |
490 | 0 | const RowsetSharedPtr rowset = ref_tablet->get_rowset_by_version(version, false); |
491 | 0 | if (rowset && rowset->is_local()) { |
492 | 0 | consistent_rowsets.push_back(rowset); |
493 | 0 | } else { |
494 | 0 | LOG(WARNING) << "failed to find local version when do compaction snapshot. " |
495 | 0 | << " tablet=" << request.tablet_id |
496 | 0 | << " schema_hash=" << request.schema_hash |
497 | 0 | << " version=" << version; |
498 | 0 | res = Status::InternalError( |
499 | 0 | "failed to find version when do compaction snapshot"); |
500 | 0 | break; |
501 | 0 | } |
502 | 0 | } |
503 | | // be would definitely set it as true no matter has missed version or not |
504 | | // but it would take no effects on the following range loop |
505 | 0 | if (!is_single_rowset_clone && request.__isset.missing_version) { |
506 | 0 | for (int64_t missed_version : request.missing_version) { |
507 | 0 | Version version = {missed_version, missed_version}; |
508 | | // find rowset in both rs_meta and stale_rs_meta |
509 | 0 | const RowsetSharedPtr rowset = ref_tablet->get_rowset_by_version(version, true); |
510 | 0 | if (rowset != nullptr) { |
511 | 0 | if (!rowset->is_local()) { |
512 | | // MUST make full snapshot to ensure `cooldown_meta_id` is consistent with the cooldowned rowsets after clone. |
513 | 0 | res = Status::Error<ErrorCode::INTERNAL_ERROR>( |
514 | 0 | "missed version is a cooldowned rowset, must make full " |
515 | 0 | "snapshot. missed_version={}, tablet_id={}", |
516 | 0 | missed_version, ref_tablet->tablet_id()); |
517 | 0 | break; |
518 | 0 | } |
519 | 0 | consistent_rowsets.push_back(rowset); |
520 | 0 | } else { |
521 | 0 | res = Status::InternalError( |
522 | 0 | "failed to find missed version when snapshot. tablet={}, " |
523 | 0 | "schema_hash={}, version={}", |
524 | 0 | request.tablet_id, request.schema_hash, version.to_string()); |
525 | 0 | break; |
526 | 0 | } |
527 | 0 | } |
528 | 0 | } |
529 | |
|
530 | 0 | DBUG_EXECUTE_IF("SnapshotManager.create_snapshot_files.allow_inc_clone", { |
531 | 0 | auto tablet_id = dp->param("tablet_id", 0); |
532 | 0 | auto is_full_clone = dp->param("is_full_clone", false); |
533 | 0 | if (ref_tablet->tablet_id() == tablet_id && is_full_clone) { |
534 | 0 | LOG(INFO) << "injected full clone for tabelt: " << tablet_id; |
535 | 0 | res = Status::InternalError("fault injection error"); |
536 | 0 | } |
537 | 0 | }); |
538 | | |
539 | | // be would definitely set it as true no matter has missed version or not, we could |
540 | | // just check whether the missed version is empty or not |
541 | 0 | int64_t version = -1; |
542 | 0 | if (!is_single_rowset_clone && (!res.ok() || request.missing_version.empty())) { |
543 | 0 | if (!request.__isset.missing_version && |
544 | 0 | ref_tablet->tablet_meta()->cooldown_meta_id().initialized()) { |
545 | 0 | LOG(WARNING) << "currently not support backup tablet with cooldowned remote " |
546 | 0 | "data. tablet=" |
547 | 0 | << request.tablet_id; |
548 | 0 | return Status::NotSupported( |
549 | 0 | "currently not support backup tablet with cooldowned remote data"); |
550 | 0 | } |
551 | | /// not all missing versions are found, fall back to full snapshot. |
552 | 0 | res = Status::OK(); // reset res |
553 | 0 | consistent_rowsets.clear(); // reset vector |
554 | | |
555 | | // get latest version |
556 | 0 | const RowsetSharedPtr last_version = ref_tablet->rowset_with_max_version(); |
557 | 0 | if (last_version == nullptr) { |
558 | 0 | res = Status::InternalError("tablet has not any version. path={}", |
559 | 0 | ref_tablet->tablet_id()); |
560 | 0 | break; |
561 | 0 | } |
562 | | // get snapshot version, use request.version if specified |
563 | 0 | version = last_version->end_version(); |
564 | 0 | if (request.__isset.version) { |
565 | 0 | if (last_version->end_version() < request.version) { |
566 | 0 | res = Status::Error<INVALID_ARGUMENT>( |
567 | 0 | "invalid make snapshot request. version={}, req_version={}", |
568 | 0 | last_version->version().to_string(), request.version); |
569 | 0 | break; |
570 | 0 | } |
571 | 0 | version = request.version; |
572 | 0 | } |
573 | 0 | if (ref_tablet->tablet_meta()->cooldown_meta_id().initialized()) { |
574 | | // Tablet has cooldowned data, MUST pick consistent rowsets with continuous cooldowned version |
575 | | // Get max cooldowned version |
576 | 0 | int64_t max_cooldowned_version = -1; |
577 | 0 | for (auto& [v, rs] : ref_tablet->rowset_map()) { |
578 | 0 | if (rs->is_local()) { |
579 | 0 | continue; |
580 | 0 | } |
581 | 0 | consistent_rowsets.push_back(rs); |
582 | 0 | max_cooldowned_version = std::max(max_cooldowned_version, v.second); |
583 | 0 | } |
584 | 0 | DCHECK_GE(max_cooldowned_version, 1) << "tablet_id=" << ref_tablet->tablet_id(); |
585 | 0 | std::sort(consistent_rowsets.begin(), consistent_rowsets.end(), |
586 | 0 | Rowset::comparator); |
587 | 0 | res = check_version_continuity(consistent_rowsets); |
588 | 0 | if (res.ok() && max_cooldowned_version < version) { |
589 | | // Pick consistent rowsets of remaining required version |
590 | 0 | res = ref_tablet->capture_consistent_rowsets( |
591 | 0 | {max_cooldowned_version + 1, version}, &consistent_rowsets); |
592 | 0 | } |
593 | 0 | } else { |
594 | | // get shortest version path |
595 | 0 | res = ref_tablet->capture_consistent_rowsets(Version(0, version), |
596 | 0 | &consistent_rowsets); |
597 | 0 | } |
598 | 0 | if (!res.ok()) { |
599 | 0 | LOG(WARNING) << "fail to select versions to span. res=" << res; |
600 | 0 | break; |
601 | 0 | } |
602 | 0 | *allow_incremental_clone = false; |
603 | 0 | } else { |
604 | 0 | version = ref_tablet->max_version_unlocked().second; |
605 | 0 | *allow_incremental_clone = true; |
606 | 0 | } |
607 | | |
608 | | // copy the tablet meta to new_tablet_meta inside header lock |
609 | 0 | CHECK(res.ok()) << res; |
610 | 0 | ref_tablet->generate_tablet_meta_copy_unlocked(new_tablet_meta); |
611 | | // The delete bitmap update operation and the add_inc_rowset operation is not atomic, |
612 | | // so delete bitmap may contains some data generated by invisible rowset, we should |
613 | | // get rid of these useless bitmaps when doing snapshot. |
614 | 0 | if (ref_tablet->keys_type() == UNIQUE_KEYS && |
615 | 0 | ref_tablet->enable_unique_key_merge_on_write()) { |
616 | 0 | delete_bitmap_snapshot = |
617 | 0 | ref_tablet->tablet_meta()->delete_bitmap().snapshot(version); |
618 | 0 | } |
619 | 0 | } |
620 | | |
621 | 0 | std::vector<RowsetMetaSharedPtr> rs_metas; |
622 | 0 | for (auto& rs : consistent_rowsets) { |
623 | 0 | if (rs->is_local()) { |
624 | | // local rowset |
625 | 0 | res = rs->link_files_to(schema_full_path, rs->rowset_id()); |
626 | 0 | if (!res.ok()) { |
627 | 0 | break; |
628 | 0 | } |
629 | 0 | } |
630 | 0 | rs_metas.push_back(rs->rowset_meta()); |
631 | 0 | VLOG_NOTICE << "add rowset meta to clone list. " |
632 | 0 | << " start version " << rs->rowset_meta()->start_version() |
633 | 0 | << " end version " << rs->rowset_meta()->end_version() << " empty " |
634 | 0 | << rs->rowset_meta()->empty(); |
635 | 0 | } |
636 | 0 | if (!res.ok()) { |
637 | 0 | LOG(WARNING) << "fail to create hard link. path=" << snapshot_id_path |
638 | 0 | << " tablet=" << target_tablet->tablet_id() |
639 | 0 | << " ref tablet=" << ref_tablet->tablet_id(); |
640 | 0 | break; |
641 | 0 | } |
642 | | |
643 | | // The inc_rs_metas is deprecated since Doris version 0.13. |
644 | | // Clear it for safety reason. |
645 | | // Whether it is incremental or full snapshot, rowset information is stored in rs_meta. |
646 | 0 | new_tablet_meta->revise_rs_metas(std::move(rs_metas)); |
647 | 0 | if (ref_tablet->keys_type() == UNIQUE_KEYS && |
648 | 0 | ref_tablet->enable_unique_key_merge_on_write()) { |
649 | 0 | new_tablet_meta->revise_delete_bitmap_unlocked(delete_bitmap_snapshot); |
650 | 0 | } |
651 | |
|
652 | 0 | if (snapshot_version == g_Types_constants.TSNAPSHOT_REQ_VERSION2) { |
653 | 0 | res = new_tablet_meta->save(header_path); |
654 | 0 | if (res.ok() && request.__isset.is_copy_tablet_task && request.is_copy_tablet_task) { |
655 | 0 | res = new_tablet_meta->save_as_json(json_header_path, ref_tablet->data_dir()); |
656 | 0 | } |
657 | 0 | } else { |
658 | 0 | res = Status::Error<INVALID_SNAPSHOT_VERSION>( |
659 | 0 | "snapshot_version not equal to g_Types_constants.TSNAPSHOT_REQ_VERSION2"); |
660 | 0 | } |
661 | |
|
662 | 0 | if (!res.ok()) { |
663 | 0 | LOG(WARNING) << "convert rowset failed, res:" << res |
664 | 0 | << ", tablet:" << new_tablet_meta->tablet_id() |
665 | 0 | << ", schema hash:" << new_tablet_meta->schema_hash() |
666 | 0 | << ", snapshot_version:" << snapshot_version |
667 | 0 | << ", is incremental:" << request.__isset.missing_version; |
668 | 0 | break; |
669 | 0 | } |
670 | |
|
671 | 0 | } while (false); |
672 | | |
673 | | // link all binlog files to snapshot path |
674 | 0 | do { |
675 | 0 | if (!res.ok()) { |
676 | 0 | break; |
677 | 0 | } |
678 | | |
679 | 0 | if (!is_copy_binlog) { |
680 | 0 | break; |
681 | 0 | } |
682 | | |
683 | 0 | RowsetBinlogMetasPB rowset_binlog_metas_pb; |
684 | 0 | for (auto& rs : consistent_rowsets) { |
685 | 0 | if (!rs->is_local()) { |
686 | 0 | continue; |
687 | 0 | } |
688 | 0 | res = ref_tablet->get_rowset_binlog_metas(rs->version(), &rowset_binlog_metas_pb); |
689 | 0 | if (!res.ok()) { |
690 | 0 | break; |
691 | 0 | } |
692 | 0 | } |
693 | 0 | if (!res.ok() || rowset_binlog_metas_pb.rowset_binlog_metas_size() == 0) { |
694 | 0 | break; |
695 | 0 | } |
696 | | |
697 | | // write to pb file |
698 | 0 | auto rowset_binlog_metas_pb_filename = |
699 | 0 | fmt::format("{}/rowset_binlog_metas.pb", schema_full_path); |
700 | 0 | res = write_pb(rowset_binlog_metas_pb_filename, rowset_binlog_metas_pb); |
701 | 0 | if (!res.ok()) { |
702 | 0 | break; |
703 | 0 | } |
704 | | |
705 | 0 | for (const auto& rowset_binlog_meta : rowset_binlog_metas_pb.rowset_binlog_metas()) { |
706 | 0 | std::string segment_file_path; |
707 | 0 | auto num_segments = rowset_binlog_meta.num_segments(); |
708 | 0 | std::string_view rowset_id = rowset_binlog_meta.rowset_id(); |
709 | |
|
710 | 0 | RowsetMetaPB rowset_meta_pb; |
711 | 0 | if (!rowset_meta_pb.ParseFromString(rowset_binlog_meta.data())) { |
712 | 0 | auto err_msg = fmt::format("fail to parse binlog meta data value:{}", |
713 | 0 | rowset_binlog_meta.data()); |
714 | 0 | res = Status::InternalError(err_msg); |
715 | 0 | LOG(WARNING) << err_msg; |
716 | 0 | return res; |
717 | 0 | } |
718 | 0 | const auto& tablet_schema_pb = rowset_meta_pb.tablet_schema(); |
719 | 0 | TabletSchema tablet_schema; |
720 | 0 | tablet_schema.init_from_pb(tablet_schema_pb); |
721 | |
|
722 | 0 | std::vector<string> linked_success_files; |
723 | 0 | Defer remove_linked_files {[&]() { // clear linked files if errors happen |
724 | 0 | if (!res.ok()) { |
725 | 0 | LOG(WARNING) << "will delete linked success files due to error " << res; |
726 | 0 | std::vector<io::Path> paths; |
727 | 0 | for (auto& file : linked_success_files) { |
728 | 0 | paths.emplace_back(file); |
729 | 0 | LOG(WARNING) |
730 | 0 | << "will delete linked success file " << file << " due to error"; |
731 | 0 | } |
732 | 0 | static_cast<void>(io::global_local_filesystem()->batch_delete(paths)); |
733 | 0 | LOG(WARNING) << "done delete linked success files due to error " << res; |
734 | 0 | } |
735 | 0 | }}; |
736 | | |
737 | | // link segment files and index files |
738 | 0 | for (int64_t segment_index = 0; segment_index < num_segments; ++segment_index) { |
739 | 0 | segment_file_path = ref_tablet->get_segment_filepath(rowset_id, segment_index); |
740 | 0 | auto snapshot_segment_file_path = |
741 | 0 | fmt::format("{}/{}_{}.binlog", schema_full_path, rowset_id, segment_index); |
742 | |
|
743 | 0 | res = io::global_local_filesystem()->link_file(segment_file_path, |
744 | 0 | snapshot_segment_file_path); |
745 | 0 | if (!res.ok()) { |
746 | 0 | LOG(WARNING) << "fail to link binlog file. [src=" << segment_file_path |
747 | 0 | << ", dest=" << snapshot_segment_file_path << "]"; |
748 | 0 | break; |
749 | 0 | } |
750 | 0 | linked_success_files.push_back(snapshot_segment_file_path); |
751 | |
|
752 | 0 | if (tablet_schema.get_inverted_index_storage_format() == |
753 | 0 | InvertedIndexStorageFormatPB::V1) { |
754 | 0 | for (const auto& index : tablet_schema.indexes()) { |
755 | 0 | if (index.index_type() != IndexType::INVERTED) { |
756 | 0 | continue; |
757 | 0 | } |
758 | 0 | auto index_id = index.index_id(); |
759 | 0 | auto index_file = ref_tablet->get_segment_index_filepath( |
760 | 0 | rowset_id, segment_index, index_id); |
761 | 0 | auto snapshot_segment_index_file_path = |
762 | 0 | fmt::format("{}/{}_{}_{}.binlog-index", schema_full_path, rowset_id, |
763 | 0 | segment_index, index_id); |
764 | 0 | VLOG_DEBUG << "link " << index_file << " to " |
765 | 0 | << snapshot_segment_index_file_path; |
766 | 0 | res = io::global_local_filesystem()->link_file( |
767 | 0 | index_file, snapshot_segment_index_file_path); |
768 | 0 | if (!res.ok()) { |
769 | 0 | LOG(WARNING) << "fail to link binlog index file. [src=" << index_file |
770 | 0 | << ", dest=" << snapshot_segment_index_file_path << "]"; |
771 | 0 | break; |
772 | 0 | } |
773 | 0 | linked_success_files.push_back(snapshot_segment_index_file_path); |
774 | 0 | } |
775 | 0 | } else { |
776 | 0 | if (tablet_schema.has_inverted_index()) { |
777 | 0 | auto index_file = |
778 | 0 | InvertedIndexDescriptor::get_index_file_name(segment_file_path); |
779 | 0 | auto snapshot_segment_index_file_path = |
780 | 0 | fmt::format("{}/{}_{}.binlog-index", schema_full_path, rowset_id, |
781 | 0 | segment_index); |
782 | 0 | VLOG_DEBUG << "link " << index_file << " to " |
783 | 0 | << snapshot_segment_index_file_path; |
784 | 0 | res = io::global_local_filesystem()->link_file( |
785 | 0 | index_file, snapshot_segment_index_file_path); |
786 | 0 | if (!res.ok()) { |
787 | 0 | LOG(WARNING) << "fail to link binlog index file. [src=" << index_file |
788 | 0 | << ", dest=" << snapshot_segment_index_file_path << "]"; |
789 | 0 | break; |
790 | 0 | } |
791 | 0 | linked_success_files.push_back(snapshot_segment_index_file_path); |
792 | 0 | } |
793 | 0 | } |
794 | 0 | } |
795 | |
|
796 | 0 | if (!res.ok()) { |
797 | 0 | break; |
798 | 0 | } |
799 | 0 | } |
800 | 0 | } while (false); |
801 | | |
802 | 0 | if (!res.ok()) { |
803 | 0 | LOG(WARNING) << "fail to make snapshot, try to delete the snapshot path. path=" |
804 | 0 | << snapshot_id_path.c_str(); |
805 | |
|
806 | 0 | bool exists = true; |
807 | 0 | RETURN_IF_ERROR(io::global_local_filesystem()->exists(snapshot_id_path, &exists)); |
808 | 0 | if (exists) { |
809 | 0 | VLOG_NOTICE << "remove snapshot path. [path=" << snapshot_id_path << "]"; |
810 | 0 | RETURN_IF_ERROR(io::global_local_filesystem()->delete_directory(snapshot_id_path)); |
811 | 0 | } |
812 | 0 | } else { |
813 | 0 | *snapshot_path = snapshot_id; |
814 | 0 | } |
815 | | |
816 | 0 | return res; |
817 | 0 | } |
818 | | |
819 | | } // namespace doris |