/root/doris/cloud/src/recycler/checker.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 "recycler/checker.h" |
19 | | |
20 | | #include <aws/s3/S3Client.h> |
21 | | #include <aws/s3/model/ListObjectsV2Request.h> |
22 | | #include <butil/endpoint.h> |
23 | | #include <butil/strings/string_split.h> |
24 | | #include <fmt/core.h> |
25 | | #include <gen_cpp/cloud.pb.h> |
26 | | #include <gen_cpp/olap_file.pb.h> |
27 | | #include <glog/logging.h> |
28 | | |
29 | | #include <chrono> |
30 | | #include <cstdint> |
31 | | #include <memory> |
32 | | #include <mutex> |
33 | | #include <sstream> |
34 | | #include <string_view> |
35 | | #include <unordered_set> |
36 | | #include <vector> |
37 | | |
38 | | #include "common/bvars.h" |
39 | | #include "common/config.h" |
40 | | #include "common/encryption_util.h" |
41 | | #include "common/logging.h" |
42 | | #include "common/util.h" |
43 | | #include "cpp/sync_point.h" |
44 | | #include "meta-service/keys.h" |
45 | | #include "meta-service/txn_kv.h" |
46 | | #include "meta-service/txn_kv_error.h" |
47 | | #include "recycler/hdfs_accessor.h" |
48 | | #include "recycler/s3_accessor.h" |
49 | | #include "recycler/storage_vault_accessor.h" |
50 | | #ifdef UNIT_TEST |
51 | | #include "../test/mock_accessor.h" |
52 | | #endif |
53 | | #include "recycler/util.h" |
54 | | |
55 | | namespace doris::cloud { |
56 | | namespace config { |
57 | | extern int32_t brpc_listen_port; |
58 | | extern int32_t scan_instances_interval_seconds; |
59 | | extern int32_t recycle_job_lease_expired_ms; |
60 | | extern int32_t recycle_concurrency; |
61 | | extern std::vector<std::string> recycle_whitelist; |
62 | | extern std::vector<std::string> recycle_blacklist; |
63 | | extern bool enable_inverted_check; |
64 | | } // namespace config |
65 | | |
66 | 5 | Checker::Checker(std::shared_ptr<TxnKv> txn_kv) : txn_kv_(std::move(txn_kv)) { |
67 | 5 | ip_port_ = std::string(butil::my_ip_cstr()) + ":" + std::to_string(config::brpc_listen_port); |
68 | 5 | } |
69 | | |
70 | 5 | Checker::~Checker() { |
71 | 5 | if (!stopped()) { |
72 | 1 | stop(); |
73 | 1 | } |
74 | 5 | } |
75 | | |
76 | 4 | int Checker::start() { |
77 | 4 | DCHECK(txn_kv_); |
78 | 4 | instance_filter_.reset(config::recycle_whitelist, config::recycle_blacklist); |
79 | | |
80 | | // launch instance scanner |
81 | 4 | auto scanner_func = [this]() { |
82 | 4 | std::this_thread::sleep_for( |
83 | 4 | std::chrono::seconds(config::recycler_sleep_before_scheduling_seconds)); |
84 | 8 | while (!stopped()) { |
85 | 4 | std::vector<InstanceInfoPB> instances; |
86 | 4 | get_all_instances(txn_kv_.get(), instances); |
87 | 4 | LOG(INFO) << "Checker get instances: " << [&instances] { |
88 | 4 | std::stringstream ss; |
89 | 30 | for (auto& i : instances) ss << ' ' << i.instance_id(); |
90 | 4 | return ss.str(); |
91 | 4 | }(); |
92 | 4 | if (!instances.empty()) { |
93 | | // enqueue instances |
94 | 3 | std::lock_guard lock(mtx_); |
95 | 30 | for (auto& instance : instances) { |
96 | 30 | if (instance_filter_.filter_out(instance.instance_id())) continue; |
97 | 30 | if (instance.status() == InstanceInfoPB::DELETED) continue; |
98 | 30 | using namespace std::chrono; |
99 | 30 | auto enqueue_time_s = |
100 | 30 | duration_cast<seconds>(system_clock::now().time_since_epoch()).count(); |
101 | 30 | auto [_, success] = |
102 | 30 | pending_instance_map_.insert({instance.instance_id(), enqueue_time_s}); |
103 | | // skip instance already in pending queue |
104 | 30 | if (success) { |
105 | 30 | pending_instance_queue_.push_back(std::move(instance)); |
106 | 30 | } |
107 | 30 | } |
108 | 3 | pending_instance_cond_.notify_all(); |
109 | 3 | } |
110 | 4 | { |
111 | 4 | std::unique_lock lock(mtx_); |
112 | 4 | notifier_.wait_for(lock, |
113 | 4 | std::chrono::seconds(config::scan_instances_interval_seconds), |
114 | 7 | [&]() { return stopped(); }); |
115 | 4 | } |
116 | 4 | } |
117 | 4 | }; |
118 | 4 | workers_.emplace_back(scanner_func); |
119 | | // Launch lease thread |
120 | 4 | workers_.emplace_back([this] { lease_check_jobs(); }); |
121 | | // Launch inspect thread |
122 | 4 | workers_.emplace_back([this] { inspect_instance_check_interval(); }); |
123 | | |
124 | | // launch check workers |
125 | 8 | auto checker_func = [this]() { |
126 | 38 | while (!stopped()) { |
127 | | // fetch instance to check |
128 | 36 | InstanceInfoPB instance; |
129 | 36 | long enqueue_time_s = 0; |
130 | 36 | { |
131 | 36 | std::unique_lock lock(mtx_); |
132 | 48 | pending_instance_cond_.wait(lock, [&]() -> bool { |
133 | 48 | return !pending_instance_queue_.empty() || stopped(); |
134 | 48 | }); |
135 | 36 | if (stopped()) { |
136 | 6 | return; |
137 | 6 | } |
138 | 30 | instance = std::move(pending_instance_queue_.front()); |
139 | 30 | pending_instance_queue_.pop_front(); |
140 | 30 | enqueue_time_s = pending_instance_map_[instance.instance_id()]; |
141 | 30 | pending_instance_map_.erase(instance.instance_id()); |
142 | 30 | } |
143 | 0 | const auto& instance_id = instance.instance_id(); |
144 | 30 | { |
145 | 30 | std::lock_guard lock(mtx_); |
146 | | // skip instance in recycling |
147 | 30 | if (working_instance_map_.count(instance_id)) continue; |
148 | 30 | } |
149 | 30 | auto checker = std::make_shared<InstanceChecker>(txn_kv_, instance.instance_id()); |
150 | 30 | if (checker->init(instance) != 0) { |
151 | 0 | LOG(WARNING) << "failed to init instance checker, instance_id=" |
152 | 0 | << instance.instance_id(); |
153 | 0 | continue; |
154 | 0 | } |
155 | 30 | std::string check_job_key; |
156 | 30 | job_check_key({instance.instance_id()}, &check_job_key); |
157 | 30 | int ret = prepare_instance_recycle_job(txn_kv_.get(), check_job_key, |
158 | 30 | instance.instance_id(), ip_port_, |
159 | 30 | config::check_object_interval_seconds * 1000); |
160 | 30 | if (ret != 0) { // Prepare failed |
161 | 20 | continue; |
162 | 20 | } else { |
163 | 10 | std::lock_guard lock(mtx_); |
164 | 10 | working_instance_map_.emplace(instance_id, checker); |
165 | 10 | } |
166 | 10 | if (stopped()) return; |
167 | 10 | using namespace std::chrono; |
168 | 10 | auto ctime_ms = |
169 | 10 | duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); |
170 | 10 | g_bvar_checker_enqueue_cost_s.put(instance_id, ctime_ms / 1000 - enqueue_time_s); |
171 | | |
172 | 10 | bool success {true}; |
173 | | |
174 | 10 | if (int ret = checker->do_check(); ret != 0) { |
175 | 0 | success = false; |
176 | 0 | } |
177 | | |
178 | 10 | if (config::enable_inverted_check) { |
179 | 0 | if (int ret = checker->do_inverted_check(); ret != 0) { |
180 | 0 | success = false; |
181 | 0 | } |
182 | 0 | } |
183 | | |
184 | 10 | if (config::enable_delete_bitmap_inverted_check) { |
185 | 0 | if (int ret = checker->do_delete_bitmap_inverted_check(); ret != 0) { |
186 | 0 | success = false; |
187 | 0 | } |
188 | 0 | } |
189 | | |
190 | 10 | if (config::enable_delete_bitmap_storage_optimize_check) { |
191 | 0 | if (int ret = checker->do_delete_bitmap_storage_optimize_check(); ret != 0) { |
192 | 0 | success = false; |
193 | 0 | } |
194 | 0 | } |
195 | | |
196 | | // If instance checker has been aborted, don't finish this job |
197 | 10 | if (!checker->stopped()) { |
198 | 10 | finish_instance_recycle_job(txn_kv_.get(), check_job_key, instance.instance_id(), |
199 | 10 | ip_port_, success, ctime_ms); |
200 | 10 | } |
201 | 10 | { |
202 | 10 | std::lock_guard lock(mtx_); |
203 | 10 | working_instance_map_.erase(instance.instance_id()); |
204 | 10 | } |
205 | 10 | } |
206 | 8 | }; |
207 | 4 | int num_threads = config::recycle_concurrency; // FIXME: use a new config entry? |
208 | 12 | for (int i = 0; i < num_threads; ++i) { |
209 | 8 | workers_.emplace_back(checker_func); |
210 | 8 | } |
211 | 4 | return 0; |
212 | 4 | } |
213 | | |
214 | 5 | void Checker::stop() { |
215 | 5 | stopped_ = true; |
216 | 5 | notifier_.notify_all(); |
217 | 5 | pending_instance_cond_.notify_all(); |
218 | 5 | { |
219 | 5 | std::lock_guard lock(mtx_); |
220 | 5 | for (auto& [_, checker] : working_instance_map_) { |
221 | 0 | checker->stop(); |
222 | 0 | } |
223 | 5 | } |
224 | 20 | for (auto& w : workers_) { |
225 | 20 | if (w.joinable()) w.join(); |
226 | 20 | } |
227 | 5 | } |
228 | | |
229 | 4 | void Checker::lease_check_jobs() { |
230 | 55 | while (!stopped()) { |
231 | 51 | std::vector<std::string> instances; |
232 | 51 | instances.reserve(working_instance_map_.size()); |
233 | 51 | { |
234 | 51 | std::lock_guard lock(mtx_); |
235 | 51 | for (auto& [id, _] : working_instance_map_) { |
236 | 30 | instances.push_back(id); |
237 | 30 | } |
238 | 51 | } |
239 | 51 | for (auto& i : instances) { |
240 | 30 | std::string check_job_key; |
241 | 30 | job_check_key({i}, &check_job_key); |
242 | 30 | int ret = lease_instance_recycle_job(txn_kv_.get(), check_job_key, i, ip_port_); |
243 | 30 | if (ret == 1) { |
244 | 0 | std::lock_guard lock(mtx_); |
245 | 0 | if (auto it = working_instance_map_.find(i); it != working_instance_map_.end()) { |
246 | 0 | it->second->stop(); |
247 | 0 | } |
248 | 0 | } |
249 | 30 | } |
250 | 51 | { |
251 | 51 | std::unique_lock lock(mtx_); |
252 | 51 | notifier_.wait_for(lock, |
253 | 51 | std::chrono::milliseconds(config::recycle_job_lease_expired_ms / 3), |
254 | 101 | [&]() { return stopped(); }); |
255 | 51 | } |
256 | 51 | } |
257 | 4 | } |
258 | | |
259 | 0 | #define LOG_CHECK_INTERVAL_ALARM LOG(WARNING) << "Err for check interval: " |
260 | 34 | void Checker::do_inspect(const InstanceInfoPB& instance) { |
261 | 34 | std::string check_job_key = job_check_key({instance.instance_id()}); |
262 | 34 | std::unique_ptr<Transaction> txn; |
263 | 34 | std::string val; |
264 | 34 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
265 | 34 | if (err != TxnErrorCode::TXN_OK) { |
266 | 0 | LOG_CHECK_INTERVAL_ALARM << "failed to create txn"; |
267 | 0 | return; |
268 | 0 | } |
269 | 34 | err = txn->get(check_job_key, &val); |
270 | 34 | if (err != TxnErrorCode::TXN_OK && err != TxnErrorCode::TXN_KEY_NOT_FOUND) { |
271 | 0 | LOG_CHECK_INTERVAL_ALARM << "failed to get kv, err=" << err |
272 | 0 | << " key=" << hex(check_job_key); |
273 | 0 | return; |
274 | 0 | } |
275 | 34 | auto checker = InstanceChecker(txn_kv_, instance.instance_id()); |
276 | 34 | if (checker.init(instance) != 0) { |
277 | 0 | LOG_CHECK_INTERVAL_ALARM << "failed to init instance checker, instance_id=" |
278 | 0 | << instance.instance_id(); |
279 | 0 | return; |
280 | 0 | } |
281 | | |
282 | 34 | int64_t bucket_lifecycle_days = 0; |
283 | 34 | if (checker.get_bucket_lifecycle(&bucket_lifecycle_days) != 0) { |
284 | 0 | LOG_CHECK_INTERVAL_ALARM << "failed to get bucket lifecycle, instance_id=" |
285 | 0 | << instance.instance_id(); |
286 | 0 | return; |
287 | 0 | } |
288 | 34 | DCHECK(bucket_lifecycle_days > 0); |
289 | | |
290 | 34 | if (bucket_lifecycle_days == INT64_MAX) { |
291 | | // No s3 bucket (may all accessors are HdfsAccessor), skip inspect |
292 | 34 | return; |
293 | 34 | } |
294 | | |
295 | 0 | int64_t last_ctime_ms = -1; |
296 | 0 | auto job_status = JobRecyclePB::IDLE; |
297 | 0 | auto has_last_ctime = [&]() { |
298 | 0 | JobRecyclePB job_info; |
299 | 0 | if (!job_info.ParseFromString(val)) { |
300 | 0 | LOG_CHECK_INTERVAL_ALARM << "failed to parse JobRecyclePB, key=" << hex(check_job_key); |
301 | 0 | } |
302 | 0 | DCHECK(job_info.instance_id() == instance.instance_id()); |
303 | 0 | if (!job_info.has_last_ctime_ms()) return false; |
304 | 0 | last_ctime_ms = job_info.last_ctime_ms(); |
305 | 0 | job_status = job_info.status(); |
306 | 0 | g_bvar_checker_last_success_time_ms.put(instance.instance_id(), |
307 | 0 | job_info.last_success_time_ms()); |
308 | 0 | return true; |
309 | 0 | }; |
310 | 0 | using namespace std::chrono; |
311 | 0 | auto now = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); |
312 | 0 | if (err == TxnErrorCode::TXN_KEY_NOT_FOUND || !has_last_ctime()) { |
313 | | // Use instance's ctime for instances that do not have job's last ctime |
314 | 0 | last_ctime_ms = instance.ctime(); |
315 | 0 | } |
316 | 0 | DCHECK(now - last_ctime_ms >= 0); |
317 | 0 | int64_t expiration_ms = |
318 | 0 | bucket_lifecycle_days > config::reserved_buffer_days |
319 | 0 | ? (bucket_lifecycle_days - config::reserved_buffer_days) * 86400000 |
320 | 0 | : bucket_lifecycle_days * 86400000; |
321 | 0 | TEST_SYNC_POINT_CALLBACK("Checker:do_inspect", &last_ctime_ms); |
322 | 0 | if (now - last_ctime_ms >= expiration_ms) { |
323 | 0 | LOG_CHECK_INTERVAL_ALARM << "check risks, instance_id: " << instance.instance_id() |
324 | 0 | << " last_ctime_ms: " << last_ctime_ms |
325 | 0 | << " job_status: " << job_status |
326 | 0 | << " bucket_lifecycle_days: " << bucket_lifecycle_days |
327 | 0 | << " reserved_buffer_days: " << config::reserved_buffer_days |
328 | 0 | << " expiration_ms: " << expiration_ms; |
329 | 0 | } |
330 | 0 | } |
331 | | #undef LOG_CHECK_INTERVAL_ALARM |
332 | 4 | void Checker::inspect_instance_check_interval() { |
333 | 7 | while (!stopped()) { |
334 | 3 | LOG(INFO) << "start to inspect instance check interval"; |
335 | 3 | std::vector<InstanceInfoPB> instances; |
336 | 3 | get_all_instances(txn_kv_.get(), instances); |
337 | 30 | for (const auto& instance : instances) { |
338 | 30 | if (instance_filter_.filter_out(instance.instance_id())) continue; |
339 | 30 | if (stopped()) return; |
340 | 30 | if (instance.status() == InstanceInfoPB::DELETED) continue; |
341 | 30 | do_inspect(instance); |
342 | 30 | } |
343 | 3 | { |
344 | 3 | std::unique_lock lock(mtx_); |
345 | 3 | notifier_.wait_for(lock, std::chrono::seconds(config::scan_instances_interval_seconds), |
346 | 6 | [&]() { return stopped(); }); |
347 | 3 | } |
348 | 3 | } |
349 | 4 | } |
350 | | |
351 | | // return 0 for success get a key, 1 for key not found, negative for error |
352 | 13 | int key_exist(TxnKv* txn_kv, std::string_view key) { |
353 | 13 | std::unique_ptr<Transaction> txn; |
354 | 13 | TxnErrorCode err = txn_kv->create_txn(&txn); |
355 | 13 | if (err != TxnErrorCode::TXN_OK) { |
356 | 0 | LOG(WARNING) << "failed to init txn, err=" << err; |
357 | 0 | return -1; |
358 | 0 | } |
359 | 13 | std::string val; |
360 | 13 | switch (txn->get(key, &val)) { |
361 | 13 | case TxnErrorCode::TXN_OK: |
362 | 13 | return 0; |
363 | 0 | case TxnErrorCode::TXN_KEY_NOT_FOUND: |
364 | 0 | return 1; |
365 | 0 | default: |
366 | 0 | return -1; |
367 | 13 | } |
368 | 13 | } |
369 | | |
370 | | InstanceChecker::InstanceChecker(std::shared_ptr<TxnKv> txn_kv, const std::string& instance_id) |
371 | 71 | : txn_kv_(std::move(txn_kv)), instance_id_(instance_id) {} |
372 | | |
373 | 71 | int InstanceChecker::init(const InstanceInfoPB& instance) { |
374 | 71 | int ret = init_obj_store_accessors(instance); |
375 | 71 | if (ret != 0) { |
376 | 0 | return ret; |
377 | 0 | } |
378 | | |
379 | 71 | return init_storage_vault_accessors(instance); |
380 | 71 | } |
381 | | |
382 | 71 | int InstanceChecker::init_obj_store_accessors(const InstanceInfoPB& instance) { |
383 | 71 | for (const auto& obj_info : instance.obj_info()) { |
384 | 71 | #ifdef UNIT_TEST |
385 | 71 | auto accessor = std::make_shared<MockAccessor>(); |
386 | | #else |
387 | | auto s3_conf = S3Conf::from_obj_store_info(obj_info); |
388 | | if (!s3_conf) { |
389 | | LOG(WARNING) << "failed to init object accessor, instance_id=" << instance_id_; |
390 | | return -1; |
391 | | } |
392 | | |
393 | | std::shared_ptr<S3Accessor> accessor; |
394 | | int ret = S3Accessor::create(std::move(*s3_conf), &accessor); |
395 | | if (ret != 0) { |
396 | | LOG(WARNING) << "failed to init object accessor. instance_id=" << instance_id_ |
397 | | << " resource_id=" << obj_info.id(); |
398 | | return ret; |
399 | | } |
400 | | #endif |
401 | | |
402 | 71 | accessor_map_.emplace(obj_info.id(), std::move(accessor)); |
403 | 71 | } |
404 | | |
405 | 71 | return 0; |
406 | 71 | } |
407 | | |
408 | 71 | int InstanceChecker::init_storage_vault_accessors(const InstanceInfoPB& instance) { |
409 | 71 | if (instance.resource_ids().empty()) { |
410 | 71 | return 0; |
411 | 71 | } |
412 | | |
413 | 0 | FullRangeGetIteratorOptions opts(txn_kv_); |
414 | 0 | opts.prefetch = true; |
415 | 0 | auto it = txn_kv_->full_range_get(storage_vault_key({instance_id_, ""}), |
416 | 0 | storage_vault_key({instance_id_, "\xff"}), std::move(opts)); |
417 | |
|
418 | 0 | for (auto kv = it->next(); kv.has_value(); kv = it->next()) { |
419 | 0 | auto [k, v] = *kv; |
420 | 0 | StorageVaultPB vault; |
421 | 0 | if (!vault.ParseFromArray(v.data(), v.size())) { |
422 | 0 | LOG(WARNING) << "malformed storage vault, unable to deserialize key=" << hex(k); |
423 | 0 | return -1; |
424 | 0 | } |
425 | | |
426 | 0 | if (vault.has_hdfs_info()) { |
427 | 0 | auto accessor = std::make_shared<HdfsAccessor>(vault.hdfs_info()); |
428 | 0 | int ret = accessor->init(); |
429 | 0 | if (ret != 0) { |
430 | 0 | LOG(WARNING) << "failed to init hdfs accessor. instance_id=" << instance_id_ |
431 | 0 | << " resource_id=" << vault.id() << " name=" << vault.name(); |
432 | 0 | return ret; |
433 | 0 | } |
434 | | |
435 | 0 | accessor_map_.emplace(vault.id(), std::move(accessor)); |
436 | 0 | } else if (vault.has_obj_info()) { |
437 | 0 | #ifdef UNIT_TEST |
438 | 0 | auto accessor = std::make_shared<MockAccessor>(); |
439 | | #else |
440 | | auto s3_conf = S3Conf::from_obj_store_info(vault.obj_info()); |
441 | | if (!s3_conf) { |
442 | | LOG(WARNING) << "failed to init object accessor, instance_id=" << instance_id_; |
443 | | return -1; |
444 | | } |
445 | | |
446 | | std::shared_ptr<S3Accessor> accessor; |
447 | | int ret = S3Accessor::create(std::move(*s3_conf), &accessor); |
448 | | if (ret != 0) { |
449 | | LOG(WARNING) << "failed to init s3 accessor. instance_id=" << instance_id_ |
450 | | << " resource_id=" << vault.id() << " name=" << vault.name(); |
451 | | return ret; |
452 | | } |
453 | | #endif |
454 | |
|
455 | 0 | accessor_map_.emplace(vault.id(), std::move(accessor)); |
456 | 0 | } |
457 | 0 | } |
458 | | |
459 | 0 | if (!it->is_valid()) { |
460 | 0 | LOG_WARNING("failed to get storage vault kv"); |
461 | 0 | return -1; |
462 | 0 | } |
463 | 0 | return 0; |
464 | 0 | } |
465 | | |
466 | 12 | int InstanceChecker::do_check() { |
467 | 12 | TEST_SYNC_POINT("InstanceChecker.do_check"); |
468 | 12 | LOG(INFO) << "begin to check instance objects instance_id=" << instance_id_; |
469 | 12 | int check_ret = 0; |
470 | 12 | long num_scanned = 0; |
471 | 12 | long num_scanned_with_segment = 0; |
472 | 12 | long num_rowset_loss = 0; |
473 | 12 | long instance_volume = 0; |
474 | 12 | using namespace std::chrono; |
475 | 12 | auto start_time = steady_clock::now(); |
476 | 12 | std::unique_ptr<int, std::function<void(int*)>> defer_log_statistics((int*)0x01, [&](int*) { |
477 | 12 | auto cost = duration<float>(steady_clock::now() - start_time).count(); |
478 | 12 | LOG(INFO) << "check instance objects finished, cost=" << cost |
479 | 12 | << "s. instance_id=" << instance_id_ << " num_scanned=" << num_scanned |
480 | 12 | << " num_scanned_with_segment=" << num_scanned_with_segment |
481 | 12 | << " num_rowset_loss=" << num_rowset_loss |
482 | 12 | << " instance_volume=" << instance_volume; |
483 | 12 | g_bvar_checker_num_scanned.put(instance_id_, num_scanned); |
484 | 12 | g_bvar_checker_num_scanned_with_segment.put(instance_id_, num_scanned_with_segment); |
485 | 12 | g_bvar_checker_num_check_failed.put(instance_id_, num_rowset_loss); |
486 | 12 | g_bvar_checker_check_cost_s.put(instance_id_, static_cast<long>(cost)); |
487 | | // FIXME(plat1ko): What if some list operation failed? |
488 | 12 | g_bvar_checker_instance_volume.put(instance_id_, instance_volume); |
489 | 12 | }); |
490 | | |
491 | 12 | struct TabletFiles { |
492 | 12 | int64_t tablet_id {0}; |
493 | 12 | std::unordered_set<std::string> files; |
494 | 12 | }; |
495 | 12 | TabletFiles tablet_files_cache; |
496 | | |
497 | 12 | auto check_rowset_objects = [&, this](const doris::RowsetMetaCloudPB& rs_meta, |
498 | 4.00k | std::string_view key) { |
499 | 4.00k | if (rs_meta.num_segments() == 0) { |
500 | 0 | return; |
501 | 0 | } |
502 | | |
503 | 4.00k | ++num_scanned_with_segment; |
504 | 4.00k | if (tablet_files_cache.tablet_id != rs_meta.tablet_id()) { |
505 | 400 | long tablet_volume = 0; |
506 | | // Clear cache |
507 | 400 | tablet_files_cache.tablet_id = 0; |
508 | 400 | tablet_files_cache.files.clear(); |
509 | | // Get all file paths under this tablet directory |
510 | 400 | auto find_it = accessor_map_.find(rs_meta.resource_id()); |
511 | 400 | if (find_it == accessor_map_.end()) { |
512 | 0 | LOG_WARNING("resource id not found in accessor map") |
513 | 0 | .tag("resource_id", rs_meta.resource_id()) |
514 | 0 | .tag("tablet_id", rs_meta.tablet_id()) |
515 | 0 | .tag("rowset_id", rs_meta.rowset_id_v2()); |
516 | 0 | check_ret = -1; |
517 | 0 | return; |
518 | 0 | } |
519 | | |
520 | 400 | std::unique_ptr<ListIterator> list_iter; |
521 | 400 | int ret = find_it->second->list_directory(tablet_path_prefix(rs_meta.tablet_id()), |
522 | 400 | &list_iter); |
523 | 400 | if (ret != 0) { // No need to log, because S3Accessor has logged this error |
524 | 0 | check_ret = -1; |
525 | 0 | return; |
526 | 0 | } |
527 | | |
528 | 18.3k | for (auto file = list_iter->next(); file.has_value(); file = list_iter->next()) { |
529 | 17.9k | tablet_files_cache.files.insert(std::move(file->path)); |
530 | 17.9k | tablet_volume += file->size; |
531 | 17.9k | } |
532 | 400 | tablet_files_cache.tablet_id = rs_meta.tablet_id(); |
533 | 400 | instance_volume += tablet_volume; |
534 | 400 | } |
535 | | |
536 | 4.00k | bool data_loss = false; |
537 | 16.0k | for (int i = 0; i < rs_meta.num_segments(); ++i) { |
538 | 12.0k | auto path = segment_path(rs_meta.tablet_id(), rs_meta.rowset_id_v2(), i); |
539 | 12.0k | if (tablet_files_cache.files.contains(path)) { |
540 | 11.9k | continue; |
541 | 11.9k | } |
542 | | |
543 | 13 | if (1 == key_exist(txn_kv_.get(), key)) { |
544 | | // Rowset has been deleted instead of data loss |
545 | 0 | break; |
546 | 0 | } |
547 | 13 | data_loss = true; |
548 | 13 | TEST_SYNC_POINT_CALLBACK("InstanceChecker.do_check1", &path); |
549 | 13 | LOG(WARNING) << "object not exist, path=" << path << " key=" << hex(key); |
550 | 13 | } |
551 | | |
552 | 4.00k | if (data_loss) { |
553 | 9 | ++num_rowset_loss; |
554 | 9 | } |
555 | 4.00k | }; |
556 | | |
557 | | // scan visible rowsets |
558 | 12 | auto start_key = meta_rowset_key({instance_id_, 0, 0}); |
559 | 12 | auto end_key = meta_rowset_key({instance_id_, INT64_MAX, 0}); |
560 | | |
561 | 12 | std::unique_ptr<RangeGetIterator> it; |
562 | 12 | do { |
563 | 12 | std::unique_ptr<Transaction> txn; |
564 | 12 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
565 | 12 | if (err != TxnErrorCode::TXN_OK) { |
566 | 0 | LOG(WARNING) << "failed to init txn, err=" << err; |
567 | 0 | return -1; |
568 | 0 | } |
569 | | |
570 | 12 | err = txn->get(start_key, end_key, &it); |
571 | 12 | if (err != TxnErrorCode::TXN_OK) { |
572 | 0 | LOG(WARNING) << "internal error, failed to get rowset meta, err=" << err; |
573 | 0 | return -1; |
574 | 0 | } |
575 | 12 | num_scanned += it->size(); |
576 | | |
577 | 4.01k | while (it->has_next() && !stopped()) { |
578 | 4.00k | auto [k, v] = it->next(); |
579 | 4.00k | if (!it->has_next()) start_key = k; |
580 | | |
581 | 4.00k | doris::RowsetMetaCloudPB rs_meta; |
582 | 4.00k | if (!rs_meta.ParseFromArray(v.data(), v.size())) { |
583 | 0 | ++num_rowset_loss; |
584 | 0 | LOG(WARNING) << "malformed rowset meta. key=" << hex(k) << " val=" << hex(v); |
585 | 0 | continue; |
586 | 0 | } |
587 | 4.00k | check_rowset_objects(rs_meta, k); |
588 | 4.00k | } |
589 | 12 | start_key.push_back('\x00'); // Update to next smallest key for iteration |
590 | 12 | } while (it->more() && !stopped()); |
591 | | |
592 | 12 | return num_rowset_loss > 0 ? 1 : check_ret; |
593 | 12 | } |
594 | | |
595 | 34 | int InstanceChecker::get_bucket_lifecycle(int64_t* lifecycle_days) { |
596 | | // If there are multiple buckets, return the minimum lifecycle. |
597 | 34 | int64_t min_lifecycle_days = INT64_MAX; |
598 | 34 | int64_t tmp_liefcycle_days = 0; |
599 | 34 | for (const auto& [id, accessor] : accessor_map_) { |
600 | 34 | if (accessor->type() != AccessorType::S3) { |
601 | 34 | continue; |
602 | 34 | } |
603 | | |
604 | 0 | auto* s3_accessor = static_cast<S3Accessor*>(accessor.get()); |
605 | |
|
606 | 0 | if (s3_accessor->check_versioning() != 0) { |
607 | 0 | return -1; |
608 | 0 | } |
609 | | |
610 | 0 | if (s3_accessor->get_life_cycle(&tmp_liefcycle_days) != 0) { |
611 | 0 | return -1; |
612 | 0 | } |
613 | | |
614 | 0 | if (tmp_liefcycle_days < min_lifecycle_days) { |
615 | 0 | min_lifecycle_days = tmp_liefcycle_days; |
616 | 0 | } |
617 | 0 | } |
618 | 34 | *lifecycle_days = min_lifecycle_days; |
619 | 34 | return 0; |
620 | 34 | } |
621 | | |
622 | 1 | int InstanceChecker::do_inverted_check() { |
623 | 1 | if (accessor_map_.size() > 1) { |
624 | 0 | LOG(INFO) << "currently not support inverted check for multi accessor. instance_id=" |
625 | 0 | << instance_id_; |
626 | 0 | return 0; |
627 | 0 | } |
628 | | |
629 | 1 | LOG(INFO) << "begin to inverted check objects instance_id=" << instance_id_; |
630 | 1 | int check_ret = 0; |
631 | 1 | long num_scanned = 0; |
632 | 1 | long num_file_leak = 0; |
633 | 1 | using namespace std::chrono; |
634 | 1 | auto start_time = steady_clock::now(); |
635 | 1 | std::unique_ptr<int, std::function<void(int*)>> defer_log_statistics((int*)0x01, [&](int*) { |
636 | 1 | g_bvar_inverted_checker_num_scanned.put(instance_id_, num_scanned); |
637 | 1 | g_bvar_inverted_checker_num_check_failed.put(instance_id_, num_file_leak); |
638 | 1 | auto cost = duration<float>(steady_clock::now() - start_time).count(); |
639 | 1 | LOG(INFO) << "inverted check instance objects finished, cost=" << cost |
640 | 1 | << "s. instance_id=" << instance_id_ << " num_scanned=" << num_scanned |
641 | 1 | << " num_file_leak=" << num_file_leak; |
642 | 1 | }); |
643 | | |
644 | 1 | struct TabletRowsets { |
645 | 1 | int64_t tablet_id {0}; |
646 | 1 | std::unordered_set<std::string> rowset_ids; |
647 | 1 | }; |
648 | 1 | TabletRowsets tablet_rowsets_cache; |
649 | | |
650 | | // Return 0 if check success, return 1 if file is garbage data, negative if error occurred |
651 | 1 | auto check_segment_file = [&](const std::string& obj_key) { |
652 | 0 | std::vector<std::string> str; |
653 | 0 | butil::SplitString(obj_key, '/', &str); |
654 | | // data/{tablet_id}/{rowset_id}_{seg_num}.dat |
655 | 0 | if (str.size() < 3) { |
656 | 0 | return -1; |
657 | 0 | } |
658 | | |
659 | 0 | int64_t tablet_id = atol(str[1].c_str()); |
660 | 0 | if (tablet_id <= 0) { |
661 | 0 | LOG(WARNING) << "failed to parse tablet_id, key=" << obj_key; |
662 | 0 | return -1; |
663 | 0 | } |
664 | | |
665 | 0 | std::string rowset_id; |
666 | 0 | if (auto pos = str.back().find('_'); pos != std::string::npos) { |
667 | 0 | rowset_id = str.back().substr(0, pos); |
668 | 0 | } else { |
669 | 0 | LOG(WARNING) << "failed to parse rowset_id, key=" << obj_key; |
670 | 0 | return -1; |
671 | 0 | } |
672 | | |
673 | 0 | if (tablet_rowsets_cache.tablet_id == tablet_id) { |
674 | 0 | if (tablet_rowsets_cache.rowset_ids.contains(rowset_id)) { |
675 | 0 | return 0; |
676 | 0 | } else { |
677 | 0 | LOG(WARNING) << "rowset not exists, key=" << obj_key; |
678 | 0 | return -1; |
679 | 0 | } |
680 | 0 | } |
681 | | // Get all rowset id of this tablet |
682 | 0 | tablet_rowsets_cache.tablet_id = tablet_id; |
683 | 0 | tablet_rowsets_cache.rowset_ids.clear(); |
684 | 0 | std::unique_ptr<Transaction> txn; |
685 | 0 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
686 | 0 | if (err != TxnErrorCode::TXN_OK) { |
687 | 0 | LOG(WARNING) << "failed to create txn"; |
688 | 0 | return -1; |
689 | 0 | } |
690 | 0 | std::unique_ptr<RangeGetIterator> it; |
691 | 0 | auto begin = meta_rowset_key({instance_id_, tablet_id, 0}); |
692 | 0 | auto end = meta_rowset_key({instance_id_, tablet_id, INT64_MAX}); |
693 | 0 | do { |
694 | 0 | TxnErrorCode err = txn->get(begin, end, &it); |
695 | 0 | if (err != TxnErrorCode::TXN_OK) { |
696 | 0 | LOG(WARNING) << "failed to get rowset kv, err=" << err; |
697 | 0 | return -1; |
698 | 0 | } |
699 | 0 | if (!it->has_next()) { |
700 | 0 | break; |
701 | 0 | } |
702 | 0 | while (it->has_next()) { |
703 | | // recycle corresponding resources |
704 | 0 | auto [k, v] = it->next(); |
705 | 0 | doris::RowsetMetaCloudPB rowset; |
706 | 0 | if (!rowset.ParseFromArray(v.data(), v.size())) { |
707 | 0 | LOG(WARNING) << "malformed rowset meta value, key=" << hex(k); |
708 | 0 | return -1; |
709 | 0 | } |
710 | 0 | tablet_rowsets_cache.rowset_ids.insert(rowset.rowset_id_v2()); |
711 | 0 | if (!it->has_next()) { |
712 | 0 | begin = k; |
713 | 0 | begin.push_back('\x00'); // Update to next smallest key for iteration |
714 | 0 | break; |
715 | 0 | } |
716 | 0 | } |
717 | 0 | } while (it->more() && !stopped()); |
718 | | |
719 | 0 | if (!tablet_rowsets_cache.rowset_ids.contains(rowset_id)) { |
720 | | // Garbage data leak |
721 | 0 | LOG(WARNING) << "rowset should be recycled, key=" << obj_key; |
722 | 0 | return 1; |
723 | 0 | } |
724 | | |
725 | 0 | return 0; |
726 | 0 | }; |
727 | | |
728 | | // TODO(Xiaocc): Currently we haven't implemented one generator-like s3 accessor list function |
729 | | // so we choose to skip here. |
730 | 1 | TEST_SYNC_POINT_RETURN_WITH_VALUE("InstanceChecker::do_inverted_check", (int)0); |
731 | |
|
732 | 0 | for (auto& [_, accessor] : accessor_map_) { |
733 | 0 | std::unique_ptr<ListIterator> list_iter; |
734 | 0 | int ret = accessor->list_directory("data", &list_iter); |
735 | 0 | if (ret != 0) { |
736 | 0 | return -1; |
737 | 0 | } |
738 | | |
739 | 0 | for (auto file = list_iter->next(); file.has_value(); file = list_iter->next()) { |
740 | 0 | ++num_scanned; |
741 | 0 | int ret = check_segment_file(file->path); |
742 | 0 | if (ret != 0) { |
743 | 0 | LOG(WARNING) << "failed to check segment file, uri=" << accessor->uri() |
744 | 0 | << " path=" << file->path; |
745 | 0 | if (ret == 1) { |
746 | 0 | ++num_file_leak; |
747 | 0 | } else { |
748 | 0 | check_ret = -1; |
749 | 0 | } |
750 | 0 | } |
751 | 0 | } |
752 | |
|
753 | 0 | if (!list_iter->is_valid()) { |
754 | 0 | LOG(WARNING) << "failed to list data directory. uri=" << accessor->uri(); |
755 | 0 | return -1; |
756 | 0 | } |
757 | 0 | } |
758 | 0 | return num_file_leak > 0 ? 1 : check_ret; |
759 | 0 | } |
760 | | |
761 | 2 | int InstanceChecker::traverse_mow_tablet(const std::function<int(int64_t)>& check_func) { |
762 | 2 | std::unique_ptr<RangeGetIterator> it; |
763 | 2 | auto begin = meta_rowset_key({instance_id_, 0, 0}); |
764 | 2 | auto end = meta_rowset_key({instance_id_, std::numeric_limits<int64_t>::max(), 0}); |
765 | 37 | do { |
766 | 37 | std::unique_ptr<Transaction> txn; |
767 | 37 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
768 | 37 | if (err != TxnErrorCode::TXN_OK) { |
769 | 0 | LOG(WARNING) << "failed to create txn"; |
770 | 0 | return -1; |
771 | 0 | } |
772 | 37 | err = txn->get(begin, end, &it, false, 1); |
773 | 37 | if (err != TxnErrorCode::TXN_OK) { |
774 | 0 | LOG(WARNING) << "failed to get rowset kv, err=" << err; |
775 | 0 | return -1; |
776 | 0 | } |
777 | 37 | if (!it->has_next()) { |
778 | 2 | break; |
779 | 2 | } |
780 | 70 | while (it->has_next() && !stopped()) { |
781 | 35 | auto [k, v] = it->next(); |
782 | 35 | std::string_view k1 = k; |
783 | 35 | k1.remove_prefix(1); |
784 | 35 | std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> out; |
785 | 35 | decode_key(&k1, &out); |
786 | | // 0x01 "meta" ${instance_id} "rowset" ${tablet_id} ${version} -> RowsetMetaCloudPB |
787 | 35 | auto tablet_id = std::get<int64_t>(std::get<0>(out[3])); |
788 | | |
789 | 35 | if (!it->has_next()) { |
790 | | // Update to next smallest key for iteration |
791 | | // scan for next tablet in this instance |
792 | 35 | begin = meta_rowset_key({instance_id_, tablet_id + 1, 0}); |
793 | 35 | } |
794 | | |
795 | 35 | TabletMetaCloudPB tablet_meta; |
796 | 35 | int ret = get_tablet_meta(txn_kv_.get(), instance_id_, tablet_id, tablet_meta); |
797 | 35 | if (ret < 0) { |
798 | 0 | LOG(WARNING) << fmt::format( |
799 | 0 | "failed to get_tablet_meta in do_delete_bitmap_integrity_check(), " |
800 | 0 | "instance_id={}, tablet_id={}", |
801 | 0 | instance_id_, tablet_id); |
802 | 0 | return ret; |
803 | 0 | } |
804 | | |
805 | 35 | if (tablet_meta.enable_unique_key_merge_on_write()) { |
806 | | // only check merge-on-write table |
807 | 25 | int ret = check_func(tablet_id); |
808 | 25 | if (ret < 0) { |
809 | | // return immediately when encounter unexpected error, |
810 | | // otherwise, we continue to check the next tablet |
811 | 0 | return ret; |
812 | 0 | } |
813 | 25 | } |
814 | 35 | } |
815 | 35 | } while (it->more() && !stopped()); |
816 | 2 | return 0; |
817 | 2 | } |
818 | | |
819 | | int InstanceChecker::traverse_rowset_delete_bitmaps( |
820 | | int64_t tablet_id, std::string rowset_id, |
821 | 60 | const std::function<int(int64_t, std::string_view, int64_t, int64_t)>& callback) { |
822 | 60 | std::unique_ptr<RangeGetIterator> it; |
823 | 60 | auto begin = meta_delete_bitmap_key({instance_id_, tablet_id, rowset_id, 0, 0}); |
824 | 60 | auto end = meta_delete_bitmap_key({instance_id_, tablet_id, rowset_id, |
825 | 60 | std::numeric_limits<int64_t>::max(), |
826 | 60 | std::numeric_limits<int64_t>::max()}); |
827 | 60 | do { |
828 | 60 | std::unique_ptr<Transaction> txn; |
829 | 60 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
830 | 60 | if (err != TxnErrorCode::TXN_OK) { |
831 | 0 | LOG(WARNING) << "failed to create txn"; |
832 | 0 | return -1; |
833 | 0 | } |
834 | 60 | err = txn->get(begin, end, &it); |
835 | 60 | if (err != TxnErrorCode::TXN_OK) { |
836 | 0 | LOG(WARNING) << "failed to get rowset kv, err=" << err; |
837 | 0 | return -1; |
838 | 0 | } |
839 | 60 | if (!it->has_next()) { |
840 | 0 | break; |
841 | 0 | } |
842 | 150 | while (it->has_next() && !stopped()) { |
843 | 150 | auto [k, v] = it->next(); |
844 | 150 | std::string_view k1 = k; |
845 | 150 | k1.remove_prefix(1); |
846 | 150 | std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> out; |
847 | 150 | decode_key(&k1, &out); |
848 | | // 0x01 "meta" ${instance_id} "delete_bitmap" ${tablet_id} ${rowset_id} ${version} ${segment_id} -> roaringbitmap |
849 | 150 | auto version = std::get<std::int64_t>(std::get<0>(out[5])); |
850 | 150 | auto segment_id = std::get<std::int64_t>(std::get<0>(out[6])); |
851 | | |
852 | 150 | int ret = callback(tablet_id, rowset_id, version, segment_id); |
853 | 150 | if (ret != 0) { |
854 | 10 | return ret; |
855 | 10 | } |
856 | | |
857 | 140 | if (!it->has_next()) { |
858 | 50 | begin = k; |
859 | 50 | begin.push_back('\x00'); // Update to next smallest key for iteration |
860 | 50 | break; |
861 | 50 | } |
862 | 140 | } |
863 | 60 | } while (it->more() && !stopped()); |
864 | | |
865 | 50 | return 0; |
866 | 60 | } |
867 | | |
868 | | int InstanceChecker::collect_tablet_rowsets( |
869 | 45 | int64_t tablet_id, const std::function<void(const doris::RowsetMetaCloudPB&)>& collect_cb) { |
870 | 45 | std::unique_ptr<Transaction> txn; |
871 | 45 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
872 | 45 | if (err != TxnErrorCode::TXN_OK) { |
873 | 0 | LOG(WARNING) << "failed to create txn"; |
874 | 0 | return -1; |
875 | 0 | } |
876 | 45 | std::unique_ptr<RangeGetIterator> it; |
877 | 45 | auto begin = meta_rowset_key({instance_id_, tablet_id, 0}); |
878 | 45 | auto end = meta_rowset_key({instance_id_, tablet_id + 1, 0}); |
879 | | |
880 | 45 | int64_t rowsets_num {0}; |
881 | 45 | do { |
882 | 45 | TxnErrorCode err = txn->get(begin, end, &it); |
883 | 45 | if (err != TxnErrorCode::TXN_OK) { |
884 | 0 | LOG(WARNING) << "failed to get rowset kv, err=" << err; |
885 | 0 | return -1; |
886 | 0 | } |
887 | 45 | if (!it->has_next()) { |
888 | 0 | break; |
889 | 0 | } |
890 | 375 | while (it->has_next() && !stopped()) { |
891 | 375 | auto [k, v] = it->next(); |
892 | 375 | doris::RowsetMetaCloudPB rowset; |
893 | 375 | if (!rowset.ParseFromArray(v.data(), v.size())) { |
894 | 0 | LOG(WARNING) << "malformed rowset meta value, key=" << hex(k); |
895 | 0 | return -1; |
896 | 0 | } |
897 | | |
898 | 375 | ++rowsets_num; |
899 | 375 | collect_cb(rowset); |
900 | | |
901 | 375 | if (!it->has_next()) { |
902 | 45 | begin = k; |
903 | 45 | begin.push_back('\x00'); // Update to next smallest key for iteration |
904 | 45 | break; |
905 | 45 | } |
906 | 375 | } |
907 | 45 | } while (it->more() && !stopped()); |
908 | | |
909 | 45 | LOG(INFO) << fmt::format( |
910 | 45 | "[delete bitmap checker] successfully collect rowsets for instance_id={}, " |
911 | 45 | "tablet_id={}, rowsets_num={}", |
912 | 45 | instance_id_, tablet_id, rowsets_num); |
913 | 45 | return 0; |
914 | 45 | } |
915 | | |
916 | 2 | int InstanceChecker::do_delete_bitmap_inverted_check() { |
917 | 2 | LOG(INFO) << fmt::format( |
918 | 2 | "[delete bitmap checker] begin to do_delete_bitmap_inverted_check for instance_id={}", |
919 | 2 | instance_id_); |
920 | | |
921 | | // number of delete bitmap keys being scanned |
922 | 2 | int64_t total_delete_bitmap_keys {0}; |
923 | | // number of delete bitmaps which belongs to non mow tablet |
924 | 2 | int64_t abnormal_delete_bitmaps {0}; |
925 | | // number of delete bitmaps which doesn't have corresponding rowset in MS |
926 | 2 | int64_t leaked_delete_bitmaps {0}; |
927 | | |
928 | 2 | auto start_time = std::chrono::steady_clock::now(); |
929 | 2 | std::unique_ptr<int, std::function<void(int*)>> defer_log_statistics((int*)0x01, [&](int*) { |
930 | 2 | g_bvar_inverted_checker_leaked_delete_bitmaps.put(instance_id_, leaked_delete_bitmaps); |
931 | 2 | g_bvar_inverted_checker_abnormal_delete_bitmaps.put(instance_id_, abnormal_delete_bitmaps); |
932 | 2 | g_bvar_inverted_checker_delete_bitmaps_scanned.put(instance_id_, total_delete_bitmap_keys); |
933 | | |
934 | 2 | auto cost = std::chrono::duration_cast<std::chrono::milliseconds>( |
935 | 2 | std::chrono::steady_clock::now() - start_time) |
936 | 2 | .count(); |
937 | 2 | if (leaked_delete_bitmaps > 0 || abnormal_delete_bitmaps > 0) { |
938 | 1 | LOG(WARNING) << fmt::format( |
939 | 1 | "[delete bitmap check fails] delete bitmap inverted check for instance_id={}, " |
940 | 1 | "cost={} ms, total_delete_bitmap_keys={}, leaked_delete_bitmaps={}, " |
941 | 1 | "abnormal_delete_bitmaps={}", |
942 | 1 | instance_id_, cost, total_delete_bitmap_keys, leaked_delete_bitmaps, |
943 | 1 | abnormal_delete_bitmaps); |
944 | 1 | } else { |
945 | 1 | LOG(INFO) << fmt::format( |
946 | 1 | "[delete bitmap checker] delete bitmap inverted check for instance_id={}, " |
947 | 1 | "passed. cost={} ms, total_delete_bitmap_keys={}", |
948 | 1 | instance_id_, cost, total_delete_bitmap_keys); |
949 | 1 | } |
950 | 2 | }); |
951 | | |
952 | 2 | struct TabletsRowsetsCache { |
953 | 2 | int64_t tablet_id {-1}; |
954 | 2 | bool enable_merge_on_write {false}; |
955 | 2 | std::unordered_set<std::string> rowsets {}; |
956 | 2 | } tablet_rowsets_cache {}; |
957 | | |
958 | 2 | std::unique_ptr<RangeGetIterator> it; |
959 | 2 | auto begin = meta_delete_bitmap_key({instance_id_, 0, "", 0, 0}); |
960 | 2 | auto end = |
961 | 2 | meta_delete_bitmap_key({instance_id_, std::numeric_limits<int64_t>::max(), "", 0, 0}); |
962 | 2 | do { |
963 | 2 | std::unique_ptr<Transaction> txn; |
964 | 2 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
965 | 2 | if (err != TxnErrorCode::TXN_OK) { |
966 | 0 | LOG(WARNING) << "failed to create txn"; |
967 | 0 | return -1; |
968 | 0 | } |
969 | 2 | err = txn->get(begin, end, &it); |
970 | 2 | if (err != TxnErrorCode::TXN_OK) { |
971 | 0 | LOG(WARNING) << "failed to get rowset kv, err=" << err; |
972 | 0 | return -1; |
973 | 0 | } |
974 | 2 | if (!it->has_next()) { |
975 | 0 | break; |
976 | 0 | } |
977 | 502 | while (it->has_next() && !stopped()) { |
978 | 500 | auto [k, v] = it->next(); |
979 | 500 | std::string_view k1 = k; |
980 | 500 | k1.remove_prefix(1); |
981 | 500 | std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> out; |
982 | 500 | decode_key(&k1, &out); |
983 | | // 0x01 "meta" ${instance_id} "delete_bitmap" ${tablet_id} ${rowset_id} ${version} ${segment_id} -> roaringbitmap |
984 | 500 | auto tablet_id = std::get<int64_t>(std::get<0>(out[3])); |
985 | 500 | auto rowset_id = std::get<std::string>(std::get<0>(out[4])); |
986 | 500 | auto version = std::get<std::int64_t>(std::get<0>(out[5])); |
987 | 500 | auto segment_id = std::get<std::int64_t>(std::get<0>(out[6])); |
988 | | |
989 | 500 | ++total_delete_bitmap_keys; |
990 | | |
991 | 500 | if (!it->has_next()) { |
992 | 2 | begin = k; |
993 | 2 | begin.push_back('\x00'); // Update to next smallest key for iteration |
994 | 2 | } |
995 | | |
996 | 500 | if (tablet_rowsets_cache.tablet_id == -1 || |
997 | 500 | tablet_rowsets_cache.tablet_id != tablet_id) { |
998 | 30 | TabletMetaCloudPB tablet_meta; |
999 | 30 | int ret = get_tablet_meta(txn_kv_.get(), instance_id_, tablet_id, tablet_meta); |
1000 | 30 | if (ret < 0) { |
1001 | 0 | LOG(WARNING) << fmt::format( |
1002 | 0 | "[delete bitmap checker] failed to get_tablet_meta in " |
1003 | 0 | "do_delete_bitmap_inverted_check(), instance_id={}, tablet_id={}", |
1004 | 0 | instance_id_, tablet_id); |
1005 | 0 | return ret; |
1006 | 0 | } |
1007 | | |
1008 | 30 | tablet_rowsets_cache.tablet_id = tablet_id; |
1009 | 30 | tablet_rowsets_cache.enable_merge_on_write = |
1010 | 30 | tablet_meta.enable_unique_key_merge_on_write(); |
1011 | 30 | tablet_rowsets_cache.rowsets.clear(); |
1012 | | |
1013 | 30 | if (tablet_rowsets_cache.enable_merge_on_write) { |
1014 | | // only collect rowsets for merge-on-write tablet |
1015 | 20 | auto collect_cb = |
1016 | 200 | [&tablet_rowsets_cache](const doris::RowsetMetaCloudPB& rowset) { |
1017 | 200 | tablet_rowsets_cache.rowsets.insert(rowset.rowset_id_v2()); |
1018 | 200 | }; |
1019 | 20 | ret = collect_tablet_rowsets(tablet_id, collect_cb); |
1020 | 20 | if (ret < 0) { |
1021 | 0 | return ret; |
1022 | 0 | } |
1023 | 20 | } |
1024 | 30 | } |
1025 | 500 | DCHECK_EQ(tablet_id, tablet_rowsets_cache.tablet_id); |
1026 | | |
1027 | 500 | if (!tablet_rowsets_cache.enable_merge_on_write) { |
1028 | | // clang-format off |
1029 | 40 | TEST_SYNC_POINT_CALLBACK( |
1030 | 40 | "InstanceChecker::do_delete_bitmap_inverted_check.get_abnormal_delete_bitmap", |
1031 | 40 | &tablet_id, &rowset_id, &version, &segment_id); |
1032 | | // clang-format on |
1033 | 40 | ++abnormal_delete_bitmaps; |
1034 | | // log an error and continue to check the next delete bitmap |
1035 | 40 | LOG(WARNING) << fmt::format( |
1036 | 40 | "[delete bitmap check fails] find a delete bitmap belongs to tablet " |
1037 | 40 | "which is not a merge-on-write table! instance_id={}, tablet_id={}, " |
1038 | 40 | "version={}, segment_id={}", |
1039 | 40 | instance_id_, tablet_id, version, segment_id); |
1040 | 40 | continue; |
1041 | 40 | } |
1042 | | |
1043 | 460 | if (!tablet_rowsets_cache.rowsets.contains(rowset_id)) { |
1044 | 170 | TEST_SYNC_POINT_CALLBACK( |
1045 | 170 | "InstanceChecker::do_delete_bitmap_inverted_check.get_leaked_delete_bitmap", |
1046 | 170 | &tablet_id, &rowset_id, &version, &segment_id); |
1047 | 170 | ++leaked_delete_bitmaps; |
1048 | | // log an error and continue to check the next delete bitmap |
1049 | 170 | LOG(WARNING) << fmt::format( |
1050 | 170 | "[delete bitmap check fails] can't find corresponding rowset for delete " |
1051 | 170 | "bitmap instance_id={}, tablet_id={}, rowset_id={}, version={}, " |
1052 | 170 | "segment_id={}", |
1053 | 170 | instance_id_, tablet_id, rowset_id, version, segment_id); |
1054 | 170 | } |
1055 | 460 | } |
1056 | 2 | } while (it->more() && !stopped()); |
1057 | | |
1058 | 2 | return (leaked_delete_bitmaps > 0 || abnormal_delete_bitmaps > 0) ? 1 : 0; |
1059 | 2 | } |
1060 | | |
1061 | 25 | int InstanceChecker::check_delete_bitmap_storage_optimize(int64_t tablet_id) { |
1062 | 25 | using Version = std::pair<int64_t, int64_t>; |
1063 | 25 | struct RowsetDigest { |
1064 | 25 | std::string rowset_id; |
1065 | 25 | Version version; |
1066 | 25 | doris::SegmentsOverlapPB segments_overlap; |
1067 | | |
1068 | 300 | bool operator<(const RowsetDigest& other) const { |
1069 | 300 | return version.first < other.version.first; |
1070 | 300 | } |
1071 | | |
1072 | 115 | bool produced_by_compaction() const { |
1073 | 115 | return (version.first < version.second) || |
1074 | 115 | ((version.first == version.second) && segments_overlap == NONOVERLAPPING); |
1075 | 115 | } |
1076 | 25 | }; |
1077 | | |
1078 | | // number of rowsets which may have problems |
1079 | 25 | int64_t abnormal_rowsets_num {0}; |
1080 | | |
1081 | 25 | std::vector<RowsetDigest> tablet_rowsets {}; |
1082 | | // Get all visible rowsets of this tablet |
1083 | 175 | auto collect_cb = [&tablet_rowsets](const doris::RowsetMetaCloudPB& rowset) { |
1084 | 175 | if (rowset.start_version() == 0 && rowset.end_version() == 1) { |
1085 | | // ignore dummy rowset [0-1] |
1086 | 0 | return; |
1087 | 0 | } |
1088 | 175 | tablet_rowsets.emplace_back( |
1089 | 175 | rowset.rowset_id_v2(), |
1090 | 175 | std::make_pair<int64_t, int64_t>(rowset.start_version(), rowset.end_version()), |
1091 | 175 | rowset.segments_overlap_pb()); |
1092 | 175 | }; |
1093 | 25 | if (int ret = collect_tablet_rowsets(tablet_id, collect_cb); ret != 0) { |
1094 | 0 | return ret; |
1095 | 0 | } |
1096 | | |
1097 | 25 | std::sort(tablet_rowsets.begin(), tablet_rowsets.end()); |
1098 | | |
1099 | | // find right-most rowset which is produced by compaction |
1100 | 25 | auto it = std::find_if( |
1101 | 25 | tablet_rowsets.crbegin(), tablet_rowsets.crend(), |
1102 | 115 | [](const RowsetDigest& rowset) { return rowset.produced_by_compaction(); }); |
1103 | 25 | if (it == tablet_rowsets.crend()) { |
1104 | 5 | LOG(INFO) << fmt::format( |
1105 | 5 | "[delete bitmap checker] skip to check delete bitmap storage optimize for " |
1106 | 5 | "tablet_id={} because it doesn't have compacted rowsets.", |
1107 | 5 | tablet_id); |
1108 | 5 | return 0; |
1109 | 5 | } |
1110 | | |
1111 | 20 | int64_t start_version = it->version.first; |
1112 | 20 | int64_t pre_min_version = it->version.second; |
1113 | | |
1114 | | // after BE sweeping stale rowsets, all rowsets in this tablet before |
1115 | | // should not have delete bitmaps with versions lower than `pre_min_version` |
1116 | 20 | if (config::delete_bitmap_storage_optimize_check_version_gap > 0) { |
1117 | 0 | pre_min_version -= config::delete_bitmap_storage_optimize_check_version_gap; |
1118 | 0 | if (pre_min_version <= 1) { |
1119 | 0 | LOG(INFO) << fmt::format( |
1120 | 0 | "[delete bitmap checker] skip to check delete bitmap storage optimize for " |
1121 | 0 | "tablet_id={} because pre_min_version is too small.", |
1122 | 0 | tablet_id); |
1123 | 0 | return 0; |
1124 | 0 | } |
1125 | 0 | } |
1126 | | |
1127 | 20 | auto check_func = [pre_min_version, instance_id = instance_id_]( |
1128 | 20 | int64_t tablet_id, std::string_view rowset_id, int64_t version, |
1129 | 150 | int64_t segment_id) -> int { |
1130 | 150 | if (version < pre_min_version) { |
1131 | 10 | LOG(WARNING) << fmt::format( |
1132 | 10 | "[delete bitmap check fails] delete bitmap storage optimize check fail for " |
1133 | 10 | "instance_id={}, tablet_id={}, rowset_id={}, found delete bitmap with " |
1134 | 10 | "version={} < pre_min_version={}", |
1135 | 10 | instance_id, tablet_id, rowset_id, version, pre_min_version); |
1136 | 10 | return 1; |
1137 | 10 | } |
1138 | 140 | return 0; |
1139 | 150 | }; |
1140 | | |
1141 | 135 | for (const auto& rowset : tablet_rowsets) { |
1142 | | // check for all rowsets before the max compacted rowset |
1143 | 135 | if (rowset.version.second < start_version) { |
1144 | 60 | auto rowset_id = rowset.rowset_id; |
1145 | 60 | int ret = traverse_rowset_delete_bitmaps(tablet_id, rowset_id, check_func); |
1146 | 60 | if (ret < 0) { |
1147 | 0 | return ret; |
1148 | 0 | } |
1149 | | |
1150 | 60 | if (ret != 0) { |
1151 | 10 | ++abnormal_rowsets_num; |
1152 | 10 | TEST_SYNC_POINT_CALLBACK( |
1153 | 10 | "InstanceChecker::check_delete_bitmap_storage_optimize.get_abnormal_rowset", |
1154 | 10 | &tablet_id, &rowset_id); |
1155 | 10 | } |
1156 | 60 | } |
1157 | 135 | } |
1158 | | |
1159 | 20 | LOG(INFO) << fmt::format( |
1160 | 20 | "[delete bitmap checker] finish check delete bitmap storage optimize for " |
1161 | 20 | "instance_id={}, tablet_id={}, rowsets_num={}, abnormal_rowsets_num={}, " |
1162 | 20 | "pre_min_version={}", |
1163 | 20 | instance_id_, tablet_id, tablet_rowsets.size(), abnormal_rowsets_num, pre_min_version); |
1164 | | |
1165 | 20 | return (abnormal_rowsets_num > 1 ? 1 : 0); |
1166 | 20 | } |
1167 | | |
1168 | 2 | int InstanceChecker::do_delete_bitmap_storage_optimize_check() { |
1169 | 2 | int64_t total_tablets_num {0}; |
1170 | 2 | int64_t failed_tablets_num {0}; |
1171 | | |
1172 | | // check that for every visible rowset, there exists at least delete one bitmap in MS |
1173 | 25 | int ret = traverse_mow_tablet([&](int64_t tablet_id) { |
1174 | 25 | ++total_tablets_num; |
1175 | 25 | int res = check_delete_bitmap_storage_optimize(tablet_id); |
1176 | 25 | failed_tablets_num += (res != 0); |
1177 | 25 | return res; |
1178 | 25 | }); |
1179 | | |
1180 | 2 | if (ret < 0) { |
1181 | 0 | return ret; |
1182 | 0 | } |
1183 | | |
1184 | 2 | LOG(INFO) << fmt::format( |
1185 | 2 | "[delete bitmap checker] check delete bitmap storage optimize for instance_id={}, " |
1186 | 2 | "total_tablets_num={}, failed_tablets_num={}", |
1187 | 2 | instance_id_, total_tablets_num, failed_tablets_num); |
1188 | | |
1189 | 2 | return (failed_tablets_num > 0) ? 1 : 0; |
1190 | 2 | } |
1191 | | |
1192 | | } // namespace doris::cloud |