/root/doris/be/src/storage/merger.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/merger.h" |
19 | | |
20 | | #include <gen_cpp/olap_file.pb.h> |
21 | | #include <gen_cpp/types.pb.h> |
22 | | #include <stddef.h> |
23 | | #include <unistd.h> |
24 | | |
25 | | #include <algorithm> |
26 | | #include <iterator> |
27 | | #include <memory> |
28 | | #include <mutex> |
29 | | #include <numeric> |
30 | | #include <ostream> |
31 | | #include <shared_mutex> |
32 | | #include <string> |
33 | | #include <unordered_map> |
34 | | #include <utility> |
35 | | #include <vector> |
36 | | |
37 | | #include "cloud/config.h" |
38 | | #include "common/config.h" |
39 | | #include "common/logging.h" |
40 | | #include "common/status.h" |
41 | | #include "core/block/block.h" |
42 | | #include "storage/iterator/block_reader.h" |
43 | | #include "storage/iterator/vertical_block_reader.h" |
44 | | #include "storage/iterator/vertical_merge_iterator.h" |
45 | | #include "storage/iterators.h" |
46 | | #include "storage/olap_common.h" |
47 | | #include "storage/olap_define.h" |
48 | | #include "storage/rowid_conversion.h" |
49 | | #include "storage/rowset/beta_rowset.h" |
50 | | #include "storage/rowset/rowset.h" |
51 | | #include "storage/rowset/rowset_meta.h" |
52 | | #include "storage/rowset/rowset_writer.h" |
53 | | #include "storage/segment/segment.h" |
54 | | #include "storage/segment/segment_writer.h" |
55 | | #include "storage/storage_engine.h" |
56 | | #include "storage/tablet/base_tablet.h" |
57 | | #include "storage/tablet/tablet.h" |
58 | | #include "storage/tablet/tablet_fwd.h" |
59 | | #include "storage/tablet/tablet_meta.h" |
60 | | #include "storage/tablet/tablet_reader.h" |
61 | | #include "storage/types.h" |
62 | | #include "storage/utils.h" |
63 | | #include "util/defer_op.h" |
64 | | #include "util/slice.h" |
65 | | |
66 | | namespace doris { |
67 | | #include "common/compile_check_begin.h" |
68 | | Status Merger::vmerge_rowsets(BaseTabletSPtr tablet, ReaderType reader_type, |
69 | | const TabletSchema& cur_tablet_schema, |
70 | | const std::vector<RowsetReaderSharedPtr>& src_rowset_readers, |
71 | 48 | RowsetWriter* dst_rowset_writer, Statistics* stats_output) { |
72 | 48 | if (!cur_tablet_schema.cluster_key_uids().empty()) { |
73 | 0 | return Status::InternalError( |
74 | 0 | "mow table with cluster keys does not support non vertical compaction"); |
75 | 0 | } |
76 | 48 | BlockReader reader; |
77 | 48 | TabletReader::ReaderParams reader_params; |
78 | 48 | reader_params.tablet = tablet; |
79 | 48 | reader_params.reader_type = reader_type; |
80 | | |
81 | 48 | TabletReadSource read_source; |
82 | 48 | read_source.rs_splits.reserve(src_rowset_readers.size()); |
83 | 144 | for (const RowsetReaderSharedPtr& rs_reader : src_rowset_readers) { |
84 | 144 | read_source.rs_splits.emplace_back(rs_reader); |
85 | 144 | } |
86 | 48 | read_source.fill_delete_predicates(); |
87 | 48 | reader_params.set_read_source(std::move(read_source)); |
88 | | |
89 | 48 | reader_params.version = dst_rowset_writer->version(); |
90 | | |
91 | 48 | TabletSchemaSPtr merge_tablet_schema = std::make_shared<TabletSchema>(); |
92 | 48 | merge_tablet_schema->copy_from(cur_tablet_schema); |
93 | | |
94 | | // Merge the columns in delete predicate that not in latest schema in to current tablet schema |
95 | 48 | for (auto& del_pred_rs : reader_params.delete_predicates) { |
96 | 24 | merge_tablet_schema->merge_dropped_columns(*del_pred_rs->tablet_schema()); |
97 | 24 | } |
98 | 48 | reader_params.tablet_schema = merge_tablet_schema; |
99 | 48 | if (!tablet->tablet_schema()->cluster_key_uids().empty()) { |
100 | 0 | reader_params.delete_bitmap = tablet->tablet_meta()->delete_bitmap_ptr(); |
101 | 0 | } |
102 | | |
103 | 48 | if (stats_output && stats_output->rowid_conversion) { |
104 | 48 | reader_params.record_rowids = true; |
105 | 48 | reader_params.rowid_conversion = stats_output->rowid_conversion; |
106 | 48 | stats_output->rowid_conversion->set_dst_rowset_id(dst_rowset_writer->rowset_id()); |
107 | 48 | } |
108 | | |
109 | 48 | reader_params.return_columns.resize(cur_tablet_schema.num_columns()); |
110 | 48 | std::iota(reader_params.return_columns.begin(), reader_params.return_columns.end(), 0); |
111 | 48 | reader_params.origin_return_columns = &reader_params.return_columns; |
112 | 48 | RETURN_IF_ERROR(reader.init(reader_params)); |
113 | | |
114 | 48 | Block block = cur_tablet_schema.create_block(reader_params.return_columns); |
115 | 48 | size_t output_rows = 0; |
116 | 48 | bool eof = false; |
117 | 626 | while (!eof && !ExecEnv::GetInstance()->storage_engine().stopped()) { |
118 | 578 | auto tablet_state = tablet->tablet_state(); |
119 | 578 | if (tablet_state != TABLET_RUNNING && tablet_state != TABLET_NOTREADY) { |
120 | 0 | tablet->clear_cache(); |
121 | 0 | return Status::Error<INTERNAL_ERROR>("tablet {} is not used any more", |
122 | 0 | tablet->tablet_id()); |
123 | 0 | } |
124 | | |
125 | | // Read one block from block reader |
126 | 578 | RETURN_NOT_OK_STATUS_WITH_WARN(reader.next_block_with_aggregation(&block, &eof), |
127 | 578 | "failed to read next block when merging rowsets of tablet " + |
128 | 578 | std::to_string(tablet->tablet_id())); |
129 | 578 | RETURN_NOT_OK_STATUS_WITH_WARN(dst_rowset_writer->add_block(&block), |
130 | 578 | "failed to write block when merging rowsets of tablet " + |
131 | 578 | std::to_string(tablet->tablet_id())); |
132 | | |
133 | 578 | if (reader_params.record_rowids && block.rows() > 0) { |
134 | 578 | std::vector<uint32_t> segment_num_rows; |
135 | 578 | RETURN_IF_ERROR(dst_rowset_writer->get_segment_num_rows(&segment_num_rows)); |
136 | 578 | stats_output->rowid_conversion->add(reader.current_block_row_locations(), |
137 | 578 | segment_num_rows); |
138 | 578 | } |
139 | | |
140 | 578 | output_rows += block.rows(); |
141 | 578 | block.clear_column_data(); |
142 | 578 | } |
143 | 48 | if (ExecEnv::GetInstance()->storage_engine().stopped()) { |
144 | 0 | return Status::Error<INTERNAL_ERROR>("tablet {} failed to do compaction, engine stopped", |
145 | 0 | tablet->tablet_id()); |
146 | 0 | } |
147 | | |
148 | 48 | if (stats_output != nullptr) { |
149 | 48 | stats_output->output_rows = output_rows; |
150 | 48 | stats_output->merged_rows = reader.merged_rows(); |
151 | 48 | stats_output->filtered_rows = reader.filtered_rows(); |
152 | 48 | stats_output->bytes_read_from_local = reader.stats().file_cache_stats.bytes_read_from_local; |
153 | 48 | stats_output->bytes_read_from_remote = |
154 | 48 | reader.stats().file_cache_stats.bytes_read_from_remote; |
155 | 48 | stats_output->cached_bytes_total = reader.stats().file_cache_stats.bytes_write_into_cache; |
156 | 48 | if (config::is_cloud_mode()) { |
157 | 0 | stats_output->cloud_local_read_time = |
158 | 0 | reader.stats().file_cache_stats.local_io_timer / 1000; |
159 | 0 | stats_output->cloud_remote_read_time = |
160 | 0 | reader.stats().file_cache_stats.remote_io_timer / 1000; |
161 | 0 | } |
162 | 48 | } |
163 | | |
164 | 48 | RETURN_NOT_OK_STATUS_WITH_WARN(dst_rowset_writer->flush(), |
165 | 48 | "failed to flush rowset when merging rowsets of tablet " + |
166 | 48 | std::to_string(tablet->tablet_id())); |
167 | | |
168 | 48 | return Status::OK(); |
169 | 48 | } |
170 | | |
171 | | // split columns into several groups, make sure all keys in one group |
172 | | // unique_key should consider sequence&delete column |
173 | | void Merger::vertical_split_columns(const TabletSchema& tablet_schema, |
174 | | std::vector<std::vector<uint32_t>>* column_groups, |
175 | | std::vector<uint32_t>* key_group_cluster_key_idxes, |
176 | 129 | int32_t num_columns_per_group) { |
177 | 129 | size_t num_key_cols = tablet_schema.num_key_columns(); |
178 | 129 | size_t total_cols = tablet_schema.num_columns(); |
179 | 129 | std::vector<uint32_t> key_columns; |
180 | 251 | for (auto i = 0; i < num_key_cols; ++i) { |
181 | 122 | key_columns.emplace_back(i); |
182 | 122 | } |
183 | | // in unique key, sequence & delete sign column should merge with key columns |
184 | 129 | int32_t sequence_col_idx = -1; |
185 | 129 | int32_t delete_sign_idx = -1; |
186 | | // in key column compaction, seq_col real index is _num_key_columns |
187 | | // and delete_sign column is _block->columns() - 1 |
188 | 129 | if (tablet_schema.keys_type() == KeysType::UNIQUE_KEYS) { |
189 | 56 | if (tablet_schema.has_sequence_col()) { |
190 | 5 | sequence_col_idx = tablet_schema.sequence_col_idx(); |
191 | 5 | key_columns.emplace_back(sequence_col_idx); |
192 | 5 | } |
193 | 56 | delete_sign_idx = tablet_schema.field_index(DELETE_SIGN); |
194 | 56 | if (delete_sign_idx != -1) { |
195 | 49 | key_columns.emplace_back(delete_sign_idx); |
196 | 49 | } |
197 | 56 | if (!tablet_schema.cluster_key_uids().empty()) { |
198 | 1 | for (const auto& cid : tablet_schema.cluster_key_uids()) { |
199 | 1 | auto idx = tablet_schema.field_index(cid); |
200 | 1 | DCHECK(idx >= 0) << "could not find cluster key column with unique_id=" << cid |
201 | 0 | << " in tablet schema, table_id=" << tablet_schema.table_id(); |
202 | 1 | if (idx >= num_key_cols) { |
203 | 1 | key_columns.emplace_back(idx); |
204 | 1 | } |
205 | 1 | } |
206 | | // tablet schema unique ids: [1, 2, 5, 3, 6, 4], [1 2] is key columns |
207 | | // cluster key unique ids: [3, 1, 4] |
208 | | // the key_columns should be [0, 1, 3, 5] |
209 | | // the key_group_cluster_key_idxes should be [2, 1, 3] |
210 | 1 | for (const auto& cid : tablet_schema.cluster_key_uids()) { |
211 | 1 | auto idx = tablet_schema.field_index(cid); |
212 | 3 | for (auto i = 0; i < key_columns.size(); ++i) { |
213 | 3 | if (idx == key_columns[i]) { |
214 | 1 | key_group_cluster_key_idxes->emplace_back(i); |
215 | 1 | break; |
216 | 1 | } |
217 | 3 | } |
218 | 1 | } |
219 | 1 | } |
220 | 56 | } |
221 | 129 | VLOG_NOTICE << "sequence_col_idx=" << sequence_col_idx |
222 | 68 | << ", delete_sign_idx=" << delete_sign_idx; |
223 | | // for duplicate no keys |
224 | 129 | if (!key_columns.empty()) { |
225 | 110 | column_groups->emplace_back(key_columns); |
226 | 110 | } |
227 | | |
228 | 129 | std::vector<uint32_t> value_columns; |
229 | | |
230 | 1.14k | for (size_t i = num_key_cols; i < total_cols; ++i) { |
231 | 1.01k | if (i == sequence_col_idx || i == delete_sign_idx || |
232 | 1.01k | key_columns.end() != std::find(key_columns.begin(), key_columns.end(), i)) { |
233 | 55 | continue; |
234 | 55 | } |
235 | | |
236 | 962 | if (!value_columns.empty() && value_columns.size() % num_columns_per_group == 0) { |
237 | 143 | column_groups->push_back(value_columns); |
238 | 143 | value_columns.clear(); |
239 | 143 | } |
240 | 962 | value_columns.push_back(cast_set<uint32_t>(i)); |
241 | 962 | } |
242 | | |
243 | 129 | if (!value_columns.empty()) { |
244 | 128 | column_groups->push_back(value_columns); |
245 | 128 | } |
246 | 129 | } |
247 | | |
248 | | Status Merger::vertical_compact_one_group( |
249 | | BaseTabletSPtr tablet, ReaderType reader_type, const TabletSchema& tablet_schema, |
250 | | bool is_key, const std::vector<uint32_t>& column_group, RowSourcesBuffer* row_source_buf, |
251 | | const std::vector<RowsetReaderSharedPtr>& src_rowset_readers, |
252 | | RowsetWriter* dst_rowset_writer, uint32_t max_rows_per_segment, Statistics* stats_output, |
253 | | std::vector<uint32_t> key_group_cluster_key_idxes, int64_t batch_size, |
254 | | CompactionSampleInfo* sample_info, VerticalCompactionContextStats* context_stats, |
255 | 357 | bool enable_sparse_optimization) { |
256 | | // build tablet reader |
257 | 357 | VLOG_NOTICE << "vertical compact one group, max_rows_per_segment=" << max_rows_per_segment; |
258 | 357 | VerticalBlockReader reader(row_source_buf, context_stats); |
259 | 357 | TabletReader::ReaderParams reader_params; |
260 | 357 | reader_params.is_key_column_group = is_key; |
261 | 357 | reader_params.key_group_cluster_key_idxes = key_group_cluster_key_idxes; |
262 | 357 | reader_params.tablet = tablet; |
263 | 357 | reader_params.reader_type = reader_type; |
264 | 357 | reader_params.enable_sparse_optimization = enable_sparse_optimization; |
265 | | |
266 | 357 | TabletReadSource read_source; |
267 | 357 | read_source.rs_splits.reserve(src_rowset_readers.size()); |
268 | 1.11k | for (const RowsetReaderSharedPtr& rs_reader : src_rowset_readers) { |
269 | 1.11k | read_source.rs_splits.emplace_back(rs_reader); |
270 | 1.11k | } |
271 | 357 | read_source.fill_delete_predicates(); |
272 | 357 | reader_params.set_read_source(std::move(read_source)); |
273 | | |
274 | 357 | reader_params.version = dst_rowset_writer->version(); |
275 | | |
276 | 357 | TabletSchemaSPtr merge_tablet_schema = std::make_shared<TabletSchema>(); |
277 | 357 | merge_tablet_schema->copy_from(tablet_schema); |
278 | | |
279 | 357 | for (auto& del_pred_rs : reader_params.delete_predicates) { |
280 | 125 | merge_tablet_schema->merge_dropped_columns(*del_pred_rs->tablet_schema()); |
281 | 125 | } |
282 | | |
283 | 357 | reader_params.tablet_schema = merge_tablet_schema; |
284 | 357 | bool has_cluster_key = false; |
285 | 357 | if (!tablet->tablet_schema()->cluster_key_uids().empty()) { |
286 | 1 | reader_params.delete_bitmap = tablet->tablet_meta()->delete_bitmap_ptr(); |
287 | 1 | has_cluster_key = true; |
288 | 1 | } |
289 | | |
290 | 357 | if (is_key && stats_output && stats_output->rowid_conversion) { |
291 | 87 | reader_params.record_rowids = true; |
292 | 87 | reader_params.rowid_conversion = stats_output->rowid_conversion; |
293 | 87 | stats_output->rowid_conversion->set_dst_rowset_id(dst_rowset_writer->rowset_id()); |
294 | 87 | } |
295 | | |
296 | 357 | reader_params.return_columns = column_group; |
297 | 357 | reader_params.origin_return_columns = &reader_params.return_columns; |
298 | 357 | reader_params.batch_size = batch_size; |
299 | 357 | RETURN_IF_ERROR(reader.init(reader_params, sample_info)); |
300 | | |
301 | 356 | Block block = tablet_schema.create_block(reader_params.return_columns); |
302 | 356 | size_t output_rows = 0; |
303 | 356 | bool eof = false; |
304 | 6.66k | while (!eof && !ExecEnv::GetInstance()->storage_engine().stopped()) { |
305 | 6.30k | auto tablet_state = tablet->tablet_state(); |
306 | 6.30k | if (tablet_state != TABLET_RUNNING && tablet_state != TABLET_NOTREADY) { |
307 | 0 | tablet->clear_cache(); |
308 | 0 | return Status::Error<INTERNAL_ERROR>("tablet {} is not used any more", |
309 | 0 | tablet->tablet_id()); |
310 | 0 | } |
311 | | // Read one block from block reader |
312 | 6.30k | RETURN_NOT_OK_STATUS_WITH_WARN(reader.next_block_with_aggregation(&block, &eof), |
313 | 6.30k | "failed to read next block when merging rowsets of tablet " + |
314 | 6.30k | std::to_string(tablet->tablet_id())); |
315 | 6.30k | RETURN_NOT_OK_STATUS_WITH_WARN( |
316 | 6.30k | dst_rowset_writer->add_columns(&block, column_group, is_key, max_rows_per_segment, |
317 | 6.30k | has_cluster_key), |
318 | 6.30k | "failed to write block when merging rowsets of tablet " + |
319 | 6.30k | std::to_string(tablet->tablet_id())); |
320 | | |
321 | 6.30k | if (is_key && reader_params.record_rowids && block.rows() > 0) { |
322 | 1.17k | std::vector<uint32_t> segment_num_rows; |
323 | 1.17k | RETURN_IF_ERROR(dst_rowset_writer->get_segment_num_rows(&segment_num_rows)); |
324 | 1.17k | stats_output->rowid_conversion->add(reader.current_block_row_locations(), |
325 | 1.17k | segment_num_rows); |
326 | 1.17k | } |
327 | 6.30k | output_rows += block.rows(); |
328 | 6.30k | block.clear_column_data(); |
329 | 6.30k | } |
330 | 356 | if (ExecEnv::GetInstance()->storage_engine().stopped()) { |
331 | 0 | return Status::Error<INTERNAL_ERROR>("tablet {} failed to do compaction, engine stopped", |
332 | 0 | tablet->tablet_id()); |
333 | 0 | } |
334 | | |
335 | 356 | if (stats_output != nullptr) { |
336 | 356 | if (is_key) { |
337 | 116 | stats_output->output_rows = output_rows; |
338 | 116 | stats_output->merged_rows = reader.merged_rows(); |
339 | 116 | stats_output->filtered_rows = reader.filtered_rows(); |
340 | 116 | } |
341 | 356 | stats_output->bytes_read_from_local = reader.stats().file_cache_stats.bytes_read_from_local; |
342 | 356 | stats_output->bytes_read_from_remote = |
343 | 356 | reader.stats().file_cache_stats.bytes_read_from_remote; |
344 | 356 | stats_output->cached_bytes_total = reader.stats().file_cache_stats.bytes_write_into_cache; |
345 | 356 | if (config::is_cloud_mode()) { |
346 | 0 | stats_output->cloud_local_read_time = |
347 | 0 | reader.stats().file_cache_stats.local_io_timer / 1000; |
348 | 0 | stats_output->cloud_remote_read_time = |
349 | 0 | reader.stats().file_cache_stats.remote_io_timer / 1000; |
350 | 0 | } |
351 | 356 | } |
352 | 356 | RETURN_IF_ERROR(dst_rowset_writer->flush_columns(is_key)); |
353 | | |
354 | 356 | return Status::OK(); |
355 | 356 | } |
356 | | |
357 | | // for segcompaction |
358 | | Status Merger::vertical_compact_one_group( |
359 | | int64_t tablet_id, ReaderType reader_type, const TabletSchema& tablet_schema, bool is_key, |
360 | | const std::vector<uint32_t>& column_group, RowSourcesBuffer* row_source_buf, |
361 | | VerticalBlockReader& src_block_reader, segment_v2::SegmentWriter& dst_segment_writer, |
362 | | Statistics* stats_output, uint64_t* index_size, KeyBoundsPB& key_bounds, |
363 | 24 | SimpleRowIdConversion* rowid_conversion) { |
364 | | // TODO: record_rowids |
365 | 24 | Block block = tablet_schema.create_block(column_group); |
366 | 24 | size_t output_rows = 0; |
367 | 24 | bool eof = false; |
368 | 142 | while (!eof && !ExecEnv::GetInstance()->storage_engine().stopped()) { |
369 | | // Read one block from block reader |
370 | 118 | RETURN_NOT_OK_STATUS_WITH_WARN(src_block_reader.next_block_with_aggregation(&block, &eof), |
371 | 118 | "failed to read next block when merging rowsets of tablet " + |
372 | 118 | std::to_string(tablet_id)); |
373 | 118 | if (!block.rows()) { |
374 | 0 | break; |
375 | 0 | } |
376 | 118 | RETURN_NOT_OK_STATUS_WITH_WARN(dst_segment_writer.append_block(&block, 0, block.rows()), |
377 | 118 | "failed to write block when merging rowsets of tablet " + |
378 | 118 | std::to_string(tablet_id)); |
379 | | |
380 | 118 | if (is_key && rowid_conversion != nullptr) { |
381 | 31 | rowid_conversion->add(src_block_reader.current_block_row_locations()); |
382 | 31 | } |
383 | 118 | output_rows += block.rows(); |
384 | 118 | block.clear_column_data(); |
385 | 118 | } |
386 | 24 | if (ExecEnv::GetInstance()->storage_engine().stopped()) { |
387 | 0 | return Status::Error<INTERNAL_ERROR>("tablet {} failed to do compaction, engine stopped", |
388 | 0 | tablet_id); |
389 | 0 | } |
390 | | |
391 | 24 | if (stats_output != nullptr) { |
392 | 24 | if (is_key) { |
393 | 12 | stats_output->output_rows = output_rows; |
394 | 12 | stats_output->merged_rows = src_block_reader.merged_rows(); |
395 | 12 | stats_output->filtered_rows = src_block_reader.filtered_rows(); |
396 | 12 | } |
397 | 24 | stats_output->bytes_read_from_local = |
398 | 24 | src_block_reader.stats().file_cache_stats.bytes_read_from_local; |
399 | 24 | stats_output->bytes_read_from_remote = |
400 | 24 | src_block_reader.stats().file_cache_stats.bytes_read_from_remote; |
401 | 24 | stats_output->cached_bytes_total = |
402 | 24 | src_block_reader.stats().file_cache_stats.bytes_write_into_cache; |
403 | 24 | } |
404 | | |
405 | | // segcompaction produce only one segment at once |
406 | 24 | RETURN_IF_ERROR(dst_segment_writer.finalize_columns_data()); |
407 | 24 | RETURN_IF_ERROR(dst_segment_writer.finalize_columns_index(index_size)); |
408 | | |
409 | 24 | if (is_key) { |
410 | 12 | Slice min_key = dst_segment_writer.min_encoded_key(); |
411 | 12 | Slice max_key = dst_segment_writer.max_encoded_key(); |
412 | 12 | DCHECK_LE(min_key.compare(max_key), 0); |
413 | 12 | key_bounds.set_min_key(min_key.to_string()); |
414 | 12 | key_bounds.set_max_key(max_key.to_string()); |
415 | 12 | } |
416 | | |
417 | 24 | return Status::OK(); |
418 | 24 | } |
419 | | |
420 | | int64_t estimate_batch_size(int group_index, BaseTabletSPtr tablet, int64_t way_cnt, |
421 | | ReaderType reader_type, int64_t group_per_row_from_footer, |
422 | 153 | bool footer_fallback) { |
423 | 153 | auto& sample_info_lock = tablet->get_sample_info_lock(reader_type); |
424 | 153 | auto& sample_infos = tablet->get_sample_infos(reader_type); |
425 | 153 | std::unique_lock<std::mutex> lock(sample_info_lock); |
426 | 153 | CompactionSampleInfo info = sample_infos[group_index]; |
427 | 153 | if (way_cnt <= 0) { |
428 | 0 | LOG(INFO) << "estimate batch size for vertical compaction, tablet id: " |
429 | 0 | << tablet->tablet_id() << " way cnt: " << way_cnt; |
430 | 0 | return 4096 - 32; |
431 | 0 | } |
432 | 153 | int64_t block_mem_limit = config::compaction_memory_bytes_limit / way_cnt; |
433 | 153 | if (tablet->last_compaction_status.is<ErrorCode::MEM_LIMIT_EXCEEDED>()) { |
434 | 0 | block_mem_limit /= 4; |
435 | 0 | } |
436 | | |
437 | 153 | int64_t group_data_size = 0; |
438 | 153 | if (info.group_data_size > 0 && info.bytes > 0 && info.rows > 0) { |
439 | 0 | double smoothing_factor = 0.5; |
440 | 0 | group_data_size = |
441 | 0 | int64_t((cast_set<double>(info.group_data_size) * (1 - smoothing_factor)) + |
442 | 0 | (cast_set<double>(info.bytes / info.rows) * smoothing_factor)); |
443 | 0 | sample_infos[group_index].group_data_size = group_data_size; |
444 | 153 | } else if (info.group_data_size > 0 && (info.bytes <= 0 || info.rows <= 0)) { |
445 | 0 | group_data_size = info.group_data_size; |
446 | 153 | } else if (info.group_data_size <= 0 && info.bytes > 0 && info.rows > 0) { |
447 | 0 | group_data_size = info.bytes / info.rows; |
448 | 0 | sample_infos[group_index].group_data_size = group_data_size; |
449 | 153 | } else { |
450 | | // No historical sampling data available. |
451 | | // Try to use raw_data_bytes from segment footer for a better estimate. |
452 | 153 | if (!footer_fallback && group_per_row_from_footer > 0) { |
453 | 123 | int64_t batch_size = block_mem_limit / group_per_row_from_footer; |
454 | 123 | int64_t res = std::max(std::min(batch_size, int64_t(4096 - 32)), int64_t(32L)); |
455 | 123 | LOG(INFO) << "estimate batch size from footer for vertical compaction, tablet id: " |
456 | 123 | << tablet->tablet_id() |
457 | 123 | << " group_per_row_from_footer: " << group_per_row_from_footer |
458 | 123 | << " way cnt: " << way_cnt << " batch size: " << res; |
459 | 123 | return res; |
460 | 123 | } |
461 | 153 | LOG(INFO) << "estimate batch size for vertical compaction, tablet id: " |
462 | 30 | << tablet->tablet_id() << " group data size: " << info.group_data_size |
463 | 30 | << " row num: " << info.rows << " consume bytes: " << info.bytes |
464 | 30 | << " footer_fallback: " << footer_fallback; |
465 | 30 | return 1024 - 32; |
466 | 153 | } |
467 | | |
468 | 0 | if (group_data_size <= 0) { |
469 | 0 | LOG(WARNING) << "estimate batch size for vertical compaction, tablet id: " |
470 | 0 | << tablet->tablet_id() << " unexpected group data size: " << group_data_size; |
471 | 0 | return 4096 - 32; |
472 | 0 | } |
473 | | |
474 | 0 | sample_infos[group_index].bytes = 0; |
475 | 0 | sample_infos[group_index].rows = 0; |
476 | |
|
477 | 0 | int64_t batch_size = block_mem_limit / group_data_size; |
478 | 0 | int64_t res = std::max(std::min(batch_size, int64_t(4096 - 32)), int64_t(32L)); |
479 | 0 | LOG(INFO) << "estimate batch size for vertical compaction, tablet id: " << tablet->tablet_id() |
480 | 0 | << " group data size: " << info.group_data_size << " row num: " << info.rows |
481 | 0 | << " consume bytes: " << info.bytes << " way cnt: " << way_cnt |
482 | 0 | << " batch size: " << res; |
483 | 0 | return res; |
484 | 0 | } |
485 | | |
486 | | // steps to do vertical merge: |
487 | | // 1. split columns into column groups |
488 | | // 2. compact groups one by one, generate a row_source_buf when compact key group |
489 | | // and use this row_source_buf to compact value column groups |
490 | | // 3. build output rowset |
491 | | Status Merger::vertical_merge_rowsets(BaseTabletSPtr tablet, ReaderType reader_type, |
492 | | const TabletSchema& tablet_schema, |
493 | | const std::vector<RowsetReaderSharedPtr>& src_rowset_readers, |
494 | | RowsetWriter* dst_rowset_writer, |
495 | | uint32_t max_rows_per_segment, int64_t merge_way_num, |
496 | | Statistics* stats_output, |
497 | 117 | VerticalCompactionProgressCallback progress_cb) { |
498 | 117 | LOG(INFO) << "Start to do vertical compaction, tablet_id: " << tablet->tablet_id(); |
499 | 117 | VerticalCompactionContextStats context_stats; |
500 | 117 | Defer log_context_stats {[&] { |
501 | 117 | DCHECK_EQ(context_stats.active_segment_contexts, 0); |
502 | 117 | LOG(INFO) << "Vertical compaction segment context statistics, tablet_id: " |
503 | 117 | << tablet->tablet_id() << ", vertical_compaction_active_segment_contexts_peak: " |
504 | 117 | << context_stats.active_segment_contexts_peak; |
505 | 117 | }}; |
506 | 117 | std::vector<std::vector<uint32_t>> column_groups; |
507 | 117 | std::vector<uint32_t> key_group_cluster_key_idxes; |
508 | | // If BE config vertical_compaction_num_columns_per_group has been modified from |
509 | | // its default value (5), use the BE config; otherwise use the tablet meta value. |
510 | 117 | constexpr int32_t default_num_columns_per_group = 5; |
511 | 117 | int32_t num_columns_per_group = |
512 | 117 | config::vertical_compaction_num_columns_per_group != default_num_columns_per_group |
513 | 117 | ? config::vertical_compaction_num_columns_per_group |
514 | 117 | : tablet->tablet_meta()->vertical_compaction_num_columns_per_group(); |
515 | | |
516 | 117 | DBUG_EXECUTE_IF("Merger.vertical_merge_rowsets.check_num_columns_per_group", { |
517 | 117 | auto expected_value = DebugPoints::instance()->get_debug_param_or_default<int32_t>( |
518 | 117 | "Merger.vertical_merge_rowsets.check_num_columns_per_group", "expected_value", -1); |
519 | 117 | auto expected_tablet_id = DebugPoints::instance()->get_debug_param_or_default<int64_t>( |
520 | 117 | "Merger.vertical_merge_rowsets.check_num_columns_per_group", "tablet_id", -1); |
521 | 117 | if (expected_tablet_id != -1 && expected_tablet_id == tablet->tablet_id()) { |
522 | 117 | if (expected_value != -1 && expected_value != num_columns_per_group) { |
523 | 117 | LOG(FATAL) << "DEBUG_POINT CHECK FAILED: expected num_columns_per_group=" |
524 | 117 | << expected_value << " but got " << num_columns_per_group |
525 | 117 | << " for tablet_id=" << tablet->tablet_id(); |
526 | 117 | } else { |
527 | 117 | LOG(INFO) << "DEBUG_POINT CHECK PASSED: num_columns_per_group=" |
528 | 117 | << num_columns_per_group << ", tablet_id=" << tablet->tablet_id(); |
529 | 117 | } |
530 | 117 | } |
531 | 117 | }); |
532 | | |
533 | 117 | vertical_split_columns(tablet_schema, &column_groups, &key_group_cluster_key_idxes, |
534 | 117 | num_columns_per_group); |
535 | | |
536 | 117 | if (progress_cb) { |
537 | 25 | progress_cb(column_groups.size(), 0); |
538 | 25 | } |
539 | | |
540 | | // Calculate total rows for density calculation after compaction |
541 | 117 | int64_t total_rows = 0; |
542 | 339 | for (const auto& rs_reader : src_rowset_readers) { |
543 | 339 | total_rows += rs_reader->rowset()->rowset_meta()->num_rows(); |
544 | 339 | } |
545 | | |
546 | | // Use historical density for sparse wide table optimization |
547 | | // density = (total_cells - null_cells) / total_cells, smaller means more sparse |
548 | | // When density <= threshold, enable sparse optimization |
549 | | // threshold = 0 means disable, 1 means always enable (default) |
550 | 117 | bool enable_sparse_optimization = false; |
551 | 117 | if (config::sparse_column_compaction_threshold_percent > 0 && |
552 | 117 | tablet->keys_type() == KeysType::UNIQUE_KEYS) { |
553 | 46 | double density = tablet->compaction_density.load(); |
554 | 46 | enable_sparse_optimization = density <= config::sparse_column_compaction_threshold_percent; |
555 | | |
556 | 46 | LOG(INFO) << "Vertical compaction sparse optimization check: tablet_id=" |
557 | 46 | << tablet->tablet_id() << ", density=" << density |
558 | 46 | << ", threshold=" << config::sparse_column_compaction_threshold_percent |
559 | 46 | << ", total_rows=" << total_rows |
560 | 46 | << ", num_columns=" << tablet_schema.num_columns() |
561 | 46 | << ", total_cells=" << total_rows * tablet_schema.num_columns() |
562 | 46 | << ", enable_sparse_optimization=" << enable_sparse_optimization; |
563 | 46 | } |
564 | | |
565 | 117 | RowSourcesBuffer row_sources_buf(tablet->tablet_id(), dst_rowset_writer->context().tablet_path, |
566 | 117 | reader_type); |
567 | 117 | Merger::Statistics total_stats; |
568 | 117 | if (stats_output != nullptr) { |
569 | 117 | total_stats.rowid_conversion = stats_output->rowid_conversion; |
570 | 117 | } |
571 | 117 | auto& sample_info_lock = tablet->get_sample_info_lock(reader_type); |
572 | 117 | auto& sample_infos = tablet->get_sample_infos(reader_type); |
573 | 117 | { |
574 | 117 | std::unique_lock<std::mutex> lock(sample_info_lock); |
575 | 117 | sample_infos.resize(column_groups.size()); |
576 | 117 | } |
577 | | // Collect per-column raw_data_bytes from segment footer for first-time batch size estimation. |
578 | | // raw_data_bytes is the original data size before encoding, close to runtime Block::bytes(). |
579 | | // Only collect when needed: skip if manual batch_size override is set, or if ALL groups |
580 | | // already have historical sampling data. Use per-group granularity so that schema evolution |
581 | | // (new groups without history) still gets footer-based estimation. |
582 | 117 | struct ColumnRawSizeInfo { |
583 | 117 | int64_t total_raw_bytes = 0; |
584 | 117 | int64_t rows_with_data = 0; |
585 | 117 | }; |
586 | 117 | std::unordered_map<int32_t, ColumnRawSizeInfo> column_raw_sizes; |
587 | 117 | bool need_footer_collection = false; |
588 | 117 | if (config::compaction_batch_size == -1) { |
589 | 77 | std::unique_lock<std::mutex> lock(sample_info_lock); |
590 | 77 | for (const auto& info : sample_infos) { |
591 | 77 | if (info.group_data_size <= 0 && info.bytes <= 0 && info.rows <= 0) { |
592 | 77 | need_footer_collection = true; |
593 | 77 | break; |
594 | 77 | } |
595 | 77 | } |
596 | 77 | } |
597 | 117 | if (need_footer_collection) { |
598 | 211 | for (const auto& rs_reader : src_rowset_readers) { |
599 | 211 | auto beta_rowset = std::dynamic_pointer_cast<BetaRowset>(rs_reader->rowset()); |
600 | 211 | if (!beta_rowset) { |
601 | 0 | continue; |
602 | 0 | } |
603 | 211 | std::vector<segment_v2::SegmentSharedPtr> segments; |
604 | 211 | auto st = beta_rowset->load_segments(&segments); |
605 | 211 | if (!st.ok()) { |
606 | 0 | LOG(WARNING) << "Failed to load segments for footer raw_data_bytes collection" |
607 | 0 | << ", tablet_id: " << tablet->tablet_id() |
608 | 0 | << ", rowset_id: " << beta_rowset->rowset_id() << ", status: " << st; |
609 | 0 | continue; |
610 | 0 | } |
611 | 313 | for (const auto& segment : segments) { |
612 | 313 | int64_t row_count = segment->num_rows(); |
613 | 313 | auto collect_st = segment->traverse_column_meta_pbs( |
614 | 1.43k | [&](const segment_v2::ColumnMetaPB& meta) { |
615 | 1.43k | int32_t uid = meta.unique_id(); |
616 | 1.43k | if (uid >= 0 && meta.has_raw_data_bytes()) { |
617 | 928 | auto& info = column_raw_sizes[uid]; |
618 | 928 | info.total_raw_bytes += meta.raw_data_bytes(); |
619 | 928 | info.rows_with_data += row_count; |
620 | 928 | } |
621 | 1.43k | }); |
622 | 313 | if (!collect_st.ok()) { |
623 | 0 | LOG(WARNING) << "Failed to traverse column meta for footer collection" |
624 | 0 | << ", tablet_id: " << tablet->tablet_id() |
625 | 0 | << ", status: " << collect_st; |
626 | 0 | } |
627 | 313 | } |
628 | 211 | } |
629 | 77 | } |
630 | | |
631 | | // Pre-compute per-row estimate for each column group from footer data. |
632 | 117 | std::vector<int64_t> group_per_row_from_footer(column_groups.size(), 0); |
633 | 117 | std::vector<bool> group_footer_fallback(column_groups.size(), false); |
634 | 474 | for (size_t i = 0; i < column_groups.size(); ++i) { |
635 | 357 | int64_t group_per_row = 0; |
636 | 357 | bool need_fallback = false; |
637 | 396 | for (uint32_t col_ordinal : column_groups[i]) { |
638 | 396 | const auto& col = tablet_schema.column(col_ordinal); |
639 | 396 | int32_t uid = col.unique_id(); |
640 | | |
641 | | // Variant columns (root or subcolumn): raw_data_bytes is 0 (TODO in writer), |
642 | | // cannot estimate from footer, fallback to default for the entire group. |
643 | 396 | if (uid < 0 || col.is_variant_type()) { |
644 | 30 | need_fallback = true; |
645 | 30 | break; |
646 | 30 | } |
647 | | |
648 | | // Any column without footer data (e.g. legacy segments written before |
649 | | // raw_data_bytes existed) makes the group sample partial and unreliable. |
650 | | // Fall back to the default for the whole group instead of summing only |
651 | | // the columns we measured. |
652 | 366 | auto it = column_raw_sizes.find(uid); |
653 | 366 | if (it == column_raw_sizes.end() || it->second.rows_with_data <= 0) { |
654 | 204 | need_fallback = true; |
655 | 204 | break; |
656 | 204 | } |
657 | | |
658 | 162 | int64_t raw_per_row = it->second.total_raw_bytes / it->second.rows_with_data; |
659 | 162 | int64_t col_per_row = 0; |
660 | | |
661 | 162 | if (col.type() == FieldType::OLAP_FIELD_TYPE_ARRAY || |
662 | 162 | col.type() == FieldType::OLAP_FIELD_TYPE_MAP || |
663 | 162 | col.type() == FieldType::OLAP_FIELD_TYPE_STRUCT) { |
664 | | // Complex types: raw_data_bytes recursively aggregates sub-writers. |
665 | 0 | col_per_row = raw_per_row; |
666 | 162 | } else if (col.is_length_variable_type()) { |
667 | | // Variable-length scalar (VARCHAR/STRING/HLL/BITMAP/...): raw_per_row |
668 | | // is the average char payload across all rows; reader still pays an |
669 | | // 8-byte offset entry per row regardless of null-ness. |
670 | 1 | col_per_row = raw_per_row + 8; |
671 | 1 | if (col.is_nullable()) { |
672 | 0 | col_per_row += 1; // null map |
673 | 0 | } |
674 | 161 | } else { |
675 | | // Fixed-width scalar (INT/BIGINT/DOUBLE/DATE/...). |
676 | | // raw_data_bytes only counts non-null payload (append_nulls() does |
677 | | // not advance the page builder), but FileColumnIterator::next_batch |
678 | | // still calls ColumnNullable::insert_many_defaults() for null runs, |
679 | | // which grows the nested PODArray by N * type_size. So the runtime |
680 | | // per-row footprint is at least type_size, no matter how sparse. |
681 | 161 | int64_t type_size = field_type_size(col.type()); |
682 | 161 | col_per_row = std::max(raw_per_row, type_size); |
683 | 161 | if (col.is_nullable()) { |
684 | 1 | col_per_row += 1; // null map |
685 | 1 | } |
686 | 161 | } |
687 | | |
688 | 162 | group_per_row += col_per_row; |
689 | 162 | } |
690 | 357 | group_per_row_from_footer[i] = group_per_row; |
691 | 357 | group_footer_fallback[i] = need_fallback; |
692 | 357 | } |
693 | | |
694 | | // compact group one by one |
695 | 473 | for (auto i = 0; i < column_groups.size(); ++i) { |
696 | 357 | VLOG_NOTICE << "row source size: " << row_sources_buf.total_size(); |
697 | 357 | bool is_key = (i == 0); |
698 | 357 | int64_t batch_size = config::compaction_batch_size != -1 |
699 | 357 | ? config::compaction_batch_size |
700 | 357 | : estimate_batch_size(i, tablet, merge_way_num, reader_type, |
701 | 153 | group_per_row_from_footer[i], |
702 | 153 | group_footer_fallback[i]); |
703 | 357 | CompactionSampleInfo sample_info; |
704 | 357 | Merger::Statistics group_stats; |
705 | 357 | group_stats.rowid_conversion = total_stats.rowid_conversion; |
706 | 357 | Merger::Statistics* group_stats_ptr = stats_output != nullptr ? &group_stats : nullptr; |
707 | 357 | Status st = vertical_compact_one_group( |
708 | 357 | tablet, reader_type, tablet_schema, is_key, column_groups[i], &row_sources_buf, |
709 | 357 | src_rowset_readers, dst_rowset_writer, max_rows_per_segment, group_stats_ptr, |
710 | 357 | key_group_cluster_key_idxes, batch_size, &sample_info, &context_stats, |
711 | 357 | enable_sparse_optimization); |
712 | 357 | { |
713 | 357 | std::unique_lock<std::mutex> lock(sample_info_lock); |
714 | 357 | sample_infos[i] = sample_info; |
715 | 357 | } |
716 | 357 | RETURN_IF_ERROR(st); |
717 | 356 | if (stats_output != nullptr) { |
718 | 356 | total_stats.bytes_read_from_local += group_stats.bytes_read_from_local; |
719 | 356 | total_stats.bytes_read_from_remote += group_stats.bytes_read_from_remote; |
720 | 356 | total_stats.cached_bytes_total += group_stats.cached_bytes_total; |
721 | 356 | total_stats.cloud_local_read_time += group_stats.cloud_local_read_time; |
722 | 356 | total_stats.cloud_remote_read_time += group_stats.cloud_remote_read_time; |
723 | 356 | if (is_key) { |
724 | 116 | total_stats.output_rows = group_stats.output_rows; |
725 | 116 | total_stats.merged_rows = group_stats.merged_rows; |
726 | 116 | total_stats.filtered_rows = group_stats.filtered_rows; |
727 | 116 | total_stats.rowid_conversion = group_stats.rowid_conversion; |
728 | 116 | } |
729 | 356 | } |
730 | 356 | if (progress_cb) { |
731 | 53 | progress_cb(column_groups.size(), i + 1); |
732 | 53 | } |
733 | 356 | if (is_key) { |
734 | 116 | RETURN_IF_ERROR(row_sources_buf.flush()); |
735 | 116 | } |
736 | 356 | RETURN_IF_ERROR(row_sources_buf.seek_to_begin()); |
737 | 356 | } |
738 | | |
739 | | // Calculate and store density for next compaction's sparse optimization threshold |
740 | | // density = (total_cells - total_null_count) / total_cells |
741 | | // Smaller density means more sparse |
742 | 116 | { |
743 | 116 | std::unique_lock<std::mutex> lock(sample_info_lock); |
744 | 116 | int64_t total_null_count = 0; |
745 | 356 | for (const auto& info : sample_infos) { |
746 | 356 | total_null_count += info.null_count; |
747 | 356 | } |
748 | 116 | int64_t total_cells = total_rows * tablet_schema.num_columns(); |
749 | 116 | if (total_cells > 0) { |
750 | 115 | double density = static_cast<double>(total_cells - total_null_count) / |
751 | 115 | static_cast<double>(total_cells); |
752 | 115 | tablet->compaction_density.store(density); |
753 | 115 | LOG(INFO) << "Vertical compaction density update: tablet_id=" << tablet->tablet_id() |
754 | 115 | << ", total_cells=" << total_cells |
755 | 115 | << ", total_null_count=" << total_null_count << ", density=" << density; |
756 | 115 | } |
757 | 116 | } |
758 | | |
759 | | // finish compact, build output rowset |
760 | 116 | VLOG_NOTICE << "finish compact groups"; |
761 | 116 | RETURN_IF_ERROR(dst_rowset_writer->final_flush()); |
762 | | |
763 | 116 | if (stats_output != nullptr) { |
764 | 116 | *stats_output = total_stats; |
765 | 116 | } |
766 | | |
767 | 116 | return Status::OK(); |
768 | 116 | } |
769 | | #include "common/compile_check_end.h" |
770 | | } // namespace doris |