Coverage Report

Created: 2026-04-09 15:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/compaction/compaction.h
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
#pragma once
19
20
#include <cstdint>
21
// clang20 + -O1 causes warnings about pass-failed
22
// error: loop not unrolled: the optimizer was unable to perform the requested transformation; the transformation might be disabled or specified as part of an unsupported transformation ordering [-Werror,-Wpass-failed=transform-warning]
23
#if defined(__clang__)
24
#pragma clang diagnostic push
25
#pragma clang diagnostic ignored "-Wpass-failed"
26
#endif
27
#include <memory>
28
#if defined(__clang__)
29
#pragma clang diagnostic pop
30
#endif
31
#include <optional>
32
#include <string>
33
#include <vector>
34
35
#include "cloud/cloud_tablet.h"
36
#include "common/status.h"
37
#include "io/io_common.h"
38
#include "runtime/runtime_profile.h"
39
#include "storage/compaction_task_tracker.h"
40
#include "storage/merger.h"
41
#include "storage/olap_common.h"
42
#include "storage/rowid_conversion.h"
43
#include "storage/rowset/pending_rowset_helper.h"
44
#include "storage/rowset/rowset_fwd.h"
45
#include "storage/tablet/tablet_fwd.h"
46
47
namespace doris {
48
49
class MemTrackerLimiter;
50
class RowsetWriter;
51
struct RowsetWriterContext;
52
class StorageEngine;
53
class CloudStorageEngine;
54
55
static constexpr int COMPACTION_DELETE_BITMAP_LOCK_ID = -1;
56
static constexpr int64_t INVALID_COMPACTION_INITIATOR_ID = -100;
57
// This class is a base class for compaction.
58
// The entrance of this class is compact()
59
// Any compaction should go through four procedures.
60
//  1. pick rowsets satisfied to compact
61
//  2. do compaction
62
//  3. modify rowsets
63
//  4. gc output rowset if failed
64
class Compaction {
65
public:
66
    Compaction(BaseTabletSPtr tablet, const std::string& label);
67
    virtual ~Compaction();
68
69
    // Pick input rowsets satisfied to compact
70
    virtual Status prepare_compact() = 0;
71
72
    // Merge input rowsets to output rowset and modify tablet meta
73
    virtual Status execute_compact() = 0;
74
75
0
    RuntimeProfile* runtime_profile() const { return _profile.get(); }
76
77
    virtual ReaderType compaction_type() const = 0;
78
    virtual std::string_view compaction_name() const = 0;
79
80
    // Returns compaction profile type for task tracking.
81
    // Default returns std::nullopt (not tracked). Only base/cumulative/full override.
82
0
    virtual std::optional<CompactionProfileType> profile_type() const { return std::nullopt; }
83
84
    // Accessors for tracker registration
85
21
    int64_t compaction_id() const { return _compaction_id; }
86
21
    int64_t input_rowsets_data_size() const { return _input_rowsets_data_size; }
87
21
    int64_t input_rowsets_index_size() const { return _input_rowsets_index_size; }
88
21
    int64_t input_rowsets_total_size() const { return _input_rowsets_total_size; }
89
21
    int64_t input_row_num_value() const { return _input_row_num; }
90
21
    int64_t input_rowsets_count() const { return static_cast<int64_t>(_input_rowsets.size()); }
91
21
    virtual int64_t input_segments_num_value() const { return _input_num_segments; }
92
21
    bool is_vertical() const { return _is_vertical; }
93
    std::string input_version_range_str() const;
94
95
    // the difference between index change compmaction and other compaction.
96
    // 1. delete predicate should be kept when input is cumu rowset.
97
    // 2. inverted compaction should be skipped.
98
    // 3. compute level should not be changed.
99
2
    virtual bool is_index_change_compaction() { return false; }
100
101
private:
102
    void set_delete_predicate_for_output_rowset();
103
104
protected:
105
    Status merge_input_rowsets();
106
107
    // merge inverted index files
108
    Status do_inverted_index_compaction();
109
110
    // mark all columns in columns_to_do_index_compaction to skip index compaction next time.
111
    void mark_skip_index_compaction(const RowsetWriterContext& context,
112
                                    const std::function<void(int64_t, int64_t)>& error_handler);
113
114
    void construct_index_compaction_columns(RowsetWriterContext& ctx);
115
116
    virtual Status construct_output_rowset_writer(RowsetWriterContext& ctx) = 0;
117
118
    Status check_correctness();
119
120
    int64_t get_avg_segment_rows();
121
122
    void init_profile(const std::string& label);
123
124
    void _load_segment_to_cache();
125
126
    int64_t merge_way_num();
127
128
    virtual Status update_delete_bitmap() = 0;
129
130
    int64_t _compaction_id {0};
131
132
    void submit_profile_record(bool success, int64_t start_time_ms,
133
                               const std::string& status_msg = "");
134
135
    // the root tracker for this compaction
136
    std::shared_ptr<MemTrackerLimiter> _mem_tracker;
137
138
    BaseTabletSPtr _tablet;
139
140
    std::vector<RowsetSharedPtr> _input_rowsets;
141
    int64_t _input_rowsets_data_size {0};
142
    int64_t _input_rowsets_index_size {0};
143
    int64_t _input_rowsets_total_size {0};
144
    int64_t _input_row_num {0};
145
    int64_t _input_num_segments {0};
146
147
    int64_t _local_read_bytes_total {};
148
    int64_t _remote_read_bytes_total {};
149
150
    int64_t _input_rowsets_cached_data_size {0};
151
    int64_t _input_rowsets_cached_index_size {0};
152
153
    Merger::Statistics _stats;
154
155
    RowsetSharedPtr _output_rowset;
156
    std::unique_ptr<RowsetWriter> _output_rs_writer;
157
158
    enum CompactionState : uint8_t { INITED = 0, SUCCESS = 1 };
159
    CompactionState _state {CompactionState::INITED};
160
161
    bool _is_vertical;
162
    bool _allow_delete_in_cumu_compaction;
163
    bool _enable_vertical_compact_variant_subcolumns;
164
165
    Version _output_version;
166
167
    int64_t _newest_write_timestamp {-1};
168
    std::unique_ptr<RowIdConversion> _rowid_conversion = nullptr;
169
    TabletSchemaSPtr _cur_tablet_schema;
170
171
    std::unique_ptr<RuntimeProfile> _profile;
172
173
    bool _enable_inverted_index_compaction {false};
174
175
    RuntimeProfile::Counter* _input_rowsets_data_size_counter = nullptr;
176
    RuntimeProfile::Counter* _input_rowsets_counter = nullptr;
177
    RuntimeProfile::Counter* _input_row_num_counter = nullptr;
178
    RuntimeProfile::Counter* _input_segments_num_counter = nullptr;
179
    RuntimeProfile::Counter* _merged_rows_counter = nullptr;
180
    RuntimeProfile::Counter* _filtered_rows_counter = nullptr;
181
    RuntimeProfile::Counter* _output_rowset_data_size_counter = nullptr;
182
    RuntimeProfile::Counter* _output_row_num_counter = nullptr;
183
    RuntimeProfile::Counter* _output_segments_num_counter = nullptr;
184
    RuntimeProfile::Counter* _merge_rowsets_latency_timer = nullptr;
185
};
186
187
// `StorageEngine` mixin for `Compaction`
188
class CompactionMixin : public Compaction {
189
public:
190
    CompactionMixin(StorageEngine& engine, TabletSharedPtr tablet, const std::string& label);
191
192
    ~CompactionMixin() override;
193
194
    Status execute_compact() override;
195
196
    int64_t get_compaction_permits();
197
198
0
    int64_t initiator() const { return INVALID_COMPACTION_INITIATOR_ID; }
199
200
    int64_t calc_input_rowsets_total_size() const;
201
202
    int64_t calc_input_rowsets_row_num() const;
203
204
protected:
205
    // Convert `_tablet` from `BaseTablet` to `Tablet`
206
    Tablet* tablet();
207
208
    Status construct_output_rowset_writer(RowsetWriterContext& ctx) override;
209
210
    virtual Status modify_rowsets();
211
212
    Status update_delete_bitmap() override;
213
214
    StorageEngine& _engine;
215
216
private:
217
    Status execute_compact_impl(int64_t permits);
218
219
    Status build_basic_info(bool is_ordered_compaction = false);
220
221
    // Return true if do ordered data compaction successfully
222
    bool handle_ordered_data_compaction();
223
224
    Status do_compact_ordered_rowsets();
225
226
    bool _check_if_includes_input_rowsets(const RowsetIdUnorderedSet& commit_rowset_ids_set) const;
227
228
    void update_compaction_level();
229
230
    PendingRowsetGuard _pending_rs_guard;
231
};
232
233
class CloudCompactionMixin : public Compaction {
234
public:
235
    CloudCompactionMixin(CloudStorageEngine& engine, CloudTabletSPtr tablet,
236
                         const std::string& label);
237
238
24
    ~CloudCompactionMixin() override = default;
239
240
    Status execute_compact() override;
241
242
    int64_t initiator() const;
243
244
    int64_t num_input_rowsets() const;
245
246
protected:
247
27
    CloudTablet* cloud_tablet() { return static_cast<CloudTablet*>(_tablet.get()); }
248
249
    Status update_delete_bitmap() override;
250
251
    virtual Status garbage_collection();
252
253
    // Helper function to apply truncation and log the result
254
    // Returns the number of rowsets that were truncated
255
    size_t apply_txn_size_truncation_and_log(const std::string& compaction_name);
256
257
    CloudStorageEngine& _engine;
258
259
    std::string _uuid;
260
261
    int64_t _expiration = 0;
262
263
0
    virtual Status rebuild_tablet_schema() { return Status::OK(); }
264
265
private:
266
    Status construct_output_rowset_writer(RowsetWriterContext& ctx) override;
267
268
    Status set_storage_resource_from_input_rowsets(RowsetWriterContext& ctx);
269
270
    Status execute_compact_impl(int64_t permits);
271
272
    Status build_basic_info();
273
274
    virtual Status modify_rowsets();
275
276
    int64_t get_compaction_permits();
277
278
    void update_compaction_level();
279
280
    bool should_cache_compaction_output();
281
};
282
283
namespace cloud {
284
285
// Truncate rowsets based on estimated transaction metadata size
286
// Returns the number of rowsets that were truncated (removed from the end)
287
// Only considers rowset meta size (using doris_rowset_meta_to_cloud for estimation)
288
size_t truncate_rowsets_by_txn_size(std::vector<RowsetSharedPtr>& rowsets, int64_t& kept_size_bytes,
289
                                    int64_t& truncated_size_bytes);
290
291
} // namespace cloud
292
293
} // namespace doris