/root/doris/be/src/cloud/cloud_tablet.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 "cloud/cloud_tablet.h" |
19 | | |
20 | | #include <gen_cpp/olap_file.pb.h> |
21 | | #include <rapidjson/document.h> |
22 | | #include <rapidjson/encodings.h> |
23 | | #include <rapidjson/prettywriter.h> |
24 | | #include <rapidjson/rapidjson.h> |
25 | | #include <rapidjson/stringbuffer.h> |
26 | | |
27 | | #include <atomic> |
28 | | #include <memory> |
29 | | #include <shared_mutex> |
30 | | #include <unordered_map> |
31 | | #include <vector> |
32 | | |
33 | | #include "cloud/cloud_meta_mgr.h" |
34 | | #include "cloud/cloud_storage_engine.h" |
35 | | #include "cloud/cloud_tablet_mgr.h" |
36 | | #include "common/config.h" |
37 | | #include "common/logging.h" |
38 | | #include "io/cache/block_file_cache_downloader.h" |
39 | | #include "io/cache/block_file_cache_factory.h" |
40 | | #include "olap/compaction.h" |
41 | | #include "olap/cumulative_compaction_time_series_policy.h" |
42 | | #include "olap/olap_define.h" |
43 | | #include "olap/rowset/beta_rowset.h" |
44 | | #include "olap/rowset/rowset.h" |
45 | | #include "olap/rowset/rowset_factory.h" |
46 | | #include "olap/rowset/rowset_fwd.h" |
47 | | #include "olap/rowset/rowset_writer.h" |
48 | | #include "olap/rowset/segment_v2/inverted_index_desc.h" |
49 | | #include "olap/storage_policy.h" |
50 | | #include "olap/tablet_schema.h" |
51 | | #include "olap/txn_manager.h" |
52 | | #include "util/debug_points.h" |
53 | | #include "vec/common/schema_util.h" |
54 | | |
55 | | namespace doris { |
56 | | #include "common/compile_check_begin.h" |
57 | | using namespace ErrorCode; |
58 | | |
59 | | static constexpr int LOAD_INITIATOR_ID = -1; |
60 | | |
61 | | CloudTablet::CloudTablet(CloudStorageEngine& engine, TabletMetaSharedPtr tablet_meta) |
62 | 1 | : BaseTablet(std::move(tablet_meta)), _engine(engine) {} |
63 | | |
64 | 1 | CloudTablet::~CloudTablet() = default; |
65 | | |
66 | 0 | bool CloudTablet::exceed_version_limit(int32_t limit) { |
67 | 0 | return _approximate_num_rowsets.load(std::memory_order_relaxed) > limit; |
68 | 0 | } |
69 | | |
70 | | Status CloudTablet::capture_consistent_rowsets_unlocked( |
71 | 0 | const Version& spec_version, std::vector<RowsetSharedPtr>* rowsets) const { |
72 | 0 | Versions version_path; |
73 | 0 | auto st = _timestamped_version_tracker.capture_consistent_versions(spec_version, &version_path); |
74 | 0 | if (!st.ok()) { |
75 | | // Check no missed versions or req version is merged |
76 | 0 | auto missed_versions = get_missed_versions(spec_version.second); |
77 | 0 | if (missed_versions.empty()) { |
78 | 0 | st.set_code(VERSION_ALREADY_MERGED); // Reset error code |
79 | 0 | } |
80 | 0 | st.append(" tablet_id=" + std::to_string(tablet_id())); |
81 | 0 | return st; |
82 | 0 | } |
83 | 0 | VLOG_DEBUG << "capture consitent versions: " << version_path; |
84 | 0 | return _capture_consistent_rowsets_unlocked(version_path, rowsets); |
85 | 0 | } |
86 | | |
87 | | Status CloudTablet::capture_rs_readers(const Version& spec_version, |
88 | | std::vector<RowSetSplits>* rs_splits, |
89 | 0 | bool skip_missing_version) { |
90 | 0 | DBUG_EXECUTE_IF("CloudTablet.capture_rs_readers.return.e-230", { |
91 | 0 | LOG_WARNING("CloudTablet.capture_rs_readers.return e-230").tag("tablet_id", tablet_id()); |
92 | 0 | return Status::Error<false>(-230, "injected error"); |
93 | 0 | }); |
94 | 0 | Versions version_path; |
95 | 0 | std::shared_lock rlock(_meta_lock); |
96 | 0 | auto st = _timestamped_version_tracker.capture_consistent_versions(spec_version, &version_path); |
97 | 0 | if (!st.ok()) { |
98 | 0 | rlock.unlock(); // avoid logging in lock range |
99 | | // Check no missed versions or req version is merged |
100 | 0 | auto missed_versions = get_missed_versions(spec_version.second); |
101 | 0 | if (missed_versions.empty()) { |
102 | 0 | st.set_code(VERSION_ALREADY_MERGED); // Reset error code |
103 | 0 | st.append(" versions are already compacted, "); |
104 | 0 | } |
105 | 0 | st.append(" tablet_id=" + std::to_string(tablet_id())); |
106 | | // clang-format off |
107 | 0 | LOG(WARNING) << st << '\n' << [this]() { std::string json; get_compaction_status(&json); return json; }(); |
108 | | // clang-format on |
109 | 0 | return st; |
110 | 0 | } |
111 | 0 | VLOG_DEBUG << "capture consitent versions: " << version_path; |
112 | 0 | return capture_rs_readers_unlocked(version_path, rs_splits); |
113 | 0 | } |
114 | | |
115 | 0 | Status CloudTablet::merge_rowsets_schema() { |
116 | | // Find the rowset with the max version |
117 | 0 | auto max_version_rowset = |
118 | 0 | std::max_element( |
119 | 0 | _rs_version_map.begin(), _rs_version_map.end(), |
120 | 0 | [](const auto& a, const auto& b) { |
121 | 0 | return !a.second->tablet_schema() |
122 | 0 | ? true |
123 | 0 | : (!b.second->tablet_schema() |
124 | 0 | ? false |
125 | 0 | : a.second->tablet_schema()->schema_version() < |
126 | 0 | b.second->tablet_schema() |
127 | 0 | ->schema_version()); |
128 | 0 | }) |
129 | 0 | ->second; |
130 | 0 | TabletSchemaSPtr max_version_schema = max_version_rowset->tablet_schema(); |
131 | | // If the schema has variant columns, perform a merge to create a wide tablet schema |
132 | 0 | if (max_version_schema->num_variant_columns() > 0) { |
133 | 0 | std::vector<TabletSchemaSPtr> schemas; |
134 | 0 | std::transform(_rs_version_map.begin(), _rs_version_map.end(), std::back_inserter(schemas), |
135 | 0 | [](const auto& rs_meta) { return rs_meta.second->tablet_schema(); }); |
136 | | // Merge the collected schemas to obtain the least common schema |
137 | 0 | RETURN_IF_ERROR(vectorized::schema_util::get_least_common_schema(schemas, nullptr, |
138 | 0 | max_version_schema)); |
139 | 0 | VLOG_DEBUG << "dump schema: " << max_version_schema->dump_full_schema(); |
140 | 0 | _merged_tablet_schema = max_version_schema; |
141 | 0 | } |
142 | 0 | return Status::OK(); |
143 | 0 | } |
144 | | |
145 | | // There are only two tablet_states RUNNING and NOT_READY in cloud mode |
146 | | // This function will erase the tablet from `CloudTabletMgr` when it can't find this tablet in MS. |
147 | 0 | Status CloudTablet::sync_rowsets(const SyncOptions& options, SyncRowsetStats* stats) { |
148 | 0 | RETURN_IF_ERROR(sync_if_not_running(stats)); |
149 | | |
150 | 0 | if (options.query_version > 0) { |
151 | 0 | std::shared_lock rlock(_meta_lock); |
152 | 0 | if (_max_version >= options.query_version) { |
153 | 0 | return Status::OK(); |
154 | 0 | } |
155 | 0 | } |
156 | | |
157 | | // serially execute sync to reduce unnecessary network overhead |
158 | 0 | std::lock_guard lock(_sync_meta_lock); |
159 | 0 | if (options.query_version > 0) { |
160 | 0 | std::shared_lock rlock(_meta_lock); |
161 | 0 | if (_max_version >= options.query_version) { |
162 | 0 | return Status::OK(); |
163 | 0 | } |
164 | 0 | } |
165 | | |
166 | 0 | auto st = _engine.meta_mgr().sync_tablet_rowsets(this, options, stats); |
167 | 0 | if (st.is<ErrorCode::NOT_FOUND>()) { |
168 | 0 | clear_cache(); |
169 | 0 | } |
170 | |
|
171 | 0 | return st; |
172 | 0 | } |
173 | | |
174 | | // Sync tablet meta and all rowset meta if not running. |
175 | | // This could happen when BE didn't finish schema change job and another BE committed this schema change job. |
176 | | // It should be a quite rare situation. |
177 | 0 | Status CloudTablet::sync_if_not_running(SyncRowsetStats* stats) { |
178 | 0 | if (tablet_state() == TABLET_RUNNING) { |
179 | 0 | return Status::OK(); |
180 | 0 | } |
181 | | |
182 | | // Serially execute sync to reduce unnecessary network overhead |
183 | 0 | std::lock_guard lock(_sync_meta_lock); |
184 | |
|
185 | 0 | { |
186 | 0 | std::shared_lock rlock(_meta_lock); |
187 | 0 | if (tablet_state() == TABLET_RUNNING) { |
188 | 0 | return Status::OK(); |
189 | 0 | } |
190 | 0 | } |
191 | | |
192 | 0 | TabletMetaSharedPtr tablet_meta; |
193 | 0 | auto st = _engine.meta_mgr().get_tablet_meta(tablet_id(), &tablet_meta); |
194 | 0 | if (!st.ok()) { |
195 | 0 | if (st.is<ErrorCode::NOT_FOUND>()) { |
196 | 0 | clear_cache(); |
197 | 0 | } |
198 | 0 | return st; |
199 | 0 | } |
200 | | |
201 | 0 | if (tablet_meta->tablet_state() != TABLET_RUNNING) [[unlikely]] { |
202 | | // MoW may go to here when load while schema change |
203 | 0 | return Status::OK(); |
204 | 0 | } |
205 | | |
206 | 0 | TimestampedVersionTracker empty_tracker; |
207 | 0 | { |
208 | 0 | std::lock_guard wlock(_meta_lock); |
209 | 0 | RETURN_IF_ERROR(set_tablet_state(TABLET_RUNNING)); |
210 | 0 | _rs_version_map.clear(); |
211 | 0 | _stale_rs_version_map.clear(); |
212 | 0 | std::swap(_timestamped_version_tracker, empty_tracker); |
213 | 0 | _tablet_meta->clear_rowsets(); |
214 | 0 | _tablet_meta->clear_stale_rowset(); |
215 | 0 | _max_version = -1; |
216 | 0 | } |
217 | | |
218 | 0 | st = _engine.meta_mgr().sync_tablet_rowsets(this, {}, stats); |
219 | 0 | if (st.is<ErrorCode::NOT_FOUND>()) { |
220 | 0 | clear_cache(); |
221 | 0 | } |
222 | 0 | return st; |
223 | 0 | } |
224 | | |
225 | 0 | TabletSchemaSPtr CloudTablet::merged_tablet_schema() const { |
226 | 0 | std::shared_lock rlock(_meta_lock); |
227 | 0 | return _merged_tablet_schema; |
228 | 0 | } |
229 | | |
230 | | void CloudTablet::add_rowsets(std::vector<RowsetSharedPtr> to_add, bool version_overlap, |
231 | | std::unique_lock<std::shared_mutex>& meta_lock, |
232 | 0 | bool warmup_delta_data) { |
233 | 0 | if (to_add.empty()) { |
234 | 0 | return; |
235 | 0 | } |
236 | | |
237 | 0 | auto add_rowsets_directly = [=, this](std::vector<RowsetSharedPtr>& rowsets) { |
238 | 0 | for (auto& rs : rowsets) { |
239 | 0 | if (version_overlap || warmup_delta_data) { |
240 | | #ifndef BE_TEST |
241 | | // Warmup rowset data in background |
242 | | for (int seg_id = 0; seg_id < rs->num_segments(); ++seg_id) { |
243 | | const auto& rowset_meta = rs->rowset_meta(); |
244 | | constexpr int64_t interval = 600; // 10 mins |
245 | | // When BE restart and receive the `load_sync` rpc, it will sync all historical rowsets first time. |
246 | | // So we need to filter out the old rowsets avoid to download the whole table. |
247 | | if (warmup_delta_data && |
248 | | ::time(nullptr) - rowset_meta->newest_write_timestamp() >= interval) { |
249 | | continue; |
250 | | } |
251 | | |
252 | | auto storage_resource = rowset_meta->remote_storage_resource(); |
253 | | if (!storage_resource) { |
254 | | LOG(WARNING) << storage_resource.error(); |
255 | | continue; |
256 | | } |
257 | | |
258 | | int64_t expiration_time = |
259 | | _tablet_meta->ttl_seconds() == 0 || |
260 | | rowset_meta->newest_write_timestamp() <= 0 |
261 | | ? 0 |
262 | | : rowset_meta->newest_write_timestamp() + |
263 | | _tablet_meta->ttl_seconds(); |
264 | | _engine.file_cache_block_downloader().submit_download_task( |
265 | | io::DownloadFileMeta { |
266 | | .path = storage_resource.value()->remote_segment_path( |
267 | | *rowset_meta, seg_id), |
268 | | .file_size = rs->rowset_meta()->segment_file_size(seg_id), |
269 | | .file_system = storage_resource.value()->fs, |
270 | | .ctx = |
271 | | { |
272 | | .expiration_time = expiration_time, |
273 | | }, |
274 | | .download_done {}, |
275 | | }); |
276 | | |
277 | | auto download_idx_file = [&](const io::Path& idx_path) { |
278 | | io::DownloadFileMeta meta { |
279 | | .path = idx_path, |
280 | | .file_size = -1, |
281 | | .file_system = storage_resource.value()->fs, |
282 | | .ctx = |
283 | | { |
284 | | .expiration_time = expiration_time, |
285 | | }, |
286 | | .download_done {}, |
287 | | }; |
288 | | _engine.file_cache_block_downloader().submit_download_task(std::move(meta)); |
289 | | }; |
290 | | auto schema_ptr = rowset_meta->tablet_schema(); |
291 | | auto idx_version = schema_ptr->get_inverted_index_storage_format(); |
292 | | if (idx_version == InvertedIndexStorageFormatPB::V1) { |
293 | | for (const auto& index : schema_ptr->inverted_indexes()) { |
294 | | auto idx_path = storage_resource.value()->remote_idx_v1_path( |
295 | | *rowset_meta, seg_id, index->index_id(), |
296 | | index->get_index_suffix()); |
297 | | download_idx_file(idx_path); |
298 | | } |
299 | | } else { |
300 | | if (schema_ptr->has_inverted_index()) { |
301 | | auto idx_path = storage_resource.value()->remote_idx_v2_path( |
302 | | *rowset_meta, seg_id); |
303 | | download_idx_file(idx_path); |
304 | | } |
305 | | } |
306 | | } |
307 | | #endif |
308 | 0 | } |
309 | 0 | _rs_version_map.emplace(rs->version(), rs); |
310 | 0 | _timestamped_version_tracker.add_version(rs->version()); |
311 | 0 | _max_version = std::max(rs->end_version(), _max_version); |
312 | 0 | update_base_size(*rs); |
313 | 0 | } |
314 | 0 | _tablet_meta->add_rowsets_unchecked(rowsets); |
315 | 0 | }; |
316 | |
|
317 | 0 | if (!version_overlap) { |
318 | 0 | add_rowsets_directly(to_add); |
319 | 0 | return; |
320 | 0 | } |
321 | | |
322 | | // Filter out existed rowsets |
323 | 0 | auto remove_it = |
324 | 0 | std::remove_if(to_add.begin(), to_add.end(), [this](const RowsetSharedPtr& rs) { |
325 | 0 | if (auto find_it = _rs_version_map.find(rs->version()); |
326 | 0 | find_it == _rs_version_map.end()) { |
327 | 0 | return false; |
328 | 0 | } else if (find_it->second->rowset_id() == rs->rowset_id()) { |
329 | 0 | return true; // Same rowset |
330 | 0 | } |
331 | | |
332 | | // If version of rowset in `to_add` is equal to rowset in tablet but rowset_id is not equal, |
333 | | // replace existed rowset with `to_add` rowset. This may occur when: |
334 | | // 1. schema change converts rowsets which have been double written to new tablet |
335 | | // 2. cumu compaction picks single overlapping input rowset to perform compaction |
336 | 0 | _tablet_meta->delete_rs_meta_by_version(rs->version(), nullptr); |
337 | 0 | _rs_version_map[rs->version()] = rs; |
338 | 0 | _tablet_meta->add_rowsets_unchecked({rs}); |
339 | 0 | update_base_size(*rs); |
340 | 0 | return true; |
341 | 0 | }); |
342 | |
|
343 | 0 | to_add.erase(remove_it, to_add.end()); |
344 | | |
345 | | // delete rowsets with overlapped version |
346 | 0 | std::vector<RowsetSharedPtr> to_add_directly; |
347 | 0 | for (auto& to_add_rs : to_add) { |
348 | | // delete rowsets with overlapped version |
349 | 0 | std::vector<RowsetSharedPtr> to_delete; |
350 | 0 | Version to_add_v = to_add_rs->version(); |
351 | | // if start_version > max_version, we can skip checking overlap here. |
352 | 0 | if (to_add_v.first > _max_version) { |
353 | | // if start_version > max_version, we can skip checking overlap here. |
354 | 0 | to_add_directly.push_back(to_add_rs); |
355 | 0 | } else { |
356 | 0 | to_add_directly.push_back(to_add_rs); |
357 | 0 | for (auto& [v, rs] : _rs_version_map) { |
358 | 0 | if (to_add_v.contains(v)) { |
359 | 0 | to_delete.push_back(rs); |
360 | 0 | } |
361 | 0 | } |
362 | 0 | delete_rowsets(to_delete, meta_lock); |
363 | 0 | } |
364 | 0 | } |
365 | |
|
366 | 0 | add_rowsets_directly(to_add_directly); |
367 | 0 | } |
368 | | |
369 | | void CloudTablet::delete_rowsets(const std::vector<RowsetSharedPtr>& to_delete, |
370 | 0 | std::unique_lock<std::shared_mutex>&) { |
371 | 0 | if (to_delete.empty()) { |
372 | 0 | return; |
373 | 0 | } |
374 | 0 | std::vector<RowsetMetaSharedPtr> rs_metas; |
375 | 0 | rs_metas.reserve(to_delete.size()); |
376 | 0 | for (auto&& rs : to_delete) { |
377 | 0 | rs_metas.push_back(rs->rowset_meta()); |
378 | 0 | _stale_rs_version_map[rs->version()] = rs; |
379 | 0 | } |
380 | 0 | _timestamped_version_tracker.add_stale_path_version(rs_metas); |
381 | 0 | for (auto&& rs : to_delete) { |
382 | 0 | _rs_version_map.erase(rs->version()); |
383 | 0 | } |
384 | |
|
385 | 0 | _tablet_meta->modify_rs_metas({}, rs_metas, false); |
386 | 0 | } |
387 | | |
388 | 0 | uint64_t CloudTablet::delete_expired_stale_rowsets() { |
389 | 0 | std::vector<RowsetSharedPtr> expired_rowsets; |
390 | | // ATTN: trick, Use stale_rowsets to temporarily increase the reference count of the rowset shared pointer in _stale_rs_version_map so that in the recycle_cached_data function, it checks if the reference count is 2. |
391 | 0 | std::vector<RowsetSharedPtr> stale_rowsets; |
392 | 0 | int64_t expired_stale_sweep_endtime = |
393 | 0 | ::time(nullptr) - config::tablet_rowset_stale_sweep_time_sec; |
394 | 0 | std::vector<std::string> version_to_delete; |
395 | 0 | { |
396 | 0 | std::unique_lock wlock(_meta_lock); |
397 | |
|
398 | 0 | std::vector<int64_t> path_ids; |
399 | | // capture the path version to delete |
400 | 0 | _timestamped_version_tracker.capture_expired_paths(expired_stale_sweep_endtime, &path_ids); |
401 | |
|
402 | 0 | if (path_ids.empty()) { |
403 | 0 | return 0; |
404 | 0 | } |
405 | | |
406 | 0 | for (int64_t path_id : path_ids) { |
407 | 0 | int64_t start_version = -1; |
408 | 0 | int64_t end_version = -1; |
409 | | // delete stale versions in version graph |
410 | 0 | auto version_path = _timestamped_version_tracker.fetch_and_delete_path_by_id(path_id); |
411 | 0 | for (auto& v_ts : version_path->timestamped_versions()) { |
412 | 0 | auto rs_it = _stale_rs_version_map.find(v_ts->version()); |
413 | 0 | if (rs_it != _stale_rs_version_map.end()) { |
414 | 0 | expired_rowsets.push_back(rs_it->second); |
415 | 0 | stale_rowsets.push_back(rs_it->second); |
416 | 0 | LOG(INFO) << "erase stale rowset, tablet_id=" << tablet_id() |
417 | 0 | << " rowset_id=" << rs_it->second->rowset_id().to_string() |
418 | 0 | << " version=" << rs_it->first.to_string(); |
419 | 0 | _stale_rs_version_map.erase(rs_it); |
420 | 0 | } else { |
421 | 0 | LOG(WARNING) << "cannot find stale rowset " << v_ts->version() << " in tablet " |
422 | 0 | << tablet_id(); |
423 | | // clang-format off |
424 | 0 | DCHECK(false) << [this, &wlock]() { wlock.unlock(); std::string json; get_compaction_status(&json); return json; }(); |
425 | | // clang-format on |
426 | 0 | } |
427 | 0 | if (start_version < 0) { |
428 | 0 | start_version = v_ts->version().first; |
429 | 0 | } |
430 | 0 | end_version = v_ts->version().second; |
431 | 0 | _tablet_meta->delete_stale_rs_meta_by_version(v_ts->version()); |
432 | 0 | } |
433 | 0 | Version version(start_version, end_version); |
434 | 0 | version_to_delete.emplace_back(version.to_string()); |
435 | 0 | } |
436 | 0 | _reconstruct_version_tracker_if_necessary(); |
437 | 0 | } |
438 | 0 | _tablet_meta->delete_bitmap().remove_stale_delete_bitmap_from_queue(version_to_delete); |
439 | 0 | recycle_cached_data(expired_rowsets); |
440 | 0 | return expired_rowsets.size(); |
441 | 0 | } |
442 | | |
443 | 0 | void CloudTablet::update_base_size(const Rowset& rs) { |
444 | | // Define base rowset as the rowset of version [2-x] |
445 | 0 | if (rs.start_version() == 2) { |
446 | 0 | _base_size = rs.total_disk_size(); |
447 | 0 | } |
448 | 0 | } |
449 | | |
450 | 0 | void CloudTablet::clear_cache() { |
451 | 0 | CloudTablet::recycle_cached_data(get_snapshot_rowset(true)); |
452 | 0 | _engine.tablet_mgr().erase_tablet(tablet_id()); |
453 | 0 | } |
454 | | |
455 | 0 | void CloudTablet::recycle_cached_data(const std::vector<RowsetSharedPtr>& rowsets) { |
456 | 0 | for (const auto& rs : rowsets) { |
457 | | // rowsets and tablet._rs_version_map each hold a rowset shared_ptr, so at this point, the reference count of the shared_ptr is at least 2. |
458 | 0 | if (rs.use_count() > 2) { |
459 | 0 | LOG(WARNING) << "Rowset " << rs->rowset_id().to_string() << " has " << rs.use_count() |
460 | 0 | << " references. File Cache won't be recycled when query is using it."; |
461 | 0 | return; |
462 | 0 | } |
463 | 0 | rs->clear_cache(); |
464 | 0 | } |
465 | 0 | } |
466 | | |
467 | | void CloudTablet::reset_approximate_stats(int64_t num_rowsets, int64_t num_segments, |
468 | 0 | int64_t num_rows, int64_t data_size) { |
469 | 0 | _approximate_num_rowsets.store(num_rowsets, std::memory_order_relaxed); |
470 | 0 | _approximate_num_segments.store(num_segments, std::memory_order_relaxed); |
471 | 0 | _approximate_num_rows.store(num_rows, std::memory_order_relaxed); |
472 | 0 | _approximate_data_size.store(data_size, std::memory_order_relaxed); |
473 | 0 | int64_t cumu_num_deltas = 0; |
474 | 0 | int64_t cumu_num_rowsets = 0; |
475 | 0 | auto cp = _cumulative_point.load(std::memory_order_relaxed); |
476 | 0 | for (auto& [v, r] : _rs_version_map) { |
477 | 0 | if (v.second < cp) { |
478 | 0 | continue; |
479 | 0 | } |
480 | | |
481 | 0 | cumu_num_deltas += r->is_segments_overlapping() ? r->num_segments() : 1; |
482 | 0 | ++cumu_num_rowsets; |
483 | 0 | } |
484 | 0 | _approximate_cumu_num_rowsets.store(cumu_num_rowsets, std::memory_order_relaxed); |
485 | 0 | _approximate_cumu_num_deltas.store(cumu_num_deltas, std::memory_order_relaxed); |
486 | 0 | } |
487 | | |
488 | | Result<std::unique_ptr<RowsetWriter>> CloudTablet::create_rowset_writer( |
489 | 0 | RowsetWriterContext& context, bool vertical) { |
490 | 0 | context.rowset_id = _engine.next_rowset_id(); |
491 | | // FIXME(plat1ko): Seems `tablet_id` and `index_id` has been set repeatedly |
492 | 0 | context.tablet_id = tablet_id(); |
493 | 0 | context.index_id = index_id(); |
494 | 0 | context.partition_id = partition_id(); |
495 | 0 | context.enable_unique_key_merge_on_write = enable_unique_key_merge_on_write(); |
496 | 0 | return RowsetFactory::create_rowset_writer(_engine, context, vertical); |
497 | 0 | } |
498 | | |
499 | | // create a rowset writer with rowset_id and seg_id |
500 | | // after writer, merge this transient rowset with original rowset |
501 | | Result<std::unique_ptr<RowsetWriter>> CloudTablet::create_transient_rowset_writer( |
502 | | const Rowset& rowset, std::shared_ptr<PartialUpdateInfo> partial_update_info, |
503 | 0 | int64_t txn_expiration) { |
504 | 0 | if (rowset.rowset_meta_state() != RowsetStatePB::BEGIN_PARTIAL_UPDATE && |
505 | 0 | rowset.rowset_meta_state() != RowsetStatePB::COMMITTED) [[unlikely]] { |
506 | 0 | auto msg = fmt::format( |
507 | 0 | "wrong rowset state when create_transient_rowset_writer, rowset state should be " |
508 | 0 | "BEGIN_PARTIAL_UPDATE or COMMITTED, but found {}, rowset_id={}, tablet_id={}", |
509 | 0 | RowsetStatePB_Name(rowset.rowset_meta_state()), rowset.rowset_id().to_string(), |
510 | 0 | tablet_id()); |
511 | | // see `CloudRowsetWriter::build` for detail. |
512 | | // if this is in a retry task, the rowset state may have been changed to RowsetStatePB::COMMITTED |
513 | | // in `RowsetMeta::merge_rowset_meta()` in previous trials. |
514 | 0 | LOG(WARNING) << msg; |
515 | 0 | DCHECK(false) << msg; |
516 | 0 | } |
517 | 0 | RowsetWriterContext context; |
518 | 0 | context.rowset_state = PREPARED; |
519 | 0 | context.segments_overlap = OVERLAPPING; |
520 | | // During a partial update, the extracted columns of a variant should not be included in the tablet schema. |
521 | | // This is because the partial update for a variant needs to ignore the extracted columns. |
522 | | // Otherwise, the schema types in different rowsets might be inconsistent. When performing a partial update, |
523 | | // the complete variant is constructed by reading all the sub-columns of the variant. |
524 | 0 | context.tablet_schema = rowset.tablet_schema()->copy_without_variant_extracted_columns(); |
525 | 0 | context.newest_write_timestamp = UnixSeconds(); |
526 | 0 | context.tablet_id = table_id(); |
527 | 0 | context.enable_segcompaction = false; |
528 | 0 | context.write_type = DataWriteType::TYPE_DIRECT; |
529 | 0 | context.partial_update_info = std::move(partial_update_info); |
530 | 0 | context.is_transient_rowset_writer = true; |
531 | 0 | context.rowset_id = rowset.rowset_id(); |
532 | 0 | context.tablet_id = tablet_id(); |
533 | 0 | context.index_id = index_id(); |
534 | 0 | context.partition_id = partition_id(); |
535 | 0 | context.enable_unique_key_merge_on_write = enable_unique_key_merge_on_write(); |
536 | 0 | context.txn_expiration = txn_expiration; |
537 | |
|
538 | 0 | auto storage_resource = rowset.rowset_meta()->remote_storage_resource(); |
539 | 0 | if (!storage_resource) { |
540 | 0 | return ResultError(std::move(storage_resource.error())); |
541 | 0 | } |
542 | | |
543 | 0 | context.storage_resource = *storage_resource.value(); |
544 | |
|
545 | 0 | return RowsetFactory::create_rowset_writer(_engine, context, false) |
546 | 0 | .transform([&](auto&& writer) { |
547 | 0 | writer->set_segment_start_id(cast_set<int32_t>(rowset.num_segments())); |
548 | 0 | return writer; |
549 | 0 | }); |
550 | 0 | } |
551 | | |
552 | 0 | int64_t CloudTablet::get_cloud_base_compaction_score() const { |
553 | 0 | if (_tablet_meta->compaction_policy() == CUMULATIVE_TIME_SERIES_POLICY) { |
554 | 0 | bool has_delete = false; |
555 | 0 | int64_t point = cumulative_layer_point(); |
556 | 0 | std::shared_lock<std::shared_mutex> rlock(_meta_lock); |
557 | 0 | for (const auto& rs_meta : _tablet_meta->all_rs_metas()) { |
558 | 0 | if (rs_meta->start_version() >= point) { |
559 | 0 | continue; |
560 | 0 | } |
561 | 0 | if (rs_meta->has_delete_predicate()) { |
562 | 0 | has_delete = true; |
563 | 0 | break; |
564 | 0 | } |
565 | 0 | } |
566 | 0 | if (!has_delete) { |
567 | 0 | return 0; |
568 | 0 | } |
569 | 0 | } |
570 | | |
571 | 0 | return _approximate_num_rowsets.load(std::memory_order_relaxed) - |
572 | 0 | _approximate_cumu_num_rowsets.load(std::memory_order_relaxed); |
573 | 0 | } |
574 | | |
575 | 0 | int64_t CloudTablet::get_cloud_cumu_compaction_score() const { |
576 | | // TODO(plat1ko): Propose an algorithm that considers tablet's key type, number of delete rowsets, |
577 | | // number of tablet versions simultaneously. |
578 | 0 | return _approximate_cumu_num_deltas.load(std::memory_order_relaxed); |
579 | 0 | } |
580 | | |
581 | | // return a json string to show the compaction status of this tablet |
582 | 0 | void CloudTablet::get_compaction_status(std::string* json_result) { |
583 | 0 | rapidjson::Document root; |
584 | 0 | root.SetObject(); |
585 | |
|
586 | 0 | rapidjson::Document path_arr; |
587 | 0 | path_arr.SetArray(); |
588 | |
|
589 | 0 | std::vector<RowsetSharedPtr> rowsets; |
590 | 0 | std::vector<RowsetSharedPtr> stale_rowsets; |
591 | 0 | { |
592 | 0 | std::shared_lock rdlock(_meta_lock); |
593 | 0 | rowsets.reserve(_rs_version_map.size()); |
594 | 0 | for (auto& it : _rs_version_map) { |
595 | 0 | rowsets.push_back(it.second); |
596 | 0 | } |
597 | 0 | stale_rowsets.reserve(_stale_rs_version_map.size()); |
598 | 0 | for (auto& it : _stale_rs_version_map) { |
599 | 0 | stale_rowsets.push_back(it.second); |
600 | 0 | } |
601 | 0 | } |
602 | 0 | std::sort(rowsets.begin(), rowsets.end(), Rowset::comparator); |
603 | 0 | std::sort(stale_rowsets.begin(), stale_rowsets.end(), Rowset::comparator); |
604 | | |
605 | | // get snapshot version path json_doc |
606 | 0 | _timestamped_version_tracker.get_stale_version_path_json_doc(path_arr); |
607 | 0 | root.AddMember("cumulative point", _cumulative_point.load(), root.GetAllocator()); |
608 | | |
609 | | // print all rowsets' version as an array |
610 | 0 | rapidjson::Document versions_arr; |
611 | 0 | rapidjson::Document missing_versions_arr; |
612 | 0 | versions_arr.SetArray(); |
613 | 0 | missing_versions_arr.SetArray(); |
614 | 0 | int64_t last_version = -1; |
615 | 0 | for (auto& rowset : rowsets) { |
616 | 0 | const Version& ver = rowset->version(); |
617 | 0 | if (ver.first != last_version + 1) { |
618 | 0 | rapidjson::Value miss_value; |
619 | 0 | miss_value.SetString(fmt::format("[{}-{}]", last_version + 1, ver.first - 1).c_str(), |
620 | 0 | missing_versions_arr.GetAllocator()); |
621 | 0 | missing_versions_arr.PushBack(miss_value, missing_versions_arr.GetAllocator()); |
622 | 0 | } |
623 | 0 | rapidjson::Value value; |
624 | 0 | std::string version_str = rowset->get_rowset_info_str(); |
625 | 0 | value.SetString(version_str.c_str(), cast_set<uint32_t>(version_str.length()), |
626 | 0 | versions_arr.GetAllocator()); |
627 | 0 | versions_arr.PushBack(value, versions_arr.GetAllocator()); |
628 | 0 | last_version = ver.second; |
629 | 0 | } |
630 | 0 | root.AddMember("rowsets", versions_arr, root.GetAllocator()); |
631 | 0 | root.AddMember("missing_rowsets", missing_versions_arr, root.GetAllocator()); |
632 | | |
633 | | // print all stale rowsets' version as an array |
634 | 0 | rapidjson::Document stale_versions_arr; |
635 | 0 | stale_versions_arr.SetArray(); |
636 | 0 | for (auto& rowset : stale_rowsets) { |
637 | 0 | rapidjson::Value value; |
638 | 0 | std::string version_str = rowset->get_rowset_info_str(); |
639 | 0 | value.SetString(version_str.c_str(), cast_set<uint32_t>(version_str.length()), |
640 | 0 | stale_versions_arr.GetAllocator()); |
641 | 0 | stale_versions_arr.PushBack(value, stale_versions_arr.GetAllocator()); |
642 | 0 | } |
643 | 0 | root.AddMember("stale_rowsets", stale_versions_arr, root.GetAllocator()); |
644 | | |
645 | | // add stale version rowsets |
646 | 0 | root.AddMember("stale version path", path_arr, root.GetAllocator()); |
647 | | |
648 | | // to json string |
649 | 0 | rapidjson::StringBuffer strbuf; |
650 | 0 | rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(strbuf); |
651 | 0 | root.Accept(writer); |
652 | 0 | *json_result = std::string(strbuf.GetString()); |
653 | 0 | } |
654 | | |
655 | 0 | void CloudTablet::set_cumulative_layer_point(int64_t new_point) { |
656 | 0 | if (new_point == Tablet::K_INVALID_CUMULATIVE_POINT || new_point >= _cumulative_point) { |
657 | 0 | _cumulative_point = new_point; |
658 | 0 | return; |
659 | 0 | } |
660 | | // cumulative point should only be reset to -1, or be increased |
661 | | // FIXME: could happen in currently unresolved race conditions |
662 | 0 | LOG(WARNING) << "Unexpected cumulative point: " << new_point |
663 | 0 | << ", origin: " << _cumulative_point.load(); |
664 | 0 | } |
665 | | |
666 | 0 | std::vector<RowsetSharedPtr> CloudTablet::pick_candidate_rowsets_to_base_compaction() { |
667 | 0 | std::vector<RowsetSharedPtr> candidate_rowsets; |
668 | 0 | { |
669 | 0 | std::shared_lock rlock(_meta_lock); |
670 | 0 | for (const auto& [version, rs] : _rs_version_map) { |
671 | 0 | if (version.first != 0 && version.first < _cumulative_point && |
672 | 0 | (_alter_version == -1 || version.second <= _alter_version)) { |
673 | 0 | candidate_rowsets.push_back(rs); |
674 | 0 | } |
675 | 0 | } |
676 | 0 | } |
677 | 0 | std::sort(candidate_rowsets.begin(), candidate_rowsets.end(), Rowset::comparator); |
678 | 0 | return candidate_rowsets; |
679 | 0 | } |
680 | | |
681 | 0 | std::vector<RowsetSharedPtr> CloudTablet::pick_candidate_rowsets_to_full_compaction() { |
682 | 0 | std::vector<RowsetSharedPtr> candidate_rowsets; |
683 | 0 | { |
684 | 0 | std::shared_lock rlock(_meta_lock); |
685 | 0 | for (auto& [v, rs] : _rs_version_map) { |
686 | | // MUST NOT compact rowset [0-1] for some historical reasons (see cloud_schema_change) |
687 | 0 | if (v.first != 0) { |
688 | 0 | candidate_rowsets.push_back(rs); |
689 | 0 | } |
690 | 0 | } |
691 | 0 | } |
692 | 0 | std::sort(candidate_rowsets.begin(), candidate_rowsets.end(), Rowset::comparator); |
693 | 0 | return candidate_rowsets; |
694 | 0 | } |
695 | | |
696 | 0 | CalcDeleteBitmapExecutor* CloudTablet::calc_delete_bitmap_executor() { |
697 | 0 | return _engine.calc_delete_bitmap_executor(); |
698 | 0 | } |
699 | | |
700 | | Status CloudTablet::save_delete_bitmap(const TabletTxnInfo* txn_info, int64_t txn_id, |
701 | | DeleteBitmapPtr delete_bitmap, RowsetWriter* rowset_writer, |
702 | | const RowsetIdUnorderedSet& cur_rowset_ids, int64_t lock_id, |
703 | 0 | int64_t next_visible_version) { |
704 | 0 | RowsetSharedPtr rowset = txn_info->rowset; |
705 | 0 | int64_t cur_version = rowset->start_version(); |
706 | | // update delete bitmap info, in order to avoid recalculation when trying again |
707 | 0 | RETURN_IF_ERROR(_engine.txn_delete_bitmap_cache().update_tablet_txn_info( |
708 | 0 | txn_id, tablet_id(), delete_bitmap, cur_rowset_ids, PublishStatus::PREPARE)); |
709 | | |
710 | 0 | if (txn_info->partial_update_info && txn_info->partial_update_info->is_partial_update() && |
711 | 0 | rowset_writer->num_rows() > 0) { |
712 | 0 | DBUG_EXECUTE_IF("CloudTablet::save_delete_bitmap.update_tmp_rowset.error", { |
713 | 0 | return Status::InternalError<false>("injected update_tmp_rowset error."); |
714 | 0 | }); |
715 | 0 | const auto& rowset_meta = rowset->rowset_meta(); |
716 | 0 | RETURN_IF_ERROR(_engine.meta_mgr().update_tmp_rowset(*rowset_meta)); |
717 | 0 | } |
718 | | |
719 | 0 | RETURN_IF_ERROR(save_delete_bitmap_to_ms(cur_version, txn_id, delete_bitmap, lock_id, |
720 | 0 | next_visible_version)); |
721 | | |
722 | | // store the delete bitmap with sentinel marks in txn_delete_bitmap_cache because if the txn is retried for some reason, |
723 | | // it will use the delete bitmap from txn_delete_bitmap_cache when re-calculating the delete bitmap, during which it will do |
724 | | // delete bitmap correctness check. If we store the new_delete_bitmap, the delete bitmap correctness check will fail |
725 | 0 | RETURN_IF_ERROR(_engine.txn_delete_bitmap_cache().update_tablet_txn_info( |
726 | 0 | txn_id, tablet_id(), delete_bitmap, cur_rowset_ids, PublishStatus::SUCCEED, |
727 | 0 | txn_info->publish_info)); |
728 | | |
729 | 0 | DBUG_EXECUTE_IF("CloudTablet::save_delete_bitmap.enable_sleep", { |
730 | 0 | auto sleep_sec = dp->param<int>("sleep", 5); |
731 | 0 | std::this_thread::sleep_for(std::chrono::seconds(sleep_sec)); |
732 | 0 | }); |
733 | |
|
734 | 0 | DBUG_EXECUTE_IF("CloudTablet::save_delete_bitmap.injected_error", { |
735 | 0 | auto retry = dp->param<bool>("retry", false); |
736 | 0 | auto sleep_sec = dp->param<int>("sleep", 0); |
737 | 0 | std::this_thread::sleep_for(std::chrono::seconds(sleep_sec)); |
738 | 0 | if (retry) { // return DELETE_BITMAP_LOCK_ERROR to let it retry |
739 | 0 | return Status::Error<ErrorCode::DELETE_BITMAP_LOCK_ERROR>( |
740 | 0 | "injected DELETE_BITMAP_LOCK_ERROR"); |
741 | 0 | } else { |
742 | 0 | return Status::InternalError<false>("injected non-retryable error"); |
743 | 0 | } |
744 | 0 | }); |
745 | |
|
746 | 0 | return Status::OK(); |
747 | 0 | } |
748 | | |
749 | | Status CloudTablet::save_delete_bitmap_to_ms(int64_t cur_version, int64_t txn_id, |
750 | | DeleteBitmapPtr delete_bitmap, int64_t lock_id, |
751 | 0 | int64_t next_visible_version) { |
752 | 0 | DeleteBitmapPtr new_delete_bitmap = std::make_shared<DeleteBitmap>(tablet_id()); |
753 | 0 | for (auto iter = delete_bitmap->delete_bitmap.begin(); |
754 | 0 | iter != delete_bitmap->delete_bitmap.end(); ++iter) { |
755 | | // skip sentinel mark, which is used for delete bitmap correctness check |
756 | 0 | if (std::get<1>(iter->first) != DeleteBitmap::INVALID_SEGMENT_ID) { |
757 | 0 | new_delete_bitmap->merge( |
758 | 0 | {std::get<0>(iter->first), std::get<1>(iter->first), cur_version}, |
759 | 0 | iter->second); |
760 | 0 | } |
761 | 0 | } |
762 | | // lock_id != -1 means this is in an explict txn |
763 | 0 | bool is_explicit_txn = (lock_id != -1); |
764 | 0 | auto ms_lock_id = !is_explicit_txn ? txn_id : lock_id; |
765 | |
|
766 | 0 | RETURN_IF_ERROR(_engine.meta_mgr().update_delete_bitmap(*this, ms_lock_id, LOAD_INITIATOR_ID, |
767 | 0 | new_delete_bitmap.get(), txn_id, |
768 | 0 | is_explicit_txn, next_visible_version)); |
769 | 0 | return Status::OK(); |
770 | 0 | } |
771 | | |
772 | 0 | Versions CloudTablet::calc_missed_versions(int64_t spec_version, Versions existing_versions) const { |
773 | 0 | DCHECK(spec_version > 0) << "invalid spec_version: " << spec_version; |
774 | | |
775 | | // sort the existing versions in ascending order |
776 | 0 | std::sort(existing_versions.begin(), existing_versions.end(), |
777 | 0 | [](const Version& a, const Version& b) { |
778 | | // simple because 2 versions are certainly not overlapping |
779 | 0 | return a.first < b.first; |
780 | 0 | }); |
781 | | |
782 | | // From the first version(=0), find the missing version until spec_version |
783 | 0 | int64_t last_version = -1; |
784 | 0 | Versions missed_versions; |
785 | 0 | for (const Version& version : existing_versions) { |
786 | 0 | if (version.first > last_version + 1) { |
787 | | // there is a hole between versions |
788 | 0 | missed_versions.emplace_back(last_version + 1, std::min(version.first, spec_version)); |
789 | 0 | } |
790 | 0 | last_version = version.second; |
791 | 0 | if (last_version >= spec_version) { |
792 | 0 | break; |
793 | 0 | } |
794 | 0 | } |
795 | 0 | if (last_version < spec_version) { |
796 | | // there is a hole between the last version and the specificed version. |
797 | 0 | missed_versions.emplace_back(last_version + 1, spec_version); |
798 | 0 | } |
799 | 0 | return missed_versions; |
800 | 0 | } |
801 | | |
802 | | Status CloudTablet::calc_delete_bitmap_for_compaction( |
803 | | const std::vector<RowsetSharedPtr>& input_rowsets, const RowsetSharedPtr& output_rowset, |
804 | | const RowIdConversion& rowid_conversion, ReaderType compaction_type, int64_t merged_rows, |
805 | | int64_t filtered_rows, int64_t initiator, DeleteBitmapPtr& output_rowset_delete_bitmap, |
806 | 0 | bool allow_delete_in_cumu_compaction) { |
807 | 0 | output_rowset_delete_bitmap = std::make_shared<DeleteBitmap>(tablet_id()); |
808 | 0 | std::unique_ptr<RowLocationSet> missed_rows; |
809 | 0 | if ((config::enable_missing_rows_correctness_check || |
810 | 0 | config::enable_mow_compaction_correctness_check_core) && |
811 | 0 | !allow_delete_in_cumu_compaction && |
812 | 0 | compaction_type == ReaderType::READER_CUMULATIVE_COMPACTION) { |
813 | 0 | missed_rows = std::make_unique<RowLocationSet>(); |
814 | 0 | LOG(INFO) << "RowLocation Set inited succ for tablet:" << tablet_id(); |
815 | 0 | } |
816 | |
|
817 | 0 | std::unique_ptr<std::map<RowsetSharedPtr, RowLocationPairList>> location_map; |
818 | 0 | if (config::enable_rowid_conversion_correctness_check && |
819 | 0 | tablet_schema()->cluster_key_uids().empty()) { |
820 | 0 | location_map = std::make_unique<std::map<RowsetSharedPtr, RowLocationPairList>>(); |
821 | 0 | LOG(INFO) << "Location Map inited succ for tablet:" << tablet_id(); |
822 | 0 | } |
823 | | |
824 | | // 1. calc delete bitmap for historical data |
825 | 0 | RETURN_IF_ERROR(_engine.meta_mgr().sync_tablet_rowsets(this)); |
826 | 0 | Version version = max_version(); |
827 | 0 | std::size_t missed_rows_size = 0; |
828 | 0 | calc_compaction_output_rowset_delete_bitmap( |
829 | 0 | input_rowsets, rowid_conversion, 0, version.second + 1, missed_rows.get(), |
830 | 0 | location_map.get(), tablet_meta()->delete_bitmap(), output_rowset_delete_bitmap.get()); |
831 | 0 | if (missed_rows) { |
832 | 0 | missed_rows_size = missed_rows->size(); |
833 | 0 | if (!allow_delete_in_cumu_compaction) { |
834 | 0 | if (compaction_type == ReaderType::READER_CUMULATIVE_COMPACTION && |
835 | 0 | tablet_state() == TABLET_RUNNING) { |
836 | 0 | if (merged_rows + filtered_rows >= 0 && |
837 | 0 | merged_rows + filtered_rows != missed_rows_size) { |
838 | 0 | std::string err_msg = fmt::format( |
839 | 0 | "cumulative compaction: the merged rows({}), the filtered rows({}) is " |
840 | 0 | "not equal to missed rows({}) in rowid conversion, tablet_id: {}, " |
841 | 0 | "table_id:{}", |
842 | 0 | merged_rows, filtered_rows, missed_rows_size, tablet_id(), table_id()); |
843 | 0 | if (config::enable_mow_compaction_correctness_check_core) { |
844 | 0 | CHECK(false) << err_msg; |
845 | 0 | } else { |
846 | 0 | DCHECK(false) << err_msg; |
847 | 0 | } |
848 | 0 | LOG(WARNING) << err_msg; |
849 | 0 | } |
850 | 0 | } |
851 | 0 | } |
852 | 0 | } |
853 | 0 | if (location_map) { |
854 | 0 | RETURN_IF_ERROR(check_rowid_conversion(output_rowset, *location_map)); |
855 | 0 | location_map->clear(); |
856 | 0 | } |
857 | | |
858 | | // 2. calc delete bitmap for incremental data |
859 | 0 | int64_t t1 = MonotonicMicros(); |
860 | 0 | RETURN_IF_ERROR(_engine.meta_mgr().get_delete_bitmap_update_lock( |
861 | 0 | *this, COMPACTION_DELETE_BITMAP_LOCK_ID, initiator)); |
862 | 0 | int64_t t2 = MonotonicMicros(); |
863 | 0 | RETURN_IF_ERROR(_engine.meta_mgr().sync_tablet_rowsets(this)); |
864 | 0 | int64_t t3 = MonotonicMicros(); |
865 | |
|
866 | 0 | calc_compaction_output_rowset_delete_bitmap( |
867 | 0 | input_rowsets, rowid_conversion, version.second, UINT64_MAX, missed_rows.get(), |
868 | 0 | location_map.get(), tablet_meta()->delete_bitmap(), output_rowset_delete_bitmap.get()); |
869 | 0 | int64_t t4 = MonotonicMicros(); |
870 | 0 | if (location_map) { |
871 | 0 | RETURN_IF_ERROR(check_rowid_conversion(output_rowset, *location_map)); |
872 | 0 | } |
873 | 0 | int64_t t5 = MonotonicMicros(); |
874 | 0 | if (missed_rows) { |
875 | 0 | DCHECK_EQ(missed_rows->size(), missed_rows_size); |
876 | 0 | if (missed_rows->size() != missed_rows_size) { |
877 | 0 | LOG(WARNING) << "missed rows don't match, before: " << missed_rows_size |
878 | 0 | << " after: " << missed_rows->size(); |
879 | 0 | } |
880 | 0 | } |
881 | | |
882 | | // 3. store delete bitmap |
883 | 0 | auto st = _engine.meta_mgr().update_delete_bitmap(*this, -1, initiator, |
884 | 0 | output_rowset_delete_bitmap.get()); |
885 | 0 | int64_t t6 = MonotonicMicros(); |
886 | 0 | LOG(INFO) << "calc_delete_bitmap_for_compaction, tablet_id=" << tablet_id() |
887 | 0 | << ", get lock cost " << (t2 - t1) << " us, sync rowsets cost " << (t3 - t2) |
888 | 0 | << " us, calc delete bitmap cost " << (t4 - t3) << " us, check rowid conversion cost " |
889 | 0 | << (t5 - t4) << " us, store delete bitmap cost " << (t6 - t5) |
890 | 0 | << " us, st=" << st.to_string(); |
891 | 0 | return st; |
892 | 0 | } |
893 | | |
894 | 0 | Status CloudTablet::sync_meta() { |
895 | 0 | if (!config::enable_file_cache) { |
896 | 0 | return Status::OK(); |
897 | 0 | } |
898 | | |
899 | 0 | TabletMetaSharedPtr tablet_meta; |
900 | 0 | auto st = _engine.meta_mgr().get_tablet_meta(tablet_id(), &tablet_meta); |
901 | 0 | if (!st.ok()) { |
902 | 0 | if (st.is<ErrorCode::NOT_FOUND>()) { |
903 | 0 | clear_cache(); |
904 | 0 | } |
905 | 0 | return st; |
906 | 0 | } |
907 | | |
908 | 0 | auto new_ttl_seconds = tablet_meta->ttl_seconds(); |
909 | 0 | if (_tablet_meta->ttl_seconds() != new_ttl_seconds) { |
910 | 0 | _tablet_meta->set_ttl_seconds(new_ttl_seconds); |
911 | 0 | int64_t cur_time = UnixSeconds(); |
912 | 0 | std::shared_lock rlock(_meta_lock); |
913 | 0 | for (auto& [_, rs] : _rs_version_map) { |
914 | 0 | for (int seg_id = 0; seg_id < rs->num_segments(); ++seg_id) { |
915 | 0 | int64_t new_expiration_time = |
916 | 0 | new_ttl_seconds + rs->rowset_meta()->newest_write_timestamp(); |
917 | 0 | new_expiration_time = new_expiration_time > cur_time ? new_expiration_time : 0; |
918 | 0 | auto file_key = Segment::file_cache_key(rs->rowset_id().to_string(), seg_id); |
919 | 0 | auto* file_cache = io::FileCacheFactory::instance()->get_by_path(file_key); |
920 | 0 | file_cache->modify_expiration_time(file_key, new_expiration_time); |
921 | 0 | } |
922 | 0 | } |
923 | 0 | } |
924 | |
|
925 | 0 | auto new_compaction_policy = tablet_meta->compaction_policy(); |
926 | 0 | if (_tablet_meta->compaction_policy() != new_compaction_policy) { |
927 | 0 | _tablet_meta->set_compaction_policy(new_compaction_policy); |
928 | 0 | } |
929 | 0 | auto new_time_series_compaction_goal_size_mbytes = |
930 | 0 | tablet_meta->time_series_compaction_goal_size_mbytes(); |
931 | 0 | if (_tablet_meta->time_series_compaction_goal_size_mbytes() != |
932 | 0 | new_time_series_compaction_goal_size_mbytes) { |
933 | 0 | _tablet_meta->set_time_series_compaction_goal_size_mbytes( |
934 | 0 | new_time_series_compaction_goal_size_mbytes); |
935 | 0 | } |
936 | 0 | auto new_time_series_compaction_file_count_threshold = |
937 | 0 | tablet_meta->time_series_compaction_file_count_threshold(); |
938 | 0 | if (_tablet_meta->time_series_compaction_file_count_threshold() != |
939 | 0 | new_time_series_compaction_file_count_threshold) { |
940 | 0 | _tablet_meta->set_time_series_compaction_file_count_threshold( |
941 | 0 | new_time_series_compaction_file_count_threshold); |
942 | 0 | } |
943 | 0 | auto new_time_series_compaction_time_threshold_seconds = |
944 | 0 | tablet_meta->time_series_compaction_time_threshold_seconds(); |
945 | 0 | if (_tablet_meta->time_series_compaction_time_threshold_seconds() != |
946 | 0 | new_time_series_compaction_time_threshold_seconds) { |
947 | 0 | _tablet_meta->set_time_series_compaction_time_threshold_seconds( |
948 | 0 | new_time_series_compaction_time_threshold_seconds); |
949 | 0 | } |
950 | 0 | auto new_time_series_compaction_empty_rowsets_threshold = |
951 | 0 | tablet_meta->time_series_compaction_empty_rowsets_threshold(); |
952 | 0 | if (_tablet_meta->time_series_compaction_empty_rowsets_threshold() != |
953 | 0 | new_time_series_compaction_empty_rowsets_threshold) { |
954 | 0 | _tablet_meta->set_time_series_compaction_empty_rowsets_threshold( |
955 | 0 | new_time_series_compaction_empty_rowsets_threshold); |
956 | 0 | } |
957 | 0 | auto new_time_series_compaction_level_threshold = |
958 | 0 | tablet_meta->time_series_compaction_level_threshold(); |
959 | 0 | if (_tablet_meta->time_series_compaction_level_threshold() != |
960 | 0 | new_time_series_compaction_level_threshold) { |
961 | 0 | _tablet_meta->set_time_series_compaction_level_threshold( |
962 | 0 | new_time_series_compaction_level_threshold); |
963 | 0 | } |
964 | |
|
965 | 0 | return Status::OK(); |
966 | 0 | } |
967 | | |
968 | 0 | void CloudTablet::build_tablet_report_info(TTabletInfo* tablet_info) { |
969 | 0 | std::shared_lock rdlock(_meta_lock); |
970 | 0 | tablet_info->__set_total_version_count(_tablet_meta->version_count()); |
971 | 0 | tablet_info->__set_tablet_id(_tablet_meta->tablet_id()); |
972 | | // Currently, this information will not be used by the cloud report, |
973 | | // but it may be used in the future. |
974 | 0 | } |
975 | | |
976 | | Status CloudTablet::check_delete_bitmap_cache(int64_t txn_id, |
977 | 0 | DeleteBitmap* expected_delete_bitmap) { |
978 | 0 | DeleteBitmapPtr cached_delete_bitmap; |
979 | 0 | CloudStorageEngine& engine = ExecEnv::GetInstance()->storage_engine().to_cloud(); |
980 | 0 | Status st = engine.txn_delete_bitmap_cache().get_delete_bitmap( |
981 | 0 | txn_id, tablet_id(), &cached_delete_bitmap, nullptr, nullptr); |
982 | 0 | if (st.ok()) { |
983 | 0 | bool res = (expected_delete_bitmap->cardinality() == cached_delete_bitmap->cardinality()); |
984 | 0 | auto msg = fmt::format( |
985 | 0 | "delete bitmap cache check failed, cur_cardinality={}, cached_cardinality={}" |
986 | 0 | "txn_id={}, tablet_id={}", |
987 | 0 | expected_delete_bitmap->cardinality(), cached_delete_bitmap->cardinality(), txn_id, |
988 | 0 | tablet_id()); |
989 | 0 | if (!res) { |
990 | 0 | DCHECK(res) << msg; |
991 | 0 | return Status::InternalError<false>(msg); |
992 | 0 | } |
993 | 0 | } |
994 | 0 | return Status::OK(); |
995 | 0 | } |
996 | | |
997 | | #include "common/compile_check_end.h" |
998 | | } // namespace doris |