Coverage Report

Created: 2026-04-11 14:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/compaction/compaction.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/compaction/compaction.h"
19
20
#include <fmt/format.h>
21
#include <gen_cpp/olap_file.pb.h>
22
#include <glog/logging.h>
23
24
#include <algorithm>
25
#include <atomic>
26
#include <cstdint>
27
#include <cstdlib>
28
#include <list>
29
#include <map>
30
#include <memory>
31
#include <mutex>
32
#include <nlohmann/json.hpp>
33
#include <numeric>
34
#include <ostream>
35
#include <set>
36
#include <shared_mutex>
37
#include <utility>
38
39
#include "cloud/cloud_meta_mgr.h"
40
#include "cloud/cloud_storage_engine.h"
41
#include "cloud/cloud_tablet.h"
42
#include "cloud/pb_convert.h"
43
#include "common/config.h"
44
#include "common/metrics/doris_metrics.h"
45
#include "common/status.h"
46
#include "cpp/sync_point.h"
47
#include "exec/common/variant_util.h"
48
#include "io/cache/block_file_cache_factory.h"
49
#include "io/fs/file_system.h"
50
#include "io/fs/file_writer.h"
51
#include "io/fs/remote_file_system.h"
52
#include "io/io_common.h"
53
#include "runtime/memory/mem_tracker_limiter.h"
54
#include "runtime/thread_context.h"
55
#include "storage/compaction/collection_statistics.h"
56
#include "storage/compaction/cumulative_compaction.h"
57
#include "storage/compaction/cumulative_compaction_policy.h"
58
#include "storage/compaction/cumulative_compaction_time_series_policy.h"
59
#include "storage/compaction_task_tracker.h"
60
#include "storage/data_dir.h"
61
#include "storage/index/index_file_reader.h"
62
#include "storage/index/index_file_writer.h"
63
#include "storage/index/inverted/inverted_index_compaction.h"
64
#include "storage/index/inverted/inverted_index_desc.h"
65
#include "storage/index/inverted/inverted_index_fs_directory.h"
66
#include "storage/olap_common.h"
67
#include "storage/olap_define.h"
68
#include "storage/rowset/beta_rowset.h"
69
#include "storage/rowset/beta_rowset_reader.h"
70
#include "storage/rowset/beta_rowset_writer.h"
71
#include "storage/rowset/rowset.h"
72
#include "storage/rowset/rowset_fwd.h"
73
#include "storage/rowset/rowset_meta.h"
74
#include "storage/rowset/rowset_writer.h"
75
#include "storage/rowset/rowset_writer_context.h"
76
#include "storage/storage_engine.h"
77
#include "storage/storage_policy.h"
78
#include "storage/tablet/tablet.h"
79
#include "storage/tablet/tablet_meta.h"
80
#include "storage/tablet/tablet_meta_manager.h"
81
#include "storage/task/engine_checksum_task.h"
82
#include "storage/txn/txn_manager.h"
83
#include "storage/utils.h"
84
#include "util/pretty_printer.h"
85
#include "util/time.h"
86
#include "util/trace.h"
87
88
using std::vector;
89
90
namespace doris {
91
using namespace ErrorCode;
92
93
// Determine whether to enable index-only file cache mode for compaction output.
94
// This function decides if only index files should be written to cache, based on:
95
// - write_file_cache: whether file cache is enabled
96
// - compaction_type: type of compaction (base or cumulative)
97
// - enable_base_index_only: config flag for base compaction
98
// - enable_cumu_index_only: config flag for cumulative compaction
99
// Returns true if index-only mode should be enabled, false otherwise.
100
bool should_enable_compaction_cache_index_only(bool write_file_cache, ReaderType compaction_type,
101
                                               bool enable_base_index_only,
102
7.13k
                                               bool enable_cumu_index_only) {
103
7.13k
    if (!write_file_cache) {
104
141
        return false;
105
141
    }
106
107
6.99k
    if (compaction_type == ReaderType::READER_BASE_COMPACTION && enable_base_index_only) {
108
2
        return true;
109
2
    }
110
111
6.98k
    if (compaction_type == ReaderType::READER_CUMULATIVE_COMPACTION && enable_cumu_index_only) {
112
2
        return true;
113
2
    }
114
115
6.98k
    return false;
116
6.98k
}
117
118
namespace {
119
120
bool is_rowset_tidy(std::string& pre_max_key, bool& pre_rs_key_bounds_truncated,
121
3.80k
                    const RowsetSharedPtr& rhs) {
122
3.80k
    size_t min_tidy_size = config::ordered_data_compaction_min_segment_size;
123
3.80k
    if (rhs->num_segments() == 0) {
124
3.44k
        return true;
125
3.44k
    }
126
357
    if (rhs->is_segments_overlapping()) {
127
0
        return false;
128
0
    }
129
    // check segment size
130
357
    auto* beta_rowset = reinterpret_cast<BetaRowset*>(rhs.get());
131
357
    std::vector<size_t> segments_size;
132
357
    RETURN_FALSE_IF_ERROR(beta_rowset->get_segments_size(&segments_size));
133
362
    for (auto segment_size : segments_size) {
134
        // is segment is too small, need to do compaction
135
362
        if (segment_size < min_tidy_size) {
136
316
            return false;
137
316
        }
138
362
    }
139
40
    std::string min_key;
140
40
    auto ret = rhs->first_key(&min_key);
141
40
    if (!ret) {
142
0
        return false;
143
0
    }
144
40
    bool cur_rs_key_bounds_truncated {rhs->is_segments_key_bounds_truncated()};
145
40
    if (!Slice::lhs_is_strictly_less_than_rhs(Slice {pre_max_key}, pre_rs_key_bounds_truncated,
146
40
                                              Slice {min_key}, cur_rs_key_bounds_truncated)) {
147
5
        return false;
148
5
    }
149
40
    CHECK(rhs->last_key(&pre_max_key));
150
35
    pre_rs_key_bounds_truncated = cur_rs_key_bounds_truncated;
151
35
    return true;
152
40
}
153
154
} // namespace
155
156
Compaction::Compaction(BaseTabletSPtr tablet, const std::string& label)
157
341k
        : _compaction_id(CompactionTaskTracker::instance()->next_compaction_id()),
158
          _mem_tracker(
159
341k
                  MemTrackerLimiter::create_shared(MemTrackerLimiter::Type::COMPACTION, label)),
160
341k
          _tablet(std::move(tablet)),
161
341k
          _is_vertical(config::enable_vertical_compaction),
162
341k
          _allow_delete_in_cumu_compaction(config::enable_delete_when_cumu_compaction),
163
          _enable_vertical_compact_variant_subcolumns(
164
341k
                  config::enable_vertical_compact_variant_subcolumns),
165
341k
          _enable_inverted_index_compaction(config::inverted_index_compaction_enable) {
166
341k
    init_profile(label);
167
341k
    SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_mem_tracker);
168
341k
    _rowid_conversion = std::make_unique<RowIdConversion>();
169
341k
}
170
171
341k
Compaction::~Compaction() {
172
341k
    SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_mem_tracker);
173
341k
    _output_rs_writer.reset();
174
341k
    _tablet.reset();
175
341k
    _input_rowsets.clear();
176
341k
    _output_rowset.reset();
177
341k
    _cur_tablet_schema.reset();
178
341k
    _rowid_conversion.reset();
179
341k
}
180
181
15.8k
std::string Compaction::input_version_range_str() const {
182
15.8k
    if (_input_rowsets.empty()) return "";
183
15.8k
    return fmt::format("[{}-{}]", _input_rowsets.front()->start_version(),
184
15.8k
                       _input_rowsets.back()->end_version());
185
15.8k
}
186
187
void Compaction::submit_profile_record(bool success, int64_t start_time_ms,
188
8.50k
                                       const std::string& status_msg) {
189
8.50k
    if (!profile_type().has_value()) {
190
846
        return;
191
846
    }
192
7.65k
    auto* tracker = CompactionTaskTracker::instance();
193
7.65k
    CompletionStats stats;
194
    // Input stats for backfill: local compaction fills these in build_basic_info()
195
    // which runs inside execute_compact_impl(), so they are available now.
196
7.65k
    stats.input_version_range = input_version_range_str();
197
7.65k
    stats.input_rowsets_count = static_cast<int64_t>(_input_rowsets.size());
198
7.65k
    stats.input_row_num = _input_row_num;
199
7.65k
    stats.input_data_size = _input_rowsets_data_size;
200
7.65k
    stats.input_index_size = _input_rowsets_index_size;
201
7.65k
    stats.input_total_size = _input_rowsets_total_size;
202
7.65k
    stats.input_segments_num = input_segments_num_value();
203
7.65k
    stats.end_time_ms = UnixMillis();
204
7.65k
    stats.merged_rows = _stats.merged_rows;
205
7.65k
    stats.filtered_rows = _stats.filtered_rows;
206
7.65k
    stats.output_rows = _stats.output_rows;
207
7.66k
    if (_output_rowset) {
208
7.66k
        stats.output_row_num = _output_rowset->num_rows();
209
7.66k
        stats.output_data_size = _output_rowset->data_disk_size();
210
7.66k
        stats.output_index_size = _output_rowset->index_disk_size();
211
7.66k
        stats.output_total_size = _output_rowset->total_disk_size();
212
7.66k
        stats.output_segments_num = _output_rowset->num_segments();
213
7.66k
    }
214
7.65k
    stats.output_version = _output_version.to_string();
215
7.66k
    if (_merge_rowsets_latency_timer) {
216
7.66k
        stats.merge_latency_ms = _merge_rowsets_latency_timer->value() / 1000000;
217
7.66k
    }
218
7.65k
    stats.bytes_read_from_local = _stats.bytes_read_from_local;
219
7.65k
    stats.bytes_read_from_remote = _stats.bytes_read_from_remote;
220
7.65k
    if (_mem_tracker) {
221
7.65k
        stats.peak_memory_bytes = _mem_tracker->peak_consumption();
222
7.65k
    }
223
7.65k
    if (success) {
224
7.60k
        tracker->complete(_compaction_id, stats);
225
7.60k
    } else {
226
56
        tracker->fail(_compaction_id, stats, status_msg);
227
56
    }
228
7.65k
}
229
230
341k
void Compaction::init_profile(const std::string& label) {
231
341k
    _profile = std::make_unique<RuntimeProfile>(label);
232
233
341k
    _input_rowsets_data_size_counter =
234
341k
            ADD_COUNTER(_profile, "input_rowsets_data_size", TUnit::BYTES);
235
341k
    _input_rowsets_counter = ADD_COUNTER(_profile, "input_rowsets_count", TUnit::UNIT);
236
341k
    _input_row_num_counter = ADD_COUNTER(_profile, "input_row_num", TUnit::UNIT);
237
341k
    _input_segments_num_counter = ADD_COUNTER(_profile, "input_segments_num", TUnit::UNIT);
238
341k
    _merged_rows_counter = ADD_COUNTER(_profile, "merged_rows", TUnit::UNIT);
239
341k
    _filtered_rows_counter = ADD_COUNTER(_profile, "filtered_rows", TUnit::UNIT);
240
341k
    _output_rowset_data_size_counter =
241
341k
            ADD_COUNTER(_profile, "output_rowset_data_size", TUnit::BYTES);
242
341k
    _output_row_num_counter = ADD_COUNTER(_profile, "output_row_num", TUnit::UNIT);
243
341k
    _output_segments_num_counter = ADD_COUNTER(_profile, "output_segments_num", TUnit::UNIT);
244
341k
    _merge_rowsets_latency_timer = ADD_TIMER(_profile, "merge_rowsets_latency");
245
341k
}
246
247
8.00k
int64_t Compaction::merge_way_num() {
248
8.00k
    int64_t way_num = 0;
249
61.3k
    for (auto&& rowset : _input_rowsets) {
250
61.3k
        way_num += rowset->rowset_meta()->get_merge_way_num();
251
61.3k
    }
252
253
8.00k
    return way_num;
254
8.00k
}
255
256
8.01k
Status Compaction::merge_input_rowsets() {
257
8.01k
    std::vector<RowsetReaderSharedPtr> input_rs_readers;
258
8.01k
    input_rs_readers.reserve(_input_rowsets.size());
259
61.4k
    for (auto& rowset : _input_rowsets) {
260
61.4k
        RowsetReaderSharedPtr rs_reader;
261
61.4k
        RETURN_IF_ERROR(rowset->create_reader(&rs_reader));
262
61.4k
        input_rs_readers.push_back(std::move(rs_reader));
263
61.4k
    }
264
265
8.01k
    RowsetWriterContext ctx;
266
8.01k
    RETURN_IF_ERROR(construct_output_rowset_writer(ctx));
267
268
    // write merged rows to output rowset
269
    // The test results show that merger is low-memory-footprint, there is no need to tracker its mem pool
270
    // if ctx.columns_to_do_index_compaction.size() > 0, it means we need to do inverted index compaction.
271
    // the row ID conversion matrix needs to be used for inverted index compaction.
272
8.01k
    if (!ctx.columns_to_do_index_compaction.empty() ||
273
8.01k
        (_tablet->keys_type() == KeysType::UNIQUE_KEYS &&
274
7.61k
         _tablet->enable_unique_key_merge_on_write())) {
275
4.26k
        _stats.rowid_conversion = _rowid_conversion.get();
276
4.26k
    }
277
278
8.01k
    int64_t way_num = merge_way_num();
279
280
8.01k
    Status res;
281
8.01k
    {
282
8.01k
        SCOPED_TIMER(_merge_rowsets_latency_timer);
283
        // 1. Merge segment files and write bkd inverted index
284
        // TODO implement vertical compaction for seq map
285
8.01k
        if (_is_vertical && !_tablet->tablet_schema()->has_seq_map()) {
286
8.01k
            if (!_tablet->tablet_schema()->cluster_key_uids().empty()) {
287
156
                RETURN_IF_ERROR(update_delete_bitmap());
288
156
            }
289
8.01k
            auto progress_cb = [compaction_id = this->_compaction_id](int64_t total,
290
30.2k
                                                                      int64_t completed) {
291
30.2k
                CompactionTaskTracker::instance()->update_progress(compaction_id, total, completed);
292
30.2k
            };
293
8.01k
            res = Merger::vertical_merge_rowsets(_tablet, compaction_type(), *_cur_tablet_schema,
294
8.01k
                                                 input_rs_readers, _output_rs_writer.get(),
295
8.01k
                                                 cast_set<uint32_t>(get_avg_segment_rows()),
296
8.01k
                                                 way_num, &_stats, progress_cb);
297
8.01k
        } else {
298
7
            if (!_tablet->tablet_schema()->cluster_key_uids().empty()) {
299
0
                return Status::InternalError(
300
0
                        "mow table with cluster keys does not support non vertical compaction");
301
0
            }
302
7
            res = Merger::vmerge_rowsets(_tablet, compaction_type(), *_cur_tablet_schema,
303
7
                                         input_rs_readers, _output_rs_writer.get(), &_stats);
304
7
        }
305
306
8.01k
        _tablet->last_compaction_status = res;
307
8.01k
        if (!res.ok()) {
308
0
            return res;
309
0
        }
310
        // 2. Merge the remaining inverted index files of the string type
311
8.01k
        RETURN_IF_ERROR(do_inverted_index_compaction());
312
8.01k
    }
313
314
8.01k
    COUNTER_UPDATE(_merged_rows_counter, _stats.merged_rows);
315
8.01k
    COUNTER_UPDATE(_filtered_rows_counter, _stats.filtered_rows);
316
317
    // 3. In the `build`, `_close_file_writers` is called to close the inverted index file writer and write the final compound index file.
318
8.01k
    RETURN_NOT_OK_STATUS_WITH_WARN(_output_rs_writer->build(_output_rowset),
319
8.01k
                                   fmt::format("rowset writer build failed. output_version: {}",
320
8.01k
                                               _output_version.to_string()));
321
322
    // When true, writers should remove variant extracted subcolumns from the
323
    // schema stored in RowsetMeta. This is used when compaction temporarily
324
    // extends schema to split variant subcolumns for vertical compaction but
325
    // the final rowset meta must not persist those extracted subcolumns.
326
8.01k
    if (_enable_vertical_compact_variant_subcolumns &&
327
8.01k
        (_cur_tablet_schema->num_variant_columns() > 0)) {
328
542
        _output_rowset->rowset_meta()->set_tablet_schema(
329
542
                _cur_tablet_schema->copy_without_variant_extracted_columns());
330
542
    }
331
332
    //RETURN_IF_ERROR(_engine.meta_mgr().commit_rowset(*_output_rowset->rowset_meta().get()));
333
8.01k
    set_delete_predicate_for_output_rowset();
334
335
8.01k
    _local_read_bytes_total = _stats.bytes_read_from_local;
336
8.01k
    _remote_read_bytes_total = _stats.bytes_read_from_remote;
337
8.01k
    DorisMetrics::instance()->local_compaction_read_bytes_total->increment(_local_read_bytes_total);
338
8.01k
    DorisMetrics::instance()->remote_compaction_read_bytes_total->increment(
339
8.01k
            _remote_read_bytes_total);
340
8.01k
    DorisMetrics::instance()->local_compaction_write_bytes_total->increment(
341
8.01k
            _stats.cached_bytes_total);
342
343
8.01k
    COUNTER_UPDATE(_output_rowset_data_size_counter, _output_rowset->data_disk_size());
344
8.01k
    COUNTER_UPDATE(_output_row_num_counter, _output_rowset->num_rows());
345
8.01k
    COUNTER_UPDATE(_output_segments_num_counter, _output_rowset->num_segments());
346
347
8.01k
    return check_correctness();
348
8.01k
}
349
350
8.00k
void Compaction::set_delete_predicate_for_output_rowset() {
351
    // Now we support delete in cumu compaction, to make all data in rowsets whose version
352
    // is below output_version to be delete in the future base compaction, we should carry
353
    // all delete predicate in the output rowset.
354
    // Output start version > 2 means we must set the delete predicate in the output rowset
355
8.00k
    if (_output_rowset->version().first > 2 &&
356
8.00k
        (_allow_delete_in_cumu_compaction || is_index_change_compaction())) {
357
501
        DeletePredicatePB delete_predicate;
358
501
        std::accumulate(_input_rowsets.begin(), _input_rowsets.end(), &delete_predicate,
359
501
                        [](DeletePredicatePB* delete_predicate, const RowsetSharedPtr& rs) {
360
500
                            if (rs->rowset_meta()->has_delete_predicate()) {
361
3
                                delete_predicate->MergeFrom(rs->rowset_meta()->delete_predicate());
362
3
                            }
363
500
                            return delete_predicate;
364
500
                        });
365
        // now version in delete_predicate is deprecated
366
501
        if (!delete_predicate.in_predicates().empty() ||
367
501
            !delete_predicate.sub_predicates_v2().empty() ||
368
501
            !delete_predicate.sub_predicates().empty()) {
369
3
            _output_rowset->rowset_meta()->set_delete_predicate(std::move(delete_predicate));
370
3
        }
371
501
    }
372
8.00k
}
373
374
7.99k
int64_t Compaction::get_avg_segment_rows() {
375
    // take care of empty rowset
376
    // input_rowsets_size is total disk_size of input_rowset, this size is the
377
    // final size after codec and compress, so expect dest segment file size
378
    // in disk is config::vertical_compaction_max_segment_size
379
7.99k
    const auto& meta = _tablet->tablet_meta();
380
7.99k
    if (meta->compaction_policy() == CUMULATIVE_TIME_SERIES_POLICY) {
381
3
        int64_t compaction_goal_size_mbytes = meta->time_series_compaction_goal_size_mbytes();
382
        // The output segment rows should be less than total input rows
383
3
        return std::min((compaction_goal_size_mbytes * 1024 * 1024 * 2) /
384
3
                                (_input_rowsets_data_size / (_input_row_num + 1) + 1),
385
3
                        _input_row_num + 1);
386
3
    }
387
7.98k
    return std::min(config::vertical_compaction_max_segment_size /
388
7.98k
                            (_input_rowsets_data_size / (_input_row_num + 1) + 1),
389
7.98k
                    _input_row_num + 1);
390
7.99k
}
391
392
CompactionMixin::CompactionMixin(StorageEngine& engine, TabletSharedPtr tablet,
393
                                 const std::string& label)
394
215k
        : Compaction(tablet, label), _engine(engine) {}
395
396
215k
CompactionMixin::~CompactionMixin() {
397
215k
    if (_state != CompactionState::SUCCESS && _output_rowset != nullptr) {
398
6
        if (!_output_rowset->is_local()) {
399
0
            tablet()->record_unused_remote_rowset(_output_rowset->rowset_id(),
400
0
                                                  _output_rowset->rowset_meta()->resource_id(),
401
0
                                                  _output_rowset->num_segments());
402
0
            return;
403
0
        }
404
6
        _engine.add_unused_rowset(_output_rowset);
405
6
    }
406
215k
}
407
408
1.95M
Tablet* CompactionMixin::tablet() {
409
1.95M
    return static_cast<Tablet*>(_tablet.get());
410
1.95M
}
411
412
502
Status CompactionMixin::do_compact_ordered_rowsets() {
413
502
    RETURN_IF_ERROR(build_basic_info(true));
414
502
    RowsetWriterContext ctx;
415
502
    RETURN_IF_ERROR(construct_output_rowset_writer(ctx));
416
417
502
    LOG(INFO) << "start to do ordered data compaction, tablet=" << _tablet->tablet_id()
418
502
              << ", output_version=" << _output_version;
419
    // link data to new rowset
420
502
    auto seg_id = 0;
421
502
    bool segments_key_bounds_truncated {false};
422
502
    std::vector<KeyBoundsPB> segment_key_bounds;
423
502
    std::vector<uint32_t> num_segment_rows;
424
3.15k
    for (auto rowset : _input_rowsets) {
425
3.15k
        RETURN_IF_ERROR(rowset->link_files_to(tablet()->tablet_path(),
426
3.15k
                                              _output_rs_writer->rowset_id(), seg_id));
427
3.15k
        seg_id += rowset->num_segments();
428
3.15k
        segments_key_bounds_truncated |= rowset->is_segments_key_bounds_truncated();
429
3.15k
        std::vector<KeyBoundsPB> key_bounds;
430
3.15k
        RETURN_IF_ERROR(rowset->get_segments_key_bounds(&key_bounds));
431
3.15k
        segment_key_bounds.insert(segment_key_bounds.end(), key_bounds.begin(), key_bounds.end());
432
3.15k
        std::vector<uint32_t> input_segment_rows;
433
3.15k
        rowset->get_num_segment_rows(&input_segment_rows);
434
3.15k
        num_segment_rows.insert(num_segment_rows.end(), input_segment_rows.begin(),
435
3.15k
                                input_segment_rows.end());
436
3.15k
    }
437
    // build output rowset
438
502
    RowsetMetaSharedPtr rowset_meta = std::make_shared<RowsetMeta>();
439
502
    rowset_meta->set_num_rows(_input_row_num);
440
502
    rowset_meta->set_total_disk_size(_input_rowsets_data_size + _input_rowsets_index_size);
441
502
    rowset_meta->set_data_disk_size(_input_rowsets_data_size);
442
502
    rowset_meta->set_index_disk_size(_input_rowsets_index_size);
443
502
    rowset_meta->set_empty(_input_row_num == 0);
444
502
    rowset_meta->set_num_segments(_input_num_segments);
445
502
    rowset_meta->set_segments_overlap(NONOVERLAPPING);
446
502
    rowset_meta->set_rowset_state(VISIBLE);
447
502
    rowset_meta->set_segments_key_bounds_truncated(segments_key_bounds_truncated);
448
502
    rowset_meta->set_segments_key_bounds(segment_key_bounds);
449
502
    rowset_meta->set_num_segment_rows(num_segment_rows);
450
451
502
    _output_rowset = _output_rs_writer->manual_build(rowset_meta);
452
453
    // 2. check variant column path stats
454
502
    RETURN_IF_ERROR(variant_util::VariantCompactionUtil::check_path_stats(_input_rowsets,
455
502
                                                                          _output_rowset, _tablet));
456
502
    return Status::OK();
457
502
}
458
459
1.39k
Status CompactionMixin::build_basic_info(bool is_ordered_compaction) {
460
9.49k
    for (auto& rowset : _input_rowsets) {
461
9.49k
        const auto& rowset_meta = rowset->rowset_meta();
462
9.49k
        auto index_size = rowset_meta->index_disk_size();
463
9.49k
        auto total_size = rowset_meta->total_disk_size();
464
9.49k
        auto data_size = rowset_meta->data_disk_size();
465
        // corrupted index size caused by bug before 2.1.5 or 3.0.0 version
466
        // try to get real index size from disk.
467
9.49k
        if (index_size < 0 || index_size > total_size * 2) {
468
0
            LOG(ERROR) << "invalid index size:" << index_size << " total size:" << total_size
469
0
                       << " data size:" << data_size << " tablet:" << rowset_meta->tablet_id()
470
0
                       << " rowset:" << rowset_meta->rowset_id();
471
0
            index_size = 0;
472
0
            auto st = rowset->get_inverted_index_size(&index_size);
473
0
            if (!st.ok()) {
474
0
                LOG(ERROR) << "failed to get inverted index size. res=" << st;
475
0
            }
476
0
        }
477
9.49k
        _input_rowsets_data_size += data_size;
478
9.49k
        _input_rowsets_index_size += index_size;
479
9.49k
        _input_rowsets_total_size += total_size;
480
9.49k
        _input_row_num += rowset->num_rows();
481
9.49k
        _input_num_segments += rowset->num_segments();
482
9.49k
    }
483
1.39k
    COUNTER_UPDATE(_input_rowsets_data_size_counter, _input_rowsets_data_size);
484
1.39k
    COUNTER_UPDATE(_input_row_num_counter, _input_row_num);
485
1.39k
    COUNTER_UPDATE(_input_segments_num_counter, _input_num_segments);
486
487
1.39k
    TEST_SYNC_POINT_RETURN_WITH_VALUE("compaction::CompactionMixin::build_basic_info",
488
1.39k
                                      Status::OK());
489
490
1.39k
    _output_version =
491
1.39k
            Version(_input_rowsets.front()->start_version(), _input_rowsets.back()->end_version());
492
493
1.39k
    _newest_write_timestamp = _input_rowsets.back()->newest_write_timestamp();
494
495
1.39k
    std::vector<RowsetMetaSharedPtr> rowset_metas(_input_rowsets.size());
496
1.39k
    std::transform(_input_rowsets.begin(), _input_rowsets.end(), rowset_metas.begin(),
497
9.65k
                   [](const RowsetSharedPtr& rowset) { return rowset->rowset_meta(); });
498
1.39k
    _cur_tablet_schema = _tablet->tablet_schema_with_merged_max_schema_version(rowset_metas);
499
500
    // if enable_vertical_compact_variant_subcolumns is true, we need to compact the variant subcolumns in seperate column groups
501
    // so get_extended_compaction_schema will extended the schema for variant columns
502
    // for ordered compaction, we don't need to extend the schema for variant columns
503
1.39k
    if (_enable_vertical_compact_variant_subcolumns && !is_ordered_compaction) {
504
900
        RETURN_IF_ERROR(variant_util::VariantCompactionUtil::get_extended_compaction_schema(
505
900
                _input_rowsets, _cur_tablet_schema));
506
900
    }
507
1.39k
    return Status::OK();
508
1.39k
}
509
510
1.40k
bool CompactionMixin::handle_ordered_data_compaction() {
511
1.40k
    if (!config::enable_ordered_data_compaction) {
512
0
        return false;
513
0
    }
514
515
    // If some rowsets has idx files and some rowsets has not, we can not do link file compaction.
516
    // Since the output rowset will be broken.
517
518
    // Use schema version instead of schema hash to check if they are the same,
519
    // because light schema change will not change the schema hash on BE, but will increase the schema version
520
    // See fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java::2979
521
1.40k
    std::vector<int32_t> schema_versions_of_rowsets;
522
523
9.69k
    for (auto input_rowset : _input_rowsets) {
524
9.69k
        schema_versions_of_rowsets.push_back(input_rowset->rowset_meta()->schema_version());
525
9.69k
    }
526
527
    // If all rowsets has same schema version, then we can do link file compaction directly.
528
1.40k
    bool all_same_schema_version =
529
1.40k
            std::all_of(schema_versions_of_rowsets.begin(), schema_versions_of_rowsets.end(),
530
9.69k
                        [&](int32_t v) { return v == schema_versions_of_rowsets.front(); });
531
532
1.40k
    if (!all_same_schema_version) {
533
0
        return false;
534
0
    }
535
536
1.40k
    if (compaction_type() == ReaderType::READER_COLD_DATA_COMPACTION ||
537
1.41k
        compaction_type() == ReaderType::READER_FULL_COMPACTION) {
538
        // The remote file system and full compaction does not support to link files.
539
0
        return false;
540
0
    }
541
1.40k
    if (_tablet->keys_type() == KeysType::UNIQUE_KEYS &&
542
1.40k
        _tablet->enable_unique_key_merge_on_write()) {
543
622
        return false;
544
622
    }
545
546
786
    if (_tablet->tablet_meta()->tablet_schema()->skip_write_index_on_load()) {
547
        // Expected to create index through normal compaction
548
0
        return false;
549
0
    }
550
551
    // check delete version: if compaction type is base compaction and
552
    // has a delete version, use original compaction
553
786
    if (compaction_type() == ReaderType::READER_BASE_COMPACTION ||
554
786
        (_allow_delete_in_cumu_compaction &&
555
772
         compaction_type() == ReaderType::READER_CUMULATIVE_COMPACTION)) {
556
42
        for (auto& rowset : _input_rowsets) {
557
42
            if (rowset->rowset_meta()->has_delete_predicate()) {
558
14
                return false;
559
14
            }
560
42
        }
561
14
    }
562
563
    // check if rowsets are tidy so we can just modify meta and do link
564
    // files to handle compaction
565
772
    auto input_size = _input_rowsets.size();
566
772
    std::string pre_max_key;
567
772
    bool pre_rs_key_bounds_truncated {false};
568
4.24k
    for (auto i = 0; i < input_size; ++i) {
569
3.80k
        if (!is_rowset_tidy(pre_max_key, pre_rs_key_bounds_truncated, _input_rowsets[i])) {
570
326
            if (i <= input_size / 2) {
571
272
                return false;
572
272
            } else {
573
54
                _input_rowsets.resize(i);
574
54
                break;
575
54
            }
576
326
        }
577
3.80k
    }
578
    // most rowset of current compaction is nonoverlapping
579
    // just handle nonoverlappint rowsets
580
500
    auto st = do_compact_ordered_rowsets();
581
500
    if (!st.ok()) {
582
0
        LOG(WARNING) << "failed to compact ordered rowsets: " << st;
583
0
        _pending_rs_guard.drop();
584
0
    }
585
586
500
    return st.ok();
587
772
}
588
589
1.39k
Status CompactionMixin::execute_compact() {
590
1.39k
    int64_t profile_start_time_ms = UnixMillis();
591
1.39k
    uint32_t checksum_before;
592
1.39k
    uint32_t checksum_after;
593
1.39k
    bool enable_compaction_checksum = config::enable_compaction_checksum;
594
1.39k
    if (enable_compaction_checksum) {
595
0
        EngineChecksumTask checksum_task(_engine, _tablet->tablet_id(), _tablet->schema_hash(),
596
0
                                         _input_rowsets.back()->end_version(), &checksum_before);
597
0
        auto st = checksum_task.execute();
598
0
        if (!st.ok()) {
599
0
            submit_profile_record(false, profile_start_time_ms, st.to_string());
600
0
            return st;
601
0
        }
602
0
    }
603
604
1.39k
    auto* data_dir = tablet()->data_dir();
605
1.39k
    int64_t permits = get_compaction_permits();
606
1.39k
    data_dir->disks_compaction_score_increment(permits);
607
1.39k
    data_dir->disks_compaction_num_increment(1);
608
609
1.39k
    auto record_compaction_stats = [&](const doris::Exception& ex) {
610
1.39k
        _tablet->compaction_count.fetch_add(1, std::memory_order_relaxed);
611
1.39k
        data_dir->disks_compaction_score_increment(-permits);
612
1.39k
        data_dir->disks_compaction_num_increment(-1);
613
1.39k
    };
614
    // Handler for execute_compact_impl failure (both Status error and C++ exception).
615
    // The macro calls this then returns, so submit_profile_record(false) must be here.
616
1.39k
    auto on_compact_impl_failure = [&](const doris::Exception& ex) {
617
0
        record_compaction_stats(ex);
618
0
        submit_profile_record(false, profile_start_time_ms,
619
0
                              ex.what() ? std::string(ex.what()) : "");
620
0
    };
621
622
1.39k
    HANDLE_EXCEPTION_IF_CATCH_EXCEPTION(execute_compact_impl(permits), on_compact_impl_failure);
623
    // Only reached on success (macro returns on failure).
624
1.39k
    record_compaction_stats(doris::Exception());
625
626
1.39k
    if (enable_compaction_checksum) {
627
0
        EngineChecksumTask checksum_task(_engine, _tablet->tablet_id(), _tablet->schema_hash(),
628
0
                                         _input_rowsets.back()->end_version(), &checksum_after);
629
0
        auto st = checksum_task.execute();
630
0
        if (!st.ok()) {
631
0
            submit_profile_record(false, profile_start_time_ms, st.to_string());
632
0
            return st;
633
0
        }
634
0
        if (checksum_before != checksum_after) {
635
0
            auto mismatch_st = Status::InternalError(
636
0
                    "compaction tablet checksum not consistent, before={}, after={}, tablet_id={}",
637
0
                    checksum_before, checksum_after, _tablet->tablet_id());
638
0
            submit_profile_record(false, profile_start_time_ms, mismatch_st.to_string());
639
0
            return mismatch_st;
640
0
        }
641
0
    }
642
643
1.39k
    DorisMetrics::instance()->local_compaction_read_rows_total->increment(_input_row_num);
644
1.39k
    DorisMetrics::instance()->local_compaction_read_bytes_total->increment(
645
1.39k
            _input_rowsets_total_size);
646
647
1.39k
    TEST_SYNC_POINT_RETURN_WITH_VALUE("compaction::CompactionMixin::execute_compact", Status::OK());
648
649
1.39k
    DorisMetrics::instance()->local_compaction_write_rows_total->increment(
650
1.39k
            _output_rowset->num_rows());
651
1.39k
    DorisMetrics::instance()->local_compaction_write_bytes_total->increment(
652
1.39k
            _output_rowset->total_disk_size());
653
654
1.39k
    _load_segment_to_cache();
655
1.39k
    submit_profile_record(true, profile_start_time_ms);
656
1.39k
    return Status::OK();
657
1.39k
}
658
659
1.39k
Status CompactionMixin::execute_compact_impl(int64_t permits) {
660
1.39k
    OlapStopWatch watch;
661
662
1.39k
    if (handle_ordered_data_compaction()) {
663
496
        RETURN_IF_ERROR(modify_rowsets());
664
496
        LOG(INFO) << "succeed to do ordered data " << compaction_name()
665
496
                  << ". tablet=" << _tablet->tablet_id() << ", output_version=" << _output_version
666
496
                  << ", disk=" << tablet()->data_dir()->path()
667
496
                  << ", segments=" << _input_num_segments << ", input_row_num=" << _input_row_num
668
496
                  << ", output_row_num=" << _output_rowset->num_rows()
669
496
                  << ", input_rowsets_data_size=" << _input_rowsets_data_size
670
496
                  << ", input_rowsets_index_size=" << _input_rowsets_index_size
671
496
                  << ", input_rowsets_total_size=" << _input_rowsets_total_size
672
496
                  << ", output_rowset_data_size=" << _output_rowset->data_disk_size()
673
496
                  << ", output_rowset_index_size=" << _output_rowset->index_disk_size()
674
496
                  << ", output_rowset_total_size=" << _output_rowset->total_disk_size()
675
496
                  << ". elapsed time=" << watch.get_elapse_second() << "s.";
676
496
        _state = CompactionState::SUCCESS;
677
496
        return Status::OK();
678
496
    }
679
900
    RETURN_IF_ERROR(build_basic_info());
680
681
900
    TEST_SYNC_POINT_RETURN_WITH_VALUE("compaction::CompactionMixin::execute_compact_impl",
682
900
                                      Status::OK());
683
684
18.4E
    VLOG_DEBUG << "dump tablet schema: " << _cur_tablet_schema->dump_structure();
685
686
900
    LOG(INFO) << "start " << compaction_name() << ". tablet=" << _tablet->tablet_id()
687
900
              << ", output_version=" << _output_version << ", permits: " << permits;
688
689
900
    RETURN_IF_ERROR(merge_input_rowsets());
690
691
    // Currently, updates are only made in the time_series.
692
900
    update_compaction_level();
693
694
900
    RETURN_IF_ERROR(modify_rowsets());
695
696
900
    auto* cumu_policy = tablet()->cumulative_compaction_policy();
697
900
    DCHECK(cumu_policy);
698
900
    LOG(INFO) << "succeed to do " << compaction_name() << " is_vertical=" << _is_vertical
699
900
              << ". tablet=" << _tablet->tablet_id() << ", output_version=" << _output_version
700
900
              << ", current_max_version=" << tablet()->max_version().second
701
900
              << ", disk=" << tablet()->data_dir()->path()
702
900
              << ", input_segments=" << _input_num_segments << ", input_rowsets_data_size="
703
900
              << PrettyPrinter::print_bytes(_input_rowsets_data_size)
704
900
              << ", input_rowsets_index_size="
705
900
              << PrettyPrinter::print_bytes(_input_rowsets_index_size)
706
900
              << ", input_rowsets_total_size="
707
900
              << PrettyPrinter::print_bytes(_input_rowsets_total_size)
708
900
              << ", output_rowset_data_size="
709
900
              << PrettyPrinter::print_bytes(_output_rowset->data_disk_size())
710
900
              << ", output_rowset_index_size="
711
900
              << PrettyPrinter::print_bytes(_output_rowset->index_disk_size())
712
900
              << ", output_rowset_total_size="
713
900
              << PrettyPrinter::print_bytes(_output_rowset->total_disk_size())
714
900
              << ", input_row_num=" << _input_row_num
715
900
              << ", output_row_num=" << _output_rowset->num_rows()
716
900
              << ", filtered_row_num=" << _stats.filtered_rows
717
900
              << ", merged_row_num=" << _stats.merged_rows
718
900
              << ". elapsed time=" << watch.get_elapse_second()
719
900
              << "s. cumulative_compaction_policy=" << cumu_policy->name()
720
900
              << ", compact_row_per_second="
721
900
              << cast_set<double>(_input_row_num) / watch.get_elapse_second();
722
723
900
    _state = CompactionState::SUCCESS;
724
725
900
    return Status::OK();
726
900
}
727
728
8.05k
Status Compaction::do_inverted_index_compaction() {
729
8.05k
    const auto& ctx = _output_rs_writer->context();
730
8.05k
    if (!_enable_inverted_index_compaction || _input_row_num <= 0 ||
731
8.05k
        ctx.columns_to_do_index_compaction.empty()) {
732
7.82k
        return Status::OK();
733
7.82k
    }
734
735
229
    auto error_handler = [this](int64_t index_id, int64_t column_uniq_id) {
736
2
        LOG(WARNING) << "failed to do index compaction"
737
2
                     << ". tablet=" << _tablet->tablet_id() << ". column uniq id=" << column_uniq_id
738
2
                     << ". index_id=" << index_id;
739
4
        for (auto& rowset : _input_rowsets) {
740
4
            rowset->set_skip_index_compaction(cast_set<int32_t>(column_uniq_id));
741
4
            LOG(INFO) << "mark skipping inverted index compaction next time"
742
4
                      << ". tablet=" << _tablet->tablet_id() << ", rowset=" << rowset->rowset_id()
743
4
                      << ", column uniq id=" << column_uniq_id << ", index_id=" << index_id;
744
4
        }
745
2
    };
746
747
229
    DBUG_EXECUTE_IF("Compaction::do_inverted_index_compaction_rowid_conversion_null",
748
229
                    { _stats.rowid_conversion = nullptr; })
749
229
    if (!_stats.rowid_conversion) {
750
0
        LOG(WARNING) << "failed to do index compaction, rowid conversion is null"
751
0
                     << ". tablet=" << _tablet->tablet_id()
752
0
                     << ", input row number=" << _input_row_num;
753
0
        mark_skip_index_compaction(ctx, error_handler);
754
755
0
        return Status::Error<INVERTED_INDEX_COMPACTION_ERROR>(
756
0
                "failed to do index compaction, rowid conversion is null. tablet={}",
757
0
                _tablet->tablet_id());
758
0
    }
759
760
229
    OlapStopWatch inverted_watch;
761
762
    // translation vec
763
    // <<dest_idx_num, dest_docId>>
764
    // the first level vector: index indicates src segment.
765
    // the second level vector: index indicates row id of source segment,
766
    // value indicates row id of destination segment.
767
    // <UINT32_MAX, UINT32_MAX> indicates current row not exist.
768
229
    const auto& trans_vec = _stats.rowid_conversion->get_rowid_conversion_map();
769
770
    // source rowset,segment -> index_id
771
229
    const auto& src_seg_to_id_map = _stats.rowid_conversion->get_src_segment_to_id_map();
772
773
    // dest rowset id
774
229
    RowsetId dest_rowset_id = _stats.rowid_conversion->get_dst_rowset_id();
775
    // dest segment id -> num rows
776
229
    std::vector<uint32_t> dest_segment_num_rows;
777
229
    RETURN_IF_ERROR(_output_rs_writer->get_segment_num_rows(&dest_segment_num_rows));
778
779
229
    auto src_segment_num = src_seg_to_id_map.size();
780
229
    auto dest_segment_num = dest_segment_num_rows.size();
781
782
    // when all the input rowsets are deleted, the output rowset will be empty and dest_segment_num will be 0.
783
229
    if (dest_segment_num <= 0) {
784
2
        LOG(INFO) << "skip doing index compaction due to no output segments"
785
2
                  << ". tablet=" << _tablet->tablet_id() << ", input row number=" << _input_row_num
786
2
                  << ". elapsed time=" << inverted_watch.get_elapse_second() << "s.";
787
2
        return Status::OK();
788
2
    }
789
790
    // Only write info files when debug index compaction is enabled.
791
    // The files are used to debug index compaction and works with index_tool.
792
227
    if (config::debug_inverted_index_compaction) {
793
        // src index files
794
        // format: rowsetId_segmentId
795
0
        std::vector<std::string> src_index_files(src_segment_num);
796
0
        for (const auto& m : src_seg_to_id_map) {
797
0
            std::pair<RowsetId, uint32_t> p = m.first;
798
0
            src_index_files[m.second] = p.first.to_string() + "_" + std::to_string(p.second);
799
0
        }
800
801
        // dest index files
802
        // format: rowsetId_segmentId
803
0
        std::vector<std::string> dest_index_files(dest_segment_num);
804
0
        for (int i = 0; i < dest_segment_num; ++i) {
805
0
            auto prefix = dest_rowset_id.to_string() + "_" + std::to_string(i);
806
0
            dest_index_files[i] = prefix;
807
0
        }
808
809
0
        auto write_json_to_file = [&](const nlohmann::json& json_obj,
810
0
                                      const std::string& file_name) {
811
0
            io::FileWriterPtr file_writer;
812
0
            std::string file_path =
813
0
                    fmt::format("{}/{}.json", std::string(getenv("LOG_DIR")), file_name);
814
0
            RETURN_IF_ERROR(io::global_local_filesystem()->create_file(file_path, &file_writer));
815
0
            RETURN_IF_ERROR(file_writer->append(json_obj.dump()));
816
0
            RETURN_IF_ERROR(file_writer->append("\n"));
817
0
            return file_writer->close();
818
0
        };
819
820
        // Convert trans_vec to JSON and print it
821
0
        nlohmann::json trans_vec_json = trans_vec;
822
0
        auto output_version =
823
0
                _output_version.to_string().substr(1, _output_version.to_string().size() - 2);
824
0
        RETURN_IF_ERROR(write_json_to_file(
825
0
                trans_vec_json,
826
0
                fmt::format("trans_vec_{}_{}", _tablet->tablet_id(), output_version)));
827
828
0
        nlohmann::json src_index_files_json = src_index_files;
829
0
        RETURN_IF_ERROR(write_json_to_file(
830
0
                src_index_files_json,
831
0
                fmt::format("src_idx_dirs_{}_{}", _tablet->tablet_id(), output_version)));
832
833
0
        nlohmann::json dest_index_files_json = dest_index_files;
834
0
        RETURN_IF_ERROR(write_json_to_file(
835
0
                dest_index_files_json,
836
0
                fmt::format("dest_idx_dirs_{}_{}", _tablet->tablet_id(), output_version)));
837
838
0
        nlohmann::json dest_segment_num_rows_json = dest_segment_num_rows;
839
0
        RETURN_IF_ERROR(write_json_to_file(
840
0
                dest_segment_num_rows_json,
841
0
                fmt::format("dest_seg_num_rows_{}_{}", _tablet->tablet_id(), output_version)));
842
0
    }
843
844
    // create index_writer to compaction indexes
845
227
    std::unordered_map<RowsetId, Rowset*> rs_id_to_rowset_map;
846
1.46k
    for (auto&& rs : _input_rowsets) {
847
1.46k
        rs_id_to_rowset_map.emplace(rs->rowset_id(), rs.get());
848
1.46k
    }
849
850
    // src index dirs
851
227
    std::vector<std::unique_ptr<IndexFileReader>> index_file_readers(src_segment_num);
852
972
    for (const auto& m : src_seg_to_id_map) {
853
972
        const auto& [rowset_id, seg_id] = m.first;
854
855
972
        auto find_it = rs_id_to_rowset_map.find(rowset_id);
856
972
        DBUG_EXECUTE_IF("Compaction::do_inverted_index_compaction_find_rowset_error",
857
972
                        { find_it = rs_id_to_rowset_map.end(); })
858
972
        if (find_it == rs_id_to_rowset_map.end()) [[unlikely]] {
859
0
            LOG(WARNING) << "failed to do index compaction, cannot find rowset. tablet_id="
860
0
                         << _tablet->tablet_id() << " rowset_id=" << rowset_id.to_string();
861
0
            mark_skip_index_compaction(ctx, error_handler);
862
0
            return Status::Error<INVERTED_INDEX_COMPACTION_ERROR>(
863
0
                    "failed to do index compaction, cannot find rowset. tablet_id={} rowset_id={}",
864
0
                    _tablet->tablet_id(), rowset_id.to_string());
865
0
        }
866
867
972
        auto* rowset = find_it->second;
868
972
        auto fs = rowset->rowset_meta()->fs();
869
972
        DBUG_EXECUTE_IF("Compaction::do_inverted_index_compaction_get_fs_error", { fs = nullptr; })
870
972
        if (!fs) {
871
0
            LOG(WARNING) << "failed to do index compaction, get fs failed. resource_id="
872
0
                         << rowset->rowset_meta()->resource_id();
873
0
            mark_skip_index_compaction(ctx, error_handler);
874
0
            return Status::Error<INVERTED_INDEX_COMPACTION_ERROR>(
875
0
                    "get fs failed, resource_id={}", rowset->rowset_meta()->resource_id());
876
0
        }
877
878
972
        auto seg_path = rowset->segment_path(seg_id);
879
972
        DBUG_EXECUTE_IF("Compaction::do_inverted_index_compaction_seg_path_nullptr", {
880
972
            seg_path = ResultError(Status::Error<ErrorCode::INTERNAL_ERROR>(
881
972
                    "do_inverted_index_compaction_seg_path_nullptr"));
882
972
        })
883
972
        if (!seg_path.has_value()) {
884
0
            LOG(WARNING) << "failed to do index compaction, get segment path failed. tablet_id="
885
0
                         << _tablet->tablet_id() << " rowset_id=" << rowset_id.to_string()
886
0
                         << " seg_id=" << seg_id;
887
0
            mark_skip_index_compaction(ctx, error_handler);
888
0
            return Status::Error<INVERTED_INDEX_COMPACTION_ERROR>(
889
0
                    "get segment path failed. tablet_id={} rowset_id={} seg_id={}",
890
0
                    _tablet->tablet_id(), rowset_id.to_string(), seg_id);
891
0
        }
892
972
        auto index_file_reader = std::make_unique<IndexFileReader>(
893
972
                fs,
894
972
                std::string {InvertedIndexDescriptor::get_index_file_path_prefix(seg_path.value())},
895
972
                _cur_tablet_schema->get_inverted_index_storage_format(),
896
972
                rowset->rowset_meta()->inverted_index_file_info(seg_id), _tablet->tablet_id());
897
972
        auto st = index_file_reader->init(config::inverted_index_read_buffer_size);
898
972
        DBUG_EXECUTE_IF("Compaction::do_inverted_index_compaction_init_inverted_index_file_reader",
899
972
                        {
900
972
                            st = Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>(
901
972
                                    "debug point: "
902
972
                                    "Compaction::do_inverted_index_compaction_init_inverted_index_"
903
972
                                    "file_reader error");
904
972
                        })
905
972
        if (!st.ok()) {
906
0
            LOG(WARNING) << "failed to do index compaction, init inverted index file reader "
907
0
                            "failed. tablet_id="
908
0
                         << _tablet->tablet_id() << " rowset_id=" << rowset_id.to_string()
909
0
                         << " seg_id=" << seg_id;
910
0
            mark_skip_index_compaction(ctx, error_handler);
911
0
            return Status::Error<INVERTED_INDEX_COMPACTION_ERROR>(
912
0
                    "init inverted index file reader failed. tablet_id={} rowset_id={} seg_id={}",
913
0
                    _tablet->tablet_id(), rowset_id.to_string(), seg_id);
914
0
        }
915
972
        index_file_readers[m.second] = std::move(index_file_reader);
916
972
    }
917
918
    // dest index files
919
    // format: rowsetId_segmentId
920
227
    auto& inverted_index_file_writers =
921
227
            dynamic_cast<BaseBetaRowsetWriter*>(_output_rs_writer.get())->index_file_writers();
922
227
    DBUG_EXECUTE_IF(
923
227
            "Compaction::do_inverted_index_compaction_inverted_index_file_writers_size_error",
924
227
            { inverted_index_file_writers.clear(); })
925
227
    if (inverted_index_file_writers.size() != dest_segment_num) {
926
0
        LOG(WARNING) << "failed to do index compaction, dest segment num not match. tablet_id="
927
0
                     << _tablet->tablet_id() << " dest_segment_num=" << dest_segment_num
928
0
                     << " inverted_index_file_writers.size()="
929
0
                     << inverted_index_file_writers.size();
930
0
        mark_skip_index_compaction(ctx, error_handler);
931
0
        return Status::Error<INVERTED_INDEX_COMPACTION_ERROR>(
932
0
                "dest segment num not match. tablet_id={} dest_segment_num={} "
933
0
                "inverted_index_file_writers.size()={}",
934
0
                _tablet->tablet_id(), dest_segment_num, inverted_index_file_writers.size());
935
0
    }
936
937
    // use tmp file dir to store index files
938
227
    auto tmp_file_dir = ExecEnv::GetInstance()->get_tmp_file_dirs()->get_tmp_file_dir();
939
227
    auto index_tmp_path = tmp_file_dir / dest_rowset_id.to_string();
940
227
    LOG(INFO) << "start index compaction"
941
227
              << ". tablet=" << _tablet->tablet_id() << ", source index size=" << src_segment_num
942
227
              << ", destination index size=" << dest_segment_num << ".";
943
944
227
    Status status = Status::OK();
945
847
    for (auto&& column_uniq_id : ctx.columns_to_do_index_compaction) {
946
847
        auto col = _cur_tablet_schema->column_by_uid(column_uniq_id);
947
847
        auto index_metas = _cur_tablet_schema->inverted_indexs(col);
948
847
        DBUG_EXECUTE_IF("Compaction::do_inverted_index_compaction_can_not_find_index_meta",
949
847
                        { index_metas.clear(); })
950
847
        if (index_metas.empty()) {
951
0
            status = Status::Error<INVERTED_INDEX_COMPACTION_ERROR>(
952
0
                    fmt::format("Can not find index_meta for col {}", col.name()));
953
0
            LOG(WARNING) << "failed to do index compaction, can not find index_meta for column"
954
0
                         << ". tablet=" << _tablet->tablet_id()
955
0
                         << ", column uniq id=" << column_uniq_id;
956
0
            error_handler(-1, column_uniq_id);
957
0
            break;
958
0
        }
959
852
        for (const auto& index_meta : index_metas) {
960
852
            std::vector<lucene::store::Directory*> dest_index_dirs(dest_segment_num);
961
852
            try {
962
852
                std::vector<std::unique_ptr<DorisCompoundReader, DirectoryDeleter>> src_idx_dirs(
963
852
                        src_segment_num);
964
3.85k
                for (int src_segment_id = 0; src_segment_id < src_segment_num; src_segment_id++) {
965
2.99k
                    auto res = index_file_readers[src_segment_id]->open(index_meta);
966
2.99k
                    DBUG_EXECUTE_IF("Compaction::open_inverted_index_file_reader", {
967
2.99k
                        res = ResultError(Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>(
968
2.99k
                                "debug point: Compaction::open_index_file_reader error"));
969
2.99k
                    })
970
2.99k
                    if (!res.has_value()) {
971
0
                        LOG(WARNING) << "failed to do index compaction, open inverted index file "
972
0
                                        "reader failed"
973
0
                                     << ". tablet=" << _tablet->tablet_id()
974
0
                                     << ", column uniq id=" << column_uniq_id
975
0
                                     << ", src_segment_id=" << src_segment_id;
976
0
                        throw Exception(ErrorCode::INVERTED_INDEX_COMPACTION_ERROR,
977
0
                                        res.error().msg());
978
0
                    }
979
2.99k
                    src_idx_dirs[src_segment_id] = std::move(res.value());
980
2.99k
                }
981
1.81k
                for (int dest_segment_id = 0; dest_segment_id < dest_segment_num;
982
963
                     dest_segment_id++) {
983
963
                    auto res = inverted_index_file_writers[dest_segment_id]->open(index_meta);
984
963
                    DBUG_EXECUTE_IF("Compaction::open_inverted_index_file_writer", {
985
963
                        res = ResultError(Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>(
986
963
                                "debug point: Compaction::open_inverted_index_file_writer error"));
987
963
                    })
988
963
                    if (!res.has_value()) {
989
0
                        LOG(WARNING) << "failed to do index compaction, open inverted index file "
990
0
                                        "writer failed"
991
0
                                     << ". tablet=" << _tablet->tablet_id()
992
0
                                     << ", column uniq id=" << column_uniq_id
993
0
                                     << ", dest_segment_id=" << dest_segment_id;
994
0
                        throw Exception(ErrorCode::INVERTED_INDEX_COMPACTION_ERROR,
995
0
                                        res.error().msg());
996
0
                    }
997
                    // Destination directories in dest_index_dirs do not need to be deconstructed,
998
                    // but their lifecycle must be managed by inverted_index_file_writers.
999
963
                    dest_index_dirs[dest_segment_id] = res.value().get();
1000
963
                }
1001
852
                auto st = compact_column(index_meta->index_id(), src_idx_dirs, dest_index_dirs,
1002
852
                                         index_tmp_path.native(), trans_vec, dest_segment_num_rows);
1003
852
                if (!st.ok()) {
1004
2
                    error_handler(index_meta->index_id(), column_uniq_id);
1005
2
                    status = Status::Error<INVERTED_INDEX_COMPACTION_ERROR>(st.msg());
1006
2
                }
1007
852
            } catch (CLuceneError& e) {
1008
0
                error_handler(index_meta->index_id(), column_uniq_id);
1009
0
                status = Status::Error<INVERTED_INDEX_COMPACTION_ERROR>(e.what());
1010
0
            } catch (const Exception& e) {
1011
0
                error_handler(index_meta->index_id(), column_uniq_id);
1012
0
                status = Status::Error<INVERTED_INDEX_COMPACTION_ERROR>(e.what());
1013
0
            }
1014
852
        }
1015
847
    }
1016
1017
    // check index compaction status. If status is not ok, we should return error and end this compaction round.
1018
227
    if (!status.ok()) {
1019
1
        return status;
1020
1
    }
1021
227
    LOG(INFO) << "succeed to do index compaction"
1022
226
              << ". tablet=" << _tablet->tablet_id()
1023
226
              << ". elapsed time=" << inverted_watch.get_elapse_second() << "s.";
1024
1025
226
    return Status::OK();
1026
227
}
1027
1028
void Compaction::mark_skip_index_compaction(
1029
        const RowsetWriterContext& context,
1030
0
        const std::function<void(int64_t, int64_t)>& error_handler) {
1031
0
    for (auto&& column_uniq_id : context.columns_to_do_index_compaction) {
1032
0
        auto col = _cur_tablet_schema->column_by_uid(column_uniq_id);
1033
0
        auto index_metas = _cur_tablet_schema->inverted_indexs(col);
1034
0
        DBUG_EXECUTE_IF("Compaction::mark_skip_index_compaction_can_not_find_index_meta",
1035
0
                        { index_metas.clear(); })
1036
0
        if (index_metas.empty()) {
1037
0
            LOG(WARNING) << "mark skip index compaction, can not find index_meta for column"
1038
0
                         << ". tablet=" << _tablet->tablet_id()
1039
0
                         << ", column uniq id=" << column_uniq_id;
1040
0
            error_handler(-1, column_uniq_id);
1041
0
            continue;
1042
0
        }
1043
0
        for (const auto& index_meta : index_metas) {
1044
0
            error_handler(index_meta->index_id(), column_uniq_id);
1045
0
        }
1046
0
    }
1047
0
}
1048
1049
static bool check_rowset_has_inverted_index(const RowsetSharedPtr& src_rs, int32_t col_unique_id,
1050
                                            const BaseTabletSPtr& tablet,
1051
11.1k
                                            const TabletSchemaSPtr& cur_tablet_schema) {
1052
11.1k
    auto* rowset = static_cast<BetaRowset*>(src_rs.get());
1053
11.1k
    DBUG_EXECUTE_IF("Compaction::construct_skip_inverted_index_is_skip_index_compaction",
1054
11.1k
                    { rowset->set_skip_index_compaction(col_unique_id); })
1055
11.1k
    if (rowset->is_skip_index_compaction(col_unique_id)) {
1056
1
        LOG(WARNING) << "tablet[" << tablet->tablet_id() << "] rowset[" << rowset->rowset_id()
1057
1
                     << "] column_unique_id[" << col_unique_id
1058
1
                     << "] skip inverted index compaction due to last failure";
1059
1
        return false;
1060
1
    }
1061
1062
11.1k
    auto fs = rowset->rowset_meta()->fs();
1063
11.1k
    DBUG_EXECUTE_IF("Compaction::construct_skip_inverted_index_get_fs_error", { fs = nullptr; })
1064
11.1k
    if (!fs) {
1065
2
        LOG(WARNING) << "get fs failed, resource_id=" << rowset->rowset_meta()->resource_id();
1066
2
        return false;
1067
2
    }
1068
1069
11.1k
    auto index_metas = rowset->tablet_schema()->inverted_indexs(col_unique_id);
1070
11.1k
    DBUG_EXECUTE_IF("Compaction::construct_skip_inverted_index_index_meta_nullptr",
1071
11.1k
                    { index_metas.clear(); })
1072
11.1k
    if (index_metas.empty()) {
1073
0
        LOG(WARNING) << "tablet[" << tablet->tablet_id() << "] column_unique_id[" << col_unique_id
1074
0
                     << "] index meta is null, will skip index compaction";
1075
0
        return false;
1076
0
    }
1077
11.3k
    for (const auto& index_meta : index_metas) {
1078
14.7k
        for (auto i = 0; i < rowset->num_segments(); i++) {
1079
            // TODO: inverted_index_path
1080
3.46k
            auto seg_path = rowset->segment_path(i);
1081
3.46k
            DBUG_EXECUTE_IF("Compaction::construct_skip_inverted_index_seg_path_nullptr", {
1082
3.46k
                seg_path = ResultError(Status::Error<ErrorCode::INTERNAL_ERROR>(
1083
3.46k
                        "construct_skip_inverted_index_seg_path_nullptr"));
1084
3.46k
            })
1085
3.46k
            if (!seg_path) {
1086
0
                LOG(WARNING) << seg_path.error();
1087
0
                return false;
1088
0
            }
1089
1090
3.46k
            std::string index_file_path;
1091
3.46k
            try {
1092
3.46k
                auto index_file_reader = std::make_unique<IndexFileReader>(
1093
3.46k
                        fs,
1094
3.46k
                        std::string {InvertedIndexDescriptor::get_index_file_path_prefix(
1095
3.46k
                                seg_path.value())},
1096
3.46k
                        cur_tablet_schema->get_inverted_index_storage_format(),
1097
3.46k
                        rowset->rowset_meta()->inverted_index_file_info(i), tablet->tablet_id());
1098
3.46k
                auto st = index_file_reader->init(config::inverted_index_read_buffer_size);
1099
3.46k
                index_file_path = index_file_reader->get_index_file_path(index_meta);
1100
3.46k
                DBUG_EXECUTE_IF(
1101
3.46k
                        "Compaction::construct_skip_inverted_index_index_file_reader_init_"
1102
3.46k
                        "status_not_ok",
1103
3.46k
                        {
1104
3.46k
                            st = Status::Error<ErrorCode::INTERNAL_ERROR>(
1105
3.46k
                                    "debug point: "
1106
3.46k
                                    "construct_skip_inverted_index_index_file_reader_init_"
1107
3.46k
                                    "status_"
1108
3.46k
                                    "not_ok");
1109
3.46k
                        })
1110
3.46k
                if (!st.ok()) {
1111
0
                    LOG(WARNING) << "init index " << index_file_path << " error:" << st;
1112
0
                    return false;
1113
0
                }
1114
1115
                // check index meta
1116
3.46k
                auto result = index_file_reader->open(index_meta);
1117
3.46k
                DBUG_EXECUTE_IF(
1118
3.46k
                        "Compaction::construct_skip_inverted_index_index_file_reader_open_"
1119
3.46k
                        "error",
1120
3.46k
                        {
1121
3.46k
                            result = ResultError(
1122
3.46k
                                    Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>(
1123
3.46k
                                            "CLuceneError occur when open idx file"));
1124
3.46k
                        })
1125
3.46k
                if (!result.has_value()) {
1126
0
                    LOG(WARNING) << "open index " << index_file_path << " error:" << result.error();
1127
0
                    return false;
1128
0
                }
1129
3.46k
                auto reader = std::move(result.value());
1130
3.46k
                std::vector<std::string> files;
1131
3.46k
                reader->list(&files);
1132
3.46k
                reader->close();
1133
3.46k
                DBUG_EXECUTE_IF(
1134
3.46k
                        "Compaction::construct_skip_inverted_index_index_reader_close_"
1135
3.46k
                        "error",
1136
3.46k
                        { _CLTHROWA(CL_ERR_IO, "debug point: reader close error"); })
1137
1138
3.46k
                DBUG_EXECUTE_IF("Compaction::construct_skip_inverted_index_index_files_count",
1139
3.46k
                                { files.clear(); })
1140
1141
                // why is 3?
1142
                // slice type index file at least has 3 files: null_bitmap, segments_N, segments.gen
1143
3.46k
                if (files.size() < 3) {
1144
0
                    LOG(WARNING) << "tablet[" << tablet->tablet_id() << "] column_unique_id["
1145
0
                                 << col_unique_id << "]," << index_file_path
1146
0
                                 << " is corrupted, will skip index compaction";
1147
0
                    return false;
1148
0
                }
1149
3.46k
            } catch (CLuceneError& err) {
1150
0
                LOG(WARNING) << "tablet[" << tablet->tablet_id() << "] column_unique_id["
1151
0
                             << col_unique_id << "] open index[" << index_file_path
1152
0
                             << "], will skip index compaction, error:" << err.what();
1153
0
                return false;
1154
0
            }
1155
3.46k
        }
1156
11.3k
    }
1157
11.0k
    return true;
1158
11.1k
}
1159
1160
6.74k
void Compaction::construct_index_compaction_columns(RowsetWriterContext& ctx) {
1161
6.74k
    for (const auto& index : _cur_tablet_schema->inverted_indexes()) {
1162
2.74k
        auto col_unique_ids = index->col_unique_ids();
1163
        // check if column unique ids is empty to avoid crash
1164
2.74k
        if (col_unique_ids.empty()) {
1165
1
            LOG(WARNING) << "tablet[" << _tablet->tablet_id() << "] index[" << index->index_id()
1166
1
                         << "] has no column unique id, will skip index compaction."
1167
1
                         << " tablet_schema=" << _cur_tablet_schema->dump_full_schema();
1168
1
            continue;
1169
1
        }
1170
2.73k
        auto col_unique_id = col_unique_ids[0];
1171
2.73k
        if (!_cur_tablet_schema->has_column_unique_id(col_unique_id)) {
1172
0
            LOG(WARNING) << "tablet[" << _tablet->tablet_id() << "] column_unique_id["
1173
0
                         << col_unique_id << "] not found, will skip index compaction";
1174
0
            continue;
1175
0
        }
1176
        // Avoid doing inverted index compaction on non-slice type columns
1177
2.73k
        if (!field_is_slice_type(_cur_tablet_schema->column_by_uid(col_unique_id).type())) {
1178
1.18k
            continue;
1179
1.18k
        }
1180
1181
        // if index properties are different, index compaction maybe needs to be skipped.
1182
1.55k
        bool is_continue = false;
1183
1.55k
        std::optional<std::map<std::string, std::string>> first_properties;
1184
11.1k
        for (const auto& rowset : _input_rowsets) {
1185
11.1k
            auto tablet_indexs = rowset->tablet_schema()->inverted_indexs(col_unique_id);
1186
            // no inverted index or index id is different from current index id
1187
11.1k
            auto it = std::find_if(tablet_indexs.begin(), tablet_indexs.end(),
1188
11.2k
                                   [&index](const auto& tablet_index) {
1189
11.2k
                                       return tablet_index->index_id() == index->index_id();
1190
11.2k
                                   });
1191
11.1k
            if (it != tablet_indexs.end()) {
1192
11.1k
                const auto* tablet_index = *it;
1193
11.1k
                auto properties = tablet_index->properties();
1194
11.1k
                if (!first_properties.has_value()) {
1195
1.55k
                    first_properties = properties;
1196
9.55k
                } else {
1197
9.55k
                    DBUG_EXECUTE_IF(
1198
9.55k
                            "Compaction::do_inverted_index_compaction_index_properties_different",
1199
9.55k
                            { properties.emplace("dummy_key", "dummy_value"); })
1200
9.55k
                    if (properties != first_properties.value()) {
1201
3
                        is_continue = true;
1202
3
                        break;
1203
3
                    }
1204
9.55k
                }
1205
11.1k
            } else {
1206
2
                is_continue = true;
1207
2
                break;
1208
2
            }
1209
11.1k
        }
1210
1.55k
        if (is_continue) {
1211
5
            continue;
1212
5
        }
1213
1.55k
        bool all_have_inverted_index =
1214
1.55k
                std::all_of(_input_rowsets.begin(), _input_rowsets.end(),
1215
11.1k
                            [this, col_unique_id](const RowsetSharedPtr& src_rs) {
1216
11.1k
                                return check_rowset_has_inverted_index(src_rs, col_unique_id,
1217
11.1k
                                                                       _tablet, _cur_tablet_schema);
1218
11.1k
                            });
1219
1220
1.55k
        if (all_have_inverted_index) {
1221
1.55k
            ctx.columns_to_do_index_compaction.insert(col_unique_id);
1222
1.55k
        }
1223
1.55k
    }
1224
6.74k
}
1225
1226
0
Status CompactionMixin::update_delete_bitmap() {
1227
    // for mow with cluster keys, compaction read data with delete bitmap
1228
    // if tablet is not ready(such as schema change), we need to update delete bitmap
1229
0
    {
1230
0
        std::shared_lock meta_rlock(_tablet->get_header_lock());
1231
0
        if (_tablet->tablet_state() != TABLET_NOTREADY) {
1232
0
            return Status::OK();
1233
0
        }
1234
0
    }
1235
0
    OlapStopWatch watch;
1236
0
    std::vector<RowsetSharedPtr> rowsets;
1237
0
    for (const auto& rowset : _input_rowsets) {
1238
0
        std::lock_guard rwlock(tablet()->get_rowset_update_lock());
1239
0
        std::shared_lock rlock(_tablet->get_header_lock());
1240
0
        Status st = _tablet->update_delete_bitmap_without_lock(_tablet, rowset, &rowsets);
1241
0
        if (!st.ok()) {
1242
0
            LOG(INFO) << "failed update_delete_bitmap_without_lock for tablet_id="
1243
0
                      << _tablet->tablet_id() << ", st=" << st.to_string();
1244
0
            return st;
1245
0
        }
1246
0
        rowsets.push_back(rowset);
1247
0
    }
1248
0
    LOG(INFO) << "finish update delete bitmap for tablet: " << _tablet->tablet_id()
1249
0
              << ", rowsets: " << _input_rowsets.size() << ", cost: " << watch.get_elapse_time_us()
1250
0
              << "(us)";
1251
0
    return Status::OK();
1252
0
}
1253
1254
156
Status CloudCompactionMixin::update_delete_bitmap() {
1255
    // for mow with cluster keys, compaction read data with delete bitmap
1256
    // if tablet is not ready(such as schema change), we need to update delete bitmap
1257
156
    {
1258
156
        std::shared_lock meta_rlock(_tablet->get_header_lock());
1259
156
        if (_tablet->tablet_state() != TABLET_NOTREADY) {
1260
156
            return Status::OK();
1261
156
        }
1262
156
    }
1263
0
    OlapStopWatch watch;
1264
0
    std::vector<RowsetSharedPtr> rowsets;
1265
0
    for (const auto& rowset : _input_rowsets) {
1266
0
        Status st = _tablet->update_delete_bitmap_without_lock(_tablet, rowset, &rowsets);
1267
0
        if (!st.ok()) {
1268
0
            LOG(INFO) << "failed update_delete_bitmap_without_lock for tablet_id="
1269
0
                      << _tablet->tablet_id() << ", st=" << st.to_string();
1270
0
            return st;
1271
0
        }
1272
0
        rowsets.push_back(rowset);
1273
0
    }
1274
0
    LOG(INFO) << "finish update delete bitmap for tablet: " << _tablet->tablet_id()
1275
0
              << ", rowsets: " << _input_rowsets.size() << ", cost: " << watch.get_elapse_time_us()
1276
0
              << "(us)";
1277
0
    return Status::OK();
1278
0
}
1279
1280
1.44k
Status CompactionMixin::construct_output_rowset_writer(RowsetWriterContext& ctx) {
1281
    // only do index compaction for dup_keys and unique_keys with mow enabled
1282
1.44k
    if (_enable_inverted_index_compaction && (((_tablet->keys_type() == KeysType::UNIQUE_KEYS &&
1283
1.42k
                                                _tablet->enable_unique_key_merge_on_write()) ||
1284
1.42k
                                               _tablet->keys_type() == KeysType::DUP_KEYS))) {
1285
1.39k
        construct_index_compaction_columns(ctx);
1286
1.39k
    }
1287
1.44k
    ctx.version = _output_version;
1288
1.44k
    ctx.rowset_state = VISIBLE;
1289
1.44k
    ctx.segments_overlap = NONOVERLAPPING;
1290
1.44k
    ctx.tablet_schema = _cur_tablet_schema;
1291
1.44k
    ctx.newest_write_timestamp = _newest_write_timestamp;
1292
1.44k
    ctx.write_type = DataWriteType::TYPE_COMPACTION;
1293
1.44k
    ctx.compaction_type = compaction_type();
1294
1.44k
    ctx.allow_packed_file = false;
1295
1.44k
    _output_rs_writer = DORIS_TRY(_tablet->create_rowset_writer(ctx, _is_vertical));
1296
1.44k
    _pending_rs_guard = _engine.add_pending_rowset(ctx);
1297
1.44k
    return Status::OK();
1298
1.44k
}
1299
1300
1.39k
Status CompactionMixin::modify_rowsets() {
1301
1.39k
    std::vector<RowsetSharedPtr> output_rowsets;
1302
1.39k
    output_rowsets.push_back(_output_rowset);
1303
1304
1.39k
    if (_tablet->keys_type() == KeysType::UNIQUE_KEYS &&
1305
1.39k
        _tablet->enable_unique_key_merge_on_write()) {
1306
622
        Version version = tablet()->max_version();
1307
622
        DeleteBitmap output_rowset_delete_bitmap(_tablet->tablet_id());
1308
622
        std::unique_ptr<RowLocationSet> missed_rows;
1309
622
        if ((config::enable_missing_rows_correctness_check ||
1310
622
             config::enable_mow_compaction_correctness_check_core ||
1311
622
             config::enable_mow_compaction_correctness_check_fail) &&
1312
622
            !_allow_delete_in_cumu_compaction &&
1313
622
            compaction_type() == ReaderType::READER_CUMULATIVE_COMPACTION) {
1314
622
            missed_rows = std::make_unique<RowLocationSet>();
1315
622
            LOG(INFO) << "RowLocation Set inited succ for tablet:" << _tablet->tablet_id();
1316
622
        }
1317
622
        std::unique_ptr<std::map<RowsetSharedPtr, RowLocationPairList>> location_map;
1318
622
        if (config::enable_rowid_conversion_correctness_check &&
1319
622
            tablet()->tablet_schema()->cluster_key_uids().empty()) {
1320
0
            location_map = std::make_unique<std::map<RowsetSharedPtr, RowLocationPairList>>();
1321
0
            LOG(INFO) << "Location Map inited succ for tablet:" << _tablet->tablet_id();
1322
0
        }
1323
        // Convert the delete bitmap of the input rowsets to output rowset.
1324
        // New loads are not blocked, so some keys of input rowsets might
1325
        // be deleted during the time. We need to deal with delete bitmap
1326
        // of incremental data later.
1327
        // TODO(LiaoXin): check if there are duplicate keys
1328
622
        std::size_t missed_rows_size = 0;
1329
622
        tablet()->calc_compaction_output_rowset_delete_bitmap(
1330
622
                _input_rowsets, *_rowid_conversion, 0, version.second + 1, missed_rows.get(),
1331
622
                location_map.get(), _tablet->tablet_meta()->delete_bitmap(),
1332
622
                &output_rowset_delete_bitmap);
1333
622
        if (missed_rows) {
1334
622
            missed_rows_size = missed_rows->size();
1335
622
            std::size_t merged_missed_rows_size = _stats.merged_rows;
1336
622
            if (!_tablet->tablet_meta()->tablet_schema()->cluster_key_uids().empty()) {
1337
0
                merged_missed_rows_size += _stats.filtered_rows;
1338
0
            }
1339
1340
            // Suppose a heavy schema change process on BE converting tablet A to tablet B.
1341
            // 1. during schema change double write, new loads write [X-Y] on tablet B.
1342
            // 2. rowsets with version [a],[a+1],...,[b-1],[b] on tablet B are picked for cumu compaction(X<=a<b<=Y).(cumu compaction
1343
            //    on new tablet during schema change double write is allowed after https://github.com/apache/doris/pull/16470)
1344
            // 3. schema change remove all rowsets on tablet B before version Z(b<=Z<=Y) before it begins to convert historical rowsets.
1345
            // 4. schema change finishes.
1346
            // 5. cumu compation begins on new tablet with version [a],...,[b]. If there are duplicate keys between these rowsets,
1347
            //    the compaction check will fail because these rowsets have skipped to calculate delete bitmap in commit phase and
1348
            //    publish phase because tablet B is in NOT_READY state when writing.
1349
1350
            // Considering that the cumu compaction will fail finally in this situation because `Tablet::modify_rowsets` will check if rowsets in
1351
            // `to_delete`(_input_rowsets) still exist in tablet's `_rs_version_map`, we can just skip to check missed rows here.
1352
622
            bool need_to_check_missed_rows = true;
1353
622
            {
1354
622
                std::shared_lock rlock(_tablet->get_header_lock());
1355
622
                need_to_check_missed_rows =
1356
622
                        std::all_of(_input_rowsets.begin(), _input_rowsets.end(),
1357
4.62k
                                    [&](const RowsetSharedPtr& rowset) {
1358
4.62k
                                        return tablet()->rowset_exists_unlocked(rowset);
1359
4.62k
                                    });
1360
622
            }
1361
1362
622
            if (_tablet->tablet_state() == TABLET_RUNNING &&
1363
622
                merged_missed_rows_size != missed_rows_size && need_to_check_missed_rows) {
1364
0
                std::stringstream ss;
1365
0
                ss << "cumulative compaction: the merged rows(" << _stats.merged_rows
1366
0
                   << "), filtered rows(" << _stats.filtered_rows
1367
0
                   << ") is not equal to missed rows(" << missed_rows_size
1368
0
                   << ") in rowid conversion, tablet_id: " << _tablet->tablet_id()
1369
0
                   << ", table_id:" << _tablet->table_id();
1370
0
                if (missed_rows_size == 0) {
1371
0
                    ss << ", debug info: ";
1372
0
                    DeleteBitmap subset_map(_tablet->tablet_id());
1373
0
                    for (auto rs : _input_rowsets) {
1374
0
                        _tablet->tablet_meta()->delete_bitmap().subset(
1375
0
                                {rs->rowset_id(), 0, 0},
1376
0
                                {rs->rowset_id(), rs->num_segments(), version.second + 1},
1377
0
                                &subset_map);
1378
0
                        ss << "(rowset id: " << rs->rowset_id()
1379
0
                           << ", delete bitmap cardinality: " << subset_map.cardinality() << ")";
1380
0
                    }
1381
0
                    ss << ", version[0-" << version.second + 1 << "]";
1382
0
                }
1383
0
                std::string err_msg = fmt::format(
1384
0
                        "cumulative compaction: the merged rows({}), filtered rows({})"
1385
0
                        " is not equal to missed rows({}) in rowid conversion,"
1386
0
                        " tablet_id: {}, table_id:{}",
1387
0
                        _stats.merged_rows, _stats.filtered_rows, missed_rows_size,
1388
0
                        _tablet->tablet_id(), _tablet->table_id());
1389
0
                LOG(WARNING) << err_msg;
1390
0
                if (config::enable_mow_compaction_correctness_check_core) {
1391
0
                    CHECK(false) << err_msg;
1392
0
                } else if (config::enable_mow_compaction_correctness_check_fail) {
1393
0
                    return Status::InternalError<false>(err_msg);
1394
0
                } else {
1395
0
                    DCHECK(false) << err_msg;
1396
0
                }
1397
0
            }
1398
622
        }
1399
1400
622
        if (location_map) {
1401
0
            RETURN_IF_ERROR(tablet()->check_rowid_conversion(_output_rowset, *location_map));
1402
0
            location_map->clear();
1403
0
        }
1404
1405
622
        {
1406
622
            std::lock_guard<std::mutex> wrlock_(tablet()->get_rowset_update_lock());
1407
622
            std::lock_guard<std::shared_mutex> wrlock(_tablet->get_header_lock());
1408
622
            SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD);
1409
1410
            // Here we will calculate all the rowsets delete bitmaps which are committed but not published to reduce the calculation pressure
1411
            // of publish phase.
1412
            // All rowsets which need to recalculate have been published so we don't need to acquire lock.
1413
            // Step1: collect this tablet's all committed rowsets' delete bitmaps
1414
622
            CommitTabletTxnInfoVec commit_tablet_txn_info_vec {};
1415
622
            _engine.txn_manager()->get_all_commit_tablet_txn_info_by_tablet(
1416
622
                    *tablet(), &commit_tablet_txn_info_vec);
1417
1418
            // Step2: calculate all rowsets' delete bitmaps which are published during compaction.
1419
622
            for (auto& it : commit_tablet_txn_info_vec) {
1420
6
                if (!_check_if_includes_input_rowsets(it.rowset_ids)) {
1421
                    // When calculating the delete bitmap of all committed rowsets relative to the compaction,
1422
                    // there may be cases where the compacted rowsets are newer than the committed rowsets.
1423
                    // At this time, row number conversion cannot be performed, otherwise data will be missing.
1424
                    // Therefore, we need to check if every committed rowset has calculated delete bitmap for
1425
                    // all compaction input rowsets.
1426
0
                    continue;
1427
0
                }
1428
6
                DeleteBitmap txn_output_delete_bitmap(_tablet->tablet_id());
1429
6
                tablet()->calc_compaction_output_rowset_delete_bitmap(
1430
6
                        _input_rowsets, *_rowid_conversion, 0, UINT64_MAX, missed_rows.get(),
1431
6
                        location_map.get(), *it.delete_bitmap.get(), &txn_output_delete_bitmap);
1432
6
                if (config::enable_merge_on_write_correctness_check) {
1433
6
                    RowsetIdUnorderedSet rowsetids;
1434
6
                    rowsetids.insert(_output_rowset->rowset_id());
1435
6
                    _tablet->add_sentinel_mark_to_delete_bitmap(&txn_output_delete_bitmap,
1436
6
                                                                rowsetids);
1437
6
                }
1438
6
                it.delete_bitmap->merge(txn_output_delete_bitmap);
1439
                // Step3: write back updated delete bitmap and tablet info.
1440
6
                it.rowset_ids.insert(_output_rowset->rowset_id());
1441
6
                _engine.txn_manager()->set_txn_related_delete_bitmap(
1442
6
                        it.partition_id, it.transaction_id, _tablet->tablet_id(),
1443
6
                        tablet()->tablet_uid(), true, it.delete_bitmap, it.rowset_ids,
1444
6
                        it.partial_update_info);
1445
6
            }
1446
1447
            // Convert the delete bitmap of the input rowsets to output rowset for
1448
            // incremental data.
1449
622
            tablet()->calc_compaction_output_rowset_delete_bitmap(
1450
622
                    _input_rowsets, *_rowid_conversion, version.second, UINT64_MAX,
1451
622
                    missed_rows.get(), location_map.get(), _tablet->tablet_meta()->delete_bitmap(),
1452
622
                    &output_rowset_delete_bitmap);
1453
1454
622
            if (location_map) {
1455
0
                RETURN_IF_ERROR(tablet()->check_rowid_conversion(_output_rowset, *location_map));
1456
0
            }
1457
1458
622
            tablet()->merge_delete_bitmap(output_rowset_delete_bitmap);
1459
622
            RETURN_IF_ERROR(tablet()->modify_rowsets(output_rowsets, _input_rowsets, true));
1460
622
        }
1461
776
    } else {
1462
776
        std::lock_guard<std::shared_mutex> wrlock(_tablet->get_header_lock());
1463
776
        SCOPED_SIMPLE_TRACE_IF_TIMEOUT(TRACE_TABLET_LOCK_THRESHOLD);
1464
776
        RETURN_IF_ERROR(tablet()->modify_rowsets(output_rowsets, _input_rowsets, true));
1465
776
    }
1466
1467
1.39k
    if (config::tablet_rowset_stale_sweep_by_size &&
1468
1.39k
        _tablet->tablet_meta()->all_stale_rs_metas().size() >=
1469
0
                config::tablet_rowset_stale_sweep_threshold_size) {
1470
0
        tablet()->delete_expired_stale_rowset();
1471
0
    }
1472
1473
1.39k
    int64_t cur_max_version = 0;
1474
1.39k
    {
1475
1.39k
        std::shared_lock rlock(_tablet->get_header_lock());
1476
1.39k
        cur_max_version = _tablet->max_version_unlocked();
1477
1.39k
        tablet()->save_meta();
1478
1.39k
    }
1479
1.39k
    if (_tablet->keys_type() == KeysType::UNIQUE_KEYS &&
1480
1.39k
        _tablet->enable_unique_key_merge_on_write()) {
1481
622
        auto st = TabletMetaManager::remove_old_version_delete_bitmap(
1482
622
                tablet()->data_dir(), _tablet->tablet_id(), cur_max_version);
1483
622
        if (!st.ok()) {
1484
0
            LOG(WARNING) << "failed to remove old version delete bitmap, st: " << st;
1485
0
        }
1486
622
    }
1487
1.39k
    DBUG_EXECUTE_IF("CumulativeCompaction.modify_rowsets.delete_expired_stale_rowset",
1488
1.39k
                    { tablet()->delete_expired_stale_rowset(); });
1489
1.39k
    _tablet->prefill_dbm_agg_cache_after_compaction(_output_rowset);
1490
1.39k
    return Status::OK();
1491
1.39k
}
1492
1493
bool CompactionMixin::_check_if_includes_input_rowsets(
1494
6
        const RowsetIdUnorderedSet& commit_rowset_ids_set) const {
1495
6
    std::vector<RowsetId> commit_rowset_ids {};
1496
6
    commit_rowset_ids.insert(commit_rowset_ids.end(), commit_rowset_ids_set.begin(),
1497
6
                             commit_rowset_ids_set.end());
1498
6
    std::sort(commit_rowset_ids.begin(), commit_rowset_ids.end());
1499
6
    std::vector<RowsetId> input_rowset_ids {};
1500
44
    for (const auto& rowset : _input_rowsets) {
1501
44
        input_rowset_ids.emplace_back(rowset->rowset_meta()->rowset_id());
1502
44
    }
1503
6
    std::sort(input_rowset_ids.begin(), input_rowset_ids.end());
1504
6
    return std::includes(commit_rowset_ids.begin(), commit_rowset_ids.end(),
1505
6
                         input_rowset_ids.begin(), input_rowset_ids.end());
1506
6
}
1507
1508
902
void CompactionMixin::update_compaction_level() {
1509
902
    auto* cumu_policy = tablet()->cumulative_compaction_policy();
1510
902
    if (cumu_policy && cumu_policy->name() == CUMULATIVE_TIME_SERIES_POLICY) {
1511
0
        int64_t compaction_level =
1512
0
                cumu_policy->get_compaction_level(tablet(), _input_rowsets, _output_rowset);
1513
0
        _output_rowset->rowset_meta()->set_compaction_level(compaction_level);
1514
0
    }
1515
902
}
1516
1517
8.01k
Status Compaction::check_correctness() {
1518
    // 1. check row number
1519
8.01k
    if (_input_row_num != _output_rowset->num_rows() + _stats.merged_rows + _stats.filtered_rows) {
1520
0
        return Status::Error<CHECK_LINES_ERROR>(
1521
0
                "row_num does not match between cumulative input and output! tablet={}, "
1522
0
                "input_row_num={}, merged_row_num={}, filtered_row_num={}, output_row_num={}",
1523
0
                _tablet->tablet_id(), _input_row_num, _stats.merged_rows, _stats.filtered_rows,
1524
0
                _output_rowset->num_rows());
1525
0
    }
1526
    // 2. check variant column path stats
1527
8.01k
    RETURN_IF_ERROR(variant_util::VariantCompactionUtil::check_path_stats(_input_rowsets,
1528
8.01k
                                                                          _output_rowset, _tablet));
1529
8.01k
    return Status::OK();
1530
8.01k
}
1531
1532
2.81k
int64_t CompactionMixin::get_compaction_permits() {
1533
2.81k
    int64_t permits = 0;
1534
19.7k
    for (auto&& rowset : _input_rowsets) {
1535
19.7k
        permits += rowset->rowset_meta()->get_compaction_score();
1536
19.7k
    }
1537
2.81k
    return permits;
1538
2.81k
}
1539
1540
0
int64_t CompactionMixin::calc_input_rowsets_total_size() const {
1541
0
    int64_t input_rowsets_total_size = 0;
1542
0
    for (const auto& rowset : _input_rowsets) {
1543
0
        const auto& rowset_meta = rowset->rowset_meta();
1544
0
        auto total_size = rowset_meta->total_disk_size();
1545
0
        input_rowsets_total_size += total_size;
1546
0
    }
1547
0
    return input_rowsets_total_size;
1548
0
}
1549
1550
0
int64_t CompactionMixin::calc_input_rowsets_row_num() const {
1551
0
    int64_t input_rowsets_row_num = 0;
1552
0
    for (const auto& rowset : _input_rowsets) {
1553
0
        const auto& rowset_meta = rowset->rowset_meta();
1554
0
        auto total_size = rowset_meta->total_disk_size();
1555
0
        input_rowsets_row_num += total_size;
1556
0
    }
1557
0
    return input_rowsets_row_num;
1558
0
}
1559
1560
8.43k
void Compaction::_load_segment_to_cache() {
1561
    // Load new rowset's segments to cache.
1562
8.43k
    SegmentCacheHandle handle;
1563
8.43k
    auto st = SegmentLoader::instance()->load_segments(
1564
8.43k
            std::static_pointer_cast<BetaRowset>(_output_rowset), &handle, true);
1565
8.43k
    if (!st.ok()) {
1566
0
        LOG(WARNING) << "failed to load segment to cache! output rowset version="
1567
0
                     << _output_rowset->start_version() << "-" << _output_rowset->end_version()
1568
0
                     << ".";
1569
0
    }
1570
8.43k
}
1571
1572
7.07k
Status CloudCompactionMixin::build_basic_info() {
1573
7.07k
    _output_version =
1574
7.07k
            Version(_input_rowsets.front()->start_version(), _input_rowsets.back()->end_version());
1575
1576
7.07k
    _newest_write_timestamp = _input_rowsets.back()->newest_write_timestamp();
1577
1578
7.07k
    std::vector<RowsetMetaSharedPtr> rowset_metas(_input_rowsets.size());
1579
7.07k
    std::transform(_input_rowsets.begin(), _input_rowsets.end(), rowset_metas.begin(),
1580
54.7k
                   [](const RowsetSharedPtr& rowset) { return rowset->rowset_meta(); });
1581
7.07k
    if (is_index_change_compaction()) {
1582
842
        RETURN_IF_ERROR(rebuild_tablet_schema());
1583
6.23k
    } else {
1584
6.23k
        _cur_tablet_schema = _tablet->tablet_schema_with_merged_max_schema_version(rowset_metas);
1585
6.23k
    }
1586
1587
    // if enable_vertical_compact_variant_subcolumns is true, we need to compact the variant subcolumns in seperate column groups
1588
    // so get_extended_compaction_schema will extended the schema for variant columns
1589
7.09k
    if (_enable_vertical_compact_variant_subcolumns) {
1590
7.09k
        RETURN_IF_ERROR(variant_util::VariantCompactionUtil::get_extended_compaction_schema(
1591
7.09k
                _input_rowsets, _cur_tablet_schema));
1592
7.09k
    }
1593
7.07k
    return Status::OK();
1594
7.07k
}
1595
1596
7.05k
int64_t CloudCompactionMixin::get_compaction_permits() {
1597
7.05k
    int64_t permits = 0;
1598
54.6k
    for (auto&& rowset : _input_rowsets) {
1599
54.6k
        permits += rowset->rowset_meta()->get_compaction_score();
1600
54.6k
    }
1601
7.05k
    return permits;
1602
7.05k
}
1603
1604
CloudCompactionMixin::CloudCompactionMixin(CloudStorageEngine& engine, CloudTabletSPtr tablet,
1605
                                           const std::string& label)
1606
125k
        : Compaction(tablet, label), _engine(engine) {
1607
125k
    auto uuid = UUIDGenerator::instance()->next_uuid();
1608
125k
    std::stringstream ss;
1609
125k
    ss << uuid;
1610
125k
    _uuid = ss.str();
1611
125k
}
1612
1613
7.08k
Status CloudCompactionMixin::execute_compact_impl(int64_t permits) {
1614
7.08k
    OlapStopWatch watch;
1615
1616
7.08k
    RETURN_IF_ERROR(build_basic_info());
1617
1618
7.08k
    LOG(INFO) << "start " << compaction_name() << ". tablet=" << _tablet->tablet_id()
1619
7.08k
              << ", output_version=" << _output_version << ", permits: " << permits;
1620
1621
7.08k
    RETURN_IF_ERROR(merge_input_rowsets());
1622
1623
7.08k
    DBUG_EXECUTE_IF("CloudFullCompaction::modify_rowsets.wrong_rowset_id", {
1624
7.08k
        DCHECK(compaction_type() == ReaderType::READER_FULL_COMPACTION);
1625
7.08k
        RowsetId id;
1626
7.08k
        id.version = 2;
1627
7.08k
        id.hi = _output_rowset->rowset_meta()->rowset_id().hi + ((int64_t)(1) << 56);
1628
7.08k
        id.mi = _output_rowset->rowset_meta()->rowset_id().mi;
1629
7.08k
        id.lo = _output_rowset->rowset_meta()->rowset_id().lo;
1630
7.08k
        _output_rowset->rowset_meta()->set_rowset_id(id);
1631
7.08k
        LOG(INFO) << "[Debug wrong rowset id]:"
1632
7.08k
                  << _output_rowset->rowset_meta()->rowset_id().to_string();
1633
7.08k
    })
1634
1635
    // Currently, updates are only made in the time_series.
1636
7.08k
    update_compaction_level();
1637
1638
7.08k
    RETURN_IF_ERROR(_engine.meta_mgr().commit_rowset(*_output_rowset->rowset_meta().get(), _uuid));
1639
1640
    // 4. modify rowsets in memory
1641
7.08k
    RETURN_IF_ERROR(modify_rowsets());
1642
1643
    // update compaction status data
1644
7.02k
    auto tablet = std::static_pointer_cast<CloudTablet>(_tablet);
1645
7.02k
    tablet->local_read_time_us.fetch_add(_stats.cloud_local_read_time);
1646
7.02k
    tablet->remote_read_time_us.fetch_add(_stats.cloud_remote_read_time);
1647
7.02k
    tablet->exec_compaction_time_us.fetch_add(watch.get_elapse_time_us());
1648
1649
7.02k
    return Status::OK();
1650
7.08k
}
1651
1652
6.99k
int64_t CloudCompactionMixin::initiator() const {
1653
6.99k
    return HashUtil::hash64(_uuid.data(), _uuid.size(), 0) & std::numeric_limits<int64_t>::max();
1654
6.99k
}
1655
1656
namespace cloud {
1657
size_t truncate_rowsets_by_txn_size(std::vector<RowsetSharedPtr>& rowsets, int64_t& kept_size_bytes,
1658
7.50k
                                    int64_t& truncated_size_bytes) {
1659
7.50k
    if (rowsets.empty()) {
1660
1
        kept_size_bytes = 0;
1661
1
        truncated_size_bytes = 0;
1662
1
        return 0;
1663
1
    }
1664
1665
7.50k
    int64_t max_size = config::compaction_txn_max_size_bytes;
1666
7.50k
    int64_t cumulative_meta_size = 0;
1667
7.50k
    size_t keep_count = 0;
1668
1669
65.5k
    for (size_t i = 0; i < rowsets.size(); ++i) {
1670
58.0k
        const auto& rs = rowsets[i];
1671
1672
        // Estimate rowset meta size using doris_rowset_meta_to_cloud
1673
58.0k
        auto cloud_meta = cloud::doris_rowset_meta_to_cloud(rs->rowset_meta()->get_rowset_pb(true));
1674
58.0k
        int64_t rowset_meta_size = cloud_meta.ByteSizeLong();
1675
1676
58.0k
        cumulative_meta_size += rowset_meta_size;
1677
1678
58.0k
        if (keep_count > 0 && cumulative_meta_size > max_size) {
1679
            // Rollback and stop
1680
4
            cumulative_meta_size -= rowset_meta_size;
1681
4
            break;
1682
4
        }
1683
1684
58.0k
        keep_count++;
1685
58.0k
    }
1686
1687
    // Ensure at least 1 rowset is kept
1688
7.50k
    if (keep_count == 0) {
1689
0
        keep_count = 1;
1690
        // Recalculate size for the first rowset
1691
0
        const auto& rs = rowsets[0];
1692
0
        auto cloud_meta = cloud::doris_rowset_meta_to_cloud(rs->rowset_meta()->get_rowset_pb());
1693
0
        cumulative_meta_size = cloud_meta.ByteSizeLong();
1694
0
    }
1695
1696
    // Calculate truncated size
1697
7.50k
    int64_t truncated_total_size = 0;
1698
7.50k
    size_t truncated_count = rowsets.size() - keep_count;
1699
7.50k
    if (truncated_count > 0) {
1700
35
        for (size_t i = keep_count; i < rowsets.size(); ++i) {
1701
31
            auto cloud_meta =
1702
31
                    cloud::doris_rowset_meta_to_cloud(rowsets[i]->rowset_meta()->get_rowset_pb());
1703
31
            truncated_total_size += cloud_meta.ByteSizeLong();
1704
31
        }
1705
4
        rowsets.resize(keep_count);
1706
4
    }
1707
1708
7.50k
    kept_size_bytes = cumulative_meta_size;
1709
7.50k
    truncated_size_bytes = truncated_total_size;
1710
7.50k
    return truncated_count;
1711
7.50k
}
1712
} // namespace cloud
1713
1714
6.64k
size_t CloudCompactionMixin::apply_txn_size_truncation_and_log(const std::string& compaction_name) {
1715
6.64k
    if (_input_rowsets.empty()) {
1716
1
        return 0;
1717
1
    }
1718
1719
6.64k
    int64_t original_count = _input_rowsets.size();
1720
6.64k
    int64_t original_start_version = _input_rowsets.front()->start_version();
1721
6.64k
    int64_t original_end_version = _input_rowsets.back()->end_version();
1722
1723
6.64k
    int64_t final_size = 0;
1724
6.64k
    int64_t truncated_size = 0;
1725
6.64k
    size_t truncated_count =
1726
6.64k
            cloud::truncate_rowsets_by_txn_size(_input_rowsets, final_size, truncated_size);
1727
1728
6.64k
    if (truncated_count > 0) {
1729
2
        int64_t original_size = final_size + truncated_size;
1730
2
        LOG(INFO) << compaction_name << " txn size estimation truncate"
1731
2
                  << ", tablet_id=" << _tablet->tablet_id() << ", original_version_range=["
1732
2
                  << original_start_version << "-" << original_end_version
1733
2
                  << "], final_version_range=[" << _input_rowsets.front()->start_version() << "-"
1734
2
                  << _input_rowsets.back()->end_version()
1735
2
                  << "], original_rowset_count=" << original_count
1736
2
                  << ", final_rowset_count=" << _input_rowsets.size()
1737
2
                  << ", truncated_rowset_count=" << truncated_count
1738
2
                  << ", original_size_bytes=" << original_size
1739
2
                  << ", final_size_bytes=" << final_size
1740
2
                  << ", truncated_size_bytes=" << truncated_size
1741
2
                  << ", threshold_bytes=" << config::compaction_txn_max_size_bytes;
1742
2
    }
1743
1744
6.64k
    return truncated_count;
1745
6.64k
}
1746
1747
7.06k
Status CloudCompactionMixin::execute_compact() {
1748
7.06k
    int64_t profile_start_time_ms = UnixMillis();
1749
7.06k
    TEST_INJECTION_POINT("Compaction::do_compaction");
1750
7.06k
    int64_t permits = get_compaction_permits();
1751
7.06k
    HANDLE_EXCEPTION_IF_CATCH_EXCEPTION(
1752
7.06k
            execute_compact_impl(permits), [&](const doris::Exception& ex) {
1753
7.06k
                auto st = garbage_collection();
1754
7.06k
                if (_tablet->keys_type() == KeysType::UNIQUE_KEYS &&
1755
7.06k
                    _tablet->enable_unique_key_merge_on_write() && !st.ok()) {
1756
                    // if compaction fail, be will try to abort compaction, and delete bitmap lock
1757
                    // will release if abort job successfully, but if abort failed, delete bitmap
1758
                    // lock will not release, in this situation, be need to send this rpc to ms
1759
                    // to try to release delete bitmap lock.
1760
7.06k
                    _engine.meta_mgr().remove_delete_bitmap_update_lock(
1761
7.06k
                            _tablet->table_id(), COMPACTION_DELETE_BITMAP_LOCK_ID, initiator(),
1762
7.06k
                            _tablet->tablet_id());
1763
7.06k
                }
1764
7.06k
                submit_profile_record(false, profile_start_time_ms, ex.what());
1765
7.06k
            });
1766
1767
7.05k
    DorisMetrics::instance()->remote_compaction_read_rows_total->increment(_input_row_num);
1768
7.05k
    DorisMetrics::instance()->remote_compaction_write_rows_total->increment(
1769
7.05k
            _output_rowset->num_rows());
1770
7.05k
    DorisMetrics::instance()->remote_compaction_write_bytes_total->increment(
1771
7.05k
            _output_rowset->total_disk_size());
1772
1773
7.05k
    _load_segment_to_cache();
1774
7.05k
    submit_profile_record(true, profile_start_time_ms);
1775
7.05k
    return Status::OK();
1776
7.06k
}
1777
1778
0
Status CloudCompactionMixin::modify_rowsets() {
1779
0
    return Status::OK();
1780
0
}
1781
1782
7.10k
Status CloudCompactionMixin::set_storage_resource_from_input_rowsets(RowsetWriterContext& ctx) {
1783
    // Set storage resource from input rowsets by iterating backwards to find the first rowset
1784
    // with non-empty resource_id. This handles two scenarios:
1785
    // 1. Hole rowsets compaction: Multiple hole rowsets may lack storage resource.
1786
    //    Example: [0-1, 2-2, 3-3, 4-4, 5-5] where 2-5 are hole rowsets.
1787
    //    If 0-1 lacks resource_id, then 2-5 also lack resource_id.
1788
    // 2. Schema change: New tablet may have later version empty rowsets without resource_id,
1789
    //    but middle rowsets get resource_id after historical rowsets are converted.
1790
    //    We iterate backwards to find the most recent rowset with valid resource_id.
1791
1792
7.10k
    for (const auto& rowset : std::ranges::reverse_view(_input_rowsets)) {
1793
7.10k
        const auto& resource_id = rowset->rowset_meta()->resource_id();
1794
1795
7.11k
        if (!resource_id.empty()) {
1796
7.11k
            ctx.storage_resource = *DORIS_TRY(rowset->rowset_meta()->remote_storage_resource());
1797
7.11k
            return Status::OK();
1798
7.11k
        }
1799
1800
        // Validate that non-empty rowsets (num_segments > 0) must have valid resource_id
1801
        // Only hole rowsets or empty rowsets are allowed to have empty resource_id
1802
18.4E
        if (rowset->num_segments() > 0) {
1803
0
            auto error_msg = fmt::format(
1804
0
                    "Non-empty rowset must have valid resource_id. "
1805
0
                    "rowset_id={}, version=[{}-{}], is_hole_rowset={}, num_segments={}, "
1806
0
                    "tablet_id={}, table_id={}",
1807
0
                    rowset->rowset_id().to_string(), rowset->start_version(), rowset->end_version(),
1808
0
                    rowset->is_hole_rowset(), rowset->num_segments(), _tablet->tablet_id(),
1809
0
                    _tablet->table_id());
1810
1811
0
#ifndef BE_TEST
1812
0
            DCHECK(false) << error_msg;
1813
0
#endif
1814
1815
0
            return Status::InternalError<false>(error_msg);
1816
0
        }
1817
18.4E
    }
1818
1819
18.4E
    return Status::OK();
1820
7.10k
}
1821
1822
7.10k
Status CloudCompactionMixin::construct_output_rowset_writer(RowsetWriterContext& ctx) {
1823
    // only do index compaction for dup_keys and unique_keys with mow enabled
1824
7.10k
    if (_enable_inverted_index_compaction && (((_tablet->keys_type() == KeysType::UNIQUE_KEYS &&
1825
6.26k
                                                _tablet->enable_unique_key_merge_on_write()) ||
1826
6.26k
                                               _tablet->keys_type() == KeysType::DUP_KEYS))) {
1827
5.35k
        construct_index_compaction_columns(ctx);
1828
5.35k
    }
1829
1830
    // Use the storage resource of the previous rowset.
1831
7.10k
    RETURN_IF_ERROR(set_storage_resource_from_input_rowsets(ctx));
1832
1833
7.10k
    ctx.txn_id = boost::uuids::hash_value(UUIDGenerator::instance()->next_uuid()) &
1834
7.10k
                 std::numeric_limits<int64_t>::max(); // MUST be positive
1835
7.10k
    ctx.txn_expiration = _expiration;
1836
1837
7.10k
    ctx.version = _output_version;
1838
7.10k
    ctx.rowset_state = VISIBLE;
1839
7.10k
    ctx.segments_overlap = NONOVERLAPPING;
1840
7.10k
    ctx.tablet_schema = _cur_tablet_schema;
1841
7.10k
    ctx.newest_write_timestamp = _newest_write_timestamp;
1842
7.10k
    ctx.write_type = DataWriteType::TYPE_COMPACTION;
1843
7.10k
    ctx.compaction_type = compaction_type();
1844
7.10k
    ctx.allow_packed_file = false;
1845
1846
    // We presume that the data involved in cumulative compaction is sufficiently 'hot'
1847
    // and should always be retained in the cache.
1848
    // TODO(gavin): Ensure that the retention of hot data is implemented with precision.
1849
1850
7.10k
    ctx.write_file_cache = should_cache_compaction_output();
1851
7.10k
    ctx.file_cache_ttl_sec = _tablet->ttl_seconds();
1852
7.10k
    ctx.approximate_bytes_to_write = _input_rowsets_total_size;
1853
1854
    // Set fine-grained control: only write index files to cache if configured
1855
7.10k
    ctx.compaction_output_write_index_only = should_enable_compaction_cache_index_only(
1856
7.10k
            ctx.write_file_cache, compaction_type(),
1857
7.10k
            config::enable_file_cache_write_base_compaction_index_only,
1858
7.10k
            config::enable_file_cache_write_cumu_compaction_index_only);
1859
1860
7.10k
    ctx.tablet = _tablet;
1861
7.10k
    ctx.job_id = _uuid;
1862
1863
7.10k
    _output_rs_writer = DORIS_TRY(_tablet->create_rowset_writer(ctx, _is_vertical));
1864
7.10k
    RETURN_IF_ERROR(
1865
7.10k
            _engine.meta_mgr().prepare_rowset(*_output_rs_writer->rowset_meta().get(), _uuid));
1866
7.10k
    return Status::OK();
1867
7.10k
}
1868
1869
62
Status CloudCompactionMixin::garbage_collection() {
1870
62
    if (!config::enable_file_cache) {
1871
0
        return Status::OK();
1872
0
    }
1873
62
    if (_output_rs_writer) {
1874
62
        auto* beta_rowset_writer = dynamic_cast<BaseBetaRowsetWriter*>(_output_rs_writer.get());
1875
62
        DCHECK(beta_rowset_writer);
1876
62
        for (const auto& [_, file_writer] : beta_rowset_writer->get_file_writers()) {
1877
57
            auto file_key = io::BlockFileCache::hash(file_writer->path().filename().native());
1878
57
            auto* file_cache = io::FileCacheFactory::instance()->get_by_path(file_key);
1879
57
            file_cache->remove_if_cached_async(file_key);
1880
57
        }
1881
62
        for (const auto& [_, index_writer] : beta_rowset_writer->index_file_writers()) {
1882
1
            for (const auto& file_name : index_writer->get_index_file_names()) {
1883
1
                auto file_key = io::BlockFileCache::hash(file_name);
1884
1
                auto* file_cache = io::FileCacheFactory::instance()->get_by_path(file_key);
1885
1
                file_cache->remove_if_cached_async(file_key);
1886
1
            }
1887
1
        }
1888
62
    }
1889
62
    return Status::OK();
1890
62
}
1891
1892
7.10k
void CloudCompactionMixin::update_compaction_level() {
1893
    // for index change compaction, compaction level should not changed.
1894
    // because input rowset num is 1.
1895
7.10k
    if (is_index_change_compaction()) {
1896
840
        DCHECK(_input_rowsets.size() == 1);
1897
840
        _output_rowset->rowset_meta()->set_compaction_level(
1898
840
                _input_rowsets.back()->rowset_meta()->compaction_level());
1899
6.26k
    } else {
1900
6.26k
        auto compaction_policy = _tablet->tablet_meta()->compaction_policy();
1901
6.26k
        auto cumu_policy = _engine.cumu_compaction_policy(compaction_policy);
1902
6.26k
        if (cumu_policy && cumu_policy->name() == CUMULATIVE_TIME_SERIES_POLICY) {
1903
3
            int64_t compaction_level = cumu_policy->get_compaction_level(
1904
3
                    cloud_tablet(), _input_rowsets, _output_rowset);
1905
3
            _output_rowset->rowset_meta()->set_compaction_level(compaction_level);
1906
3
        }
1907
6.26k
    }
1908
7.10k
}
1909
1910
// should skip hole rowsets, ortherwise the count will be wrong in ms
1911
7.10k
int64_t CloudCompactionMixin::num_input_rowsets() const {
1912
7.10k
    int64_t count = 0;
1913
54.9k
    for (const auto& r : _input_rowsets) {
1914
54.9k
        if (!r->is_hole_rowset()) {
1915
54.9k
            count++;
1916
54.9k
        }
1917
54.9k
    }
1918
7.10k
    return count;
1919
7.10k
}
1920
1921
7.12k
bool CloudCompactionMixin::should_cache_compaction_output() {
1922
7.12k
    if (compaction_type() == ReaderType::READER_CUMULATIVE_COMPACTION) {
1923
6.93k
        return true;
1924
6.93k
    }
1925
1926
186
    if (compaction_type() == ReaderType::READER_BASE_COMPACTION) {
1927
83
        double input_rowsets_hit_cache_ratio = 0.0;
1928
1929
83
        int64_t _input_rowsets_cached_size =
1930
83
                _input_rowsets_cached_data_size + _input_rowsets_cached_index_size;
1931
83
        if (_input_rowsets_total_size > 0) {
1932
76
            input_rowsets_hit_cache_ratio =
1933
76
                    double(_input_rowsets_cached_size) / double(_input_rowsets_total_size);
1934
76
        }
1935
1936
83
        LOG(INFO) << "CloudBaseCompaction should_cache_compaction_output"
1937
83
                  << ", tablet_id=" << _tablet->tablet_id()
1938
83
                  << ", input_rowsets_hit_cache_ratio=" << input_rowsets_hit_cache_ratio
1939
83
                  << ", _input_rowsets_cached_size=" << _input_rowsets_cached_size
1940
83
                  << ", _input_rowsets_total_size=" << _input_rowsets_total_size
1941
83
                  << ", enable_file_cache_keep_base_compaction_output="
1942
83
                  << config::enable_file_cache_keep_base_compaction_output
1943
83
                  << ", file_cache_keep_base_compaction_output_min_hit_ratio="
1944
83
                  << config::file_cache_keep_base_compaction_output_min_hit_ratio;
1945
1946
83
        if (config::enable_file_cache_keep_base_compaction_output) {
1947
0
            return true;
1948
0
        }
1949
1950
83
        if (input_rowsets_hit_cache_ratio >
1951
83
            config::file_cache_keep_base_compaction_output_min_hit_ratio) {
1952
47
            return true;
1953
47
        }
1954
83
    }
1955
139
    return false;
1956
186
}
1957
1958
} // namespace doris