Coverage Report

Created: 2025-07-23 20:08

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
protected:
79
    Status merge_input_rowsets();
80
81
    // merge inverted index files
82
    Status do_inverted_index_compaction();
83
84
    // mark all columns in columns_to_do_index_compaction to skip index compaction next time.
85
    void mark_skip_index_compaction(const RowsetWriterContext& context,
86
                                    const std::function<void(int64_t, int64_t)>& error_handler);
87
88
    void construct_index_compaction_columns(RowsetWriterContext& ctx);
89
90
    virtual Status construct_output_rowset_writer(RowsetWriterContext& ctx) = 0;
91
92
    Status check_correctness();
93
94
    int64_t get_avg_segment_rows();
95
96
    void init_profile(const std::string& label);
97
98
    void _load_segment_to_cache();
99
100
    int64_t merge_way_num();
101
102
    virtual Status update_delete_bitmap() = 0;
103
104
    // the root tracker for this compaction
105
    std::shared_ptr<MemTrackerLimiter> _mem_tracker;
106
107
    BaseTabletSPtr _tablet;
108
109
    std::vector<RowsetSharedPtr> _input_rowsets;
110
    int64_t _input_rowsets_data_size {0};
111
    int64_t _input_rowsets_index_size {0};
112
    int64_t _input_rowsets_total_size {0};
113
    int64_t _input_row_num {0};
114
    int64_t _input_num_segments {0};
115
116
    int64_t _local_read_bytes_total {};
117
    int64_t _remote_read_bytes_total {};
118
119
    Merger::Statistics _stats;
120
121
    RowsetSharedPtr _output_rowset;
122
    std::unique_ptr<RowsetWriter> _output_rs_writer;
123
124
    enum CompactionState : uint8_t { INITED = 0, SUCCESS = 1 };
125
    CompactionState _state {CompactionState::INITED};
126
127
    bool _is_vertical;
128
    bool _allow_delete_in_cumu_compaction;
129
130
    Version _output_version;
131
132
    int64_t _newest_write_timestamp {-1};
133
    std::unique_ptr<RowIdConversion> _rowid_conversion = nullptr;
134
    TabletSchemaSPtr _cur_tablet_schema;
135
136
    std::unique_ptr<RuntimeProfile> _profile;
137
138
    RuntimeProfile::Counter* _input_rowsets_data_size_counter = nullptr;
139
    RuntimeProfile::Counter* _input_rowsets_counter = nullptr;
140
    RuntimeProfile::Counter* _input_row_num_counter = nullptr;
141
    RuntimeProfile::Counter* _input_segments_num_counter = nullptr;
142
    RuntimeProfile::Counter* _merged_rows_counter = nullptr;
143
    RuntimeProfile::Counter* _filtered_rows_counter = nullptr;
144
    RuntimeProfile::Counter* _output_rowset_data_size_counter = nullptr;
145
    RuntimeProfile::Counter* _output_row_num_counter = nullptr;
146
    RuntimeProfile::Counter* _output_segments_num_counter = nullptr;
147
    RuntimeProfile::Counter* _merge_rowsets_latency_timer = nullptr;
148
};
149
150
// `StorageEngine` mixin for `Compaction`
151
class CompactionMixin : public Compaction {
152
public:
153
    CompactionMixin(StorageEngine& engine, TabletSharedPtr tablet, const std::string& label);
154
155
    ~CompactionMixin() override;
156
157
    Status execute_compact() override;
158
159
    int64_t get_compaction_permits();
160
161
0
    int64_t initiator() const { return INVALID_COMPACTION_INITIATOR_ID; }
162
163
    int64_t calc_input_rowsets_total_size() const;
164
165
    int64_t calc_input_rowsets_row_num() const;
166
167
protected:
168
    // Convert `_tablet` from `BaseTablet` to `Tablet`
169
    Tablet* tablet();
170
171
    Status construct_output_rowset_writer(RowsetWriterContext& ctx) override;
172
173
    virtual Status modify_rowsets();
174
175
    Status update_delete_bitmap() override;
176
177
    StorageEngine& _engine;
178
179
private:
180
    Status execute_compact_impl(int64_t permits);
181
182
    void build_basic_info();
183
184
    // Return true if do ordered data compaction successfully
185
    bool handle_ordered_data_compaction();
186
187
    Status do_compact_ordered_rowsets();
188
189
    bool _check_if_includes_input_rowsets(const RowsetIdUnorderedSet& commit_rowset_ids_set) const;
190
191
    void update_compaction_level();
192
193
    PendingRowsetGuard _pending_rs_guard;
194
};
195
196
class CloudCompactionMixin : public Compaction {
197
public:
198
    CloudCompactionMixin(CloudStorageEngine& engine, CloudTabletSPtr tablet,
199
                         const std::string& label);
200
201
0
    ~CloudCompactionMixin() override = default;
202
203
    Status execute_compact() override;
204
205
    int64_t initiator() const;
206
207
protected:
208
0
    CloudTablet* cloud_tablet() { return static_cast<CloudTablet*>(_tablet.get()); }
209
210
    Status update_delete_bitmap() override;
211
212
    virtual Status garbage_collection();
213
214
    CloudStorageEngine& _engine;
215
216
    std::string _uuid;
217
218
    int64_t _expiration = 0;
219
220
private:
221
    Status construct_output_rowset_writer(RowsetWriterContext& ctx) override;
222
223
    Status execute_compact_impl(int64_t permits);
224
225
    void build_basic_info();
226
227
    virtual Status modify_rowsets();
228
229
    int64_t get_compaction_permits();
230
231
    void update_compaction_level();
232
};
233
234
} // namespace doris