be/src/storage/compaction/compaction.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 "storage/compaction/compaction.h" |
19 | | |
20 | | #include <fmt/format.h> |
21 | | #include <gen_cpp/olap_file.pb.h> |
22 | | #include <glog/logging.h> |
23 | | |
24 | | #include <algorithm> |
25 | | #include <atomic> |
26 | | #include <cstdint> |
27 | | #include <cstdlib> |
28 | | #include <list> |
29 | | #include <map> |
30 | | #include <memory> |
31 | | #include <mutex> |
32 | | #include <nlohmann/json.hpp> |
33 | | #include <numeric> |
34 | | #include <ostream> |
35 | | #include <ranges> |
36 | | #include <set> |
37 | | #include <shared_mutex> |
38 | | #include <utility> |
39 | | |
40 | | #include "cloud/cloud_meta_mgr.h" |
41 | | #include "cloud/cloud_storage_engine.h" |
42 | | #include "cloud/cloud_tablet.h" |
43 | | #include "cloud/config.h" |
44 | | #include "cloud/pb_convert.h" |
45 | | #include "common/config.h" |
46 | | #include "common/metrics/doris_metrics.h" |
47 | | #include "common/status.h" |
48 | | #include "cpp/sync_point.h" |
49 | | #include "exec/common/variant_util.h" |
50 | | #include "io/cache/block_file_cache_factory.h" |
51 | | #include "io/fs/file_system.h" |
52 | | #include "io/fs/file_writer.h" |
53 | | #include "io/fs/remote_file_system.h" |
54 | | #include "io/io_common.h" |
55 | | #include "runtime/memory/mem_tracker_limiter.h" |
56 | | #include "runtime/thread_context.h" |
57 | | #include "storage/compaction/collection_statistics.h" |
58 | | #include "storage/compaction/cumulative_compaction.h" |
59 | | #include "storage/compaction/cumulative_compaction_binlog_policy.h" |
60 | | #include "storage/compaction/cumulative_compaction_policy.h" |
61 | | #include "storage/compaction/cumulative_compaction_time_series_policy.h" |
62 | | #include "storage/compaction_task_tracker.h" |
63 | | #include "storage/data_dir.h" |
64 | | #include "storage/index/index_file_reader.h" |
65 | | #include "storage/index/index_file_writer.h" |
66 | | #include "storage/index/inverted/inverted_index_compaction.h" |
67 | | #include "storage/index/inverted/inverted_index_desc.h" |
68 | | #include "storage/index/inverted/inverted_index_fs_directory.h" |
69 | | #include "storage/olap_common.h" |
70 | | #include "storage/olap_define.h" |
71 | | #include "storage/rowset/beta_rowset.h" |
72 | | #include "storage/rowset/beta_rowset_reader.h" |
73 | | #include "storage/rowset/beta_rowset_writer.h" |
74 | | #include "storage/rowset/rowset.h" |
75 | | #include "storage/rowset/rowset_fwd.h" |
76 | | #include "storage/rowset/rowset_meta.h" |
77 | | #include "storage/rowset/rowset_writer.h" |
78 | | #include "storage/rowset/rowset_writer_context.h" |
79 | | #include "storage/storage_engine.h" |
80 | | #include "storage/storage_policy.h" |
81 | | #include "storage/tablet/tablet.h" |
82 | | #include "storage/tablet/tablet_meta.h" |
83 | | #include "storage/tablet/tablet_meta_manager.h" |
84 | | #include "storage/task/engine_checksum_task.h" |
85 | | #include "storage/txn/txn_manager.h" |
86 | | #include "storage/utils.h" |
87 | | #include "util/pretty_printer.h" |
88 | | #include "util/time.h" |
89 | | #include "util/trace.h" |
90 | | |
91 | | using std::vector; |
92 | | |
93 | | namespace doris { |
94 | | using namespace ErrorCode; |
95 | | |
96 | | // Determine whether to enable index-only file cache mode for compaction output. |
97 | | // This function decides if only index files should be written to cache, based on: |
98 | | // - write_file_cache: whether file cache is enabled |
99 | | // - compaction_type: type of compaction (base or cumulative) |
100 | | // - enable_base_index_only: config flag for base compaction |
101 | | // - enable_cumu_index_only: config flag for cumulative compaction |
102 | | // Returns true if index-only mode should be enabled, false otherwise. |
103 | | bool should_enable_compaction_cache_index_only(bool write_file_cache, ReaderType compaction_type, |
104 | | bool enable_base_index_only, |
105 | 10.0k | bool enable_cumu_index_only) { |
106 | 10.0k | if (!write_file_cache) { |
107 | 120 | return false; |
108 | 120 | } |
109 | | |
110 | 9.94k | if (compaction_type == ReaderType::READER_BASE_COMPACTION && enable_base_index_only) { |
111 | 2 | return true; |
112 | 2 | } |
113 | | |
114 | 9.94k | if (compaction_type == ReaderType::READER_CUMULATIVE_COMPACTION && enable_cumu_index_only) { |
115 | 2 | return true; |
116 | 2 | } |
117 | | |
118 | 9.93k | return false; |
119 | 9.94k | } |
120 | | |
121 | | namespace { |
122 | | |
123 | | bool is_rowset_tidy(std::string& pre_max_key, bool& pre_rs_key_bounds_truncated, |
124 | 107 | const RowsetSharedPtr& rhs) { |
125 | 107 | size_t min_tidy_size = config::ordered_data_compaction_min_segment_size; |
126 | 107 | if (rhs->num_segments() == 0) { |
127 | 8 | return true; |
128 | 8 | } |
129 | 99 | if (rhs->is_segments_overlapping()) { |
130 | 0 | return false; |
131 | 0 | } |
132 | | // check segment size |
133 | 99 | auto* beta_rowset = reinterpret_cast<BetaRowset*>(rhs.get()); |
134 | 99 | std::vector<size_t> segments_size; |
135 | 99 | RETURN_FALSE_IF_ERROR(beta_rowset->get_segments_size(&segments_size)); |
136 | 109 | for (auto segment_size : segments_size) { |
137 | | // is segment is too small, need to do compaction |
138 | 109 | if (segment_size < min_tidy_size) { |
139 | 2 | return false; |
140 | 2 | } |
141 | 109 | } |
142 | 96 | std::string min_key; |
143 | 96 | auto ret = rhs->first_key(&min_key); |
144 | 96 | if (!ret) { |
145 | 0 | return false; |
146 | 0 | } |
147 | 96 | bool cur_rs_key_bounds_truncated {rhs->is_segments_key_bounds_truncated()}; |
148 | 96 | if (!Slice::lhs_is_strictly_less_than_rhs(Slice {pre_max_key}, pre_rs_key_bounds_truncated, |
149 | 96 | Slice {min_key}, cur_rs_key_bounds_truncated)) { |
150 | 28 | return false; |
151 | 28 | } |
152 | 96 | CHECK(rhs->last_key(&pre_max_key)); |
153 | 68 | pre_rs_key_bounds_truncated = cur_rs_key_bounds_truncated; |
154 | 68 | return true; |
155 | 96 | } |
156 | | |
157 | 10.0k | TsoRange commit_tso_range(const std::vector<RowsetSharedPtr>& rowsets) { |
158 | 10.0k | DCHECK(!rowsets.empty()); |
159 | 10.0k | auto range = rowsets.front()->commit_tso(); |
160 | 74.1k | for (const auto& rowset : rowsets) { |
161 | 74.1k | const auto commit_tso = rowset->commit_tso(); |
162 | 74.1k | range.first = std::min(range.first, commit_tso.start_tso()); |
163 | 74.1k | range.second = std::max(range.second, commit_tso.end_tso()); |
164 | 74.1k | } |
165 | 10.0k | return range; |
166 | 10.0k | } |
167 | | |
168 | | } // namespace |
169 | | |
170 | | Compaction::Compaction(BaseTabletSPtr tablet, const std::string& label) |
171 | 208k | : _compaction_id(CompactionTaskTracker::instance()->next_compaction_id()), |
172 | | _mem_tracker( |
173 | 208k | MemTrackerLimiter::create_shared(MemTrackerLimiter::Type::COMPACTION, label)), |
174 | 208k | _tablet(std::move(tablet)), |
175 | 208k | _is_vertical(config::enable_vertical_compaction), |
176 | 208k | _allow_delete_in_cumu_compaction(config::enable_delete_when_cumu_compaction), |
177 | | _enable_vertical_compact_variant_subcolumns( |
178 | 208k | config::enable_vertical_compact_variant_subcolumns), |
179 | 208k | _enable_inverted_index_compaction(config::inverted_index_compaction_enable) { |
180 | 208k | init_profile(label); |
181 | 208k | SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_mem_tracker); |
182 | 208k | _rowid_conversion = std::make_unique<RowIdConversion>(); |
183 | 208k | } |
184 | | |
185 | 208k | Compaction::~Compaction() { |
186 | 208k | SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_mem_tracker); |
187 | 208k | _output_rs_writer.reset(); |
188 | 208k | _tablet.reset(); |
189 | 208k | _input_rowsets.clear(); |
190 | 208k | _output_rowset.reset(); |
191 | 208k | _cur_tablet_schema.reset(); |
192 | 208k | _rowid_conversion.reset(); |
193 | 208k | } |
194 | | |
195 | 18.7k | std::string Compaction::input_version_range_str() const { |
196 | 18.7k | if (_input_rowsets.empty()) return ""; |
197 | 18.7k | return fmt::format("[{}-{}]", _input_rowsets.front()->start_version(), |
198 | 18.7k | _input_rowsets.back()->end_version()); |
199 | 18.7k | } |
200 | | |
201 | | void Compaction::submit_profile_record(bool success, int64_t start_time_ms, |
202 | 10.0k | const std::string& status_msg) { |
203 | 10.0k | if (!profile_type().has_value()) { |
204 | 693 | return; |
205 | 693 | } |
206 | 9.30k | auto* tracker = CompactionTaskTracker::instance(); |
207 | 9.30k | CompletionStats stats; |
208 | | // Input stats for backfill: local compaction fills these in build_basic_info() |
209 | | // which runs inside execute_compact_impl(), so they are available now. |
210 | 9.30k | stats.input_version_range = input_version_range_str(); |
211 | 9.30k | stats.input_rowsets_count = static_cast<int64_t>(_input_rowsets.size()); |
212 | 9.30k | stats.input_row_num = _input_row_num; |
213 | 9.30k | stats.input_data_size = _input_rowsets_data_size; |
214 | 9.30k | stats.input_index_size = _input_rowsets_index_size; |
215 | 9.30k | stats.input_total_size = _input_rowsets_total_size; |
216 | 9.30k | stats.input_segments_num = input_segments_num_value(); |
217 | 9.30k | stats.end_time_ms = UnixMillis(); |
218 | 9.30k | stats.merged_rows = _stats.merged_rows; |
219 | 9.30k | stats.filtered_rows = _stats.filtered_rows; |
220 | 9.30k | stats.output_rows = _stats.output_rows; |
221 | 9.34k | if (_output_rowset) { |
222 | 9.34k | stats.output_row_num = _output_rowset->num_rows(); |
223 | 9.34k | stats.output_data_size = _output_rowset->data_disk_size(); |
224 | 9.34k | stats.output_index_size = _output_rowset->index_disk_size(); |
225 | 9.34k | stats.output_total_size = _output_rowset->total_disk_size(); |
226 | 9.34k | stats.output_segments_num = _output_rowset->num_segments(); |
227 | 9.34k | } |
228 | 9.30k | stats.output_version = _output_version.to_string(); |
229 | 9.30k | stats.is_ordered_data_compaction = _is_ordered_data_compaction; |
230 | 9.39k | if (_merge_rowsets_latency_timer) { |
231 | 9.39k | stats.merge_latency_ms = _merge_rowsets_latency_timer->value() / 1000000; |
232 | 9.39k | } |
233 | 9.30k | stats.bytes_read_from_local = _stats.bytes_read_from_local; |
234 | 9.30k | stats.bytes_read_from_remote = _stats.bytes_read_from_remote; |
235 | 9.37k | if (_mem_tracker) { |
236 | 9.37k | stats.peak_memory_bytes = _mem_tracker->peak_consumption(); |
237 | 9.37k | } |
238 | 9.34k | if (success) { |
239 | 9.34k | tracker->complete(_compaction_id, stats); |
240 | 18.4E | } else { |
241 | 18.4E | tracker->fail(_compaction_id, stats, status_msg); |
242 | 18.4E | } |
243 | 9.30k | } |
244 | | |
245 | 208k | void Compaction::init_profile(const std::string& label) { |
246 | 208k | _profile = std::make_unique<RuntimeProfile>(label); |
247 | | |
248 | 208k | _input_rowsets_data_size_counter = |
249 | 208k | ADD_COUNTER(_profile, "input_rowsets_data_size", TUnit::BYTES); |
250 | 208k | _input_rowsets_counter = ADD_COUNTER(_profile, "input_rowsets_count", TUnit::UNIT); |
251 | 208k | _input_row_num_counter = ADD_COUNTER(_profile, "input_row_num", TUnit::UNIT); |
252 | 208k | _input_segments_num_counter = ADD_COUNTER(_profile, "input_segments_num", TUnit::UNIT); |
253 | 208k | _merged_rows_counter = ADD_COUNTER(_profile, "merged_rows", TUnit::UNIT); |
254 | 208k | _filtered_rows_counter = ADD_COUNTER(_profile, "filtered_rows", TUnit::UNIT); |
255 | 208k | _output_rowset_data_size_counter = |
256 | 208k | ADD_COUNTER(_profile, "output_rowset_data_size", TUnit::BYTES); |
257 | 208k | _output_row_num_counter = ADD_COUNTER(_profile, "output_row_num", TUnit::UNIT); |
258 | 208k | _output_segments_num_counter = ADD_COUNTER(_profile, "output_segments_num", TUnit::UNIT); |
259 | 208k | _merge_rowsets_latency_timer = ADD_TIMER(_profile, "merge_rowsets_latency"); |
260 | 208k | } |
261 | | |
262 | 10.0k | int64_t Compaction::merge_way_num() { |
263 | 10.0k | int64_t way_num = 0; |
264 | 73.5k | for (auto&& rowset : _input_rowsets) { |
265 | 73.5k | way_num += rowset->rowset_meta()->get_merge_way_num(); |
266 | 73.5k | } |
267 | | |
268 | 10.0k | return way_num; |
269 | 10.0k | } |
270 | | |
271 | 10.1k | Status Compaction::merge_input_rowsets() { |
272 | 10.1k | std::vector<RowsetReaderSharedPtr> input_rs_readers; |
273 | 10.1k | input_rs_readers.reserve(_input_rowsets.size()); |
274 | 74.2k | for (auto& rowset : _input_rowsets) { |
275 | 74.2k | RowsetReaderSharedPtr rs_reader; |
276 | 74.2k | RETURN_IF_ERROR(rowset->create_reader(&rs_reader)); |
277 | 74.2k | input_rs_readers.push_back(std::move(rs_reader)); |
278 | 74.2k | } |
279 | | |
280 | 10.1k | RowsetWriterContext ctx; |
281 | | // Propagate input rowset readers into the rowset writer context before the writer is created. |
282 | | // Variant nested-group compaction uses this metadata to enable the streaming writer path. |
283 | 10.1k | ctx.input_rs_readers = input_rs_readers; |
284 | 10.1k | RETURN_IF_ERROR(construct_output_rowset_writer(ctx)); |
285 | | |
286 | | // write merged rows to output rowset |
287 | | // The test results show that merger is low-memory-footprint, there is no need to tracker its mem pool |
288 | | // if ctx.columns_to_do_index_compaction.size() > 0, it means we need to do inverted index compaction. |
289 | | // the row ID conversion matrix needs to be used for inverted index compaction. |
290 | 10.1k | if (!ctx.columns_to_do_index_compaction.empty() || |
291 | 10.1k | (_tablet->keys_type() == KeysType::UNIQUE_KEYS && |
292 | 9.69k | _tablet->enable_unique_key_merge_on_write())) { |
293 | 3.60k | _stats.rowid_conversion = _rowid_conversion.get(); |
294 | 3.60k | } |
295 | | |
296 | 10.1k | int64_t way_num = merge_way_num(); |
297 | | |
298 | 10.1k | Status res; |
299 | 10.1k | { |
300 | 10.1k | SCOPED_TIMER(_merge_rowsets_latency_timer); |
301 | | // 1. Merge segment files and write bkd inverted index |
302 | | // TODO implement vertical compaction for seq map |
303 | 10.1k | if (_is_vertical && !_tablet->tablet_schema()->has_seq_map()) { |
304 | 10.1k | if (!_tablet->tablet_schema()->cluster_key_uids().empty()) { |
305 | 115 | RETURN_IF_ERROR(update_delete_bitmap()); |
306 | 115 | } |
307 | 10.1k | auto progress_cb = [compaction_id = this->_compaction_id](int64_t total, |
308 | 37.9k | int64_t completed) { |
309 | 37.9k | CompactionTaskTracker::instance()->update_progress(compaction_id, total, completed); |
310 | 37.9k | }; |
311 | 10.1k | res = Merger::vertical_merge_rowsets(_tablet, compaction_type(), *_cur_tablet_schema, |
312 | 10.1k | input_rs_readers, _output_rs_writer.get(), |
313 | 10.1k | cast_set<uint32_t>(get_avg_segment_rows()), |
314 | 10.1k | way_num, &_stats, progress_cb); |
315 | 10.1k | } else { |
316 | 25 | if (!_tablet->tablet_schema()->cluster_key_uids().empty()) { |
317 | 0 | return Status::InternalError( |
318 | 0 | "mow table with cluster keys does not support non vertical compaction"); |
319 | 0 | } |
320 | 25 | res = Merger::vmerge_rowsets(_tablet, compaction_type(), *_cur_tablet_schema, |
321 | 25 | input_rs_readers, _output_rs_writer.get(), &_stats); |
322 | 25 | } |
323 | | |
324 | 10.1k | _tablet->last_compaction_status = res; |
325 | 10.1k | if (!res.ok()) { |
326 | 0 | return res; |
327 | 0 | } |
328 | | // 2. Merge the remaining inverted index files of the string type |
329 | 10.1k | RETURN_IF_ERROR(do_inverted_index_compaction()); |
330 | 10.1k | } |
331 | | |
332 | 10.1k | COUNTER_UPDATE(_merged_rows_counter, _stats.merged_rows); |
333 | 10.1k | COUNTER_UPDATE(_filtered_rows_counter, _stats.filtered_rows); |
334 | | |
335 | | // 3. In the `build`, `_close_file_writers` is called to close the inverted index file writer and write the final compound index file. |
336 | 10.1k | RETURN_NOT_OK_STATUS_WITH_WARN(_output_rs_writer->build(_output_rowset), |
337 | 10.1k | fmt::format("rowset writer build failed. output_version: {}", |
338 | 10.1k | _output_version.to_string())); |
339 | 10.1k | _output_rowset->rowset_meta()->set_commit_tso(commit_tso_range(_input_rowsets)); |
340 | | |
341 | | // When true, writers should remove variant extracted subcolumns from the |
342 | | // schema stored in RowsetMeta. This is used when compaction temporarily |
343 | | // extends schema to split variant subcolumns for vertical compaction but |
344 | | // the final rowset meta must not persist those extracted subcolumns. |
345 | 10.1k | if (_enable_vertical_compact_variant_subcolumns && |
346 | 10.1k | (_cur_tablet_schema->num_variant_columns() > 0)) { |
347 | 455 | _output_rowset->rowset_meta()->set_tablet_schema( |
348 | 455 | _cur_tablet_schema->copy_without_variant_extracted_columns()); |
349 | 455 | } |
350 | | |
351 | | //RETURN_IF_ERROR(_engine.meta_mgr().commit_rowset(*_output_rowset->rowset_meta().get())); |
352 | 10.1k | set_delete_predicate_for_output_rowset(); |
353 | | |
354 | 10.1k | _local_read_bytes_total = _stats.bytes_read_from_local; |
355 | 10.1k | _remote_read_bytes_total = _stats.bytes_read_from_remote; |
356 | 10.1k | DorisMetrics::instance()->local_compaction_read_bytes_total->increment(_local_read_bytes_total); |
357 | 10.1k | DorisMetrics::instance()->remote_compaction_read_bytes_total->increment( |
358 | 10.1k | _remote_read_bytes_total); |
359 | 10.1k | DorisMetrics::instance()->local_compaction_write_bytes_total->increment( |
360 | 10.1k | _stats.cached_bytes_total); |
361 | | |
362 | 10.1k | COUNTER_UPDATE(_output_rowset_data_size_counter, _output_rowset->data_disk_size()); |
363 | 10.1k | COUNTER_UPDATE(_output_row_num_counter, _output_rowset->num_rows()); |
364 | 10.1k | COUNTER_UPDATE(_output_segments_num_counter, _output_rowset->num_segments()); |
365 | | |
366 | 10.1k | return check_correctness(); |
367 | 10.1k | } |
368 | | |
369 | 10.0k | void Compaction::set_delete_predicate_for_output_rowset() { |
370 | | // Now we support delete in cumu compaction, to make all data in rowsets whose version |
371 | | // is below output_version to be delete in the future base compaction, we should carry |
372 | | // all delete predicate in the output rowset. |
373 | | // Output start version > 2 means we must set the delete predicate in the output rowset |
374 | 10.0k | if (_output_rowset->version().first > 2 && |
375 | 10.0k | (_allow_delete_in_cumu_compaction || is_index_change_compaction())) { |
376 | 347 | DeletePredicatePB delete_predicate; |
377 | 347 | std::accumulate(_input_rowsets.begin(), _input_rowsets.end(), &delete_predicate, |
378 | 347 | [](DeletePredicatePB* delete_predicate, const RowsetSharedPtr& rs) { |
379 | 346 | if (rs->rowset_meta()->has_delete_predicate()) { |
380 | 3 | delete_predicate->MergeFrom(rs->rowset_meta()->delete_predicate()); |
381 | 3 | } |
382 | 346 | return delete_predicate; |
383 | 346 | }); |
384 | | // now version in delete_predicate is deprecated |
385 | 347 | if (!delete_predicate.in_predicates().empty() || |
386 | 347 | !delete_predicate.sub_predicates_v2().empty() || |
387 | 347 | !delete_predicate.sub_predicates().empty()) { |
388 | 3 | _output_rowset->rowset_meta()->set_delete_predicate(std::move(delete_predicate)); |
389 | 3 | } |
390 | 347 | } |
391 | 10.0k | } |
392 | | |
393 | 9.97k | int64_t Compaction::get_avg_segment_rows() { |
394 | | // take care of empty rowset |
395 | | // input_rowsets_size is total disk_size of input_rowset, this size is the |
396 | | // final size after codec and compress, so expect dest segment file size |
397 | | // in disk is config::vertical_compaction_max_segment_size |
398 | 9.97k | const auto& meta = _tablet->tablet_meta(); |
399 | 9.97k | if (meta->compaction_policy() == CUMULATIVE_TIME_SERIES_POLICY) { |
400 | 4 | int64_t compaction_goal_size_mbytes = meta->time_series_compaction_goal_size_mbytes(); |
401 | | // The output segment rows should be less than total input rows |
402 | 4 | return std::min((compaction_goal_size_mbytes * 1024 * 1024 * 2) / |
403 | 4 | (_input_rowsets_data_size / (_input_row_num + 1) + 1), |
404 | 4 | _input_row_num + 1); |
405 | 4 | } |
406 | 9.97k | return std::min(config::vertical_compaction_max_segment_size / |
407 | 9.97k | (_input_rowsets_data_size / (_input_row_num + 1) + 1), |
408 | 9.97k | _input_row_num + 1); |
409 | 9.97k | } |
410 | | |
411 | | CompactionMixin::CompactionMixin(StorageEngine& engine, TabletSharedPtr tablet, |
412 | | const std::string& label) |
413 | 116k | : Compaction(tablet, label), _engine(engine) {} |
414 | | |
415 | 116k | CompactionMixin::~CompactionMixin() { |
416 | 116k | if (_state != CompactionState::SUCCESS && _output_rowset != nullptr) { |
417 | 6 | if (!_output_rowset->is_local()) { |
418 | 0 | tablet()->record_unused_remote_rowset(_output_rowset->rowset_id(), |
419 | 0 | _output_rowset->rowset_meta()->resource_id(), |
420 | 0 | _output_rowset->num_segments()); |
421 | 0 | return; |
422 | 0 | } |
423 | 6 | _engine.add_unused_rowset(_output_rowset); |
424 | 6 | } |
425 | 116k | } |
426 | | |
427 | 1.15M | Tablet* CompactionMixin::tablet() { |
428 | 1.15M | return static_cast<Tablet*>(_tablet.get()); |
429 | 1.15M | } |
430 | | |
431 | 20 | Status CompactionMixin::do_compact_ordered_rowsets() { |
432 | 20 | RETURN_IF_ERROR(build_basic_info(true)); |
433 | 20 | RowsetWriterContext ctx; |
434 | 20 | RETURN_IF_ERROR(construct_output_rowset_writer(ctx)); |
435 | 20 | const auto& output_rowset_dir = tablet()->tablet_path(); |
436 | | |
437 | 20 | LOG(INFO) << "start to do ordered data compaction, tablet=" << _tablet->tablet_id() |
438 | 20 | << ", output_version=" << _output_version; |
439 | | // link data to new rowset |
440 | 20 | auto seg_id = 0; |
441 | 20 | bool segments_key_bounds_truncated {false}; |
442 | 20 | bool any_input_aggregated {false}; |
443 | 20 | std::vector<KeyBoundsPB> segment_key_bounds; |
444 | 20 | std::vector<uint32_t> num_segment_rows; |
445 | 48 | for (auto rowset : _input_rowsets) { |
446 | 48 | RETURN_IF_ERROR( |
447 | 48 | rowset->link_files_to(output_rowset_dir, _output_rs_writer->rowset_id(), seg_id)); |
448 | 48 | seg_id += rowset->num_segments(); |
449 | 48 | segments_key_bounds_truncated |= rowset->is_segments_key_bounds_truncated(); |
450 | 48 | any_input_aggregated |= rowset->rowset_meta()->is_segments_key_bounds_aggregated(); |
451 | 48 | std::vector<KeyBoundsPB> key_bounds; |
452 | 48 | RETURN_IF_ERROR(rowset->get_segments_key_bounds(&key_bounds)); |
453 | 48 | segment_key_bounds.insert(segment_key_bounds.end(), key_bounds.begin(), key_bounds.end()); |
454 | 48 | std::vector<uint32_t> input_segment_rows; |
455 | 48 | rowset->get_num_segment_rows(&input_segment_rows); |
456 | 48 | num_segment_rows.insert(num_segment_rows.end(), input_segment_rows.begin(), |
457 | 48 | input_segment_rows.end()); |
458 | 48 | } |
459 | | // build output rowset |
460 | 20 | RowsetMetaSharedPtr rowset_meta = std::make_shared<RowsetMeta>(); |
461 | 20 | rowset_meta->set_num_rows(_input_row_num); |
462 | 20 | rowset_meta->set_total_disk_size(_input_rowsets_data_size + _input_rowsets_index_size); |
463 | 20 | rowset_meta->set_data_disk_size(_input_rowsets_data_size); |
464 | 20 | rowset_meta->set_index_disk_size(_input_rowsets_index_size); |
465 | 20 | rowset_meta->set_empty(_input_row_num == 0); |
466 | 20 | rowset_meta->set_num_segments(_input_num_segments); |
467 | 20 | rowset_meta->set_segments_overlap(_trigger_quick_merge_by_binlog ? OVERLAPPING |
468 | 20 | : NONOVERLAPPING); |
469 | 20 | rowset_meta->set_rowset_state(VISIBLE); |
470 | 20 | rowset_meta->set_segments_key_bounds_truncated(segments_key_bounds_truncated); |
471 | | // If any input was already aggregated we have no way to recover per-segment |
472 | | // bounds, so force aggregation on the output to keep the layout consistent |
473 | | // with `num_segments` / the aggregated flag, even if the config is off now. |
474 | 20 | bool aggregate_key_bounds = |
475 | 20 | any_input_aggregated || |
476 | 20 | (config::enable_aggregate_non_mow_key_bounds && |
477 | 8 | !_tablet->enable_unique_key_merge_on_write() && !tablet()->is_row_binlog_tablet()); |
478 | 20 | rowset_meta->set_segments_key_bounds(segment_key_bounds, aggregate_key_bounds); |
479 | 20 | rowset_meta->set_num_segment_rows(num_segment_rows); |
480 | 20 | rowset_meta->set_commit_tso(commit_tso_range(_input_rowsets)); |
481 | | |
482 | 20 | _output_rowset = _output_rs_writer->manual_build(rowset_meta); |
483 | | |
484 | | // 2. check variant column path stats |
485 | 20 | RETURN_IF_ERROR(variant_util::VariantCompactionUtil::check_path_stats(_input_rowsets, |
486 | 20 | _output_rowset, _tablet)); |
487 | 20 | return Status::OK(); |
488 | 20 | } |
489 | | |
490 | 60 | Status CompactionMixin::build_basic_info(bool is_ordered_compaction) { |
491 | 644 | for (auto& rowset : _input_rowsets) { |
492 | 644 | const auto& rowset_meta = rowset->rowset_meta(); |
493 | 644 | auto index_size = rowset_meta->index_disk_size(); |
494 | 644 | auto total_size = rowset_meta->total_disk_size(); |
495 | 644 | auto data_size = rowset_meta->data_disk_size(); |
496 | | // corrupted index size caused by bug before 2.1.5 or 3.0.0 version |
497 | | // try to get real index size from disk. |
498 | 644 | if (index_size < 0 || index_size > total_size * 2) { |
499 | 0 | LOG(ERROR) << "invalid index size:" << index_size << " total size:" << total_size |
500 | 0 | << " data size:" << data_size << " tablet:" << rowset_meta->tablet_id() |
501 | 0 | << " rowset:" << rowset_meta->rowset_id(); |
502 | 0 | index_size = 0; |
503 | 0 | auto st = rowset->get_inverted_index_size(&index_size); |
504 | 0 | if (!st.ok()) { |
505 | 0 | LOG(ERROR) << "failed to get inverted index size. res=" << st; |
506 | 0 | } |
507 | 0 | } |
508 | 644 | _input_rowsets_data_size += data_size; |
509 | 644 | _input_rowsets_index_size += index_size; |
510 | 644 | _input_rowsets_total_size += total_size; |
511 | 644 | _input_row_num += rowset->num_rows(); |
512 | 644 | _input_num_segments += rowset->num_segments(); |
513 | 644 | } |
514 | 60 | COUNTER_UPDATE(_input_rowsets_data_size_counter, _input_rowsets_data_size); |
515 | 60 | COUNTER_UPDATE(_input_row_num_counter, _input_row_num); |
516 | 60 | COUNTER_UPDATE(_input_segments_num_counter, _input_num_segments); |
517 | | |
518 | 60 | TEST_SYNC_POINT_RETURN_WITH_VALUE("compaction::CompactionMixin::build_basic_info", |
519 | 60 | Status::OK()); |
520 | | |
521 | 60 | _output_version = |
522 | 60 | Version(_input_rowsets.front()->start_version(), _input_rowsets.back()->end_version()); |
523 | | |
524 | 60 | _newest_write_timestamp = _input_rowsets.back()->newest_write_timestamp(); |
525 | | |
526 | 60 | std::vector<RowsetMetaSharedPtr> rowset_metas(_input_rowsets.size()); |
527 | 60 | std::transform(_input_rowsets.begin(), _input_rowsets.end(), rowset_metas.begin(), |
528 | 841 | [](const RowsetSharedPtr& rowset) { return rowset->rowset_meta(); }); |
529 | 60 | _cur_tablet_schema = _tablet->tablet_schema_with_merged_max_schema_version(rowset_metas); |
530 | | |
531 | | // if enable_vertical_compact_variant_subcolumns is true, we need to compact the variant subcolumns in seperate column groups |
532 | | // so get_extended_compaction_schema will extended the schema for variant columns |
533 | | // for ordered compaction, we don't need to extend the schema for variant columns |
534 | 60 | if (_enable_vertical_compact_variant_subcolumns && !is_ordered_compaction) { |
535 | 56 | RETURN_IF_ERROR(variant_util::VariantCompactionUtil::get_extended_compaction_schema( |
536 | 56 | _input_rowsets, _cur_tablet_schema)); |
537 | 56 | } |
538 | 60 | return Status::OK(); |
539 | 60 | } |
540 | | |
541 | 107 | bool CompactionMixin::handle_ordered_data_compaction() { |
542 | 107 | if (config::is_cloud_mode()) { |
543 | 0 | return false; |
544 | 0 | } |
545 | 107 | if (!config::enable_ordered_data_compaction) { |
546 | 0 | return false; |
547 | 0 | } |
548 | | |
549 | | // If some rowsets has idx files and some rowsets has not, we can not do link file compaction. |
550 | | // Since the output rowset will be broken. |
551 | | |
552 | | // Use schema version instead of schema hash to check if they are the same, |
553 | | // because light schema change will not change the schema hash on BE, but will increase the schema version |
554 | | // See fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java::2979 |
555 | 107 | std::vector<int32_t> schema_versions_of_rowsets; |
556 | | |
557 | 792 | for (auto input_rowset : _input_rowsets) { |
558 | 792 | schema_versions_of_rowsets.push_back(input_rowset->rowset_meta()->schema_version()); |
559 | 792 | } |
560 | | |
561 | | // If all rowsets has same schema version, then we can do link file compaction directly. |
562 | 107 | bool all_same_schema_version = |
563 | 107 | std::all_of(schema_versions_of_rowsets.begin(), schema_versions_of_rowsets.end(), |
564 | 792 | [&](int32_t v) { return v == schema_versions_of_rowsets.front(); }); |
565 | | |
566 | 107 | if (!all_same_schema_version) { |
567 | 0 | return false; |
568 | 0 | } |
569 | | |
570 | 107 | if (compaction_type() == ReaderType::READER_COLD_DATA_COMPACTION || |
571 | 107 | compaction_type() == ReaderType::READER_FULL_COMPACTION) { |
572 | | // The remote file system and full compaction does not support to link files. |
573 | 2 | return false; |
574 | 2 | } |
575 | 105 | bool is_binlog_compaction = _tablet->is_row_binlog_tablet(); |
576 | 105 | if (is_binlog_compaction && !_input_rowsets.empty()) { |
577 | 0 | RowsetIdUnorderedSet input_rowset_ids; |
578 | 0 | input_rowset_ids.reserve(_input_rowsets.size()); |
579 | 0 | for (const auto& rs : _input_rowsets) { |
580 | 0 | input_rowset_ids.insert(rs->rowset_id()); |
581 | 0 | } |
582 | 0 | if (_tablet->tablet_meta()->delete_bitmap().contain_rowsets(input_rowset_ids)) { |
583 | 0 | return false; |
584 | 0 | } |
585 | 0 | } |
586 | | |
587 | 105 | if (_tablet->keys_type() == KeysType::UNIQUE_KEYS && |
588 | 105 | _tablet->enable_unique_key_merge_on_write() && !is_binlog_compaction) { |
589 | 54 | return false; |
590 | 54 | } |
591 | | |
592 | 51 | if (_tablet->tablet_meta()->tablet_schema()->skip_write_index_on_load()) { |
593 | | // Expected to create index through normal compaction |
594 | 0 | return false; |
595 | 0 | } |
596 | | |
597 | | // check delete version: if compaction type is base compaction and |
598 | | // has a delete version, use original compaction |
599 | 51 | if (compaction_type() == ReaderType::READER_BASE_COMPACTION || |
600 | 51 | (_allow_delete_in_cumu_compaction && |
601 | 51 | compaction_type() == ReaderType::READER_CUMULATIVE_COMPACTION)) { |
602 | 0 | for (auto& rowset : _input_rowsets) { |
603 | 0 | if (rowset->rowset_meta()->has_delete_predicate()) { |
604 | 0 | return false; |
605 | 0 | } |
606 | 0 | } |
607 | 0 | } |
608 | | |
609 | 51 | if (is_binlog_compaction) { |
610 | 0 | DCHECK(!_input_rowsets.empty()) << "tablet=" << _tablet->tablet_id(); |
611 | 0 | auto compaction_level = _input_rowsets.front()->rowset_meta()->compaction_level(); |
612 | 0 | bool can_quick_merge_binlog = |
613 | 0 | compaction_level == |
614 | 0 | BinlogCumulativeCompactionPolicy::kBinlogCompactionMaxLevel - 1 && |
615 | 0 | _input_rowsets.size() >= 2 && _input_rowsets[0]->start_version() == 0; |
616 | 0 | if (!can_quick_merge_binlog) { |
617 | 0 | return false; |
618 | 0 | } |
619 | 0 | _trigger_quick_merge_by_binlog = true; |
620 | | |
621 | | // Binlog quick merge at LMax is a special meta/link compaction path selected by |
622 | | // BinlogCumulativeCompactionPolicy. It does not require the whole input to satisfy the normal |
623 | | // ordered-data tidy check, but the output rowset built by do_compact_ordered_rowsets() |
624 | | // is still NONOVERLAPPING (excluding row-binlog). |
625 | 0 | auto st = do_compact_ordered_rowsets(); |
626 | 0 | if (!st.ok()) { |
627 | 0 | LOG(WARNING) << "failed to compact ordered rowsets: " << st; |
628 | 0 | _pending_rs_guard.drop(); |
629 | 0 | } |
630 | 0 | return st.ok(); |
631 | 51 | } else { |
632 | | // Check if rowsets are tidy so we can just modify meta and do link files to handle |
633 | | // ordered data compaction. |
634 | 51 | auto input_size = _input_rowsets.size(); |
635 | 51 | std::string pre_max_key; |
636 | 51 | bool pre_rs_key_bounds_truncated {false}; |
637 | 127 | for (auto i = 0; i < input_size; ++i) { |
638 | 107 | if (!is_rowset_tidy(pre_max_key, pre_rs_key_bounds_truncated, _input_rowsets[i])) { |
639 | 31 | if (i <= input_size / 2) { |
640 | 31 | return false; |
641 | 31 | } else { |
642 | 0 | _input_rowsets.resize(i); |
643 | 0 | break; |
644 | 0 | } |
645 | 31 | } |
646 | 107 | } |
647 | 51 | } |
648 | | |
649 | | // most rowset of current compaction is nonoverlapping |
650 | | // just handle nonoverlappint rowsets |
651 | 20 | auto st = do_compact_ordered_rowsets(); |
652 | 20 | if (!st.ok()) { |
653 | 0 | LOG(WARNING) << "failed to compact ordered rowsets: " << st; |
654 | 0 | _pending_rs_guard.drop(); |
655 | 0 | } |
656 | | |
657 | 20 | return st.ok(); |
658 | 51 | } |
659 | | |
660 | 60 | Status CompactionMixin::execute_compact() { |
661 | 60 | int64_t profile_start_time_ms = UnixMillis(); |
662 | 60 | uint32_t checksum_before; |
663 | 60 | uint32_t checksum_after; |
664 | 60 | bool enable_compaction_checksum = config::enable_compaction_checksum; |
665 | 60 | if (enable_compaction_checksum) { |
666 | 0 | EngineChecksumTask checksum_task(_engine, _tablet->tablet_id(), _tablet->schema_hash(), |
667 | 0 | _input_rowsets.back()->end_version(), &checksum_before); |
668 | 0 | auto st = checksum_task.execute(); |
669 | 0 | if (!st.ok()) { |
670 | 0 | submit_profile_record(false, profile_start_time_ms, st.to_string()); |
671 | 0 | return st; |
672 | 0 | } |
673 | 0 | } |
674 | | |
675 | 60 | auto* data_dir = tablet()->data_dir(); |
676 | 60 | int64_t permits = get_compaction_permits(); |
677 | 60 | data_dir->disks_compaction_score_increment(permits); |
678 | 60 | data_dir->disks_compaction_num_increment(1); |
679 | | |
680 | 60 | auto record_compaction_stats = [&](const doris::Exception& ex) { |
681 | 60 | _tablet->compaction_count.fetch_add(1, std::memory_order_relaxed); |
682 | 60 | data_dir->disks_compaction_score_increment(-permits); |
683 | 60 | data_dir->disks_compaction_num_increment(-1); |
684 | 60 | }; |
685 | | // Handler for execute_compact_impl failure (both Status error and C++ exception). |
686 | | // The macro calls this then returns, so submit_profile_record(false) must be here. |
687 | 60 | auto on_compact_impl_failure = [&](const doris::Exception& ex) { |
688 | 0 | record_compaction_stats(ex); |
689 | 0 | submit_profile_record(false, profile_start_time_ms, |
690 | 0 | ex.what() ? std::string(ex.what()) : ""); |
691 | 0 | }; |
692 | | |
693 | 60 | HANDLE_EXCEPTION_IF_CATCH_EXCEPTION(execute_compact_impl(permits), on_compact_impl_failure); |
694 | | // Only reached on success (macro returns on failure). |
695 | 60 | record_compaction_stats(doris::Exception()); |
696 | | |
697 | 60 | if (enable_compaction_checksum) { |
698 | 0 | EngineChecksumTask checksum_task(_engine, _tablet->tablet_id(), _tablet->schema_hash(), |
699 | 0 | _input_rowsets.back()->end_version(), &checksum_after); |
700 | 0 | auto st = checksum_task.execute(); |
701 | 0 | if (!st.ok()) { |
702 | 0 | submit_profile_record(false, profile_start_time_ms, st.to_string()); |
703 | 0 | return st; |
704 | 0 | } |
705 | 0 | if (checksum_before != checksum_after) { |
706 | 0 | auto mismatch_st = Status::InternalError( |
707 | 0 | "compaction tablet checksum not consistent, before={}, after={}, tablet_id={}", |
708 | 0 | checksum_before, checksum_after, _tablet->tablet_id()); |
709 | 0 | submit_profile_record(false, profile_start_time_ms, mismatch_st.to_string()); |
710 | 0 | return mismatch_st; |
711 | 0 | } |
712 | 0 | } |
713 | | |
714 | 60 | DorisMetrics::instance()->local_compaction_read_rows_total->increment(_input_row_num); |
715 | 60 | DorisMetrics::instance()->local_compaction_read_bytes_total->increment( |
716 | 60 | _input_rowsets_total_size); |
717 | | |
718 | 60 | TEST_SYNC_POINT_RETURN_WITH_VALUE("compaction::CompactionMixin::execute_compact", Status::OK()); |
719 | | |
720 | 60 | DorisMetrics::instance()->local_compaction_write_rows_total->increment( |
721 | 60 | _output_rowset->num_rows()); |
722 | 60 | DorisMetrics::instance()->local_compaction_write_bytes_total->increment( |
723 | 60 | _output_rowset->total_disk_size()); |
724 | | |
725 | 60 | _load_segment_to_cache(); |
726 | 60 | submit_profile_record(true, profile_start_time_ms); |
727 | 60 | return Status::OK(); |
728 | 60 | } |
729 | | |
730 | 60 | Status CompactionMixin::execute_compact_impl(int64_t permits) { |
731 | 60 | OlapStopWatch watch; |
732 | | |
733 | 60 | if (handle_ordered_data_compaction()) { |
734 | 4 | _is_ordered_data_compaction = true; |
735 | 4 | RETURN_IF_ERROR(modify_rowsets()); |
736 | 4 | int64_t input_compaction_level = _input_rowsets.front()->rowset_meta()->compaction_level(); |
737 | 4 | LOG(INFO) << "succeed to do ordered data " << compaction_name() |
738 | 4 | << ". tablet=" << _tablet->tablet_id() << ", output_version=" << _output_version |
739 | 4 | << ", disk=" << tablet()->data_dir()->path() |
740 | 4 | << ", input_compaction_level=" << input_compaction_level |
741 | 4 | << ", segments=" << _input_num_segments << ", input_row_num=" << _input_row_num |
742 | 4 | << ", output_row_num=" << _output_rowset->num_rows() |
743 | 4 | << ", input_rowsets_data_size=" << _input_rowsets_data_size |
744 | 4 | << ", input_rowsets_index_size=" << _input_rowsets_index_size |
745 | 4 | << ", input_rowsets_total_size=" << _input_rowsets_total_size |
746 | 4 | << ", output_rowset_data_size=" << _output_rowset->data_disk_size() |
747 | 4 | << ", output_rowset_index_size=" << _output_rowset->index_disk_size() |
748 | 4 | << ", output_rowset_total_size=" << _output_rowset->total_disk_size() |
749 | 4 | << ". elapsed time=" << watch.get_elapse_second() << "s."; |
750 | 4 | _state = CompactionState::SUCCESS; |
751 | 4 | return Status::OK(); |
752 | 4 | } |
753 | 56 | RETURN_IF_ERROR(build_basic_info()); |
754 | | |
755 | 56 | TEST_SYNC_POINT_RETURN_WITH_VALUE("compaction::CompactionMixin::execute_compact_impl", |
756 | 56 | Status::OK()); |
757 | | |
758 | 56 | VLOG_DEBUG << "dump tablet schema: " << _cur_tablet_schema->dump_structure(); |
759 | | |
760 | 56 | LOG(INFO) << "start " << compaction_name() << ". tablet=" << _tablet->tablet_id() |
761 | 56 | << ", output_version=" << _output_version << ", permits: " << permits; |
762 | | |
763 | 56 | RETURN_IF_ERROR(merge_input_rowsets()); |
764 | | |
765 | | // Currently, updates are only made in the time_series and binlog policies. |
766 | 56 | update_compaction_level(); |
767 | | |
768 | 56 | RETURN_IF_ERROR(modify_rowsets()); |
769 | | |
770 | 56 | auto* cumu_policy = tablet()->cumulative_compaction_policy(); |
771 | 56 | DCHECK(cumu_policy); |
772 | 56 | int64_t input_compaction_level = _input_rowsets.front()->rowset_meta()->compaction_level(); |
773 | 56 | LOG(INFO) << "succeed to do " << compaction_name() << " is_vertical=" << _is_vertical |
774 | 56 | << ". tablet=" << _tablet->tablet_id() << ", output_version=" << _output_version |
775 | 56 | << ", current_max_version=" << tablet()->max_version().second |
776 | 56 | << ", disk=" << tablet()->data_dir()->path() |
777 | 56 | << ", input_compaction_level=" << input_compaction_level |
778 | 56 | << ", input_segments=" << _input_num_segments << ", input_rowsets_data_size=" |
779 | 56 | << PrettyPrinter::print_bytes(_input_rowsets_data_size) |
780 | 56 | << ", input_rowsets_index_size=" |
781 | 56 | << PrettyPrinter::print_bytes(_input_rowsets_index_size) |
782 | 56 | << ", input_rowsets_total_size=" |
783 | 56 | << PrettyPrinter::print_bytes(_input_rowsets_total_size) |
784 | 56 | << ", output_rowset_data_size=" |
785 | 56 | << PrettyPrinter::print_bytes(_output_rowset->data_disk_size()) |
786 | 56 | << ", output_rowset_index_size=" |
787 | 56 | << PrettyPrinter::print_bytes(_output_rowset->index_disk_size()) |
788 | 56 | << ", output_rowset_total_size=" |
789 | 56 | << PrettyPrinter::print_bytes(_output_rowset->total_disk_size()) |
790 | 56 | << ", input_row_num=" << _input_row_num |
791 | 56 | << ", output_row_num=" << _output_rowset->num_rows() |
792 | 56 | << ", filtered_row_num=" << _stats.filtered_rows |
793 | 56 | << ", merged_row_num=" << _stats.merged_rows |
794 | 56 | << ". elapsed time=" << watch.get_elapse_second() |
795 | 56 | << "s. cumulative_compaction_policy=" << cumu_policy->name() |
796 | 56 | << ", compact_row_per_second=" |
797 | 56 | << cast_set<double>(_input_row_num) / watch.get_elapse_second(); |
798 | | |
799 | 56 | _state = CompactionState::SUCCESS; |
800 | | |
801 | 56 | return Status::OK(); |
802 | 56 | } |
803 | | |
804 | 10.1k | Status Compaction::do_inverted_index_compaction() { |
805 | 10.1k | const auto& ctx = _output_rs_writer->context(); |
806 | 10.1k | if (!_enable_inverted_index_compaction || _input_row_num <= 0 || |
807 | 10.1k | ctx.columns_to_do_index_compaction.empty()) { |
808 | 9.95k | return Status::OK(); |
809 | 9.95k | } |
810 | | |
811 | 212 | auto error_handler = [this](int64_t index_id, int64_t column_uniq_id) { |
812 | 2 | LOG(WARNING) << "failed to do index compaction" |
813 | 2 | << ". tablet=" << _tablet->tablet_id() << ". column uniq id=" << column_uniq_id |
814 | 2 | << ". index_id=" << index_id; |
815 | 4 | for (auto& rowset : _input_rowsets) { |
816 | 4 | rowset->set_skip_index_compaction(cast_set<int32_t>(column_uniq_id)); |
817 | 4 | LOG(INFO) << "mark skipping inverted index compaction next time" |
818 | 4 | << ". tablet=" << _tablet->tablet_id() << ", rowset=" << rowset->rowset_id() |
819 | 4 | << ", column uniq id=" << column_uniq_id << ", index_id=" << index_id; |
820 | 4 | } |
821 | 2 | }; |
822 | | |
823 | 212 | DBUG_EXECUTE_IF("Compaction::do_inverted_index_compaction_rowid_conversion_null", |
824 | 212 | { _stats.rowid_conversion = nullptr; }) |
825 | 212 | if (!_stats.rowid_conversion) { |
826 | 0 | LOG(WARNING) << "failed to do index compaction, rowid conversion is null" |
827 | 0 | << ". tablet=" << _tablet->tablet_id() |
828 | 0 | << ", input row number=" << _input_row_num; |
829 | 0 | mark_skip_index_compaction(ctx, error_handler); |
830 | |
|
831 | 0 | return Status::Error<INVERTED_INDEX_COMPACTION_ERROR>( |
832 | 0 | "failed to do index compaction, rowid conversion is null. tablet={}", |
833 | 0 | _tablet->tablet_id()); |
834 | 0 | } |
835 | | |
836 | 212 | OlapStopWatch inverted_watch; |
837 | | |
838 | | // translation vec |
839 | | // <<dest_idx_num, dest_docId>> |
840 | | // the first level vector: index indicates src segment. |
841 | | // the second level vector: index indicates row id of source segment, |
842 | | // value indicates row id of destination segment. |
843 | | // <UINT32_MAX, UINT32_MAX> indicates current row not exist. |
844 | 212 | const auto& trans_vec = _stats.rowid_conversion->get_rowid_conversion_map(); |
845 | | |
846 | | // source rowset,segment -> index_id |
847 | 212 | const auto& src_seg_to_id_map = _stats.rowid_conversion->get_src_segment_to_id_map(); |
848 | | |
849 | | // dest rowset id |
850 | 212 | RowsetId dest_rowset_id = _stats.rowid_conversion->get_dst_rowset_id(); |
851 | | // dest segment id -> num rows |
852 | 212 | std::vector<uint32_t> dest_segment_num_rows; |
853 | 212 | RETURN_IF_ERROR(_output_rs_writer->get_segment_num_rows(&dest_segment_num_rows)); |
854 | | |
855 | 212 | auto src_segment_num = src_seg_to_id_map.size(); |
856 | 212 | auto dest_segment_num = dest_segment_num_rows.size(); |
857 | | |
858 | | // when all the input rowsets are deleted, the output rowset will be empty and dest_segment_num will be 0. |
859 | 212 | if (dest_segment_num <= 0) { |
860 | 2 | LOG(INFO) << "skip doing index compaction due to no output segments" |
861 | 2 | << ". tablet=" << _tablet->tablet_id() << ", input row number=" << _input_row_num |
862 | 2 | << ". elapsed time=" << inverted_watch.get_elapse_second() << "s."; |
863 | 2 | return Status::OK(); |
864 | 2 | } |
865 | | |
866 | | // Only write info files when debug index compaction is enabled. |
867 | | // The files are used to debug index compaction and works with index_tool. |
868 | 210 | if (config::debug_inverted_index_compaction) { |
869 | | // src index files |
870 | | // format: rowsetId_segmentId |
871 | 0 | std::vector<std::string> src_index_files(src_segment_num); |
872 | 0 | for (const auto& m : src_seg_to_id_map) { |
873 | 0 | std::pair<RowsetId, uint32_t> p = m.first; |
874 | 0 | src_index_files[m.second] = p.first.to_string() + "_" + std::to_string(p.second); |
875 | 0 | } |
876 | | |
877 | | // dest index files |
878 | | // format: rowsetId_segmentId |
879 | 0 | std::vector<std::string> dest_index_files(dest_segment_num); |
880 | 0 | for (int i = 0; i < dest_segment_num; ++i) { |
881 | 0 | auto prefix = dest_rowset_id.to_string() + "_" + std::to_string(i); |
882 | 0 | dest_index_files[i] = prefix; |
883 | 0 | } |
884 | |
|
885 | 0 | auto write_json_to_file = [&](const nlohmann::json& json_obj, |
886 | 0 | const std::string& file_name) { |
887 | 0 | io::FileWriterPtr file_writer; |
888 | 0 | std::string file_path = |
889 | 0 | fmt::format("{}/{}.json", std::string(getenv("LOG_DIR")), file_name); |
890 | 0 | RETURN_IF_ERROR(io::global_local_filesystem()->create_file(file_path, &file_writer)); |
891 | 0 | RETURN_IF_ERROR(file_writer->append(json_obj.dump())); |
892 | 0 | RETURN_IF_ERROR(file_writer->append("\n")); |
893 | 0 | return file_writer->close(); |
894 | 0 | }; |
895 | | |
896 | | // Convert trans_vec to JSON and print it |
897 | 0 | nlohmann::json trans_vec_json = trans_vec; |
898 | 0 | auto output_version = |
899 | 0 | _output_version.to_string().substr(1, _output_version.to_string().size() - 2); |
900 | 0 | RETURN_IF_ERROR(write_json_to_file( |
901 | 0 | trans_vec_json, |
902 | 0 | fmt::format("trans_vec_{}_{}", _tablet->tablet_id(), output_version))); |
903 | | |
904 | 0 | nlohmann::json src_index_files_json = src_index_files; |
905 | 0 | RETURN_IF_ERROR(write_json_to_file( |
906 | 0 | src_index_files_json, |
907 | 0 | fmt::format("src_idx_dirs_{}_{}", _tablet->tablet_id(), output_version))); |
908 | | |
909 | 0 | nlohmann::json dest_index_files_json = dest_index_files; |
910 | 0 | RETURN_IF_ERROR(write_json_to_file( |
911 | 0 | dest_index_files_json, |
912 | 0 | fmt::format("dest_idx_dirs_{}_{}", _tablet->tablet_id(), output_version))); |
913 | | |
914 | 0 | nlohmann::json dest_segment_num_rows_json = dest_segment_num_rows; |
915 | 0 | RETURN_IF_ERROR(write_json_to_file( |
916 | 0 | dest_segment_num_rows_json, |
917 | 0 | fmt::format("dest_seg_num_rows_{}_{}", _tablet->tablet_id(), output_version))); |
918 | 0 | } |
919 | | |
920 | | // create index_writer to compaction indexes |
921 | 210 | std::unordered_map<RowsetId, Rowset*> rs_id_to_rowset_map; |
922 | 1.36k | for (auto&& rs : _input_rowsets) { |
923 | 1.36k | rs_id_to_rowset_map.emplace(rs->rowset_id(), rs.get()); |
924 | 1.36k | } |
925 | | |
926 | | // src index dirs |
927 | 210 | std::vector<std::unique_ptr<IndexFileReader>> index_file_readers(src_segment_num); |
928 | 907 | for (const auto& m : src_seg_to_id_map) { |
929 | 907 | const auto& [rowset_id, seg_id] = m.first; |
930 | | |
931 | 907 | auto find_it = rs_id_to_rowset_map.find(rowset_id); |
932 | 907 | DBUG_EXECUTE_IF("Compaction::do_inverted_index_compaction_find_rowset_error", |
933 | 907 | { find_it = rs_id_to_rowset_map.end(); }) |
934 | 907 | if (find_it == rs_id_to_rowset_map.end()) [[unlikely]] { |
935 | 0 | LOG(WARNING) << "failed to do index compaction, cannot find rowset. tablet_id=" |
936 | 0 | << _tablet->tablet_id() << " rowset_id=" << rowset_id.to_string(); |
937 | 0 | mark_skip_index_compaction(ctx, error_handler); |
938 | 0 | return Status::Error<INVERTED_INDEX_COMPACTION_ERROR>( |
939 | 0 | "failed to do index compaction, cannot find rowset. tablet_id={} rowset_id={}", |
940 | 0 | _tablet->tablet_id(), rowset_id.to_string()); |
941 | 0 | } |
942 | | |
943 | 907 | auto* rowset = find_it->second; |
944 | 907 | auto fs = rowset->rowset_meta()->fs(); |
945 | 907 | DBUG_EXECUTE_IF("Compaction::do_inverted_index_compaction_get_fs_error", { fs = nullptr; }) |
946 | 907 | if (!fs) { |
947 | 0 | LOG(WARNING) << "failed to do index compaction, get fs failed. resource_id=" |
948 | 0 | << rowset->rowset_meta()->resource_id(); |
949 | 0 | mark_skip_index_compaction(ctx, error_handler); |
950 | 0 | return Status::Error<INVERTED_INDEX_COMPACTION_ERROR>( |
951 | 0 | "get fs failed, resource_id={}", rowset->rowset_meta()->resource_id()); |
952 | 0 | } |
953 | | |
954 | 907 | auto seg_path = rowset->segment_path(seg_id); |
955 | 907 | DBUG_EXECUTE_IF("Compaction::do_inverted_index_compaction_seg_path_nullptr", { |
956 | 907 | seg_path = ResultError(Status::Error<ErrorCode::INTERNAL_ERROR>( |
957 | 907 | "do_inverted_index_compaction_seg_path_nullptr")); |
958 | 907 | }) |
959 | 907 | if (!seg_path.has_value()) { |
960 | 0 | LOG(WARNING) << "failed to do index compaction, get segment path failed. tablet_id=" |
961 | 0 | << _tablet->tablet_id() << " rowset_id=" << rowset_id.to_string() |
962 | 0 | << " seg_id=" << seg_id; |
963 | 0 | mark_skip_index_compaction(ctx, error_handler); |
964 | 0 | return Status::Error<INVERTED_INDEX_COMPACTION_ERROR>( |
965 | 0 | "get segment path failed. tablet_id={} rowset_id={} seg_id={}", |
966 | 0 | _tablet->tablet_id(), rowset_id.to_string(), seg_id); |
967 | 0 | } |
968 | 907 | auto index_file_reader = std::make_unique<IndexFileReader>( |
969 | 907 | fs, |
970 | 907 | std::string {InvertedIndexDescriptor::get_index_file_path_prefix(seg_path.value())}, |
971 | 907 | _cur_tablet_schema->get_inverted_index_storage_format(), |
972 | 907 | rowset->rowset_meta()->inverted_index_file_info(seg_id), _tablet->tablet_id()); |
973 | 907 | auto st = index_file_reader->init(config::inverted_index_read_buffer_size); |
974 | 907 | DBUG_EXECUTE_IF("Compaction::do_inverted_index_compaction_init_inverted_index_file_reader", |
975 | 907 | { |
976 | 907 | st = Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( |
977 | 907 | "debug point: " |
978 | 907 | "Compaction::do_inverted_index_compaction_init_inverted_index_" |
979 | 907 | "file_reader error"); |
980 | 907 | }) |
981 | 907 | if (!st.ok()) { |
982 | 0 | LOG(WARNING) << "failed to do index compaction, init inverted index file reader " |
983 | 0 | "failed. tablet_id=" |
984 | 0 | << _tablet->tablet_id() << " rowset_id=" << rowset_id.to_string() |
985 | 0 | << " seg_id=" << seg_id; |
986 | 0 | mark_skip_index_compaction(ctx, error_handler); |
987 | 0 | return Status::Error<INVERTED_INDEX_COMPACTION_ERROR>( |
988 | 0 | "init inverted index file reader failed. tablet_id={} rowset_id={} seg_id={}", |
989 | 0 | _tablet->tablet_id(), rowset_id.to_string(), seg_id); |
990 | 0 | } |
991 | 907 | index_file_readers[m.second] = std::move(index_file_reader); |
992 | 907 | } |
993 | | |
994 | | // dest index files |
995 | | // format: rowsetId_segmentId |
996 | 210 | auto& inverted_index_file_writers = |
997 | 210 | dynamic_cast<BaseBetaRowsetWriter*>(_output_rs_writer.get())->index_file_writers(); |
998 | 210 | DBUG_EXECUTE_IF( |
999 | 210 | "Compaction::do_inverted_index_compaction_inverted_index_file_writers_size_error", |
1000 | 210 | { inverted_index_file_writers.clear(); }) |
1001 | 210 | if (inverted_index_file_writers.size() != dest_segment_num) { |
1002 | 0 | LOG(WARNING) << "failed to do index compaction, dest segment num not match. tablet_id=" |
1003 | 0 | << _tablet->tablet_id() << " dest_segment_num=" << dest_segment_num |
1004 | 0 | << " inverted_index_file_writers.size()=" |
1005 | 0 | << inverted_index_file_writers.size(); |
1006 | 0 | mark_skip_index_compaction(ctx, error_handler); |
1007 | 0 | return Status::Error<INVERTED_INDEX_COMPACTION_ERROR>( |
1008 | 0 | "dest segment num not match. tablet_id={} dest_segment_num={} " |
1009 | 0 | "inverted_index_file_writers.size()={}", |
1010 | 0 | _tablet->tablet_id(), dest_segment_num, inverted_index_file_writers.size()); |
1011 | 0 | } |
1012 | | |
1013 | | // use tmp file dir to store index files |
1014 | 210 | auto tmp_file_dir = ExecEnv::GetInstance()->get_tmp_file_dirs()->get_tmp_file_dir(); |
1015 | 210 | auto index_tmp_path = tmp_file_dir / dest_rowset_id.to_string(); |
1016 | 210 | LOG(INFO) << "start index compaction" |
1017 | 210 | << ". tablet=" << _tablet->tablet_id() << ", source index size=" << src_segment_num |
1018 | 210 | << ", destination index size=" << dest_segment_num << "."; |
1019 | | |
1020 | 210 | Status status = Status::OK(); |
1021 | 813 | for (auto&& column_uniq_id : ctx.columns_to_do_index_compaction) { |
1022 | 813 | auto col = _cur_tablet_schema->column_by_uid(column_uniq_id); |
1023 | 813 | auto index_metas = _cur_tablet_schema->inverted_indexs(col); |
1024 | 813 | DBUG_EXECUTE_IF("Compaction::do_inverted_index_compaction_can_not_find_index_meta", |
1025 | 813 | { index_metas.clear(); }) |
1026 | 813 | if (index_metas.empty()) { |
1027 | 0 | status = Status::Error<INVERTED_INDEX_COMPACTION_ERROR>( |
1028 | 0 | fmt::format("Can not find index_meta for col {}", col.name())); |
1029 | 0 | LOG(WARNING) << "failed to do index compaction, can not find index_meta for column" |
1030 | 0 | << ". tablet=" << _tablet->tablet_id() |
1031 | 0 | << ", column uniq id=" << column_uniq_id; |
1032 | 0 | error_handler(-1, column_uniq_id); |
1033 | 0 | break; |
1034 | 0 | } |
1035 | 819 | for (const auto& index_meta : index_metas) { |
1036 | 819 | std::vector<lucene::store::Directory*> dest_index_dirs(dest_segment_num); |
1037 | 819 | try { |
1038 | 819 | std::vector<std::unique_ptr<DorisCompoundReader, DirectoryDeleter>> src_idx_dirs( |
1039 | 819 | src_segment_num); |
1040 | 3.58k | for (int src_segment_id = 0; src_segment_id < src_segment_num; src_segment_id++) { |
1041 | 2.76k | auto res = index_file_readers[src_segment_id]->open(index_meta); |
1042 | 2.76k | DBUG_EXECUTE_IF("Compaction::open_inverted_index_file_reader", { |
1043 | 2.76k | res = ResultError(Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( |
1044 | 2.76k | "debug point: Compaction::open_index_file_reader error")); |
1045 | 2.76k | }) |
1046 | 2.76k | if (!res.has_value()) { |
1047 | 0 | LOG(WARNING) << "failed to do index compaction, open inverted index file " |
1048 | 0 | "reader failed" |
1049 | 0 | << ". tablet=" << _tablet->tablet_id() |
1050 | 0 | << ", column uniq id=" << column_uniq_id |
1051 | 0 | << ", src_segment_id=" << src_segment_id; |
1052 | 0 | throw Exception(ErrorCode::INVERTED_INDEX_COMPACTION_ERROR, |
1053 | 0 | res.error().msg()); |
1054 | 0 | } |
1055 | 2.76k | src_idx_dirs[src_segment_id] = std::move(res.value()); |
1056 | 2.76k | } |
1057 | 1.74k | for (int dest_segment_id = 0; dest_segment_id < dest_segment_num; |
1058 | 930 | dest_segment_id++) { |
1059 | 930 | auto res = inverted_index_file_writers[dest_segment_id]->open(index_meta); |
1060 | 930 | DBUG_EXECUTE_IF("Compaction::open_inverted_index_file_writer", { |
1061 | 930 | res = ResultError(Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( |
1062 | 930 | "debug point: Compaction::open_inverted_index_file_writer error")); |
1063 | 930 | }) |
1064 | 930 | if (!res.has_value()) { |
1065 | 0 | LOG(WARNING) << "failed to do index compaction, open inverted index file " |
1066 | 0 | "writer failed" |
1067 | 0 | << ". tablet=" << _tablet->tablet_id() |
1068 | 0 | << ", column uniq id=" << column_uniq_id |
1069 | 0 | << ", dest_segment_id=" << dest_segment_id; |
1070 | 0 | throw Exception(ErrorCode::INVERTED_INDEX_COMPACTION_ERROR, |
1071 | 0 | res.error().msg()); |
1072 | 0 | } |
1073 | | // Destination directories in dest_index_dirs do not need to be deconstructed, |
1074 | | // but their lifecycle must be managed by inverted_index_file_writers. |
1075 | 930 | dest_index_dirs[dest_segment_id] = res.value().get(); |
1076 | 930 | } |
1077 | 819 | auto st = compact_column(index_meta->index_id(), src_idx_dirs, dest_index_dirs, |
1078 | 819 | index_tmp_path.native(), trans_vec, dest_segment_num_rows); |
1079 | 819 | if (!st.ok()) { |
1080 | 2 | error_handler(index_meta->index_id(), column_uniq_id); |
1081 | 2 | status = Status::Error<INVERTED_INDEX_COMPACTION_ERROR>(st.msg()); |
1082 | 2 | } |
1083 | 819 | } catch (CLuceneError& e) { |
1084 | 0 | error_handler(index_meta->index_id(), column_uniq_id); |
1085 | 0 | status = Status::Error<INVERTED_INDEX_COMPACTION_ERROR>(e.what()); |
1086 | 0 | } catch (const Exception& e) { |
1087 | 0 | error_handler(index_meta->index_id(), column_uniq_id); |
1088 | 0 | status = Status::Error<INVERTED_INDEX_COMPACTION_ERROR>(e.what()); |
1089 | 0 | } |
1090 | 819 | } |
1091 | 813 | } |
1092 | | |
1093 | | // check index compaction status. If status is not ok, we should return error and end this compaction round. |
1094 | 210 | if (!status.ok()) { |
1095 | 1 | return status; |
1096 | 1 | } |
1097 | 210 | LOG(INFO) << "succeed to do index compaction" |
1098 | 209 | << ". tablet=" << _tablet->tablet_id() |
1099 | 209 | << ". elapsed time=" << inverted_watch.get_elapse_second() << "s."; |
1100 | | |
1101 | 209 | return Status::OK(); |
1102 | 210 | } |
1103 | | |
1104 | | void Compaction::mark_skip_index_compaction( |
1105 | | const RowsetWriterContext& context, |
1106 | 0 | const std::function<void(int64_t, int64_t)>& error_handler) { |
1107 | 0 | for (auto&& column_uniq_id : context.columns_to_do_index_compaction) { |
1108 | 0 | auto col = _cur_tablet_schema->column_by_uid(column_uniq_id); |
1109 | 0 | auto index_metas = _cur_tablet_schema->inverted_indexs(col); |
1110 | 0 | DBUG_EXECUTE_IF("Compaction::mark_skip_index_compaction_can_not_find_index_meta", |
1111 | 0 | { index_metas.clear(); }) |
1112 | 0 | if (index_metas.empty()) { |
1113 | 0 | LOG(WARNING) << "mark skip index compaction, can not find index_meta for column" |
1114 | 0 | << ". tablet=" << _tablet->tablet_id() |
1115 | 0 | << ", column uniq id=" << column_uniq_id; |
1116 | 0 | error_handler(-1, column_uniq_id); |
1117 | 0 | continue; |
1118 | 0 | } |
1119 | 0 | for (const auto& index_meta : index_metas) { |
1120 | 0 | error_handler(index_meta->index_id(), column_uniq_id); |
1121 | 0 | } |
1122 | 0 | } |
1123 | 0 | } |
1124 | | |
1125 | | static bool check_rowset_has_inverted_index(const RowsetSharedPtr& src_rs, int32_t col_unique_id, |
1126 | | const BaseTabletSPtr& tablet, |
1127 | 10.2k | const TabletSchemaSPtr& cur_tablet_schema) { |
1128 | 10.2k | auto* rowset = static_cast<BetaRowset*>(src_rs.get()); |
1129 | 10.2k | DBUG_EXECUTE_IF("Compaction::construct_skip_inverted_index_is_skip_index_compaction", |
1130 | 10.2k | { rowset->set_skip_index_compaction(col_unique_id); }) |
1131 | 10.2k | if (rowset->is_skip_index_compaction(col_unique_id)) { |
1132 | 1 | LOG(WARNING) << "tablet[" << tablet->tablet_id() << "] rowset[" << rowset->rowset_id() |
1133 | 1 | << "] column_unique_id[" << col_unique_id |
1134 | 1 | << "] skip inverted index compaction due to last failure"; |
1135 | 1 | return false; |
1136 | 1 | } |
1137 | | |
1138 | 10.2k | auto fs = rowset->rowset_meta()->fs(); |
1139 | 10.2k | DBUG_EXECUTE_IF("Compaction::construct_skip_inverted_index_get_fs_error", { fs = nullptr; }) |
1140 | 10.2k | if (!fs) { |
1141 | 471 | LOG(WARNING) << "get fs failed, resource_id=" << rowset->rowset_meta()->resource_id(); |
1142 | 471 | return false; |
1143 | 471 | } |
1144 | | |
1145 | 9.73k | auto index_metas = rowset->tablet_schema()->inverted_indexs(col_unique_id); |
1146 | 9.73k | DBUG_EXECUTE_IF("Compaction::construct_skip_inverted_index_index_meta_nullptr", |
1147 | 9.73k | { index_metas.clear(); }) |
1148 | 9.73k | if (index_metas.empty()) { |
1149 | 0 | LOG(WARNING) << "tablet[" << tablet->tablet_id() << "] column_unique_id[" << col_unique_id |
1150 | 0 | << "] index meta is null, will skip index compaction"; |
1151 | 0 | return false; |
1152 | 0 | } |
1153 | 9.94k | for (const auto& index_meta : index_metas) { |
1154 | 13.1k | for (auto i = 0; i < rowset->num_segments(); i++) { |
1155 | | // TODO: inverted_index_path |
1156 | 3.23k | auto seg_path = rowset->segment_path(i); |
1157 | 3.23k | DBUG_EXECUTE_IF("Compaction::construct_skip_inverted_index_seg_path_nullptr", { |
1158 | 3.23k | seg_path = ResultError(Status::Error<ErrorCode::INTERNAL_ERROR>( |
1159 | 3.23k | "construct_skip_inverted_index_seg_path_nullptr")); |
1160 | 3.23k | }) |
1161 | 3.23k | if (!seg_path) { |
1162 | 0 | LOG(WARNING) << seg_path.error(); |
1163 | 0 | return false; |
1164 | 0 | } |
1165 | | |
1166 | 3.23k | std::string index_file_path; |
1167 | 3.23k | try { |
1168 | 3.23k | auto index_file_reader = std::make_unique<IndexFileReader>( |
1169 | 3.23k | fs, |
1170 | 3.23k | std::string {InvertedIndexDescriptor::get_index_file_path_prefix( |
1171 | 3.23k | seg_path.value())}, |
1172 | 3.23k | cur_tablet_schema->get_inverted_index_storage_format(), |
1173 | 3.23k | rowset->rowset_meta()->inverted_index_file_info(i), tablet->tablet_id()); |
1174 | 3.23k | auto st = index_file_reader->init(config::inverted_index_read_buffer_size); |
1175 | 3.23k | index_file_path = index_file_reader->get_index_file_path(index_meta); |
1176 | 3.23k | DBUG_EXECUTE_IF( |
1177 | 3.23k | "Compaction::construct_skip_inverted_index_index_file_reader_init_" |
1178 | 3.23k | "status_not_ok", |
1179 | 3.23k | { |
1180 | 3.23k | st = Status::Error<ErrorCode::INTERNAL_ERROR>( |
1181 | 3.23k | "debug point: " |
1182 | 3.23k | "construct_skip_inverted_index_index_file_reader_init_" |
1183 | 3.23k | "status_" |
1184 | 3.23k | "not_ok"); |
1185 | 3.23k | }) |
1186 | 3.23k | if (!st.ok()) { |
1187 | 0 | LOG(WARNING) << "init index " << index_file_path << " error:" << st; |
1188 | 0 | return false; |
1189 | 0 | } |
1190 | | |
1191 | | // check index meta |
1192 | 3.23k | auto result = index_file_reader->open(index_meta); |
1193 | 3.23k | DBUG_EXECUTE_IF( |
1194 | 3.23k | "Compaction::construct_skip_inverted_index_index_file_reader_open_" |
1195 | 3.23k | "error", |
1196 | 3.23k | { |
1197 | 3.23k | result = ResultError( |
1198 | 3.23k | Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( |
1199 | 3.23k | "CLuceneError occur when open idx file")); |
1200 | 3.23k | }) |
1201 | 3.23k | if (!result.has_value()) { |
1202 | 0 | LOG(WARNING) << "open index " << index_file_path << " error:" << result.error(); |
1203 | 0 | return false; |
1204 | 0 | } |
1205 | 3.23k | auto reader = std::move(result.value()); |
1206 | 3.23k | std::vector<std::string> files; |
1207 | 3.23k | reader->list(&files); |
1208 | 3.23k | reader->close(); |
1209 | 3.23k | DBUG_EXECUTE_IF( |
1210 | 3.23k | "Compaction::construct_skip_inverted_index_index_reader_close_" |
1211 | 3.23k | "error", |
1212 | 3.23k | { _CLTHROWA(CL_ERR_IO, "debug point: reader close error"); }) |
1213 | | |
1214 | 3.23k | DBUG_EXECUTE_IF("Compaction::construct_skip_inverted_index_index_files_count", |
1215 | 3.23k | { files.clear(); }) |
1216 | | |
1217 | | // why is 3? |
1218 | | // slice type index file at least has 3 files: null_bitmap, segments_N, segments.gen |
1219 | 3.23k | if (files.size() < 3) { |
1220 | 0 | LOG(WARNING) << "tablet[" << tablet->tablet_id() << "] column_unique_id[" |
1221 | 0 | << col_unique_id << "]," << index_file_path |
1222 | 0 | << " is corrupted, will skip index compaction"; |
1223 | 0 | return false; |
1224 | 0 | } |
1225 | 3.23k | } catch (CLuceneError& err) { |
1226 | 0 | LOG(WARNING) << "tablet[" << tablet->tablet_id() << "] column_unique_id[" |
1227 | 0 | << col_unique_id << "] open index[" << index_file_path |
1228 | 0 | << "], will skip index compaction, error:" << err.what(); |
1229 | 0 | return false; |
1230 | 0 | } |
1231 | 3.23k | } |
1232 | 9.94k | } |
1233 | 9.74k | return true; |
1234 | 9.73k | } |
1235 | | |
1236 | 8.22k | void Compaction::construct_index_compaction_columns(RowsetWriterContext& ctx) { |
1237 | 8.22k | for (const auto& index : _cur_tablet_schema->inverted_indexes()) { |
1238 | 4.38k | auto col_unique_ids = index->col_unique_ids(); |
1239 | | // check if column unique ids is empty to avoid crash |
1240 | 4.38k | if (col_unique_ids.empty()) { |
1241 | 1 | LOG(WARNING) << "tablet[" << _tablet->tablet_id() << "] index[" << index->index_id() |
1242 | 1 | << "] has no column unique id, will skip index compaction." |
1243 | 1 | << " tablet_schema=" << _cur_tablet_schema->dump_full_schema(); |
1244 | 1 | continue; |
1245 | 1 | } |
1246 | 4.38k | auto col_unique_id = col_unique_ids[0]; |
1247 | 4.38k | if (!_cur_tablet_schema->has_column_unique_id(col_unique_id)) { |
1248 | 0 | LOG(WARNING) << "tablet[" << _tablet->tablet_id() << "] column_unique_id[" |
1249 | 0 | << col_unique_id << "] not found, will skip index compaction"; |
1250 | 0 | continue; |
1251 | 0 | } |
1252 | | // Avoid doing inverted index compaction on non-slice type columns |
1253 | 4.38k | if (!field_is_slice_type(_cur_tablet_schema->column_by_uid(col_unique_id).type())) { |
1254 | 2.43k | continue; |
1255 | 2.43k | } |
1256 | | |
1257 | | // if index properties are different, index compaction maybe needs to be skipped. |
1258 | 1.94k | bool is_continue = false; |
1259 | 1.94k | std::optional<std::map<std::string, std::string>> first_properties; |
1260 | 13.1k | for (const auto& rowset : _input_rowsets) { |
1261 | 13.1k | auto tablet_indexs = rowset->tablet_schema()->inverted_indexs(col_unique_id); |
1262 | | // no inverted index or index id is different from current index id |
1263 | 13.1k | auto it = std::find_if(tablet_indexs.begin(), tablet_indexs.end(), |
1264 | 13.2k | [&index](const auto& tablet_index) { |
1265 | 13.2k | return tablet_index->index_id() == index->index_id(); |
1266 | 13.2k | }); |
1267 | 13.1k | if (it != tablet_indexs.end()) { |
1268 | 13.1k | const auto* tablet_index = *it; |
1269 | 13.1k | auto properties = tablet_index->properties(); |
1270 | 13.1k | if (!first_properties.has_value()) { |
1271 | 1.94k | first_properties = properties; |
1272 | 11.1k | } else { |
1273 | 11.1k | DBUG_EXECUTE_IF( |
1274 | 11.1k | "Compaction::do_inverted_index_compaction_index_properties_different", |
1275 | 11.1k | { properties.emplace("dummy_key", "dummy_value"); }) |
1276 | 11.1k | if (properties != first_properties.value()) { |
1277 | 3 | is_continue = true; |
1278 | 3 | break; |
1279 | 3 | } |
1280 | 11.1k | } |
1281 | 18.4E | } else { |
1282 | 18.4E | is_continue = true; |
1283 | 18.4E | break; |
1284 | 18.4E | } |
1285 | 13.1k | } |
1286 | 1.94k | if (is_continue) { |
1287 | 5 | continue; |
1288 | 5 | } |
1289 | 1.93k | bool all_have_inverted_index = |
1290 | 1.93k | std::all_of(_input_rowsets.begin(), _input_rowsets.end(), |
1291 | 10.2k | [this, col_unique_id](const RowsetSharedPtr& src_rs) { |
1292 | 10.2k | return check_rowset_has_inverted_index(src_rs, col_unique_id, |
1293 | 10.2k | _tablet, _cur_tablet_schema); |
1294 | 10.2k | }); |
1295 | | |
1296 | 1.93k | if (all_have_inverted_index) { |
1297 | 1.47k | ctx.columns_to_do_index_compaction.insert(col_unique_id); |
1298 | 1.47k | } |
1299 | 1.93k | } |
1300 | 8.22k | } |
1301 | | |
1302 | 0 | Status CompactionMixin::update_delete_bitmap() { |
1303 | | // for mow with cluster keys, compaction read data with delete bitmap |
1304 | | // if tablet is not ready(such as schema change), we need to update delete bitmap |
1305 | 0 | { |
1306 | 0 | std::shared_lock meta_rlock(_tablet->get_header_lock()); |
1307 | 0 | if (_tablet->tablet_state() != TABLET_NOTREADY) { |
1308 | 0 | return Status::OK(); |
1309 | 0 | } |
1310 | 0 | } |
1311 | 0 | OlapStopWatch watch; |
1312 | 0 | std::vector<RowsetSharedPtr> rowsets; |
1313 | 0 | for (const auto& rowset : _input_rowsets) { |
1314 | 0 | std::lock_guard rwlock(tablet()->get_rowset_update_lock()); |
1315 | 0 | std::shared_lock rlock(_tablet->get_header_lock()); |
1316 | 0 | Status st = _tablet->update_delete_bitmap_without_lock(_tablet, rowset, &rowsets); |
1317 | 0 | if (!st.ok()) { |
1318 | 0 | LOG(INFO) << "failed update_delete_bitmap_without_lock for tablet_id=" |
1319 | 0 | << _tablet->tablet_id() << ", st=" << st.to_string(); |
1320 | 0 | return st; |
1321 | 0 | } |
1322 | 0 | rowsets.push_back(rowset); |
1323 | 0 | } |
1324 | 0 | LOG(INFO) << "finish update delete bitmap for tablet: " << _tablet->tablet_id() |
1325 | 0 | << ", rowsets: " << _input_rowsets.size() << ", cost: " << watch.get_elapse_time_us() |
1326 | 0 | << "(us)"; |
1327 | 0 | return Status::OK(); |
1328 | 0 | } |
1329 | | |
1330 | 115 | Status CloudCompactionMixin::update_delete_bitmap() { |
1331 | | // for mow with cluster keys, compaction read data with delete bitmap |
1332 | | // if tablet is not ready(such as schema change), we need to update delete bitmap |
1333 | 115 | { |
1334 | 115 | std::shared_lock meta_rlock(_tablet->get_header_lock()); |
1335 | 115 | if (_tablet->tablet_state() != TABLET_NOTREADY) { |
1336 | 115 | return Status::OK(); |
1337 | 115 | } |
1338 | 115 | } |
1339 | 0 | OlapStopWatch watch; |
1340 | 0 | std::vector<RowsetSharedPtr> rowsets; |
1341 | 0 | for (const auto& rowset : _input_rowsets) { |
1342 | 0 | Status st = _tablet->update_delete_bitmap_without_lock(_tablet, rowset, &rowsets); |
1343 | 0 | if (!st.ok()) { |
1344 | 0 | LOG(INFO) << "failed update_delete_bitmap_without_lock for tablet_id=" |
1345 | 0 | << _tablet->tablet_id() << ", st=" << st.to_string(); |
1346 | 0 | return st; |
1347 | 0 | } |
1348 | 0 | rowsets.push_back(rowset); |
1349 | 0 | } |
1350 | 0 | LOG(INFO) << "finish update delete bitmap for tablet: " << _tablet->tablet_id() |
1351 | 0 | << ", rowsets: " << _input_rowsets.size() << ", cost: " << watch.get_elapse_time_us() |
1352 | 0 | << "(us)"; |
1353 | 0 | return Status::OK(); |
1354 | 0 | } |
1355 | | |
1356 | | void CompactionMixin::find_longest_consecutive_version(std::vector<RowsetSharedPtr>* rowsets, |
1357 | 93.2k | std::vector<Version>* missing_version) { |
1358 | 93.2k | if (rowsets->empty()) { |
1359 | 0 | return; |
1360 | 0 | } |
1361 | | |
1362 | 93.2k | RowsetSharedPtr prev_rowset = rowsets->front(); |
1363 | 93.2k | int max_start = 0; |
1364 | 93.2k | int max_length = 1; |
1365 | 93.2k | int start = 0; |
1366 | 93.2k | int length = 1; |
1367 | 369k | for (int i = 1; i < rowsets->size(); ++i) { |
1368 | 276k | RowsetSharedPtr rowset = (*rowsets)[i]; |
1369 | 276k | if (rowset->start_version() != prev_rowset->end_version() + 1) { |
1370 | 6 | if (missing_version != nullptr) { |
1371 | 6 | missing_version->push_back(prev_rowset->version()); |
1372 | 6 | missing_version->push_back(rowset->version()); |
1373 | 6 | } |
1374 | 6 | start = i; |
1375 | 6 | length = 1; |
1376 | 276k | } else { |
1377 | 276k | ++length; |
1378 | 276k | } |
1379 | | |
1380 | 276k | if (length > max_length) { |
1381 | 276k | max_start = start; |
1382 | 276k | max_length = length; |
1383 | 276k | } |
1384 | | |
1385 | 276k | prev_rowset = rowset; |
1386 | 276k | } |
1387 | 93.2k | *rowsets = {rowsets->begin() + max_start, rowsets->begin() + max_start + max_length}; |
1388 | 93.2k | } |
1389 | | |
1390 | 137 | Status CompactionMixin::construct_output_rowset_writer(RowsetWriterContext& ctx) { |
1391 | | // only do index compaction for dup_keys and unique_keys with mow enabled |
1392 | 137 | if (_enable_inverted_index_compaction && (((_tablet->keys_type() == KeysType::UNIQUE_KEYS && |
1393 | 85 | _tablet->enable_unique_key_merge_on_write()) || |
1394 | 85 | _tablet->keys_type() == KeysType::DUP_KEYS))) { |
1395 | 79 | construct_index_compaction_columns(ctx); |
1396 | 79 | } |
1397 | 137 | if (_tablet->is_row_binlog_tablet()) { |
1398 | 0 | ctx.write_binlog_opt().enable = true; |
1399 | 0 | } |
1400 | 137 | ctx.version = _output_version; |
1401 | 137 | ctx.rowset_state = VISIBLE; |
1402 | 137 | ctx.segments_overlap = _trigger_quick_merge_by_binlog ? OVERLAPPING : NONOVERLAPPING; |
1403 | 137 | ctx.tablet_schema = _cur_tablet_schema; |
1404 | 137 | ctx.newest_write_timestamp = _newest_write_timestamp; |
1405 | 137 | ctx.write_type = DataWriteType::TYPE_COMPACTION; |
1406 | 137 | ctx.compaction_type = compaction_type(); |
1407 | 137 | ctx.allow_packed_file = false; |
1408 | 137 | _output_rs_writer = DORIS_TRY(_tablet->create_rowset_writer(ctx, _is_vertical)); |
1409 | 137 | _pending_rs_guard = _engine.add_pending_rowset(ctx); |
1410 | 137 | return Status::OK(); |
1411 | 137 | } |
1412 | | |
1413 | 93 | Status CompactionMixin::modify_rowsets() { |
1414 | 93 | std::vector<RowsetSharedPtr> output_rowsets; |
1415 | 93 | output_rowsets.push_back(_output_rowset); |
1416 | | |
1417 | 93 | if (_tablet->keys_type() == KeysType::UNIQUE_KEYS && |
1418 | 93 | _tablet->enable_unique_key_merge_on_write()) { |
1419 | 54 | Version version = tablet()->max_version(); |
1420 | 54 | DeleteBitmap output_rowset_delete_bitmap(_tablet->tablet_id()); |
1421 | 54 | std::unique_ptr<RowLocationSet> missed_rows; |
1422 | 54 | if ((config::enable_missing_rows_correctness_check || |
1423 | 54 | config::enable_mow_compaction_correctness_check_core || |
1424 | 54 | config::enable_mow_compaction_correctness_check_fail) && |
1425 | 54 | !_allow_delete_in_cumu_compaction && |
1426 | 54 | compaction_type() == ReaderType::READER_CUMULATIVE_COMPACTION) { |
1427 | 54 | missed_rows = std::make_unique<RowLocationSet>(); |
1428 | 54 | LOG(INFO) << "RowLocation Set inited succ for tablet:" << _tablet->tablet_id(); |
1429 | 54 | } |
1430 | 54 | std::unique_ptr<std::map<RowsetSharedPtr, RowLocationPairList>> location_map; |
1431 | 54 | if (config::enable_rowid_conversion_correctness_check && |
1432 | 54 | tablet()->tablet_schema()->cluster_key_uids().empty()) { |
1433 | 0 | location_map = std::make_unique<std::map<RowsetSharedPtr, RowLocationPairList>>(); |
1434 | 0 | LOG(INFO) << "Location Map inited succ for tablet:" << _tablet->tablet_id(); |
1435 | 0 | } |
1436 | | // Convert the delete bitmap of the input rowsets to output rowset. |
1437 | | // New loads are not blocked, so some keys of input rowsets might |
1438 | | // be deleted during the time. We need to deal with delete bitmap |
1439 | | // of incremental data later. |
1440 | | // TODO(LiaoXin): check if there are duplicate keys |
1441 | 54 | std::size_t missed_rows_size = 0; |
1442 | 54 | tablet()->calc_compaction_output_rowset_delete_bitmap( |
1443 | 54 | _input_rowsets, *_rowid_conversion, 0, version.second + 1, missed_rows.get(), |
1444 | 54 | location_map.get(), _tablet->tablet_meta()->delete_bitmap(), |
1445 | 54 | &output_rowset_delete_bitmap); |
1446 | 54 | if (missed_rows) { |
1447 | 54 | missed_rows_size = missed_rows->size(); |
1448 | 54 | std::size_t merged_missed_rows_size = _stats.merged_rows; |
1449 | 54 | if (!_tablet->tablet_meta()->tablet_schema()->cluster_key_uids().empty()) { |
1450 | 0 | merged_missed_rows_size += _stats.filtered_rows; |
1451 | 0 | } |
1452 | | |
1453 | | // Suppose a heavy schema change process on BE converting tablet A to tablet B. |
1454 | | // 1. during schema change double write, new loads write [X-Y] on tablet B. |
1455 | | // 2. rowsets with version [a],[a+1],...,[b-1],[b] on tablet B are picked for cumu compaction(X<=a<b<=Y).(cumu compaction |
1456 | | // on new tablet during schema change double write is allowed after https://github.com/apache/doris/pull/16470) |
1457 | | // 3. schema change remove all rowsets on tablet B before version Z(b<=Z<=Y) before it begins to convert historical rowsets. |
1458 | | // 4. schema change finishes. |
1459 | | // 5. cumu compation begins on new tablet with version [a],...,[b]. If there are duplicate keys between these rowsets, |
1460 | | // the compaction check will fail because these rowsets have skipped to calculate delete bitmap in commit phase and |
1461 | | // publish phase because tablet B is in NOT_READY state when writing. |
1462 | | |
1463 | | // Considering that the cumu compaction will fail finally in this situation because `Tablet::modify_rowsets` will check if rowsets in |
1464 | | // `to_delete`(_input_rowsets) still exist in tablet's `_rs_version_map`, we can just skip to check missed rows here. |
1465 | 54 | bool need_to_check_missed_rows = true; |
1466 | 54 | { |
1467 | 54 | std::shared_lock rlock(_tablet->get_header_lock()); |
1468 | 54 | need_to_check_missed_rows = |
1469 | 54 | std::all_of(_input_rowsets.begin(), _input_rowsets.end(), |
1470 | 632 | [&](const RowsetSharedPtr& rowset) { |
1471 | 632 | return tablet()->rowset_exists_unlocked(rowset); |
1472 | 632 | }); |
1473 | 54 | } |
1474 | | |
1475 | 54 | if (_tablet->tablet_state() == TABLET_RUNNING && |
1476 | 54 | merged_missed_rows_size != missed_rows_size && need_to_check_missed_rows) { |
1477 | 0 | std::stringstream ss; |
1478 | 0 | ss << "cumulative compaction: the merged rows(" << _stats.merged_rows |
1479 | 0 | << "), filtered rows(" << _stats.filtered_rows |
1480 | 0 | << ") is not equal to missed rows(" << missed_rows_size |
1481 | 0 | << ") in rowid conversion, tablet_id: " << _tablet->tablet_id() |
1482 | 0 | << ", table_id:" << _tablet->table_id(); |
1483 | 0 | if (missed_rows_size == 0) { |
1484 | 0 | ss << ", debug info: "; |
1485 | 0 | DeleteBitmap subset_map(_tablet->tablet_id()); |
1486 | 0 | for (auto rs : _input_rowsets) { |
1487 | 0 | _tablet->tablet_meta()->delete_bitmap().subset( |
1488 | 0 | {rs->rowset_id(), 0, 0}, |
1489 | 0 | {rs->rowset_id(), rs->num_segments(), version.second + 1}, |
1490 | 0 | &subset_map); |
1491 | 0 | ss << "(rowset id: " << rs->rowset_id() |
1492 | 0 | << ", delete bitmap cardinality: " << subset_map.cardinality() << ")"; |
1493 | 0 | } |
1494 | 0 | ss << ", version[0-" << version.second + 1 << "]"; |
1495 | 0 | } |
1496 | 0 | std::string err_msg = fmt::format( |
1497 | 0 | "cumulative compaction: the merged rows({}), filtered rows({})" |
1498 | 0 | " is not equal to missed rows({}) in rowid conversion," |
1499 | 0 | " tablet_id: {}, table_id:{}", |
1500 | 0 | _stats.merged_rows, _stats.filtered_rows, missed_rows_size, |
1501 | 0 | _tablet->tablet_id(), _tablet->table_id()); |
1502 | 0 | LOG(WARNING) << err_msg; |
1503 | 0 | if (config::enable_mow_compaction_correctness_check_core) { |
1504 | 0 | CHECK(false) << err_msg; |
1505 | 0 | } else if (config::enable_mow_compaction_correctness_check_fail) { |
1506 | 0 | return Status::InternalError<false>(err_msg); |
1507 | 0 | } else { |
1508 | 0 | DCHECK(false) << err_msg; |
1509 | 0 | } |
1510 | 0 | } |
1511 | 54 | } |
1512 | | |
1513 | 54 | if (location_map) { |
1514 | 0 | RETURN_IF_ERROR(tablet()->check_rowid_conversion(_output_rowset, *location_map)); |
1515 | 0 | location_map->clear(); |
1516 | 0 | } |
1517 | | |
1518 | 54 | { |
1519 | 54 | std::lock_guard<std::mutex> wrlock_(tablet()->get_rowset_update_lock()); |
1520 | 54 | std::lock_guard wrlock(_tablet->get_header_lock()); |
1521 | 54 | SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD); |
1522 | | |
1523 | | // Here we will calculate all the rowsets delete bitmaps which are committed but not published to reduce the calculation pressure |
1524 | | // of publish phase. |
1525 | | // All rowsets which need to recalculate have been published so we don't need to acquire lock. |
1526 | | // Step1: collect this tablet's all committed rowsets' delete bitmaps |
1527 | 54 | CommitTabletTxnInfoVec commit_tablet_txn_info_vec {}; |
1528 | 54 | _engine.txn_manager()->get_all_commit_tablet_txn_info_by_tablet( |
1529 | 54 | *tablet(), &commit_tablet_txn_info_vec); |
1530 | | |
1531 | | // Step2: calculate all rowsets' delete bitmaps which are published during compaction. |
1532 | 54 | for (auto& it : commit_tablet_txn_info_vec) { |
1533 | 0 | if (!_check_if_includes_input_rowsets(it.rowset_ids)) { |
1534 | | // When calculating the delete bitmap of all committed rowsets relative to the compaction, |
1535 | | // there may be cases where the compacted rowsets are newer than the committed rowsets. |
1536 | | // At this time, row number conversion cannot be performed, otherwise data will be missing. |
1537 | | // Therefore, we need to check if every committed rowset has calculated delete bitmap for |
1538 | | // all compaction input rowsets. |
1539 | 0 | continue; |
1540 | 0 | } |
1541 | 0 | DeleteBitmap txn_output_delete_bitmap(_tablet->tablet_id()); |
1542 | 0 | tablet()->calc_compaction_output_rowset_delete_bitmap( |
1543 | 0 | _input_rowsets, *_rowid_conversion, 0, UINT64_MAX, missed_rows.get(), |
1544 | 0 | location_map.get(), *it.delete_bitmap.get(), &txn_output_delete_bitmap); |
1545 | 0 | if (config::enable_merge_on_write_correctness_check) { |
1546 | 0 | RowsetIdUnorderedSet rowsetids; |
1547 | 0 | rowsetids.insert(_output_rowset->rowset_id()); |
1548 | 0 | _tablet->add_sentinel_mark_to_delete_bitmap(&txn_output_delete_bitmap, |
1549 | 0 | rowsetids); |
1550 | 0 | } |
1551 | 0 | it.delete_bitmap->merge(txn_output_delete_bitmap); |
1552 | | // Step3: write back updated delete bitmap and tablet info. |
1553 | 0 | it.rowset_ids.insert(_output_rowset->rowset_id()); |
1554 | 0 | _engine.txn_manager()->set_txn_related_delete_bitmap( |
1555 | 0 | it.partition_id, it.transaction_id, _tablet->tablet_id(), |
1556 | 0 | tablet()->tablet_uid(), true, it.delete_bitmap, it.rowset_ids, |
1557 | 0 | it.partial_update_info); |
1558 | 0 | } |
1559 | | |
1560 | | // Convert the delete bitmap of the input rowsets to output rowset for |
1561 | | // incremental data. |
1562 | 54 | tablet()->calc_compaction_output_rowset_delete_bitmap( |
1563 | 54 | _input_rowsets, *_rowid_conversion, version.second, UINT64_MAX, |
1564 | 54 | missed_rows.get(), location_map.get(), _tablet->tablet_meta()->delete_bitmap(), |
1565 | 54 | &output_rowset_delete_bitmap); |
1566 | | |
1567 | 54 | if (location_map) { |
1568 | 0 | RETURN_IF_ERROR(tablet()->check_rowid_conversion(_output_rowset, *location_map)); |
1569 | 0 | } |
1570 | | |
1571 | 54 | tablet()->merge_delete_bitmap(output_rowset_delete_bitmap); |
1572 | 54 | RETURN_IF_ERROR(tablet()->modify_rowsets(output_rowsets, _input_rowsets, true)); |
1573 | 54 | } |
1574 | 54 | } else { |
1575 | 39 | std::lock_guard wrlock(_tablet->get_header_lock()); |
1576 | 39 | SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD); |
1577 | 39 | RETURN_IF_ERROR(tablet()->modify_rowsets(output_rowsets, _input_rowsets, true)); |
1578 | 39 | } |
1579 | | |
1580 | 93 | if (config::tablet_rowset_stale_sweep_by_size && |
1581 | 93 | _tablet->tablet_meta()->all_stale_rs_metas().size() >= |
1582 | 0 | config::tablet_rowset_stale_sweep_threshold_size) { |
1583 | 0 | tablet()->delete_expired_stale_rowset(); |
1584 | 0 | } |
1585 | | |
1586 | 93 | int64_t cur_max_version = 0; |
1587 | 93 | { |
1588 | 93 | std::shared_lock rlock(_tablet->get_header_lock()); |
1589 | 93 | cur_max_version = _tablet->max_version_unlocked(); |
1590 | 93 | tablet()->save_meta(); |
1591 | 93 | } |
1592 | 93 | if (_tablet->keys_type() == KeysType::UNIQUE_KEYS && |
1593 | 93 | _tablet->enable_unique_key_merge_on_write()) { |
1594 | 54 | auto st = TabletMetaManager::remove_old_version_delete_bitmap( |
1595 | 54 | tablet()->data_dir(), _tablet->tablet_id(), cur_max_version); |
1596 | 54 | if (!st.ok()) { |
1597 | 0 | LOG(WARNING) << "failed to remove old version delete bitmap, st: " << st; |
1598 | 0 | } |
1599 | 54 | } |
1600 | 93 | DBUG_EXECUTE_IF("CumulativeCompaction.modify_rowsets.delete_expired_stale_rowset", |
1601 | 93 | { tablet()->delete_expired_stale_rowset(); }); |
1602 | 93 | _tablet->prefill_dbm_agg_cache_after_compaction(_output_rowset); |
1603 | 93 | return Status::OK(); |
1604 | 93 | } |
1605 | | |
1606 | | bool CompactionMixin::_check_if_includes_input_rowsets( |
1607 | 0 | const RowsetIdUnorderedSet& commit_rowset_ids_set) const { |
1608 | 0 | std::vector<RowsetId> commit_rowset_ids {}; |
1609 | 0 | commit_rowset_ids.insert(commit_rowset_ids.end(), commit_rowset_ids_set.begin(), |
1610 | 0 | commit_rowset_ids_set.end()); |
1611 | 0 | std::sort(commit_rowset_ids.begin(), commit_rowset_ids.end()); |
1612 | 0 | std::vector<RowsetId> input_rowset_ids {}; |
1613 | 0 | for (const auto& rowset : _input_rowsets) { |
1614 | 0 | input_rowset_ids.emplace_back(rowset->rowset_meta()->rowset_id()); |
1615 | 0 | } |
1616 | 0 | std::sort(input_rowset_ids.begin(), input_rowset_ids.end()); |
1617 | 0 | return std::includes(commit_rowset_ids.begin(), commit_rowset_ids.end(), |
1618 | 0 | input_rowset_ids.begin(), input_rowset_ids.end()); |
1619 | 0 | } |
1620 | | |
1621 | 81 | void CompactionMixin::update_compaction_level() { |
1622 | 81 | auto* cumu_policy = tablet()->cumulative_compaction_policy(); |
1623 | 81 | if (cumu_policy && (cumu_policy->name() == CUMULATIVE_TIME_SERIES_POLICY || |
1624 | 81 | cumu_policy->name() == CUMULATIVE_BINLOG_POLICY)) { |
1625 | 0 | int64_t compaction_level = |
1626 | 0 | cumu_policy->get_compaction_level(tablet(), _input_rowsets, _output_rowset); |
1627 | 0 | _output_rowset->rowset_meta()->set_compaction_level(compaction_level); |
1628 | 0 | } |
1629 | 81 | } |
1630 | | |
1631 | 10.0k | Status Compaction::check_correctness() { |
1632 | | // 1. check row number |
1633 | 10.0k | if (_input_row_num != _output_rowset->num_rows() + _stats.merged_rows + _stats.filtered_rows) { |
1634 | 0 | return Status::Error<CHECK_LINES_ERROR>( |
1635 | 0 | "row_num does not match between cumulative input and output! tablet={}, " |
1636 | 0 | "input_row_num={}, merged_row_num={}, filtered_row_num={}, output_row_num={}", |
1637 | 0 | _tablet->tablet_id(), _input_row_num, _stats.merged_rows, _stats.filtered_rows, |
1638 | 0 | _output_rowset->num_rows()); |
1639 | 0 | } |
1640 | | // 2. check variant column path stats |
1641 | 10.0k | RETURN_IF_ERROR(variant_util::VariantCompactionUtil::check_path_stats(_input_rowsets, |
1642 | 10.0k | _output_rowset, _tablet)); |
1643 | 10.0k | return Status::OK(); |
1644 | 10.0k | } |
1645 | | |
1646 | 177 | int64_t CompactionMixin::get_compaction_permits() { |
1647 | 177 | int64_t permits = 0; |
1648 | 177 | const int64_t point = tablet()->cumulative_layer_point(); |
1649 | 1.97k | for (auto&& rowset : _input_rowsets) { |
1650 | 1.97k | if (tablet()->is_row_binlog_tablet() && point != Tablet::K_INVALID_CUMULATIVE_POINT && |
1651 | 1.97k | rowset->end_version() < point) { |
1652 | 0 | ++permits; |
1653 | 0 | continue; |
1654 | 0 | } |
1655 | 1.97k | permits += rowset->rowset_meta()->get_compaction_score(); |
1656 | 1.97k | } |
1657 | 177 | return permits; |
1658 | 177 | } |
1659 | | |
1660 | 6 | int64_t CompactionMixin::calc_input_rowsets_total_size() const { |
1661 | 6 | int64_t input_rowsets_total_size = 0; |
1662 | 12 | for (const auto& rowset : _input_rowsets) { |
1663 | 12 | const auto& rowset_meta = rowset->rowset_meta(); |
1664 | 12 | auto total_size = rowset_meta->total_disk_size(); |
1665 | 12 | input_rowsets_total_size += total_size; |
1666 | 12 | } |
1667 | 6 | return input_rowsets_total_size; |
1668 | 6 | } |
1669 | | |
1670 | 6 | int64_t CompactionMixin::calc_input_rowsets_row_num() const { |
1671 | 6 | int64_t input_rowsets_row_num = 0; |
1672 | 12 | for (const auto& rowset : _input_rowsets) { |
1673 | 12 | const auto& rowset_meta = rowset->rowset_meta(); |
1674 | 12 | auto total_size = rowset_meta->total_disk_size(); |
1675 | 12 | input_rowsets_row_num += total_size; |
1676 | 12 | } |
1677 | 6 | return input_rowsets_row_num; |
1678 | 6 | } |
1679 | | |
1680 | 9.94k | void Compaction::_load_segment_to_cache() { |
1681 | | // Load new rowset's segments to cache. |
1682 | 9.94k | SegmentCacheHandle handle; |
1683 | 9.94k | auto st = SegmentLoader::instance()->load_segments( |
1684 | 9.94k | std::static_pointer_cast<BetaRowset>(_output_rowset), &handle, true); |
1685 | 9.94k | if (!st.ok()) { |
1686 | 0 | LOG(WARNING) << "failed to load segment to cache! output rowset version=" |
1687 | 0 | << _output_rowset->start_version() << "-" << _output_rowset->end_version() |
1688 | 0 | << "."; |
1689 | 0 | } |
1690 | 9.94k | } |
1691 | | |
1692 | 9.90k | Status CloudCompactionMixin::build_basic_info() { |
1693 | 9.90k | _output_version = |
1694 | 9.90k | Version(_input_rowsets.front()->start_version(), _input_rowsets.back()->end_version()); |
1695 | | |
1696 | 9.90k | _newest_write_timestamp = _input_rowsets.back()->newest_write_timestamp(); |
1697 | | |
1698 | 9.90k | std::vector<RowsetMetaSharedPtr> rowset_metas(_input_rowsets.size()); |
1699 | 9.90k | std::transform(_input_rowsets.begin(), _input_rowsets.end(), rowset_metas.begin(), |
1700 | 71.6k | [](const RowsetSharedPtr& rowset) { return rowset->rowset_meta(); }); |
1701 | 9.90k | if (is_index_change_compaction()) { |
1702 | 681 | RETURN_IF_ERROR(rebuild_tablet_schema()); |
1703 | 9.22k | } else { |
1704 | 9.22k | _cur_tablet_schema = _tablet->tablet_schema_with_merged_max_schema_version(rowset_metas); |
1705 | 9.22k | } |
1706 | | |
1707 | | // if enable_vertical_compact_variant_subcolumns is true, we need to compact the variant subcolumns in seperate column groups |
1708 | | // so get_extended_compaction_schema will extended the schema for variant columns |
1709 | 9.91k | if (_enable_vertical_compact_variant_subcolumns) { |
1710 | 9.91k | RETURN_IF_ERROR(variant_util::VariantCompactionUtil::get_extended_compaction_schema( |
1711 | 9.91k | _input_rowsets, _cur_tablet_schema)); |
1712 | 9.91k | } |
1713 | 9.90k | return Status::OK(); |
1714 | 9.90k | } |
1715 | | |
1716 | 9.89k | int64_t CloudCompactionMixin::get_compaction_permits() { |
1717 | 9.89k | int64_t permits = 0; |
1718 | 72.0k | for (auto&& rowset : _input_rowsets) { |
1719 | 72.0k | permits += rowset->rowset_meta()->get_compaction_score(); |
1720 | 72.0k | } |
1721 | 9.89k | return permits; |
1722 | 9.89k | } |
1723 | | |
1724 | | CloudCompactionMixin::CloudCompactionMixin(CloudStorageEngine& engine, CloudTabletSPtr tablet, |
1725 | | const std::string& label) |
1726 | 91.2k | : Compaction(tablet, label), _engine(engine) { |
1727 | 91.2k | auto uuid = UUIDGenerator::instance()->next_uuid(); |
1728 | 91.2k | std::stringstream ss; |
1729 | 91.2k | ss << uuid; |
1730 | 91.2k | _uuid = ss.str(); |
1731 | 91.2k | } |
1732 | | |
1733 | 9.95k | Status CloudCompactionMixin::execute_compact_impl(int64_t permits) { |
1734 | 9.95k | OlapStopWatch watch; |
1735 | | |
1736 | 9.95k | RETURN_IF_ERROR(build_basic_info()); |
1737 | | |
1738 | 9.95k | LOG(INFO) << "start " << compaction_name() << ". tablet=" << _tablet->tablet_id() |
1739 | 9.95k | << ", output_version=" << _output_version << ", permits: " << permits; |
1740 | | |
1741 | 9.95k | RETURN_IF_ERROR(merge_input_rowsets()); |
1742 | | |
1743 | 9.95k | DBUG_EXECUTE_IF("CloudFullCompaction::modify_rowsets.wrong_rowset_id", { |
1744 | 9.95k | DCHECK(compaction_type() == ReaderType::READER_FULL_COMPACTION); |
1745 | 9.95k | RowsetId id; |
1746 | 9.95k | id.version = 2; |
1747 | 9.95k | id.hi = _output_rowset->rowset_meta()->rowset_id().hi + ((int64_t)(1) << 56); |
1748 | 9.95k | id.mi = _output_rowset->rowset_meta()->rowset_id().mi; |
1749 | 9.95k | id.lo = _output_rowset->rowset_meta()->rowset_id().lo; |
1750 | 9.95k | _output_rowset->rowset_meta()->set_rowset_id(id); |
1751 | 9.95k | LOG(INFO) << "[Debug wrong rowset id]:" |
1752 | 9.95k | << _output_rowset->rowset_meta()->rowset_id().to_string(); |
1753 | 9.95k | }) |
1754 | | |
1755 | | // Currently, updates are only made in the time_series and binlog policies. |
1756 | 9.95k | update_compaction_level(); |
1757 | | |
1758 | 9.95k | RETURN_IF_ERROR(_engine.meta_mgr().commit_rowset(*_output_rowset->rowset_meta().get(), _uuid, |
1759 | 9.95k | _tablet->table_id())); |
1760 | | |
1761 | | // 4. modify rowsets in memory |
1762 | 9.95k | RETURN_IF_ERROR(modify_rowsets()); |
1763 | | |
1764 | | // update compaction status data |
1765 | 9.91k | auto tablet = std::static_pointer_cast<CloudTablet>(_tablet); |
1766 | 9.91k | tablet->local_read_time_us.fetch_add(_stats.cloud_local_read_time); |
1767 | 9.91k | tablet->remote_read_time_us.fetch_add(_stats.cloud_remote_read_time); |
1768 | 9.91k | tablet->exec_compaction_time_us.fetch_add(watch.get_elapse_time_us()); |
1769 | | |
1770 | 9.91k | return Status::OK(); |
1771 | 9.95k | } |
1772 | | |
1773 | 9.70k | int64_t CloudCompactionMixin::initiator() const { |
1774 | 9.70k | return HashUtil::hash64(_uuid.data(), _uuid.size(), 0) & std::numeric_limits<int64_t>::max(); |
1775 | 9.70k | } |
1776 | | |
1777 | | namespace cloud { |
1778 | | size_t truncate_rowsets_by_txn_size(std::vector<RowsetSharedPtr>& rowsets, int64_t& kept_size_bytes, |
1779 | 10.2k | int64_t& truncated_size_bytes) { |
1780 | 10.2k | if (rowsets.empty()) { |
1781 | 1 | kept_size_bytes = 0; |
1782 | 1 | truncated_size_bytes = 0; |
1783 | 1 | return 0; |
1784 | 1 | } |
1785 | | |
1786 | 10.2k | int64_t max_size = config::compaction_txn_max_size_bytes; |
1787 | 10.2k | int64_t cumulative_meta_size = 0; |
1788 | 10.2k | size_t keep_count = 0; |
1789 | | |
1790 | 85.0k | for (size_t i = 0; i < rowsets.size(); ++i) { |
1791 | 74.7k | const auto& rs = rowsets[i]; |
1792 | | |
1793 | | // Estimate rowset meta size using doris_rowset_meta_to_cloud |
1794 | 74.7k | auto cloud_meta = cloud::doris_rowset_meta_to_cloud(rs->rowset_meta()->get_rowset_pb(true)); |
1795 | 74.7k | int64_t rowset_meta_size = cloud_meta.ByteSizeLong(); |
1796 | | |
1797 | 74.7k | cumulative_meta_size += rowset_meta_size; |
1798 | | |
1799 | 74.7k | if (keep_count > 0 && cumulative_meta_size > max_size) { |
1800 | | // Rollback and stop |
1801 | 4 | cumulative_meta_size -= rowset_meta_size; |
1802 | 4 | break; |
1803 | 4 | } |
1804 | | |
1805 | 74.7k | keep_count++; |
1806 | 74.7k | } |
1807 | | |
1808 | | // Ensure at least 1 rowset is kept |
1809 | 10.2k | if (keep_count == 0) { |
1810 | 0 | keep_count = 1; |
1811 | | // Recalculate size for the first rowset |
1812 | 0 | const auto& rs = rowsets[0]; |
1813 | 0 | auto cloud_meta = cloud::doris_rowset_meta_to_cloud(rs->rowset_meta()->get_rowset_pb()); |
1814 | 0 | cumulative_meta_size = cloud_meta.ByteSizeLong(); |
1815 | 0 | } |
1816 | | |
1817 | | // Calculate truncated size |
1818 | 10.2k | int64_t truncated_total_size = 0; |
1819 | 10.2k | size_t truncated_count = rowsets.size() - keep_count; |
1820 | 10.2k | if (truncated_count > 0) { |
1821 | 35 | for (size_t i = keep_count; i < rowsets.size(); ++i) { |
1822 | 31 | auto cloud_meta = |
1823 | 31 | cloud::doris_rowset_meta_to_cloud(rowsets[i]->rowset_meta()->get_rowset_pb()); |
1824 | 31 | truncated_total_size += cloud_meta.ByteSizeLong(); |
1825 | 31 | } |
1826 | 4 | rowsets.resize(keep_count); |
1827 | 4 | } |
1828 | | |
1829 | 10.2k | kept_size_bytes = cumulative_meta_size; |
1830 | 10.2k | truncated_size_bytes = truncated_total_size; |
1831 | 10.2k | return truncated_count; |
1832 | 10.2k | } |
1833 | | } // namespace cloud |
1834 | | |
1835 | 9.50k | size_t CloudCompactionMixin::apply_txn_size_truncation_and_log(const std::string& compaction_name) { |
1836 | 9.50k | if (_input_rowsets.empty()) { |
1837 | 1 | return 0; |
1838 | 1 | } |
1839 | | |
1840 | 9.50k | int64_t original_count = _input_rowsets.size(); |
1841 | 9.50k | int64_t original_start_version = _input_rowsets.front()->start_version(); |
1842 | 9.50k | int64_t original_end_version = _input_rowsets.back()->end_version(); |
1843 | | |
1844 | 9.50k | int64_t final_size = 0; |
1845 | 9.50k | int64_t truncated_size = 0; |
1846 | 9.50k | size_t truncated_count = |
1847 | 9.50k | cloud::truncate_rowsets_by_txn_size(_input_rowsets, final_size, truncated_size); |
1848 | | |
1849 | 9.50k | if (truncated_count > 0) { |
1850 | 2 | int64_t original_size = final_size + truncated_size; |
1851 | 2 | LOG(INFO) << compaction_name << " txn size estimation truncate" |
1852 | 2 | << ", tablet_id=" << _tablet->tablet_id() << ", original_version_range=[" |
1853 | 2 | << original_start_version << "-" << original_end_version |
1854 | 2 | << "], final_version_range=[" << _input_rowsets.front()->start_version() << "-" |
1855 | 2 | << _input_rowsets.back()->end_version() |
1856 | 2 | << "], original_rowset_count=" << original_count |
1857 | 2 | << ", final_rowset_count=" << _input_rowsets.size() |
1858 | 2 | << ", truncated_rowset_count=" << truncated_count |
1859 | 2 | << ", original_size_bytes=" << original_size |
1860 | 2 | << ", final_size_bytes=" << final_size |
1861 | 2 | << ", truncated_size_bytes=" << truncated_size |
1862 | 2 | << ", threshold_bytes=" << config::compaction_txn_max_size_bytes; |
1863 | 2 | } |
1864 | | |
1865 | 9.50k | return truncated_count; |
1866 | 9.50k | } |
1867 | | |
1868 | 9.85k | Status CloudCompactionMixin::execute_compact() { |
1869 | 9.85k | int64_t profile_start_time_ms = UnixMillis(); |
1870 | 9.85k | TEST_INJECTION_POINT("Compaction::do_compaction"); |
1871 | 9.85k | int64_t permits = get_compaction_permits(); |
1872 | 9.85k | HANDLE_EXCEPTION_IF_CATCH_EXCEPTION( |
1873 | 9.85k | execute_compact_impl(permits), [&](const doris::Exception& ex) { |
1874 | 9.85k | auto st = garbage_collection(); |
1875 | 9.85k | if (_tablet->keys_type() == KeysType::UNIQUE_KEYS && |
1876 | 9.85k | _tablet->enable_unique_key_merge_on_write() && !st.ok()) { |
1877 | | // if compaction fail, be will try to abort compaction, and delete bitmap lock |
1878 | | // will release if abort job successfully, but if abort failed, delete bitmap |
1879 | | // lock will not release, in this situation, be need to send this rpc to ms |
1880 | | // to try to release delete bitmap lock. |
1881 | 9.85k | _engine.meta_mgr().remove_delete_bitmap_update_lock( |
1882 | 9.85k | _tablet->table_id(), COMPACTION_DELETE_BITMAP_LOCK_ID, initiator(), |
1883 | 9.85k | _tablet->tablet_id()); |
1884 | 9.85k | } |
1885 | 9.85k | submit_profile_record(false, profile_start_time_ms, ex.what()); |
1886 | 9.91k | }); |
1887 | | |
1888 | 9.91k | DorisMetrics::instance()->remote_compaction_read_rows_total->increment(_input_row_num); |
1889 | 9.91k | DorisMetrics::instance()->remote_compaction_write_rows_total->increment( |
1890 | 9.91k | _output_rowset->num_rows()); |
1891 | 9.91k | DorisMetrics::instance()->remote_compaction_write_bytes_total->increment( |
1892 | 9.91k | _output_rowset->total_disk_size()); |
1893 | | |
1894 | 9.91k | _load_segment_to_cache(); |
1895 | 9.91k | submit_profile_record(true, profile_start_time_ms); |
1896 | 9.91k | return Status::OK(); |
1897 | 9.85k | } |
1898 | | |
1899 | 0 | Status CloudCompactionMixin::modify_rowsets() { |
1900 | 0 | return Status::OK(); |
1901 | 0 | } |
1902 | | |
1903 | 10.0k | Status CloudCompactionMixin::set_storage_resource_from_input_rowsets(RowsetWriterContext& ctx) { |
1904 | | // Set storage resource from input rowsets by iterating backwards to find the first rowset |
1905 | | // with non-empty resource_id. This handles two scenarios: |
1906 | | // 1. Hole rowsets compaction: Multiple hole rowsets may lack storage resource. |
1907 | | // Example: [0-1, 2-2, 3-3, 4-4, 5-5] where 2-5 are hole rowsets. |
1908 | | // If 0-1 lacks resource_id, then 2-5 also lack resource_id. |
1909 | | // 2. Schema change: New tablet may have later version empty rowsets without resource_id, |
1910 | | // but middle rowsets get resource_id after historical rowsets are converted. |
1911 | | // We iterate backwards to find the most recent rowset with valid resource_id. |
1912 | | |
1913 | 24.2k | for (const auto& rowset : std::ranges::reverse_view(_input_rowsets)) { |
1914 | 24.2k | const auto& resource_id = rowset->rowset_meta()->resource_id(); |
1915 | | |
1916 | 24.2k | if (!resource_id.empty()) { |
1917 | 7.27k | ctx.storage_resource = *DORIS_TRY(rowset->rowset_meta()->remote_storage_resource()); |
1918 | 7.27k | return Status::OK(); |
1919 | 7.27k | } |
1920 | | |
1921 | | // Validate that non-empty rowsets (num_segments > 0) must have valid resource_id |
1922 | | // Only hole rowsets or empty rowsets are allowed to have empty resource_id |
1923 | 16.9k | if (rowset->num_segments() > 0) { |
1924 | 0 | auto error_msg = fmt::format( |
1925 | 0 | "Non-empty rowset must have valid resource_id. " |
1926 | 0 | "rowset_id={}, version=[{}-{}], is_hole_rowset={}, num_segments={}, " |
1927 | 0 | "tablet_id={}, table_id={}", |
1928 | 0 | rowset->rowset_id().to_string(), rowset->start_version(), rowset->end_version(), |
1929 | 0 | rowset->is_hole_rowset(), rowset->num_segments(), _tablet->tablet_id(), |
1930 | 0 | _tablet->table_id()); |
1931 | |
|
1932 | 0 | #ifndef BE_TEST |
1933 | 0 | DCHECK(false) << error_msg; |
1934 | 0 | #endif |
1935 | |
|
1936 | 0 | return Status::InternalError<false>(error_msg); |
1937 | 0 | } |
1938 | 16.9k | } |
1939 | | |
1940 | 2.72k | return Status::OK(); |
1941 | 10.0k | } |
1942 | | |
1943 | 10.0k | Status CloudCompactionMixin::construct_output_rowset_writer(RowsetWriterContext& ctx) { |
1944 | | // only do index compaction for dup_keys and unique_keys with mow enabled |
1945 | 10.0k | if (_enable_inverted_index_compaction && (((_tablet->keys_type() == KeysType::UNIQUE_KEYS && |
1946 | 9.31k | _tablet->enable_unique_key_merge_on_write()) || |
1947 | 9.31k | _tablet->keys_type() == KeysType::DUP_KEYS))) { |
1948 | 8.15k | construct_index_compaction_columns(ctx); |
1949 | 8.15k | } |
1950 | | |
1951 | | // Use the storage resource of the previous rowset. |
1952 | 10.0k | RETURN_IF_ERROR(set_storage_resource_from_input_rowsets(ctx)); |
1953 | | |
1954 | 10.0k | ctx.txn_id = boost::uuids::hash_value(UUIDGenerator::instance()->next_uuid()) & |
1955 | 10.0k | std::numeric_limits<int64_t>::max(); // MUST be positive |
1956 | 10.0k | ctx.txn_expiration = _expiration; |
1957 | | |
1958 | 10.0k | ctx.version = _output_version; |
1959 | 10.0k | ctx.rowset_state = VISIBLE; |
1960 | 10.0k | ctx.segments_overlap = NONOVERLAPPING; |
1961 | 10.0k | ctx.tablet_schema = _cur_tablet_schema; |
1962 | 10.0k | ctx.newest_write_timestamp = _newest_write_timestamp; |
1963 | 10.0k | ctx.write_type = DataWriteType::TYPE_COMPACTION; |
1964 | 10.0k | ctx.compaction_type = compaction_type(); |
1965 | 10.0k | ctx.allow_packed_file = false; |
1966 | 10.0k | if (_tablet->is_row_binlog_tablet()) { |
1967 | 0 | ctx.write_binlog_opt().enable = true; |
1968 | 0 | } |
1969 | | |
1970 | | // We presume that the data involved in cumulative compaction is sufficiently 'hot' |
1971 | | // and should always be retained in the cache. |
1972 | | // TODO(gavin): Ensure that the retention of hot data is implemented with precision. |
1973 | | |
1974 | 10.0k | ctx.write_file_cache = should_cache_compaction_output(); |
1975 | 10.0k | ctx.file_cache_ttl_sec = _tablet->ttl_seconds(); |
1976 | 10.0k | ctx.approximate_bytes_to_write = _input_rowsets_total_size; |
1977 | | |
1978 | | // Set fine-grained control: only write index files to cache if configured |
1979 | 10.0k | ctx.compaction_output_write_index_only = should_enable_compaction_cache_index_only( |
1980 | 10.0k | ctx.write_file_cache, compaction_type(), |
1981 | 10.0k | config::enable_file_cache_write_base_compaction_index_only, |
1982 | 10.0k | config::enable_file_cache_write_cumu_compaction_index_only); |
1983 | | |
1984 | 10.0k | ctx.tablet = _tablet; |
1985 | 10.0k | ctx.job_id = _uuid; |
1986 | | |
1987 | 10.0k | _output_rs_writer = DORIS_TRY(_tablet->create_rowset_writer(ctx, _is_vertical)); |
1988 | 10.0k | RETURN_IF_ERROR(_engine.meta_mgr().prepare_rowset(*_output_rs_writer->rowset_meta().get(), |
1989 | 10.0k | _uuid, _tablet->table_id())); |
1990 | 10.0k | return Status::OK(); |
1991 | 10.0k | } |
1992 | | |
1993 | 48 | Status CloudCompactionMixin::garbage_collection() { |
1994 | 48 | if (!config::enable_file_cache) { |
1995 | 0 | return Status::OK(); |
1996 | 0 | } |
1997 | 48 | if (_output_rs_writer) { |
1998 | 48 | auto* beta_rowset_writer = dynamic_cast<BaseBetaRowsetWriter*>(_output_rs_writer.get()); |
1999 | 48 | DCHECK(beta_rowset_writer); |
2000 | 48 | for (const auto& [_, file_writer] : beta_rowset_writer->get_file_writers()) { |
2001 | 43 | auto file_key = io::BlockFileCache::hash(file_writer->path().filename().native()); |
2002 | 43 | auto* file_cache = io::FileCacheFactory::instance()->get_by_path(file_key); |
2003 | 43 | file_cache->remove_if_cached_async(file_key); |
2004 | 43 | } |
2005 | 48 | for (const auto& [_, index_writer] : beta_rowset_writer->index_file_writers()) { |
2006 | 0 | for (const auto& file_name : index_writer->get_index_file_names()) { |
2007 | 0 | auto file_key = io::BlockFileCache::hash(file_name); |
2008 | 0 | auto* file_cache = io::FileCacheFactory::instance()->get_by_path(file_key); |
2009 | 0 | file_cache->remove_if_cached_async(file_key); |
2010 | 0 | } |
2011 | 0 | } |
2012 | 48 | } |
2013 | 48 | return Status::OK(); |
2014 | 48 | } |
2015 | | |
2016 | 9.96k | void CloudCompactionMixin::update_compaction_level() { |
2017 | | // for index change compaction, compaction level should not changed. |
2018 | | // because input rowset num is 1. |
2019 | 9.96k | if (is_index_change_compaction()) { |
2020 | 691 | DCHECK(_input_rowsets.size() == 1); |
2021 | 691 | _output_rowset->rowset_meta()->set_compaction_level( |
2022 | 691 | _input_rowsets.back()->rowset_meta()->compaction_level()); |
2023 | 9.27k | } else { |
2024 | 9.27k | auto compaction_policy = _tablet->tablet_meta()->compaction_policy(); |
2025 | 9.27k | auto cumu_policy = _engine.cumu_compaction_policy(compaction_policy); |
2026 | 9.32k | if (cumu_policy && (cumu_policy->name() == CUMULATIVE_TIME_SERIES_POLICY || |
2027 | 9.32k | cumu_policy->name() == CUMULATIVE_BINLOG_POLICY)) { |
2028 | 4 | int64_t compaction_level = cumu_policy->get_compaction_level( |
2029 | 4 | cloud_tablet(), _input_rowsets, _output_rowset); |
2030 | 4 | _output_rowset->rowset_meta()->set_compaction_level(compaction_level); |
2031 | 4 | } |
2032 | 9.27k | } |
2033 | 9.96k | } |
2034 | | |
2035 | | // should skip hole rowsets, ortherwise the count will be wrong in ms |
2036 | 9.97k | int64_t CloudCompactionMixin::num_input_rowsets() const { |
2037 | 9.97k | int64_t count = 0; |
2038 | 72.7k | for (const auto& r : _input_rowsets) { |
2039 | 72.7k | if (!r->is_hole_rowset()) { |
2040 | 28.7k | count++; |
2041 | 28.7k | } |
2042 | 72.7k | } |
2043 | 9.97k | return count; |
2044 | 9.97k | } |
2045 | | |
2046 | 10.0k | bool CloudCompactionMixin::should_cache_compaction_output() { |
2047 | 10.0k | if (config::enable_file_cache_write_index_file_only) { |
2048 | 1 | return false; |
2049 | 1 | } |
2050 | | |
2051 | 10.0k | if (compaction_type() == ReaderType::READER_CUMULATIVE_COMPACTION) { |
2052 | 9.91k | return true; |
2053 | 9.91k | } |
2054 | | |
2055 | 142 | if (compaction_type() == ReaderType::READER_BASE_COMPACTION) { |
2056 | 82 | double input_rowsets_hit_cache_ratio = 0.0; |
2057 | | |
2058 | 82 | int64_t _input_rowsets_cached_size = |
2059 | 82 | _input_rowsets_cached_data_size + _input_rowsets_cached_index_size; |
2060 | 82 | if (_input_rowsets_total_size > 0) { |
2061 | 67 | input_rowsets_hit_cache_ratio = |
2062 | 67 | double(_input_rowsets_cached_size) / double(_input_rowsets_total_size); |
2063 | 67 | } |
2064 | | |
2065 | 82 | LOG(INFO) << "CloudBaseCompaction should_cache_compaction_output" |
2066 | 82 | << ", tablet_id=" << _tablet->tablet_id() |
2067 | 82 | << ", input_rowsets_hit_cache_ratio=" << input_rowsets_hit_cache_ratio |
2068 | 82 | << ", _input_rowsets_cached_size=" << _input_rowsets_cached_size |
2069 | 82 | << ", _input_rowsets_total_size=" << _input_rowsets_total_size |
2070 | 82 | << ", enable_file_cache_keep_base_compaction_output=" |
2071 | 82 | << config::enable_file_cache_keep_base_compaction_output |
2072 | 82 | << ", file_cache_keep_base_compaction_output_min_hit_ratio=" |
2073 | 82 | << config::file_cache_keep_base_compaction_output_min_hit_ratio; |
2074 | | |
2075 | 82 | if (config::enable_file_cache_keep_base_compaction_output) { |
2076 | 1 | return true; |
2077 | 1 | } |
2078 | | |
2079 | 81 | if (input_rowsets_hit_cache_ratio > |
2080 | 81 | config::file_cache_keep_base_compaction_output_min_hit_ratio) { |
2081 | 20 | return true; |
2082 | 20 | } |
2083 | 81 | } |
2084 | 121 | return false; |
2085 | 142 | } |
2086 | | |
2087 | | } // namespace doris |