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 "cloud/cloud_cumulative_compaction.h" |
19 | | |
20 | | #include <gen_cpp/cloud.pb.h> |
21 | | |
22 | | #include <algorithm> |
23 | | #include <random> |
24 | | |
25 | | #include "cloud/cloud_meta_mgr.h" |
26 | | #include "cloud/cloud_tablet_mgr.h" |
27 | | #include "cloud/config.h" |
28 | | #include "common/config.h" |
29 | | #include "common/logging.h" |
30 | | #include "common/status.h" |
31 | | #include "cpp/sync_point.h" |
32 | | #include "service/backend_options.h" |
33 | | #include "storage/compaction/compaction.h" |
34 | | #include "storage/compaction/cumulative_compaction_policy.h" |
35 | | #include "util/debug_points.h" |
36 | | #include "util/trace.h" |
37 | | #include "util/uuid_generator.h" |
38 | | |
39 | | namespace doris { |
40 | | using namespace ErrorCode; |
41 | | |
42 | | bvar::Adder<uint64_t> cumu_output_size("cumu_compaction", "output_size"); |
43 | | bvar::LatencyRecorder g_cu_compaction_hold_delete_bitmap_lock_time_ms( |
44 | | "cu_compaction_hold_delete_bitmap_lock_time_ms"); |
45 | | |
46 | | CloudCumulativeCompaction::CloudCumulativeCompaction(CloudStorageEngine& engine, |
47 | | CloudTabletSPtr tablet) |
48 | 123k | : CloudCompactionMixin(engine, tablet, |
49 | 123k | "BaseCompaction:" + std::to_string(tablet->tablet_id())) {} |
50 | | |
51 | 123k | CloudCumulativeCompaction::~CloudCumulativeCompaction() = default; |
52 | | |
53 | 129k | int64_t CloudCumulativeCompaction::_refresh_conflict_versions() { |
54 | 129k | std::vector<std::shared_ptr<CloudCumulativeCompaction>> cumu_compactions; |
55 | 129k | _engine.get_cumu_compaction(_tablet->tablet_id(), cumu_compactions); |
56 | 129k | for (const auto& cumu : cumu_compactions) { |
57 | 6.44k | _min_conflict_version = |
58 | 6.44k | std::min(_min_conflict_version, cumu->_input_rowsets.front()->start_version()); |
59 | 6.44k | _max_conflict_version = |
60 | 6.44k | std::max(_max_conflict_version, cumu->_input_rowsets.back()->end_version()); |
61 | 6.44k | } |
62 | 129k | return _max_conflict_version; |
63 | 129k | } |
64 | | |
65 | 123k | Status CloudCumulativeCompaction::prepare_compact() { |
66 | 123k | DBUG_EXECUTE_IF("CloudCumulativeCompaction.prepare_compact.sleep", { sleep(5); }) |
67 | 123k | Status st; |
68 | 123k | Defer defer_set_st([&] { |
69 | 123k | if (!st.ok()) { |
70 | 116k | cloud_tablet()->set_last_cumu_compaction_status(st.to_string()); |
71 | 116k | } |
72 | 123k | }); |
73 | 123k | if (_tablet->tablet_state() != TABLET_RUNNING && |
74 | 123k | (!config::enable_new_tablet_do_compaction || |
75 | 6 | static_cast<CloudTablet*>(_tablet.get())->alter_version() == -1)) { |
76 | 0 | st = Status::InternalError("invalid tablet state. tablet_id={}", _tablet->tablet_id()); |
77 | 0 | return st; |
78 | 0 | } |
79 | | |
80 | 123k | _refresh_conflict_versions(); |
81 | | |
82 | 123k | bool need_sync_tablet = true; |
83 | 123k | { |
84 | 123k | std::shared_lock rlock(_tablet->get_header_lock()); |
85 | | // If number of rowsets is equal to approximate_num_rowsets, it is very likely that this tablet has been |
86 | | // synchronized with meta-service. |
87 | 123k | if (_tablet->tablet_meta()->all_rs_metas().size() >= |
88 | 123k | cloud_tablet()->fetch_add_approximate_num_rowsets(0) && |
89 | 123k | cloud_tablet()->last_sync_time_s > 0) { |
90 | 122k | need_sync_tablet = false; |
91 | 122k | } |
92 | 123k | } |
93 | 123k | if (need_sync_tablet) { |
94 | 1.07k | st = cloud_tablet()->sync_rowsets(); |
95 | 1.07k | RETURN_IF_ERROR(st); |
96 | 1.07k | } |
97 | | |
98 | | // pick rowsets to compact |
99 | 123k | st = pick_rowsets_to_compact(); |
100 | 123k | if (!st.ok()) { |
101 | 116k | if (_last_delete_version.first != -1) { |
102 | | // we meet a delete version, should increase the cumulative point to let base compaction handle the delete version. |
103 | | // plus 1 to skip the delete version. |
104 | | // NOTICE: after that, the cumulative point may be larger than max version of this tablet, but it doesn't matter. |
105 | 31 | update_cumulative_point(); |
106 | 31 | if (!config::enable_sleep_between_delete_cumu_compaction) { |
107 | 31 | st = Status::Error<CUMULATIVE_MEET_DELETE_VERSION>( |
108 | 31 | "cumulative compaction meet delete version"); |
109 | 31 | } |
110 | 31 | } |
111 | 116k | return st; |
112 | 116k | } |
113 | | |
114 | 57.9k | for (auto& rs : _input_rowsets) { |
115 | 57.9k | _input_row_num += rs->num_rows(); |
116 | 57.9k | _input_segments += rs->num_segments(); |
117 | 57.9k | _input_rowsets_data_size += rs->data_disk_size(); |
118 | 57.9k | _input_rowsets_index_size += rs->index_disk_size(); |
119 | 57.9k | _input_rowsets_total_size += rs->total_disk_size(); |
120 | 57.9k | } |
121 | 6.78k | LOG_INFO("start CloudCumulativeCompaction, tablet_id={}, range=[{}-{}]", _tablet->tablet_id(), |
122 | 6.78k | _input_rowsets.front()->start_version(), _input_rowsets.back()->end_version()) |
123 | 6.78k | .tag("job_id", _uuid) |
124 | 6.78k | .tag("input_rowsets", _input_rowsets.size()) |
125 | 6.78k | .tag("input_rows", _input_row_num) |
126 | 6.78k | .tag("input_segments", _input_segments) |
127 | 6.78k | .tag("input_rowsets_data_size", _input_rowsets_data_size) |
128 | 6.78k | .tag("input_rowsets_index_size", _input_rowsets_index_size) |
129 | 6.78k | .tag("input_rowsets_total_size", _input_rowsets_total_size) |
130 | 6.78k | .tag("tablet_max_version", cloud_tablet()->max_version_unlocked()) |
131 | 6.78k | .tag("cumulative_point", cloud_tablet()->cumulative_layer_point()) |
132 | 6.78k | .tag("num_rowsets", cloud_tablet()->fetch_add_approximate_num_rowsets(0)) |
133 | 6.78k | .tag("cumu_num_rowsets", cloud_tablet()->fetch_add_approximate_cumu_num_rowsets(0)); |
134 | 6.78k | return st; |
135 | 123k | } |
136 | | |
137 | 6.78k | Status CloudCumulativeCompaction::request_global_lock() { |
138 | | // prepare compaction job |
139 | 6.78k | cloud::TabletJobInfoPB job; |
140 | 6.78k | auto idx = job.mutable_idx(); |
141 | 6.78k | idx->set_tablet_id(_tablet->tablet_id()); |
142 | 6.78k | idx->set_table_id(_tablet->table_id()); |
143 | 6.78k | idx->set_index_id(_tablet->index_id()); |
144 | 6.78k | idx->set_partition_id(_tablet->partition_id()); |
145 | 6.78k | auto compaction_job = job.add_compaction(); |
146 | 6.78k | compaction_job->set_id(_uuid); |
147 | 6.78k | compaction_job->set_initiator(BackendOptions::get_localhost() + ':' + |
148 | 6.78k | std::to_string(config::heartbeat_service_port)); |
149 | 6.78k | compaction_job->set_type(cloud::TabletCompactionJobPB::CUMULATIVE); |
150 | 6.78k | compaction_job->set_base_compaction_cnt(_base_compaction_cnt); |
151 | 6.78k | compaction_job->set_cumulative_compaction_cnt(_cumulative_compaction_cnt); |
152 | 6.78k | using namespace std::chrono; |
153 | 6.78k | int64_t now = duration_cast<seconds>(system_clock::now().time_since_epoch()).count(); |
154 | 6.78k | _expiration = now + config::compaction_timeout_seconds; |
155 | 6.78k | compaction_job->set_expiration(_expiration); |
156 | 6.78k | compaction_job->set_lease(now + config::lease_compaction_interval_seconds * 4); |
157 | | |
158 | 6.78k | compaction_job->add_input_versions(_input_rowsets.front()->start_version()); |
159 | 6.78k | compaction_job->add_input_versions(_input_rowsets.back()->end_version()); |
160 | | // Set input version range to let meta-service check version range conflict |
161 | 6.78k | compaction_job->set_check_input_versions_range(config::enable_parallel_cumu_compaction); |
162 | 6.78k | cloud::StartTabletJobResponse resp; |
163 | 6.78k | Status st = _engine.meta_mgr().prepare_tablet_job(job, &resp); |
164 | 6.78k | if (!st.ok()) { |
165 | 346 | if (resp.status().code() == cloud::STALE_TABLET_CACHE) { |
166 | | // set last_sync_time to 0 to force sync tablet next time |
167 | 1 | cloud_tablet()->last_sync_time_s = 0; |
168 | 345 | } else if (resp.status().code() == cloud::TABLET_NOT_FOUND) { |
169 | | // tablet not found |
170 | 341 | cloud_tablet()->clear_cache(); |
171 | 341 | } else if (resp.status().code() == cloud::JOB_TABLET_BUSY) { |
172 | 3 | LOG_WARNING("failed to prepare cumu compaction") |
173 | 3 | .tag("job_id", _uuid) |
174 | 3 | .tag("msg", resp.status().msg()); |
175 | 3 | return Status::Error<CUMULATIVE_NO_SUITABLE_VERSION>( |
176 | 3 | "cumu no suitable versions: job tablet busy"); |
177 | 3 | } else if (resp.status().code() == cloud::JOB_CHECK_ALTER_VERSION) { |
178 | 2 | (static_cast<CloudTablet*>(_tablet.get()))->set_alter_version(resp.alter_version()); |
179 | 2 | std::stringstream ss; |
180 | 2 | ss << "failed to prepare cumu compaction. Check compaction input versions " |
181 | 2 | "failed in schema change. " |
182 | 2 | "input_version_start=" |
183 | 2 | << compaction_job->input_versions(0) |
184 | 2 | << " input_version_end=" << compaction_job->input_versions(1) |
185 | 2 | << " schema_change_alter_version=" << resp.alter_version(); |
186 | 2 | std::string msg = ss.str(); |
187 | 2 | LOG(WARNING) << msg; |
188 | 2 | return Status::InternalError(msg); |
189 | 2 | } |
190 | 346 | } |
191 | 6.78k | return st; |
192 | 6.78k | } |
193 | | |
194 | 6.43k | Status CloudCumulativeCompaction::execute_compact() { |
195 | 6.43k | TEST_SYNC_POINT_RETURN_WITH_VALUE("CloudCumulativeCompaction::execute_compact_impl", |
196 | 6.43k | Status::OK(), this); |
197 | | |
198 | 6.43k | SCOPED_ATTACH_TASK(_mem_tracker); |
199 | | |
200 | 6.43k | using namespace std::chrono; |
201 | 6.43k | auto start = steady_clock::now(); |
202 | 6.43k | Status st; |
203 | 6.43k | Defer defer_set_st([&] { |
204 | 6.43k | cloud_tablet()->set_last_cumu_compaction_status(st.to_string()); |
205 | 6.43k | if (!st.ok()) { |
206 | 35 | cloud_tablet()->set_last_cumu_compaction_failure_time(UnixMillis()); |
207 | 6.40k | } else { |
208 | 6.40k | cloud_tablet()->set_last_cumu_compaction_success_time(UnixMillis()); |
209 | 6.40k | } |
210 | 6.43k | }); |
211 | 6.43k | st = CloudCompactionMixin::execute_compact(); |
212 | 6.43k | if (!st.ok()) { |
213 | 35 | LOG(WARNING) << "fail to do " << compaction_name() << ". res=" << st |
214 | 35 | << ", tablet=" << _tablet->tablet_id() |
215 | 35 | << ", output_version=" << _output_version; |
216 | 35 | return st; |
217 | 35 | } |
218 | 6.40k | LOG_INFO("finish CloudCumulativeCompaction, tablet_id={}, cost={}ms, range=[{}-{}]", |
219 | 6.40k | _tablet->tablet_id(), duration_cast<milliseconds>(steady_clock::now() - start).count(), |
220 | 6.40k | _input_rowsets.front()->start_version(), _input_rowsets.back()->end_version()) |
221 | 6.40k | .tag("job_id", _uuid) |
222 | 6.40k | .tag("input_rowsets", _input_rowsets.size()) |
223 | 6.40k | .tag("input_rows", _input_row_num) |
224 | 6.40k | .tag("input_segments", _input_segments) |
225 | 6.40k | .tag("input_rowsets_data_size", _input_rowsets_data_size) |
226 | 6.40k | .tag("input_rowsets_index_size", _input_rowsets_index_size) |
227 | 6.40k | .tag("input_rowsets_total_size", _input_rowsets_total_size) |
228 | 6.40k | .tag("output_rows", _output_rowset->num_rows()) |
229 | 6.40k | .tag("output_segments", _output_rowset->num_segments()) |
230 | 6.40k | .tag("output_rowset_data_size", _output_rowset->data_disk_size()) |
231 | 6.40k | .tag("output_rowset_index_size", _output_rowset->index_disk_size()) |
232 | 6.40k | .tag("output_rowset_total_size", _output_rowset->total_disk_size()) |
233 | 6.40k | .tag("tablet_max_version", _tablet->max_version_unlocked()) |
234 | 6.40k | .tag("cumulative_point", cloud_tablet()->cumulative_layer_point()) |
235 | 6.40k | .tag("num_rowsets", cloud_tablet()->fetch_add_approximate_num_rowsets(0)) |
236 | 6.40k | .tag("cumu_num_rowsets", cloud_tablet()->fetch_add_approximate_cumu_num_rowsets(0)) |
237 | 6.40k | .tag("local_read_time_us", _stats.cloud_local_read_time) |
238 | 6.40k | .tag("remote_read_time_us", _stats.cloud_remote_read_time) |
239 | 6.40k | .tag("local_read_bytes", _local_read_bytes_total) |
240 | 6.40k | .tag("remote_read_bytes", _remote_read_bytes_total); |
241 | | |
242 | 6.40k | _state = CompactionState::SUCCESS; |
243 | | |
244 | 6.40k | DorisMetrics::instance()->cumulative_compaction_deltas_total->increment(_input_rowsets.size()); |
245 | 6.40k | DorisMetrics::instance()->cumulative_compaction_bytes_total->increment( |
246 | 6.40k | _input_rowsets_total_size); |
247 | 6.40k | cumu_output_size << _output_rowset->total_disk_size(); |
248 | | |
249 | 6.40k | st = Status::OK(); |
250 | 6.40k | return st; |
251 | 6.43k | } |
252 | | |
253 | 6.42k | Status CloudCumulativeCompaction::modify_rowsets() { |
254 | | // calculate new cumulative point |
255 | 6.42k | int64_t prepare_max_conflict_version = _max_conflict_version; |
256 | 6.42k | int64_t max_conflict_version = _refresh_conflict_versions(); |
257 | 6.42k | int64_t input_cumulative_point; |
258 | 6.42k | bool output_already_passed; |
259 | 6.42k | std::vector<RowsetSharedPtr> rowsets; |
260 | 6.42k | { |
261 | 6.42k | std::shared_lock rlock(_tablet->get_header_lock()); |
262 | 6.42k | input_cumulative_point = cloud_tablet()->cumulative_layer_point(); |
263 | 6.42k | DORIS_CHECK(input_cumulative_point <= _output_rowset->start_version() || |
264 | 6.42k | input_cumulative_point > _output_rowset->end_version()); |
265 | 6.42k | output_already_passed = input_cumulative_point > _output_rowset->end_version(); |
266 | 6.43k | if (!output_already_passed && (input_cumulative_point < _output_rowset->start_version() || |
267 | 6.43k | max_conflict_version > _output_rowset->end_version())) { |
268 | 1.73k | cloud_tablet()->traverse_rowsets_unlocked( |
269 | 1.73k | [&rowsets, input_cumulative_point, |
270 | 1.73k | output_start_version = _output_rowset->start_version(), |
271 | 1.73k | output_end_version = _output_rowset->end_version(), |
272 | 24.2k | max_conflict_version](const RowsetSharedPtr& rowset) { |
273 | 24.2k | if (rowset->start_version() < input_cumulative_point) { |
274 | 3.87k | return; |
275 | 3.87k | } |
276 | 20.3k | bool is_preceding = rowset->end_version() < output_start_version; |
277 | 20.3k | bool is_following = rowset->start_version() > output_end_version && |
278 | 20.3k | rowset->end_version() <= max_conflict_version; |
279 | 20.3k | if (is_preceding || is_following) { |
280 | 4.60k | rowsets.push_back(rowset); |
281 | 4.60k | } |
282 | 20.3k | }); |
283 | 1.73k | } |
284 | 6.42k | } |
285 | 6.42k | int64_t new_cumulative_point = input_cumulative_point; |
286 | 6.43k | if (!output_already_passed) { |
287 | 6.43k | rowsets.push_back(_output_rowset); |
288 | 6.43k | std::sort(rowsets.begin(), rowsets.end(), Rowset::comparator); |
289 | 6.43k | auto compaction_policy = cloud_tablet()->tablet_meta()->compaction_policy(); |
290 | 6.43k | new_cumulative_point = |
291 | 6.43k | _engine.cumu_compaction_policy(compaction_policy) |
292 | 6.43k | ->calculate_cumulative_point(cloud_tablet(), rowsets, _output_rowset, |
293 | 6.43k | _last_delete_version, input_cumulative_point); |
294 | 6.43k | } |
295 | 6.42k | LOG_INFO("calculate cumulative point for CloudCumulativeCompaction") |
296 | 6.42k | .tag("job_id", _uuid) |
297 | 6.42k | .tag("tablet_id", _tablet->tablet_id()) |
298 | 6.42k | .tag("input_cumulative_point", input_cumulative_point) |
299 | 6.42k | .tag("new_cumulative_point", new_cumulative_point) |
300 | 6.42k | .tag("output_rowset_start_version", _output_rowset->start_version()) |
301 | 6.42k | .tag("output_rowset_end_version", _output_rowset->end_version()) |
302 | 6.42k | .tag("rowsets", rowsets.size()) |
303 | 6.42k | .tag("prepare_max_conflict_version", prepare_max_conflict_version) |
304 | 6.42k | .tag("max_conflict_version", max_conflict_version); |
305 | | // commit compaction job |
306 | 6.42k | cloud::TabletJobInfoPB job; |
307 | 6.42k | auto idx = job.mutable_idx(); |
308 | 6.42k | idx->set_tablet_id(_tablet->tablet_id()); |
309 | 6.42k | idx->set_table_id(_tablet->table_id()); |
310 | 6.42k | idx->set_index_id(_tablet->index_id()); |
311 | 6.42k | idx->set_partition_id(_tablet->partition_id()); |
312 | 6.42k | auto compaction_job = job.add_compaction(); |
313 | 6.42k | compaction_job->set_id(_uuid); |
314 | 6.42k | compaction_job->set_initiator(BackendOptions::get_localhost() + ':' + |
315 | 6.42k | std::to_string(config::heartbeat_service_port)); |
316 | 6.42k | compaction_job->set_type(cloud::TabletCompactionJobPB::CUMULATIVE); |
317 | 6.42k | compaction_job->set_input_cumulative_point(input_cumulative_point); |
318 | 6.42k | compaction_job->set_output_cumulative_point(new_cumulative_point); |
319 | 6.42k | compaction_job->set_num_input_rows(_input_row_num); |
320 | 6.42k | compaction_job->set_num_output_rows(_output_rowset->num_rows()); |
321 | 6.42k | compaction_job->set_size_input_rowsets(_input_rowsets_total_size); |
322 | 6.42k | compaction_job->set_size_output_rowsets(_output_rowset->total_disk_size()); |
323 | 6.42k | compaction_job->set_num_input_segments(_input_segments); |
324 | 6.42k | compaction_job->set_num_output_segments(_output_rowset->num_segments()); |
325 | 6.42k | compaction_job->set_num_input_rowsets(num_input_rowsets()); |
326 | 6.42k | compaction_job->set_num_output_rowsets(1); |
327 | 6.42k | compaction_job->add_input_versions(_input_rowsets.front()->start_version()); |
328 | 6.42k | compaction_job->add_input_versions(_input_rowsets.back()->end_version()); |
329 | 6.42k | compaction_job->add_output_versions(_output_rowset->end_version()); |
330 | 6.42k | compaction_job->add_txn_id(_output_rowset->txn_id()); |
331 | 6.42k | compaction_job->add_output_rowset_ids(_output_rowset->rowset_id().to_string()); |
332 | 6.42k | compaction_job->set_index_size_input_rowsets(_input_rowsets_index_size); |
333 | 6.42k | compaction_job->set_segment_size_input_rowsets(_input_rowsets_data_size); |
334 | 6.42k | compaction_job->set_index_size_output_rowsets(_output_rowset->index_disk_size()); |
335 | 6.42k | compaction_job->set_segment_size_output_rowsets(_output_rowset->data_disk_size()); |
336 | | |
337 | 6.42k | DBUG_EXECUTE_IF("CloudCumulativeCompaction::modify_rowsets.enable_spin_wait", { |
338 | 6.42k | LOG(INFO) << "CloudCumulativeCompaction::modify_rowsets.enable_spin_wait, start"; |
339 | 6.42k | while (DebugPoints::instance()->is_enable( |
340 | 6.42k | "CloudCumulativeCompaction::modify_rowsets.block")) { |
341 | 6.42k | std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
342 | 6.42k | } |
343 | 6.42k | LOG(INFO) << "CloudCumulativeCompaction::modify_rowsets.enable_spin_wait, exit"; |
344 | 6.42k | }); |
345 | | |
346 | | // Block only NOTREADY tablets (SC new tablets) before compaction commit. |
347 | | // RUNNING tablets (system tables, base tablets) are not affected. |
348 | 6.42k | DBUG_EXECUTE_IF("CloudCumulativeCompaction::modify_rowsets.block_notready", { |
349 | 6.42k | if (_tablet->tablet_state() == TABLET_NOTREADY) { |
350 | 6.42k | LOG(INFO) << "block NOTREADY tablet compaction before commit" |
351 | 6.42k | << ", tablet_id=" << _tablet->tablet_id() << ", output=[" |
352 | 6.42k | << _input_rowsets.front()->start_version() << "-" |
353 | 6.42k | << _input_rowsets.back()->end_version() << "]"; |
354 | 6.42k | while (DebugPoints::instance()->is_enable( |
355 | 6.42k | "CloudCumulativeCompaction::modify_rowsets.block_notready")) { |
356 | 6.42k | std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
357 | 6.42k | } |
358 | 6.42k | LOG(INFO) << "release NOTREADY tablet compaction, tablet_id=" << _tablet->tablet_id(); |
359 | 6.42k | } |
360 | 6.42k | }); |
361 | | |
362 | 6.42k | DBUG_EXECUTE_IF("CloudCumulativeCompaction::modify_rowsets.random_sleep", { |
363 | 6.42k | auto probability = dp->param("probability", dp->param("percent", 0.0)); |
364 | 6.42k | DORIS_CHECK(probability >= 0.0 && probability <= 1.0); |
365 | 6.42k | static thread_local std::mt19937 gen(std::random_device {}()); |
366 | 6.42k | std::bernoulli_distribution inject_sleep {probability}; |
367 | 6.42k | if (inject_sleep(gen)) { |
368 | 6.42k | auto max_sleep_ms = dp->param<int64_t>( |
369 | 6.42k | "max_sleep_ms", dp->param<int64_t>("max_sleep_time_ms", |
370 | 6.42k | dp->param<int64_t>("max_sleep_time", 0))); |
371 | 6.42k | DORIS_CHECK(max_sleep_ms >= 0); |
372 | 6.42k | std::uniform_int_distribution<int64_t> sleep_dist(0, max_sleep_ms); |
373 | 6.42k | auto sleep_ms = sleep_dist(gen); |
374 | 6.42k | LOG(INFO) << "CloudCumulativeCompaction::modify_rowsets.random_sleep" |
375 | 6.42k | << ", tablet_id=" << _tablet->tablet_id() << ", sleep_ms=" << sleep_ms |
376 | 6.42k | << ", probability=" << probability; |
377 | 6.42k | std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms)); |
378 | 6.42k | } |
379 | 6.42k | }); |
380 | | |
381 | 6.42k | DBUG_EXECUTE_IF("CloudCumulativeCompaction::modify_rowsets.random_fail", { |
382 | 6.42k | auto probability = dp->param("probability", dp->param("percent", 0.0)); |
383 | 6.42k | DORIS_CHECK(probability >= 0.0 && probability <= 1.0); |
384 | 6.42k | static thread_local std::mt19937 gen(std::random_device {}()); |
385 | 6.42k | std::bernoulli_distribution inject_fail {probability}; |
386 | 6.42k | if (inject_fail(gen)) { |
387 | 6.42k | LOG(WARNING) << "CloudCumulativeCompaction::modify_rowsets.random_fail" |
388 | 6.42k | << ", tablet_id=" << _tablet->tablet_id() |
389 | 6.42k | << ", probability=" << probability; |
390 | 6.42k | return Status::InternalError( |
391 | 6.42k | "debug cloud cumulative compaction modify rowsets random failed"); |
392 | 6.42k | } |
393 | 6.42k | }); |
394 | | |
395 | 6.42k | DeleteBitmapPtr output_rowset_delete_bitmap = nullptr; |
396 | 6.42k | int64_t initiator = this->initiator(); |
397 | 6.42k | int64_t get_delete_bitmap_lock_start_time = 0; |
398 | 6.42k | if (_tablet->keys_type() == KeysType::UNIQUE_KEYS && |
399 | 6.42k | _tablet->enable_unique_key_merge_on_write()) { |
400 | 3.35k | RETURN_IF_ERROR(cloud_tablet()->calc_delete_bitmap_for_compaction( |
401 | 3.35k | _input_rowsets, _output_rowset, *_rowid_conversion, compaction_type(), |
402 | 3.35k | _stats.merged_rows, _stats.filtered_rows, initiator, output_rowset_delete_bitmap, |
403 | 3.35k | _allow_delete_in_cumu_compaction, get_delete_bitmap_lock_start_time)); |
404 | 3.34k | LOG_INFO("update delete bitmap in CloudCumulativeCompaction, tablet_id={}, range=[{}-{}]", |
405 | 3.34k | _tablet->tablet_id(), _input_rowsets.front()->start_version(), |
406 | 3.34k | _input_rowsets.back()->end_version()) |
407 | 3.34k | .tag("job_id", _uuid) |
408 | 3.34k | .tag("initiator", initiator) |
409 | 3.34k | .tag("input_rowsets", _input_rowsets.size()) |
410 | 3.34k | .tag("input_rows", _input_row_num) |
411 | 3.34k | .tag("input_segments", _input_segments) |
412 | 3.34k | .tag("number_output_delete_bitmap", |
413 | 3.34k | output_rowset_delete_bitmap->delete_bitmap.size()); |
414 | 3.34k | compaction_job->set_delete_bitmap_lock_initiator(initiator); |
415 | 3.34k | } |
416 | | |
417 | 6.42k | DBUG_EXECUTE_IF("CumulativeCompaction.modify_rowsets.trigger_abort_job_failed", { |
418 | 6.42k | LOG(INFO) << "CumulativeCompaction.modify_rowsets.trigger_abort_job_failed for tablet_id" |
419 | 6.42k | << cloud_tablet()->tablet_id(); |
420 | 6.42k | return Status::InternalError( |
421 | 6.42k | "CumulativeCompaction.modify_rowsets.trigger_abort_job_failed for tablet_id {}", |
422 | 6.42k | cloud_tablet()->tablet_id()); |
423 | 6.42k | }); |
424 | 6.42k | cloud::FinishTabletJobResponse resp; |
425 | 6.42k | auto st = _engine.meta_mgr().commit_tablet_job(job, &resp); |
426 | 6.42k | if (_tablet->keys_type() == KeysType::UNIQUE_KEYS && |
427 | 6.42k | _tablet->enable_unique_key_merge_on_write()) { |
428 | 3.35k | int64_t hold_delete_bitmap_lock_time_ms = |
429 | 3.35k | (MonotonicMicros() - get_delete_bitmap_lock_start_time) / 1000; |
430 | 3.35k | g_cu_compaction_hold_delete_bitmap_lock_time_ms << hold_delete_bitmap_lock_time_ms; |
431 | 3.35k | } |
432 | 6.42k | if (resp.has_alter_version()) { |
433 | 6.39k | (static_cast<CloudTablet*>(_tablet.get()))->set_alter_version(resp.alter_version()); |
434 | 6.39k | } |
435 | 6.42k | if (!st.ok()) { |
436 | 32 | if (resp.status().code() == cloud::TABLET_NOT_FOUND) { |
437 | 7 | cloud_tablet()->clear_cache(); |
438 | 25 | } else if (resp.status().code() == cloud::JOB_CHECK_ALTER_VERSION) { |
439 | 6 | std::stringstream ss; |
440 | 6 | ss << "failed to prepare cumu compaction. Check compaction input versions " |
441 | 6 | "failed in schema change. " |
442 | 6 | "input_version_start=" |
443 | 6 | << compaction_job->input_versions(0) |
444 | 6 | << " input_version_end=" << compaction_job->input_versions(1) |
445 | 6 | << " schema_change_alter_version=" << resp.alter_version(); |
446 | 6 | std::string msg = ss.str(); |
447 | 6 | LOG(WARNING) << msg; |
448 | 6 | return Status::InternalError(msg); |
449 | 6 | } |
450 | 26 | return st; |
451 | 32 | } |
452 | | |
453 | 6.39k | auto& stats = resp.stats(); |
454 | 6.39k | LOG(INFO) << "tablet stats=" << stats.ShortDebugString(); |
455 | 6.39k | { |
456 | 6.39k | std::unique_lock wrlock(_tablet->get_header_lock()); |
457 | | // clang-format off |
458 | 6.39k | cloud_tablet()->set_last_base_compaction_success_time(std::max(cloud_tablet()->last_base_compaction_success_time(), stats.last_base_compaction_time_ms())); |
459 | 6.39k | cloud_tablet()->set_last_cumu_compaction_success_time(std::max(cloud_tablet()->last_cumu_compaction_success_time(), stats.last_cumu_compaction_time_ms())); |
460 | 6.39k | cloud_tablet()->set_last_full_compaction_success_time(std::max(cloud_tablet()->last_full_compaction_success_time(), stats.last_full_compaction_time_ms())); |
461 | | // clang-format on |
462 | 6.39k | if (cloud_tablet()->cumulative_compaction_cnt() >= stats.cumulative_compaction_cnt()) { |
463 | | // This could happen while calling `sync_tablet_rowsets` during `commit_tablet_job`, or parallel cumu compactions which are |
464 | | // committed later increase tablet.cumulative_compaction_cnt (see CloudCompactionTest.parallel_cumu_compaction) |
465 | 0 | return Status::OK(); |
466 | 0 | } |
467 | | // Try to make output rowset visible immediately in tablet cache, instead of waiting for next synchronization from meta-service. |
468 | 6.39k | if (stats.cumulative_point() > cloud_tablet()->cumulative_layer_point() && |
469 | 6.39k | stats.cumulative_compaction_cnt() != cloud_tablet()->cumulative_compaction_cnt() + 1) { |
470 | | // This could happen when there are multiple parallel cumu compaction committed, tablet cache lags several |
471 | | // cumu compactions behind meta-service (stats.cumulative_compaction_cnt > tablet.cumulative_compaction_cnt + 1). |
472 | | // If `cumu_point` of the tablet cache also falls behind, MUST ONLY synchronize tablet cache from meta-service, |
473 | | // otherwise may cause the tablet to be unable to synchronize the rowset meta changes generated by other cumu compaction. |
474 | 0 | return Status::OK(); |
475 | 0 | } |
476 | 6.39k | if (_input_rowsets.size() == 1) { |
477 | 0 | DCHECK_EQ(_output_rowset->version(), _input_rowsets[0]->version()); |
478 | | // MUST NOT move input rowset to stale path |
479 | 0 | cloud_tablet()->add_rowsets({_output_rowset}, true, wrlock, true); |
480 | 6.39k | } else { |
481 | 6.39k | cloud_tablet()->delete_rowsets(_input_rowsets, wrlock); |
482 | 6.39k | cloud_tablet()->add_rowsets({_output_rowset}, false, wrlock); |
483 | 6.39k | } |
484 | | // ATTN: MUST NOT update `base_compaction_cnt` which are used when sync rowsets, otherwise may cause |
485 | | // the tablet to be unable to synchronize the rowset meta changes generated by base compaction. |
486 | 6.39k | cloud_tablet()->set_cumulative_compaction_cnt(stats.cumulative_compaction_cnt()); |
487 | 6.39k | cloud_tablet()->set_cumulative_layer_point(stats.cumulative_point()); |
488 | 6.39k | if (output_rowset_delete_bitmap) { |
489 | 3.32k | _tablet->tablet_meta()->delete_bitmap().merge(*output_rowset_delete_bitmap); |
490 | 3.32k | } |
491 | 6.39k | if (stats.base_compaction_cnt() >= cloud_tablet()->base_compaction_cnt()) { |
492 | 6.37k | cloud_tablet()->reset_approximate_stats(stats.num_rowsets(), stats.num_segments(), |
493 | 6.37k | stats.num_rows(), stats.data_size()); |
494 | 6.37k | } |
495 | 6.39k | } |
496 | | // agg delete bitmap for pre rowsets |
497 | 6.39k | if (config::enable_agg_and_remove_pre_rowsets_delete_bitmap && |
498 | 6.39k | _tablet->keys_type() == KeysType::UNIQUE_KEYS && |
499 | 6.39k | _tablet->enable_unique_key_merge_on_write() && _input_rowsets.size() != 1) { |
500 | 3.32k | OlapStopWatch watch; |
501 | 3.32k | std::vector<RowsetSharedPtr> pre_rowsets {}; |
502 | 3.32k | { |
503 | 3.32k | std::shared_lock rlock(_tablet->get_header_lock()); |
504 | 19.5k | for (const auto& it2 : cloud_tablet()->rowset_map()) { |
505 | 19.5k | if (it2.first.second < _output_rowset->start_version()) { |
506 | 8.84k | pre_rowsets.emplace_back(it2.second); |
507 | 8.84k | } |
508 | 19.5k | } |
509 | 3.32k | } |
510 | 3.32k | std::sort(pre_rowsets.begin(), pre_rowsets.end(), Rowset::comparator); |
511 | 3.32k | auto pre_rowsets_delete_bitmap = std::make_shared<DeleteBitmap>(_tablet->tablet_id()); |
512 | 3.32k | std::map<std::string, int64_t> pre_rowset_to_versions; |
513 | 3.32k | cloud_tablet()->agg_delete_bitmap_for_compaction( |
514 | 3.32k | _output_rowset->start_version(), _output_rowset->end_version(), pre_rowsets, |
515 | 3.32k | pre_rowsets_delete_bitmap, pre_rowset_to_versions); |
516 | | // update delete bitmap to ms |
517 | 3.32k | DBUG_EXECUTE_IF( |
518 | 3.32k | "CumulativeCompaction.modify_rowsets.cloud_update_delete_bitmap_without_lock.block", |
519 | 3.32k | DBUG_BLOCK); |
520 | 3.32k | auto status = _engine.meta_mgr().cloud_update_delete_bitmap_without_lock( |
521 | 3.32k | *cloud_tablet(), pre_rowsets_delete_bitmap.get(), pre_rowset_to_versions, |
522 | 3.32k | cloud_tablet()->table_id(), _output_rowset->start_version(), |
523 | 3.32k | _output_rowset->end_version()); |
524 | 3.32k | if (!status.ok()) { |
525 | 0 | LOG(WARNING) << "failed to agg pre rowsets delete bitmap to ms. tablet_id=" |
526 | 0 | << _tablet->tablet_id() << ", pre rowset num=" << pre_rowsets.size() |
527 | 0 | << ", output version=" << _output_rowset->version().to_string() |
528 | 0 | << ", status=" << status.to_string(); |
529 | 3.32k | } else { |
530 | 3.32k | LOG(INFO) << "agg pre rowsets delete bitmap to ms. tablet_id=" << _tablet->tablet_id() |
531 | 3.32k | << ", pre rowset num=" << pre_rowsets.size() |
532 | 3.32k | << ", output version=" << _output_rowset->version().to_string() |
533 | 3.32k | << ", cost(us)=" << watch.get_elapse_time_us(); |
534 | 3.32k | } |
535 | 3.32k | } |
536 | 6.39k | DBUG_EXECUTE_IF("CumulativeCompaction.modify_rowsets.delete_expired_stale_rowset", { |
537 | 6.39k | LOG(INFO) << "delete_expired_stale_rowsets for tablet=" << _tablet->tablet_id(); |
538 | 6.39k | _engine.tablet_mgr().vacuum_stale_rowsets(CountDownLatch(1)); |
539 | 6.39k | }); |
540 | | |
541 | 6.39k | _tablet->prefill_dbm_agg_cache_after_compaction(_output_rowset); |
542 | 6.39k | return Status::OK(); |
543 | 6.39k | } |
544 | | |
545 | 35 | Status CloudCumulativeCompaction::garbage_collection() { |
546 | 35 | RETURN_IF_ERROR(CloudCompactionMixin::garbage_collection()); |
547 | 35 | cloud::TabletJobInfoPB job; |
548 | 35 | auto idx = job.mutable_idx(); |
549 | 35 | idx->set_tablet_id(_tablet->tablet_id()); |
550 | 35 | idx->set_table_id(_tablet->table_id()); |
551 | 35 | idx->set_index_id(_tablet->index_id()); |
552 | 35 | idx->set_partition_id(_tablet->partition_id()); |
553 | 35 | auto compaction_job = job.add_compaction(); |
554 | 35 | compaction_job->set_id(_uuid); |
555 | 35 | compaction_job->set_initiator(BackendOptions::get_localhost() + ':' + |
556 | 35 | std::to_string(config::heartbeat_service_port)); |
557 | 35 | compaction_job->set_type(cloud::TabletCompactionJobPB::CUMULATIVE); |
558 | 35 | if (_tablet->keys_type() == KeysType::UNIQUE_KEYS && |
559 | 35 | _tablet->enable_unique_key_merge_on_write()) { |
560 | 27 | compaction_job->set_delete_bitmap_lock_initiator(this->initiator()); |
561 | 27 | } |
562 | 35 | DBUG_EXECUTE_IF("CumulativeCompaction.modify_rowsets.trigger_abort_job_failed", { |
563 | 35 | LOG(INFO) << "CumulativeCompaction.modify_rowsets.abort_job_failed for tablet_id" |
564 | 35 | << cloud_tablet()->tablet_id(); |
565 | 35 | return Status::InternalError( |
566 | 35 | "CumulativeCompaction.modify_rowsets.abort_job_failed for tablet_id {}", |
567 | 35 | cloud_tablet()->tablet_id()); |
568 | 35 | }); |
569 | 35 | auto st = _engine.meta_mgr().abort_tablet_job(job); |
570 | 35 | if (!st.ok()) { |
571 | 13 | LOG_WARNING("failed to abort compaction job") |
572 | 13 | .tag("job_id", _uuid) |
573 | 13 | .tag("tablet_id", _tablet->tablet_id()) |
574 | 13 | .error(st); |
575 | 13 | } |
576 | 35 | return st; |
577 | 35 | } |
578 | | |
579 | 123k | Status CloudCumulativeCompaction::pick_rowsets_to_compact() { |
580 | 123k | _input_rowsets.clear(); |
581 | | |
582 | 123k | int64_t min_conflict_version = _min_conflict_version; |
583 | 123k | int64_t max_conflict_version = _max_conflict_version; |
584 | 123k | int64_t max_score = config::cumulative_compaction_max_deltas; |
585 | 123k | double process_memory_usage = |
586 | 123k | cast_set<double>(doris::GlobalMemoryArbitrator::process_memory_usage()); |
587 | 123k | bool memory_usage_high = |
588 | 123k | process_memory_usage > cast_set<double>(MemInfo::soft_mem_limit()) * 0.8; |
589 | 123k | if (cloud_tablet()->last_compaction_status.is<ErrorCode::MEM_LIMIT_EXCEEDED>() || |
590 | 123k | memory_usage_high) { |
591 | 0 | max_score = std::max(config::cumulative_compaction_max_deltas / |
592 | 0 | config::cumulative_compaction_max_deltas_factor, |
593 | 0 | config::cumulative_compaction_min_deltas + 1); |
594 | 0 | } |
595 | | |
596 | 123k | auto compaction_policy = cloud_tablet()->tablet_meta()->compaction_policy(); |
597 | 123k | auto pick_from_candidates = [&](std::vector<RowsetSharedPtr>& candidates) { |
598 | 123k | _input_rowsets.clear(); |
599 | 123k | _last_delete_version = Version {-1, -1}; |
600 | | |
601 | 123k | if (candidates.empty()) { |
602 | 698 | return Status::Error<CUMULATIVE_NO_SUITABLE_VERSION>( |
603 | 698 | "no suitable versions: candidate rowsets empty"); |
604 | 698 | } |
605 | 122k | std::sort(candidates.begin(), candidates.end(), Rowset::comparator); |
606 | 122k | if (auto st = check_version_continuity(candidates); !st.ok()) { |
607 | 0 | DCHECK(false) << st; |
608 | 0 | return st; |
609 | 0 | } |
610 | | |
611 | 122k | size_t compaction_score = 0; |
612 | 122k | _engine.cumu_compaction_policy(compaction_policy) |
613 | 122k | ->pick_input_rowsets(cloud_tablet(), candidates, max_score, |
614 | 122k | config::cumulative_compaction_min_deltas, &_input_rowsets, |
615 | 122k | &_last_delete_version, &compaction_score); |
616 | | |
617 | 122k | if (_input_rowsets.empty()) { |
618 | 112k | return Status::Error<CUMULATIVE_NO_SUITABLE_VERSION>( |
619 | 112k | "no suitable versions: input rowsets empty"); |
620 | 112k | } else if (_input_rowsets.size() == 1 && |
621 | 10.0k | !_input_rowsets.front()->rowset_meta()->is_segments_overlapping()) { |
622 | 3.29k | VLOG_DEBUG << "there is only one rowset and not overlapping. tablet_id=" |
623 | 0 | << _tablet->tablet_id() << ", version=" << _input_rowsets.front()->version(); |
624 | 3.29k | _input_rowsets.clear(); |
625 | 3.29k | return Status::Error<CUMULATIVE_NO_SUITABLE_VERSION>( |
626 | 3.29k | "no suitable versions: only one rowset and not overlapping"); |
627 | 3.29k | } |
628 | 6.78k | return Status::OK(); |
629 | 122k | }; |
630 | | |
631 | 123k | auto pick_from_version_range = [&](int64_t start_version, int64_t end_version) { |
632 | 123k | std::vector<RowsetSharedPtr> candidates; |
633 | 123k | { |
634 | 123k | std::shared_lock rlock(_tablet->get_header_lock()); |
635 | 123k | _base_compaction_cnt = cloud_tablet()->base_compaction_cnt(); |
636 | 123k | _cumulative_compaction_cnt = cloud_tablet()->cumulative_compaction_cnt(); |
637 | 123k | int64_t range_start_version = |
638 | 123k | std::max(std::max(cloud_tablet()->cumulative_layer_point(), start_version), |
639 | 123k | cloud_tablet()->alter_version() + 1); |
640 | | // Get candidate rowsets from the selected non-conflicting version range. |
641 | 123k | cloud_tablet()->traverse_rowsets_unlocked( |
642 | 594k | [&candidates, range_start_version, end_version](const RowsetSharedPtr& rs) { |
643 | 594k | if (rs->start_version() >= range_start_version && |
644 | 594k | rs->start_version() < end_version) { |
645 | 458k | candidates.push_back(rs); |
646 | 458k | } |
647 | 594k | }); |
648 | 123k | } |
649 | 123k | return pick_from_candidates(candidates); |
650 | 123k | }; |
651 | | |
652 | | // pick_from_version_range clamps the lower bound by cumulative point and alter version. |
653 | 123k | auto st = pick_from_version_range(0, min_conflict_version); |
654 | 123k | if (!st.ok()) { |
655 | 116k | if (!st.is<CUMULATIVE_NO_SUITABLE_VERSION>() || |
656 | 116k | min_conflict_version == std::numeric_limits<int64_t>::max()) { |
657 | 116k | return st; |
658 | 116k | } |
659 | 2 | st = pick_from_version_range(max_conflict_version + 1, std::numeric_limits<int64_t>::max()); |
660 | 2 | RETURN_IF_ERROR(st); |
661 | 2 | } |
662 | | |
663 | 6.78k | apply_txn_size_truncation_and_log("CloudCumulativeCompaction"); |
664 | 6.78k | return Status::OK(); |
665 | 123k | } |
666 | | |
667 | 31 | void CloudCumulativeCompaction::update_cumulative_point() { |
668 | 31 | cloud::TabletJobInfoPB job; |
669 | 31 | auto idx = job.mutable_idx(); |
670 | 31 | idx->set_tablet_id(_tablet->tablet_id()); |
671 | 31 | idx->set_table_id(_tablet->table_id()); |
672 | 31 | idx->set_index_id(_tablet->index_id()); |
673 | 31 | idx->set_partition_id(_tablet->partition_id()); |
674 | 31 | auto compaction_job = job.add_compaction(); |
675 | 31 | compaction_job->set_id(_uuid); |
676 | 31 | compaction_job->set_initiator(BackendOptions::get_localhost() + ':' + |
677 | 31 | std::to_string(config::heartbeat_service_port)); |
678 | 31 | compaction_job->set_type(cloud::TabletCompactionJobPB::EMPTY_CUMULATIVE); |
679 | 31 | compaction_job->set_base_compaction_cnt(_base_compaction_cnt); |
680 | 31 | compaction_job->set_cumulative_compaction_cnt(_cumulative_compaction_cnt); |
681 | 31 | int64_t now = time(nullptr); |
682 | 31 | compaction_job->set_lease(now + config::lease_compaction_interval_seconds); |
683 | | // No need to set expiration time, since there is no output rowset |
684 | 31 | cloud::StartTabletJobResponse start_resp; |
685 | 31 | auto st = _engine.meta_mgr().prepare_tablet_job(job, &start_resp); |
686 | 31 | if (!st.ok()) { |
687 | 2 | if (start_resp.status().code() == cloud::STALE_TABLET_CACHE) { |
688 | | // set last_sync_time to 0 to force sync tablet next time |
689 | 0 | cloud_tablet()->last_sync_time_s = 0; |
690 | 2 | } else if (start_resp.status().code() == cloud::TABLET_NOT_FOUND) { |
691 | | // tablet not found |
692 | 2 | cloud_tablet()->clear_cache(); |
693 | 2 | } |
694 | 2 | LOG_WARNING("failed to update cumulative point to meta srv") |
695 | 2 | .tag("job_id", _uuid) |
696 | 2 | .tag("tablet_id", _tablet->tablet_id()) |
697 | 2 | .error(st); |
698 | 2 | return; |
699 | 2 | } |
700 | 29 | int64_t input_cumulative_point = cloud_tablet()->cumulative_layer_point(); |
701 | 29 | int64_t output_cumulative_point = _last_delete_version.first + 1; |
702 | 29 | compaction_job->set_input_cumulative_point(input_cumulative_point); |
703 | 29 | compaction_job->set_output_cumulative_point(output_cumulative_point); |
704 | 29 | cloud::FinishTabletJobResponse finish_resp; |
705 | 29 | st = _engine.meta_mgr().commit_tablet_job(job, &finish_resp); |
706 | 29 | if (!st.ok()) { |
707 | 0 | if (finish_resp.status().code() == cloud::TABLET_NOT_FOUND) { |
708 | 0 | cloud_tablet()->clear_cache(); |
709 | 0 | } |
710 | 0 | LOG_WARNING("failed to update cumulative point to meta srv") |
711 | 0 | .tag("job_id", _uuid) |
712 | 0 | .tag("tablet_id", _tablet->tablet_id()) |
713 | 0 | .error(st); |
714 | 0 | return; |
715 | 0 | } |
716 | 29 | LOG_INFO("do empty cumulative compaction to update cumulative point") |
717 | 29 | .tag("job_id", _uuid) |
718 | 29 | .tag("tablet_id", _tablet->tablet_id()) |
719 | 29 | .tag("input_cumulative_point", input_cumulative_point) |
720 | 29 | .tag("output_cumulative_point", output_cumulative_point); |
721 | 29 | auto& stats = finish_resp.stats(); |
722 | 29 | LOG(INFO) << "tablet stats=" << stats.ShortDebugString(); |
723 | 29 | { |
724 | 29 | std::lock_guard wrlock(_tablet->get_header_lock()); |
725 | | // clang-format off |
726 | 29 | cloud_tablet()->set_last_base_compaction_success_time(std::max(cloud_tablet()->last_base_compaction_success_time(), stats.last_base_compaction_time_ms())); |
727 | 29 | cloud_tablet()->set_last_cumu_compaction_success_time(std::max(cloud_tablet()->last_cumu_compaction_success_time(), stats.last_cumu_compaction_time_ms())); |
728 | | // clang-format on |
729 | 29 | if (cloud_tablet()->cumulative_compaction_cnt() >= stats.cumulative_compaction_cnt()) { |
730 | | // This could happen while calling `sync_tablet_rowsets` during `commit_tablet_job` |
731 | 0 | return; |
732 | 0 | } |
733 | | // ATTN: MUST NOT update `base_compaction_cnt` which are used when sync rowsets, otherwise may cause |
734 | | // the tablet to be unable to synchronize the rowset meta changes generated by base compaction. |
735 | 29 | cloud_tablet()->set_cumulative_compaction_cnt(cloud_tablet()->cumulative_compaction_cnt() + |
736 | 29 | 1); |
737 | 29 | cloud_tablet()->set_cumulative_layer_point(stats.cumulative_point()); |
738 | 29 | if (stats.base_compaction_cnt() >= cloud_tablet()->base_compaction_cnt()) { |
739 | 29 | cloud_tablet()->reset_approximate_stats(stats.num_rowsets(), stats.num_segments(), |
740 | 29 | stats.num_rows(), stats.data_size()); |
741 | 29 | } |
742 | 29 | } |
743 | 29 | } |
744 | | |
745 | 175 | void CloudCumulativeCompaction::do_lease() { |
746 | 175 | TEST_INJECTION_POINT_RETURN_WITH_VOID("CloudCumulativeCompaction::do_lease"); |
747 | 175 | if (_state == CompactionState::SUCCESS) { |
748 | 7 | return; |
749 | 7 | } |
750 | 168 | cloud::TabletJobInfoPB job; |
751 | 168 | auto idx = job.mutable_idx(); |
752 | 168 | idx->set_tablet_id(_tablet->tablet_id()); |
753 | 168 | idx->set_table_id(_tablet->table_id()); |
754 | 168 | idx->set_index_id(_tablet->index_id()); |
755 | 168 | idx->set_partition_id(_tablet->partition_id()); |
756 | 168 | auto compaction_job = job.add_compaction(); |
757 | 168 | compaction_job->set_id(_uuid); |
758 | 168 | using namespace std::chrono; |
759 | 168 | int64_t lease_time = duration_cast<seconds>(system_clock::now().time_since_epoch()).count() + |
760 | 168 | config::lease_compaction_interval_seconds * 4; |
761 | 168 | compaction_job->set_lease(lease_time); |
762 | 168 | auto st = _engine.meta_mgr().lease_tablet_job(job); |
763 | 168 | if (!st.ok()) { |
764 | 3 | LOG_WARNING("failed to lease compaction job") |
765 | 3 | .tag("job_id", _uuid) |
766 | 3 | .tag("tablet_id", _tablet->tablet_id()) |
767 | 3 | .error(st); |
768 | 3 | } |
769 | 168 | } |
770 | | |
771 | | } // namespace doris |