/root/doris/be/src/olap/compaction.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "olap/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 <set> |
36 | | #include <shared_mutex> |
37 | | #include <utility> |
38 | | |
39 | | #include "cloud/cloud_meta_mgr.h" |
40 | | #include "cloud/cloud_storage_engine.h" |
41 | | #include "common/config.h" |
42 | | #include "common/status.h" |
43 | | #include "cpp/sync_point.h" |
44 | | #include "io/cache/block_file_cache_factory.h" |
45 | | #include "io/fs/file_system.h" |
46 | | #include "io/fs/file_writer.h" |
47 | | #include "io/fs/remote_file_system.h" |
48 | | #include "io/io_common.h" |
49 | | #include "olap/cumulative_compaction.h" |
50 | | #include "olap/cumulative_compaction_policy.h" |
51 | | #include "olap/cumulative_compaction_time_series_policy.h" |
52 | | #include "olap/data_dir.h" |
53 | | #include "olap/olap_common.h" |
54 | | #include "olap/olap_define.h" |
55 | | #include "olap/rowset/beta_rowset.h" |
56 | | #include "olap/rowset/beta_rowset_reader.h" |
57 | | #include "olap/rowset/beta_rowset_writer.h" |
58 | | #include "olap/rowset/rowset.h" |
59 | | #include "olap/rowset/rowset_fwd.h" |
60 | | #include "olap/rowset/rowset_meta.h" |
61 | | #include "olap/rowset/rowset_writer.h" |
62 | | #include "olap/rowset/rowset_writer_context.h" |
63 | | #include "olap/rowset/segment_v2/inverted_index_compaction.h" |
64 | | #include "olap/rowset/segment_v2/inverted_index_desc.h" |
65 | | #include "olap/rowset/segment_v2/inverted_index_file_reader.h" |
66 | | #include "olap/rowset/segment_v2/inverted_index_file_writer.h" |
67 | | #include "olap/rowset/segment_v2/inverted_index_fs_directory.h" |
68 | | #include "olap/storage_engine.h" |
69 | | #include "olap/storage_policy.h" |
70 | | #include "olap/tablet.h" |
71 | | #include "olap/tablet_meta.h" |
72 | | #include "olap/tablet_meta_manager.h" |
73 | | #include "olap/task/engine_checksum_task.h" |
74 | | #include "olap/txn_manager.h" |
75 | | #include "olap/utils.h" |
76 | | #include "runtime/memory/mem_tracker_limiter.h" |
77 | | #include "runtime/thread_context.h" |
78 | | #include "util/doris_metrics.h" |
79 | | #include "util/time.h" |
80 | | #include "util/trace.h" |
81 | | |
82 | | using std::vector; |
83 | | |
84 | | namespace doris { |
85 | | using namespace ErrorCode; |
86 | | |
87 | | namespace { |
88 | | |
89 | 128k | bool is_rowset_tidy(std::string& pre_max_key, const RowsetSharedPtr& rhs) { |
90 | 128k | size_t min_tidy_size = config::ordered_data_compaction_min_segment_size; |
91 | 128k | if (rhs->num_segments() == 0) { |
92 | 123k | return true; |
93 | 123k | } |
94 | 5.16k | if (rhs->is_segments_overlapping()) { |
95 | 0 | return false; |
96 | 0 | } |
97 | | // check segment size |
98 | 5.16k | auto* beta_rowset = reinterpret_cast<BetaRowset*>(rhs.get()); |
99 | 5.16k | std::vector<size_t> segments_size; |
100 | 5.16k | RETURN_FALSE_IF_ERROR(beta_rowset->get_segments_size(&segments_size)); |
101 | 5.19k | for (auto segment_size : segments_size) { |
102 | | // is segment is too small, need to do compaction |
103 | 5.19k | if (segment_size < min_tidy_size) { |
104 | 5.16k | return false; |
105 | 5.16k | } |
106 | 5.19k | } |
107 | 18.4E | std::string min_key; |
108 | 18.4E | auto ret = rhs->first_key(&min_key); |
109 | 18.4E | if (!ret) { |
110 | 0 | return false; |
111 | 0 | } |
112 | 18.4E | if (min_key <= pre_max_key) { |
113 | 0 | return false; |
114 | 0 | } |
115 | 18.4E | CHECK(rhs->last_key(&pre_max_key)); |
116 | | |
117 | 18.4E | return true; |
118 | 18.4E | } |
119 | | |
120 | | } // namespace |
121 | | |
122 | | Compaction::Compaction(BaseTabletSPtr tablet, const std::string& label) |
123 | | : _mem_tracker( |
124 | | MemTrackerLimiter::create_shared(MemTrackerLimiter::Type::COMPACTION, label)), |
125 | | _tablet(std::move(tablet)), |
126 | | _is_vertical(config::enable_vertical_compaction), |
127 | 103k | _allow_delete_in_cumu_compaction(config::enable_delete_when_cumu_compaction) { |
128 | 103k | init_profile(label); |
129 | 103k | SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_mem_tracker); |
130 | 103k | _rowid_conversion = std::make_unique<RowIdConversion>(); |
131 | 103k | } |
132 | | |
133 | 103k | Compaction::~Compaction() { |
134 | 103k | SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_mem_tracker); |
135 | 103k | _output_rs_writer.reset(); |
136 | 103k | _tablet.reset(); |
137 | 103k | _input_rowsets.clear(); |
138 | 103k | _output_rowset.reset(); |
139 | 103k | _cur_tablet_schema.reset(); |
140 | 103k | _rowid_conversion.reset(); |
141 | 103k | } |
142 | | |
143 | 103k | void Compaction::init_profile(const std::string& label) { |
144 | 103k | _profile = std::make_unique<RuntimeProfile>(label); |
145 | | |
146 | 103k | _input_rowsets_data_size_counter = |
147 | 103k | ADD_COUNTER(_profile, "input_rowsets_data_size", TUnit::BYTES); |
148 | 103k | _input_rowsets_counter = ADD_COUNTER(_profile, "input_rowsets_count", TUnit::UNIT); |
149 | 103k | _input_row_num_counter = ADD_COUNTER(_profile, "input_row_num", TUnit::UNIT); |
150 | 103k | _input_segments_num_counter = ADD_COUNTER(_profile, "input_segments_num", TUnit::UNIT); |
151 | 103k | _merged_rows_counter = ADD_COUNTER(_profile, "merged_rows", TUnit::UNIT); |
152 | 103k | _filtered_rows_counter = ADD_COUNTER(_profile, "filtered_rows", TUnit::UNIT); |
153 | 103k | _output_rowset_data_size_counter = |
154 | 103k | ADD_COUNTER(_profile, "output_rowset_data_size", TUnit::BYTES); |
155 | 103k | _output_row_num_counter = ADD_COUNTER(_profile, "output_row_num", TUnit::UNIT); |
156 | 103k | _output_segments_num_counter = ADD_COUNTER(_profile, "output_segments_num", TUnit::UNIT); |
157 | 103k | _merge_rowsets_latency_timer = ADD_TIMER(_profile, "merge_rowsets_latency"); |
158 | 103k | } |
159 | | |
160 | 11.5k | int64_t Compaction::merge_way_num() { |
161 | 11.5k | int64_t way_num = 0; |
162 | 137k | for (auto&& rowset : _input_rowsets) { |
163 | 137k | way_num += rowset->rowset_meta()->get_merge_way_num(); |
164 | 137k | } |
165 | | |
166 | 11.5k | return way_num; |
167 | 11.5k | } |
168 | | |
169 | 11.5k | Status Compaction::merge_input_rowsets() { |
170 | 11.5k | std::vector<RowsetReaderSharedPtr> input_rs_readers; |
171 | 11.5k | input_rs_readers.reserve(_input_rowsets.size()); |
172 | 137k | for (auto& rowset : _input_rowsets) { |
173 | 137k | RowsetReaderSharedPtr rs_reader; |
174 | 137k | RETURN_IF_ERROR(rowset->create_reader(&rs_reader)); |
175 | 137k | input_rs_readers.push_back(std::move(rs_reader)); |
176 | 137k | } |
177 | | |
178 | 11.5k | RowsetWriterContext ctx; |
179 | 11.5k | RETURN_IF_ERROR(construct_output_rowset_writer(ctx)); |
180 | | |
181 | | // write merged rows to output rowset |
182 | | // The test results show that merger is low-memory-footprint, there is no need to tracker its mem pool |
183 | | // if ctx.columns_to_do_index_compaction.size() > 0, it means we need to do inverted index compaction. |
184 | | // the row ID conversion matrix needs to be used for inverted index compaction. |
185 | 11.5k | if (!ctx.columns_to_do_index_compaction.empty() || |
186 | 11.5k | (_tablet->keys_type() == KeysType::UNIQUE_KEYS && |
187 | 11.1k | _tablet->enable_unique_key_merge_on_write())) { |
188 | 7.22k | _stats.rowid_conversion = _rowid_conversion.get(); |
189 | 7.22k | } |
190 | | |
191 | 11.5k | int64_t way_num = merge_way_num(); |
192 | | |
193 | 11.5k | Status res; |
194 | 11.5k | { |
195 | 11.5k | SCOPED_TIMER(_merge_rowsets_latency_timer); |
196 | | // 1. Merge segment files and write bkd inverted index |
197 | 11.5k | if (_is_vertical) { |
198 | 11.5k | if (!_tablet->tablet_schema()->cluster_key_uids().empty()) { |
199 | 358 | RETURN_IF_ERROR(update_delete_bitmap()); |
200 | 358 | } |
201 | 11.5k | res = Merger::vertical_merge_rowsets(_tablet, compaction_type(), *_cur_tablet_schema, |
202 | 11.5k | input_rs_readers, _output_rs_writer.get(), |
203 | 11.5k | get_avg_segment_rows(), way_num, &_stats); |
204 | 11.5k | } else { |
205 | 0 | if (!_tablet->tablet_schema()->cluster_key_uids().empty()) { |
206 | 0 | return Status::InternalError( |
207 | 0 | "mow table with cluster keys does not support non vertical compaction"); |
208 | 0 | } |
209 | 0 | res = Merger::vmerge_rowsets(_tablet, compaction_type(), *_cur_tablet_schema, |
210 | 0 | input_rs_readers, _output_rs_writer.get(), &_stats); |
211 | 0 | } |
212 | | |
213 | 11.5k | _tablet->last_compaction_status = res; |
214 | 11.5k | if (!res.ok()) { |
215 | 0 | return res; |
216 | 0 | } |
217 | | // 2. Merge the remaining inverted index files of the string type |
218 | 11.5k | RETURN_IF_ERROR(do_inverted_index_compaction()); |
219 | 11.5k | } |
220 | | |
221 | 11.5k | COUNTER_UPDATE(_merged_rows_counter, _stats.merged_rows); |
222 | 11.5k | COUNTER_UPDATE(_filtered_rows_counter, _stats.filtered_rows); |
223 | | |
224 | | // 3. In the `build`, `_close_file_writers` is called to close the inverted index file writer and write the final compound index file. |
225 | 11.5k | RETURN_NOT_OK_STATUS_WITH_WARN(_output_rs_writer->build(_output_rowset), |
226 | 11.5k | fmt::format("rowset writer build failed. output_version: {}", |
227 | 11.5k | _output_version.to_string())); |
228 | | |
229 | | //RETURN_IF_ERROR(_engine.meta_mgr().commit_rowset(*_output_rowset->rowset_meta().get())); |
230 | | |
231 | | // Now we support delete in cumu compaction, to make all data in rowsets whose version |
232 | | // is below output_version to be delete in the future base compaction, we should carry |
233 | | // all delete predicate in the output rowset. |
234 | | // Output start version > 2 means we must set the delete predicate in the output rowset |
235 | 11.5k | if (_allow_delete_in_cumu_compaction && _output_rowset->version().first > 2) { |
236 | 0 | DeletePredicatePB delete_predicate; |
237 | 0 | std::accumulate(_input_rowsets.begin(), _input_rowsets.end(), &delete_predicate, |
238 | 0 | [](DeletePredicatePB* delete_predicate, const RowsetSharedPtr& rs) { |
239 | 0 | if (rs->rowset_meta()->has_delete_predicate()) { |
240 | 0 | delete_predicate->MergeFrom(rs->rowset_meta()->delete_predicate()); |
241 | 0 | } |
242 | 0 | return delete_predicate; |
243 | 0 | }); |
244 | | // now version in delete_predicate is deprecated |
245 | 0 | if (!delete_predicate.in_predicates().empty() || |
246 | 0 | !delete_predicate.sub_predicates_v2().empty() || |
247 | 0 | !delete_predicate.sub_predicates().empty()) { |
248 | 0 | _output_rowset->rowset_meta()->set_delete_predicate(std::move(delete_predicate)); |
249 | 0 | } |
250 | 0 | } |
251 | | |
252 | 11.5k | _local_read_bytes_total = _stats.bytes_read_from_local; |
253 | 11.5k | _remote_read_bytes_total = _stats.bytes_read_from_remote; |
254 | 11.5k | DorisMetrics::instance()->local_compaction_read_bytes_total->increment(_local_read_bytes_total); |
255 | 11.5k | DorisMetrics::instance()->remote_compaction_read_bytes_total->increment( |
256 | 11.5k | _remote_read_bytes_total); |
257 | 11.5k | DorisMetrics::instance()->local_compaction_write_bytes_total->increment( |
258 | 11.5k | _stats.cached_bytes_total); |
259 | | |
260 | 11.5k | COUNTER_UPDATE(_output_rowset_data_size_counter, _output_rowset->data_disk_size()); |
261 | 11.5k | COUNTER_UPDATE(_output_row_num_counter, _output_rowset->num_rows()); |
262 | 11.5k | COUNTER_UPDATE(_output_segments_num_counter, _output_rowset->num_segments()); |
263 | | |
264 | 11.5k | return check_correctness(); |
265 | 11.5k | } |
266 | | |
267 | 11.5k | int64_t Compaction::get_avg_segment_rows() { |
268 | | // take care of empty rowset |
269 | | // input_rowsets_size is total disk_size of input_rowset, this size is the |
270 | | // final size after codec and compress, so expect dest segment file size |
271 | | // in disk is config::vertical_compaction_max_segment_size |
272 | 11.5k | const auto& meta = _tablet->tablet_meta(); |
273 | 11.5k | if (meta->compaction_policy() == CUMULATIVE_TIME_SERIES_POLICY) { |
274 | 4 | int64_t compaction_goal_size_mbytes = meta->time_series_compaction_goal_size_mbytes(); |
275 | 4 | return (compaction_goal_size_mbytes * 1024 * 1024 * 2) / |
276 | 4 | (_input_rowsets_data_size / (_input_row_num + 1) + 1); |
277 | 4 | } |
278 | 11.5k | return config::vertical_compaction_max_segment_size / |
279 | 11.5k | (_input_rowsets_data_size / (_input_row_num + 1) + 1); |
280 | 11.5k | } |
281 | | |
282 | | CompactionMixin::CompactionMixin(StorageEngine& engine, TabletSharedPtr tablet, |
283 | | const std::string& label) |
284 | 103k | : Compaction(tablet, label), _engine(engine) {} |
285 | | |
286 | 103k | CompactionMixin::~CompactionMixin() { |
287 | 103k | if (_state != CompactionState::SUCCESS && _output_rowset != nullptr) { |
288 | 22 | if (!_output_rowset->is_local()) { |
289 | 0 | tablet()->record_unused_remote_rowset(_output_rowset->rowset_id(), |
290 | 0 | _output_rowset->rowset_meta()->resource_id(), |
291 | 0 | _output_rowset->num_segments()); |
292 | 0 | return; |
293 | 0 | } |
294 | 22 | _engine.add_unused_rowset(_output_rowset); |
295 | 22 | } |
296 | 103k | } |
297 | | |
298 | 1.47M | Tablet* CompactionMixin::tablet() { |
299 | 1.47M | return static_cast<Tablet*>(_tablet.get()); |
300 | 1.47M | } |
301 | | |
302 | 17.6k | Status CompactionMixin::do_compact_ordered_rowsets() { |
303 | 17.6k | build_basic_info(); |
304 | 17.6k | RowsetWriterContext ctx; |
305 | 17.6k | RETURN_IF_ERROR(construct_output_rowset_writer(ctx)); |
306 | | |
307 | 17.6k | LOG(INFO) << "start to do ordered data compaction, tablet=" << _tablet->tablet_id() |
308 | 17.6k | << ", output_version=" << _output_version; |
309 | | // link data to new rowset |
310 | 17.6k | auto seg_id = 0; |
311 | 17.6k | std::vector<KeyBoundsPB> segment_key_bounds; |
312 | 120k | for (auto rowset : _input_rowsets) { |
313 | 120k | RETURN_IF_ERROR(rowset->link_files_to(tablet()->tablet_path(), |
314 | 120k | _output_rs_writer->rowset_id(), seg_id)); |
315 | 120k | seg_id += rowset->num_segments(); |
316 | | |
317 | 120k | std::vector<KeyBoundsPB> key_bounds; |
318 | 120k | RETURN_IF_ERROR(rowset->get_segments_key_bounds(&key_bounds)); |
319 | 120k | segment_key_bounds.insert(segment_key_bounds.end(), key_bounds.begin(), key_bounds.end()); |
320 | 120k | } |
321 | | // build output rowset |
322 | 17.6k | RowsetMetaSharedPtr rowset_meta = std::make_shared<RowsetMeta>(); |
323 | 17.6k | rowset_meta->set_num_rows(_input_row_num); |
324 | 17.6k | rowset_meta->set_total_disk_size(_input_rowsets_data_size + _input_rowsets_index_size); |
325 | 17.6k | rowset_meta->set_data_disk_size(_input_rowsets_data_size); |
326 | 17.6k | rowset_meta->set_index_disk_size(_input_rowsets_index_size); |
327 | 17.6k | rowset_meta->set_empty(_input_row_num == 0); |
328 | 17.6k | rowset_meta->set_num_segments(_input_num_segments); |
329 | 17.6k | rowset_meta->set_segments_overlap(NONOVERLAPPING); |
330 | 17.6k | rowset_meta->set_rowset_state(VISIBLE); |
331 | | |
332 | 17.6k | rowset_meta->set_segments_key_bounds(segment_key_bounds); |
333 | 17.6k | _output_rowset = _output_rs_writer->manual_build(rowset_meta); |
334 | 17.6k | return Status::OK(); |
335 | 17.6k | } |
336 | | |
337 | 29.1k | void CompactionMixin::build_basic_info() { |
338 | 257k | for (auto& rowset : _input_rowsets) { |
339 | 257k | const auto& rowset_meta = rowset->rowset_meta(); |
340 | 257k | auto index_size = rowset_meta->index_disk_size(); |
341 | 257k | auto total_size = rowset_meta->total_disk_size(); |
342 | 257k | auto data_size = rowset_meta->data_disk_size(); |
343 | | // corrupted index size caused by bug before 2.1.5 or 3.0.0 version |
344 | | // try to get real index size from disk. |
345 | 257k | if (index_size < 0 || index_size > total_size * 2) { |
346 | 0 | LOG(ERROR) << "invalid index size:" << index_size << " total size:" << total_size |
347 | 0 | << " data size:" << data_size << " tablet:" << rowset_meta->tablet_id() |
348 | 0 | << " rowset:" << rowset_meta->rowset_id(); |
349 | 0 | index_size = 0; |
350 | 0 | auto st = rowset->get_inverted_index_size(&index_size); |
351 | 0 | if (!st.ok()) { |
352 | 0 | LOG(ERROR) << "failed to get inverted index size. res=" << st; |
353 | 0 | } |
354 | 0 | } |
355 | 257k | _input_rowsets_data_size += data_size; |
356 | 257k | _input_rowsets_index_size += index_size; |
357 | 257k | _input_rowsets_total_size += total_size; |
358 | 257k | _input_row_num += rowset->num_rows(); |
359 | 257k | _input_num_segments += rowset->num_segments(); |
360 | 257k | } |
361 | 29.1k | COUNTER_UPDATE(_input_rowsets_data_size_counter, _input_rowsets_data_size); |
362 | 29.1k | COUNTER_UPDATE(_input_row_num_counter, _input_row_num); |
363 | 29.1k | COUNTER_UPDATE(_input_segments_num_counter, _input_num_segments); |
364 | | |
365 | 29.1k | TEST_SYNC_POINT_RETURN_WITH_VOID("compaction::CompactionMixin::build_basic_info"); |
366 | | |
367 | 29.1k | _output_version = |
368 | 29.1k | Version(_input_rowsets.front()->start_version(), _input_rowsets.back()->end_version()); |
369 | | |
370 | 29.1k | _newest_write_timestamp = _input_rowsets.back()->newest_write_timestamp(); |
371 | | |
372 | 29.1k | std::vector<RowsetMetaSharedPtr> rowset_metas(_input_rowsets.size()); |
373 | 29.1k | std::transform(_input_rowsets.begin(), _input_rowsets.end(), rowset_metas.begin(), |
374 | 257k | [](const RowsetSharedPtr& rowset) { return rowset->rowset_meta(); }); |
375 | 29.1k | _cur_tablet_schema = _tablet->tablet_schema_with_merged_max_schema_version(rowset_metas); |
376 | 29.1k | } |
377 | | |
378 | 29.1k | bool CompactionMixin::handle_ordered_data_compaction() { |
379 | 29.1k | if (!config::enable_ordered_data_compaction) { |
380 | 0 | return false; |
381 | 0 | } |
382 | 29.1k | if (compaction_type() == ReaderType::READER_COLD_DATA_COMPACTION || |
383 | 29.1k | compaction_type() == ReaderType::READER_FULL_COMPACTION) { |
384 | | // The remote file system and full compaction does not support to link files. |
385 | 108 | return false; |
386 | 108 | } |
387 | 29.0k | if (_tablet->keys_type() == KeysType::UNIQUE_KEYS && |
388 | 29.0k | _tablet->enable_unique_key_merge_on_write()) { |
389 | 6.97k | return false; |
390 | 6.97k | } |
391 | | |
392 | 22.1k | if (_tablet->tablet_meta()->tablet_schema()->skip_write_index_on_load()) { |
393 | | // Expected to create index through normal compaction |
394 | 0 | return false; |
395 | 0 | } |
396 | | |
397 | | // check delete version: if compaction type is base compaction and |
398 | | // has a delete version, use original compaction |
399 | 22.1k | if (compaction_type() == ReaderType::READER_BASE_COMPACTION || |
400 | 22.1k | (_allow_delete_in_cumu_compaction && |
401 | 21.9k | compaction_type() == ReaderType::READER_CUMULATIVE_COMPACTION)) { |
402 | 374 | for (auto& rowset : _input_rowsets) { |
403 | 374 | if (rowset->rowset_meta()->has_delete_predicate()) { |
404 | 124 | return false; |
405 | 124 | } |
406 | 374 | } |
407 | 124 | } |
408 | | |
409 | | // check if rowsets are tidy so we can just modify meta and do link |
410 | | // files to handle compaction |
411 | 21.9k | auto input_size = _input_rowsets.size(); |
412 | 21.9k | std::string pre_max_key; |
413 | 145k | for (auto i = 0; i < input_size; ++i) { |
414 | 128k | if (!is_rowset_tidy(pre_max_key, _input_rowsets[i])) { |
415 | 5.16k | if (i <= input_size / 2) { |
416 | 4.34k | return false; |
417 | 4.34k | } else { |
418 | 824 | _input_rowsets.resize(i); |
419 | 824 | break; |
420 | 824 | } |
421 | 5.16k | } |
422 | 128k | } |
423 | | // most rowset of current compaction is nonoverlapping |
424 | | // just handle nonoverlappint rowsets |
425 | 17.6k | auto st = do_compact_ordered_rowsets(); |
426 | 17.6k | if (!st.ok()) { |
427 | 0 | LOG(WARNING) << "failed to compact ordered rowsets: " << st; |
428 | 0 | _pending_rs_guard.drop(); |
429 | 0 | } |
430 | | |
431 | 17.6k | return st.ok(); |
432 | 21.9k | } |
433 | | |
434 | 29.1k | Status CompactionMixin::execute_compact() { |
435 | 29.1k | uint32_t checksum_before; |
436 | 29.1k | uint32_t checksum_after; |
437 | 29.1k | bool enable_compaction_checksum = config::enable_compaction_checksum; |
438 | 29.1k | if (enable_compaction_checksum) { |
439 | 0 | EngineChecksumTask checksum_task(_engine, _tablet->tablet_id(), _tablet->schema_hash(), |
440 | 0 | _input_rowsets.back()->end_version(), &checksum_before); |
441 | 0 | RETURN_IF_ERROR(checksum_task.execute()); |
442 | 0 | } |
443 | | |
444 | 29.1k | auto* data_dir = tablet()->data_dir(); |
445 | 29.1k | int64_t permits = get_compaction_permits(); |
446 | 29.1k | data_dir->disks_compaction_score_increment(permits); |
447 | 29.1k | data_dir->disks_compaction_num_increment(1); |
448 | | |
449 | 29.1k | auto record_compaction_stats = [&](const doris::Exception& ex) { |
450 | 29.1k | _tablet->compaction_count.fetch_add(1, std::memory_order_relaxed); |
451 | 29.1k | data_dir->disks_compaction_score_increment(-permits); |
452 | 29.1k | data_dir->disks_compaction_num_increment(-1); |
453 | 29.1k | }; |
454 | | |
455 | 29.1k | HANDLE_EXCEPTION_IF_CATCH_EXCEPTION(execute_compact_impl(permits), record_compaction_stats); |
456 | 29.1k | record_compaction_stats(doris::Exception()); |
457 | | |
458 | 29.1k | if (enable_compaction_checksum) { |
459 | 0 | EngineChecksumTask checksum_task(_engine, _tablet->tablet_id(), _tablet->schema_hash(), |
460 | 0 | _input_rowsets.back()->end_version(), &checksum_after); |
461 | 0 | RETURN_IF_ERROR(checksum_task.execute()); |
462 | 0 | if (checksum_before != checksum_after) { |
463 | 0 | return Status::InternalError( |
464 | 0 | "compaction tablet checksum not consistent, before={}, after={}, tablet_id={}", |
465 | 0 | checksum_before, checksum_after, _tablet->tablet_id()); |
466 | 0 | } |
467 | 0 | } |
468 | | |
469 | 29.1k | DorisMetrics::instance()->local_compaction_read_rows_total->increment(_input_row_num); |
470 | 29.1k | DorisMetrics::instance()->local_compaction_read_bytes_total->increment( |
471 | 29.1k | _input_rowsets_total_size); |
472 | | |
473 | 29.1k | TEST_SYNC_POINT_RETURN_WITH_VALUE("compaction::CompactionMixin::execute_compact", Status::OK()); |
474 | | |
475 | 29.1k | DorisMetrics::instance()->local_compaction_write_rows_total->increment( |
476 | 29.1k | _output_rowset->num_rows()); |
477 | 29.1k | DorisMetrics::instance()->local_compaction_write_bytes_total->increment( |
478 | 29.1k | _output_rowset->total_disk_size()); |
479 | | |
480 | 29.1k | _load_segment_to_cache(); |
481 | 29.1k | return Status::OK(); |
482 | 29.1k | } |
483 | | |
484 | 29.1k | Status CompactionMixin::execute_compact_impl(int64_t permits) { |
485 | 29.1k | OlapStopWatch watch; |
486 | | |
487 | 29.1k | if (handle_ordered_data_compaction()) { |
488 | 17.6k | RETURN_IF_ERROR(modify_rowsets()); |
489 | 17.6k | LOG(INFO) << "succeed to do ordered data " << compaction_name() |
490 | 17.6k | << ". tablet=" << _tablet->tablet_id() << ", output_version=" << _output_version |
491 | 17.6k | << ", disk=" << tablet()->data_dir()->path() |
492 | 17.6k | << ", segments=" << _input_num_segments << ", input_row_num=" << _input_row_num |
493 | 17.6k | << ", output_row_num=" << _output_rowset->num_rows() |
494 | 17.6k | << ", input_rowsets_data_size=" << _input_rowsets_data_size |
495 | 17.6k | << ", input_rowsets_index_size=" << _input_rowsets_index_size |
496 | 17.6k | << ", input_rowsets_total_size=" << _input_rowsets_total_size |
497 | 17.6k | << ", output_rowset_data_size=" << _output_rowset->data_disk_size() |
498 | 17.6k | << ", output_rowset_index_size=" << _output_rowset->index_disk_size() |
499 | 17.6k | << ", output_rowset_total_size=" << _output_rowset->total_disk_size() |
500 | 17.6k | << ". elapsed time=" << watch.get_elapse_second() << "s."; |
501 | 17.6k | _state = CompactionState::SUCCESS; |
502 | 17.6k | return Status::OK(); |
503 | 17.6k | } |
504 | 11.5k | build_basic_info(); |
505 | | |
506 | 11.5k | TEST_SYNC_POINT_RETURN_WITH_VALUE("compaction::CompactionMixin::execute_compact_impl", |
507 | 11.5k | Status::OK()); |
508 | | |
509 | 18.4E | VLOG_DEBUG << "dump tablet schema: " << _cur_tablet_schema->dump_structure(); |
510 | | |
511 | 11.5k | LOG(INFO) << "start " << compaction_name() << ". tablet=" << _tablet->tablet_id() |
512 | 11.5k | << ", output_version=" << _output_version << ", permits: " << permits; |
513 | | |
514 | 11.5k | RETURN_IF_ERROR(merge_input_rowsets()); |
515 | | |
516 | | // Currently, updates are only made in the time_series. |
517 | 11.5k | update_compaction_level(); |
518 | | |
519 | 11.5k | RETURN_IF_ERROR(modify_rowsets()); |
520 | | |
521 | 11.5k | auto* cumu_policy = tablet()->cumulative_compaction_policy(); |
522 | 11.5k | DCHECK(cumu_policy); |
523 | 11.5k | LOG(INFO) << "succeed to do " << compaction_name() << " is_vertical=" << _is_vertical |
524 | 11.5k | << ". tablet=" << _tablet->tablet_id() << ", output_version=" << _output_version |
525 | 11.5k | << ", current_max_version=" << tablet()->max_version().second |
526 | 11.5k | << ", disk=" << tablet()->data_dir()->path() << ", segments=" << _input_num_segments |
527 | 11.5k | << ", input_rowsets_data_size=" << _input_rowsets_data_size |
528 | 11.5k | << ", input_rowsets_index_size=" << _input_rowsets_index_size |
529 | 11.5k | << ", input_rowsets_total_size=" << _input_rowsets_total_size |
530 | 11.5k | << ", output_rowset_data_size=" << _output_rowset->data_disk_size() |
531 | 11.5k | << ", output_rowset_index_size=" << _output_rowset->index_disk_size() |
532 | 11.5k | << ", output_rowset_total_size=" << _output_rowset->total_disk_size() |
533 | 11.5k | << ", input_row_num=" << _input_row_num |
534 | 11.5k | << ", output_row_num=" << _output_rowset->num_rows() |
535 | 11.5k | << ", filtered_row_num=" << _stats.filtered_rows |
536 | 11.5k | << ", merged_row_num=" << _stats.merged_rows |
537 | 11.5k | << ". elapsed time=" << watch.get_elapse_second() |
538 | 11.5k | << "s. cumulative_compaction_policy=" << cumu_policy->name() |
539 | 11.5k | << ", compact_row_per_second=" << int(_input_row_num / watch.get_elapse_second()); |
540 | | |
541 | 11.5k | _state = CompactionState::SUCCESS; |
542 | | |
543 | 11.5k | return Status::OK(); |
544 | 11.5k | } |
545 | | |
546 | 11.5k | Status Compaction::do_inverted_index_compaction() { |
547 | 11.5k | const auto& ctx = _output_rs_writer->context(); |
548 | 11.5k | if (!config::inverted_index_compaction_enable || _input_row_num <= 0 || |
549 | 11.5k | ctx.columns_to_do_index_compaction.empty()) { |
550 | 11.2k | return Status::OK(); |
551 | 11.2k | } |
552 | | |
553 | 346 | auto error_handler = [this](int64_t index_id, int64_t column_uniq_id) { |
554 | 0 | LOG(WARNING) << "failed to do index compaction" |
555 | 0 | << ". tablet=" << _tablet->tablet_id() << ". column uniq id=" << column_uniq_id |
556 | 0 | << ". index_id=" << index_id; |
557 | 0 | for (auto& rowset : _input_rowsets) { |
558 | 0 | rowset->set_skip_index_compaction(column_uniq_id); |
559 | 0 | LOG(INFO) << "mark skipping inverted index compaction next time" |
560 | 0 | << ". tablet=" << _tablet->tablet_id() << ", rowset=" << rowset->rowset_id() |
561 | 0 | << ", column uniq id=" << column_uniq_id << ", index_id=" << index_id; |
562 | 0 | } |
563 | 0 | }; |
564 | | |
565 | 346 | DBUG_EXECUTE_IF("Compaction::do_inverted_index_compaction_rowid_conversion_null", |
566 | 346 | { _stats.rowid_conversion = nullptr; }) |
567 | 346 | if (!_stats.rowid_conversion) { |
568 | 0 | LOG(WARNING) << "failed to do index compaction, rowid conversion is null" |
569 | 0 | << ". tablet=" << _tablet->tablet_id() |
570 | 0 | << ", input row number=" << _input_row_num; |
571 | 0 | mark_skip_index_compaction(ctx, error_handler); |
572 | |
|
573 | 0 | return Status::Error<INVERTED_INDEX_COMPACTION_ERROR>( |
574 | 0 | "failed to do index compaction, rowid conversion is null. tablet={}", |
575 | 0 | _tablet->tablet_id()); |
576 | 0 | } |
577 | | |
578 | 346 | OlapStopWatch inverted_watch; |
579 | | |
580 | | // translation vec |
581 | | // <<dest_idx_num, dest_docId>> |
582 | | // the first level vector: index indicates src segment. |
583 | | // the second level vector: index indicates row id of source segment, |
584 | | // value indicates row id of destination segment. |
585 | | // <UINT32_MAX, UINT32_MAX> indicates current row not exist. |
586 | 346 | const auto& trans_vec = _stats.rowid_conversion->get_rowid_conversion_map(); |
587 | | |
588 | | // source rowset,segment -> index_id |
589 | 346 | const auto& src_seg_to_id_map = _stats.rowid_conversion->get_src_segment_to_id_map(); |
590 | | |
591 | | // dest rowset id |
592 | 346 | RowsetId dest_rowset_id = _stats.rowid_conversion->get_dst_rowset_id(); |
593 | | // dest segment id -> num rows |
594 | 346 | std::vector<uint32_t> dest_segment_num_rows; |
595 | 346 | RETURN_IF_ERROR(_output_rs_writer->get_segment_num_rows(&dest_segment_num_rows)); |
596 | | |
597 | 346 | auto src_segment_num = src_seg_to_id_map.size(); |
598 | 346 | auto dest_segment_num = dest_segment_num_rows.size(); |
599 | | |
600 | | // when all the input rowsets are deleted, the output rowset will be empty and dest_segment_num will be 0. |
601 | 346 | if (dest_segment_num <= 0) { |
602 | 2 | LOG(INFO) << "skip doing index compaction due to no output segments" |
603 | 2 | << ". tablet=" << _tablet->tablet_id() << ", input row number=" << _input_row_num |
604 | 2 | << ". elapsed time=" << inverted_watch.get_elapse_second() << "s."; |
605 | 2 | return Status::OK(); |
606 | 2 | } |
607 | | |
608 | | // Only write info files when debug index compaction is enabled. |
609 | | // The files are used to debug index compaction and works with index_tool. |
610 | 344 | if (config::debug_inverted_index_compaction) { |
611 | | // src index files |
612 | | // format: rowsetId_segmentId |
613 | 0 | std::vector<std::string> src_index_files(src_segment_num); |
614 | 0 | for (const auto& m : src_seg_to_id_map) { |
615 | 0 | std::pair<RowsetId, uint32_t> p = m.first; |
616 | 0 | src_index_files[m.second] = p.first.to_string() + "_" + std::to_string(p.second); |
617 | 0 | } |
618 | | |
619 | | // dest index files |
620 | | // format: rowsetId_segmentId |
621 | 0 | std::vector<std::string> dest_index_files(dest_segment_num); |
622 | 0 | for (int i = 0; i < dest_segment_num; ++i) { |
623 | 0 | auto prefix = dest_rowset_id.to_string() + "_" + std::to_string(i); |
624 | 0 | dest_index_files[i] = prefix; |
625 | 0 | } |
626 | |
|
627 | 0 | auto write_json_to_file = [&](const nlohmann::json& json_obj, |
628 | 0 | const std::string& file_name) { |
629 | 0 | io::FileWriterPtr file_writer; |
630 | 0 | std::string file_path = |
631 | 0 | fmt::format("{}/{}.json", std::string(getenv("LOG_DIR")), file_name); |
632 | 0 | RETURN_IF_ERROR(io::global_local_filesystem()->create_file(file_path, &file_writer)); |
633 | 0 | RETURN_IF_ERROR(file_writer->append(json_obj.dump())); |
634 | 0 | RETURN_IF_ERROR(file_writer->append("\n")); |
635 | 0 | return file_writer->close(); |
636 | 0 | }; |
637 | | |
638 | | // Convert trans_vec to JSON and print it |
639 | 0 | nlohmann::json trans_vec_json = trans_vec; |
640 | 0 | auto output_version = |
641 | 0 | _output_version.to_string().substr(1, _output_version.to_string().size() - 2); |
642 | 0 | RETURN_IF_ERROR(write_json_to_file( |
643 | 0 | trans_vec_json, |
644 | 0 | fmt::format("trans_vec_{}_{}", _tablet->tablet_id(), output_version))); |
645 | | |
646 | 0 | nlohmann::json src_index_files_json = src_index_files; |
647 | 0 | RETURN_IF_ERROR(write_json_to_file( |
648 | 0 | src_index_files_json, |
649 | 0 | fmt::format("src_idx_dirs_{}_{}", _tablet->tablet_id(), output_version))); |
650 | | |
651 | 0 | nlohmann::json dest_index_files_json = dest_index_files; |
652 | 0 | RETURN_IF_ERROR(write_json_to_file( |
653 | 0 | dest_index_files_json, |
654 | 0 | fmt::format("dest_idx_dirs_{}_{}", _tablet->tablet_id(), output_version))); |
655 | | |
656 | 0 | nlohmann::json dest_segment_num_rows_json = dest_segment_num_rows; |
657 | 0 | RETURN_IF_ERROR(write_json_to_file( |
658 | 0 | dest_segment_num_rows_json, |
659 | 0 | fmt::format("dest_seg_num_rows_{}_{}", _tablet->tablet_id(), output_version))); |
660 | 0 | } |
661 | | |
662 | | // create index_writer to compaction indexes |
663 | 344 | std::unordered_map<RowsetId, Rowset*> rs_id_to_rowset_map; |
664 | 2.27k | for (auto&& rs : _input_rowsets) { |
665 | 2.27k | rs_id_to_rowset_map.emplace(rs->rowset_id(), rs.get()); |
666 | 2.27k | } |
667 | | |
668 | | // src index dirs |
669 | 344 | std::vector<std::unique_ptr<InvertedIndexFileReader>> inverted_index_file_readers( |
670 | 344 | src_segment_num); |
671 | 1.49k | for (const auto& m : src_seg_to_id_map) { |
672 | 1.49k | const auto& [rowset_id, seg_id] = m.first; |
673 | | |
674 | 1.49k | auto find_it = rs_id_to_rowset_map.find(rowset_id); |
675 | 1.49k | DBUG_EXECUTE_IF("Compaction::do_inverted_index_compaction_find_rowset_error", |
676 | 1.49k | { find_it = rs_id_to_rowset_map.end(); }) |
677 | 1.49k | if (find_it == rs_id_to_rowset_map.end()) [[unlikely]] { |
678 | 0 | LOG(WARNING) << "failed to do index compaction, cannot find rowset. tablet_id=" |
679 | 0 | << _tablet->tablet_id() << " rowset_id=" << rowset_id.to_string(); |
680 | 0 | mark_skip_index_compaction(ctx, error_handler); |
681 | 0 | return Status::Error<INVERTED_INDEX_COMPACTION_ERROR>( |
682 | 0 | "failed to do index compaction, cannot find rowset. tablet_id={} rowset_id={}", |
683 | 0 | _tablet->tablet_id(), rowset_id.to_string()); |
684 | 0 | } |
685 | | |
686 | 1.49k | auto* rowset = find_it->second; |
687 | 1.49k | auto fs = rowset->rowset_meta()->fs(); |
688 | 1.49k | DBUG_EXECUTE_IF("Compaction::do_inverted_index_compaction_get_fs_error", { fs = nullptr; }) |
689 | 1.49k | if (!fs) { |
690 | 0 | LOG(WARNING) << "failed to do index compaction, get fs failed. resource_id=" |
691 | 0 | << rowset->rowset_meta()->resource_id(); |
692 | 0 | mark_skip_index_compaction(ctx, error_handler); |
693 | 0 | return Status::Error<INVERTED_INDEX_COMPACTION_ERROR>( |
694 | 0 | "get fs failed, resource_id={}", rowset->rowset_meta()->resource_id()); |
695 | 0 | } |
696 | | |
697 | 1.49k | auto seg_path = rowset->segment_path(seg_id); |
698 | 1.49k | DBUG_EXECUTE_IF("Compaction::do_inverted_index_compaction_seg_path_nullptr", { |
699 | 1.49k | seg_path = ResultError(Status::Error<ErrorCode::INTERNAL_ERROR>( |
700 | 1.49k | "do_inverted_index_compaction_seg_path_nullptr")); |
701 | 1.49k | }) |
702 | 1.49k | if (!seg_path.has_value()) { |
703 | 0 | LOG(WARNING) << "failed to do index compaction, get segment path failed. tablet_id=" |
704 | 0 | << _tablet->tablet_id() << " rowset_id=" << rowset_id.to_string() |
705 | 0 | << " seg_id=" << seg_id; |
706 | 0 | mark_skip_index_compaction(ctx, error_handler); |
707 | 0 | return Status::Error<INVERTED_INDEX_COMPACTION_ERROR>( |
708 | 0 | "get segment path failed. tablet_id={} rowset_id={} seg_id={}", |
709 | 0 | _tablet->tablet_id(), rowset_id.to_string(), seg_id); |
710 | 0 | } |
711 | 1.49k | auto inverted_index_file_reader = std::make_unique<InvertedIndexFileReader>( |
712 | 1.49k | fs, |
713 | 1.49k | std::string {InvertedIndexDescriptor::get_index_file_path_prefix(seg_path.value())}, |
714 | 1.49k | _cur_tablet_schema->get_inverted_index_storage_format(), |
715 | 1.49k | rowset->rowset_meta()->inverted_index_file_info(seg_id)); |
716 | 1.49k | auto st = inverted_index_file_reader->init(config::inverted_index_read_buffer_size); |
717 | 1.49k | DBUG_EXECUTE_IF("Compaction::do_inverted_index_compaction_init_inverted_index_file_reader", |
718 | 1.49k | { |
719 | 1.49k | st = Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( |
720 | 1.49k | "debug point: " |
721 | 1.49k | "Compaction::do_inverted_index_compaction_init_inverted_index_" |
722 | 1.49k | "file_reader error"); |
723 | 1.49k | }) |
724 | 1.49k | if (!st.ok()) { |
725 | 0 | LOG(WARNING) << "failed to do index compaction, init inverted index file reader " |
726 | 0 | "failed. tablet_id=" |
727 | 0 | << _tablet->tablet_id() << " rowset_id=" << rowset_id.to_string() |
728 | 0 | << " seg_id=" << seg_id; |
729 | 0 | mark_skip_index_compaction(ctx, error_handler); |
730 | 0 | return Status::Error<INVERTED_INDEX_COMPACTION_ERROR>( |
731 | 0 | "init inverted index file reader failed. tablet_id={} rowset_id={} seg_id={}", |
732 | 0 | _tablet->tablet_id(), rowset_id.to_string(), seg_id); |
733 | 0 | } |
734 | 1.49k | inverted_index_file_readers[m.second] = std::move(inverted_index_file_reader); |
735 | 1.49k | } |
736 | | |
737 | | // dest index files |
738 | | // format: rowsetId_segmentId |
739 | 344 | auto& inverted_index_file_writers = dynamic_cast<BaseBetaRowsetWriter*>(_output_rs_writer.get()) |
740 | 344 | ->inverted_index_file_writers(); |
741 | 344 | DBUG_EXECUTE_IF( |
742 | 344 | "Compaction::do_inverted_index_compaction_inverted_index_file_writers_size_error", |
743 | 344 | { inverted_index_file_writers.clear(); }) |
744 | 344 | if (inverted_index_file_writers.size() != dest_segment_num) { |
745 | 0 | LOG(WARNING) << "failed to do index compaction, dest segment num not match. tablet_id=" |
746 | 0 | << _tablet->tablet_id() << " dest_segment_num=" << dest_segment_num |
747 | 0 | << " inverted_index_file_writers.size()=" |
748 | 0 | << inverted_index_file_writers.size(); |
749 | 0 | mark_skip_index_compaction(ctx, error_handler); |
750 | 0 | return Status::Error<INVERTED_INDEX_COMPACTION_ERROR>( |
751 | 0 | "dest segment num not match. tablet_id={} dest_segment_num={} " |
752 | 0 | "inverted_index_file_writers.size()={}", |
753 | 0 | _tablet->tablet_id(), dest_segment_num, inverted_index_file_writers.size()); |
754 | 0 | } |
755 | | |
756 | | // use tmp file dir to store index files |
757 | 344 | auto tmp_file_dir = ExecEnv::GetInstance()->get_tmp_file_dirs()->get_tmp_file_dir(); |
758 | 344 | auto index_tmp_path = tmp_file_dir / dest_rowset_id.to_string(); |
759 | 344 | LOG(INFO) << "start index compaction" |
760 | 344 | << ". tablet=" << _tablet->tablet_id() << ", source index size=" << src_segment_num |
761 | 344 | << ", destination index size=" << dest_segment_num << "."; |
762 | | |
763 | 344 | Status status = Status::OK(); |
764 | 900 | for (auto&& column_uniq_id : ctx.columns_to_do_index_compaction) { |
765 | 900 | auto col = _cur_tablet_schema->column_by_uid(column_uniq_id); |
766 | 900 | const auto* index_meta = _cur_tablet_schema->inverted_index(col); |
767 | 900 | DBUG_EXECUTE_IF("Compaction::do_inverted_index_compaction_can_not_find_index_meta", |
768 | 900 | { index_meta = nullptr; }) |
769 | 900 | if (index_meta == nullptr) { |
770 | 0 | status = Status::Error<INVERTED_INDEX_COMPACTION_ERROR>( |
771 | 0 | fmt::format("Can not find index_meta for col {}", col.name())); |
772 | 0 | LOG(WARNING) << "failed to do index compaction, can not find index_meta for column" |
773 | 0 | << ". tablet=" << _tablet->tablet_id() |
774 | 0 | << ", column uniq id=" << column_uniq_id; |
775 | 0 | error_handler(-1, column_uniq_id); |
776 | 0 | break; |
777 | 0 | } |
778 | | |
779 | 900 | std::vector<lucene::store::Directory*> dest_index_dirs(dest_segment_num); |
780 | 900 | try { |
781 | 900 | std::vector<std::unique_ptr<DorisCompoundReader>> src_idx_dirs(src_segment_num); |
782 | 4.16k | for (int src_segment_id = 0; src_segment_id < src_segment_num; src_segment_id++) { |
783 | 3.26k | auto res = inverted_index_file_readers[src_segment_id]->open(index_meta); |
784 | 3.26k | DBUG_EXECUTE_IF("Compaction::open_inverted_index_file_reader", { |
785 | 3.26k | res = ResultError(Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( |
786 | 3.26k | "debug point: Compaction::open_index_file_reader error")); |
787 | 3.26k | }) |
788 | 3.26k | if (!res.has_value()) { |
789 | 0 | LOG(WARNING) << "failed to do index compaction, open inverted index file " |
790 | 0 | "reader failed" |
791 | 0 | << ". tablet=" << _tablet->tablet_id() |
792 | 0 | << ", column uniq id=" << column_uniq_id |
793 | 0 | << ", src_segment_id=" << src_segment_id; |
794 | 0 | throw Exception(ErrorCode::INVERTED_INDEX_COMPACTION_ERROR, res.error().msg()); |
795 | 0 | } |
796 | 3.26k | src_idx_dirs[src_segment_id] = std::move(res.value()); |
797 | 3.26k | } |
798 | 1.91k | for (int dest_segment_id = 0; dest_segment_id < dest_segment_num; dest_segment_id++) { |
799 | 1.01k | auto res = inverted_index_file_writers[dest_segment_id]->open(index_meta); |
800 | 1.01k | DBUG_EXECUTE_IF("Compaction::open_inverted_index_file_writer", { |
801 | 1.01k | res = ResultError(Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( |
802 | 1.01k | "debug point: Compaction::open_inverted_index_file_writer error")); |
803 | 1.01k | }) |
804 | 1.01k | if (!res.has_value()) { |
805 | 0 | LOG(WARNING) << "failed to do index compaction, open inverted index file " |
806 | 0 | "writer failed" |
807 | 0 | << ". tablet=" << _tablet->tablet_id() |
808 | 0 | << ", column uniq id=" << column_uniq_id |
809 | 0 | << ", dest_segment_id=" << dest_segment_id; |
810 | 0 | throw Exception(ErrorCode::INVERTED_INDEX_COMPACTION_ERROR, res.error().msg()); |
811 | 0 | } |
812 | | // Destination directories in dest_index_dirs do not need to be deconstructed, |
813 | | // but their lifecycle must be managed by inverted_index_file_writers. |
814 | 1.01k | dest_index_dirs[dest_segment_id] = res.value().get(); |
815 | 1.01k | } |
816 | 900 | auto st = compact_column(index_meta->index_id(), src_idx_dirs, dest_index_dirs, |
817 | 900 | index_tmp_path.native(), trans_vec, dest_segment_num_rows); |
818 | 900 | if (!st.ok()) { |
819 | 0 | error_handler(index_meta->index_id(), column_uniq_id); |
820 | 0 | status = Status::Error<INVERTED_INDEX_COMPACTION_ERROR>(st.msg()); |
821 | 0 | } |
822 | 900 | } catch (CLuceneError& e) { |
823 | 0 | error_handler(index_meta->index_id(), column_uniq_id); |
824 | 0 | status = Status::Error<INVERTED_INDEX_COMPACTION_ERROR>(e.what()); |
825 | 0 | } catch (const Exception& e) { |
826 | 0 | error_handler(index_meta->index_id(), column_uniq_id); |
827 | 0 | status = Status::Error<INVERTED_INDEX_COMPACTION_ERROR>(e.what()); |
828 | 0 | } |
829 | 900 | } |
830 | | |
831 | | // check index compaction status. If status is not ok, we should return error and end this compaction round. |
832 | 344 | if (!status.ok()) { |
833 | 0 | return status; |
834 | 0 | } |
835 | 344 | LOG(INFO) << "succeed to do index compaction" |
836 | 344 | << ". tablet=" << _tablet->tablet_id() |
837 | 344 | << ". elapsed time=" << inverted_watch.get_elapse_second() << "s."; |
838 | | |
839 | 344 | return Status::OK(); |
840 | 344 | } |
841 | | |
842 | | void Compaction::mark_skip_index_compaction( |
843 | | const RowsetWriterContext& context, |
844 | 0 | const std::function<void(int64_t, int64_t)>& error_handler) { |
845 | 0 | for (auto&& column_uniq_id : context.columns_to_do_index_compaction) { |
846 | 0 | auto col = _cur_tablet_schema->column_by_uid(column_uniq_id); |
847 | 0 | const auto* index_meta = _cur_tablet_schema->inverted_index(col); |
848 | 0 | DBUG_EXECUTE_IF("Compaction::mark_skip_index_compaction_can_not_find_index_meta", |
849 | 0 | { index_meta = nullptr; }) |
850 | 0 | if (index_meta == nullptr) { |
851 | 0 | LOG(WARNING) << "mark skip index compaction, can not find index_meta for column" |
852 | 0 | << ". tablet=" << _tablet->tablet_id() |
853 | 0 | << ", column uniq id=" << column_uniq_id; |
854 | 0 | error_handler(-1, column_uniq_id); |
855 | 0 | continue; |
856 | 0 | } |
857 | 0 | error_handler(index_meta->index_id(), column_uniq_id); |
858 | 0 | } |
859 | 0 | } |
860 | | |
861 | 26.4k | void Compaction::construct_index_compaction_columns(RowsetWriterContext& ctx) { |
862 | 26.4k | for (const auto& index : _cur_tablet_schema->inverted_indexes()) { |
863 | 5.83k | auto col_unique_ids = index->col_unique_ids(); |
864 | | // check if column unique ids is empty to avoid crash |
865 | 5.83k | if (col_unique_ids.empty()) { |
866 | 1 | LOG(WARNING) << "tablet[" << _tablet->tablet_id() << "] index[" << index->index_id() |
867 | 1 | << "] has no column unique id, will skip index compaction." |
868 | 1 | << " tablet_schema=" << _cur_tablet_schema->dump_full_schema(); |
869 | 1 | continue; |
870 | 1 | } |
871 | 5.83k | auto col_unique_id = col_unique_ids[0]; |
872 | 5.83k | if (!_cur_tablet_schema->has_column_unique_id(col_unique_id)) { |
873 | 0 | LOG(WARNING) << "tablet[" << _tablet->tablet_id() << "] column_unique_id[" |
874 | 0 | << col_unique_id << "] not found, will skip index compaction"; |
875 | 0 | continue; |
876 | 0 | } |
877 | | // Avoid doing inverted index compaction on non-slice type columns |
878 | 5.83k | if (!field_is_slice_type(_cur_tablet_schema->column_by_uid(col_unique_id).type())) { |
879 | 4.26k | continue; |
880 | 4.26k | } |
881 | | |
882 | | // if index properties are different, index compaction maybe needs to be skipped. |
883 | 1.56k | bool is_continue = false; |
884 | 1.56k | std::optional<std::map<std::string, std::string>> first_properties; |
885 | 9.37k | for (const auto& rowset : _input_rowsets) { |
886 | 9.37k | const auto* tablet_index = rowset->tablet_schema()->inverted_index(col_unique_id); |
887 | | // no inverted index or index id is different from current index id |
888 | 9.37k | if (tablet_index == nullptr || tablet_index->index_id() != index->index_id()) { |
889 | 3 | is_continue = true; |
890 | 3 | break; |
891 | 3 | } |
892 | 9.37k | auto properties = tablet_index->properties(); |
893 | 9.37k | if (!first_properties.has_value()) { |
894 | 1.56k | first_properties = properties; |
895 | 7.81k | } else { |
896 | 7.81k | DBUG_EXECUTE_IF( |
897 | 7.81k | "Compaction::do_inverted_index_compaction_index_properties_different", |
898 | 7.81k | { properties.emplace("dummy_key", "dummy_value"); }) |
899 | 7.81k | if (properties != first_properties.value()) { |
900 | 3 | is_continue = true; |
901 | 3 | break; |
902 | 3 | } |
903 | 7.81k | } |
904 | 9.37k | } |
905 | 1.56k | if (is_continue) { |
906 | 6 | continue; |
907 | 6 | } |
908 | 9.36k | auto has_inverted_index = [&](const RowsetSharedPtr& src_rs) { |
909 | 9.36k | auto* rowset = static_cast<BetaRowset*>(src_rs.get()); |
910 | 9.36k | DBUG_EXECUTE_IF("Compaction::construct_skip_inverted_index_is_skip_index_compaction", |
911 | 9.36k | { rowset->set_skip_index_compaction(col_unique_id); }) |
912 | 9.36k | if (rowset->is_skip_index_compaction(col_unique_id)) { |
913 | 1 | LOG(WARNING) << "tablet[" << _tablet->tablet_id() << "] rowset[" |
914 | 1 | << rowset->rowset_id() << "] column_unique_id[" << col_unique_id |
915 | 1 | << "] skip inverted index compaction due to last failure"; |
916 | 1 | return false; |
917 | 1 | } |
918 | | |
919 | 9.36k | auto fs = rowset->rowset_meta()->fs(); |
920 | 9.36k | DBUG_EXECUTE_IF("Compaction::construct_skip_inverted_index_get_fs_error", |
921 | 9.36k | { fs = nullptr; }) |
922 | 9.36k | if (!fs) { |
923 | 2 | LOG(WARNING) << "get fs failed, resource_id=" |
924 | 2 | << rowset->rowset_meta()->resource_id(); |
925 | 2 | return false; |
926 | 2 | } |
927 | | |
928 | 9.36k | const auto* index_meta = rowset->tablet_schema()->inverted_index(col_unique_id); |
929 | 9.36k | DBUG_EXECUTE_IF("Compaction::construct_skip_inverted_index_index_meta_nullptr", |
930 | 9.36k | { index_meta = nullptr; }) |
931 | 9.36k | if (index_meta == nullptr) { |
932 | 0 | LOG(WARNING) << "tablet[" << _tablet->tablet_id() << "] column_unique_id[" |
933 | 0 | << col_unique_id << "] index meta is null, will skip index compaction"; |
934 | 0 | return false; |
935 | 0 | } |
936 | | |
937 | 12.8k | for (auto i = 0; i < rowset->num_segments(); i++) { |
938 | | // TODO: inverted_index_path |
939 | 3.50k | auto seg_path = rowset->segment_path(i); |
940 | 3.50k | DBUG_EXECUTE_IF("Compaction::construct_skip_inverted_index_seg_path_nullptr", { |
941 | 3.50k | seg_path = ResultError(Status::Error<ErrorCode::INTERNAL_ERROR>( |
942 | 3.50k | "construct_skip_inverted_index_seg_path_nullptr")); |
943 | 3.50k | }) |
944 | 3.50k | if (!seg_path) { |
945 | 0 | LOG(WARNING) << seg_path.error(); |
946 | 0 | return false; |
947 | 0 | } |
948 | | |
949 | 3.50k | std::string index_file_path; |
950 | 3.50k | try { |
951 | 3.50k | auto inverted_index_file_reader = std::make_unique<InvertedIndexFileReader>( |
952 | 3.50k | fs, |
953 | 3.50k | std::string {InvertedIndexDescriptor::get_index_file_path_prefix( |
954 | 3.50k | seg_path.value())}, |
955 | 3.50k | _cur_tablet_schema->get_inverted_index_storage_format(), |
956 | 3.50k | rowset->rowset_meta()->inverted_index_file_info(i)); |
957 | 3.50k | auto st = inverted_index_file_reader->init( |
958 | 3.50k | config::inverted_index_read_buffer_size); |
959 | 3.50k | index_file_path = inverted_index_file_reader->get_index_file_path(index_meta); |
960 | 3.50k | DBUG_EXECUTE_IF( |
961 | 3.50k | "Compaction::construct_skip_inverted_index_index_file_reader_init_" |
962 | 3.50k | "status_not_ok", |
963 | 3.50k | { |
964 | 3.50k | st = Status::Error<ErrorCode::INTERNAL_ERROR>( |
965 | 3.50k | "debug point: " |
966 | 3.50k | "construct_skip_inverted_index_index_file_reader_init_" |
967 | 3.50k | "status_" |
968 | 3.50k | "not_ok"); |
969 | 3.50k | }) |
970 | 3.50k | if (!st.ok()) { |
971 | 0 | LOG(WARNING) << "init index " << index_file_path << " error:" << st; |
972 | 0 | return false; |
973 | 0 | } |
974 | | |
975 | | // check index meta |
976 | 3.50k | auto result = inverted_index_file_reader->open(index_meta); |
977 | 3.50k | DBUG_EXECUTE_IF( |
978 | 3.50k | "Compaction::construct_skip_inverted_index_index_file_reader_open_" |
979 | 3.50k | "error", |
980 | 3.50k | { |
981 | 3.50k | result = ResultError( |
982 | 3.50k | Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( |
983 | 3.50k | "CLuceneError occur when open idx file")); |
984 | 3.50k | }) |
985 | 3.50k | if (!result.has_value()) { |
986 | 0 | LOG(WARNING) |
987 | 0 | << "open index " << index_file_path << " error:" << result.error(); |
988 | 0 | return false; |
989 | 0 | } |
990 | 3.50k | auto reader = std::move(result.value()); |
991 | 3.50k | std::vector<std::string> files; |
992 | 3.50k | reader->list(&files); |
993 | 3.50k | reader->close(); |
994 | 3.50k | DBUG_EXECUTE_IF( |
995 | 3.50k | "Compaction::construct_skip_inverted_index_index_reader_close_error", |
996 | 3.50k | { _CLTHROWA(CL_ERR_IO, "debug point: reader close error"); }) |
997 | | |
998 | 3.50k | DBUG_EXECUTE_IF("Compaction::construct_skip_inverted_index_index_files_count", |
999 | 3.50k | { files.clear(); }) |
1000 | | |
1001 | | // why is 3? |
1002 | | // slice type index file at least has 3 files: null_bitmap, segments_N, segments.gen |
1003 | 3.50k | if (files.size() < 3) { |
1004 | 0 | LOG(WARNING) << "tablet[" << _tablet->tablet_id() << "] column_unique_id[" |
1005 | 0 | << col_unique_id << "]," << index_file_path |
1006 | 0 | << " is corrupted, will skip index compaction"; |
1007 | 0 | return false; |
1008 | 0 | } |
1009 | 3.50k | } catch (CLuceneError& err) { |
1010 | 0 | LOG(WARNING) << "tablet[" << _tablet->tablet_id() << "] column_unique_id[" |
1011 | 0 | << col_unique_id << "] open index[" << index_file_path |
1012 | 0 | << "], will skip index compaction, error:" << err.what(); |
1013 | 0 | return false; |
1014 | 0 | } |
1015 | 3.50k | } |
1016 | 9.36k | return true; |
1017 | 9.36k | }; |
1018 | | |
1019 | 1.56k | bool all_have_inverted_index = std::all_of(_input_rowsets.begin(), _input_rowsets.end(), |
1020 | 1.56k | std::move(has_inverted_index)); |
1021 | | |
1022 | 1.56k | if (all_have_inverted_index) { |
1023 | 1.55k | ctx.columns_to_do_index_compaction.insert(col_unique_id); |
1024 | 1.55k | } |
1025 | 1.56k | } |
1026 | 26.4k | } |
1027 | | |
1028 | 358 | Status CompactionMixin::update_delete_bitmap() { |
1029 | | // for mow with cluster keys, compaction read data with delete bitmap |
1030 | | // if tablet is not ready(such as schema change), we need to update delete bitmap |
1031 | 358 | { |
1032 | 358 | std::shared_lock meta_rlock(_tablet->get_header_lock()); |
1033 | 358 | if (_tablet->tablet_state() != TABLET_NOTREADY) { |
1034 | 342 | return Status::OK(); |
1035 | 342 | } |
1036 | 358 | } |
1037 | 16 | OlapStopWatch watch; |
1038 | 16 | std::vector<RowsetSharedPtr> rowsets; |
1039 | 108 | for (const auto& rowset : _input_rowsets) { |
1040 | 108 | std::lock_guard rwlock(tablet()->get_rowset_update_lock()); |
1041 | 108 | std::shared_lock rlock(_tablet->get_header_lock()); |
1042 | 108 | Status st = _tablet->update_delete_bitmap_without_lock(_tablet, rowset, &rowsets); |
1043 | 108 | if (!st.ok()) { |
1044 | 0 | LOG(INFO) << "failed update_delete_bitmap_without_lock for tablet_id=" |
1045 | 0 | << _tablet->tablet_id() << ", st=" << st.to_string(); |
1046 | 0 | return st; |
1047 | 0 | } |
1048 | 108 | rowsets.push_back(rowset); |
1049 | 108 | } |
1050 | 16 | LOG(INFO) << "finish update delete bitmap for tablet: " << _tablet->tablet_id() |
1051 | 16 | << ", rowsets: " << _input_rowsets.size() << ", cost: " << watch.get_elapse_time_us() |
1052 | 16 | << "(us)"; |
1053 | 16 | return Status::OK(); |
1054 | 16 | } |
1055 | | |
1056 | 0 | Status CloudCompactionMixin::update_delete_bitmap() { |
1057 | | // for mow with cluster keys, compaction read data with delete bitmap |
1058 | | // if tablet is not ready(such as schema change), we need to update delete bitmap |
1059 | 0 | { |
1060 | 0 | std::shared_lock meta_rlock(_tablet->get_header_lock()); |
1061 | 0 | if (_tablet->tablet_state() != TABLET_NOTREADY) { |
1062 | 0 | return Status::OK(); |
1063 | 0 | } |
1064 | 0 | } |
1065 | 0 | OlapStopWatch watch; |
1066 | 0 | std::vector<RowsetSharedPtr> rowsets; |
1067 | 0 | for (const auto& rowset : _input_rowsets) { |
1068 | 0 | Status st = _tablet->update_delete_bitmap_without_lock(_tablet, rowset, &rowsets); |
1069 | 0 | if (!st.ok()) { |
1070 | 0 | LOG(INFO) << "failed update_delete_bitmap_without_lock for tablet_id=" |
1071 | 0 | << _tablet->tablet_id() << ", st=" << st.to_string(); |
1072 | 0 | return st; |
1073 | 0 | } |
1074 | 0 | rowsets.push_back(rowset); |
1075 | 0 | } |
1076 | 0 | LOG(INFO) << "finish update delete bitmap for tablet: " << _tablet->tablet_id() |
1077 | 0 | << ", rowsets: " << _input_rowsets.size() << ", cost: " << watch.get_elapse_time_us() |
1078 | 0 | << "(us)"; |
1079 | 0 | return Status::OK(); |
1080 | 0 | } |
1081 | | |
1082 | 29.2k | Status CompactionMixin::construct_output_rowset_writer(RowsetWriterContext& ctx) { |
1083 | | // only do index compaction for dup_keys and unique_keys with mow enabled |
1084 | 29.2k | if (config::inverted_index_compaction_enable && |
1085 | 29.2k | (((_tablet->keys_type() == KeysType::UNIQUE_KEYS && |
1086 | 29.2k | _tablet->enable_unique_key_merge_on_write()) || |
1087 | 29.2k | _tablet->keys_type() == KeysType::DUP_KEYS))) { |
1088 | 26.4k | construct_index_compaction_columns(ctx); |
1089 | 26.4k | } |
1090 | 29.2k | ctx.version = _output_version; |
1091 | 29.2k | ctx.rowset_state = VISIBLE; |
1092 | 29.2k | ctx.segments_overlap = NONOVERLAPPING; |
1093 | 29.2k | ctx.tablet_schema = _cur_tablet_schema; |
1094 | 29.2k | ctx.newest_write_timestamp = _newest_write_timestamp; |
1095 | 29.2k | ctx.write_type = DataWriteType::TYPE_COMPACTION; |
1096 | 29.2k | ctx.compaction_type = compaction_type(); |
1097 | 29.2k | _output_rs_writer = DORIS_TRY(_tablet->create_rowset_writer(ctx, _is_vertical)); |
1098 | 29.2k | _pending_rs_guard = _engine.add_pending_rowset(ctx); |
1099 | 29.2k | return Status::OK(); |
1100 | 29.2k | } |
1101 | | |
1102 | 29.0k | Status CompactionMixin::modify_rowsets() { |
1103 | 29.0k | std::vector<RowsetSharedPtr> output_rowsets; |
1104 | 29.0k | output_rowsets.push_back(_output_rowset); |
1105 | | |
1106 | 29.0k | if (_tablet->keys_type() == KeysType::UNIQUE_KEYS && |
1107 | 29.0k | _tablet->enable_unique_key_merge_on_write()) { |
1108 | 6.97k | Version version = tablet()->max_version(); |
1109 | 6.97k | DeleteBitmap output_rowset_delete_bitmap(_tablet->tablet_id()); |
1110 | 6.97k | std::unique_ptr<RowLocationSet> missed_rows; |
1111 | 6.97k | if ((config::enable_missing_rows_correctness_check || |
1112 | 6.97k | config::enable_mow_compaction_correctness_check_core || |
1113 | 6.97k | config::enable_mow_compaction_correctness_check_fail) && |
1114 | 6.97k | !_allow_delete_in_cumu_compaction && |
1115 | 6.97k | compaction_type() == ReaderType::READER_CUMULATIVE_COMPACTION) { |
1116 | 6.89k | missed_rows = std::make_unique<RowLocationSet>(); |
1117 | 6.89k | LOG(INFO) << "RowLocation Set inited succ for tablet:" << _tablet->tablet_id(); |
1118 | 6.89k | } |
1119 | 6.97k | std::unique_ptr<std::map<RowsetSharedPtr, RowLocationPairList>> location_map; |
1120 | 6.97k | if (config::enable_rowid_conversion_correctness_check && |
1121 | 6.97k | tablet()->tablet_schema()->cluster_key_uids().empty()) { |
1122 | 0 | location_map = std::make_unique<std::map<RowsetSharedPtr, RowLocationPairList>>(); |
1123 | 0 | LOG(INFO) << "Location Map inited succ for tablet:" << _tablet->tablet_id(); |
1124 | 0 | } |
1125 | | // Convert the delete bitmap of the input rowsets to output rowset. |
1126 | | // New loads are not blocked, so some keys of input rowsets might |
1127 | | // be deleted during the time. We need to deal with delete bitmap |
1128 | | // of incremental data later. |
1129 | | // TODO(LiaoXin): check if there are duplicate keys |
1130 | 6.97k | std::size_t missed_rows_size = 0; |
1131 | 6.97k | tablet()->calc_compaction_output_rowset_delete_bitmap( |
1132 | 6.97k | _input_rowsets, *_rowid_conversion, 0, version.second + 1, missed_rows.get(), |
1133 | 6.97k | location_map.get(), _tablet->tablet_meta()->delete_bitmap(), |
1134 | 6.97k | &output_rowset_delete_bitmap); |
1135 | 6.97k | if (missed_rows) { |
1136 | 6.89k | missed_rows_size = missed_rows->size(); |
1137 | 6.89k | std::size_t merged_missed_rows_size = _stats.merged_rows; |
1138 | 6.89k | if (!_tablet->tablet_meta()->tablet_schema()->cluster_key_uids().empty()) { |
1139 | 336 | merged_missed_rows_size += _stats.filtered_rows; |
1140 | 336 | } |
1141 | | |
1142 | | // Suppose a heavy schema change process on BE converting tablet A to tablet B. |
1143 | | // 1. during schema change double write, new loads write [X-Y] on tablet B. |
1144 | | // 2. rowsets with version [a],[a+1],...,[b-1],[b] on tablet B are picked for cumu compaction(X<=a<b<=Y).(cumu compaction |
1145 | | // on new tablet during schema change double write is allowed after https://github.com/apache/doris/pull/16470) |
1146 | | // 3. schema change remove all rowsets on tablet B before version Z(b<=Z<=Y) before it begins to convert historical rowsets. |
1147 | | // 4. schema change finishes. |
1148 | | // 5. cumu compation begins on new tablet with version [a],...,[b]. If there are duplicate keys between these rowsets, |
1149 | | // the compaction check will fail because these rowsets have skipped to calculate delete bitmap in commit phase and |
1150 | | // publish phase because tablet B is in NOT_READY state when writing. |
1151 | | |
1152 | | // Considering that the cumu compaction will fail finally in this situation because `Tablet::modify_rowsets` will check if rowsets in |
1153 | | // `to_delete`(_input_rowsets) still exist in tablet's `_rs_version_map`, we can just skip to check missed rows here. |
1154 | 6.89k | bool need_to_check_missed_rows = true; |
1155 | 6.89k | { |
1156 | 6.89k | std::shared_lock rlock(_tablet->get_header_lock()); |
1157 | 6.89k | need_to_check_missed_rows = |
1158 | 6.89k | std::all_of(_input_rowsets.begin(), _input_rowsets.end(), |
1159 | 98.0k | [&](const RowsetSharedPtr& rowset) { |
1160 | 98.0k | return tablet()->rowset_exists_unlocked(rowset); |
1161 | 98.0k | }); |
1162 | 6.89k | } |
1163 | | |
1164 | 6.89k | if (_tablet->tablet_state() == TABLET_RUNNING && |
1165 | 6.89k | merged_missed_rows_size != missed_rows_size && need_to_check_missed_rows) { |
1166 | 0 | std::stringstream ss; |
1167 | 0 | ss << "cumulative compaction: the merged rows(" << _stats.merged_rows |
1168 | 0 | << "), filtered rows(" << _stats.filtered_rows |
1169 | 0 | << ") is not equal to missed rows(" << missed_rows_size |
1170 | 0 | << ") in rowid conversion, tablet_id: " << _tablet->tablet_id() |
1171 | 0 | << ", table_id:" << _tablet->table_id(); |
1172 | 0 | if (missed_rows_size == 0) { |
1173 | 0 | ss << ", debug info: "; |
1174 | 0 | DeleteBitmap subset_map(_tablet->tablet_id()); |
1175 | 0 | for (auto rs : _input_rowsets) { |
1176 | 0 | _tablet->tablet_meta()->delete_bitmap().subset( |
1177 | 0 | {rs->rowset_id(), 0, 0}, |
1178 | 0 | {rs->rowset_id(), rs->num_segments(), version.second + 1}, |
1179 | 0 | &subset_map); |
1180 | 0 | ss << "(rowset id: " << rs->rowset_id() |
1181 | 0 | << ", delete bitmap cardinality: " << subset_map.cardinality() << ")"; |
1182 | 0 | } |
1183 | 0 | ss << ", version[0-" << version.second + 1 << "]"; |
1184 | 0 | } |
1185 | 0 | std::string err_msg = fmt::format( |
1186 | 0 | "cumulative compaction: the merged rows({}), filtered rows({})" |
1187 | 0 | " is not equal to missed rows({}) in rowid conversion," |
1188 | 0 | " tablet_id: {}, table_id:{}", |
1189 | 0 | _stats.merged_rows, _stats.filtered_rows, missed_rows_size, |
1190 | 0 | _tablet->tablet_id(), _tablet->table_id()); |
1191 | 0 | LOG(WARNING) << err_msg; |
1192 | 0 | if (config::enable_mow_compaction_correctness_check_core) { |
1193 | 0 | CHECK(false) << err_msg; |
1194 | 0 | } else if (config::enable_mow_compaction_correctness_check_fail) { |
1195 | 0 | return Status::InternalError<false>(err_msg); |
1196 | 0 | } else { |
1197 | 0 | DCHECK(false) << err_msg; |
1198 | 0 | } |
1199 | 0 | } |
1200 | 6.89k | } |
1201 | | |
1202 | 6.97k | if (location_map) { |
1203 | 0 | RETURN_IF_ERROR(tablet()->check_rowid_conversion(_output_rowset, *location_map)); |
1204 | 0 | location_map->clear(); |
1205 | 0 | } |
1206 | | |
1207 | 6.97k | { |
1208 | 6.97k | std::lock_guard<std::mutex> wrlock_(tablet()->get_rowset_update_lock()); |
1209 | 6.97k | std::lock_guard<std::shared_mutex> wrlock(_tablet->get_header_lock()); |
1210 | 6.97k | SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD); |
1211 | | |
1212 | | // Here we will calculate all the rowsets delete bitmaps which are committed but not published to reduce the calculation pressure |
1213 | | // of publish phase. |
1214 | | // All rowsets which need to recalculate have been published so we don't need to acquire lock. |
1215 | | // Step1: collect this tablet's all committed rowsets' delete bitmaps |
1216 | 6.97k | CommitTabletTxnInfoVec commit_tablet_txn_info_vec {}; |
1217 | 6.97k | _engine.txn_manager()->get_all_commit_tablet_txn_info_by_tablet( |
1218 | 6.97k | *tablet(), &commit_tablet_txn_info_vec); |
1219 | | |
1220 | | // Step2: calculate all rowsets' delete bitmaps which are published during compaction. |
1221 | 6.97k | for (auto& it : commit_tablet_txn_info_vec) { |
1222 | 460 | if (!_check_if_includes_input_rowsets(it.rowset_ids)) { |
1223 | | // When calculating the delete bitmap of all committed rowsets relative to the compaction, |
1224 | | // there may be cases where the compacted rowsets are newer than the committed rowsets. |
1225 | | // At this time, row number conversion cannot be performed, otherwise data will be missing. |
1226 | | // Therefore, we need to check if every committed rowset has calculated delete bitmap for |
1227 | | // all compaction input rowsets. |
1228 | 0 | continue; |
1229 | 0 | } |
1230 | 460 | DeleteBitmap txn_output_delete_bitmap(_tablet->tablet_id()); |
1231 | 460 | tablet()->calc_compaction_output_rowset_delete_bitmap( |
1232 | 460 | _input_rowsets, *_rowid_conversion, 0, UINT64_MAX, missed_rows.get(), |
1233 | 460 | location_map.get(), *it.delete_bitmap.get(), &txn_output_delete_bitmap); |
1234 | 460 | if (config::enable_merge_on_write_correctness_check) { |
1235 | 460 | RowsetIdUnorderedSet rowsetids; |
1236 | 460 | rowsetids.insert(_output_rowset->rowset_id()); |
1237 | 460 | _tablet->add_sentinel_mark_to_delete_bitmap(&txn_output_delete_bitmap, |
1238 | 460 | rowsetids); |
1239 | 460 | } |
1240 | 460 | it.delete_bitmap->merge(txn_output_delete_bitmap); |
1241 | | // Step3: write back updated delete bitmap and tablet info. |
1242 | 460 | it.rowset_ids.insert(_output_rowset->rowset_id()); |
1243 | 460 | _engine.txn_manager()->set_txn_related_delete_bitmap( |
1244 | 460 | it.partition_id, it.transaction_id, _tablet->tablet_id(), |
1245 | 460 | tablet()->tablet_uid(), true, it.delete_bitmap, it.rowset_ids, |
1246 | 460 | it.partial_update_info); |
1247 | 460 | } |
1248 | | |
1249 | | // Convert the delete bitmap of the input rowsets to output rowset for |
1250 | | // incremental data. |
1251 | 6.97k | tablet()->calc_compaction_output_rowset_delete_bitmap( |
1252 | 6.97k | _input_rowsets, *_rowid_conversion, version.second, UINT64_MAX, |
1253 | 6.97k | missed_rows.get(), location_map.get(), _tablet->tablet_meta()->delete_bitmap(), |
1254 | 6.97k | &output_rowset_delete_bitmap); |
1255 | | |
1256 | 6.97k | if (missed_rows) { |
1257 | 6.89k | DCHECK_EQ(missed_rows->size(), missed_rows_size); |
1258 | 6.89k | if (missed_rows->size() != missed_rows_size) { |
1259 | 0 | LOG(WARNING) << "missed rows don't match, before: " << missed_rows_size |
1260 | 0 | << " after: " << missed_rows->size(); |
1261 | 0 | } |
1262 | 6.89k | } |
1263 | | |
1264 | 6.97k | if (location_map) { |
1265 | 0 | RETURN_IF_ERROR(tablet()->check_rowid_conversion(_output_rowset, *location_map)); |
1266 | 0 | } |
1267 | | |
1268 | 6.97k | tablet()->merge_delete_bitmap(output_rowset_delete_bitmap); |
1269 | 6.97k | RETURN_IF_ERROR(tablet()->modify_rowsets(output_rowsets, _input_rowsets, true)); |
1270 | 6.97k | } |
1271 | 22.1k | } else { |
1272 | 22.1k | std::lock_guard<std::shared_mutex> wrlock(_tablet->get_header_lock()); |
1273 | 22.1k | SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD); |
1274 | 22.1k | RETURN_IF_ERROR(tablet()->modify_rowsets(output_rowsets, _input_rowsets, true)); |
1275 | 22.1k | } |
1276 | | |
1277 | 29.0k | if (config::tablet_rowset_stale_sweep_by_size && |
1278 | 29.0k | _tablet->tablet_meta()->all_stale_rs_metas().size() >= |
1279 | 0 | config::tablet_rowset_stale_sweep_threshold_size) { |
1280 | 0 | tablet()->delete_expired_stale_rowset(); |
1281 | 0 | } |
1282 | | |
1283 | 29.0k | int64_t cur_max_version = 0; |
1284 | 29.0k | { |
1285 | 29.0k | std::shared_lock rlock(_tablet->get_header_lock()); |
1286 | 29.0k | cur_max_version = _tablet->max_version_unlocked(); |
1287 | 29.0k | tablet()->save_meta(); |
1288 | 29.0k | } |
1289 | 29.0k | if (_tablet->keys_type() == KeysType::UNIQUE_KEYS && |
1290 | 29.0k | _tablet->enable_unique_key_merge_on_write()) { |
1291 | 6.96k | auto st = TabletMetaManager::remove_old_version_delete_bitmap( |
1292 | 6.96k | tablet()->data_dir(), _tablet->tablet_id(), cur_max_version); |
1293 | 6.96k | if (!st.ok()) { |
1294 | 0 | LOG(WARNING) << "failed to remove old version delete bitmap, st: " << st; |
1295 | 0 | } |
1296 | 6.96k | } |
1297 | 29.0k | DBUG_EXECUTE_IF("CumulativeCompaction.modify_rowsets.delete_expired_stale_rowset", |
1298 | 29.0k | { tablet()->delete_expired_stale_rowset(); }); |
1299 | 29.0k | return Status::OK(); |
1300 | 29.0k | } |
1301 | | |
1302 | | bool CompactionMixin::_check_if_includes_input_rowsets( |
1303 | 460 | const RowsetIdUnorderedSet& commit_rowset_ids_set) const { |
1304 | 460 | std::vector<RowsetId> commit_rowset_ids {}; |
1305 | 460 | commit_rowset_ids.insert(commit_rowset_ids.end(), commit_rowset_ids_set.begin(), |
1306 | 460 | commit_rowset_ids_set.end()); |
1307 | 460 | std::sort(commit_rowset_ids.begin(), commit_rowset_ids.end()); |
1308 | 460 | std::vector<RowsetId> input_rowset_ids {}; |
1309 | 9.76k | for (const auto& rowset : _input_rowsets) { |
1310 | 9.76k | input_rowset_ids.emplace_back(rowset->rowset_meta()->rowset_id()); |
1311 | 9.76k | } |
1312 | 460 | std::sort(input_rowset_ids.begin(), input_rowset_ids.end()); |
1313 | 460 | return std::includes(commit_rowset_ids.begin(), commit_rowset_ids.end(), |
1314 | 460 | input_rowset_ids.begin(), input_rowset_ids.end()); |
1315 | 460 | } |
1316 | | |
1317 | 11.5k | void CompactionMixin::update_compaction_level() { |
1318 | 11.5k | auto* cumu_policy = tablet()->cumulative_compaction_policy(); |
1319 | 11.5k | if (cumu_policy && cumu_policy->name() == CUMULATIVE_TIME_SERIES_POLICY) { |
1320 | 4 | int64_t compaction_level = |
1321 | 4 | cumu_policy->get_compaction_level(tablet(), _input_rowsets, _output_rowset); |
1322 | 4 | _output_rowset->rowset_meta()->set_compaction_level(compaction_level); |
1323 | 4 | } |
1324 | 11.5k | } |
1325 | | |
1326 | 11.5k | Status Compaction::check_correctness() { |
1327 | | // 1. check row number |
1328 | 11.5k | if (_input_row_num != _output_rowset->num_rows() + _stats.merged_rows + _stats.filtered_rows) { |
1329 | 0 | return Status::Error<CHECK_LINES_ERROR>( |
1330 | 0 | "row_num does not match between cumulative input and output! tablet={}, " |
1331 | 0 | "input_row_num={}, merged_row_num={}, filtered_row_num={}, output_row_num={}", |
1332 | 0 | _tablet->tablet_id(), _input_row_num, _stats.merged_rows, _stats.filtered_rows, |
1333 | 0 | _output_rowset->num_rows()); |
1334 | 0 | } |
1335 | 11.5k | return Status::OK(); |
1336 | 11.5k | } |
1337 | | |
1338 | 57.6k | int64_t CompactionMixin::get_compaction_permits() { |
1339 | 57.6k | int64_t permits = 0; |
1340 | 512k | for (auto&& rowset : _input_rowsets) { |
1341 | 512k | permits += rowset->rowset_meta()->get_compaction_score(); |
1342 | 512k | } |
1343 | 57.6k | return permits; |
1344 | 57.6k | } |
1345 | | |
1346 | 0 | int64_t CompactionMixin::calc_input_rowsets_total_size() const { |
1347 | 0 | int64_t input_rowsets_total_size = 0; |
1348 | 0 | for (const auto& rowset : _input_rowsets) { |
1349 | 0 | const auto& rowset_meta = rowset->rowset_meta(); |
1350 | 0 | auto total_size = rowset_meta->total_disk_size(); |
1351 | 0 | input_rowsets_total_size += total_size; |
1352 | 0 | } |
1353 | 0 | return input_rowsets_total_size; |
1354 | 0 | } |
1355 | | |
1356 | 0 | int64_t CompactionMixin::calc_input_rowsets_row_num() const { |
1357 | 0 | int64_t input_rowsets_row_num = 0; |
1358 | 0 | for (const auto& rowset : _input_rowsets) { |
1359 | 0 | const auto& rowset_meta = rowset->rowset_meta(); |
1360 | 0 | auto total_size = rowset_meta->total_disk_size(); |
1361 | 0 | input_rowsets_row_num += total_size; |
1362 | 0 | } |
1363 | 0 | return input_rowsets_row_num; |
1364 | 0 | } |
1365 | | |
1366 | 29.1k | void Compaction::_load_segment_to_cache() { |
1367 | | // Load new rowset's segments to cache. |
1368 | 29.1k | SegmentCacheHandle handle; |
1369 | 29.1k | auto st = SegmentLoader::instance()->load_segments( |
1370 | 29.1k | std::static_pointer_cast<BetaRowset>(_output_rowset), &handle, true); |
1371 | 29.1k | if (!st.ok()) { |
1372 | 0 | LOG(WARNING) << "failed to load segment to cache! output rowset version=" |
1373 | 0 | << _output_rowset->start_version() << "-" << _output_rowset->end_version() |
1374 | 0 | << "."; |
1375 | 0 | } |
1376 | 29.1k | } |
1377 | | |
1378 | 0 | void CloudCompactionMixin::build_basic_info() { |
1379 | 0 | _output_version = |
1380 | 0 | Version(_input_rowsets.front()->start_version(), _input_rowsets.back()->end_version()); |
1381 | |
|
1382 | 0 | _newest_write_timestamp = _input_rowsets.back()->newest_write_timestamp(); |
1383 | |
|
1384 | 0 | std::vector<RowsetMetaSharedPtr> rowset_metas(_input_rowsets.size()); |
1385 | 0 | std::transform(_input_rowsets.begin(), _input_rowsets.end(), rowset_metas.begin(), |
1386 | 0 | [](const RowsetSharedPtr& rowset) { return rowset->rowset_meta(); }); |
1387 | 0 | _cur_tablet_schema = _tablet->tablet_schema_with_merged_max_schema_version(rowset_metas); |
1388 | 0 | } |
1389 | | |
1390 | 0 | int64_t CloudCompactionMixin::get_compaction_permits() { |
1391 | 0 | int64_t permits = 0; |
1392 | 0 | for (auto&& rowset : _input_rowsets) { |
1393 | 0 | permits += rowset->rowset_meta()->get_compaction_score(); |
1394 | 0 | } |
1395 | 0 | return permits; |
1396 | 0 | } |
1397 | | |
1398 | | CloudCompactionMixin::CloudCompactionMixin(CloudStorageEngine& engine, CloudTabletSPtr tablet, |
1399 | | const std::string& label) |
1400 | 0 | : Compaction(tablet, label), _engine(engine) { |
1401 | 0 | auto uuid = UUIDGenerator::instance()->next_uuid(); |
1402 | 0 | std::stringstream ss; |
1403 | 0 | ss << uuid; |
1404 | 0 | _uuid = ss.str(); |
1405 | 0 | } |
1406 | | |
1407 | 0 | Status CloudCompactionMixin::execute_compact_impl(int64_t permits) { |
1408 | 0 | OlapStopWatch watch; |
1409 | |
|
1410 | 0 | build_basic_info(); |
1411 | |
|
1412 | 0 | LOG(INFO) << "start " << compaction_name() << ". tablet=" << _tablet->tablet_id() |
1413 | 0 | << ", output_version=" << _output_version << ", permits: " << permits; |
1414 | |
|
1415 | 0 | RETURN_IF_ERROR(merge_input_rowsets()); |
1416 | | |
1417 | 0 | DBUG_EXECUTE_IF("CloudFullCompaction::modify_rowsets.wrong_rowset_id", { |
1418 | 0 | DCHECK(compaction_type() == ReaderType::READER_FULL_COMPACTION); |
1419 | 0 | RowsetId id; |
1420 | 0 | id.version = 2; |
1421 | 0 | id.hi = _output_rowset->rowset_meta()->rowset_id().hi + ((int64_t)(1) << 56); |
1422 | 0 | id.mi = _output_rowset->rowset_meta()->rowset_id().mi; |
1423 | 0 | id.lo = _output_rowset->rowset_meta()->rowset_id().lo; |
1424 | 0 | _output_rowset->rowset_meta()->set_rowset_id(id); |
1425 | 0 | LOG(INFO) << "[Debug wrong rowset id]:" |
1426 | 0 | << _output_rowset->rowset_meta()->rowset_id().to_string(); |
1427 | 0 | }) |
1428 | | |
1429 | | // Currently, updates are only made in the time_series. |
1430 | 0 | update_compaction_level(); |
1431 | |
|
1432 | 0 | RETURN_IF_ERROR(_engine.meta_mgr().commit_rowset(*_output_rowset->rowset_meta().get())); |
1433 | | |
1434 | | // 4. modify rowsets in memory |
1435 | 0 | RETURN_IF_ERROR(modify_rowsets()); |
1436 | | |
1437 | 0 | return Status::OK(); |
1438 | 0 | } |
1439 | | |
1440 | 0 | int64_t CloudCompactionMixin::initiator() const { |
1441 | 0 | return HashUtil::hash64(_uuid.data(), _uuid.size(), 0) & std::numeric_limits<int64_t>::max(); |
1442 | 0 | } |
1443 | | |
1444 | 0 | Status CloudCompactionMixin::execute_compact() { |
1445 | 0 | TEST_INJECTION_POINT("Compaction::do_compaction"); |
1446 | 0 | int64_t permits = get_compaction_permits(); |
1447 | 0 | HANDLE_EXCEPTION_IF_CATCH_EXCEPTION( |
1448 | 0 | execute_compact_impl(permits), [&](const doris::Exception& ex) { |
1449 | 0 | auto st = garbage_collection(); |
1450 | 0 | if (_tablet->keys_type() == KeysType::UNIQUE_KEYS && |
1451 | 0 | _tablet->enable_unique_key_merge_on_write() && !st.ok()) { |
1452 | | // if compaction fail, be will try to abort compaction, and delete bitmap lock |
1453 | | // will release if abort job successfully, but if abort failed, delete bitmap |
1454 | | // lock will not release, in this situation, be need to send this rpc to ms |
1455 | | // to try to release delete bitmap lock. |
1456 | 0 | _engine.meta_mgr().remove_delete_bitmap_update_lock( |
1457 | 0 | _tablet->table_id(), COMPACTION_DELETE_BITMAP_LOCK_ID, initiator(), |
1458 | 0 | _tablet->tablet_id()); |
1459 | 0 | } |
1460 | 0 | }); |
1461 | |
|
1462 | 0 | DorisMetrics::instance()->remote_compaction_read_rows_total->increment(_input_row_num); |
1463 | 0 | DorisMetrics::instance()->remote_compaction_write_rows_total->increment( |
1464 | 0 | _output_rowset->num_rows()); |
1465 | 0 | DorisMetrics::instance()->remote_compaction_write_bytes_total->increment( |
1466 | 0 | _output_rowset->total_disk_size()); |
1467 | |
|
1468 | 0 | _load_segment_to_cache(); |
1469 | 0 | return Status::OK(); |
1470 | 0 | } |
1471 | | |
1472 | 0 | Status CloudCompactionMixin::modify_rowsets() { |
1473 | 0 | return Status::OK(); |
1474 | 0 | } |
1475 | | |
1476 | 0 | Status CloudCompactionMixin::construct_output_rowset_writer(RowsetWriterContext& ctx) { |
1477 | | // only do index compaction for dup_keys and unique_keys with mow enabled |
1478 | 0 | if (config::inverted_index_compaction_enable && |
1479 | 0 | (((_tablet->keys_type() == KeysType::UNIQUE_KEYS && |
1480 | 0 | _tablet->enable_unique_key_merge_on_write()) || |
1481 | 0 | _tablet->keys_type() == KeysType::DUP_KEYS))) { |
1482 | 0 | construct_index_compaction_columns(ctx); |
1483 | 0 | } |
1484 | | |
1485 | | // Use the storage resource of the previous rowset |
1486 | 0 | ctx.storage_resource = |
1487 | 0 | *DORIS_TRY(_input_rowsets.back()->rowset_meta()->remote_storage_resource()); |
1488 | |
|
1489 | 0 | ctx.txn_id = boost::uuids::hash_value(UUIDGenerator::instance()->next_uuid()) & |
1490 | 0 | std::numeric_limits<int64_t>::max(); // MUST be positive |
1491 | 0 | ctx.txn_expiration = _expiration; |
1492 | |
|
1493 | 0 | ctx.version = _output_version; |
1494 | 0 | ctx.rowset_state = VISIBLE; |
1495 | 0 | ctx.segments_overlap = NONOVERLAPPING; |
1496 | 0 | ctx.tablet_schema = _cur_tablet_schema; |
1497 | 0 | ctx.newest_write_timestamp = _newest_write_timestamp; |
1498 | 0 | ctx.write_type = DataWriteType::TYPE_COMPACTION; |
1499 | 0 | ctx.compaction_type = compaction_type(); |
1500 | | |
1501 | | // We presume that the data involved in cumulative compaction is sufficiently 'hot' |
1502 | | // and should always be retained in the cache. |
1503 | | // TODO(gavin): Ensure that the retention of hot data is implemented with precision. |
1504 | 0 | ctx.write_file_cache = (compaction_type() == ReaderType::READER_CUMULATIVE_COMPACTION) || |
1505 | 0 | (config::enable_file_cache_keep_base_compaction_output && |
1506 | 0 | compaction_type() == ReaderType::READER_BASE_COMPACTION); |
1507 | 0 | ctx.file_cache_ttl_sec = _tablet->ttl_seconds(); |
1508 | 0 | _output_rs_writer = DORIS_TRY(_tablet->create_rowset_writer(ctx, _is_vertical)); |
1509 | 0 | RETURN_IF_ERROR(_engine.meta_mgr().prepare_rowset(*_output_rs_writer->rowset_meta().get())); |
1510 | 0 | return Status::OK(); |
1511 | 0 | } |
1512 | | |
1513 | 0 | Status CloudCompactionMixin::garbage_collection() { |
1514 | 0 | if (!config::enable_file_cache) { |
1515 | 0 | return Status::OK(); |
1516 | 0 | } |
1517 | 0 | if (_output_rs_writer) { |
1518 | 0 | auto* beta_rowset_writer = dynamic_cast<BaseBetaRowsetWriter*>(_output_rs_writer.get()); |
1519 | 0 | DCHECK(beta_rowset_writer); |
1520 | 0 | for (const auto& [_, file_writer] : beta_rowset_writer->get_file_writers()) { |
1521 | 0 | auto file_key = io::BlockFileCache::hash(file_writer->path().filename().native()); |
1522 | 0 | auto* file_cache = io::FileCacheFactory::instance()->get_by_path(file_key); |
1523 | 0 | file_cache->remove_if_cached_async(file_key); |
1524 | 0 | } |
1525 | 0 | } |
1526 | 0 | return Status::OK(); |
1527 | 0 | } |
1528 | | |
1529 | 0 | void CloudCompactionMixin::update_compaction_level() { |
1530 | 0 | auto compaction_policy = _tablet->tablet_meta()->compaction_policy(); |
1531 | 0 | auto cumu_policy = _engine.cumu_compaction_policy(compaction_policy); |
1532 | 0 | if (cumu_policy && cumu_policy->name() == CUMULATIVE_TIME_SERIES_POLICY) { |
1533 | 0 | int64_t compaction_level = |
1534 | 0 | cumu_policy->get_compaction_level(cloud_tablet(), _input_rowsets, _output_rowset); |
1535 | 0 | _output_rowset->rowset_meta()->set_compaction_level(compaction_level); |
1536 | 0 | } |
1537 | 0 | } |
1538 | | |
1539 | | } // namespace doris |