/root/doris/be/src/olap/merger.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/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 <utility> |
34 | | #include <vector> |
35 | | |
36 | | #include "common/config.h" |
37 | | #include "common/logging.h" |
38 | | #include "common/status.h" |
39 | | #include "olap/base_tablet.h" |
40 | | #include "olap/iterators.h" |
41 | | #include "olap/olap_common.h" |
42 | | #include "olap/olap_define.h" |
43 | | #include "olap/rowid_conversion.h" |
44 | | #include "olap/rowset/rowset.h" |
45 | | #include "olap/rowset/rowset_meta.h" |
46 | | #include "olap/rowset/rowset_writer.h" |
47 | | #include "olap/rowset/segment_v2/segment_writer.h" |
48 | | #include "olap/storage_engine.h" |
49 | | #include "olap/tablet.h" |
50 | | #include "olap/tablet_fwd.h" |
51 | | #include "olap/tablet_meta.h" |
52 | | #include "olap/tablet_reader.h" |
53 | | #include "olap/utils.h" |
54 | | #include "util/slice.h" |
55 | | #include "vec/core/block.h" |
56 | | #include "vec/olap/block_reader.h" |
57 | | #include "vec/olap/vertical_block_reader.h" |
58 | | #include "vec/olap/vertical_merge_iterator.h" |
59 | | |
60 | | namespace doris { |
61 | | |
62 | | Status Merger::vmerge_rowsets(BaseTabletSPtr tablet, ReaderType reader_type, |
63 | | const TabletSchema& cur_tablet_schema, |
64 | | const std::vector<RowsetReaderSharedPtr>& src_rowset_readers, |
65 | 48 | RowsetWriter* dst_rowset_writer, Statistics* stats_output) { |
66 | 48 | vectorized::BlockReader reader; |
67 | 48 | TabletReader::ReaderParams reader_params; |
68 | 48 | reader_params.tablet = tablet; |
69 | 48 | reader_params.reader_type = reader_type; |
70 | | |
71 | 48 | TabletReader::ReadSource read_source; |
72 | 48 | read_source.rs_splits.reserve(src_rowset_readers.size()); |
73 | 144 | for (const RowsetReaderSharedPtr& rs_reader : src_rowset_readers) { |
74 | 144 | read_source.rs_splits.emplace_back(rs_reader); |
75 | 144 | } |
76 | 48 | read_source.fill_delete_predicates(); |
77 | 48 | reader_params.set_read_source(std::move(read_source)); |
78 | | |
79 | 48 | reader_params.version = dst_rowset_writer->version(); |
80 | | |
81 | 48 | TabletSchemaSPtr merge_tablet_schema = std::make_shared<TabletSchema>(); |
82 | 48 | merge_tablet_schema->copy_from(cur_tablet_schema); |
83 | | |
84 | | // Merge the columns in delete predicate that not in latest schema in to current tablet schema |
85 | 48 | for (auto& del_pred_rs : reader_params.delete_predicates) { |
86 | 24 | merge_tablet_schema->merge_dropped_columns(*del_pred_rs->tablet_schema()); |
87 | 24 | } |
88 | 48 | reader_params.tablet_schema = merge_tablet_schema; |
89 | 48 | if (!tablet->tablet_schema()->cluster_key_idxes().empty()) { |
90 | 0 | reader_params.delete_bitmap = &tablet->tablet_meta()->delete_bitmap(); |
91 | 0 | } |
92 | | |
93 | 48 | if (stats_output && stats_output->rowid_conversion) { |
94 | 48 | reader_params.record_rowids = true; |
95 | 48 | reader_params.rowid_conversion = stats_output->rowid_conversion; |
96 | 48 | stats_output->rowid_conversion->set_dst_rowset_id(dst_rowset_writer->rowset_id()); |
97 | 48 | } |
98 | | |
99 | 48 | reader_params.return_columns.resize(cur_tablet_schema.num_columns()); |
100 | 48 | std::iota(reader_params.return_columns.begin(), reader_params.return_columns.end(), 0); |
101 | 48 | reader_params.origin_return_columns = &reader_params.return_columns; |
102 | 48 | RETURN_IF_ERROR(reader.init(reader_params)); |
103 | | |
104 | 48 | vectorized::Block block = cur_tablet_schema.create_block(reader_params.return_columns); |
105 | 48 | size_t output_rows = 0; |
106 | 48 | bool eof = false; |
107 | 626 | while (!eof && !ExecEnv::GetInstance()->storage_engine().stopped()) { |
108 | 578 | auto tablet_state = tablet->tablet_state(); |
109 | 578 | if (tablet_state != TABLET_RUNNING && tablet_state != TABLET_NOTREADY) { |
110 | 0 | tablet->clear_cache(); |
111 | 0 | return Status::Error<INTERNAL_ERROR>("tablet {} is not used any more", |
112 | 0 | tablet->tablet_id()); |
113 | 0 | } |
114 | | |
115 | | // Read one block from block reader |
116 | 578 | RETURN_NOT_OK_STATUS_WITH_WARN(reader.next_block_with_aggregation(&block, &eof), |
117 | 578 | "failed to read next block when merging rowsets of tablet " + |
118 | 578 | std::to_string(tablet->tablet_id())); |
119 | 578 | RETURN_NOT_OK_STATUS_WITH_WARN(dst_rowset_writer->add_block(&block), |
120 | 578 | "failed to write block when merging rowsets of tablet " + |
121 | 578 | std::to_string(tablet->tablet_id())); |
122 | | |
123 | 578 | if (reader_params.record_rowids && block.rows() > 0) { |
124 | 578 | std::vector<uint32_t> segment_num_rows; |
125 | 578 | RETURN_IF_ERROR(dst_rowset_writer->get_segment_num_rows(&segment_num_rows)); |
126 | 578 | stats_output->rowid_conversion->add(reader.current_block_row_locations(), |
127 | 578 | segment_num_rows); |
128 | 578 | } |
129 | | |
130 | 578 | output_rows += block.rows(); |
131 | 578 | block.clear_column_data(); |
132 | 578 | } |
133 | 48 | if (ExecEnv::GetInstance()->storage_engine().stopped()) { |
134 | 0 | return Status::Error<INTERNAL_ERROR>("tablet {} failed to do compaction, engine stopped", |
135 | 0 | tablet->tablet_id()); |
136 | 0 | } |
137 | | |
138 | 48 | if (stats_output != nullptr) { |
139 | 48 | stats_output->output_rows = output_rows; |
140 | 48 | stats_output->merged_rows = reader.merged_rows(); |
141 | 48 | stats_output->filtered_rows = reader.filtered_rows(); |
142 | 48 | } |
143 | | |
144 | 48 | RETURN_NOT_OK_STATUS_WITH_WARN(dst_rowset_writer->flush(), |
145 | 48 | "failed to flush rowset when merging rowsets of tablet " + |
146 | 48 | std::to_string(tablet->tablet_id())); |
147 | | |
148 | 48 | return Status::OK(); |
149 | 48 | } |
150 | | |
151 | | // split columns into several groups, make sure all keys in one group |
152 | | // unique_key should consider sequence&delete column |
153 | | void Merger::vertical_split_columns(const TabletSchema& tablet_schema, |
154 | | std::vector<std::vector<uint32_t>>* column_groups, |
155 | 60 | std::vector<uint32_t>* key_group_cluster_key_idxes) { |
156 | 60 | uint32_t num_key_cols = tablet_schema.num_key_columns(); |
157 | 60 | uint32_t total_cols = tablet_schema.num_columns(); |
158 | 60 | std::vector<uint32_t> key_columns; |
159 | 126 | for (auto i = 0; i < num_key_cols; ++i) { |
160 | 66 | key_columns.emplace_back(i); |
161 | 66 | } |
162 | | // in unique key, sequence & delete sign column should merge with key columns |
163 | 60 | int32_t sequence_col_idx = -1; |
164 | 60 | int32_t delete_sign_idx = -1; |
165 | | // in key column compaction, seq_col real index is _num_key_columns |
166 | | // and delete_sign column is _block->columns() - 1 |
167 | 60 | if (tablet_schema.keys_type() == KeysType::UNIQUE_KEYS) { |
168 | 39 | if (tablet_schema.has_sequence_col()) { |
169 | 4 | sequence_col_idx = tablet_schema.sequence_col_idx(); |
170 | 4 | key_columns.emplace_back(sequence_col_idx); |
171 | 4 | } |
172 | 39 | delete_sign_idx = tablet_schema.field_index(DELETE_SIGN); |
173 | 39 | if (delete_sign_idx != -1) { |
174 | 33 | key_columns.emplace_back(delete_sign_idx); |
175 | 33 | } |
176 | 39 | if (!tablet_schema.cluster_key_idxes().empty()) { |
177 | 0 | for (const auto& cid : tablet_schema.cluster_key_idxes()) { |
178 | 0 | auto idx = tablet_schema.field_index(cid); |
179 | 0 | DCHECK(idx >= 0) << "could not find cluster key column with unique_id=" << cid |
180 | 0 | << " in tablet schema, table_id=" << tablet_schema.table_id(); |
181 | 0 | if (idx >= num_key_cols) { |
182 | 0 | key_columns.emplace_back(idx); |
183 | 0 | } |
184 | 0 | } |
185 | | // tablet schema unique ids: [1, 2, 5, 3, 6, 4], [1 2] is key columns |
186 | | // cluster key unique ids: [3, 1, 4] |
187 | | // the key_columns should be [0, 1, 3, 5] |
188 | | // the key_group_cluster_key_idxes should be [2, 1, 3] |
189 | 0 | for (const auto& cid : tablet_schema.cluster_key_idxes()) { |
190 | 0 | auto idx = tablet_schema.field_index(cid); |
191 | 0 | for (auto i = 0; i < key_columns.size(); ++i) { |
192 | 0 | if (idx == key_columns[i]) { |
193 | 0 | key_group_cluster_key_idxes->emplace_back(i); |
194 | 0 | break; |
195 | 0 | } |
196 | 0 | } |
197 | 0 | } |
198 | 0 | } |
199 | 39 | } |
200 | 60 | VLOG_NOTICE << "sequence_col_idx=" << sequence_col_idx |
201 | 0 | << ", delete_sign_idx=" << delete_sign_idx; |
202 | | // for duplicate no keys |
203 | 60 | if (!key_columns.empty()) { |
204 | 55 | column_groups->emplace_back(std::move(key_columns)); |
205 | 55 | } |
206 | | |
207 | 60 | std::vector<uint32_t> value_columns; |
208 | | |
209 | 168 | for (uint32_t i = num_key_cols; i < total_cols; ++i) { |
210 | 108 | if (i == sequence_col_idx || i == delete_sign_idx || |
211 | 108 | key_columns.end() != std::find(key_columns.begin(), key_columns.end(), i)) { |
212 | 37 | continue; |
213 | 37 | } |
214 | | |
215 | 71 | if (!value_columns.empty() && |
216 | 71 | value_columns.size() % config::vertical_compaction_num_columns_per_group == 0) { |
217 | 0 | column_groups->push_back(value_columns); |
218 | 0 | value_columns.clear(); |
219 | 0 | } |
220 | 71 | value_columns.push_back(i); |
221 | 71 | } |
222 | | |
223 | 60 | if (!value_columns.empty()) { |
224 | 60 | column_groups->push_back(value_columns); |
225 | 60 | } |
226 | 60 | } |
227 | | |
228 | | Status Merger::vertical_compact_one_group( |
229 | | BaseTabletSPtr tablet, ReaderType reader_type, const TabletSchema& tablet_schema, |
230 | | bool is_key, const std::vector<uint32_t>& column_group, |
231 | | vectorized::RowSourcesBuffer* row_source_buf, |
232 | | const std::vector<RowsetReaderSharedPtr>& src_rowset_readers, |
233 | | RowsetWriter* dst_rowset_writer, int64_t max_rows_per_segment, Statistics* stats_output, |
234 | | std::vector<uint32_t> key_group_cluster_key_idxes, int64_t batch_size, |
235 | 93 | CompactionSampleInfo* sample_info) { |
236 | | // build tablet reader |
237 | 93 | VLOG_NOTICE << "vertical compact one group, max_rows_per_segment=" << max_rows_per_segment; |
238 | 93 | vectorized::VerticalBlockReader reader(row_source_buf); |
239 | 93 | TabletReader::ReaderParams reader_params; |
240 | 93 | reader_params.is_key_column_group = is_key; |
241 | 93 | reader_params.key_group_cluster_key_idxes = key_group_cluster_key_idxes; |
242 | 93 | reader_params.tablet = tablet; |
243 | 93 | reader_params.reader_type = reader_type; |
244 | | |
245 | 93 | TabletReader::ReadSource read_source; |
246 | 93 | read_source.rs_splits.reserve(src_rowset_readers.size()); |
247 | 279 | for (const RowsetReaderSharedPtr& rs_reader : src_rowset_readers) { |
248 | 279 | read_source.rs_splits.emplace_back(rs_reader); |
249 | 279 | } |
250 | 93 | read_source.fill_delete_predicates(); |
251 | 93 | reader_params.set_read_source(std::move(read_source)); |
252 | | |
253 | 93 | reader_params.version = dst_rowset_writer->version(); |
254 | | |
255 | 93 | TabletSchemaSPtr merge_tablet_schema = std::make_shared<TabletSchema>(); |
256 | 93 | merge_tablet_schema->copy_from(tablet_schema); |
257 | | |
258 | 93 | for (auto& del_pred_rs : reader_params.delete_predicates) { |
259 | 53 | merge_tablet_schema->merge_dropped_columns(*del_pred_rs->tablet_schema()); |
260 | 53 | } |
261 | | |
262 | 93 | reader_params.tablet_schema = merge_tablet_schema; |
263 | 93 | bool has_cluster_key = false; |
264 | 93 | if (!tablet->tablet_schema()->cluster_key_idxes().empty()) { |
265 | 0 | reader_params.delete_bitmap = &tablet->tablet_meta()->delete_bitmap(); |
266 | 0 | has_cluster_key = true; |
267 | 0 | } |
268 | | |
269 | 93 | if (is_key && stats_output && stats_output->rowid_conversion) { |
270 | 49 | reader_params.record_rowids = true; |
271 | 49 | reader_params.rowid_conversion = stats_output->rowid_conversion; |
272 | 49 | stats_output->rowid_conversion->set_dst_rowset_id(dst_rowset_writer->rowset_id()); |
273 | 49 | } |
274 | | |
275 | 93 | reader_params.return_columns = column_group; |
276 | 93 | reader_params.origin_return_columns = &reader_params.return_columns; |
277 | 93 | reader_params.batch_size = batch_size; |
278 | 93 | RETURN_IF_ERROR(reader.init(reader_params, sample_info)); |
279 | | |
280 | 93 | vectorized::Block block = tablet_schema.create_block(reader_params.return_columns); |
281 | 93 | size_t output_rows = 0; |
282 | 93 | bool eof = false; |
283 | 7.64k | while (!eof && !ExecEnv::GetInstance()->storage_engine().stopped()) { |
284 | 7.55k | auto tablet_state = tablet->tablet_state(); |
285 | 7.55k | if (tablet_state != TABLET_RUNNING && tablet_state != TABLET_NOTREADY) { |
286 | 0 | tablet->clear_cache(); |
287 | 0 | return Status::Error<INTERNAL_ERROR>("tablet {} is not used any more", |
288 | 0 | tablet->tablet_id()); |
289 | 0 | } |
290 | | // Read one block from block reader |
291 | 7.55k | RETURN_NOT_OK_STATUS_WITH_WARN(reader.next_block_with_aggregation(&block, &eof), |
292 | 7.55k | "failed to read next block when merging rowsets of tablet " + |
293 | 7.55k | std::to_string(tablet->tablet_id())); |
294 | 7.55k | RETURN_NOT_OK_STATUS_WITH_WARN( |
295 | 7.55k | dst_rowset_writer->add_columns(&block, column_group, is_key, max_rows_per_segment, |
296 | 7.55k | has_cluster_key), |
297 | 7.55k | "failed to write block when merging rowsets of tablet " + |
298 | 7.55k | std::to_string(tablet->tablet_id())); |
299 | | |
300 | 7.55k | if (is_key && reader_params.record_rowids && block.rows() > 0) { |
301 | 4.63k | std::vector<uint32_t> segment_num_rows; |
302 | 4.63k | RETURN_IF_ERROR(dst_rowset_writer->get_segment_num_rows(&segment_num_rows)); |
303 | 4.63k | stats_output->rowid_conversion->add(reader.current_block_row_locations(), |
304 | 4.63k | segment_num_rows); |
305 | 4.63k | } |
306 | 7.55k | output_rows += block.rows(); |
307 | 7.55k | block.clear_column_data(); |
308 | 7.55k | } |
309 | 93 | if (ExecEnv::GetInstance()->storage_engine().stopped()) { |
310 | 0 | return Status::Error<INTERNAL_ERROR>("tablet {} failed to do compaction, engine stopped", |
311 | 0 | tablet->tablet_id()); |
312 | 0 | } |
313 | | |
314 | 93 | if (is_key && stats_output != nullptr) { |
315 | 49 | stats_output->output_rows = output_rows; |
316 | 49 | stats_output->merged_rows = reader.merged_rows(); |
317 | 49 | stats_output->filtered_rows = reader.filtered_rows(); |
318 | 49 | } |
319 | 93 | RETURN_IF_ERROR(dst_rowset_writer->flush_columns(is_key)); |
320 | | |
321 | 93 | return Status::OK(); |
322 | 93 | } |
323 | | |
324 | | // for segcompaction |
325 | | Status Merger::vertical_compact_one_group( |
326 | | int64_t tablet_id, ReaderType reader_type, const TabletSchema& tablet_schema, bool is_key, |
327 | | const std::vector<uint32_t>& column_group, vectorized::RowSourcesBuffer* row_source_buf, |
328 | | vectorized::VerticalBlockReader& src_block_reader, |
329 | | segment_v2::SegmentWriter& dst_segment_writer, Statistics* stats_output, |
330 | 22 | uint64_t* index_size, KeyBoundsPB& key_bounds, SimpleRowIdConversion* rowid_conversion) { |
331 | | // TODO: record_rowids |
332 | 22 | vectorized::Block block = tablet_schema.create_block(column_group); |
333 | 22 | size_t output_rows = 0; |
334 | 22 | bool eof = false; |
335 | 138 | while (!eof && !ExecEnv::GetInstance()->storage_engine().stopped()) { |
336 | | // Read one block from block reader |
337 | 116 | RETURN_NOT_OK_STATUS_WITH_WARN(src_block_reader.next_block_with_aggregation(&block, &eof), |
338 | 116 | "failed to read next block when merging rowsets of tablet " + |
339 | 116 | std::to_string(tablet_id)); |
340 | 116 | if (!block.rows()) { |
341 | 0 | break; |
342 | 0 | } |
343 | 116 | RETURN_NOT_OK_STATUS_WITH_WARN(dst_segment_writer.append_block(&block, 0, block.rows()), |
344 | 116 | "failed to write block when merging rowsets of tablet " + |
345 | 116 | std::to_string(tablet_id)); |
346 | | |
347 | 116 | if (is_key && rowid_conversion != nullptr) { |
348 | 30 | rowid_conversion->add(src_block_reader.current_block_row_locations()); |
349 | 30 | } |
350 | 116 | output_rows += block.rows(); |
351 | 116 | block.clear_column_data(); |
352 | 116 | } |
353 | 22 | if (ExecEnv::GetInstance()->storage_engine().stopped()) { |
354 | 0 | return Status::Error<INTERNAL_ERROR>("tablet {} failed to do compaction, engine stopped", |
355 | 0 | tablet_id); |
356 | 0 | } |
357 | | |
358 | 22 | if (is_key && stats_output != nullptr) { |
359 | 11 | stats_output->output_rows = output_rows; |
360 | 11 | stats_output->merged_rows = src_block_reader.merged_rows(); |
361 | 11 | stats_output->filtered_rows = src_block_reader.filtered_rows(); |
362 | 11 | } |
363 | | |
364 | | // segcompaction produce only one segment at once |
365 | 22 | RETURN_IF_ERROR(dst_segment_writer.finalize_columns_data()); |
366 | 22 | RETURN_IF_ERROR(dst_segment_writer.finalize_columns_index(index_size)); |
367 | | |
368 | 22 | if (is_key) { |
369 | 11 | Slice min_key = dst_segment_writer.min_encoded_key(); |
370 | 11 | Slice max_key = dst_segment_writer.max_encoded_key(); |
371 | 11 | DCHECK_LE(min_key.compare(max_key), 0); |
372 | 11 | key_bounds.set_min_key(min_key.to_string()); |
373 | 11 | key_bounds.set_max_key(max_key.to_string()); |
374 | 11 | } |
375 | | |
376 | 22 | return Status::OK(); |
377 | 22 | } |
378 | | |
379 | 93 | int64_t estimate_batch_size(int group_index, BaseTabletSPtr tablet, int64_t way_cnt) { |
380 | 93 | std::unique_lock<std::mutex> lock(tablet->sample_info_lock); |
381 | 93 | CompactionSampleInfo info = tablet->sample_infos[group_index]; |
382 | 93 | if (way_cnt <= 0) { |
383 | 0 | LOG(INFO) << "estimate batch size for vertical compaction, tablet id: " |
384 | 0 | << tablet->tablet_id() << " way cnt: " << way_cnt; |
385 | 0 | return 4096 - 32; |
386 | 0 | } |
387 | 93 | int64_t block_mem_limit = config::compaction_memory_bytes_limit / way_cnt; |
388 | 93 | if (tablet->last_compaction_status.is<ErrorCode::MEM_LIMIT_EXCEEDED>()) { |
389 | 0 | block_mem_limit /= 4; |
390 | 0 | } |
391 | | |
392 | 93 | int64_t group_data_size = 0; |
393 | 93 | if (info.group_data_size > 0 && info.bytes > 0 && info.rows > 0) { |
394 | 1 | float smoothing_factor = 0.5; |
395 | 1 | group_data_size = int64_t(info.group_data_size * (1 - smoothing_factor) + |
396 | 1 | info.bytes / info.rows * smoothing_factor); |
397 | 1 | tablet->sample_infos[group_index].group_data_size = group_data_size; |
398 | 92 | } else if (info.group_data_size > 0 && (info.bytes <= 0 || info.rows <= 0)) { |
399 | 0 | group_data_size = info.group_data_size; |
400 | 92 | } else if (info.group_data_size <= 0 && info.bytes > 0 && info.rows > 0) { |
401 | 0 | group_data_size = info.bytes / info.rows; |
402 | 0 | tablet->sample_infos[group_index].group_data_size = group_data_size; |
403 | 92 | } else { |
404 | 92 | LOG(INFO) << "estimate batch size for vertical compaction, tablet id: " |
405 | 92 | << tablet->tablet_id() << " group data size: " << info.group_data_size |
406 | 92 | << " row num: " << info.rows << " consume bytes: " << info.bytes; |
407 | 92 | return 1024 - 32; |
408 | 92 | } |
409 | | |
410 | 1 | if (group_data_size <= 0) { |
411 | 0 | LOG(WARNING) << "estimate batch size for vertical compaction, tablet id: " |
412 | 0 | << tablet->tablet_id() << " unexpected group data size: " << group_data_size; |
413 | 0 | return 4096 - 32; |
414 | 0 | } |
415 | | |
416 | 1 | tablet->sample_infos[group_index].bytes = 0; |
417 | 1 | tablet->sample_infos[group_index].rows = 0; |
418 | | |
419 | 1 | int64_t batch_size = block_mem_limit / group_data_size; |
420 | 1 | int64_t res = std::max(std::min(batch_size, int64_t(4096 - 32)), int64_t(32L)); |
421 | 1 | LOG(INFO) << "estimate batch size for vertical compaction, tablet id: " << tablet->tablet_id() |
422 | 1 | << " group data size: " << info.group_data_size << " row num: " << info.rows |
423 | 1 | << " consume bytes: " << info.bytes << " way cnt: " << way_cnt |
424 | 1 | << " batch size: " << res; |
425 | 1 | return res; |
426 | 1 | } |
427 | | |
428 | | // steps to do vertical merge: |
429 | | // 1. split columns into column groups |
430 | | // 2. compact groups one by one, generate a row_source_buf when compact key group |
431 | | // and use this row_source_buf to compact value column groups |
432 | | // 3. build output rowset |
433 | | Status Merger::vertical_merge_rowsets(BaseTabletSPtr tablet, ReaderType reader_type, |
434 | | const TabletSchema& tablet_schema, |
435 | | const std::vector<RowsetReaderSharedPtr>& src_rowset_readers, |
436 | | RowsetWriter* dst_rowset_writer, int64_t max_rows_per_segment, |
437 | 49 | int64_t merge_way_num, Statistics* stats_output) { |
438 | 49 | LOG(INFO) << "Start to do vertical compaction, tablet_id: " << tablet->tablet_id(); |
439 | 49 | std::vector<std::vector<uint32_t>> column_groups; |
440 | 49 | std::vector<uint32_t> key_group_cluster_key_idxes; |
441 | 49 | vertical_split_columns(tablet_schema, &column_groups, &key_group_cluster_key_idxes); |
442 | | |
443 | 49 | vectorized::RowSourcesBuffer row_sources_buf( |
444 | 49 | tablet->tablet_id(), dst_rowset_writer->context().tablet_path, reader_type); |
445 | 49 | { |
446 | 49 | std::unique_lock<std::mutex> lock(tablet->sample_info_lock); |
447 | 49 | tablet->sample_infos.resize(column_groups.size(), {0, 0, 0}); |
448 | 49 | } |
449 | | // compact group one by one |
450 | 142 | for (auto i = 0; i < column_groups.size(); ++i) { |
451 | 93 | VLOG_NOTICE << "row source size: " << row_sources_buf.total_size(); |
452 | 93 | bool is_key = (i == 0); |
453 | 93 | int64_t batch_size = config::compaction_batch_size != -1 |
454 | 93 | ? config::compaction_batch_size |
455 | 93 | : estimate_batch_size(i, tablet, merge_way_num); |
456 | 93 | CompactionSampleInfo sample_info; |
457 | 93 | Status st = vertical_compact_one_group( |
458 | 93 | tablet, reader_type, tablet_schema, is_key, column_groups[i], &row_sources_buf, |
459 | 93 | src_rowset_readers, dst_rowset_writer, max_rows_per_segment, stats_output, |
460 | 93 | key_group_cluster_key_idxes, batch_size, &sample_info); |
461 | 93 | { |
462 | 93 | std::unique_lock<std::mutex> lock(tablet->sample_info_lock); |
463 | 93 | tablet->sample_infos[i] = sample_info; |
464 | 93 | } |
465 | 93 | RETURN_IF_ERROR(st); |
466 | 93 | if (is_key) { |
467 | 49 | RETURN_IF_ERROR(row_sources_buf.flush()); |
468 | 49 | } |
469 | 93 | RETURN_IF_ERROR(row_sources_buf.seek_to_begin()); |
470 | 93 | } |
471 | | |
472 | | // finish compact, build output rowset |
473 | 49 | VLOG_NOTICE << "finish compact groups"; |
474 | 49 | RETURN_IF_ERROR(dst_rowset_writer->final_flush()); |
475 | | |
476 | 49 | return Status::OK(); |
477 | 49 | } |
478 | | |
479 | | } // namespace doris |