Coverage Report

Created: 2026-05-29 19:02

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
0
    virtual int8_t compaction_level() const { return -1; }
80
81
    // Returns compaction profile type for task tracking.
82
    // Default returns std::nullopt (not tracked). Only base/cumulative/full override.
83
0
    virtual std::optional<CompactionProfileType> profile_type() const { return std::nullopt; }
84
85
    // Accessors for tracker registration
86
21
    int64_t compaction_id() const { return _compaction_id; }
87
21
    int64_t input_rowsets_data_size() const { return _input_rowsets_data_size; }
88
21
    int64_t input_rowsets_index_size() const { return _input_rowsets_index_size; }
89
21
    int64_t input_rowsets_total_size() const { return _input_rowsets_total_size; }
90
21
    int64_t input_row_num_value() const { return _input_row_num; }
91
21
    int64_t input_rowsets_count() const { return static_cast<int64_t>(_input_rowsets.size()); }
92
21
    virtual int64_t input_segments_num_value() const { return _input_num_segments; }
93
21
    bool is_vertical() const { return _is_vertical; }
94
    std::string input_version_range_str() const;
95
96
    // the difference between index change compmaction and other compaction.
97
    // 1. delete predicate should be kept when input is cumu rowset.
98
    // 2. inverted compaction should be skipped.
99
    // 3. compute level should not be changed.
100
2
    virtual bool is_index_change_compaction() { return false; }
101
102
private:
103
    void set_delete_predicate_for_output_rowset();
104
105
protected:
106
    Status merge_input_rowsets();
107
108
    // merge inverted index files
109
    Status do_inverted_index_compaction();
110
111
    // mark all columns in columns_to_do_index_compaction to skip index compaction next time.
112
    void mark_skip_index_compaction(const RowsetWriterContext& context,
113
                                    const std::function<void(int64_t, int64_t)>& error_handler);
114
115
    void construct_index_compaction_columns(RowsetWriterContext& ctx);
116
117
    virtual Status construct_output_rowset_writer(RowsetWriterContext& ctx) = 0;
118
119
    Status check_correctness();
120
121
    int64_t get_avg_segment_rows();
122
123
    void init_profile(const std::string& label);
124
125
    void _load_segment_to_cache();
126
127
    int64_t merge_way_num();
128
129
    virtual Status update_delete_bitmap() = 0;
130
131
    int64_t _compaction_id {0};
132
133
    void submit_profile_record(bool success, int64_t start_time_ms,
134
                               const std::string& status_msg = "");
135
136
    // the root tracker for this compaction
137
    std::shared_ptr<MemTrackerLimiter> _mem_tracker;
138
139
    BaseTabletSPtr _tablet;
140
141
    std::vector<RowsetSharedPtr> _input_rowsets;
142
    int64_t _input_rowsets_data_size {0};
143
    int64_t _input_rowsets_index_size {0};
144
    int64_t _input_rowsets_total_size {0};
145
    int64_t _input_row_num {0};
146
    int64_t _input_num_segments {0};
147
148
    int64_t _local_read_bytes_total {};
149
    int64_t _remote_read_bytes_total {};
150
151
    int64_t _input_rowsets_cached_data_size {0};
152
    int64_t _input_rowsets_cached_index_size {0};
153
154
    Merger::Statistics _stats;
155
156
    RowsetSharedPtr _output_rowset;
157
    std::unique_ptr<RowsetWriter> _output_rs_writer;
158
159
    enum CompactionState : uint8_t { INITED = 0, SUCCESS = 1 };
160
    CompactionState _state {CompactionState::INITED};
161
162
    bool _is_vertical;
163
    bool _is_ordered_data_compaction {false};
164
    bool _allow_delete_in_cumu_compaction;
165
    bool _enable_vertical_compact_variant_subcolumns;
166
167
    Version _output_version;
168
169
    int64_t _newest_write_timestamp {-1};
170
    std::unique_ptr<RowIdConversion> _rowid_conversion = nullptr;
171
    TabletSchemaSPtr _cur_tablet_schema;
172
173
    std::unique_ptr<RuntimeProfile> _profile;
174
175
    bool _enable_inverted_index_compaction {false};
176
177
    RuntimeProfile::Counter* _input_rowsets_data_size_counter = nullptr;
178
    RuntimeProfile::Counter* _input_rowsets_counter = nullptr;
179
    RuntimeProfile::Counter* _input_row_num_counter = nullptr;
180
    RuntimeProfile::Counter* _input_segments_num_counter = nullptr;
181
    RuntimeProfile::Counter* _merged_rows_counter = nullptr;
182
    RuntimeProfile::Counter* _filtered_rows_counter = nullptr;
183
    RuntimeProfile::Counter* _output_rowset_data_size_counter = nullptr;
184
    RuntimeProfile::Counter* _output_row_num_counter = nullptr;
185
    RuntimeProfile::Counter* _output_segments_num_counter = nullptr;
186
    RuntimeProfile::Counter* _merge_rowsets_latency_timer = nullptr;
187
};
188
189
// `StorageEngine` mixin for `Compaction`
190
class CompactionMixin : public Compaction {
191
public:
192
    CompactionMixin(StorageEngine& engine, TabletSharedPtr tablet, const std::string& label);
193
194
    ~CompactionMixin() override;
195
196
    Status execute_compact() override;
197
198
    int64_t get_compaction_permits();
199
200
0
    int64_t initiator() const { return INVALID_COMPACTION_INITIATOR_ID; }
201
202
    int64_t calc_input_rowsets_total_size() const;
203
204
    int64_t calc_input_rowsets_row_num() const;
205
206
protected:
207
    // Convert `_tablet` from `BaseTablet` to `Tablet`
208
    Tablet* tablet();
209
210
    Status construct_output_rowset_writer(RowsetWriterContext& ctx) override;
211
212
    virtual Status modify_rowsets();
213
214
    Status update_delete_bitmap() override;
215
216
    void find_longest_consecutive_version(std::vector<RowsetSharedPtr>* rowsets,
217
                                          std::vector<Version>* missing_version);
218
219
    StorageEngine& _engine;
220
221
private:
222
    Status execute_compact_impl(int64_t permits);
223
224
    Status build_basic_info(bool is_ordered_compaction = false);
225
226
    // Return true if do ordered data compaction successfully
227
    bool handle_ordered_data_compaction();
228
229
    Status do_compact_ordered_rowsets();
230
231
    bool _check_if_includes_input_rowsets(const RowsetIdUnorderedSet& commit_rowset_ids_set) const;
232
233
    void update_compaction_level();
234
235
    PendingRowsetGuard _pending_rs_guard;
236
};
237
238
class CloudCompactionMixin : public Compaction {
239
public:
240
    CloudCompactionMixin(CloudStorageEngine& engine, CloudTabletSPtr tablet,
241
                         const std::string& label);
242
243
24
    ~CloudCompactionMixin() override = default;
244
245
    Status execute_compact() override;
246
247
    int64_t initiator() const;
248
249
    int64_t num_input_rowsets() const;
250
251
protected:
252
27
    CloudTablet* cloud_tablet() { return static_cast<CloudTablet*>(_tablet.get()); }
253
254
    Status update_delete_bitmap() override;
255
256
    virtual Status garbage_collection();
257
258
    // Helper function to apply truncation and log the result
259
    // Returns the number of rowsets that were truncated
260
    size_t apply_txn_size_truncation_and_log(const std::string& compaction_name);
261
262
    CloudStorageEngine& _engine;
263
264
    std::string _uuid;
265
266
    int64_t _expiration = 0;
267
268
0
    virtual Status rebuild_tablet_schema() { return Status::OK(); }
269
270
private:
271
    Status construct_output_rowset_writer(RowsetWriterContext& ctx) override;
272
273
    Status set_storage_resource_from_input_rowsets(RowsetWriterContext& ctx);
274
275
    Status execute_compact_impl(int64_t permits);
276
277
    Status build_basic_info();
278
279
    virtual Status modify_rowsets();
280
281
    int64_t get_compaction_permits();
282
283
    void update_compaction_level();
284
285
    bool should_cache_compaction_output();
286
};
287
288
namespace cloud {
289
290
// Truncate rowsets based on estimated transaction metadata size
291
// Returns the number of rowsets that were truncated (removed from the end)
292
// Only considers rowset meta size (using doris_rowset_meta_to_cloud for estimation)
293
size_t truncate_rowsets_by_txn_size(std::vector<RowsetSharedPtr>& rowsets, int64_t& kept_size_bytes,
294
                                    int64_t& truncated_size_bytes);
295
296
} // namespace cloud
297
298
} // namespace doris