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