Coverage Report

Created: 2025-09-10 18:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/olap/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 <string>
32
#include <vector>
33
34
#include "cloud/cloud_tablet.h"
35
#include "common/status.h"
36
#include "io/io_common.h"
37
#include "olap/merger.h"
38
#include "olap/olap_common.h"
39
#include "olap/rowid_conversion.h"
40
#include "olap/rowset/pending_rowset_helper.h"
41
#include "olap/rowset/rowset_fwd.h"
42
#include "olap/tablet_fwd.h"
43
#include "util/runtime_profile.h"
44
45
namespace doris {
46
47
class MemTrackerLimiter;
48
class RowsetWriter;
49
struct RowsetWriterContext;
50
class StorageEngine;
51
class CloudStorageEngine;
52
53
static constexpr int COMPACTION_DELETE_BITMAP_LOCK_ID = -1;
54
static constexpr int64_t INVALID_COMPACTION_INITIATOR_ID = -100;
55
// This class is a base class for compaction.
56
// The entrance of this class is compact()
57
// Any compaction should go through four procedures.
58
//  1. pick rowsets satisfied to compact
59
//  2. do compaction
60
//  3. modify rowsets
61
//  4. gc output rowset if failed
62
class Compaction {
63
public:
64
    Compaction(BaseTabletSPtr tablet, const std::string& label);
65
    virtual ~Compaction();
66
67
    // Pick input rowsets satisfied to compact
68
    virtual Status prepare_compact() = 0;
69
70
    // Merge input rowsets to output rowset and modify tablet meta
71
    virtual Status execute_compact() = 0;
72
73
0
    RuntimeProfile* runtime_profile() const { return _profile.get(); }
74
75
    virtual ReaderType compaction_type() const = 0;
76
    virtual std::string_view compaction_name() const = 0;
77
78
    // the difference between index change compmaction and other compaction.
79
    // 1. delete predicate should be kept when input is cumu rowset.
80
    // 2. inverted compaction should be skipped.
81
    // 3. compute level should not be changed.
82
2
    virtual bool is_index_change_compaction() { return false; }
83
84
private:
85
    void set_delete_predicate_for_output_rowset();
86
87
protected:
88
    Status merge_input_rowsets();
89
90
    // merge inverted index files
91
    Status do_inverted_index_compaction();
92
93
    // mark all columns in columns_to_do_index_compaction to skip index compaction next time.
94
    void mark_skip_index_compaction(const RowsetWriterContext& context,
95
                                    const std::function<void(int64_t, int64_t)>& error_handler);
96
97
    void construct_index_compaction_columns(RowsetWriterContext& ctx);
98
99
    virtual Status construct_output_rowset_writer(RowsetWriterContext& ctx) = 0;
100
101
    Status check_correctness();
102
103
    int64_t get_avg_segment_rows();
104
105
    void init_profile(const std::string& label);
106
107
    void _load_segment_to_cache();
108
109
    int64_t merge_way_num();
110
111
    virtual Status update_delete_bitmap() = 0;
112
113
    // the root tracker for this compaction
114
    std::shared_ptr<MemTrackerLimiter> _mem_tracker;
115
116
    BaseTabletSPtr _tablet;
117
118
    std::vector<RowsetSharedPtr> _input_rowsets;
119
    int64_t _input_rowsets_data_size {0};
120
    int64_t _input_rowsets_index_size {0};
121
    int64_t _input_rowsets_total_size {0};
122
    int64_t _input_row_num {0};
123
    int64_t _input_num_segments {0};
124
125
    int64_t _local_read_bytes_total {};
126
    int64_t _remote_read_bytes_total {};
127
128
    Merger::Statistics _stats;
129
130
    RowsetSharedPtr _output_rowset;
131
    std::unique_ptr<RowsetWriter> _output_rs_writer;
132
133
    enum CompactionState : uint8_t { INITED = 0, SUCCESS = 1 };
134
    CompactionState _state {CompactionState::INITED};
135
136
    bool _is_vertical;
137
    bool _allow_delete_in_cumu_compaction;
138
    bool _enable_vertical_compact_variant_subcolumns;
139
140
    Version _output_version;
141
142
    int64_t _newest_write_timestamp {-1};
143
    std::unique_ptr<RowIdConversion> _rowid_conversion = nullptr;
144
    TabletSchemaSPtr _cur_tablet_schema;
145
146
    std::unique_ptr<RuntimeProfile> _profile;
147
148
    bool _enable_inverted_index_compaction {false};
149
150
    RuntimeProfile::Counter* _input_rowsets_data_size_counter = nullptr;
151
    RuntimeProfile::Counter* _input_rowsets_counter = nullptr;
152
    RuntimeProfile::Counter* _input_row_num_counter = nullptr;
153
    RuntimeProfile::Counter* _input_segments_num_counter = nullptr;
154
    RuntimeProfile::Counter* _merged_rows_counter = nullptr;
155
    RuntimeProfile::Counter* _filtered_rows_counter = nullptr;
156
    RuntimeProfile::Counter* _output_rowset_data_size_counter = nullptr;
157
    RuntimeProfile::Counter* _output_row_num_counter = nullptr;
158
    RuntimeProfile::Counter* _output_segments_num_counter = nullptr;
159
    RuntimeProfile::Counter* _merge_rowsets_latency_timer = nullptr;
160
};
161
162
// `StorageEngine` mixin for `Compaction`
163
class CompactionMixin : public Compaction {
164
public:
165
    CompactionMixin(StorageEngine& engine, TabletSharedPtr tablet, const std::string& label);
166
167
    ~CompactionMixin() override;
168
169
    Status execute_compact() override;
170
171
    int64_t get_compaction_permits();
172
173
0
    int64_t initiator() const { return INVALID_COMPACTION_INITIATOR_ID; }
174
175
    int64_t calc_input_rowsets_total_size() const;
176
177
    int64_t calc_input_rowsets_row_num() const;
178
179
protected:
180
    // Convert `_tablet` from `BaseTablet` to `Tablet`
181
    Tablet* tablet();
182
183
    Status construct_output_rowset_writer(RowsetWriterContext& ctx) override;
184
185
    virtual Status modify_rowsets();
186
187
    Status update_delete_bitmap() override;
188
189
    StorageEngine& _engine;
190
191
private:
192
    Status execute_compact_impl(int64_t permits);
193
194
    Status build_basic_info(bool is_ordered_compaction = false);
195
196
    // Return true if do ordered data compaction successfully
197
    bool handle_ordered_data_compaction();
198
199
    Status do_compact_ordered_rowsets();
200
201
    bool _check_if_includes_input_rowsets(const RowsetIdUnorderedSet& commit_rowset_ids_set) const;
202
203
    void update_compaction_level();
204
205
    PendingRowsetGuard _pending_rs_guard;
206
};
207
208
class CloudCompactionMixin : public Compaction {
209
public:
210
    CloudCompactionMixin(CloudStorageEngine& engine, CloudTabletSPtr tablet,
211
                         const std::string& label);
212
213
18
    ~CloudCompactionMixin() override = default;
214
215
    Status execute_compact() override;
216
217
    int64_t initiator() const;
218
219
    int64_t num_input_rowsets() const;
220
221
protected:
222
27
    CloudTablet* cloud_tablet() { return static_cast<CloudTablet*>(_tablet.get()); }
223
224
    Status update_delete_bitmap() override;
225
226
    virtual Status garbage_collection();
227
228
    CloudStorageEngine& _engine;
229
230
    std::string _uuid;
231
232
    int64_t _expiration = 0;
233
234
0
    virtual Status rebuild_tablet_schema() { return Status::OK(); }
235
236
private:
237
    Status construct_output_rowset_writer(RowsetWriterContext& ctx) override;
238
239
    Status set_storage_resource_from_input_rowsets(RowsetWriterContext& ctx);
240
241
    Status execute_compact_impl(int64_t permits);
242
243
    Status build_basic_info();
244
245
    virtual Status modify_rowsets();
246
247
    int64_t get_compaction_permits();
248
249
    void update_compaction_level();
250
};
251
252
} // namespace doris