/root/doris/be/src/olap/base_compaction.cpp
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 | | #include "olap/base_compaction.h" |
19 | | |
20 | | #include <gen_cpp/olap_file.pb.h> |
21 | | |
22 | | #include <memory> |
23 | | #include <mutex> |
24 | | #include <ostream> |
25 | | |
26 | | #include "common/cast_set.h" |
27 | | #include "common/config.h" |
28 | | #include "common/logging.h" |
29 | | #include "olap/compaction.h" |
30 | | #include "olap/olap_define.h" |
31 | | #include "olap/rowset/rowset_meta.h" |
32 | | #include "olap/tablet.h" |
33 | | #include "runtime/thread_context.h" |
34 | | #include "util/doris_metrics.h" |
35 | | #include "util/thread.h" |
36 | | #include "util/trace.h" |
37 | | |
38 | | namespace doris { |
39 | | #include "common/compile_check_begin.h" |
40 | | |
41 | | using namespace ErrorCode; |
42 | | |
43 | | BaseCompaction::BaseCompaction(StorageEngine& engine, const TabletSharedPtr& tablet) |
44 | 34 | : CompactionMixin(engine, tablet, "BaseCompaction:" + std::to_string(tablet->tablet_id())) { |
45 | 34 | } |
46 | | |
47 | 34 | BaseCompaction::~BaseCompaction() = default; |
48 | | |
49 | 0 | Status BaseCompaction::prepare_compact() { |
50 | 0 | Status st; |
51 | 0 | Defer defer_set_st([&] { |
52 | 0 | if (!st.ok()) { |
53 | 0 | tablet()->set_last_base_compaction_status(st.to_string()); |
54 | 0 | tablet()->set_last_base_compaction_failure_time(UnixMillis()); |
55 | 0 | } |
56 | 0 | }); |
57 | |
|
58 | 0 | if (!tablet()->init_succeeded()) { |
59 | 0 | st = Status::Error<INVALID_ARGUMENT, false>("_tablet init failed"); |
60 | 0 | return st; |
61 | 0 | } |
62 | | |
63 | 0 | std::unique_lock<std::mutex> lock(tablet()->get_base_compaction_lock(), std::try_to_lock); |
64 | 0 | if (!lock.owns_lock()) { |
65 | 0 | st = Status::Error<TRY_LOCK_FAILED, false>("another base compaction is running. tablet={}", |
66 | 0 | _tablet->tablet_id()); |
67 | 0 | return st; |
68 | 0 | } |
69 | | |
70 | | // 1. pick rowsets to compact |
71 | 0 | st = pick_rowsets_to_compact(); |
72 | 0 | RETURN_IF_ERROR(st); |
73 | 0 | COUNTER_UPDATE(_input_rowsets_counter, _input_rowsets.size()); |
74 | |
|
75 | 0 | st = Status::OK(); |
76 | 0 | return st; |
77 | 0 | } |
78 | | |
79 | 0 | Status BaseCompaction::execute_compact() { |
80 | 0 | #ifndef __APPLE__ |
81 | 0 | if (config::enable_base_compaction_idle_sched) { |
82 | 0 | Thread::set_idle_sched(); |
83 | 0 | } |
84 | 0 | #endif |
85 | 0 | Status st; |
86 | 0 | Defer defer_set_st([&] { |
87 | 0 | tablet()->set_last_base_compaction_status(st.to_string()); |
88 | 0 | if (!st.ok()) { |
89 | 0 | tablet()->set_last_base_compaction_failure_time(UnixMillis()); |
90 | 0 | } else { |
91 | 0 | tablet()->set_last_base_compaction_success_time(UnixMillis()); |
92 | 0 | } |
93 | 0 | }); |
94 | |
|
95 | 0 | std::unique_lock<std::mutex> lock(tablet()->get_base_compaction_lock(), std::try_to_lock); |
96 | 0 | if (!lock.owns_lock()) { |
97 | 0 | st = Status::Error<TRY_LOCK_FAILED, false>("another base compaction is running. tablet={}", |
98 | 0 | _tablet->tablet_id()); |
99 | 0 | return st; |
100 | 0 | } |
101 | | |
102 | 0 | SCOPED_ATTACH_TASK(_mem_tracker); |
103 | |
|
104 | 0 | st = CompactionMixin::execute_compact(); |
105 | 0 | RETURN_IF_ERROR(st); |
106 | | |
107 | 0 | DCHECK_EQ(_state, CompactionState::SUCCESS); |
108 | |
|
109 | 0 | DorisMetrics::instance()->base_compaction_deltas_total->increment(_input_rowsets.size()); |
110 | 0 | DorisMetrics::instance()->base_compaction_bytes_total->increment(_input_rowsets_total_size); |
111 | |
|
112 | 0 | st = Status::OK(); |
113 | 0 | return st; |
114 | 0 | } |
115 | | |
116 | 1 | void BaseCompaction::_filter_input_rowset() { |
117 | | // if dup_key and no delete predicate |
118 | | // we skip big files to save resources |
119 | 1 | if (_tablet->keys_type() != KeysType::DUP_KEYS) { |
120 | 0 | return; |
121 | 0 | } |
122 | 24 | for (auto& rs : _input_rowsets) { |
123 | 24 | if (rs->rowset_meta()->has_delete_predicate()) { |
124 | 0 | return; |
125 | 0 | } |
126 | 24 | } |
127 | 1 | int64_t max_size = config::base_compaction_dup_key_max_file_size_mbytes * 1024 * 1024; |
128 | | // first find a proper rowset for start |
129 | 1 | auto rs_iter = _input_rowsets.begin(); |
130 | 1 | while (rs_iter != _input_rowsets.end()) { |
131 | 1 | if ((*rs_iter)->rowset_meta()->total_disk_size() >= max_size) { |
132 | 0 | rs_iter = _input_rowsets.erase(rs_iter); |
133 | 1 | } else { |
134 | 1 | break; |
135 | 1 | } |
136 | 1 | } |
137 | 1 | } |
138 | | |
139 | 1 | Status BaseCompaction::pick_rowsets_to_compact() { |
140 | 1 | _input_rowsets = tablet()->pick_candidate_rowsets_to_base_compaction(); |
141 | 1 | RETURN_IF_ERROR(check_version_continuity(_input_rowsets)); |
142 | 1 | _filter_input_rowset(); |
143 | 1 | if (_input_rowsets.size() <= 1) { |
144 | 0 | return Status::Error<BE_NO_SUITABLE_VERSION>("_input_rowsets.size() is 1"); |
145 | 0 | } |
146 | | |
147 | | // There are two occasions, first is that we set enable_delete_when_cumu_compaction false: |
148 | | // If there are delete predicate rowsets in tablet, start_version > 0 implies some rowsets before |
149 | | // delete version cannot apply these delete predicates, which can cause incorrect query result. |
150 | | // So we must abort this base compaction. |
151 | | // A typical scenario is that some rowsets before cumulative point are on remote storage. |
152 | | // For example, consider rowset[0,3] is on remote storage, now we pass [4,4],[5,5],[6,9] |
153 | | // to do base compaction and rowset[5,5] is delete predicate rowset, if we allow them to do |
154 | | // such procedure, then we'll get [4,9] while it will lose the delete predicate information in [5,5] |
155 | | // which rusult in data in [0,3] will not be deleted. |
156 | | // Another occasion is that we set enable_delete_when_cumu_compaction true: |
157 | | // Then whatever the _input_rowsets.front()->start_version() > 0 or not, once the output |
158 | | // rowset's start version is bigger than 2, we'll always remain the delete pred information inside |
159 | | // the output rowset so the rowsets whose version is less than _input_rowsets.front()->start_version() > 0 |
160 | | // would apply the delete pred in the end. |
161 | 1 | if (!_allow_delete_in_cumu_compaction && _input_rowsets.front()->start_version() > 0) { |
162 | 0 | bool has_delete_predicate = false; |
163 | 0 | for (const auto& rs : _input_rowsets) { |
164 | 0 | if (rs->rowset_meta()->has_delete_predicate()) { |
165 | 0 | has_delete_predicate = true; |
166 | 0 | break; |
167 | 0 | } |
168 | 0 | } |
169 | 0 | if (has_delete_predicate) { |
170 | 0 | return Status::Error<BE_NO_SUITABLE_VERSION>( |
171 | 0 | "Some rowsets cannot apply delete predicates in base compaction. tablet_id={}", |
172 | 0 | _tablet->tablet_id()); |
173 | 0 | } |
174 | 0 | } |
175 | | |
176 | 1 | if (_input_rowsets.size() == 2 && _input_rowsets[0]->end_version() == 1) { |
177 | 0 | return Status::Error<BE_NO_SUITABLE_VERSION>( |
178 | 0 | "the tablet is with rowset: [0-1], [2-y], and [0-1] has no data. in this " |
179 | 0 | "situation, no need to do base compaction."); |
180 | 0 | } |
181 | | |
182 | 1 | int score = 0; |
183 | 1 | int rowset_cnt = 0; |
184 | 21 | while (rowset_cnt < _input_rowsets.size()) { |
185 | 21 | score += _input_rowsets[rowset_cnt++]->rowset_meta()->get_compaction_score(); |
186 | 21 | if (score > config::base_compaction_max_compaction_score) { |
187 | 1 | break; |
188 | 1 | } |
189 | 21 | } |
190 | 1 | _input_rowsets.resize(rowset_cnt); |
191 | | |
192 | | // 1. cumulative rowset must reach base_compaction_num_cumulative_deltas threshold |
193 | 1 | if (_input_rowsets.size() > config::base_compaction_min_rowset_num) { |
194 | 1 | VLOG_NOTICE << "satisfy the base compaction policy. tablet=" << _tablet->tablet_id() |
195 | 0 | << ", num_cumulative_rowsets=" << _input_rowsets.size() - 1 |
196 | 0 | << ", base_compaction_num_cumulative_rowsets=" |
197 | 0 | << config::base_compaction_min_rowset_num; |
198 | 1 | return Status::OK(); |
199 | 1 | } |
200 | | |
201 | | // 2. the ratio between base rowset and all input cumulative rowsets reaches the threshold |
202 | | // `_input_rowsets` has been sorted by end version, so we consider `_input_rowsets[0]` is the base rowset. |
203 | 0 | int64_t base_size = _input_rowsets.front()->data_disk_size(); |
204 | 0 | int64_t cumulative_total_size = 0; |
205 | 0 | for (auto it = _input_rowsets.begin() + 1; it != _input_rowsets.end(); ++it) { |
206 | 0 | cumulative_total_size += (*it)->data_disk_size(); |
207 | 0 | } |
208 | |
|
209 | 0 | double min_data_ratio = config::base_compaction_min_data_ratio; |
210 | 0 | if (base_size == 0) { |
211 | | // base_size == 0 means this may be a base version [0-1], which has no data. |
212 | | // set to 1 to void divide by zero |
213 | 0 | base_size = 1; |
214 | 0 | } |
215 | 0 | double cumulative_base_ratio = |
216 | 0 | cast_set<double>(cumulative_total_size) / cast_set<double>(base_size); |
217 | |
|
218 | 0 | if (cumulative_base_ratio > min_data_ratio) { |
219 | 0 | VLOG_NOTICE << "satisfy the base compaction policy. tablet=" << _tablet->tablet_id() |
220 | 0 | << ", cumulative_total_size=" << cumulative_total_size |
221 | 0 | << ", base_size=" << base_size |
222 | 0 | << ", cumulative_base_ratio=" << cumulative_base_ratio |
223 | 0 | << ", policy_min_data_ratio=" << min_data_ratio; |
224 | 0 | return Status::OK(); |
225 | 0 | } |
226 | | |
227 | | // 3. the interval since last base compaction reaches the threshold |
228 | 0 | int64_t base_creation_time = _input_rowsets[0]->creation_time(); |
229 | 0 | int64_t interval_threshold = config::base_compaction_interval_seconds_since_last_operation; |
230 | 0 | int64_t interval_since_last_base_compaction = time(nullptr) - base_creation_time; |
231 | 0 | if (interval_since_last_base_compaction > interval_threshold) { |
232 | 0 | VLOG_NOTICE << "satisfy the base compaction policy. tablet=" << _tablet->tablet_id() |
233 | 0 | << ", interval_since_last_base_compaction=" |
234 | 0 | << interval_since_last_base_compaction |
235 | 0 | << ", interval_threshold=" << interval_threshold; |
236 | 0 | return Status::OK(); |
237 | 0 | } |
238 | | |
239 | 0 | return Status::Error<BE_NO_SUITABLE_VERSION>( |
240 | 0 | "don't satisfy the base compaction policy. tablet={}, num_cumulative_rowsets={}, " |
241 | 0 | "cumulative_base_ratio={}, interval_since_last_base_compaction={}", |
242 | 0 | _tablet->tablet_id(), _input_rowsets.size() - 1, cumulative_base_ratio, |
243 | 0 | interval_since_last_base_compaction); |
244 | 0 | } |
245 | | |
246 | | } // namespace doris |