Coverage Report

Created: 2025-06-11 07:23

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