Coverage Report

Created: 2025-06-16 08:21

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