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