Coverage Report

Created: 2025-07-29 08:54

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