Coverage Report

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