/root/doris/be/src/olap/cumulative_compaction.cpp
Line | Count | Source |
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/cumulative_compaction.h" |
19 | | |
20 | | #include <cpp/sync_point.h> |
21 | | #include <gen_cpp/AgentService_types.h> |
22 | | #include <gen_cpp/Types_types.h> |
23 | | |
24 | | #include <memory> |
25 | | #include <mutex> |
26 | | #include <ostream> |
27 | | #include <vector> |
28 | | |
29 | | #include "common/config.h" |
30 | | #include "common/logging.h" |
31 | | #include "olap/cumulative_compaction_policy.h" |
32 | | #include "olap/cumulative_compaction_time_series_policy.h" |
33 | | #include "olap/olap_define.h" |
34 | | #include "olap/rowset/rowset_meta.h" |
35 | | #include "olap/storage_engine.h" |
36 | | #include "olap/tablet.h" |
37 | | #include "runtime/exec_env.h" |
38 | | #include "runtime/thread_context.h" |
39 | | #include "util/doris_metrics.h" |
40 | | #include "util/time.h" |
41 | | #include "util/trace.h" |
42 | | |
43 | | namespace doris { |
44 | | #include "common/compile_check_begin.h" |
45 | | using namespace ErrorCode; |
46 | | |
47 | | void CumulativeCompaction::find_longest_consecutive_version(std::vector<RowsetSharedPtr>* rowsets, |
48 | 28 | std::vector<Version>* missing_version) { |
49 | 28 | if (rowsets->empty()) { |
50 | 0 | return; |
51 | 0 | } |
52 | | |
53 | 28 | RowsetSharedPtr prev_rowset = rowsets->front(); |
54 | 28 | int i = 1; |
55 | 28 | int max_start = 0; |
56 | 28 | int max_length = 1; |
57 | | |
58 | 28 | int start = 0; |
59 | 28 | int length = 1; |
60 | 631 | for (; i < rowsets->size(); ++i) { |
61 | 603 | RowsetSharedPtr rowset = (*rowsets)[i]; |
62 | 603 | if (rowset->start_version() != prev_rowset->end_version() + 1) { |
63 | 6 | if (missing_version != nullptr) { |
64 | 6 | missing_version->push_back(prev_rowset->version()); |
65 | 6 | missing_version->push_back(rowset->version()); |
66 | 6 | } |
67 | 6 | start = i; |
68 | 6 | length = 1; |
69 | 597 | } else { |
70 | 597 | length++; |
71 | 597 | } |
72 | | |
73 | 603 | if (length > max_length) { |
74 | 587 | max_start = start; |
75 | 587 | max_length = length; |
76 | 587 | } |
77 | | |
78 | 603 | prev_rowset = rowset; |
79 | 603 | } |
80 | 28 | *rowsets = {rowsets->begin() + max_start, rowsets->begin() + max_start + max_length}; |
81 | 28 | } |
82 | | |
83 | | CumulativeCompaction::CumulativeCompaction(StorageEngine& engine, const TabletSharedPtr& tablet) |
84 | 33 | : CompactionMixin(engine, tablet, |
85 | 33 | "CumulativeCompaction:" + std::to_string(tablet->tablet_id())) {} |
86 | | |
87 | 33 | CumulativeCompaction::~CumulativeCompaction() = default; |
88 | | |
89 | 21 | Status CumulativeCompaction::prepare_compact() { |
90 | 21 | Status st; |
91 | 21 | Defer defer_set_st([&] { |
92 | 21 | if (!st.ok()) { |
93 | 0 | tablet()->set_last_cumu_compaction_status(st.to_string()); |
94 | 0 | } |
95 | 21 | }); |
96 | | |
97 | 21 | if (!tablet()->init_succeeded()) { |
98 | 0 | st = Status::Error<CUMULATIVE_INVALID_PARAMETERS, false>("_tablet init failed"); |
99 | 0 | return st; |
100 | 0 | } |
101 | | |
102 | 21 | std::unique_lock<std::mutex> lock(tablet()->get_cumulative_compaction_lock(), std::try_to_lock); |
103 | 21 | if (!lock.owns_lock()) { |
104 | 0 | st = Status::Error<TRY_LOCK_FAILED, false>( |
105 | 0 | "The tablet is under cumulative compaction. tablet={}", _tablet->tablet_id()); |
106 | 0 | return st; |
107 | 0 | } |
108 | | |
109 | 21 | tablet()->calculate_cumulative_point(); |
110 | 21 | VLOG_CRITICAL << "after calculate, current cumulative point is " |
111 | 0 | << tablet()->cumulative_layer_point() << ", tablet=" << _tablet->tablet_id(); |
112 | | |
113 | 21 | st = pick_rowsets_to_compact(); |
114 | 21 | RETURN_IF_ERROR(st); |
115 | | |
116 | 21 | COUNTER_UPDATE(_input_rowsets_counter, _input_rowsets.size()); |
117 | | |
118 | 21 | st = Status::OK(); |
119 | 21 | return st; |
120 | 21 | } |
121 | | |
122 | 1 | Status CumulativeCompaction::execute_compact() { |
123 | 1 | DBUG_EXECUTE_IF("CumulativeCompaction::execute_compact.block", { |
124 | 1 | auto target_tablet_id = dp->param<int64_t>("tablet_id", -1); |
125 | 1 | if (target_tablet_id == _tablet->tablet_id()) { |
126 | 1 | LOG(INFO) << "start debug block " |
127 | 1 | << "CumulativeCompaction::execute_compact.block"; |
128 | 1 | while (DebugPoints::instance()->is_enable( |
129 | 1 | "CumulativeCompaction::execute_compact.block")) { |
130 | 1 | std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
131 | 1 | } |
132 | 1 | LOG(INFO) << "end debug block " |
133 | 1 | << "CumulativeCompaction::execute_compact.block"; |
134 | 1 | } |
135 | 1 | }) |
136 | | |
137 | 1 | Status st; |
138 | 1 | Defer defer_set_st([&] { |
139 | 1 | tablet()->set_last_cumu_compaction_status(st.to_string()); |
140 | 1 | if (!st.ok()) { |
141 | 0 | tablet()->set_last_cumu_compaction_failure_time(UnixMillis()); |
142 | 1 | } else { |
143 | | // TIME_SERIES_POLICY, generating an empty rowset doesn't need to update the timestamp. |
144 | 1 | if (!(tablet()->tablet_meta()->compaction_policy() == CUMULATIVE_TIME_SERIES_POLICY && |
145 | 1 | _output_rowset->num_segments() == 0)) { |
146 | 1 | tablet()->set_last_cumu_compaction_success_time(UnixMillis()); |
147 | 1 | } |
148 | 1 | } |
149 | 1 | }); |
150 | 1 | std::unique_lock<std::mutex> lock(tablet()->get_cumulative_compaction_lock(), std::try_to_lock); |
151 | 1 | if (!lock.owns_lock()) { |
152 | 0 | st = Status::Error<TRY_LOCK_FAILED, false>( |
153 | 0 | "The tablet is under cumulative compaction. tablet={}", _tablet->tablet_id()); |
154 | 0 | return st; |
155 | 0 | } |
156 | | |
157 | 1 | SCOPED_ATTACH_TASK(_mem_tracker); |
158 | | |
159 | 1 | st = CompactionMixin::execute_compact(); |
160 | 1 | RETURN_IF_ERROR(st); |
161 | | |
162 | 1 | TEST_SYNC_POINT_RETURN_WITH_VALUE( |
163 | 0 | "cumulative_compaction::CumulativeCompaction::execute_compact", Status::OK()); |
164 | |
|
165 | 0 | DCHECK_EQ(_state, CompactionState::SUCCESS); |
166 | |
|
167 | 0 | tablet()->cumulative_compaction_policy()->update_cumulative_point( |
168 | 0 | tablet(), _input_rowsets, _output_rowset, _last_delete_version); |
169 | 0 | VLOG_CRITICAL << "after cumulative compaction, current cumulative point is " |
170 | 0 | << tablet()->cumulative_layer_point() << ", tablet=" << _tablet->tablet_id(); |
171 | 0 | DorisMetrics::instance()->cumulative_compaction_deltas_total->increment(_input_rowsets.size()); |
172 | 0 | DorisMetrics::instance()->cumulative_compaction_bytes_total->increment( |
173 | 0 | _input_rowsets_total_size); |
174 | |
|
175 | 0 | st = Status::OK(); |
176 | 0 | return st; |
177 | 1 | } |
178 | | |
179 | 21 | Status CumulativeCompaction::pick_rowsets_to_compact() { |
180 | 21 | auto candidate_rowsets = tablet()->pick_candidate_rowsets_to_cumulative_compaction(); |
181 | 21 | if (candidate_rowsets.empty()) { |
182 | 0 | return Status::Error<CUMULATIVE_NO_SUITABLE_VERSION>("candidate_rowsets is empty"); |
183 | 0 | } |
184 | | |
185 | | // candidate_rowsets may not be continuous |
186 | | // So we need to choose the longest continuous path from it. |
187 | 21 | std::vector<Version> missing_versions; |
188 | 21 | find_longest_consecutive_version(&candidate_rowsets, &missing_versions); |
189 | 21 | if (!missing_versions.empty()) { |
190 | 0 | DCHECK(missing_versions.size() % 2 == 0); |
191 | 0 | LOG(WARNING) << "There are missed versions among rowsets. " |
192 | 0 | << "total missed version size: " << missing_versions.size() / 2 |
193 | 0 | << ", first missed version prev rowset verison=" << missing_versions[0] |
194 | 0 | << ", first missed version next rowset version=" << missing_versions[1] |
195 | 0 | << ", tablet=" << _tablet->tablet_id(); |
196 | 0 | if (config::enable_auto_clone_on_compaction_missing_version) { |
197 | 0 | LOG_INFO("cumulative compaction submit missing rowset clone task.") |
198 | 0 | .tag("tablet_id", _tablet->tablet_id()) |
199 | 0 | .tag("version", missing_versions.back().first) |
200 | 0 | .tag("replica_id", tablet()->replica_id()) |
201 | 0 | .tag("partition_id", _tablet->partition_id()) |
202 | 0 | .tag("table_id", _tablet->table_id()); |
203 | 0 | Status st = _engine.submit_clone_task(tablet(), missing_versions.back().first); |
204 | 0 | if (!st) { |
205 | 0 | LOG_WARNING("cumulative compaction failed to submit missing rowset clone task.") |
206 | 0 | .tag("st", st.msg()) |
207 | 0 | .tag("tablet_id", _tablet->tablet_id()) |
208 | 0 | .tag("version", missing_versions.back().first) |
209 | 0 | .tag("replica_id", tablet()->replica_id()) |
210 | 0 | .tag("partition_id", _tablet->partition_id()) |
211 | 0 | .tag("table_id", _tablet->table_id()); |
212 | 0 | } |
213 | 0 | } |
214 | 0 | } |
215 | | |
216 | 21 | int64_t max_score = config::cumulative_compaction_max_deltas; |
217 | 21 | int64_t process_memory_usage = doris::GlobalMemoryArbitrator::process_memory_usage(); |
218 | 21 | bool memory_usage_high = process_memory_usage > MemInfo::soft_mem_limit() * 8 / 10; |
219 | 21 | if (tablet()->last_compaction_status.is<ErrorCode::MEM_LIMIT_EXCEEDED>() || memory_usage_high) { |
220 | 0 | max_score = std::max(config::cumulative_compaction_max_deltas / |
221 | 0 | config::cumulative_compaction_max_deltas_factor, |
222 | 0 | config::cumulative_compaction_min_deltas + 1); |
223 | 0 | } |
224 | | |
225 | 21 | size_t compaction_score = 0; |
226 | 21 | tablet()->cumulative_compaction_policy()->pick_input_rowsets( |
227 | 21 | tablet(), candidate_rowsets, max_score, config::cumulative_compaction_min_deltas, |
228 | 21 | &_input_rowsets, &_last_delete_version, &compaction_score, |
229 | 21 | _allow_delete_in_cumu_compaction); |
230 | | |
231 | | // Cumulative compaction will process with at least 1 rowset. |
232 | | // So when there is no rowset being chosen, we should return Status::Error<CUMULATIVE_NO_SUITABLE_VERSION>(): |
233 | 21 | if (_input_rowsets.empty()) { |
234 | 0 | if (_last_delete_version.first != -1) { |
235 | | // we meet a delete version, should increase the cumulative point to let base compaction handle the delete version. |
236 | | // plus 1 to skip the delete version. |
237 | | // NOTICE: after that, the cumulative point may be larger than max version of this tablet, but it doesn't matter. |
238 | 0 | tablet()->set_cumulative_layer_point(_last_delete_version.first + 1); |
239 | 0 | LOG_INFO( |
240 | 0 | "cumulative compaction meet delete rowset, increase cumu point without " |
241 | 0 | "other " |
242 | 0 | "operation.") |
243 | 0 | .tag("tablet id:", tablet()->tablet_id()) |
244 | 0 | .tag("after cumulative compaction, cumu point:", |
245 | 0 | tablet()->cumulative_layer_point()); |
246 | 0 | return Status::Error<CUMULATIVE_MEET_DELETE_VERSION>( |
247 | 0 | "cumulative compaction meet delete version"); |
248 | 0 | } |
249 | | |
250 | | // we did not meet any delete version. which means compaction_score is not enough to do cumulative compaction. |
251 | | // We should wait until there are more rowsets to come, and keep the cumulative point unchanged. |
252 | | // But in order to avoid the stall of compaction because no new rowset arrives later, we should increase |
253 | | // the cumulative point after waiting for a long time, to ensure that the base compaction can continue. |
254 | | |
255 | | // check both last success time of base and cumulative compaction |
256 | 0 | int64_t now = UnixMillis(); |
257 | 0 | int64_t last_cumu = tablet()->last_cumu_compaction_success_time(); |
258 | 0 | int64_t last_base = tablet()->last_base_compaction_success_time(); |
259 | 0 | if (last_cumu != 0 || last_base != 0) { |
260 | 0 | int64_t interval_threshold = config::pick_rowset_to_compact_interval_sec * 1000; |
261 | 0 | int64_t cumu_interval = now - last_cumu; |
262 | 0 | int64_t base_interval = now - last_base; |
263 | 0 | if (cumu_interval > interval_threshold && base_interval > interval_threshold) { |
264 | | // before increasing cumulative point, we should make sure all rowsets are non-overlapping. |
265 | | // if at least one rowset is overlapping, we should compact them first. |
266 | 0 | for (auto& rs : candidate_rowsets) { |
267 | 0 | if (rs->rowset_meta()->is_segments_overlapping()) { |
268 | 0 | _input_rowsets = candidate_rowsets; |
269 | 0 | return Status::OK(); |
270 | 0 | } |
271 | 0 | } |
272 | | |
273 | | // all candidate rowsets are non-overlapping, increase the cumulative point |
274 | 0 | tablet()->set_cumulative_layer_point(candidate_rowsets.back()->start_version() + 1); |
275 | 0 | } |
276 | 0 | } else { |
277 | | // init the compaction success time for first time |
278 | 0 | if (last_cumu == 0) { |
279 | 0 | tablet()->set_last_cumu_compaction_success_time(now); |
280 | 0 | } |
281 | |
|
282 | 0 | if (last_base == 0) { |
283 | 0 | tablet()->set_last_base_compaction_success_time(now); |
284 | 0 | } |
285 | 0 | } |
286 | | |
287 | 0 | return Status::Error<CUMULATIVE_NO_SUITABLE_VERSION>("_input_rowsets is empty"); |
288 | 0 | } |
289 | | |
290 | 21 | return Status::OK(); |
291 | 21 | } |
292 | | #include "common/compile_check_end.h" |
293 | | |
294 | | } // namespace doris |