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