/root/doris/be/src/cloud/cloud_tablet.cpp
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 | | #include "cloud/cloud_tablet.h" |
19 | | |
20 | | #include <bvar/bvar.h> |
21 | | #include <bvar/latency_recorder.h> |
22 | | #include <gen_cpp/Types_types.h> |
23 | | #include <gen_cpp/olap_file.pb.h> |
24 | | #include <rapidjson/document.h> |
25 | | #include <rapidjson/encodings.h> |
26 | | #include <rapidjson/prettywriter.h> |
27 | | #include <rapidjson/rapidjson.h> |
28 | | #include <rapidjson/stringbuffer.h> |
29 | | |
30 | | #include <atomic> |
31 | | #include <chrono> |
32 | | #include <cstdint> |
33 | | #include <memory> |
34 | | #include <ranges> |
35 | | #include <ratio> |
36 | | #include <shared_mutex> |
37 | | #include <unordered_map> |
38 | | #include <vector> |
39 | | |
40 | | #include "cloud/cloud_meta_mgr.h" |
41 | | #include "cloud/cloud_storage_engine.h" |
42 | | #include "cloud/cloud_tablet_mgr.h" |
43 | | #include "cloud/cloud_warm_up_manager.h" |
44 | | #include "common/cast_set.h" |
45 | | #include "common/config.h" |
46 | | #include "common/logging.h" |
47 | | #include "cpp/sync_point.h" |
48 | | #include "io/cache/block_file_cache_downloader.h" |
49 | | #include "io/cache/block_file_cache_factory.h" |
50 | | #include "olap/compaction.h" |
51 | | #include "olap/cumulative_compaction_time_series_policy.h" |
52 | | #include "olap/olap_define.h" |
53 | | #include "olap/rowset/beta_rowset.h" |
54 | | #include "olap/rowset/rowset.h" |
55 | | #include "olap/rowset/rowset_factory.h" |
56 | | #include "olap/rowset/rowset_fwd.h" |
57 | | #include "olap/rowset/rowset_writer.h" |
58 | | #include "olap/rowset/segment_v2/inverted_index_desc.h" |
59 | | #include "olap/storage_policy.h" |
60 | | #include "olap/tablet_schema.h" |
61 | | #include "olap/txn_manager.h" |
62 | | #include "util/debug_points.h" |
63 | | #include "vec/common/schema_util.h" |
64 | | |
65 | | namespace doris { |
66 | | #include "common/compile_check_begin.h" |
67 | | using namespace ErrorCode; |
68 | | |
69 | | bvar::LatencyRecorder g_cu_compaction_get_delete_bitmap_lock_time_ms( |
70 | | "cu_compaction_get_delete_bitmap_lock_time_ms"); |
71 | | bvar::LatencyRecorder g_base_compaction_get_delete_bitmap_lock_time_ms( |
72 | | "base_compaction_get_delete_bitmap_lock_time_ms"); |
73 | | |
74 | | bvar::Adder<int64_t> g_unused_rowsets_count("unused_rowsets_count"); |
75 | | bvar::Adder<int64_t> g_unused_rowsets_bytes("unused_rowsets_bytes"); |
76 | | |
77 | | bvar::Adder<int64_t> g_capture_prefer_cache_count("capture_prefer_cache_count"); |
78 | | bvar::Adder<int64_t> g_capture_with_freshness_tolerance_count( |
79 | | "capture_with_freshness_tolerance_count"); |
80 | | bvar::Adder<int64_t> g_capture_with_freshness_tolerance_fallback_count( |
81 | | "capture_with_freshness_tolerance_fallback_count"); |
82 | | bvar::Window<bvar::Adder<int64_t>> g_capture_prefer_cache_count_window( |
83 | | "capture_prefer_cache_count_window", &g_capture_prefer_cache_count, 30); |
84 | | bvar::Window<bvar::Adder<int64_t>> g_capture_with_freshness_tolerance_count_window( |
85 | | "capture_with_freshness_tolerance_count_window", &g_capture_with_freshness_tolerance_count, |
86 | | 30); |
87 | | bvar::Window<bvar::Adder<int64_t>> g_capture_with_freshness_tolerance_fallback_count_window( |
88 | | "capture_with_freshness_tolerance_fallback_count_window", |
89 | | &g_capture_with_freshness_tolerance_fallback_count, 30); |
90 | | |
91 | | static constexpr int LOAD_INITIATOR_ID = -1; |
92 | | |
93 | | bvar::Adder<uint64_t> g_file_cache_cloud_tablet_submitted_segment_size( |
94 | | "file_cache_cloud_tablet_submitted_segment_size"); |
95 | | bvar::Adder<uint64_t> g_file_cache_cloud_tablet_submitted_segment_num( |
96 | | "file_cache_cloud_tablet_submitted_segment_num"); |
97 | | bvar::Adder<uint64_t> g_file_cache_cloud_tablet_submitted_index_size( |
98 | | "file_cache_cloud_tablet_submitted_index_size"); |
99 | | bvar::Adder<uint64_t> g_file_cache_cloud_tablet_submitted_index_num( |
100 | | "file_cache_cloud_tablet_submitted_index_num"); |
101 | | bvar::Adder<uint64_t> g_file_cache_cloud_tablet_finished_segment_size( |
102 | | "file_cache_cloud_tablet_finished_segment_size"); |
103 | | bvar::Adder<uint64_t> g_file_cache_cloud_tablet_finished_segment_num( |
104 | | "file_cache_cloud_tablet_finished_segment_num"); |
105 | | bvar::Adder<uint64_t> g_file_cache_cloud_tablet_finished_index_size( |
106 | | "file_cache_cloud_tablet_finished_index_size"); |
107 | | bvar::Adder<uint64_t> g_file_cache_cloud_tablet_finished_index_num( |
108 | | "file_cache_cloud_tablet_finished_index_num"); |
109 | | |
110 | | bvar::Adder<uint64_t> g_file_cache_recycle_cached_data_segment_num( |
111 | | "file_cache_recycle_cached_data_segment_num"); |
112 | | bvar::Adder<uint64_t> g_file_cache_recycle_cached_data_segment_size( |
113 | | "file_cache_recycle_cached_data_segment_size"); |
114 | | bvar::Adder<uint64_t> g_file_cache_recycle_cached_data_index_num( |
115 | | "file_cache_recycle_cached_data_index_num"); |
116 | | |
117 | | bvar::Adder<uint64_t> g_file_cache_warm_up_segment_complete_num( |
118 | | "file_cache_warm_up_segment_complete_num"); |
119 | | bvar::Adder<uint64_t> g_file_cache_warm_up_segment_failed_num( |
120 | | "file_cache_warm_up_segment_failed_num"); |
121 | | bvar::Adder<uint64_t> g_file_cache_warm_up_inverted_idx_complete_num( |
122 | | "file_cache_warm_up_inverted_idx_complete_num"); |
123 | | bvar::Adder<uint64_t> g_file_cache_warm_up_inverted_idx_failed_num( |
124 | | "file_cache_warm_up_inverted_idx_failed_num"); |
125 | | bvar::Adder<uint64_t> g_file_cache_warm_up_rowset_complete_num( |
126 | | "file_cache_warm_up_rowset_complete_num"); |
127 | | bvar::Adder<uint64_t> g_file_cache_warm_up_rowset_triggered_by_job_num( |
128 | | "file_cache_warm_up_rowset_triggered_by_job_num"); |
129 | | bvar::Adder<uint64_t> g_file_cache_warm_up_rowset_triggered_by_sync_rowset_num( |
130 | | "file_cache_warm_up_rowset_triggered_by_sync_rowset_num"); |
131 | | bvar::LatencyRecorder g_file_cache_warm_up_rowset_all_segments_latency( |
132 | | "file_cache_warm_up_rowset_all_segments_latency"); |
133 | | |
134 | | CloudTablet::CloudTablet(CloudStorageEngine& engine, TabletMetaSharedPtr tablet_meta) |
135 | 66 | : BaseTablet(std::move(tablet_meta)), _engine(engine) {} |
136 | | |
137 | 66 | CloudTablet::~CloudTablet() = default; |
138 | | |
139 | 0 | bool CloudTablet::exceed_version_limit(int32_t limit) { |
140 | 0 | return _approximate_num_rowsets.load(std::memory_order_relaxed) > limit; |
141 | 0 | } |
142 | | |
143 | 19 | std::string CloudTablet::tablet_path() const { |
144 | 19 | return ""; |
145 | 19 | } |
146 | | |
147 | | Status CloudTablet::capture_consistent_rowsets_unlocked( |
148 | 0 | const Version& spec_version, std::vector<RowsetSharedPtr>* rowsets) const { |
149 | 0 | Versions version_path; |
150 | 0 | auto st = _timestamped_version_tracker.capture_consistent_versions(spec_version, &version_path); |
151 | 0 | if (!st.ok()) { |
152 | | // Check no missed versions or req version is merged |
153 | 0 | auto missed_versions = get_missed_versions(spec_version.second); |
154 | 0 | if (missed_versions.empty()) { |
155 | 0 | st.set_code(VERSION_ALREADY_MERGED); // Reset error code |
156 | 0 | } |
157 | 0 | st.append(" tablet_id=" + std::to_string(tablet_id())); |
158 | 0 | return st; |
159 | 0 | } |
160 | 0 | VLOG_DEBUG << "capture consitent versions: " << version_path; |
161 | 0 | return _capture_consistent_rowsets_unlocked(version_path, rowsets); |
162 | 0 | } |
163 | | |
164 | | Status CloudTablet::capture_rs_readers(const Version& spec_version, |
165 | | std::vector<RowSetSplits>* rs_splits, |
166 | 33 | const CaptureRsReaderOptions& opts) { |
167 | 33 | if (opts.query_freshness_tolerance_ms > 0) { |
168 | 20 | return capture_rs_readers_with_freshness_tolerance(spec_version, rs_splits, |
169 | 20 | opts.query_freshness_tolerance_ms); |
170 | 20 | } else if (opts.enable_prefer_cached_rowset && !enable_unique_key_merge_on_write()) { |
171 | 13 | return capture_rs_readers_prefer_cache(spec_version, rs_splits); |
172 | 13 | } |
173 | 0 | return capture_rs_readers_internal(spec_version, rs_splits); |
174 | 33 | } |
175 | | |
176 | | Status CloudTablet::capture_rs_readers_internal(const Version& spec_version, |
177 | 5 | std::vector<RowSetSplits>* rs_splits) { |
178 | 5 | DBUG_EXECUTE_IF("CloudTablet.capture_rs_readers.return.e-230", { |
179 | 5 | LOG_WARNING("CloudTablet.capture_rs_readers.return e-230").tag("tablet_id", tablet_id()); |
180 | 5 | return Status::Error<false>(-230, "injected error"); |
181 | 5 | }); |
182 | 5 | Versions version_path; |
183 | 5 | std::shared_lock rlock(_meta_lock); |
184 | 5 | auto st = _timestamped_version_tracker.capture_consistent_versions(spec_version, &version_path); |
185 | 5 | if (!st.ok()) { |
186 | 0 | rlock.unlock(); // avoid logging in lock range |
187 | | // Check no missed versions or req version is merged |
188 | 0 | auto missed_versions = get_missed_versions(spec_version.second); |
189 | 0 | if (missed_versions.empty()) { |
190 | 0 | st.set_code(VERSION_ALREADY_MERGED); // Reset error code |
191 | 0 | st.append(" versions are already compacted, "); |
192 | 0 | } |
193 | 0 | st.append(" tablet_id=" + std::to_string(tablet_id())); |
194 | | // clang-format off |
195 | 0 | LOG(WARNING) << st << '\n' << [this]() { std::string json; get_compaction_status(&json); return json; }(); |
196 | | // clang-format on |
197 | 0 | return st; |
198 | 0 | } |
199 | 5 | VLOG_DEBUG << "capture consitent versions: " << version_path; |
200 | 5 | return capture_rs_readers_unlocked(version_path, rs_splits); |
201 | 5 | } |
202 | | |
203 | | Status CloudTablet::capture_rs_readers_prefer_cache(const Version& spec_version, |
204 | 13 | std::vector<RowSetSplits>* rs_splits) { |
205 | 13 | g_capture_prefer_cache_count << 1; |
206 | 13 | Versions version_path; |
207 | 13 | std::shared_lock rlock(_meta_lock); |
208 | 13 | RETURN_IF_ERROR(_timestamped_version_tracker.capture_consistent_versions_prefer_cache( |
209 | 13 | spec_version, version_path, |
210 | 13 | [&](int64_t start, int64_t end) { return rowset_is_warmed_up_unlocked(start, end); })); |
211 | 13 | int64_t path_max_version = version_path.back().second; |
212 | 13 | VLOG_DEBUG << fmt::format( |
213 | 0 | "[verbose] CloudTablet::capture_rs_readers_prefer_cache, capture path: {}, " |
214 | 0 | "tablet_id={}, spec_version={}, path_max_version={}", |
215 | 0 | fmt::join(version_path | std::views::transform([](const auto& version) { |
216 | 0 | return fmt::format("{}", version.to_string()); |
217 | 0 | }), |
218 | 0 | ", "), |
219 | 0 | tablet_id(), spec_version.to_string(), path_max_version); |
220 | 13 | return capture_rs_readers_unlocked(version_path, rs_splits); |
221 | 13 | } |
222 | | |
223 | 210 | bool CloudTablet::rowset_is_warmed_up_unlocked(int64_t start_version, int64_t end_version) { |
224 | 210 | if (start_version > end_version) { |
225 | 0 | return false; |
226 | 0 | } |
227 | 210 | Version version {start_version, end_version}; |
228 | 210 | auto it = _rs_version_map.find(version); |
229 | 210 | if (it == _rs_version_map.end()) { |
230 | 78 | it = _stale_rs_version_map.find(version); |
231 | 78 | if (it == _stale_rs_version_map.end()) { |
232 | 0 | LOG_WARNING( |
233 | 0 | "fail to find Rowset in rs_version or stale_rs_version for version. " |
234 | 0 | "tablet={}, version={}", |
235 | 0 | tablet_id(), version.to_string()); |
236 | 0 | return false; |
237 | 0 | } |
238 | 78 | } |
239 | 210 | const auto& rs = it->second; |
240 | 210 | if (rs->visible_timestamp() < _engine.startup_timepoint()) { |
241 | | // We only care about rowsets that are created after startup time point. For other rowsets, |
242 | | // we assume they are warmed up. |
243 | 10 | return true; |
244 | 10 | } |
245 | 200 | return is_rowset_warmed_up(rs->rowset_id()); |
246 | 210 | }; |
247 | | |
248 | | Status CloudTablet::capture_rs_readers_with_freshness_tolerance( |
249 | | const Version& spec_version, std::vector<RowSetSplits>* rs_splits, |
250 | 20 | int64_t query_freshness_tolerance_ms) { |
251 | 20 | g_capture_with_freshness_tolerance_count << 1; |
252 | 20 | using namespace std::chrono; |
253 | 20 | auto freshness_limit_tp = system_clock::now() - milliseconds(query_freshness_tolerance_ms); |
254 | | // find a version path where every edge(rowset) has been warmuped |
255 | 20 | Versions version_path; |
256 | 20 | std::shared_lock rlock(_meta_lock); |
257 | 20 | if (enable_unique_key_merge_on_write()) { |
258 | | // For merge-on-write table, newly generated delete bitmap marks will be on the rowsets which are in newest layout. |
259 | | // So we can ony capture rowsets which are in newest data layout. Otherwise there may be data correctness issue. |
260 | 11 | RETURN_IF_ERROR(_timestamped_version_tracker.capture_consistent_versions_with_validator_mow( |
261 | 11 | spec_version, version_path, [&](int64_t start, int64_t end) { |
262 | 11 | return rowset_is_warmed_up_unlocked(start, end); |
263 | 11 | })); |
264 | 11 | } else { |
265 | 9 | RETURN_IF_ERROR(_timestamped_version_tracker.capture_consistent_versions_with_validator( |
266 | 9 | spec_version, version_path, [&](int64_t start, int64_t end) { |
267 | 9 | return rowset_is_warmed_up_unlocked(start, end); |
268 | 9 | })); |
269 | 9 | } |
270 | 20 | int64_t path_max_version = version_path.back().second; |
271 | 376 | auto should_be_visible_but_not_warmed_up = [&](const auto& rs_meta) -> bool { |
272 | 376 | if (rs_meta->version() == Version {0, 1}) { |
273 | | // skip rowset[0-1] |
274 | 20 | return false; |
275 | 20 | } |
276 | 356 | bool ret = rs_meta->start_version() > path_max_version && |
277 | 356 | rs_meta->visible_timestamp() < freshness_limit_tp; |
278 | 356 | if (ret && config::read_cluster_cache_opt_verbose_log) { |
279 | 5 | std::time_t t1 = system_clock::to_time_t(rs_meta->visible_timestamp()); |
280 | 5 | std::tm tm1 = *std::localtime(&t1); |
281 | 5 | std::ostringstream oss1; |
282 | 5 | oss1 << std::put_time(&tm1, "%Y-%m-%d %H:%M:%S"); |
283 | | |
284 | 5 | std::time_t t2 = system_clock::to_time_t(freshness_limit_tp); |
285 | 5 | std::tm tm2 = *std::localtime(&t2); |
286 | 5 | std::ostringstream oss2; |
287 | 5 | oss2 << std::put_time(&tm2, "%Y-%m-%d %H:%M:%S"); |
288 | 5 | LOG_INFO( |
289 | 5 | "[verbose] CloudTablet::capture_rs_readers_with_freshness_tolerance, " |
290 | 5 | "find a rowset which should be visible but not warmed up, tablet_id={}, " |
291 | 5 | "path_max_version={}, rowset_id={}, version={}, visible_time={}, " |
292 | 5 | "freshness_limit={}, version_graph={}, rowset_warmup_digest={}", |
293 | 5 | tablet_id(), path_max_version, rs_meta->rowset_id().to_string(), |
294 | 5 | rs_meta->version().to_string(), oss1.str(), oss2.str(), |
295 | 5 | _timestamped_version_tracker.debug_string(), rowset_warmup_digest()); |
296 | 5 | } |
297 | 356 | return ret; |
298 | 376 | }; |
299 | | // use std::views::concat after C++26 |
300 | 20 | bool should_fallback = std::ranges::any_of(_tablet_meta->all_rs_metas(), |
301 | 20 | should_be_visible_but_not_warmed_up) || |
302 | 20 | std::ranges::any_of(_tablet_meta->all_stale_rs_metas(), |
303 | 18 | should_be_visible_but_not_warmed_up); |
304 | 20 | if (should_fallback) { |
305 | 5 | rlock.unlock(); |
306 | 5 | g_capture_with_freshness_tolerance_fallback_count << 1; |
307 | | // if there exists a rowset which satisfies freshness tolerance and its start version is larger than the path max version |
308 | | // but has not been warmuped up yet, fallback to capture rowsets as usual |
309 | 5 | return capture_rs_readers_internal(spec_version, rs_splits); |
310 | 5 | } |
311 | 15 | VLOG_DEBUG << fmt::format( |
312 | 0 | "[verbose] CloudTablet::capture_rs_readers_with_freshness_tolerance, capture path: {}, " |
313 | 0 | "tablet_id={}, spec_version={}, path_max_version={}", |
314 | 0 | fmt::join(version_path | std::views::transform([](const auto& version) { |
315 | 0 | return fmt::format("{}", version.to_string()); |
316 | 0 | }), |
317 | 0 | ", "), |
318 | 0 | tablet_id(), spec_version.to_string(), path_max_version); |
319 | 15 | return capture_rs_readers_unlocked(version_path, rs_splits); |
320 | 20 | } |
321 | | |
322 | | // There are only two tablet_states RUNNING and NOT_READY in cloud mode |
323 | | // This function will erase the tablet from `CloudTabletMgr` when it can't find this tablet in MS. |
324 | 16 | Status CloudTablet::sync_rowsets(const SyncOptions& options, SyncRowsetStats* stats) { |
325 | 16 | RETURN_IF_ERROR(sync_if_not_running(stats)); |
326 | | |
327 | 16 | if (options.query_version > 0) { |
328 | 0 | std::shared_lock rlock(_meta_lock); |
329 | 0 | if (_max_version >= options.query_version) { |
330 | 0 | return Status::OK(); |
331 | 0 | } |
332 | 0 | } |
333 | | |
334 | | // serially execute sync to reduce unnecessary network overhead |
335 | 16 | std::unique_lock lock(_sync_meta_lock); |
336 | 16 | if (options.query_version > 0) { |
337 | 0 | std::shared_lock rlock(_meta_lock); |
338 | 0 | if (_max_version >= options.query_version) { |
339 | 0 | return Status::OK(); |
340 | 0 | } |
341 | 0 | } |
342 | | |
343 | 16 | auto st = _engine.meta_mgr().sync_tablet_rowsets_unlocked(this, lock, options, stats); |
344 | 16 | if (st.is<ErrorCode::NOT_FOUND>()) { |
345 | 0 | clear_cache(); |
346 | 0 | } |
347 | | |
348 | 16 | return st; |
349 | 16 | } |
350 | | |
351 | | // Sync tablet meta and all rowset meta if not running. |
352 | | // This could happen when BE didn't finish schema change job and another BE committed this schema change job. |
353 | | // It should be a quite rare situation. |
354 | 16 | Status CloudTablet::sync_if_not_running(SyncRowsetStats* stats) { |
355 | 16 | if (tablet_state() == TABLET_RUNNING) { |
356 | 16 | return Status::OK(); |
357 | 16 | } |
358 | | |
359 | | // Serially execute sync to reduce unnecessary network overhead |
360 | 0 | std::unique_lock lock(_sync_meta_lock); |
361 | |
|
362 | 0 | { |
363 | 0 | std::shared_lock rlock(_meta_lock); |
364 | 0 | if (tablet_state() == TABLET_RUNNING) { |
365 | 0 | return Status::OK(); |
366 | 0 | } |
367 | 0 | } |
368 | | |
369 | 0 | TabletMetaSharedPtr tablet_meta; |
370 | 0 | auto st = _engine.meta_mgr().get_tablet_meta(tablet_id(), &tablet_meta); |
371 | 0 | if (!st.ok()) { |
372 | 0 | if (st.is<ErrorCode::NOT_FOUND>()) { |
373 | 0 | clear_cache(); |
374 | 0 | } |
375 | 0 | return st; |
376 | 0 | } |
377 | | |
378 | 0 | if (tablet_meta->tablet_state() != TABLET_RUNNING) [[unlikely]] { |
379 | | // MoW may go to here when load while schema change |
380 | 0 | return Status::OK(); |
381 | 0 | } |
382 | | |
383 | 0 | TimestampedVersionTracker empty_tracker; |
384 | 0 | { |
385 | 0 | std::lock_guard wlock(_meta_lock); |
386 | 0 | RETURN_IF_ERROR(set_tablet_state(TABLET_RUNNING)); |
387 | 0 | _rs_version_map.clear(); |
388 | 0 | _stale_rs_version_map.clear(); |
389 | 0 | std::swap(_timestamped_version_tracker, empty_tracker); |
390 | 0 | _tablet_meta->clear_rowsets(); |
391 | 0 | _tablet_meta->clear_stale_rowset(); |
392 | 0 | _max_version = -1; |
393 | 0 | } |
394 | | |
395 | 0 | st = _engine.meta_mgr().sync_tablet_rowsets_unlocked(this, lock, {}, stats); |
396 | 0 | if (st.is<ErrorCode::NOT_FOUND>()) { |
397 | 0 | clear_cache(); |
398 | 0 | } |
399 | 0 | return st; |
400 | 0 | } |
401 | | |
402 | | void CloudTablet::add_rowsets(std::vector<RowsetSharedPtr> to_add, bool version_overlap, |
403 | | std::unique_lock<std::shared_mutex>& meta_lock, |
404 | 237 | bool warmup_delta_data) { |
405 | 237 | if (to_add.empty()) { |
406 | 0 | return; |
407 | 0 | } |
408 | | |
409 | 237 | auto add_rowsets_directly = [=, this](std::vector<RowsetSharedPtr>& rowsets) { |
410 | 722 | for (auto& rs : rowsets) { |
411 | 722 | if (version_overlap || warmup_delta_data) { |
412 | | #ifndef BE_TEST |
413 | | bool warm_up_state_updated = false; |
414 | | // Warmup rowset data in background |
415 | | for (int seg_id = 0; seg_id < rs->num_segments(); ++seg_id) { |
416 | | const auto& rowset_meta = rs->rowset_meta(); |
417 | | constexpr int64_t interval = 600; // 10 mins |
418 | | // When BE restart and receive the `load_sync` rpc, it will sync all historical rowsets first time. |
419 | | // So we need to filter out the old rowsets avoid to download the whole table. |
420 | | if (warmup_delta_data && |
421 | | ::time(nullptr) - rowset_meta->newest_write_timestamp() >= interval) { |
422 | | continue; |
423 | | } |
424 | | |
425 | | auto storage_resource = rowset_meta->remote_storage_resource(); |
426 | | if (!storage_resource) { |
427 | | LOG(WARNING) << storage_resource.error(); |
428 | | continue; |
429 | | } |
430 | | |
431 | | int64_t expiration_time = |
432 | | _tablet_meta->ttl_seconds() == 0 || |
433 | | rowset_meta->newest_write_timestamp() <= 0 |
434 | | ? 0 |
435 | | : rowset_meta->newest_write_timestamp() + |
436 | | _tablet_meta->ttl_seconds(); |
437 | | g_file_cache_cloud_tablet_submitted_segment_num << 1; |
438 | | if (rs->rowset_meta()->segment_file_size(seg_id) > 0) { |
439 | | g_file_cache_cloud_tablet_submitted_segment_size |
440 | | << rs->rowset_meta()->segment_file_size(seg_id); |
441 | | } |
442 | | if (!warm_up_state_updated) { |
443 | | VLOG_DEBUG << "warm up rowset " << rs->version() << "(" << rs->rowset_id() |
444 | | << ") triggerd by sync rowset"; |
445 | | if (!add_rowset_warmup_state_unlocked( |
446 | | *(rs->rowset_meta()), WarmUpState::TRIGGERED_BY_SYNC_ROWSET)) { |
447 | | LOG(INFO) << "found duplicate warmup task for rowset " |
448 | | << rs->rowset_id() << ", skip it"; |
449 | | break; |
450 | | } |
451 | | warm_up_state_updated = true; |
452 | | } |
453 | | // clang-format off |
454 | | auto self = std::dynamic_pointer_cast<CloudTablet>(shared_from_this()); |
455 | | _engine.file_cache_block_downloader().submit_download_task(io::DownloadFileMeta { |
456 | | .path = storage_resource.value()->remote_segment_path(*rowset_meta, seg_id), |
457 | | .file_size = rs->rowset_meta()->segment_file_size(seg_id), |
458 | | .file_system = storage_resource.value()->fs, |
459 | | .ctx = |
460 | | { |
461 | | .expiration_time = expiration_time, |
462 | | .is_dryrun = config::enable_reader_dryrun_when_download_file_cache, |
463 | | .is_warmup = true |
464 | | }, |
465 | | .download_done {[=](Status st) { |
466 | | DBUG_EXECUTE_IF("CloudTablet::add_rowsets.download_data.callback.block_compaction_rowset", { |
467 | | if (rs->version().second > rs->version().first) { |
468 | | auto sleep_time = dp->param<int>("sleep", 3); |
469 | | LOG_INFO( |
470 | | "[verbose] block download for rowset={}, " |
471 | | "version={}, sleep={}", |
472 | | rs->rowset_id().to_string(), |
473 | | rs->version().to_string(), sleep_time); |
474 | | std::this_thread::sleep_for( |
475 | | std::chrono::seconds(sleep_time)); |
476 | | } |
477 | | }); |
478 | | self->complete_rowset_segment_warmup(rowset_meta->rowset_id(), st, 1, 0); |
479 | | if (!st) { |
480 | | LOG_WARNING("add rowset warm up error ").error(st); |
481 | | } |
482 | | }}, |
483 | | }); |
484 | | |
485 | | auto download_idx_file = [&, self](const io::Path& idx_path, int64_t idx_size) { |
486 | | io::DownloadFileMeta meta { |
487 | | .path = idx_path, |
488 | | .file_size = idx_size, |
489 | | .file_system = storage_resource.value()->fs, |
490 | | .ctx = |
491 | | { |
492 | | .expiration_time = expiration_time, |
493 | | .is_dryrun = config::enable_reader_dryrun_when_download_file_cache, |
494 | | .is_warmup = true |
495 | | }, |
496 | | .download_done {[=](Status st) { |
497 | | DBUG_EXECUTE_IF("CloudTablet::add_rowsets.download_idx.callback.block", { |
498 | | // clang-format on |
499 | | auto sleep_time = dp->param<int>("sleep", 3); |
500 | | LOG_INFO( |
501 | | "[verbose] block download for " |
502 | | "rowset={}, inverted_idx_file={}, " |
503 | | "sleep={}", |
504 | | rs->rowset_id().to_string(), |
505 | | idx_path.string(), sleep_time); |
506 | | std::this_thread::sleep_for( |
507 | | std::chrono::seconds(sleep_time)); |
508 | | // clang-format off |
509 | | }); |
510 | | self->complete_rowset_segment_warmup(rowset_meta->rowset_id(), st, 0, 1); |
511 | | if (!st) { |
512 | | LOG_WARNING("add rowset warm up error ").error(st); |
513 | | } |
514 | | }}, |
515 | | }; |
516 | | self->update_rowset_warmup_state_inverted_idx_num_unlocked(rowset_meta->rowset_id(), 1); |
517 | | _engine.file_cache_block_downloader().submit_download_task(std::move(meta)); |
518 | | g_file_cache_cloud_tablet_submitted_index_num << 1; |
519 | | g_file_cache_cloud_tablet_submitted_index_size << idx_size; |
520 | | }; |
521 | | // clang-format on |
522 | | auto schema_ptr = rowset_meta->tablet_schema(); |
523 | | auto idx_version = schema_ptr->get_inverted_index_storage_format(); |
524 | | if (idx_version == InvertedIndexStorageFormatPB::V1) { |
525 | | std::unordered_map<int64_t, int64_t> index_size_map; |
526 | | auto&& inverted_index_info = rowset_meta->inverted_index_file_info(seg_id); |
527 | | for (const auto& info : inverted_index_info.index_info()) { |
528 | | if (info.index_file_size() != -1) { |
529 | | index_size_map[info.index_id()] = info.index_file_size(); |
530 | | } else { |
531 | | VLOG_DEBUG << "Invalid index_file_size for segment_id " << seg_id |
532 | | << ", index_id " << info.index_id(); |
533 | | } |
534 | | } |
535 | | for (const auto& index : schema_ptr->inverted_indexes()) { |
536 | | auto idx_path = storage_resource.value()->remote_idx_v1_path( |
537 | | *rowset_meta, seg_id, index->index_id(), |
538 | | index->get_index_suffix()); |
539 | | download_idx_file(idx_path, index_size_map[index->index_id()]); |
540 | | } |
541 | | } else { |
542 | | if (schema_ptr->has_inverted_index() || schema_ptr->has_ann_index()) { |
543 | | auto&& inverted_index_info = |
544 | | rowset_meta->inverted_index_file_info(seg_id); |
545 | | int64_t idx_size = 0; |
546 | | if (inverted_index_info.has_index_size()) { |
547 | | idx_size = inverted_index_info.index_size(); |
548 | | } else { |
549 | | VLOG_DEBUG << "index_size is not set for segment " << seg_id; |
550 | | } |
551 | | auto idx_path = storage_resource.value()->remote_idx_v2_path( |
552 | | *rowset_meta, seg_id); |
553 | | download_idx_file(idx_path, idx_size); |
554 | | } |
555 | | } |
556 | | } |
557 | | #endif |
558 | 0 | } |
559 | 722 | _rs_version_map.emplace(rs->version(), rs); |
560 | 722 | _timestamped_version_tracker.add_version(rs->version()); |
561 | 722 | _max_version = std::max(rs->end_version(), _max_version); |
562 | 722 | update_base_size(*rs); |
563 | 722 | } |
564 | 237 | _tablet_meta->add_rowsets_unchecked(rowsets); |
565 | 237 | }; |
566 | | |
567 | 237 | if (!version_overlap) { |
568 | 237 | add_rowsets_directly(to_add); |
569 | 237 | return; |
570 | 237 | } |
571 | | |
572 | | // Filter out existed rowsets |
573 | 0 | auto remove_it = |
574 | 0 | std::remove_if(to_add.begin(), to_add.end(), [this](const RowsetSharedPtr& rs) { |
575 | 0 | if (auto find_it = _rs_version_map.find(rs->version()); |
576 | 0 | find_it == _rs_version_map.end()) { |
577 | 0 | return false; |
578 | 0 | } else if (find_it->second->rowset_id() == rs->rowset_id()) { |
579 | 0 | return true; // Same rowset |
580 | 0 | } |
581 | | |
582 | | // If version of rowset in `to_add` is equal to rowset in tablet but rowset_id is not equal, |
583 | | // replace existed rowset with `to_add` rowset. This may occur when: |
584 | | // 1. schema change converts rowsets which have been double written to new tablet |
585 | | // 2. cumu compaction picks single overlapping input rowset to perform compaction |
586 | | |
587 | | // add existed rowset to unused_rowsets to remove delete bitmap and recycle cached data |
588 | | |
589 | 0 | std::vector<RowsetSharedPtr> unused_rowsets; |
590 | 0 | if (auto find_it = _rs_version_map.find(rs->version()); |
591 | 0 | find_it != _rs_version_map.end()) { |
592 | 0 | if (find_it->second->rowset_id() == rs->rowset_id()) { |
593 | 0 | LOG(WARNING) << "tablet_id=" << tablet_id() |
594 | 0 | << ", rowset_id=" << rs->rowset_id().to_string() |
595 | 0 | << ", existed rowset_id=" |
596 | 0 | << find_it->second->rowset_id().to_string(); |
597 | 0 | DCHECK(find_it->second->rowset_id() != rs->rowset_id()) |
598 | 0 | << "tablet_id=" << tablet_id() |
599 | 0 | << ", rowset_id=" << rs->rowset_id().to_string() |
600 | 0 | << ", existed rowset_id=" |
601 | 0 | << find_it->second->rowset_id().to_string(); |
602 | 0 | } |
603 | 0 | unused_rowsets.push_back(find_it->second); |
604 | 0 | } |
605 | 0 | add_unused_rowsets(unused_rowsets); |
606 | |
|
607 | 0 | _tablet_meta->delete_rs_meta_by_version(rs->version(), nullptr); |
608 | 0 | _rs_version_map[rs->version()] = rs; |
609 | 0 | _tablet_meta->add_rowsets_unchecked({rs}); |
610 | 0 | update_base_size(*rs); |
611 | 0 | return true; |
612 | 0 | }); |
613 | |
|
614 | 0 | to_add.erase(remove_it, to_add.end()); |
615 | | |
616 | | // delete rowsets with overlapped version |
617 | 0 | std::vector<RowsetSharedPtr> to_add_directly; |
618 | 0 | for (auto& to_add_rs : to_add) { |
619 | | // delete rowsets with overlapped version |
620 | 0 | std::vector<RowsetSharedPtr> to_delete; |
621 | 0 | Version to_add_v = to_add_rs->version(); |
622 | | // if start_version > max_version, we can skip checking overlap here. |
623 | 0 | if (to_add_v.first > _max_version) { |
624 | | // if start_version > max_version, we can skip checking overlap here. |
625 | 0 | to_add_directly.push_back(to_add_rs); |
626 | 0 | } else { |
627 | 0 | to_add_directly.push_back(to_add_rs); |
628 | 0 | for (auto& [v, rs] : _rs_version_map) { |
629 | 0 | if (to_add_v.contains(v)) { |
630 | 0 | to_delete.push_back(rs); |
631 | 0 | } |
632 | 0 | } |
633 | 0 | delete_rowsets(to_delete, meta_lock); |
634 | 0 | } |
635 | 0 | } |
636 | |
|
637 | 0 | add_rowsets_directly(to_add_directly); |
638 | 0 | } |
639 | | |
640 | | void CloudTablet::delete_rowsets(const std::vector<RowsetSharedPtr>& to_delete, |
641 | 94 | std::unique_lock<std::shared_mutex>&) { |
642 | 94 | if (to_delete.empty()) { |
643 | 0 | return; |
644 | 0 | } |
645 | 94 | std::vector<RowsetMetaSharedPtr> rs_metas; |
646 | 94 | rs_metas.reserve(to_delete.size()); |
647 | 94 | int64_t now = ::time(nullptr); |
648 | 540 | for (auto&& rs : to_delete) { |
649 | 540 | rs->rowset_meta()->set_stale_at(now); |
650 | 540 | rs_metas.push_back(rs->rowset_meta()); |
651 | 540 | _stale_rs_version_map[rs->version()] = rs; |
652 | 540 | } |
653 | 94 | _timestamped_version_tracker.add_stale_path_version(rs_metas); |
654 | 540 | for (auto&& rs : to_delete) { |
655 | 540 | _rs_version_map.erase(rs->version()); |
656 | 540 | } |
657 | | |
658 | 94 | _tablet_meta->modify_rs_metas({}, rs_metas, false); |
659 | 94 | } |
660 | | |
661 | 0 | uint64_t CloudTablet::delete_expired_stale_rowsets() { |
662 | 0 | if (config::enable_mow_verbose_log) { |
663 | 0 | LOG_INFO("begin delete_expired_stale_rowset for tablet={}", tablet_id()); |
664 | 0 | } |
665 | 0 | std::vector<RowsetSharedPtr> expired_rowsets; |
666 | | // 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. |
667 | 0 | std::vector<std::pair<Version, std::vector<RowsetSharedPtr>>> deleted_stale_rowsets; |
668 | 0 | int64_t expired_stale_sweep_endtime = |
669 | 0 | ::time(nullptr) - config::tablet_rowset_stale_sweep_time_sec; |
670 | 0 | { |
671 | 0 | std::unique_lock wlock(_meta_lock); |
672 | |
|
673 | 0 | std::vector<int64_t> path_ids; |
674 | | // capture the path version to delete |
675 | 0 | _timestamped_version_tracker.capture_expired_paths(expired_stale_sweep_endtime, &path_ids); |
676 | |
|
677 | 0 | if (path_ids.empty()) { |
678 | 0 | return 0; |
679 | 0 | } |
680 | | |
681 | 0 | for (int64_t path_id : path_ids) { |
682 | 0 | int64_t start_version = -1; |
683 | 0 | int64_t end_version = -1; |
684 | 0 | std::vector<RowsetSharedPtr> stale_rowsets; |
685 | | // delete stale versions in version graph |
686 | 0 | auto version_path = _timestamped_version_tracker.fetch_and_delete_path_by_id(path_id); |
687 | 0 | for (auto& v_ts : version_path->timestamped_versions()) { |
688 | 0 | auto rs_it = _stale_rs_version_map.find(v_ts->version()); |
689 | 0 | if (rs_it != _stale_rs_version_map.end()) { |
690 | 0 | expired_rowsets.push_back(rs_it->second); |
691 | 0 | stale_rowsets.push_back(rs_it->second); |
692 | 0 | VLOG_DEBUG << "erase stale rowset, tablet_id=" << tablet_id() |
693 | 0 | << " rowset_id=" << rs_it->second->rowset_id().to_string() |
694 | 0 | << " version=" << rs_it->first.to_string(); |
695 | 0 | _stale_rs_version_map.erase(rs_it); |
696 | 0 | } else { |
697 | 0 | LOG(WARNING) << "cannot find stale rowset " << v_ts->version() << " in tablet " |
698 | 0 | << tablet_id(); |
699 | | // clang-format off |
700 | 0 | DCHECK(false) << [this, &wlock]() { wlock.unlock(); std::string json; get_compaction_status(&json); return json; }(); |
701 | | // clang-format on |
702 | 0 | } |
703 | 0 | if (start_version < 0) { |
704 | 0 | start_version = v_ts->version().first; |
705 | 0 | } |
706 | 0 | end_version = v_ts->version().second; |
707 | 0 | _tablet_meta->delete_stale_rs_meta_by_version(v_ts->version()); |
708 | 0 | } |
709 | 0 | Version version(start_version, end_version); |
710 | 0 | if (!stale_rowsets.empty()) { |
711 | 0 | deleted_stale_rowsets.emplace_back(version, std::move(stale_rowsets)); |
712 | 0 | } |
713 | 0 | } |
714 | 0 | _reconstruct_version_tracker_if_necessary(); |
715 | 0 | } |
716 | | |
717 | | // if the rowset is not used by any query, we can recycle its cached data early. |
718 | 0 | auto recycled_rowsets = recycle_cached_data(expired_rowsets); |
719 | 0 | if (!recycled_rowsets.empty()) { |
720 | 0 | auto& manager = ExecEnv::GetInstance()->storage_engine().to_cloud().cloud_warm_up_manager(); |
721 | 0 | manager.recycle_cache(tablet_id(), recycled_rowsets); |
722 | 0 | } |
723 | 0 | if (config::enable_mow_verbose_log) { |
724 | 0 | LOG_INFO("finish delete_expired_stale_rowset for tablet={}", tablet_id()); |
725 | 0 | } |
726 | |
|
727 | 0 | add_unused_rowsets(expired_rowsets); |
728 | 0 | if (config::enable_agg_and_remove_pre_rowsets_delete_bitmap && keys_type() == UNIQUE_KEYS && |
729 | 0 | enable_unique_key_merge_on_write() && !deleted_stale_rowsets.empty()) { |
730 | | // agg delete bitmap for pre rowsets; record unused delete bitmap key ranges |
731 | 0 | OlapStopWatch watch; |
732 | 0 | for (const auto& [version, unused_rowsets] : deleted_stale_rowsets) { |
733 | | // agg delete bitmap for pre rowset |
734 | 0 | DeleteBitmapKeyRanges remove_delete_bitmap_key_ranges; |
735 | 0 | agg_delete_bitmap_for_stale_rowsets(version, remove_delete_bitmap_key_ranges); |
736 | | // add remove delete bitmap |
737 | 0 | if (!remove_delete_bitmap_key_ranges.empty()) { |
738 | 0 | std::vector<RowsetId> rowset_ids; |
739 | 0 | for (const auto& rs : unused_rowsets) { |
740 | 0 | rowset_ids.push_back(rs->rowset_id()); |
741 | 0 | } |
742 | 0 | std::lock_guard<std::mutex> lock(_gc_mutex); |
743 | 0 | _unused_delete_bitmap.push_back( |
744 | 0 | std::make_pair(rowset_ids, remove_delete_bitmap_key_ranges)); |
745 | 0 | } |
746 | 0 | } |
747 | 0 | LOG(INFO) << "agg pre rowsets delete bitmap. tablet_id=" << tablet_id() |
748 | 0 | << ", size=" << deleted_stale_rowsets.size() |
749 | 0 | << ", cost(us)=" << watch.get_elapse_time_us(); |
750 | 0 | } |
751 | 0 | return expired_rowsets.size(); |
752 | 0 | } |
753 | | |
754 | 0 | bool CloudTablet::need_remove_unused_rowsets() { |
755 | 0 | std::lock_guard<std::mutex> lock(_gc_mutex); |
756 | 0 | return !_unused_rowsets.empty() || !_unused_delete_bitmap.empty(); |
757 | 0 | } |
758 | | |
759 | 0 | void CloudTablet::add_unused_rowsets(const std::vector<RowsetSharedPtr>& rowsets) { |
760 | 0 | std::lock_guard<std::mutex> lock(_gc_mutex); |
761 | 0 | for (const auto& rowset : rowsets) { |
762 | 0 | _unused_rowsets[rowset->rowset_id()] = rowset; |
763 | 0 | g_unused_rowsets_bytes << rowset->total_disk_size(); |
764 | 0 | } |
765 | 0 | g_unused_rowsets_count << rowsets.size(); |
766 | 0 | } |
767 | | |
768 | 0 | void CloudTablet::remove_unused_rowsets() { |
769 | 0 | std::vector<std::shared_ptr<Rowset>> removed_rowsets; |
770 | 0 | int64_t removed_delete_bitmap_num = 0; |
771 | 0 | OlapStopWatch watch; |
772 | 0 | { |
773 | 0 | std::lock_guard<std::mutex> lock(_gc_mutex); |
774 | | // 1. remove unused rowsets's cache data and delete bitmap |
775 | 0 | for (auto it = _unused_rowsets.begin(); it != _unused_rowsets.end();) { |
776 | 0 | auto& rs = it->second; |
777 | 0 | if (rs.use_count() > 1) { |
778 | 0 | LOG(WARNING) << "tablet_id:" << tablet_id() << " rowset: " << rs->rowset_id() |
779 | 0 | << " has " << rs.use_count() << " references, it cannot be removed"; |
780 | 0 | ++it; |
781 | 0 | continue; |
782 | 0 | } |
783 | 0 | tablet_meta()->remove_rowset_delete_bitmap(rs->rowset_id(), rs->version()); |
784 | 0 | _rowset_warm_up_states.erase(rs->rowset_id()); |
785 | 0 | rs->clear_cache(); |
786 | 0 | g_unused_rowsets_count << -1; |
787 | 0 | g_unused_rowsets_bytes << -rs->total_disk_size(); |
788 | 0 | removed_rowsets.push_back(std::move(rs)); |
789 | 0 | it = _unused_rowsets.erase(it); |
790 | 0 | } |
791 | 0 | } |
792 | |
|
793 | 0 | { |
794 | 0 | std::vector<RecycledRowsets> recycled_rowsets; |
795 | |
|
796 | 0 | for (auto& rs : removed_rowsets) { |
797 | 0 | auto index_names = rs->get_index_file_names(); |
798 | 0 | recycled_rowsets.emplace_back(rs->rowset_id(), rs->num_segments(), index_names); |
799 | 0 | int64_t segment_size_sum = 0; |
800 | 0 | for (int32_t i = 0; i < rs->num_segments(); i++) { |
801 | 0 | segment_size_sum += rs->rowset_meta()->segment_file_size(i); |
802 | 0 | } |
803 | 0 | g_file_cache_recycle_cached_data_segment_num << rs->num_segments(); |
804 | 0 | g_file_cache_recycle_cached_data_segment_size << segment_size_sum; |
805 | 0 | g_file_cache_recycle_cached_data_index_num << index_names.size(); |
806 | 0 | } |
807 | |
|
808 | 0 | if (recycled_rowsets.size() > 0) { |
809 | 0 | auto& manager = |
810 | 0 | ExecEnv::GetInstance()->storage_engine().to_cloud().cloud_warm_up_manager(); |
811 | 0 | manager.recycle_cache(tablet_id(), recycled_rowsets); |
812 | 0 | } |
813 | 0 | } |
814 | |
|
815 | 0 | { |
816 | 0 | std::lock_guard<std::mutex> lock(_gc_mutex); |
817 | | // 2. remove delete bitmap of pre rowsets |
818 | 0 | for (auto it = _unused_delete_bitmap.begin(); it != _unused_delete_bitmap.end();) { |
819 | 0 | auto& rowset_ids = std::get<0>(*it); |
820 | 0 | bool find_unused_rowset = false; |
821 | 0 | for (const auto& rowset_id : rowset_ids) { |
822 | 0 | if (_unused_rowsets.find(rowset_id) != _unused_rowsets.end()) { |
823 | 0 | LOG(INFO) << "can not remove pre rowset delete bitmap because rowset is in use" |
824 | 0 | << ", tablet_id=" << tablet_id() << ", rowset_id=" << rowset_id; |
825 | 0 | find_unused_rowset = true; |
826 | 0 | break; |
827 | 0 | } |
828 | 0 | } |
829 | 0 | if (find_unused_rowset) { |
830 | 0 | ++it; |
831 | 0 | continue; |
832 | 0 | } |
833 | 0 | auto& key_ranges = std::get<1>(*it); |
834 | 0 | tablet_meta()->delete_bitmap().remove(key_ranges); |
835 | 0 | it = _unused_delete_bitmap.erase(it); |
836 | 0 | removed_delete_bitmap_num++; |
837 | | // TODO(kaijie): recycle cache for unused delete bitmap |
838 | 0 | } |
839 | 0 | } |
840 | |
|
841 | 0 | LOG(INFO) << "tablet_id=" << tablet_id() << ", unused_rowset size=" << _unused_rowsets.size() |
842 | 0 | << ", unused_delete_bitmap size=" << _unused_delete_bitmap.size() |
843 | 0 | << ", removed_rowsets_num=" << removed_rowsets.size() |
844 | 0 | << ", removed_delete_bitmap_num=" << removed_delete_bitmap_num |
845 | 0 | << ", cost(us)=" << watch.get_elapse_time_us(); |
846 | 0 | } |
847 | | |
848 | 722 | void CloudTablet::update_base_size(const Rowset& rs) { |
849 | | // Define base rowset as the rowset of version [2-x] |
850 | 722 | if (rs.start_version() == 2) { |
851 | 88 | _base_size = rs.total_disk_size(); |
852 | 88 | } |
853 | 722 | } |
854 | | |
855 | 0 | void CloudTablet::clear_cache() { |
856 | 0 | auto recycled_rowsets = CloudTablet::recycle_cached_data(get_snapshot_rowset(true)); |
857 | 0 | if (!recycled_rowsets.empty()) { |
858 | 0 | auto& manager = ExecEnv::GetInstance()->storage_engine().to_cloud().cloud_warm_up_manager(); |
859 | 0 | manager.recycle_cache(tablet_id(), recycled_rowsets); |
860 | 0 | } |
861 | 0 | _engine.tablet_mgr().erase_tablet(tablet_id()); |
862 | 0 | } |
863 | | |
864 | | std::vector<RecycledRowsets> CloudTablet::recycle_cached_data( |
865 | 0 | const std::vector<RowsetSharedPtr>& rowsets) { |
866 | 0 | std::vector<RecycledRowsets> recycled_rowsets; |
867 | 0 | for (const auto& rs : rowsets) { |
868 | | // 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. |
869 | 0 | if (rs.use_count() > 2) { |
870 | 0 | LOG(WARNING) << "Rowset " << rs->rowset_id().to_string() << " has " << rs.use_count() |
871 | 0 | << " references. File Cache won't be recycled when query is using it."; |
872 | 0 | continue; |
873 | 0 | } |
874 | 0 | rs->clear_cache(); |
875 | 0 | auto index_names = rs->get_index_file_names(); |
876 | 0 | recycled_rowsets.emplace_back(rs->rowset_id(), rs->num_segments(), index_names); |
877 | |
|
878 | 0 | int64_t segment_size_sum = 0; |
879 | 0 | for (int32_t i = 0; i < rs->num_segments(); i++) { |
880 | 0 | segment_size_sum += rs->rowset_meta()->segment_file_size(i); |
881 | 0 | } |
882 | 0 | g_file_cache_recycle_cached_data_segment_num << rs->num_segments(); |
883 | 0 | g_file_cache_recycle_cached_data_segment_size << segment_size_sum; |
884 | 0 | g_file_cache_recycle_cached_data_index_num << index_names.size(); |
885 | 0 | } |
886 | 0 | return recycled_rowsets; |
887 | 0 | } |
888 | | |
889 | | void CloudTablet::reset_approximate_stats(int64_t num_rowsets, int64_t num_segments, |
890 | 0 | int64_t num_rows, int64_t data_size) { |
891 | 0 | _approximate_num_segments.store(num_segments, std::memory_order_relaxed); |
892 | 0 | _approximate_num_rows.store(num_rows, std::memory_order_relaxed); |
893 | 0 | _approximate_data_size.store(data_size, std::memory_order_relaxed); |
894 | 0 | int64_t cumu_num_deltas = 0; |
895 | 0 | int64_t cumu_num_rowsets = 0; |
896 | 0 | auto cp = _cumulative_point.load(std::memory_order_relaxed); |
897 | 0 | for (auto& [v, r] : _rs_version_map) { |
898 | 0 | if (v.second < cp) { |
899 | 0 | continue; |
900 | 0 | } |
901 | 0 | cumu_num_deltas += r->is_segments_overlapping() ? r->num_segments() : 1; |
902 | 0 | ++cumu_num_rowsets; |
903 | 0 | } |
904 | | // num_rowsets may be less than the size of _rs_version_map when there are some hole rowsets |
905 | | // in the version map, so we use the max value to ensure that the approximate number |
906 | | // of rowsets is at least the size of _rs_version_map. |
907 | | // Note that this is not the exact number of rowsets, but an approximate number. |
908 | 0 | int64_t approximate_num_rowsets = |
909 | 0 | std::max(num_rowsets, static_cast<int64_t>(_rs_version_map.size())); |
910 | 0 | _approximate_num_rowsets.store(approximate_num_rowsets, std::memory_order_relaxed); |
911 | 0 | _approximate_cumu_num_rowsets.store(cumu_num_rowsets, std::memory_order_relaxed); |
912 | 0 | _approximate_cumu_num_deltas.store(cumu_num_deltas, std::memory_order_relaxed); |
913 | 0 | } |
914 | | |
915 | | Result<std::unique_ptr<RowsetWriter>> CloudTablet::create_rowset_writer( |
916 | 0 | RowsetWriterContext& context, bool vertical) { |
917 | 0 | context.rowset_id = _engine.next_rowset_id(); |
918 | | // FIXME(plat1ko): Seems `tablet_id` and `index_id` has been set repeatedly |
919 | 0 | context.tablet_id = tablet_id(); |
920 | 0 | context.index_id = index_id(); |
921 | 0 | context.partition_id = partition_id(); |
922 | 0 | context.enable_unique_key_merge_on_write = enable_unique_key_merge_on_write(); |
923 | 0 | context.encrypt_algorithm = tablet_meta()->encryption_algorithm(); |
924 | 0 | return RowsetFactory::create_rowset_writer(_engine, context, vertical); |
925 | 0 | } |
926 | | |
927 | | // create a rowset writer with rowset_id and seg_id |
928 | | // after writer, merge this transient rowset with original rowset |
929 | | Result<std::unique_ptr<RowsetWriter>> CloudTablet::create_transient_rowset_writer( |
930 | | const Rowset& rowset, std::shared_ptr<PartialUpdateInfo> partial_update_info, |
931 | 0 | int64_t txn_expiration) { |
932 | 0 | if (rowset.rowset_meta_state() != RowsetStatePB::BEGIN_PARTIAL_UPDATE && |
933 | 0 | rowset.rowset_meta_state() != RowsetStatePB::COMMITTED) [[unlikely]] { |
934 | 0 | auto msg = fmt::format( |
935 | 0 | "wrong rowset state when create_transient_rowset_writer, rowset state should be " |
936 | 0 | "BEGIN_PARTIAL_UPDATE or COMMITTED, but found {}, rowset_id={}, tablet_id={}", |
937 | 0 | RowsetStatePB_Name(rowset.rowset_meta_state()), rowset.rowset_id().to_string(), |
938 | 0 | tablet_id()); |
939 | | // see `CloudRowsetWriter::build` for detail. |
940 | | // if this is in a retry task, the rowset state may have been changed to RowsetStatePB::COMMITTED |
941 | | // in `RowsetMeta::merge_rowset_meta()` in previous trials. |
942 | 0 | LOG(WARNING) << msg; |
943 | 0 | DCHECK(false) << msg; |
944 | 0 | } |
945 | 0 | RowsetWriterContext context; |
946 | 0 | context.rowset_state = PREPARED; |
947 | 0 | context.segments_overlap = OVERLAPPING; |
948 | | // During a partial update, the extracted columns of a variant should not be included in the tablet schema. |
949 | | // This is because the partial update for a variant needs to ignore the extracted columns. |
950 | | // Otherwise, the schema types in different rowsets might be inconsistent. When performing a partial update, |
951 | | // the complete variant is constructed by reading all the sub-columns of the variant. |
952 | 0 | context.tablet_schema = rowset.tablet_schema()->copy_without_variant_extracted_columns(); |
953 | 0 | context.newest_write_timestamp = UnixSeconds(); |
954 | 0 | context.tablet_id = table_id(); |
955 | 0 | context.enable_segcompaction = false; |
956 | 0 | context.write_type = DataWriteType::TYPE_DIRECT; |
957 | 0 | context.partial_update_info = std::move(partial_update_info); |
958 | 0 | context.is_transient_rowset_writer = true; |
959 | 0 | context.rowset_id = rowset.rowset_id(); |
960 | 0 | context.tablet_id = tablet_id(); |
961 | 0 | context.index_id = index_id(); |
962 | 0 | context.partition_id = partition_id(); |
963 | 0 | context.enable_unique_key_merge_on_write = enable_unique_key_merge_on_write(); |
964 | 0 | context.txn_expiration = txn_expiration; |
965 | 0 | context.encrypt_algorithm = tablet_meta()->encryption_algorithm(); |
966 | |
|
967 | 0 | auto storage_resource = rowset.rowset_meta()->remote_storage_resource(); |
968 | 0 | if (!storage_resource) { |
969 | 0 | return ResultError(std::move(storage_resource.error())); |
970 | 0 | } |
971 | | |
972 | 0 | context.storage_resource = *storage_resource.value(); |
973 | |
|
974 | 0 | return RowsetFactory::create_rowset_writer(_engine, context, false) |
975 | 0 | .transform([&](auto&& writer) { |
976 | 0 | writer->set_segment_start_id(cast_set<int32_t>(rowset.num_segments())); |
977 | 0 | return writer; |
978 | 0 | }); |
979 | 0 | } |
980 | | |
981 | 3 | int64_t CloudTablet::get_cloud_base_compaction_score() const { |
982 | 3 | if (_tablet_meta->compaction_policy() == CUMULATIVE_TIME_SERIES_POLICY) { |
983 | 0 | bool has_delete = false; |
984 | 0 | int64_t point = cumulative_layer_point(); |
985 | 0 | std::shared_lock<std::shared_mutex> rlock(_meta_lock); |
986 | 0 | for (const auto& rs_meta : _tablet_meta->all_rs_metas()) { |
987 | 0 | if (rs_meta->start_version() >= point) { |
988 | 0 | continue; |
989 | 0 | } |
990 | 0 | if (rs_meta->has_delete_predicate()) { |
991 | 0 | has_delete = true; |
992 | 0 | break; |
993 | 0 | } |
994 | 0 | } |
995 | 0 | if (!has_delete) { |
996 | 0 | return 0; |
997 | 0 | } |
998 | 0 | } |
999 | | |
1000 | 3 | return _approximate_num_rowsets.load(std::memory_order_relaxed) - |
1001 | 3 | _approximate_cumu_num_rowsets.load(std::memory_order_relaxed); |
1002 | 3 | } |
1003 | | |
1004 | 1 | int64_t CloudTablet::get_cloud_cumu_compaction_score() const { |
1005 | | // TODO(plat1ko): Propose an algorithm that considers tablet's key type, number of delete rowsets, |
1006 | | // number of tablet versions simultaneously. |
1007 | 1 | return _approximate_cumu_num_deltas.load(std::memory_order_relaxed); |
1008 | 1 | } |
1009 | | |
1010 | | // return a json string to show the compaction status of this tablet |
1011 | 33 | void CloudTablet::get_compaction_status(std::string* json_result) { |
1012 | 33 | rapidjson::Document root; |
1013 | 33 | root.SetObject(); |
1014 | | |
1015 | 33 | rapidjson::Document path_arr; |
1016 | 33 | path_arr.SetArray(); |
1017 | | |
1018 | 33 | std::vector<RowsetSharedPtr> rowsets; |
1019 | 33 | std::vector<RowsetSharedPtr> stale_rowsets; |
1020 | 33 | { |
1021 | 33 | std::shared_lock rdlock(_meta_lock); |
1022 | 33 | rowsets.reserve(_rs_version_map.size()); |
1023 | 148 | for (auto& it : _rs_version_map) { |
1024 | 148 | rowsets.push_back(it.second); |
1025 | 148 | } |
1026 | 33 | stale_rowsets.reserve(_stale_rs_version_map.size()); |
1027 | 540 | for (auto& it : _stale_rs_version_map) { |
1028 | 540 | stale_rowsets.push_back(it.second); |
1029 | 540 | } |
1030 | 33 | } |
1031 | 33 | std::sort(rowsets.begin(), rowsets.end(), Rowset::comparator); |
1032 | 33 | std::sort(stale_rowsets.begin(), stale_rowsets.end(), Rowset::comparator); |
1033 | | |
1034 | | // get snapshot version path json_doc |
1035 | 33 | _timestamped_version_tracker.get_stale_version_path_json_doc(path_arr); |
1036 | 33 | root.AddMember("cumulative point", _cumulative_point.load(), root.GetAllocator()); |
1037 | 33 | rapidjson::Value cumu_value; |
1038 | 33 | std::string format_str = ToStringFromUnixMillis(_last_cumu_compaction_failure_millis.load()); |
1039 | 33 | cumu_value.SetString(format_str.c_str(), cast_set<uint>(format_str.length()), |
1040 | 33 | root.GetAllocator()); |
1041 | 33 | root.AddMember("last cumulative failure time", cumu_value, root.GetAllocator()); |
1042 | 33 | rapidjson::Value base_value; |
1043 | 33 | format_str = ToStringFromUnixMillis(_last_base_compaction_failure_millis.load()); |
1044 | 33 | base_value.SetString(format_str.c_str(), cast_set<uint>(format_str.length()), |
1045 | 33 | root.GetAllocator()); |
1046 | 33 | root.AddMember("last base failure time", base_value, root.GetAllocator()); |
1047 | 33 | rapidjson::Value full_value; |
1048 | 33 | format_str = ToStringFromUnixMillis(_last_full_compaction_failure_millis.load()); |
1049 | 33 | full_value.SetString(format_str.c_str(), cast_set<uint>(format_str.length()), |
1050 | 33 | root.GetAllocator()); |
1051 | 33 | root.AddMember("last full failure time", full_value, root.GetAllocator()); |
1052 | 33 | rapidjson::Value cumu_success_value; |
1053 | 33 | format_str = ToStringFromUnixMillis(_last_cumu_compaction_success_millis.load()); |
1054 | 33 | cumu_success_value.SetString(format_str.c_str(), cast_set<uint>(format_str.length()), |
1055 | 33 | root.GetAllocator()); |
1056 | 33 | root.AddMember("last cumulative success time", cumu_success_value, root.GetAllocator()); |
1057 | 33 | rapidjson::Value base_success_value; |
1058 | 33 | format_str = ToStringFromUnixMillis(_last_base_compaction_success_millis.load()); |
1059 | 33 | base_success_value.SetString(format_str.c_str(), cast_set<uint>(format_str.length()), |
1060 | 33 | root.GetAllocator()); |
1061 | 33 | root.AddMember("last base success time", base_success_value, root.GetAllocator()); |
1062 | 33 | rapidjson::Value full_success_value; |
1063 | 33 | format_str = ToStringFromUnixMillis(_last_full_compaction_success_millis.load()); |
1064 | 33 | full_success_value.SetString(format_str.c_str(), cast_set<uint>(format_str.length()), |
1065 | 33 | root.GetAllocator()); |
1066 | 33 | root.AddMember("last full success time", full_success_value, root.GetAllocator()); |
1067 | 33 | rapidjson::Value cumu_schedule_value; |
1068 | 33 | format_str = ToStringFromUnixMillis(_last_cumu_compaction_schedule_millis.load()); |
1069 | 33 | cumu_schedule_value.SetString(format_str.c_str(), cast_set<uint>(format_str.length()), |
1070 | 33 | root.GetAllocator()); |
1071 | 33 | root.AddMember("last cumulative schedule time", cumu_schedule_value, root.GetAllocator()); |
1072 | 33 | rapidjson::Value base_schedule_value; |
1073 | 33 | format_str = ToStringFromUnixMillis(_last_base_compaction_schedule_millis.load()); |
1074 | 33 | base_schedule_value.SetString(format_str.c_str(), cast_set<uint>(format_str.length()), |
1075 | 33 | root.GetAllocator()); |
1076 | 33 | root.AddMember("last base schedule time", base_schedule_value, root.GetAllocator()); |
1077 | 33 | rapidjson::Value full_schedule_value; |
1078 | 33 | format_str = ToStringFromUnixMillis(_last_full_compaction_schedule_millis.load()); |
1079 | 33 | full_schedule_value.SetString(format_str.c_str(), cast_set<uint>(format_str.length()), |
1080 | 33 | root.GetAllocator()); |
1081 | 33 | root.AddMember("last full schedule time", full_schedule_value, root.GetAllocator()); |
1082 | 33 | rapidjson::Value cumu_compaction_status_value; |
1083 | 33 | cumu_compaction_status_value.SetString(_last_cumu_compaction_status.c_str(), |
1084 | 33 | cast_set<uint>(_last_cumu_compaction_status.length()), |
1085 | 33 | root.GetAllocator()); |
1086 | 33 | root.AddMember("last cumulative status", cumu_compaction_status_value, root.GetAllocator()); |
1087 | 33 | rapidjson::Value base_compaction_status_value; |
1088 | 33 | base_compaction_status_value.SetString(_last_base_compaction_status.c_str(), |
1089 | 33 | cast_set<uint>(_last_base_compaction_status.length()), |
1090 | 33 | root.GetAllocator()); |
1091 | 33 | root.AddMember("last base status", base_compaction_status_value, root.GetAllocator()); |
1092 | 33 | rapidjson::Value full_compaction_status_value; |
1093 | 33 | full_compaction_status_value.SetString(_last_full_compaction_status.c_str(), |
1094 | 33 | cast_set<uint>(_last_full_compaction_status.length()), |
1095 | 33 | root.GetAllocator()); |
1096 | 33 | root.AddMember("last full status", full_compaction_status_value, root.GetAllocator()); |
1097 | 33 | rapidjson::Value exec_compaction_time; |
1098 | 33 | std::string num_str {std::to_string(exec_compaction_time_us.load())}; |
1099 | 33 | exec_compaction_time.SetString(num_str.c_str(), cast_set<uint>(num_str.length()), |
1100 | 33 | root.GetAllocator()); |
1101 | 33 | root.AddMember("exec compaction time us", exec_compaction_time, root.GetAllocator()); |
1102 | 33 | rapidjson::Value local_read_time; |
1103 | 33 | num_str = std::to_string(local_read_time_us.load()); |
1104 | 33 | local_read_time.SetString(num_str.c_str(), cast_set<uint>(num_str.length()), |
1105 | 33 | root.GetAllocator()); |
1106 | 33 | root.AddMember("compaction local read time us", local_read_time, root.GetAllocator()); |
1107 | 33 | rapidjson::Value remote_read_time; |
1108 | 33 | num_str = std::to_string(remote_read_time_us.load()); |
1109 | 33 | remote_read_time.SetString(num_str.c_str(), cast_set<uint>(num_str.length()), |
1110 | 33 | root.GetAllocator()); |
1111 | 33 | root.AddMember("compaction remote read time us", remote_read_time, root.GetAllocator()); |
1112 | | |
1113 | | // print all rowsets' version as an array |
1114 | 33 | rapidjson::Document versions_arr; |
1115 | 33 | rapidjson::Document missing_versions_arr; |
1116 | 33 | versions_arr.SetArray(); |
1117 | 33 | missing_versions_arr.SetArray(); |
1118 | 33 | int64_t last_version = -1; |
1119 | 148 | for (auto& rowset : rowsets) { |
1120 | 148 | const Version& ver = rowset->version(); |
1121 | 148 | if (ver.first != last_version + 1) { |
1122 | 0 | rapidjson::Value miss_value; |
1123 | 0 | miss_value.SetString(fmt::format("[{}-{}]", last_version + 1, ver.first - 1).c_str(), |
1124 | 0 | missing_versions_arr.GetAllocator()); |
1125 | 0 | missing_versions_arr.PushBack(miss_value, missing_versions_arr.GetAllocator()); |
1126 | 0 | } |
1127 | 148 | rapidjson::Value value; |
1128 | 148 | std::string version_str = rowset->get_rowset_info_str(); |
1129 | 148 | value.SetString(version_str.c_str(), cast_set<uint32_t>(version_str.length()), |
1130 | 148 | versions_arr.GetAllocator()); |
1131 | 148 | versions_arr.PushBack(value, versions_arr.GetAllocator()); |
1132 | 148 | last_version = ver.second; |
1133 | 148 | } |
1134 | 33 | root.AddMember("rowsets", versions_arr, root.GetAllocator()); |
1135 | 33 | root.AddMember("missing_rowsets", missing_versions_arr, root.GetAllocator()); |
1136 | | |
1137 | | // print all stale rowsets' version as an array |
1138 | 33 | rapidjson::Document stale_versions_arr; |
1139 | 33 | stale_versions_arr.SetArray(); |
1140 | 540 | for (auto& rowset : stale_rowsets) { |
1141 | 540 | rapidjson::Value value; |
1142 | 540 | std::string version_str = rowset->get_rowset_info_str(); |
1143 | 540 | value.SetString(version_str.c_str(), cast_set<uint32_t>(version_str.length()), |
1144 | 540 | stale_versions_arr.GetAllocator()); |
1145 | 540 | stale_versions_arr.PushBack(value, stale_versions_arr.GetAllocator()); |
1146 | 540 | } |
1147 | 33 | root.AddMember("stale_rowsets", stale_versions_arr, root.GetAllocator()); |
1148 | | |
1149 | | // add stale version rowsets |
1150 | 33 | root.AddMember("stale version path", path_arr, root.GetAllocator()); |
1151 | | |
1152 | | // to json string |
1153 | 33 | rapidjson::StringBuffer strbuf; |
1154 | 33 | rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(strbuf); |
1155 | 33 | root.Accept(writer); |
1156 | 33 | *json_result = std::string(strbuf.GetString()); |
1157 | 33 | } |
1158 | | |
1159 | 0 | void CloudTablet::set_cumulative_layer_point(int64_t new_point) { |
1160 | 0 | if (new_point == Tablet::K_INVALID_CUMULATIVE_POINT || new_point >= _cumulative_point) { |
1161 | 0 | _cumulative_point = new_point; |
1162 | 0 | return; |
1163 | 0 | } |
1164 | | // cumulative point should only be reset to -1, or be increased |
1165 | | // FIXME: could happen in currently unresolved race conditions |
1166 | 0 | LOG(WARNING) << "Unexpected cumulative point: " << new_point |
1167 | 0 | << ", origin: " << _cumulative_point.load(); |
1168 | 0 | } |
1169 | | |
1170 | | Status CloudTablet::check_rowset_schema_for_build_index(std::vector<TColumn>& columns, |
1171 | 10 | int schema_version) { |
1172 | 10 | std::map<std::string, TabletColumn> fe_col_map; |
1173 | 14 | for (int i = 0; i < columns.size(); i++) { |
1174 | 4 | fe_col_map[columns[i].column_name] = TabletColumn(columns[i]); |
1175 | 4 | } |
1176 | | |
1177 | 10 | std::shared_lock rlock(_meta_lock); |
1178 | 10 | for (const auto& [version, rs] : _rs_version_map) { |
1179 | 4 | if (version.first == 0) { |
1180 | 0 | continue; |
1181 | 0 | } |
1182 | | |
1183 | 4 | if (rs->tablet_schema()->schema_version() >= schema_version) { |
1184 | 0 | continue; |
1185 | 0 | } |
1186 | | |
1187 | 4 | for (auto rs_col : rs->tablet_schema()->columns()) { |
1188 | 4 | auto find_ret = fe_col_map.find(rs_col->name()); |
1189 | 4 | if (find_ret == fe_col_map.end()) { |
1190 | 1 | return Status::InternalError( |
1191 | 1 | "check rowset meta failed:rowset's col is dropped in FE."); |
1192 | 1 | } |
1193 | | |
1194 | 3 | if (rs_col->unique_id() != find_ret->second.unique_id()) { |
1195 | 1 | return Status::InternalError("check rowset meta failed:col id not match."); |
1196 | 1 | } |
1197 | | |
1198 | 2 | if (rs_col->type() != find_ret->second.type()) { |
1199 | 1 | return Status::InternalError("check rowset meta failed:col type not match."); |
1200 | 1 | } |
1201 | 2 | } |
1202 | 4 | } |
1203 | | |
1204 | 7 | return Status::OK(); |
1205 | 10 | } |
1206 | | |
1207 | | Result<RowsetSharedPtr> CloudTablet::pick_a_rowset_for_index_change(int schema_version, |
1208 | 9 | bool& is_base_rowset) { |
1209 | 9 | TEST_SYNC_POINT_RETURN_WITH_VALUE("CloudTablet::pick_a_rowset_for_index_change", |
1210 | 2 | Result<RowsetSharedPtr>(nullptr)); |
1211 | 2 | RowsetSharedPtr ret_rowset = nullptr; |
1212 | 2 | std::shared_lock rlock(_meta_lock); |
1213 | 2 | for (const auto& [version, rs] : _rs_version_map) { |
1214 | 2 | if (version.first == 0) { |
1215 | 0 | continue; |
1216 | 0 | } |
1217 | 2 | if (rs->num_rows() == 0) { |
1218 | 1 | VLOG_DEBUG << "[index_change]find empty rs, index change may " |
1219 | 0 | "failed, id=" |
1220 | 0 | << rs->rowset_id().to_string(); |
1221 | 1 | } |
1222 | | |
1223 | 2 | if (rs->tablet_schema()->schema_version() >= schema_version) { |
1224 | 2 | VLOG_DEBUG << "[index_change] skip rowset " << rs->tablet_schema()->schema_version() |
1225 | 0 | << "," << schema_version; |
1226 | 2 | continue; |
1227 | 2 | } |
1228 | | |
1229 | 0 | if (ret_rowset == nullptr) { |
1230 | 0 | ret_rowset = rs; |
1231 | 0 | continue; |
1232 | 0 | } |
1233 | | |
1234 | 0 | if (rs->start_version() > ret_rowset->start_version()) { |
1235 | 0 | ret_rowset = rs; |
1236 | 0 | } |
1237 | 0 | } |
1238 | | |
1239 | 2 | if (ret_rowset != nullptr) { |
1240 | 0 | is_base_rowset = ret_rowset->version().first < _cumulative_point; |
1241 | 0 | } |
1242 | | |
1243 | 2 | return ret_rowset; |
1244 | 9 | } |
1245 | | |
1246 | 0 | std::vector<RowsetSharedPtr> CloudTablet::pick_candidate_rowsets_to_base_compaction() { |
1247 | 0 | std::vector<RowsetSharedPtr> candidate_rowsets; |
1248 | 0 | { |
1249 | 0 | std::shared_lock rlock(_meta_lock); |
1250 | 0 | for (const auto& [version, rs] : _rs_version_map) { |
1251 | 0 | if (version.first != 0 && version.first < _cumulative_point && |
1252 | 0 | (_alter_version == -1 || version.second <= _alter_version)) { |
1253 | 0 | candidate_rowsets.push_back(rs); |
1254 | 0 | } |
1255 | 0 | } |
1256 | 0 | } |
1257 | 0 | std::sort(candidate_rowsets.begin(), candidate_rowsets.end(), Rowset::comparator); |
1258 | 0 | return candidate_rowsets; |
1259 | 0 | } |
1260 | | |
1261 | 0 | std::vector<RowsetSharedPtr> CloudTablet::pick_candidate_rowsets_to_full_compaction() { |
1262 | 0 | std::vector<RowsetSharedPtr> candidate_rowsets; |
1263 | 0 | { |
1264 | 0 | std::shared_lock rlock(_meta_lock); |
1265 | 0 | for (auto& [v, rs] : _rs_version_map) { |
1266 | | // MUST NOT compact rowset [0-1] for some historical reasons (see cloud_schema_change) |
1267 | 0 | if (v.first != 0) { |
1268 | 0 | candidate_rowsets.push_back(rs); |
1269 | 0 | } |
1270 | 0 | } |
1271 | 0 | } |
1272 | 0 | std::sort(candidate_rowsets.begin(), candidate_rowsets.end(), Rowset::comparator); |
1273 | 0 | return candidate_rowsets; |
1274 | 0 | } |
1275 | | |
1276 | 0 | CalcDeleteBitmapExecutor* CloudTablet::calc_delete_bitmap_executor() { |
1277 | 0 | return _engine.calc_delete_bitmap_executor(); |
1278 | 0 | } |
1279 | | |
1280 | | Status CloudTablet::save_delete_bitmap(const TabletTxnInfo* txn_info, int64_t txn_id, |
1281 | | DeleteBitmapPtr delete_bitmap, RowsetWriter* rowset_writer, |
1282 | | const RowsetIdUnorderedSet& cur_rowset_ids, int64_t lock_id, |
1283 | 0 | int64_t next_visible_version) { |
1284 | 0 | RowsetSharedPtr rowset = txn_info->rowset; |
1285 | 0 | int64_t cur_version = rowset->start_version(); |
1286 | | // update delete bitmap info, in order to avoid recalculation when trying again |
1287 | 0 | RETURN_IF_ERROR(_engine.txn_delete_bitmap_cache().update_tablet_txn_info( |
1288 | 0 | txn_id, tablet_id(), delete_bitmap, cur_rowset_ids, PublishStatus::PREPARE)); |
1289 | | |
1290 | 0 | if (txn_info->partial_update_info && txn_info->partial_update_info->is_partial_update() && |
1291 | 0 | rowset_writer->num_rows() > 0) { |
1292 | 0 | DBUG_EXECUTE_IF("CloudTablet::save_delete_bitmap.update_tmp_rowset.error", { |
1293 | 0 | return Status::InternalError<false>("injected update_tmp_rowset error."); |
1294 | 0 | }); |
1295 | 0 | const auto& rowset_meta = rowset->rowset_meta(); |
1296 | 0 | RETURN_IF_ERROR(_engine.meta_mgr().update_tmp_rowset(*rowset_meta)); |
1297 | 0 | } |
1298 | | |
1299 | 0 | RETURN_IF_ERROR(save_delete_bitmap_to_ms(cur_version, txn_id, delete_bitmap, lock_id, |
1300 | 0 | next_visible_version, rowset)); |
1301 | | |
1302 | | // store the delete bitmap with sentinel marks in txn_delete_bitmap_cache because if the txn is retried for some reason, |
1303 | | // it will use the delete bitmap from txn_delete_bitmap_cache when re-calculating the delete bitmap, during which it will do |
1304 | | // delete bitmap correctness check. If we store the new_delete_bitmap, the delete bitmap correctness check will fail |
1305 | 0 | RETURN_IF_ERROR(_engine.txn_delete_bitmap_cache().update_tablet_txn_info( |
1306 | 0 | txn_id, tablet_id(), delete_bitmap, cur_rowset_ids, PublishStatus::SUCCEED, |
1307 | 0 | txn_info->publish_info)); |
1308 | | |
1309 | 0 | DBUG_EXECUTE_IF("CloudTablet::save_delete_bitmap.enable_sleep", { |
1310 | 0 | auto sleep_sec = dp->param<int>("sleep", 5); |
1311 | 0 | std::this_thread::sleep_for(std::chrono::seconds(sleep_sec)); |
1312 | 0 | }); |
1313 | |
|
1314 | 0 | DBUG_EXECUTE_IF("CloudTablet::save_delete_bitmap.injected_error", { |
1315 | 0 | auto retry = dp->param<bool>("retry", false); |
1316 | 0 | auto sleep_sec = dp->param<int>("sleep", 0); |
1317 | 0 | std::this_thread::sleep_for(std::chrono::seconds(sleep_sec)); |
1318 | 0 | if (retry) { // return DELETE_BITMAP_LOCK_ERROR to let it retry |
1319 | 0 | return Status::Error<ErrorCode::DELETE_BITMAP_LOCK_ERROR>( |
1320 | 0 | "injected DELETE_BITMAP_LOCK_ERROR"); |
1321 | 0 | } else { |
1322 | 0 | return Status::InternalError<false>("injected non-retryable error"); |
1323 | 0 | } |
1324 | 0 | }); |
1325 | |
|
1326 | 0 | return Status::OK(); |
1327 | 0 | } |
1328 | | |
1329 | | Status CloudTablet::save_delete_bitmap_to_ms(int64_t cur_version, int64_t txn_id, |
1330 | | DeleteBitmapPtr delete_bitmap, int64_t lock_id, |
1331 | 0 | int64_t next_visible_version, RowsetSharedPtr rowset) { |
1332 | 0 | DeleteBitmapPtr new_delete_bitmap = std::make_shared<DeleteBitmap>(tablet_id()); |
1333 | 0 | for (auto iter = delete_bitmap->delete_bitmap.begin(); |
1334 | 0 | iter != delete_bitmap->delete_bitmap.end(); ++iter) { |
1335 | | // skip sentinel mark, which is used for delete bitmap correctness check |
1336 | 0 | if (std::get<1>(iter->first) != DeleteBitmap::INVALID_SEGMENT_ID) { |
1337 | 0 | new_delete_bitmap->merge( |
1338 | 0 | {std::get<0>(iter->first), std::get<1>(iter->first), cur_version}, |
1339 | 0 | iter->second); |
1340 | 0 | } |
1341 | 0 | } |
1342 | | // lock_id != -1 means this is in an explict txn |
1343 | 0 | bool is_explicit_txn = (lock_id != -1); |
1344 | 0 | auto ms_lock_id = !is_explicit_txn ? txn_id : lock_id; |
1345 | 0 | std::optional<StorageResource> storage_resource; |
1346 | 0 | auto storage_resource_result = rowset->rowset_meta()->remote_storage_resource(); |
1347 | 0 | if (storage_resource_result) { |
1348 | 0 | storage_resource = *storage_resource_result.value(); |
1349 | 0 | } |
1350 | 0 | RETURN_IF_ERROR(_engine.meta_mgr().update_delete_bitmap( |
1351 | 0 | *this, ms_lock_id, LOAD_INITIATOR_ID, new_delete_bitmap.get(), new_delete_bitmap.get(), |
1352 | 0 | rowset->rowset_id().to_string(), storage_resource, |
1353 | 0 | config::delete_bitmap_store_write_version, txn_id, is_explicit_txn, |
1354 | 0 | next_visible_version)); |
1355 | 0 | return Status::OK(); |
1356 | 0 | } |
1357 | | |
1358 | 0 | Versions CloudTablet::calc_missed_versions(int64_t spec_version, Versions existing_versions) const { |
1359 | 0 | DCHECK(spec_version > 0) << "invalid spec_version: " << spec_version; |
1360 | | |
1361 | | // sort the existing versions in ascending order |
1362 | 0 | std::sort(existing_versions.begin(), existing_versions.end(), |
1363 | 0 | [](const Version& a, const Version& b) { |
1364 | | // simple because 2 versions are certainly not overlapping |
1365 | 0 | return a.first < b.first; |
1366 | 0 | }); |
1367 | | |
1368 | | // From the first version(=0), find the missing version until spec_version |
1369 | 0 | int64_t last_version = -1; |
1370 | 0 | Versions missed_versions; |
1371 | 0 | for (const Version& version : existing_versions) { |
1372 | 0 | if (version.first > last_version + 1) { |
1373 | | // there is a hole between versions |
1374 | 0 | missed_versions.emplace_back(last_version + 1, std::min(version.first, spec_version)); |
1375 | 0 | } |
1376 | 0 | last_version = version.second; |
1377 | 0 | if (last_version >= spec_version) { |
1378 | 0 | break; |
1379 | 0 | } |
1380 | 0 | } |
1381 | 0 | if (last_version < spec_version) { |
1382 | | // there is a hole between the last version and the specificed version. |
1383 | 0 | missed_versions.emplace_back(last_version + 1, spec_version); |
1384 | 0 | } |
1385 | 0 | return missed_versions; |
1386 | 0 | } |
1387 | | |
1388 | | Status CloudTablet::calc_delete_bitmap_for_compaction( |
1389 | | const std::vector<RowsetSharedPtr>& input_rowsets, const RowsetSharedPtr& output_rowset, |
1390 | | const RowIdConversion& rowid_conversion, ReaderType compaction_type, int64_t merged_rows, |
1391 | | int64_t filtered_rows, int64_t initiator, DeleteBitmapPtr& output_rowset_delete_bitmap, |
1392 | 0 | bool allow_delete_in_cumu_compaction, int64_t& get_delete_bitmap_lock_start_time) { |
1393 | 0 | output_rowset_delete_bitmap = std::make_shared<DeleteBitmap>(tablet_id()); |
1394 | 0 | std::unique_ptr<RowLocationSet> missed_rows; |
1395 | 0 | if ((config::enable_missing_rows_correctness_check || |
1396 | 0 | config::enable_mow_compaction_correctness_check_core || |
1397 | 0 | config::enable_mow_compaction_correctness_check_fail) && |
1398 | 0 | !allow_delete_in_cumu_compaction && |
1399 | 0 | (compaction_type == ReaderType::READER_CUMULATIVE_COMPACTION || |
1400 | 0 | !config::enable_prune_delete_sign_when_base_compaction)) { |
1401 | | // also check duplicate key for base compaction when config::enable_prune_delete_sign_when_base_compaction==false |
1402 | 0 | missed_rows = std::make_unique<RowLocationSet>(); |
1403 | 0 | LOG(INFO) << "RowLocation Set inited succ for tablet:" << tablet_id(); |
1404 | 0 | } |
1405 | |
|
1406 | 0 | std::unique_ptr<std::map<RowsetSharedPtr, RowLocationPairList>> location_map; |
1407 | 0 | if (config::enable_rowid_conversion_correctness_check && |
1408 | 0 | tablet_schema()->cluster_key_uids().empty()) { |
1409 | 0 | location_map = std::make_unique<std::map<RowsetSharedPtr, RowLocationPairList>>(); |
1410 | 0 | LOG(INFO) << "Location Map inited succ for tablet:" << tablet_id(); |
1411 | 0 | } |
1412 | | |
1413 | | // 1. calc delete bitmap for historical data |
1414 | 0 | RETURN_IF_ERROR(_engine.meta_mgr().sync_tablet_rowsets(this)); |
1415 | 0 | Version version = max_version(); |
1416 | 0 | std::size_t missed_rows_size = 0; |
1417 | 0 | calc_compaction_output_rowset_delete_bitmap( |
1418 | 0 | input_rowsets, rowid_conversion, 0, version.second + 1, missed_rows.get(), |
1419 | 0 | location_map.get(), tablet_meta()->delete_bitmap(), output_rowset_delete_bitmap.get()); |
1420 | 0 | if (missed_rows) { |
1421 | 0 | missed_rows_size = missed_rows->size(); |
1422 | 0 | if (!allow_delete_in_cumu_compaction) { |
1423 | 0 | if ((compaction_type == ReaderType::READER_CUMULATIVE_COMPACTION || |
1424 | 0 | !config::enable_prune_delete_sign_when_base_compaction) && |
1425 | 0 | tablet_state() == TABLET_RUNNING) { |
1426 | 0 | if (merged_rows + filtered_rows >= 0 && |
1427 | 0 | merged_rows + filtered_rows != missed_rows_size) { |
1428 | 0 | std::string err_msg = fmt::format( |
1429 | 0 | "cumulative compaction: the merged rows({}), the filtered rows({}) is " |
1430 | 0 | "not equal to missed rows({}) in rowid conversion, tablet_id: {}, " |
1431 | 0 | "table_id:{}", |
1432 | 0 | merged_rows, filtered_rows, missed_rows_size, tablet_id(), table_id()); |
1433 | 0 | LOG(WARNING) << err_msg; |
1434 | 0 | if (config::enable_mow_compaction_correctness_check_core) { |
1435 | 0 | CHECK(false) << err_msg; |
1436 | 0 | } else if (config::enable_mow_compaction_correctness_check_fail) { |
1437 | 0 | return Status::InternalError<false>(err_msg); |
1438 | 0 | } else { |
1439 | 0 | DCHECK(false) << err_msg; |
1440 | 0 | } |
1441 | 0 | } |
1442 | 0 | } |
1443 | 0 | } |
1444 | 0 | } |
1445 | 0 | if (location_map) { |
1446 | 0 | RETURN_IF_ERROR(check_rowid_conversion(output_rowset, *location_map)); |
1447 | 0 | location_map->clear(); |
1448 | 0 | } |
1449 | | |
1450 | | // 2. calc delete bitmap for incremental data |
1451 | 0 | int64_t t1 = MonotonicMicros(); |
1452 | 0 | RETURN_IF_ERROR(_engine.meta_mgr().get_delete_bitmap_update_lock( |
1453 | 0 | *this, COMPACTION_DELETE_BITMAP_LOCK_ID, initiator)); |
1454 | 0 | int64_t t2 = MonotonicMicros(); |
1455 | 0 | if (compaction_type == ReaderType::READER_CUMULATIVE_COMPACTION) { |
1456 | 0 | g_cu_compaction_get_delete_bitmap_lock_time_ms << (t2 - t1) / 1000; |
1457 | 0 | } else if (compaction_type == ReaderType::READER_BASE_COMPACTION) { |
1458 | 0 | g_base_compaction_get_delete_bitmap_lock_time_ms << (t2 - t1) / 1000; |
1459 | 0 | } |
1460 | 0 | get_delete_bitmap_lock_start_time = t2; |
1461 | 0 | RETURN_IF_ERROR(_engine.meta_mgr().sync_tablet_rowsets(this)); |
1462 | 0 | int64_t t3 = MonotonicMicros(); |
1463 | |
|
1464 | 0 | calc_compaction_output_rowset_delete_bitmap( |
1465 | 0 | input_rowsets, rowid_conversion, version.second, UINT64_MAX, missed_rows.get(), |
1466 | 0 | location_map.get(), tablet_meta()->delete_bitmap(), output_rowset_delete_bitmap.get()); |
1467 | 0 | int64_t t4 = MonotonicMicros(); |
1468 | 0 | if (location_map) { |
1469 | 0 | RETURN_IF_ERROR(check_rowid_conversion(output_rowset, *location_map)); |
1470 | 0 | } |
1471 | 0 | int64_t t5 = MonotonicMicros(); |
1472 | | |
1473 | | // 3. store delete bitmap |
1474 | 0 | DeleteBitmapPtr delete_bitmap_v2 = nullptr; |
1475 | 0 | auto delete_bitmap_size = output_rowset_delete_bitmap->delete_bitmap.size(); |
1476 | 0 | auto store_version = config::delete_bitmap_store_write_version; |
1477 | 0 | if (store_version == 2 || store_version == 3) { |
1478 | 0 | delete_bitmap_v2 = std::make_shared<DeleteBitmap>(*output_rowset_delete_bitmap); |
1479 | 0 | std::vector<std::pair<RowsetId, int64_t>> retained_rowsets_to_seg_num; |
1480 | 0 | { |
1481 | 0 | std::shared_lock rlock(get_header_lock()); |
1482 | 0 | for (const auto& [rowset_version, rowset_ptr] : rowset_map()) { |
1483 | 0 | if (rowset_version.second < output_rowset->start_version()) { |
1484 | 0 | retained_rowsets_to_seg_num.emplace_back( |
1485 | 0 | std::make_pair(rowset_ptr->rowset_id(), rowset_ptr->num_segments())); |
1486 | 0 | } |
1487 | 0 | } |
1488 | 0 | } |
1489 | 0 | if (config::enable_agg_delta_delete_bitmap_for_store_v2) { |
1490 | 0 | tablet_meta()->delete_bitmap().subset_and_agg( |
1491 | 0 | retained_rowsets_to_seg_num, output_rowset->start_version(), |
1492 | 0 | output_rowset->end_version(), delete_bitmap_v2.get()); |
1493 | 0 | } else { |
1494 | 0 | tablet_meta()->delete_bitmap().subset( |
1495 | 0 | retained_rowsets_to_seg_num, output_rowset->start_version(), |
1496 | 0 | output_rowset->end_version(), delete_bitmap_v2.get()); |
1497 | 0 | } |
1498 | 0 | } |
1499 | 0 | std::optional<StorageResource> storage_resource; |
1500 | 0 | auto storage_resource_result = output_rowset->rowset_meta()->remote_storage_resource(); |
1501 | 0 | if (storage_resource_result) { |
1502 | 0 | storage_resource = *storage_resource_result.value(); |
1503 | 0 | } |
1504 | 0 | auto st = _engine.meta_mgr().update_delete_bitmap( |
1505 | 0 | *this, -1, initiator, output_rowset_delete_bitmap.get(), delete_bitmap_v2.get(), |
1506 | 0 | output_rowset->rowset_id().to_string(), storage_resource, store_version); |
1507 | 0 | int64_t t6 = MonotonicMicros(); |
1508 | 0 | LOG(INFO) << "calc_delete_bitmap_for_compaction, tablet_id=" << tablet_id() |
1509 | 0 | << ", get lock cost " << (t2 - t1) << " us, sync rowsets cost " << (t3 - t2) |
1510 | 0 | << " us, calc delete bitmap cost " << (t4 - t3) << " us, check rowid conversion cost " |
1511 | 0 | << (t5 - t4) << " us, store delete bitmap cost " << (t6 - t5) |
1512 | 0 | << " us, st=" << st.to_string() << ". store_version=" << store_version |
1513 | 0 | << ", calculated delete bitmap size=" << delete_bitmap_size |
1514 | 0 | << ", update delete bitmap size=" |
1515 | 0 | << output_rowset_delete_bitmap->delete_bitmap.size(); |
1516 | 0 | return st; |
1517 | 0 | } |
1518 | | |
1519 | | void CloudTablet::agg_delete_bitmap_for_compaction( |
1520 | | int64_t start_version, int64_t end_version, const std::vector<RowsetSharedPtr>& pre_rowsets, |
1521 | | DeleteBitmapPtr& new_delete_bitmap, |
1522 | 0 | std::map<std::string, int64_t>& pre_rowset_to_versions) { |
1523 | 0 | for (auto& rowset : pre_rowsets) { |
1524 | 0 | for (uint32_t seg_id = 0; seg_id < rowset->num_segments(); ++seg_id) { |
1525 | 0 | auto d = tablet_meta()->delete_bitmap().get_agg_without_cache( |
1526 | 0 | {rowset->rowset_id(), seg_id, end_version}, start_version); |
1527 | 0 | if (d->isEmpty()) { |
1528 | 0 | continue; |
1529 | 0 | } |
1530 | 0 | VLOG_DEBUG << "agg delete bitmap for tablet_id=" << tablet_id() |
1531 | 0 | << ", rowset_id=" << rowset->rowset_id() << ", seg_id=" << seg_id |
1532 | 0 | << ", rowset_version=" << rowset->version().to_string() |
1533 | 0 | << ". compaction start_version=" << start_version |
1534 | 0 | << ", end_version=" << end_version |
1535 | 0 | << ". delete_bitmap cardinality=" << d->cardinality(); |
1536 | 0 | DeleteBitmap::BitmapKey end_key {rowset->rowset_id(), seg_id, end_version}; |
1537 | 0 | new_delete_bitmap->set(end_key, *d); |
1538 | 0 | pre_rowset_to_versions[rowset->rowset_id().to_string()] = rowset->version().second; |
1539 | 0 | } |
1540 | 0 | } |
1541 | 0 | } |
1542 | | |
1543 | 0 | Status CloudTablet::sync_meta() { |
1544 | 0 | if (!config::enable_file_cache) { |
1545 | 0 | return Status::OK(); |
1546 | 0 | } |
1547 | | |
1548 | 0 | TabletMetaSharedPtr tablet_meta; |
1549 | 0 | auto st = _engine.meta_mgr().get_tablet_meta(tablet_id(), &tablet_meta); |
1550 | 0 | if (!st.ok()) { |
1551 | 0 | if (st.is<ErrorCode::NOT_FOUND>()) { |
1552 | 0 | clear_cache(); |
1553 | 0 | } |
1554 | 0 | return st; |
1555 | 0 | } |
1556 | | |
1557 | 0 | auto new_ttl_seconds = tablet_meta->ttl_seconds(); |
1558 | 0 | if (_tablet_meta->ttl_seconds() != new_ttl_seconds) { |
1559 | 0 | _tablet_meta->set_ttl_seconds(new_ttl_seconds); |
1560 | 0 | int64_t cur_time = UnixSeconds(); |
1561 | 0 | std::shared_lock rlock(_meta_lock); |
1562 | 0 | for (auto& [_, rs] : _rs_version_map) { |
1563 | 0 | for (int seg_id = 0; seg_id < rs->num_segments(); ++seg_id) { |
1564 | 0 | int64_t new_expiration_time = |
1565 | 0 | new_ttl_seconds + rs->rowset_meta()->newest_write_timestamp(); |
1566 | 0 | new_expiration_time = new_expiration_time > cur_time ? new_expiration_time : 0; |
1567 | 0 | auto file_key = Segment::file_cache_key(rs->rowset_id().to_string(), seg_id); |
1568 | 0 | auto* file_cache = io::FileCacheFactory::instance()->get_by_path(file_key); |
1569 | 0 | file_cache->modify_expiration_time(file_key, new_expiration_time); |
1570 | 0 | } |
1571 | 0 | } |
1572 | 0 | } |
1573 | |
|
1574 | 0 | auto new_compaction_policy = tablet_meta->compaction_policy(); |
1575 | 0 | if (_tablet_meta->compaction_policy() != new_compaction_policy) { |
1576 | 0 | _tablet_meta->set_compaction_policy(new_compaction_policy); |
1577 | 0 | } |
1578 | 0 | auto new_time_series_compaction_goal_size_mbytes = |
1579 | 0 | tablet_meta->time_series_compaction_goal_size_mbytes(); |
1580 | 0 | if (_tablet_meta->time_series_compaction_goal_size_mbytes() != |
1581 | 0 | new_time_series_compaction_goal_size_mbytes) { |
1582 | 0 | _tablet_meta->set_time_series_compaction_goal_size_mbytes( |
1583 | 0 | new_time_series_compaction_goal_size_mbytes); |
1584 | 0 | } |
1585 | 0 | auto new_time_series_compaction_file_count_threshold = |
1586 | 0 | tablet_meta->time_series_compaction_file_count_threshold(); |
1587 | 0 | if (_tablet_meta->time_series_compaction_file_count_threshold() != |
1588 | 0 | new_time_series_compaction_file_count_threshold) { |
1589 | 0 | _tablet_meta->set_time_series_compaction_file_count_threshold( |
1590 | 0 | new_time_series_compaction_file_count_threshold); |
1591 | 0 | } |
1592 | 0 | auto new_time_series_compaction_time_threshold_seconds = |
1593 | 0 | tablet_meta->time_series_compaction_time_threshold_seconds(); |
1594 | 0 | if (_tablet_meta->time_series_compaction_time_threshold_seconds() != |
1595 | 0 | new_time_series_compaction_time_threshold_seconds) { |
1596 | 0 | _tablet_meta->set_time_series_compaction_time_threshold_seconds( |
1597 | 0 | new_time_series_compaction_time_threshold_seconds); |
1598 | 0 | } |
1599 | 0 | auto new_time_series_compaction_empty_rowsets_threshold = |
1600 | 0 | tablet_meta->time_series_compaction_empty_rowsets_threshold(); |
1601 | 0 | if (_tablet_meta->time_series_compaction_empty_rowsets_threshold() != |
1602 | 0 | new_time_series_compaction_empty_rowsets_threshold) { |
1603 | 0 | _tablet_meta->set_time_series_compaction_empty_rowsets_threshold( |
1604 | 0 | new_time_series_compaction_empty_rowsets_threshold); |
1605 | 0 | } |
1606 | 0 | auto new_time_series_compaction_level_threshold = |
1607 | 0 | tablet_meta->time_series_compaction_level_threshold(); |
1608 | 0 | if (_tablet_meta->time_series_compaction_level_threshold() != |
1609 | 0 | new_time_series_compaction_level_threshold) { |
1610 | 0 | _tablet_meta->set_time_series_compaction_level_threshold( |
1611 | 0 | new_time_series_compaction_level_threshold); |
1612 | 0 | } |
1613 | |
|
1614 | 0 | return Status::OK(); |
1615 | 0 | } |
1616 | | |
1617 | 0 | void CloudTablet::build_tablet_report_info(TTabletInfo* tablet_info) { |
1618 | 0 | std::shared_lock rdlock(_meta_lock); |
1619 | 0 | tablet_info->__set_total_version_count(_tablet_meta->version_count()); |
1620 | 0 | tablet_info->__set_tablet_id(_tablet_meta->tablet_id()); |
1621 | | // Currently, this information will not be used by the cloud report, |
1622 | | // but it may be used in the future. |
1623 | 0 | } |
1624 | | |
1625 | | Status CloudTablet::check_delete_bitmap_cache(int64_t txn_id, |
1626 | 0 | DeleteBitmap* expected_delete_bitmap) { |
1627 | 0 | DeleteBitmapPtr cached_delete_bitmap; |
1628 | 0 | CloudStorageEngine& engine = ExecEnv::GetInstance()->storage_engine().to_cloud(); |
1629 | 0 | Status st = engine.txn_delete_bitmap_cache().get_delete_bitmap( |
1630 | 0 | txn_id, tablet_id(), &cached_delete_bitmap, nullptr, nullptr); |
1631 | 0 | if (st.ok()) { |
1632 | 0 | bool res = (expected_delete_bitmap->cardinality() == cached_delete_bitmap->cardinality()); |
1633 | 0 | auto msg = fmt::format( |
1634 | 0 | "delete bitmap cache check failed, cur_cardinality={}, cached_cardinality={}" |
1635 | 0 | "txn_id={}, tablet_id={}", |
1636 | 0 | expected_delete_bitmap->cardinality(), cached_delete_bitmap->cardinality(), txn_id, |
1637 | 0 | tablet_id()); |
1638 | 0 | if (!res) { |
1639 | 0 | DCHECK(res) << msg; |
1640 | 0 | return Status::InternalError<false>(msg); |
1641 | 0 | } |
1642 | 0 | } |
1643 | 0 | return Status::OK(); |
1644 | 0 | } |
1645 | | |
1646 | 18 | WarmUpState CloudTablet::get_rowset_warmup_state(RowsetId rowset_id) { |
1647 | 18 | std::shared_lock rlock(_meta_lock); |
1648 | 18 | if (!_rowset_warm_up_states.contains(rowset_id)) { |
1649 | 1 | return WarmUpState::NONE; |
1650 | 1 | } |
1651 | 17 | return _rowset_warm_up_states[rowset_id].state; |
1652 | 18 | } |
1653 | | |
1654 | | bool CloudTablet::add_rowset_warmup_state(const RowsetMeta& rowset, WarmUpState state, |
1655 | 15 | std::chrono::steady_clock::time_point start_tp) { |
1656 | 15 | std::lock_guard wlock(_meta_lock); |
1657 | 15 | return add_rowset_warmup_state_unlocked(rowset, state, start_tp); |
1658 | 15 | } |
1659 | | |
1660 | 3 | void CloudTablet::update_rowset_warmup_state_inverted_idx_num(RowsetId rowset_id, int64_t delta) { |
1661 | 3 | std::lock_guard wlock(_meta_lock); |
1662 | 3 | update_rowset_warmup_state_inverted_idx_num_unlocked(rowset_id, delta); |
1663 | 3 | } |
1664 | | |
1665 | | void CloudTablet::update_rowset_warmup_state_inverted_idx_num_unlocked(RowsetId rowset_id, |
1666 | 3 | int64_t delta) { |
1667 | 3 | if (!_rowset_warm_up_states.contains(rowset_id)) { |
1668 | 0 | return; |
1669 | 0 | } |
1670 | 3 | _rowset_warm_up_states[rowset_id].num_inverted_idx += delta; |
1671 | 3 | } |
1672 | | |
1673 | | bool CloudTablet::add_rowset_warmup_state_unlocked(const RowsetMeta& rowset, WarmUpState state, |
1674 | 15 | std::chrono::steady_clock::time_point start_tp) { |
1675 | 15 | if (_rowset_warm_up_states.contains(rowset.rowset_id())) { |
1676 | 1 | return false; |
1677 | 1 | } |
1678 | 14 | if (state == WarmUpState::TRIGGERED_BY_JOB) { |
1679 | 10 | g_file_cache_warm_up_rowset_triggered_by_job_num << 1; |
1680 | 10 | } else if (state == WarmUpState::TRIGGERED_BY_SYNC_ROWSET) { |
1681 | 4 | g_file_cache_warm_up_rowset_triggered_by_sync_rowset_num << 1; |
1682 | 4 | } |
1683 | 14 | _rowset_warm_up_states[rowset.rowset_id()] = { |
1684 | 14 | .state = state, .num_segments = rowset.num_segments(), .start_tp = start_tp}; |
1685 | 14 | return true; |
1686 | 15 | } |
1687 | | |
1688 | | WarmUpState CloudTablet::complete_rowset_segment_warmup(RowsetId rowset_id, Status status, |
1689 | | int64_t segment_num, |
1690 | 17 | int64_t inverted_idx_num) { |
1691 | 17 | std::lock_guard wlock(_meta_lock); |
1692 | 17 | if (!_rowset_warm_up_states.contains(rowset_id)) { |
1693 | 1 | return WarmUpState::NONE; |
1694 | 1 | } |
1695 | 16 | VLOG_DEBUG << "complete rowset segment warmup for rowset " << rowset_id << ", " << status; |
1696 | 16 | if (segment_num > 0) { |
1697 | 14 | g_file_cache_warm_up_segment_complete_num << segment_num; |
1698 | 14 | if (!status.ok()) { |
1699 | 1 | g_file_cache_warm_up_segment_failed_num << segment_num; |
1700 | 1 | } |
1701 | 14 | } |
1702 | 16 | if (inverted_idx_num > 0) { |
1703 | 2 | g_file_cache_warm_up_inverted_idx_complete_num << inverted_idx_num; |
1704 | 2 | if (!status.ok()) { |
1705 | 0 | g_file_cache_warm_up_inverted_idx_failed_num << inverted_idx_num; |
1706 | 0 | } |
1707 | 2 | } |
1708 | 16 | _rowset_warm_up_states[rowset_id].done(segment_num, inverted_idx_num); |
1709 | 16 | if (_rowset_warm_up_states[rowset_id].has_finished()) { |
1710 | 6 | g_file_cache_warm_up_rowset_complete_num << 1; |
1711 | 6 | auto cost = std::chrono::duration_cast<std::chrono::milliseconds>( |
1712 | 6 | std::chrono::steady_clock::now() - |
1713 | 6 | _rowset_warm_up_states[rowset_id].start_tp) |
1714 | 6 | .count(); |
1715 | 6 | g_file_cache_warm_up_rowset_all_segments_latency << cost; |
1716 | 6 | _rowset_warm_up_states[rowset_id].state = WarmUpState::DONE; |
1717 | 6 | } |
1718 | 16 | return _rowset_warm_up_states[rowset_id].state; |
1719 | 17 | } |
1720 | | |
1721 | 200 | bool CloudTablet::is_rowset_warmed_up(const RowsetId& rowset_id) const { |
1722 | 200 | auto it = _rowset_warm_up_states.find(rowset_id); |
1723 | 200 | if (it == _rowset_warm_up_states.end()) { |
1724 | 58 | return false; |
1725 | 58 | } |
1726 | 142 | return it->second.state == WarmUpState::DONE; |
1727 | 200 | } |
1728 | | |
1729 | 598 | void CloudTablet::add_warmed_up_rowset(const RowsetId& rowset_id) { |
1730 | 598 | _rowset_warm_up_states[rowset_id] = {.state = WarmUpState::DONE, |
1731 | 598 | .num_segments = 1, |
1732 | 598 | .start_tp = std::chrono::steady_clock::now()}; |
1733 | 598 | } |
1734 | | |
1735 | | #include "common/compile_check_end.h" |
1736 | | } // namespace doris |