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