Coverage Report

Created: 2025-06-04 14:42

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