/root/doris/cloud/src/recycler/checker.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 "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 <algorithm> |
30 | | #include <chrono> |
31 | | #include <climits> |
32 | | #include <cstdint> |
33 | | #include <functional> |
34 | | #include <memory> |
35 | | #include <mutex> |
36 | | #include <numeric> |
37 | | #include <sstream> |
38 | | #include <string_view> |
39 | | #include <unordered_map> |
40 | | #include <unordered_set> |
41 | | #include <vector> |
42 | | |
43 | | #include "common/bvars.h" |
44 | | #include "common/config.h" |
45 | | #include "common/defer.h" |
46 | | #include "common/encryption_util.h" |
47 | | #include "common/logging.h" |
48 | | #include "common/util.h" |
49 | | #include "cpp/sync_point.h" |
50 | | #include "meta-service/meta_service.h" |
51 | | #include "meta-service/meta_service_schema.h" |
52 | | #include "meta-service/meta_service_tablet_stats.h" |
53 | | #include "meta-store/blob_message.h" |
54 | | #include "meta-store/keys.h" |
55 | | #include "meta-store/txn_kv.h" |
56 | | #include "snapshot/snapshot_manager_factory.h" |
57 | | #ifdef ENABLE_HDFS_STORAGE_VAULT |
58 | | #include "recycler/hdfs_accessor.h" |
59 | | #endif |
60 | | #include "recycler/s3_accessor.h" |
61 | | #include "recycler/storage_vault_accessor.h" |
62 | | #ifdef UNIT_TEST |
63 | | #include "../test/mock_accessor.h" |
64 | | #endif |
65 | | #include "recycler/recycler.h" |
66 | | #include "recycler/util.h" |
67 | | |
68 | | namespace doris::cloud { |
69 | | namespace config { |
70 | | extern int32_t brpc_listen_port; |
71 | | extern int32_t scan_instances_interval_seconds; |
72 | | extern int32_t recycle_job_lease_expired_ms; |
73 | | extern int32_t recycle_concurrency; |
74 | | extern std::vector<std::string> recycle_whitelist; |
75 | | extern std::vector<std::string> recycle_blacklist; |
76 | | extern bool enable_inverted_check; |
77 | | } // namespace config |
78 | | |
79 | | using namespace std::chrono; |
80 | | |
81 | | Checker::Checker(std::shared_ptr<TxnKv> txn_kv) |
82 | 5 | : txn_kv_(std::move(txn_kv)), resource_mgr_(std::make_shared<ResourceManager>(txn_kv_)) { |
83 | 5 | ip_port_ = std::string(butil::my_ip_cstr()) + ":" + std::to_string(config::brpc_listen_port); |
84 | 5 | } |
85 | | |
86 | 5 | Checker::~Checker() { |
87 | 5 | if (!stopped()) { |
88 | 1 | stop(); |
89 | 1 | } |
90 | 5 | } |
91 | | |
92 | 4 | int Checker::start() { |
93 | 4 | DCHECK(txn_kv_); |
94 | 4 | instance_filter_.reset(config::recycle_whitelist, config::recycle_blacklist); |
95 | | |
96 | | // launch instance scanner |
97 | 4 | auto scanner_func = [this]() { |
98 | 4 | std::this_thread::sleep_for( |
99 | 4 | std::chrono::seconds(config::recycler_sleep_before_scheduling_seconds)); |
100 | 8 | while (!stopped()) { |
101 | 4 | std::vector<InstanceInfoPB> instances; |
102 | 4 | if (get_all_instances(txn_kv_.get(), instances) != 0) { |
103 | | // Do not publish a partial owner index after a failed scan. |
104 | 0 | instances.clear(); |
105 | 0 | } |
106 | 4 | LOG(INFO) << "Checker get instances: " << [&instances] { |
107 | 4 | std::stringstream ss; |
108 | 30 | for (auto& i : instances) ss << ' ' << i.instance_id(); |
109 | 4 | return ss.str(); |
110 | 4 | }(); |
111 | 4 | if (!instances.empty()) { |
112 | 3 | auto shared_storage_index = |
113 | 3 | std::make_shared<SharedStorageIndex>(txn_kv_, instances); |
114 | | // enqueue instances |
115 | 3 | std::lock_guard lock(mtx_); |
116 | 30 | for (auto& instance : instances) { |
117 | 30 | if (instance_filter_.filter_out(instance.instance_id())) continue; |
118 | 30 | if (instance.status() == InstanceInfoPB::DELETED) continue; |
119 | 30 | using namespace std::chrono; |
120 | 30 | auto enqueue_time_s = |
121 | 30 | duration_cast<seconds>(system_clock::now().time_since_epoch()).count(); |
122 | 30 | auto [_, success] = |
123 | 30 | pending_instance_map_.insert({instance.instance_id(), enqueue_time_s}); |
124 | | // skip instance already in pending queue |
125 | 30 | if (success) { |
126 | 30 | pending_instance_queue_.push_back( |
127 | 30 | {std::move(instance), shared_storage_index}); |
128 | 30 | } |
129 | 30 | } |
130 | 3 | pending_instance_cond_.notify_all(); |
131 | 3 | } |
132 | 4 | { |
133 | 4 | std::unique_lock lock(mtx_); |
134 | 4 | notifier_.wait_for(lock, |
135 | 4 | std::chrono::seconds(config::scan_instances_interval_seconds), |
136 | 7 | [&]() { return stopped(); }); |
137 | 4 | } |
138 | 4 | } |
139 | 4 | }; |
140 | 4 | workers_.emplace_back(scanner_func); |
141 | | // Launch lease thread |
142 | 4 | workers_.emplace_back([this] { lease_check_jobs(); }); |
143 | | // Launch inspect thread |
144 | 4 | workers_.emplace_back([this] { inspect_instance_check_interval(); }); |
145 | | |
146 | | // launch check workers |
147 | 8 | auto checker_func = [this]() { |
148 | 38 | while (!stopped()) { |
149 | | // fetch instance to check |
150 | 36 | InstanceInfoPB instance; |
151 | 36 | std::shared_ptr<SharedStorageIndex> shared_storage_index; |
152 | 36 | long enqueue_time_s = 0; |
153 | 36 | { |
154 | 36 | std::unique_lock lock(mtx_); |
155 | 48 | pending_instance_cond_.wait(lock, [&]() -> bool { |
156 | 48 | return !pending_instance_queue_.empty() || stopped(); |
157 | 48 | }); |
158 | 36 | if (stopped()) { |
159 | 6 | return; |
160 | 6 | } |
161 | 30 | instance = std::move(pending_instance_queue_.front().instance); |
162 | 30 | shared_storage_index = |
163 | 30 | std::move(pending_instance_queue_.front().shared_storage_index); |
164 | 30 | pending_instance_queue_.pop_front(); |
165 | 30 | enqueue_time_s = pending_instance_map_[instance.instance_id()]; |
166 | 30 | pending_instance_map_.erase(instance.instance_id()); |
167 | 30 | } |
168 | 0 | const auto& instance_id = instance.instance_id(); |
169 | 30 | { |
170 | 30 | std::lock_guard lock(mtx_); |
171 | | // skip instance in recycling |
172 | 30 | if (working_instance_map_.count(instance_id)) { |
173 | 0 | LOG(INFO) << "checker skip instance already working, instance_id=" |
174 | 0 | << instance_id; |
175 | 0 | continue; |
176 | 0 | } |
177 | 30 | } |
178 | 30 | auto checker = std::make_shared<InstanceChecker>(txn_kv_, instance.instance_id(), |
179 | 30 | resource_mgr_, |
180 | 30 | std::move(shared_storage_index)); |
181 | 30 | if (checker->init(instance) != 0) { |
182 | 0 | LOG(WARNING) << "failed to init instance checker, instance_id=" |
183 | 0 | << instance.instance_id(); |
184 | 0 | continue; |
185 | 0 | } |
186 | 30 | std::string check_job_key; |
187 | 30 | job_check_key({instance.instance_id()}, &check_job_key); |
188 | 30 | LOG(INFO) << "checker picked instance, instance_id=" << instance.instance_id() |
189 | 30 | << " enqueue_time_s=" << enqueue_time_s; |
190 | 30 | int ret = prepare_instance_recycle_job(txn_kv_.get(), check_job_key, |
191 | 30 | instance.instance_id(), ip_port_, |
192 | 30 | config::check_object_interval_seconds * 1000); |
193 | 30 | if (ret != 0) { // Prepare failed |
194 | 20 | LOG(WARNING) << "checker prepare job failed, instance_id=" << instance.instance_id() |
195 | 20 | << " ret=" << ret; |
196 | 20 | continue; |
197 | 20 | } else { |
198 | 10 | std::lock_guard lock(mtx_); |
199 | 10 | working_instance_map_.emplace(instance_id, checker); |
200 | 10 | } |
201 | 10 | if (stopped()) return; |
202 | 10 | using namespace std::chrono; |
203 | 10 | auto ctime_ms = |
204 | 10 | duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); |
205 | 10 | g_bvar_checker_enqueue_cost_s.put(instance_id, ctime_ms / 1000 - enqueue_time_s); |
206 | | |
207 | 10 | bool success {true}; |
208 | 10 | auto log_progress = [&](std::string_view stage) { |
209 | 10 | LOG(INFO) << "checker progress, instance_id=" << instance_id << " stage=" << stage; |
210 | 10 | }; |
211 | | |
212 | 10 | log_progress("do_check"); |
213 | 10 | if (int ret = checker->do_check(); ret != 0) { |
214 | 0 | success = false; |
215 | 0 | } |
216 | | |
217 | 10 | if (config::enable_inverted_check) { |
218 | 0 | log_progress("do_inverted_check"); |
219 | 0 | if (int ret = checker->do_inverted_check(); ret != 0) { |
220 | 0 | success = false; |
221 | 0 | } |
222 | 0 | } |
223 | | |
224 | 10 | if (config::enable_delete_bitmap_inverted_check) { |
225 | 0 | log_progress("do_delete_bitmap_inverted_check"); |
226 | 0 | if (int ret = checker->do_delete_bitmap_inverted_check(); ret != 0) { |
227 | 0 | success = false; |
228 | 0 | } |
229 | 0 | } |
230 | | |
231 | 10 | if (config::enable_mow_job_key_check) { |
232 | 0 | log_progress("do_mow_job_key_check"); |
233 | 0 | if (int ret = checker->do_mow_job_key_check(); ret != 0) { |
234 | 0 | success = false; |
235 | 0 | } |
236 | 0 | } |
237 | | |
238 | 10 | if (config::enable_tablet_stats_key_check) { |
239 | 0 | log_progress("do_tablet_stats_key_check"); |
240 | 0 | if (int ret = checker->do_tablet_stats_key_check(); ret != 0) { |
241 | 0 | success = false; |
242 | 0 | } |
243 | 0 | } |
244 | | |
245 | 10 | if (config::enable_restore_job_check) { |
246 | 0 | log_progress("do_restore_job_check"); |
247 | 0 | if (int ret = checker->do_restore_job_check(); ret != 0) { |
248 | 0 | success = false; |
249 | 0 | } |
250 | 0 | } |
251 | | |
252 | 10 | if (config::enable_txn_key_check) { |
253 | 0 | log_progress("do_txn_key_check"); |
254 | 0 | if (int ret = checker->do_txn_key_check(); ret != 0) { |
255 | 0 | success = false; |
256 | 0 | } |
257 | 0 | } |
258 | | |
259 | 10 | if (config::enable_meta_rowset_key_check) { |
260 | 0 | log_progress("do_meta_rowset_key_check"); |
261 | 0 | if (int ret = checker->do_meta_rowset_key_check(); ret != 0) { |
262 | 0 | success = false; |
263 | 0 | } |
264 | 0 | } |
265 | | |
266 | 10 | if (config::enable_delete_bitmap_storage_optimize_v2_check) { |
267 | 0 | log_progress("do_delete_bitmap_storage_optimize_check_v2"); |
268 | 0 | if (int ret = checker->do_delete_bitmap_storage_optimize_check(2 /*version*/); |
269 | 0 | ret != 0) { |
270 | 0 | success = false; |
271 | 0 | } |
272 | 0 | } |
273 | | |
274 | 10 | if (config::enable_version_key_check) { |
275 | 0 | log_progress("do_version_key_check"); |
276 | 0 | if (int ret = checker->do_version_key_check(); ret != 0) { |
277 | 0 | success = false; |
278 | 0 | } |
279 | 0 | } |
280 | | |
281 | 10 | if (config::enable_snapshot_check) { |
282 | 0 | log_progress("do_snapshots_check"); |
283 | 0 | if (int ret = checker->do_snapshots_check(); ret != 0) { |
284 | 0 | success = false; |
285 | 0 | } |
286 | 0 | } |
287 | | |
288 | 10 | if (config::enable_mvcc_meta_key_check) { |
289 | 0 | log_progress("do_mvcc_meta_key_check"); |
290 | 0 | if (int ret = checker->do_mvcc_meta_key_check(); ret != 0) { |
291 | 0 | success = false; |
292 | 0 | } |
293 | 0 | } |
294 | | |
295 | 10 | if (config::enable_packed_file_check) { |
296 | 0 | log_progress("do_packed_file_check"); |
297 | 0 | if (int ret = checker->do_packed_file_check(); ret != 0) { |
298 | 0 | success = false; |
299 | 0 | } |
300 | 0 | } |
301 | | |
302 | | // If instance checker has been aborted, don't finish this job |
303 | 10 | if (!checker->stopped()) { |
304 | 10 | finish_instance_recycle_job(txn_kv_.get(), check_job_key, instance.instance_id(), |
305 | 10 | ip_port_, success, ctime_ms); |
306 | 10 | } |
307 | 10 | LOG(INFO) << "checker finished instance, instance_id=" << instance.instance_id() |
308 | 10 | << " success=" << success; |
309 | 10 | { |
310 | 10 | std::lock_guard lock(mtx_); |
311 | 10 | working_instance_map_.erase(instance.instance_id()); |
312 | 10 | } |
313 | 10 | } |
314 | 8 | }; |
315 | 4 | int num_threads = config::recycle_concurrency; // FIXME: use a new config entry? |
316 | 12 | for (int i = 0; i < num_threads; ++i) { |
317 | 8 | workers_.emplace_back(checker_func); |
318 | 8 | } |
319 | 4 | return 0; |
320 | 4 | } |
321 | | |
322 | 5 | void Checker::stop() { |
323 | 5 | stopped_ = true; |
324 | 5 | notifier_.notify_all(); |
325 | 5 | pending_instance_cond_.notify_all(); |
326 | 5 | { |
327 | 5 | std::lock_guard lock(mtx_); |
328 | 5 | for (auto& [_, checker] : working_instance_map_) { |
329 | 0 | checker->stop(); |
330 | 0 | } |
331 | 5 | } |
332 | 20 | for (auto& w : workers_) { |
333 | 20 | if (w.joinable()) w.join(); |
334 | 20 | } |
335 | 5 | } |
336 | | |
337 | 4 | void Checker::lease_check_jobs() { |
338 | 54 | while (!stopped()) { |
339 | 50 | std::vector<std::string> instances; |
340 | 50 | instances.reserve(working_instance_map_.size()); |
341 | 50 | { |
342 | 50 | std::lock_guard lock(mtx_); |
343 | 50 | for (auto& [id, _] : working_instance_map_) { |
344 | 30 | instances.push_back(id); |
345 | 30 | } |
346 | 50 | } |
347 | 50 | for (auto& i : instances) { |
348 | 30 | std::string check_job_key; |
349 | 30 | job_check_key({i}, &check_job_key); |
350 | 30 | int ret = lease_instance_recycle_job(txn_kv_.get(), check_job_key, i, ip_port_); |
351 | 30 | if (ret == 1) { |
352 | 0 | std::lock_guard lock(mtx_); |
353 | 0 | if (auto it = working_instance_map_.find(i); it != working_instance_map_.end()) { |
354 | 0 | it->second->stop(); |
355 | 0 | } |
356 | 0 | } |
357 | 30 | } |
358 | 50 | { |
359 | 50 | std::unique_lock lock(mtx_); |
360 | 50 | notifier_.wait_for(lock, |
361 | 50 | std::chrono::milliseconds(config::recycle_job_lease_expired_ms / 3), |
362 | 100 | [&]() { return stopped(); }); |
363 | 50 | } |
364 | 50 | } |
365 | 4 | } |
366 | 0 | #define LOG_CHECK_INTERVAL_ALARM LOG(WARNING) << "Err for check interval: " |
367 | 34 | void Checker::do_inspect(const InstanceInfoPB& instance) { |
368 | 34 | std::string check_job_key = job_check_key({instance.instance_id()}); |
369 | 34 | std::unique_ptr<Transaction> txn; |
370 | 34 | std::string val; |
371 | 34 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
372 | 34 | if (err != TxnErrorCode::TXN_OK) { |
373 | 0 | LOG_CHECK_INTERVAL_ALARM << "failed to create txn"; |
374 | 0 | return; |
375 | 0 | } |
376 | 34 | err = txn->get(check_job_key, &val); |
377 | 34 | if (err != TxnErrorCode::TXN_OK && err != TxnErrorCode::TXN_KEY_NOT_FOUND) { |
378 | 0 | LOG_CHECK_INTERVAL_ALARM << "failed to get kv, err=" << err |
379 | 0 | << " key=" << hex(check_job_key); |
380 | 0 | return; |
381 | 0 | } |
382 | 34 | auto checker = InstanceChecker(txn_kv_, instance.instance_id(), resource_mgr_); |
383 | 34 | if (checker.init(instance) != 0) { |
384 | 0 | LOG_CHECK_INTERVAL_ALARM << "failed to init instance checker, instance_id=" |
385 | 0 | << instance.instance_id(); |
386 | 0 | return; |
387 | 0 | } |
388 | | |
389 | 34 | int64_t bucket_lifecycle_days = 0; |
390 | 34 | if (checker.get_bucket_lifecycle(&bucket_lifecycle_days) != 0) { |
391 | 0 | LOG_CHECK_INTERVAL_ALARM << "failed to get bucket lifecycle, instance_id=" |
392 | 0 | << instance.instance_id(); |
393 | 0 | return; |
394 | 0 | } |
395 | 34 | DCHECK(bucket_lifecycle_days > 0); |
396 | | |
397 | 34 | if (bucket_lifecycle_days == INT64_MAX) { |
398 | | // No s3 bucket (may all accessors are HdfsAccessor), skip inspect |
399 | 34 | return; |
400 | 34 | } |
401 | | |
402 | 0 | int64_t last_ctime_ms = -1; |
403 | 0 | auto job_status = JobRecyclePB::IDLE; |
404 | 0 | auto has_last_ctime = [&]() { |
405 | 0 | JobRecyclePB job_info; |
406 | 0 | if (!job_info.ParseFromString(val)) { |
407 | 0 | LOG_CHECK_INTERVAL_ALARM << "failed to parse JobRecyclePB, key=" << hex(check_job_key); |
408 | 0 | } |
409 | 0 | DCHECK(job_info.instance_id() == instance.instance_id()); |
410 | 0 | if (!job_info.has_last_ctime_ms()) return false; |
411 | 0 | last_ctime_ms = job_info.last_ctime_ms(); |
412 | 0 | job_status = job_info.status(); |
413 | 0 | g_bvar_checker_last_success_time_ms.put(instance.instance_id(), |
414 | 0 | job_info.last_success_time_ms()); |
415 | 0 | return true; |
416 | 0 | }; |
417 | 0 | using namespace std::chrono; |
418 | 0 | auto now = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); |
419 | 0 | if (err == TxnErrorCode::TXN_KEY_NOT_FOUND || !has_last_ctime()) { |
420 | | // Use instance's ctime for instances that do not have job's last ctime |
421 | 0 | last_ctime_ms = instance.ctime(); |
422 | 0 | } |
423 | 0 | DCHECK(now - last_ctime_ms >= 0); |
424 | 0 | int64_t expiration_ms = |
425 | 0 | bucket_lifecycle_days > config::reserved_buffer_days |
426 | 0 | ? (bucket_lifecycle_days - config::reserved_buffer_days) * 86400000 |
427 | 0 | : bucket_lifecycle_days * 86400000; |
428 | 0 | TEST_SYNC_POINT_CALLBACK("Checker:do_inspect", &last_ctime_ms); |
429 | 0 | if (now - last_ctime_ms >= expiration_ms) { |
430 | 0 | LOG_CHECK_INTERVAL_ALARM << "check risks, instance_id: " << instance.instance_id() |
431 | 0 | << " last_ctime_ms: " << last_ctime_ms |
432 | 0 | << " job_status: " << job_status |
433 | 0 | << " bucket_lifecycle_days: " << bucket_lifecycle_days |
434 | 0 | << " reserved_buffer_days: " << config::reserved_buffer_days |
435 | 0 | << " expiration_ms: " << expiration_ms; |
436 | 0 | } |
437 | 0 | } |
438 | | #undef LOG_CHECK_INTERVAL_ALARM |
439 | 4 | void Checker::inspect_instance_check_interval() { |
440 | 8 | while (!stopped()) { |
441 | 4 | LOG(INFO) << "start to inspect instance check interval"; |
442 | 4 | std::vector<InstanceInfoPB> instances; |
443 | 4 | get_all_instances(txn_kv_.get(), instances); |
444 | 30 | for (const auto& instance : instances) { |
445 | 30 | if (instance_filter_.filter_out(instance.instance_id())) continue; |
446 | 30 | if (stopped()) return; |
447 | 30 | if (instance.status() == InstanceInfoPB::DELETED) continue; |
448 | 30 | do_inspect(instance); |
449 | 30 | } |
450 | 4 | { |
451 | 4 | std::unique_lock lock(mtx_); |
452 | 4 | notifier_.wait_for(lock, std::chrono::seconds(config::scan_instances_interval_seconds), |
453 | 7 | [&]() { return stopped(); }); |
454 | 4 | } |
455 | 4 | } |
456 | 4 | } |
457 | | |
458 | | // return 0 for success get a key, 1 for key not found, negative for error |
459 | 23 | int key_exist(TxnKv* txn_kv, std::string_view key) { |
460 | 23 | std::unique_ptr<Transaction> txn; |
461 | 23 | TxnErrorCode err = txn_kv->create_txn(&txn); |
462 | 23 | if (err != TxnErrorCode::TXN_OK) { |
463 | 0 | LOG(WARNING) << "failed to init txn, err=" << err; |
464 | 0 | return -1; |
465 | 0 | } |
466 | 23 | std::string val; |
467 | 23 | switch (txn->get(key, &val)) { |
468 | 20 | case TxnErrorCode::TXN_OK: |
469 | 20 | return 0; |
470 | 3 | case TxnErrorCode::TXN_KEY_NOT_FOUND: |
471 | 3 | return 1; |
472 | 0 | default: |
473 | 0 | return -1; |
474 | 23 | } |
475 | 23 | } |
476 | | |
477 | | SharedStorageIndex::SharedStorageIndex(std::shared_ptr<TxnKv> txn_kv, |
478 | | std::optional<std::vector<InstanceInfoPB>> instances) |
479 | 73 | : txn_kv_(std::move(txn_kv)), preloaded_instances_(std::move(instances)) {} |
480 | | |
481 | | std::optional<SharedStorageIndex::StorageLocation> SharedStorageIndex::s3_location( |
482 | 0 | const ObjectStoreInfoPB& obj_info) { |
483 | | // Missing connection info must not make unrelated resources match. |
484 | 0 | if (obj_info.endpoint().empty() || obj_info.bucket().empty()) { |
485 | 0 | return std::nullopt; |
486 | 0 | } |
487 | 0 | return StorageLocation {"s3", obj_info.endpoint(), obj_info.bucket(), obj_info.prefix()}; |
488 | 0 | } |
489 | | |
490 | | std::optional<SharedStorageIndex::StorageLocation> SharedStorageIndex::hdfs_location( |
491 | 0 | const HdfsVaultInfo& hdfs_info) { |
492 | 0 | if (hdfs_info.build_conf().fs_name().empty()) { |
493 | 0 | return std::nullopt; |
494 | 0 | } |
495 | 0 | return StorageLocation {"hdfs", hdfs_info.build_conf().fs_name(), "", hdfs_info.prefix()}; |
496 | 0 | } |
497 | | |
498 | 0 | void SharedStorageIndex::ensure_loaded() { |
499 | 0 | std::call_once(load_once_, [this] { |
500 | 0 | load_status_ = load_all_instances(); |
501 | 0 | if (load_status_ != 0) { |
502 | | // Never expose a partially built index. |
503 | 0 | instance_resource_locations_.clear(); |
504 | 0 | location_to_owners_.clear(); |
505 | 0 | } |
506 | 0 | }); |
507 | 0 | } |
508 | | |
509 | 0 | int SharedStorageIndex::load_all_instances() { |
510 | 0 | std::vector<InstanceInfoPB> instances; |
511 | 0 | if (preloaded_instances_.has_value()) { |
512 | 0 | instances = std::move(*preloaded_instances_); |
513 | 0 | preloaded_instances_.reset(); |
514 | 0 | } else if (get_all_instances(txn_kv_.get(), instances) != 0) { |
515 | 0 | return -1; |
516 | 0 | } |
517 | | // Load all instances, even those not scheduled for checking: decoupled |
518 | | // instances can still share storage with the checked one. |
519 | 0 | for (const auto& instance : instances) { |
520 | 0 | if (load_instance(instance) != 0) { |
521 | 0 | return -1; |
522 | 0 | } |
523 | 0 | } |
524 | 0 | return 0; |
525 | 0 | } |
526 | | |
527 | 0 | int SharedStorageIndex::load_instance(const InstanceInfoPB& instance) { |
528 | 0 | const std::string& instance_id = instance.instance_id(); |
529 | | // Register the instance even if it has no resources, so that it can be told |
530 | | // apart from an unknown instance. |
531 | 0 | instance_resource_locations_[instance_id]; |
532 | | // Locations of the resources in `resource_ids` are filled by the vault scan below. |
533 | 0 | for (const auto& resource_id : instance.resource_ids()) { |
534 | 0 | add_resource(instance_id, resource_id, std::nullopt); |
535 | 0 | } |
536 | 0 | for (const auto& obj_info : instance.obj_info()) { |
537 | 0 | add_resource(instance_id, obj_info.id(), s3_location(obj_info)); |
538 | 0 | } |
539 | 0 | if (instance.resource_ids().empty()) { |
540 | 0 | return 0; |
541 | 0 | } |
542 | 0 | return load_storage_vaults(instance_id); |
543 | 0 | } |
544 | | |
545 | 0 | int SharedStorageIndex::load_storage_vaults(const std::string& instance_id) { |
546 | 0 | std::unique_ptr<Transaction> txn; |
547 | 0 | if (txn_kv_->create_txn(&txn) != TxnErrorCode::TXN_OK) { |
548 | 0 | return -1; |
549 | 0 | } |
550 | 0 | auto iter = txn->full_range_get(storage_vault_key({instance_id, ""}), |
551 | 0 | storage_vault_key({instance_id, "\xff"})); |
552 | 0 | for (auto entry = iter->next(); entry.has_value(); entry = iter->next()) { |
553 | 0 | StorageVaultPB vault; |
554 | 0 | if (!vault.ParseFromArray(entry->second.data(), entry->second.size())) { |
555 | 0 | LOG_WARNING("malformed storage vault").tag("instance_id", instance_id); |
556 | 0 | return -1; |
557 | 0 | } |
558 | 0 | std::optional<StorageLocation> location; |
559 | 0 | if (vault.has_obj_info()) { |
560 | 0 | location = s3_location(vault.obj_info()); |
561 | 0 | } else if (vault.has_hdfs_info()) { |
562 | 0 | location = hdfs_location(vault.hdfs_info()); |
563 | 0 | } |
564 | 0 | add_resource(instance_id, vault.id(), std::move(location)); |
565 | 0 | } |
566 | 0 | return iter->is_valid() ? 0 : -1; |
567 | 0 | } |
568 | | |
569 | | void SharedStorageIndex::add_resource(const std::string& instance_id, |
570 | | const std::string& resource_id, |
571 | 0 | std::optional<StorageLocation> location) { |
572 | 0 | auto& resource_location = instance_resource_locations_[instance_id][resource_id]; |
573 | 0 | if (!location.has_value()) { |
574 | 0 | return; |
575 | 0 | } |
576 | 0 | if (!resource_location.has_value()) { |
577 | 0 | resource_location = location; |
578 | 0 | } |
579 | 0 | location_to_owners_[std::move(*location)].push_back({instance_id, resource_id}); |
580 | 0 | } |
581 | | |
582 | | int SharedStorageIndex::get_shared_resource_owners(std::string_view instance_id, |
583 | 0 | SharedResourceOwners* owners) { |
584 | 0 | ensure_loaded(); |
585 | 0 | owners->clear(); |
586 | 0 | if (load_status_ != 0) { |
587 | 0 | return -1; |
588 | 0 | } |
589 | 0 | auto instance = instance_resource_locations_.find(std::string(instance_id)); |
590 | 0 | if (instance == instance_resource_locations_.end()) { |
591 | 0 | LOG_WARNING("instance not found while checking shared resources") |
592 | 0 | .tag("instance_id", instance_id); |
593 | 0 | return -1; |
594 | 0 | } |
595 | 0 | for (const auto& [resource_id, location] : instance->second) { |
596 | 0 | if (location.has_value()) { |
597 | 0 | (*owners)[resource_id] = location_to_owners_.at(*location); |
598 | 0 | } else { |
599 | | // Without a known location, the resource is only owned by its own instance. |
600 | 0 | (*owners)[resource_id] = {{instance->first, resource_id}}; |
601 | 0 | } |
602 | 0 | } |
603 | 0 | return 0; |
604 | 0 | } |
605 | | |
606 | | InstanceChecker::InstanceChecker(std::shared_ptr<TxnKv> txn_kv, const std::string& instance_id, |
607 | | std::shared_ptr<ResourceManager> resource_mgr, |
608 | | std::shared_ptr<SharedStorageIndex> shared_storage_index) |
609 | 100 | : txn_kv_(txn_kv), |
610 | 100 | instance_id_(instance_id), |
611 | 100 | resource_mgr_(std::move(resource_mgr)), |
612 | 100 | shared_storage_index_(std::move(shared_storage_index)) { |
613 | 100 | snapshot_manager_ = create_snapshot_manager(txn_kv); |
614 | 100 | if (!shared_storage_index_) { |
615 | 70 | shared_storage_index_ = std::make_shared<SharedStorageIndex>(txn_kv); |
616 | 70 | } |
617 | 100 | if (!resource_mgr_) { |
618 | 36 | resource_mgr_ = std::make_shared<ResourceManager>(std::move(txn_kv)); |
619 | 36 | resource_mgr_->init(); |
620 | 36 | } |
621 | 100 | } |
622 | | |
623 | 100 | int InstanceChecker::init(const InstanceInfoPB& instance) { |
624 | 100 | resource_mgr_->refresh_instance(instance_id_, instance); |
625 | | |
626 | 100 | int ret = init_obj_store_accessors(instance); |
627 | 100 | if (ret != 0) { |
628 | 0 | return ret; |
629 | 0 | } |
630 | | |
631 | 100 | return init_storage_vault_accessors(instance); |
632 | 100 | } |
633 | | |
634 | 100 | int InstanceChecker::init_obj_store_accessors(const InstanceInfoPB& instance) { |
635 | 100 | for (const auto& obj_info : instance.obj_info()) { |
636 | 100 | #ifdef UNIT_TEST |
637 | 100 | auto accessor = std::make_shared<MockAccessor>(); |
638 | | #else |
639 | | auto s3_conf = S3Conf::from_obj_store_info(obj_info); |
640 | | if (!s3_conf) { |
641 | | LOG(WARNING) << "failed to init object accessor, instance_id=" << instance_id_; |
642 | | return -1; |
643 | | } |
644 | | |
645 | | std::shared_ptr<S3Accessor> accessor; |
646 | | int ret = S3Accessor::create(std::move(*s3_conf), &accessor); |
647 | | if (ret != 0) { |
648 | | LOG(WARNING) << "failed to init object accessor. instance_id=" << instance_id_ |
649 | | << " resource_id=" << obj_info.id(); |
650 | | return ret; |
651 | | } |
652 | | #endif |
653 | | |
654 | 100 | accessor_map_.emplace(obj_info.id(), std::move(accessor)); |
655 | 100 | } |
656 | | |
657 | 100 | return 0; |
658 | 100 | } |
659 | | |
660 | 100 | int InstanceChecker::init_storage_vault_accessors(const InstanceInfoPB& instance) { |
661 | 100 | if (instance.resource_ids().empty()) { |
662 | 100 | return 0; |
663 | 100 | } |
664 | | |
665 | 0 | FullRangeGetOptions opts(txn_kv_); |
666 | 0 | opts.prefetch = true; |
667 | 0 | auto it = txn_kv_->full_range_get(storage_vault_key({instance_id_, ""}), |
668 | 0 | storage_vault_key({instance_id_, "\xff"}), std::move(opts)); |
669 | |
|
670 | 0 | for (auto kv = it->next(); kv.has_value(); kv = it->next()) { |
671 | 0 | auto [k, v] = *kv; |
672 | 0 | StorageVaultPB vault; |
673 | 0 | if (!vault.ParseFromArray(v.data(), v.size())) { |
674 | 0 | LOG(WARNING) << "malformed storage vault, unable to deserialize key=" << hex(k); |
675 | 0 | return -1; |
676 | 0 | } |
677 | 0 | TEST_SYNC_POINT_CALLBACK("InstanceRecycler::init_storage_vault_accessors.mock_vault", |
678 | 0 | &accessor_map_, &vault); |
679 | 0 | if (vault.has_hdfs_info()) { |
680 | 0 | #ifdef ENABLE_HDFS_STORAGE_VAULT |
681 | 0 | auto accessor = std::make_shared<HdfsAccessor>(vault.hdfs_info()); |
682 | 0 | int ret = accessor->init(); |
683 | 0 | if (ret != 0) { |
684 | 0 | LOG(WARNING) << "failed to init hdfs accessor. instance_id=" << instance_id_ |
685 | 0 | << " resource_id=" << vault.id() << " name=" << vault.name(); |
686 | 0 | return ret; |
687 | 0 | } |
688 | | |
689 | 0 | accessor_map_.emplace(vault.id(), std::move(accessor)); |
690 | | #else |
691 | | LOG(ERROR) << "HDFS is disabled (via the ENABLE_HDFS_STORAGE_VAULT build option), " |
692 | | << "but HDFS storage vaults were detected"; |
693 | | #endif |
694 | 0 | } else if (vault.has_obj_info()) { |
695 | 0 | #ifdef UNIT_TEST |
696 | 0 | auto accessor = std::make_shared<MockAccessor>(); |
697 | | #else |
698 | | auto s3_conf = S3Conf::from_obj_store_info(vault.obj_info()); |
699 | | if (!s3_conf) { |
700 | | LOG(WARNING) << "failed to init object accessor, instance_id=" << instance_id_; |
701 | | return -1; |
702 | | } |
703 | | |
704 | | std::shared_ptr<S3Accessor> accessor; |
705 | | int ret = S3Accessor::create(std::move(*s3_conf), &accessor); |
706 | | if (ret != 0) { |
707 | | LOG(WARNING) << "failed to init s3 accessor. instance_id=" << instance_id_ |
708 | | << " resource_id=" << vault.id() << " name=" << vault.name(); |
709 | | return ret; |
710 | | } |
711 | | #endif |
712 | |
|
713 | 0 | accessor_map_.emplace(vault.id(), std::move(accessor)); |
714 | 0 | } |
715 | 0 | } |
716 | | |
717 | 0 | if (!it->is_valid()) { |
718 | 0 | LOG_WARNING("failed to get storage vault kv"); |
719 | 0 | return -1; |
720 | 0 | } |
721 | 0 | return 0; |
722 | 0 | } |
723 | | |
724 | 16 | int InstanceChecker::do_check() { |
725 | 16 | TEST_SYNC_POINT("InstanceChecker.do_check"); |
726 | 16 | LOG(INFO) << "begin to check instance objects instance_id=" << instance_id_; |
727 | 16 | int check_ret = 0; |
728 | 16 | long num_scanned = 0; |
729 | 16 | long num_scanned_with_segment = 0; |
730 | 16 | long num_rowset_loss = 0; |
731 | 16 | long instance_volume = 0; |
732 | 16 | using namespace std::chrono; |
733 | 16 | auto start_time = steady_clock::now(); |
734 | 16 | DORIS_CLOUD_DEFER { |
735 | 16 | auto cost = duration<float>(steady_clock::now() - start_time).count(); |
736 | 16 | LOG(INFO) << "check instance objects finished, cost=" << cost |
737 | 16 | << "s. instance_id=" << instance_id_ << " num_scanned=" << num_scanned |
738 | 16 | << " num_scanned_with_segment=" << num_scanned_with_segment |
739 | 16 | << " num_rowset_loss=" << num_rowset_loss |
740 | 16 | << " instance_volume=" << instance_volume; |
741 | 16 | g_bvar_checker_num_scanned.put(instance_id_, num_scanned); |
742 | 16 | g_bvar_checker_num_scanned_with_segment.put(instance_id_, num_scanned_with_segment); |
743 | 16 | g_bvar_checker_num_check_failed.put(instance_id_, num_rowset_loss); |
744 | 16 | g_bvar_checker_check_cost_s.put(instance_id_, static_cast<long>(cost)); |
745 | | // FIXME(plat1ko): What if some list operation failed? |
746 | 16 | g_bvar_checker_instance_volume.put(instance_id_, instance_volume); |
747 | 16 | }; |
748 | | |
749 | 16 | struct TabletFiles { |
750 | 16 | int64_t tablet_id {0}; |
751 | 16 | std::string resource_id; |
752 | 16 | std::unordered_set<std::string> files; |
753 | 16 | }; |
754 | 16 | TabletFiles tablet_files_cache; |
755 | | |
756 | 4.05k | auto check_rowset_objects = [&, this](doris::RowsetMetaCloudPB& rs_meta, std::string_view key) { |
757 | 4.05k | if (rs_meta.num_segments() == 0) { |
758 | 0 | return; |
759 | 0 | } |
760 | | |
761 | 4.05k | bool data_loss = false; |
762 | 4.05k | bool segment_file_loss = false; |
763 | 4.05k | bool index_file_loss = false; |
764 | | |
765 | 4.05k | DORIS_CLOUD_DEFER { |
766 | 4.05k | if (data_loss) { |
767 | 30 | LOG(INFO) << "segment file is" << (segment_file_loss ? "" : " not") << " loss, " |
768 | 30 | << "index file is" << (index_file_loss ? "" : " not") << " loss, " |
769 | 30 | << "rowset.tablet_id = " << rs_meta.tablet_id(); |
770 | 30 | num_rowset_loss++; |
771 | 30 | } |
772 | 4.05k | }; |
773 | | |
774 | 4.05k | ++num_scanned_with_segment; |
775 | 4.05k | if (tablet_files_cache.tablet_id != rs_meta.tablet_id() || |
776 | 4.05k | tablet_files_cache.resource_id != rs_meta.resource_id()) { |
777 | 454 | long tablet_volume = 0; |
778 | | // Clear cache |
779 | 454 | tablet_files_cache.tablet_id = 0; |
780 | 454 | tablet_files_cache.files.clear(); |
781 | | // Get all file paths under this tablet directory |
782 | 454 | auto find_it = accessor_map_.find(rs_meta.resource_id()); |
783 | 454 | if (find_it == accessor_map_.end()) { |
784 | 0 | LOG_WARNING("resource id not found in accessor map") |
785 | 0 | .tag("resource_id", rs_meta.resource_id()) |
786 | 0 | .tag("tablet_id", rs_meta.tablet_id()) |
787 | 0 | .tag("rowset_id", rs_meta.rowset_id_v2()); |
788 | 0 | check_ret = -1; |
789 | 0 | return; |
790 | 0 | } |
791 | | |
792 | 454 | std::unique_ptr<ListIterator> list_iter; |
793 | 454 | int ret = find_it->second->list_directory(tablet_path_prefix(rs_meta.tablet_id()), |
794 | 454 | &list_iter); |
795 | 454 | if (ret != 0) { // No need to log, because S3Accessor has logged this error |
796 | 0 | check_ret = -1; |
797 | 0 | return; |
798 | 0 | } |
799 | | |
800 | 18.5k | for (auto file = list_iter->next(); file.has_value(); file = list_iter->next()) { |
801 | 18.0k | tablet_files_cache.files.insert(std::move(file->path)); |
802 | 18.0k | tablet_volume += file->size; |
803 | 18.0k | } |
804 | 454 | if (!list_iter->is_valid()) { |
805 | 0 | check_ret = -1; |
806 | 0 | return; |
807 | 0 | } |
808 | 454 | tablet_files_cache.tablet_id = rs_meta.tablet_id(); |
809 | 454 | tablet_files_cache.resource_id = rs_meta.resource_id(); |
810 | 454 | instance_volume += tablet_volume; |
811 | 454 | } |
812 | | |
813 | 16.1k | for (int i = 0; i < rs_meta.num_segments(); ++i) { |
814 | 12.0k | auto path = segment_path(rs_meta.tablet_id(), rs_meta.rowset_id_v2(), i); |
815 | | |
816 | | // Skip check if segment is already packed into a larger file |
817 | 12.0k | const auto& index_map = rs_meta.packed_slice_locations(); |
818 | 12.0k | if (index_map.find(path) != index_map.end()) { |
819 | 0 | continue; |
820 | 0 | } |
821 | | |
822 | 12.0k | if (tablet_files_cache.files.contains(path)) { |
823 | 12.0k | continue; |
824 | 12.0k | } |
825 | | |
826 | 11 | if (1 == key_exist(txn_kv_.get(), key)) { |
827 | | // Rowset has been deleted instead of data loss |
828 | 0 | break; |
829 | 0 | } |
830 | 11 | data_loss = true; |
831 | 11 | segment_file_loss = true; |
832 | 11 | TEST_SYNC_POINT_CALLBACK("InstanceChecker.do_check1", &path); |
833 | 11 | LOG(WARNING) << "object not exist, path=" << path |
834 | 11 | << ", rs_meta=" << rs_meta.ShortDebugString() << " key=" << hex(key); |
835 | 11 | } |
836 | | |
837 | 4.05k | std::unique_ptr<Transaction> txn; |
838 | 4.05k | TxnErrorCode err = txn_kv_->create_txn(&txn); |
839 | 4.05k | if (err != TxnErrorCode::TXN_OK) { |
840 | 0 | LOG(WARNING) << "failed to init txn, err=" << err; |
841 | 0 | check_ret = -1; |
842 | 0 | return; |
843 | 0 | } |
844 | | |
845 | 4.05k | TabletIndexPB tablet_index; |
846 | 4.05k | CloneChainReader reader(instance_id_, resource_mgr_.get()); |
847 | 4.05k | int index_ret = |
848 | 4.05k | get_tablet_idx(txn_kv_.get(), instance_id_, rs_meta.tablet_id(), tablet_index); |
849 | 4.05k | if (index_ret == 1) { |
850 | 0 | err = reader.get_tablet_index(txn.get(), rs_meta.tablet_id(), &tablet_index); |
851 | 0 | } |
852 | 4.05k | if (index_ret < 0 || err != TxnErrorCode::TXN_OK) { |
853 | 0 | LOG(WARNING) << "failed to get tablet index, tablet_id= " << rs_meta.tablet_id(); |
854 | 0 | check_ret = -1; |
855 | 0 | return; |
856 | 0 | } |
857 | | |
858 | 4.05k | auto tablet_schema_key = |
859 | 4.05k | meta_schema_key({instance_id_, tablet_index.index_id(), rs_meta.schema_version()}); |
860 | 4.05k | ValueBuf tablet_schema_val; |
861 | 4.05k | err = cloud::blob_get(txn.get(), tablet_schema_key, &tablet_schema_val); |
862 | 4.05k | auto* schema = rs_meta.mutable_tablet_schema(); |
863 | 4.05k | if (err == TxnErrorCode::TXN_KEY_NOT_FOUND) { |
864 | | // Clone/rollback rowsets may reference a schema that only exists in |
865 | | // versioned metadata, possibly in an ancestor of the current instance. |
866 | 2.00k | err = reader.get_tablet_schema(txn.get(), tablet_index.index_id(), |
867 | 2.00k | rs_meta.schema_version(), schema); |
868 | 2.05k | } else if (err == TxnErrorCode::TXN_OK && !parse_schema_value(tablet_schema_val, schema)) { |
869 | 0 | check_ret = -1; |
870 | 0 | LOG(WARNING) << "malformed schema value, key=" << hex(tablet_schema_key); |
871 | 0 | return; |
872 | 0 | } |
873 | 4.05k | if (err != TxnErrorCode::TXN_OK) { |
874 | 2.00k | check_ret = -1; |
875 | 2.00k | LOG(WARNING) << "failed to get schema, err=" << err; |
876 | 2.00k | return; |
877 | 2.00k | } |
878 | | |
879 | 2.05k | std::vector<std::pair<int64_t, std::string>> index_ids; |
880 | 2.05k | for (const auto& i : rs_meta.tablet_schema().index()) { |
881 | 2.05k | if (i.has_index_type() && i.index_type() == IndexType::INVERTED) { |
882 | 2.05k | index_ids.emplace_back(i.index_id(), i.index_suffix_name()); |
883 | 2.05k | } |
884 | 2.05k | } |
885 | 2.05k | if (!index_ids.empty()) { |
886 | 2.05k | const auto& index_map = rs_meta.packed_slice_locations(); |
887 | 2.05k | const auto index_format = |
888 | 2.05k | rs_meta.has_inverted_index_storage_format() |
889 | 2.05k | ? rs_meta.inverted_index_storage_format() |
890 | 2.05k | : rs_meta.tablet_schema().inverted_index_storage_format(); |
891 | 8.10k | for (int i = 0; i < rs_meta.num_segments(); ++i) { |
892 | 6.05k | std::vector<std::string> index_path_v; |
893 | 6.05k | if (index_format == InvertedIndexStorageFormatPB::V1) { |
894 | 6.01k | for (const auto& index_id : index_ids) { |
895 | 6.01k | LOG(INFO) << "check inverted index, tablet_id=" << rs_meta.tablet_id() |
896 | 6.01k | << " rowset_id=" << rs_meta.rowset_id_v2() << " segment_id=" << i |
897 | 6.01k | << " index_id=" << index_id.first |
898 | 6.01k | << " index_suffix_name=" << index_id.second; |
899 | 6.01k | index_path_v.emplace_back( |
900 | 6.01k | inverted_index_path_v1(rs_meta.tablet_id(), rs_meta.rowset_id_v2(), |
901 | 6.01k | i, index_id.first, index_id.second)); |
902 | 6.01k | } |
903 | 6.01k | } else { |
904 | 40 | index_path_v.emplace_back( |
905 | 40 | inverted_index_path_v2(rs_meta.tablet_id(), rs_meta.rowset_id_v2(), i)); |
906 | 40 | } |
907 | | |
908 | 6.05k | if (std::ranges::all_of(index_path_v, [&](const auto& idx_file_path) { |
909 | | // Skip check if inverted index file is already packed into a larger file |
910 | 6.05k | if (index_map.find(idx_file_path) != index_map.end()) { |
911 | 0 | return true; |
912 | 0 | } |
913 | 6.05k | if (!tablet_files_cache.files.contains(idx_file_path)) { |
914 | 23 | LOG(INFO) << "loss index file: " << idx_file_path; |
915 | 23 | return false; |
916 | 23 | } |
917 | 6.03k | return true; |
918 | 6.05k | })) { |
919 | 6.03k | continue; |
920 | 6.03k | } |
921 | 23 | index_file_loss = true; |
922 | 23 | data_loss = true; |
923 | 23 | } |
924 | 2.05k | } |
925 | 2.05k | }; |
926 | | |
927 | | // scan visible rowsets |
928 | 16 | auto start_key = meta_rowset_key({instance_id_, 0, 0}); |
929 | 16 | auto end_key = meta_rowset_key({instance_id_, INT64_MAX, 0}); |
930 | | |
931 | 16 | std::unique_ptr<RangeGetIterator> it; |
932 | 32 | while (it == nullptr /* may be not init */ || (it->more() && !stopped())) { |
933 | 16 | std::unique_ptr<Transaction> txn; |
934 | 16 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
935 | 16 | if (err != TxnErrorCode::TXN_OK) { |
936 | 0 | LOG(WARNING) << "failed to init txn, err=" << err; |
937 | 0 | return -1; |
938 | 0 | } |
939 | | |
940 | 16 | err = txn->get(start_key, end_key, &it); |
941 | 16 | if (err != TxnErrorCode::TXN_OK) { |
942 | 0 | LOG(WARNING) << "internal error, failed to get rowset meta, err=" << err; |
943 | 0 | return -1; |
944 | 0 | } |
945 | 16 | num_scanned += it->size(); |
946 | | |
947 | 4.07k | while (it->has_next() && !stopped()) { |
948 | 4.05k | auto [k, v] = it->next(); |
949 | 4.05k | if (!it->has_next()) { |
950 | 6 | start_key = k; |
951 | 6 | } |
952 | | |
953 | 4.05k | doris::RowsetMetaCloudPB rs_meta; |
954 | 4.05k | if (!rs_meta.ParseFromArray(v.data(), v.size())) { |
955 | 0 | ++num_rowset_loss; |
956 | 0 | LOG(WARNING) << "malformed rowset meta. key=" << hex(k) << " val=" << hex(v); |
957 | 0 | continue; |
958 | 0 | } |
959 | 4.05k | check_rowset_objects(rs_meta, k); |
960 | 4.05k | } |
961 | 16 | start_key.push_back('\x00'); // Update to next smallest key for iteration |
962 | 16 | } |
963 | | |
964 | 16 | return num_rowset_loss > 0 ? 1 : check_ret; |
965 | 16 | } |
966 | | |
967 | 34 | int InstanceChecker::get_bucket_lifecycle(int64_t* lifecycle_days) { |
968 | | // If there are multiple buckets, return the minimum lifecycle. |
969 | 34 | int64_t min_lifecycle_days = INT64_MAX; |
970 | 34 | int64_t tmp_lifecycle_days = 0; |
971 | 34 | for (const auto& [id, accessor] : accessor_map_) { |
972 | 34 | if (accessor->type() != AccessorType::S3) { |
973 | 34 | continue; |
974 | 34 | } |
975 | | |
976 | 0 | auto* s3_accessor = static_cast<S3Accessor*>(accessor.get()); |
977 | |
|
978 | 0 | if (s3_accessor->check_versioning() != 0) { |
979 | 0 | return -1; |
980 | 0 | } |
981 | | |
982 | 0 | if (s3_accessor->get_lifecycle(&tmp_lifecycle_days) != 0) { |
983 | 0 | return -1; |
984 | 0 | } |
985 | | |
986 | 0 | if (tmp_lifecycle_days < min_lifecycle_days) { |
987 | 0 | min_lifecycle_days = tmp_lifecycle_days; |
988 | 0 | } |
989 | 0 | } |
990 | 34 | *lifecycle_days = min_lifecycle_days; |
991 | 34 | return 0; |
992 | 34 | } |
993 | | |
994 | 5 | int InstanceChecker::do_inverted_check() { |
995 | 5 | if (accessor_map_.size() > 1) { |
996 | 0 | LOG(INFO) << "currently not support inverted check for multi accessor. instance_id=" |
997 | 0 | << instance_id_; |
998 | 0 | return 0; |
999 | 0 | } |
1000 | | |
1001 | 5 | LOG(INFO) << "begin to inverted check objects instance_id=" << instance_id_; |
1002 | 5 | int check_ret = 0; |
1003 | 5 | long num_scanned = 0; |
1004 | 5 | long num_file_leak = 0; |
1005 | 5 | using namespace std::chrono; |
1006 | 5 | auto start_time = steady_clock::now(); |
1007 | 5 | DORIS_CLOUD_DEFER { |
1008 | 5 | g_bvar_inverted_checker_num_scanned.put(instance_id_, num_scanned); |
1009 | 5 | g_bvar_inverted_checker_num_check_failed.put(instance_id_, num_file_leak); |
1010 | 5 | auto cost = duration<float>(steady_clock::now() - start_time).count(); |
1011 | 5 | LOG(INFO) << "inverted check instance objects finished, cost=" << cost |
1012 | 5 | << "s. instance_id=" << instance_id_ << " num_scanned=" << num_scanned |
1013 | 5 | << " num_file_leak=" << num_file_leak; |
1014 | 5 | }; |
1015 | | |
1016 | 5 | struct TabletRowsets { |
1017 | 5 | int64_t tablet_id {0}; |
1018 | 5 | std::unordered_set<std::string> rowset_ids; |
1019 | 5 | }; |
1020 | 5 | TabletRowsets tablet_rowsets_cache; |
1021 | | |
1022 | 5 | RowsetIndexesFormatV1 rowset_index_cache_v1; |
1023 | 5 | RowsetIndexesFormatV2 rowset_index_cache_v2; |
1024 | | |
1025 | | // Return 0 if check success, return 1 if file is garbage data, negative if error occurred |
1026 | 108 | auto check_segment_file = [&](const std::string& obj_key) { |
1027 | 108 | std::vector<std::string> str; |
1028 | 108 | butil::SplitString(obj_key, '/', &str); |
1029 | | // data/{tablet_id}/{rowset_id}_{seg_num}.dat |
1030 | 108 | if (str.size() < 3) { |
1031 | | // clang-format off |
1032 | 0 | LOG(WARNING) << "split obj_key error, str.size() should be less than 3," |
1033 | 0 | << " value = " << str.size(); |
1034 | | // clang-format on |
1035 | 0 | return -1; |
1036 | 0 | } |
1037 | | |
1038 | 108 | int64_t tablet_id = atol(str[1].c_str()); |
1039 | 108 | if (tablet_id <= 0) { |
1040 | 0 | LOG(WARNING) << "failed to parse tablet_id, key=" << obj_key; |
1041 | 0 | return -1; |
1042 | 0 | } |
1043 | | |
1044 | 108 | if (!str[2].ends_with(".dat")) { |
1045 | | // skip check not segment file |
1046 | 54 | return 0; |
1047 | 54 | } |
1048 | | |
1049 | 54 | std::string rowset_id; |
1050 | 54 | if (auto pos = str.back().find('_'); pos != std::string::npos) { |
1051 | 54 | rowset_id = str.back().substr(0, pos); |
1052 | 54 | } else { |
1053 | 0 | LOG(WARNING) << "failed to parse rowset_id, key=" << obj_key; |
1054 | 0 | return -1; |
1055 | 0 | } |
1056 | | |
1057 | 54 | if (tablet_rowsets_cache.tablet_id == tablet_id) { |
1058 | 7 | if (tablet_rowsets_cache.rowset_ids.contains(rowset_id)) { |
1059 | 2 | return 0; |
1060 | 5 | } else { |
1061 | 5 | LOG(WARNING) << "rowset not exists, key=" << obj_key; |
1062 | 5 | return -1; |
1063 | 5 | } |
1064 | 7 | } |
1065 | | // Get all rowset id of this tablet |
1066 | 47 | tablet_rowsets_cache.tablet_id = tablet_id; |
1067 | 47 | tablet_rowsets_cache.rowset_ids.clear(); |
1068 | 47 | std::unique_ptr<Transaction> txn; |
1069 | 47 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
1070 | 47 | if (err != TxnErrorCode::TXN_OK) { |
1071 | 0 | LOG(WARNING) << "failed to create txn"; |
1072 | 0 | return -1; |
1073 | 0 | } |
1074 | 47 | std::unique_ptr<RangeGetIterator> it; |
1075 | 47 | auto begin = meta_rowset_key({instance_id_, tablet_id, 0}); |
1076 | 47 | auto end = meta_rowset_key({instance_id_, tablet_id, INT64_MAX}); |
1077 | 84 | while (it == nullptr /* may be not init */ || (it->more() && !stopped())) { |
1078 | 47 | TxnErrorCode err = txn->get(begin, end, &it); |
1079 | 47 | if (err != TxnErrorCode::TXN_OK) { |
1080 | 0 | LOG(WARNING) << "failed to get rowset kv, err=" << err; |
1081 | 0 | return -1; |
1082 | 0 | } |
1083 | 47 | if (!it->has_next()) { |
1084 | 10 | break; |
1085 | 10 | } |
1086 | 37 | while (it->has_next()) { |
1087 | | // recycle corresponding resources |
1088 | 37 | auto [k, v] = it->next(); |
1089 | 37 | doris::RowsetMetaCloudPB rowset; |
1090 | 37 | if (!rowset.ParseFromArray(v.data(), v.size())) { |
1091 | 0 | LOG(WARNING) << "malformed rowset meta value, key=" << hex(k); |
1092 | 0 | return -1; |
1093 | 0 | } |
1094 | 37 | tablet_rowsets_cache.rowset_ids.insert(rowset.rowset_id_v2()); |
1095 | 37 | if (!it->has_next()) { |
1096 | 37 | begin = k; |
1097 | 37 | begin.push_back('\x00'); // Update to next smallest key for iteration |
1098 | 37 | break; |
1099 | 37 | } |
1100 | 37 | } |
1101 | 37 | } |
1102 | | |
1103 | 47 | if (!tablet_rowsets_cache.rowset_ids.contains(rowset_id)) { |
1104 | | // Garbage data leak |
1105 | 12 | LOG(WARNING) << "rowset should be recycled, key=" << obj_key; |
1106 | 12 | return 1; |
1107 | 12 | } |
1108 | | |
1109 | 35 | return 0; |
1110 | 47 | }; |
1111 | | |
1112 | 108 | auto check_inverted_index_file = [&](const std::string& obj_key) { |
1113 | 108 | std::vector<std::string> str; |
1114 | 108 | butil::SplitString(obj_key, '/', &str); |
1115 | | // format v1: data/{tablet_id}/{rowset_id}_{seg_num}_{idx_id}{idx_suffix}.idx |
1116 | | // format v2: data/{tablet_id}/{rowset_id}_{seg_num}.idx |
1117 | 108 | if (str.size() < 3) { |
1118 | | // clang-format off |
1119 | 0 | LOG(WARNING) << "split obj_key error, str.size() should be less than 3," |
1120 | 0 | << " value = " << str.size(); |
1121 | | // clang-format on |
1122 | 0 | return -1; |
1123 | 0 | } |
1124 | | |
1125 | 108 | int64_t tablet_id = atol(str[1].c_str()); |
1126 | 108 | if (tablet_id <= 0) { |
1127 | 0 | LOG(WARNING) << "failed to parse tablet_id, key=" << obj_key; |
1128 | 0 | return -1; |
1129 | 0 | } |
1130 | | |
1131 | | // v1: {rowset_id}_{seg_num}_{idx_id}{idx_suffix}.idx |
1132 | | // v2: {rowset_id}_{seg_num}.idx |
1133 | 108 | std::string rowset_info = str.back(); |
1134 | | |
1135 | 108 | if (!rowset_info.ends_with(".idx")) { |
1136 | 54 | return 0; // Not an index file |
1137 | 54 | } |
1138 | | |
1139 | 54 | InvertedIndexStorageFormatPB inverted_index_storage_format = |
1140 | 54 | std::count(rowset_info.begin(), rowset_info.end(), '_') > 1 |
1141 | 54 | ? InvertedIndexStorageFormatPB::V1 |
1142 | 54 | : InvertedIndexStorageFormatPB::V2; |
1143 | | |
1144 | 54 | size_t pos = rowset_info.find_last_of('_'); |
1145 | 54 | if (pos == std::string::npos || pos + 1 >= str.back().size() - 4) { |
1146 | 0 | LOG(WARNING) << "Invalid index_id format, key=" << obj_key; |
1147 | 0 | return -1; |
1148 | 0 | } |
1149 | 54 | if (inverted_index_storage_format == InvertedIndexStorageFormatPB::V1) { |
1150 | 14 | return check_inverted_index_file_storage_format_v1(tablet_id, obj_key, rowset_info, |
1151 | 14 | rowset_index_cache_v1); |
1152 | 40 | } else { |
1153 | 40 | return check_inverted_index_file_storage_format_v2(tablet_id, obj_key, rowset_info, |
1154 | 40 | rowset_index_cache_v2); |
1155 | 40 | } |
1156 | 54 | }; |
1157 | | // so we choose to skip here. |
1158 | 5 | TEST_SYNC_POINT_RETURN_WITH_VALUE("InstanceChecker::do_inverted_check", (int)0); |
1159 | | |
1160 | 3 | for (auto& [_, accessor] : accessor_map_) { |
1161 | 3 | std::unique_ptr<ListIterator> list_iter; |
1162 | 3 | int ret = accessor->list_directory("data", &list_iter); |
1163 | 3 | if (ret != 0) { |
1164 | 0 | return -1; |
1165 | 0 | } |
1166 | | |
1167 | 111 | for (auto file = list_iter->next(); file.has_value(); file = list_iter->next()) { |
1168 | 108 | const auto& path = file->path; |
1169 | 108 | if (path == "data/packed_file" || path.starts_with("data/packed_file/")) { |
1170 | 0 | continue; // packed_file has dedicated check logic |
1171 | 0 | } |
1172 | 108 | ++num_scanned; |
1173 | 108 | int ret = check_segment_file(path); |
1174 | 108 | if (ret != 0) { |
1175 | 17 | LOG(WARNING) << "failed to check segment file, uri=" << accessor->uri() |
1176 | 17 | << " path=" << path; |
1177 | 17 | if (ret == 1) { |
1178 | 12 | ++num_file_leak; |
1179 | 12 | } else { |
1180 | 5 | check_ret = -1; |
1181 | 5 | } |
1182 | 17 | } |
1183 | 108 | ret = check_inverted_index_file(path); |
1184 | 108 | if (ret != 0) { |
1185 | 13 | LOG(WARNING) << "failed to check index file, uri=" << accessor->uri() |
1186 | 13 | << " path=" << path; |
1187 | 13 | if (ret == 1) { |
1188 | 13 | ++num_file_leak; |
1189 | 13 | } else { |
1190 | 0 | check_ret = -1; |
1191 | 0 | } |
1192 | 13 | } |
1193 | 108 | } |
1194 | | |
1195 | 3 | if (!list_iter->is_valid()) { |
1196 | 0 | LOG(WARNING) << "failed to list data directory. uri=" << accessor->uri(); |
1197 | 0 | return -1; |
1198 | 0 | } |
1199 | 3 | } |
1200 | 3 | return num_file_leak > 0 ? 1 : check_ret; |
1201 | 3 | } |
1202 | | |
1203 | 3 | int InstanceChecker::traverse_mow_tablet(const std::function<int(int64_t, bool)>& check_func) { |
1204 | 3 | std::unique_ptr<RangeGetIterator> it; |
1205 | 3 | auto begin = meta_rowset_key({instance_id_, 0, 0}); |
1206 | 3 | auto end = meta_rowset_key({instance_id_, std::numeric_limits<int64_t>::max(), 0}); |
1207 | 43 | while (it == nullptr /* may be not init */ || (it->more() && !stopped())) { |
1208 | 43 | std::unique_ptr<Transaction> txn; |
1209 | 43 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
1210 | 43 | if (err != TxnErrorCode::TXN_OK) { |
1211 | 0 | LOG(WARNING) << "failed to create txn"; |
1212 | 0 | return -1; |
1213 | 0 | } |
1214 | 43 | err = txn->get(begin, end, &it, false, 1); |
1215 | 43 | if (err != TxnErrorCode::TXN_OK) { |
1216 | 0 | LOG(WARNING) << "failed to get rowset kv, err=" << err; |
1217 | 0 | return -1; |
1218 | 0 | } |
1219 | 43 | if (!it->has_next()) { |
1220 | 3 | break; |
1221 | 3 | } |
1222 | 80 | while (it->has_next() && !stopped()) { |
1223 | 40 | auto [k, v] = it->next(); |
1224 | 40 | std::string_view k1 = k; |
1225 | 40 | k1.remove_prefix(1); |
1226 | 40 | std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> out; |
1227 | 40 | decode_key(&k1, &out); |
1228 | | // 0x01 "meta" ${instance_id} "rowset" ${tablet_id} ${version} -> RowsetMetaCloudPB |
1229 | 40 | auto tablet_id = std::get<int64_t>(std::get<0>(out[3])); |
1230 | | |
1231 | 40 | if (!it->has_next()) { |
1232 | | // Update to next smallest key for iteration |
1233 | | // scan for next tablet in this instance |
1234 | 40 | begin = meta_rowset_key({instance_id_, tablet_id + 1, 0}); |
1235 | 40 | } |
1236 | | |
1237 | 40 | TabletMetaCloudPB tablet_meta; |
1238 | 40 | int ret = get_tablet_meta(txn_kv_.get(), instance_id_, tablet_id, tablet_meta); |
1239 | 40 | if (ret < 0) { |
1240 | 0 | LOG(WARNING) << fmt::format( |
1241 | 0 | "failed to get_tablet_meta in do_delete_bitmap_integrity_check(), " |
1242 | 0 | "instance_id={}, tablet_id={}", |
1243 | 0 | instance_id_, tablet_id); |
1244 | 0 | return ret; |
1245 | 0 | } |
1246 | | |
1247 | 40 | if (tablet_meta.enable_unique_key_merge_on_write()) { |
1248 | | // only check merge-on-write table |
1249 | 30 | bool has_sequence_col = tablet_meta.schema().has_sequence_col_idx() && |
1250 | 30 | tablet_meta.schema().sequence_col_idx() != -1; |
1251 | 30 | int ret = check_func(tablet_id, has_sequence_col); |
1252 | 30 | if (ret < 0) { |
1253 | | // return immediately when encounter unexpected error, |
1254 | | // otherwise, we continue to check the next tablet |
1255 | 0 | return ret; |
1256 | 0 | } |
1257 | 30 | } |
1258 | 40 | } |
1259 | 40 | } |
1260 | 3 | return 0; |
1261 | 3 | } |
1262 | | |
1263 | | int InstanceChecker::traverse_rowset_delete_bitmaps( |
1264 | | int64_t tablet_id, std::string rowset_id, |
1265 | 0 | const std::function<int(int64_t, std::string_view, int64_t, int64_t)>& callback) { |
1266 | 0 | std::unique_ptr<RangeGetIterator> it; |
1267 | 0 | auto begin = meta_delete_bitmap_key({instance_id_, tablet_id, rowset_id, 0, 0}); |
1268 | 0 | auto end = meta_delete_bitmap_key({instance_id_, tablet_id, rowset_id, |
1269 | 0 | std::numeric_limits<int64_t>::max(), |
1270 | 0 | std::numeric_limits<int64_t>::max()}); |
1271 | 0 | while (it == nullptr /* may be not init */ || (it->more() && !stopped())) { |
1272 | 0 | std::unique_ptr<Transaction> txn; |
1273 | 0 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
1274 | 0 | if (err != TxnErrorCode::TXN_OK) { |
1275 | 0 | LOG(WARNING) << "failed to create txn"; |
1276 | 0 | return -1; |
1277 | 0 | } |
1278 | 0 | err = txn->get(begin, end, &it); |
1279 | 0 | if (err != TxnErrorCode::TXN_OK) { |
1280 | 0 | LOG(WARNING) << "failed to get rowset kv, err=" << err; |
1281 | 0 | return -1; |
1282 | 0 | } |
1283 | 0 | if (!it->has_next()) { |
1284 | 0 | break; |
1285 | 0 | } |
1286 | 0 | while (it->has_next() && !stopped()) { |
1287 | 0 | auto [k, v] = it->next(); |
1288 | 0 | std::string_view k1 = k; |
1289 | 0 | k1.remove_prefix(1); |
1290 | 0 | std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> out; |
1291 | 0 | decode_key(&k1, &out); |
1292 | | // 0x01 "meta" ${instance_id} "delete_bitmap" ${tablet_id} ${rowset_id} ${version} ${segment_id} -> roaringbitmap |
1293 | 0 | auto version = std::get<std::int64_t>(std::get<0>(out[5])); |
1294 | 0 | auto segment_id = std::get<std::int64_t>(std::get<0>(out[6])); |
1295 | |
|
1296 | 0 | int ret = callback(tablet_id, rowset_id, version, segment_id); |
1297 | 0 | if (ret != 0) { |
1298 | 0 | return ret; |
1299 | 0 | } |
1300 | | |
1301 | 0 | if (!it->has_next()) { |
1302 | 0 | begin = k; |
1303 | 0 | begin.push_back('\x00'); // Update to next smallest key for iteration |
1304 | 0 | break; |
1305 | 0 | } |
1306 | 0 | } |
1307 | 0 | } |
1308 | | |
1309 | 0 | return 0; |
1310 | 0 | } |
1311 | | |
1312 | | int InstanceChecker::collect_tablet_rowsets( |
1313 | 53 | int64_t tablet_id, const std::function<void(const doris::RowsetMetaCloudPB&)>& collect_cb) { |
1314 | 53 | std::unique_ptr<Transaction> txn; |
1315 | 53 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
1316 | 53 | if (err != TxnErrorCode::TXN_OK) { |
1317 | 0 | LOG(WARNING) << "failed to create txn"; |
1318 | 0 | return -1; |
1319 | 0 | } |
1320 | 53 | std::unique_ptr<RangeGetIterator> it; |
1321 | 53 | auto begin = meta_rowset_key({instance_id_, tablet_id, 0}); |
1322 | 53 | auto end = meta_rowset_key({instance_id_, tablet_id + 1, 0}); |
1323 | | |
1324 | 53 | int64_t rowsets_num {0}; |
1325 | 103 | while (it == nullptr /* may be not init */ || (it->more() && !stopped())) { |
1326 | 53 | TxnErrorCode err = txn->get(begin, end, &it); |
1327 | 53 | if (err != TxnErrorCode::TXN_OK) { |
1328 | 0 | LOG(WARNING) << "failed to get rowset kv, err=" << err; |
1329 | 0 | return -1; |
1330 | 0 | } |
1331 | 53 | if (!it->has_next()) { |
1332 | 3 | break; |
1333 | 3 | } |
1334 | 394 | while (it->has_next() && !stopped()) { |
1335 | 394 | auto [k, v] = it->next(); |
1336 | 394 | doris::RowsetMetaCloudPB rowset; |
1337 | 394 | if (!rowset.ParseFromArray(v.data(), v.size())) { |
1338 | 0 | LOG(WARNING) << "malformed rowset meta value, key=" << hex(k); |
1339 | 0 | return -1; |
1340 | 0 | } |
1341 | | |
1342 | 394 | ++rowsets_num; |
1343 | 394 | collect_cb(rowset); |
1344 | | |
1345 | 394 | if (!it->has_next()) { |
1346 | 50 | begin = k; |
1347 | 50 | begin.push_back('\x00'); // Update to next smallest key for iteration |
1348 | 50 | break; |
1349 | 50 | } |
1350 | 394 | } |
1351 | 50 | } |
1352 | | |
1353 | 53 | LOG(INFO) << fmt::format( |
1354 | 53 | "[delete bitmap checker] successfully collect rowsets for instance_id={}, " |
1355 | 53 | "tablet_id={}, rowsets_num={}", |
1356 | 53 | instance_id_, tablet_id, rowsets_num); |
1357 | 53 | return 0; |
1358 | 53 | } |
1359 | | |
1360 | 5 | int InstanceChecker::do_delete_bitmap_inverted_check() { |
1361 | 5 | LOG(INFO) << fmt::format( |
1362 | 5 | "[delete bitmap checker] begin to do_delete_bitmap_inverted_check for instance_id={}", |
1363 | 5 | instance_id_); |
1364 | | |
1365 | | // number of delete bitmap keys being scanned |
1366 | 5 | int64_t total_delete_bitmap_keys {0}; |
1367 | | // number of delete bitmaps which belongs to non mow tablet |
1368 | 5 | int64_t abnormal_delete_bitmaps {0}; |
1369 | | // number of delete bitmaps which doesn't have corresponding rowset in MS |
1370 | 5 | int64_t leaked_delete_bitmaps {0}; |
1371 | | |
1372 | 5 | auto start_time = std::chrono::steady_clock::now(); |
1373 | 5 | DORIS_CLOUD_DEFER { |
1374 | 5 | g_bvar_inverted_checker_leaked_delete_bitmaps.put(instance_id_, leaked_delete_bitmaps); |
1375 | 5 | g_bvar_inverted_checker_abnormal_delete_bitmaps.put(instance_id_, abnormal_delete_bitmaps); |
1376 | 5 | g_bvar_inverted_checker_delete_bitmaps_scanned.put(instance_id_, total_delete_bitmap_keys); |
1377 | | |
1378 | 5 | auto cost = std::chrono::duration_cast<std::chrono::milliseconds>( |
1379 | 5 | std::chrono::steady_clock::now() - start_time) |
1380 | 5 | .count(); |
1381 | 5 | if (leaked_delete_bitmaps > 0 || abnormal_delete_bitmaps > 0) { |
1382 | 3 | LOG(WARNING) << fmt::format( |
1383 | 3 | "[delete bitmap check fails] delete bitmap inverted check for instance_id={}, " |
1384 | 3 | "cost={} ms, total_delete_bitmap_keys={}, leaked_delete_bitmaps={}, " |
1385 | 3 | "abnormal_delete_bitmaps={}", |
1386 | 3 | instance_id_, cost, total_delete_bitmap_keys, leaked_delete_bitmaps, |
1387 | 3 | abnormal_delete_bitmaps); |
1388 | 3 | } else { |
1389 | 2 | LOG(INFO) << fmt::format( |
1390 | 2 | "[delete bitmap checker] delete bitmap inverted check for instance_id={}, " |
1391 | 2 | "passed. cost={} ms, total_delete_bitmap_keys={}", |
1392 | 2 | instance_id_, cost, total_delete_bitmap_keys); |
1393 | 2 | } |
1394 | 5 | }; |
1395 | | |
1396 | 5 | struct TabletsRowsetsCache { |
1397 | 5 | int64_t tablet_id {-1}; |
1398 | 5 | bool enable_merge_on_write {false}; |
1399 | 5 | std::unordered_set<std::string> rowsets {}; |
1400 | 5 | std::unordered_set<std::string> pending_delete_bitmaps {}; |
1401 | 5 | } tablet_rowsets_cache {}; |
1402 | | |
1403 | 5 | std::unordered_map<int64_t, std::unordered_set<std::string>> unexpired_tmp_rowsets; |
1404 | 5 | if (int ret = collect_unexpired_job_tmp_rowsets(unexpired_tmp_rowsets); ret < 0) { |
1405 | 0 | return ret; |
1406 | 0 | } |
1407 | | |
1408 | 5 | std::unique_ptr<RangeGetIterator> it; |
1409 | 5 | auto begin = meta_delete_bitmap_key({instance_id_, 0, "", 0, 0}); |
1410 | 5 | auto end = |
1411 | 5 | meta_delete_bitmap_key({instance_id_, std::numeric_limits<int64_t>::max(), "", 0, 0}); |
1412 | 10 | while (it == nullptr /* may be not init */ || (it->more() && !stopped())) { |
1413 | 5 | std::unique_ptr<Transaction> txn; |
1414 | 5 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
1415 | 5 | if (err != TxnErrorCode::TXN_OK) { |
1416 | 0 | LOG(WARNING) << "failed to create txn"; |
1417 | 0 | return -1; |
1418 | 0 | } |
1419 | 5 | err = txn->get(begin, end, &it); |
1420 | 5 | if (err != TxnErrorCode::TXN_OK) { |
1421 | 0 | LOG(WARNING) << "failed to get rowset kv, err=" << err; |
1422 | 0 | return -1; |
1423 | 0 | } |
1424 | 5 | if (!it->has_next()) { |
1425 | 0 | break; |
1426 | 0 | } |
1427 | 388 | while (it->has_next() && !stopped()) { |
1428 | 383 | auto [k, v] = it->next(); |
1429 | 383 | std::string_view k1 = k; |
1430 | 383 | k1.remove_prefix(1); |
1431 | 383 | std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> out; |
1432 | 383 | decode_key(&k1, &out); |
1433 | | // 0x01 "meta" ${instance_id} "delete_bitmap" ${tablet_id} ${rowset_id} ${version} ${segment_id} -> roaringbitmap |
1434 | 383 | auto tablet_id = std::get<int64_t>(std::get<0>(out[3])); |
1435 | 383 | auto rowset_id = std::get<std::string>(std::get<0>(out[4])); |
1436 | 383 | auto version = std::get<std::int64_t>(std::get<0>(out[5])); |
1437 | 383 | auto segment_id = std::get<std::int64_t>(std::get<0>(out[6])); |
1438 | | |
1439 | 383 | ++total_delete_bitmap_keys; |
1440 | | |
1441 | 383 | if (!it->has_next()) { |
1442 | 5 | begin = k; |
1443 | 5 | begin.push_back('\x00'); // Update to next smallest key for iteration |
1444 | 5 | } |
1445 | | |
1446 | 383 | if (tablet_rowsets_cache.tablet_id == -1 || |
1447 | 383 | tablet_rowsets_cache.tablet_id != tablet_id) { |
1448 | 33 | if (tablet_rowsets_cache.tablet_id != -1) { |
1449 | 28 | unexpired_tmp_rowsets.erase(tablet_rowsets_cache.tablet_id); |
1450 | 28 | } |
1451 | 33 | TabletMetaCloudPB tablet_meta; |
1452 | 33 | int ret = get_tablet_meta(txn_kv_.get(), instance_id_, tablet_id, tablet_meta); |
1453 | 33 | if (ret < 0) { |
1454 | 0 | LOG(WARNING) << fmt::format( |
1455 | 0 | "[delete bitmap checker] failed to get_tablet_meta in " |
1456 | 0 | "do_delete_bitmap_inverted_check(), instance_id={}, tablet_id={}", |
1457 | 0 | instance_id_, tablet_id); |
1458 | 0 | return ret; |
1459 | 0 | } |
1460 | | |
1461 | 33 | tablet_rowsets_cache.tablet_id = tablet_id; |
1462 | 33 | tablet_rowsets_cache.enable_merge_on_write = |
1463 | 33 | tablet_meta.enable_unique_key_merge_on_write(); |
1464 | 33 | tablet_rowsets_cache.rowsets.clear(); |
1465 | 33 | tablet_rowsets_cache.pending_delete_bitmaps.clear(); |
1466 | | |
1467 | 33 | if (tablet_rowsets_cache.enable_merge_on_write) { |
1468 | | // only collect rowsets for merge-on-write tablet |
1469 | 23 | auto collect_cb = |
1470 | 199 | [&tablet_rowsets_cache](const doris::RowsetMetaCloudPB& rowset) { |
1471 | 199 | tablet_rowsets_cache.rowsets.insert(rowset.rowset_id_v2()); |
1472 | 199 | }; |
1473 | 23 | ret = collect_tablet_rowsets(tablet_id, collect_cb); |
1474 | 23 | if (ret < 0) { |
1475 | 0 | return ret; |
1476 | 0 | } |
1477 | | // get pending delete bitmaps |
1478 | 23 | ret = get_pending_delete_bitmap_keys( |
1479 | 23 | tablet_id, tablet_rowsets_cache.pending_delete_bitmaps); |
1480 | 23 | if (ret < 0) { |
1481 | 0 | return ret; |
1482 | 0 | } |
1483 | 23 | } |
1484 | 33 | } |
1485 | 383 | DCHECK_EQ(tablet_id, tablet_rowsets_cache.tablet_id); |
1486 | | |
1487 | 383 | if (!tablet_rowsets_cache.enable_merge_on_write) { |
1488 | | // clang-format off |
1489 | 40 | TEST_SYNC_POINT_CALLBACK( |
1490 | 40 | "InstanceChecker::do_delete_bitmap_inverted_check.get_abnormal_delete_bitmap", |
1491 | 40 | &tablet_id, &rowset_id, &version, &segment_id); |
1492 | | // clang-format on |
1493 | 40 | ++abnormal_delete_bitmaps; |
1494 | | // log an error and continue to check the next delete bitmap |
1495 | 40 | LOG(WARNING) << fmt::format( |
1496 | 40 | "[delete bitmap check fails] find a delete bitmap belongs to tablet " |
1497 | 40 | "which is not a merge-on-write table! instance_id={}, tablet_id={}, " |
1498 | 40 | "version={}, segment_id={}", |
1499 | 40 | instance_id_, tablet_id, version, segment_id); |
1500 | 40 | continue; |
1501 | 40 | } |
1502 | | |
1503 | 343 | bool belongs_to_unexpired_tmp_rowset = false; |
1504 | 343 | auto tmp_rowsets_it = unexpired_tmp_rowsets.find(tablet_id); |
1505 | 343 | if (tmp_rowsets_it != unexpired_tmp_rowsets.end()) { |
1506 | 1 | belongs_to_unexpired_tmp_rowset = tmp_rowsets_it->second.contains(rowset_id); |
1507 | 1 | } |
1508 | | |
1509 | 343 | if (!tablet_rowsets_cache.rowsets.contains(rowset_id) && |
1510 | 343 | !tablet_rowsets_cache.pending_delete_bitmaps.contains(std::string(k)) && |
1511 | 343 | !belongs_to_unexpired_tmp_rowset) { |
1512 | 112 | TEST_SYNC_POINT_CALLBACK( |
1513 | 112 | "InstanceChecker::do_delete_bitmap_inverted_check.get_leaked_delete_bitmap", |
1514 | 112 | &tablet_id, &rowset_id, &version, &segment_id); |
1515 | 112 | ++leaked_delete_bitmaps; |
1516 | | // log an error and continue to check the next delete bitmap |
1517 | 112 | LOG(WARNING) << fmt::format( |
1518 | 112 | "[delete bitmap check fails] can't find corresponding rowset for delete " |
1519 | 112 | "bitmap instance_id={}, tablet_id={}, rowset_id={}, version={}, " |
1520 | 112 | "segment_id={}", |
1521 | 112 | instance_id_, tablet_id, rowset_id, version, segment_id); |
1522 | 112 | } |
1523 | 343 | } |
1524 | 5 | } |
1525 | | |
1526 | 5 | return (leaked_delete_bitmaps > 0 || abnormal_delete_bitmaps > 0) ? 1 : 0; |
1527 | 5 | } |
1528 | | |
1529 | | int InstanceChecker::collect_unexpired_job_tmp_rowsets( |
1530 | 5 | std::unordered_map<int64_t, std::unordered_set<std::string>>& tmp_rowsets) { |
1531 | 5 | static constexpr int64_t max_unexpired_tmp_rowsets = 1000; |
1532 | 5 | auto begin = meta_rowset_tmp_key({instance_id_, 0, 0}); |
1533 | 5 | auto end = meta_rowset_tmp_key({instance_id_, INT64_MAX, 0}); |
1534 | 5 | std::unique_ptr<RangeGetIterator> it; |
1535 | 5 | int64_t num_scanned = 0; |
1536 | 5 | int64_t num_non_job = 0; |
1537 | 5 | int64_t num_skipped_non_job_txns = 0; |
1538 | 5 | int64_t num_unexpired = 0; |
1539 | 5 | int64_t num_expired = 0; |
1540 | 5 | int64_t last_txn_id = -1; |
1541 | 5 | int64_t current_time = duration_cast<seconds>(system_clock::now().time_since_epoch()).count(); |
1542 | | |
1543 | 8 | while (it == nullptr /* may be not init */ || (it->more() && !stopped())) { |
1544 | 6 | std::unique_ptr<Transaction> txn; |
1545 | 6 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
1546 | 6 | if (err != TxnErrorCode::TXN_OK) { |
1547 | 0 | LOG(WARNING) << "failed to create txn"; |
1548 | 0 | return -1; |
1549 | 0 | } |
1550 | 6 | err = txn->get(begin, end, &it); |
1551 | 6 | if (err != TxnErrorCode::TXN_OK) { |
1552 | 0 | LOG(WARNING) << "failed to get tmp rowset kv, err=" << err; |
1553 | 0 | return -1; |
1554 | 0 | } |
1555 | 6 | if (!it->has_next()) { |
1556 | 3 | break; |
1557 | 3 | } |
1558 | 5 | while (it->has_next() && !stopped()) { |
1559 | 3 | auto [k, v] = it->next(); |
1560 | 3 | ++num_scanned; |
1561 | | |
1562 | 3 | std::string_view k1 = k; |
1563 | 3 | k1.remove_prefix(1); |
1564 | 3 | std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> out; |
1565 | 3 | if (decode_key(&k1, &out) != 0 || out.size() < 5) { |
1566 | 0 | LOG(WARNING) << "malformed tmp rowset key, key=" << hex(k); |
1567 | 0 | return -1; |
1568 | 0 | } |
1569 | | // 0x01 "meta" ${instance_id} "rowset_tmp" ${txn_id} ${tablet_id} -> RowsetMetaCloudPB |
1570 | 3 | auto txn_id = std::get<int64_t>(std::get<0>(out[3])); |
1571 | 3 | bool is_first_rowset_of_txn = last_txn_id != txn_id; |
1572 | 3 | last_txn_id = txn_id; |
1573 | | |
1574 | 3 | doris::RowsetMetaCloudPB rowset; |
1575 | 3 | if (!rowset.ParseFromArray(v.data(), v.size())) { |
1576 | 0 | LOG(WARNING) << "malformed tmp rowset meta, key=" << hex(k); |
1577 | 0 | return -1; |
1578 | 0 | } |
1579 | 3 | if (!rowset.has_job_id() || rowset.job_id().empty()) { |
1580 | 1 | ++num_non_job; |
1581 | 1 | if (is_first_rowset_of_txn) { |
1582 | 1 | ++num_skipped_non_job_txns; |
1583 | 1 | if (txn_id == INT64_MAX) { |
1584 | 0 | begin = end; |
1585 | 1 | } else { |
1586 | 1 | begin = meta_rowset_tmp_key({instance_id_, txn_id + 1, 0}); |
1587 | 1 | } |
1588 | 1 | it.reset(); |
1589 | 1 | break; |
1590 | 1 | } |
1591 | 0 | if (!it->has_next()) { |
1592 | 0 | begin = k; |
1593 | 0 | begin.push_back('\x00'); |
1594 | 0 | } |
1595 | 0 | continue; |
1596 | 1 | } |
1597 | | |
1598 | | // Must use the same threshold as the recycler so that a delete bitmap is never |
1599 | | // reported as leaked while its tmp rowset is still alive from the recycler's view. |
1600 | | // `earlest_ts` is a local sentinel initialized to 0 on purpose: it keeps the value |
1601 | | // below any real expiration so the helper never updates the recycler's |
1602 | | // earliest-ts bvar (the checker must not touch the recycler's metrics). |
1603 | 2 | int64_t earlest_ts = 0; |
1604 | 2 | int64_t expiration = |
1605 | 2 | calculate_tmp_rowset_expired_time(instance_id_, rowset, &earlest_ts); |
1606 | 2 | if (current_time < expiration) { |
1607 | 1 | tmp_rowsets[rowset.tablet_id()].insert(rowset.rowset_id_v2()); |
1608 | 1 | ++num_unexpired; |
1609 | 1 | if (num_unexpired >= max_unexpired_tmp_rowsets) { |
1610 | 0 | LOG(WARNING) |
1611 | 0 | << "collect unexpired tmp rowsets for delete bitmap checker reached " |
1612 | 0 | << "limit, remaining tmp rowsets will not be considered and may cause " |
1613 | 0 | << "false positives, instance_id=" << instance_id_ |
1614 | 0 | << ", num_scanned=" << num_scanned << ", num_non_job=" << num_non_job |
1615 | 0 | << ", num_skipped_non_job_txns=" << num_skipped_non_job_txns |
1616 | 0 | << ", num_unexpired=" << num_unexpired |
1617 | 0 | << ", num_expired=" << num_expired |
1618 | 0 | << ", limit=" << max_unexpired_tmp_rowsets; |
1619 | 0 | return 0; |
1620 | 0 | } |
1621 | 1 | } else { |
1622 | 1 | ++num_expired; |
1623 | 1 | } |
1624 | | |
1625 | 2 | if (!it->has_next()) { |
1626 | 2 | begin = k; |
1627 | 2 | begin.push_back('\x00'); |
1628 | 2 | } |
1629 | 2 | } |
1630 | 3 | } |
1631 | | |
1632 | 5 | LOG(INFO) << "collect unexpired tmp rowsets for delete bitmap checker finished, instance_id=" |
1633 | 5 | << instance_id_ << ", num_scanned=" << num_scanned << ", num_non_job=" << num_non_job |
1634 | 5 | << ", num_skipped_non_job_txns=" << num_skipped_non_job_txns |
1635 | 5 | << ", num_unexpired=" << num_unexpired << ", num_expired=" << num_expired; |
1636 | 5 | return 0; |
1637 | 5 | } |
1638 | | |
1639 | | int InstanceChecker::get_pending_delete_bitmap_keys( |
1640 | 53 | int64_t tablet_id, std::unordered_set<std::string>& pending_delete_bitmaps) { |
1641 | 53 | std::unique_ptr<Transaction> txn; |
1642 | 53 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
1643 | 53 | if (err != TxnErrorCode::TXN_OK) { |
1644 | 0 | LOG(WARNING) << "failed to create txn"; |
1645 | 0 | return -1; |
1646 | 0 | } |
1647 | 53 | std::string pending_key = meta_pending_delete_bitmap_key({instance_id_, tablet_id}); |
1648 | 53 | std::string pending_val; |
1649 | 53 | err = txn->get(pending_key, &pending_val); |
1650 | 53 | if (err != TxnErrorCode::TXN_OK && err != TxnErrorCode::TXN_KEY_NOT_FOUND) { |
1651 | 0 | LOG(WARNING) << "failed to get pending delete bitmap kv, err=" << err; |
1652 | 0 | return -1; |
1653 | 0 | } |
1654 | 53 | if (err == TxnErrorCode::TXN_OK) { |
1655 | 2 | PendingDeleteBitmapPB pending_info; |
1656 | 2 | if (!pending_info.ParseFromString(pending_val)) [[unlikely]] { |
1657 | 0 | LOG(WARNING) << "failed to parse PendingDeleteBitmapPB, tablet=" << tablet_id; |
1658 | 0 | return -1; |
1659 | 0 | } |
1660 | 12 | for (auto& delete_bitmap_key : pending_info.delete_bitmap_keys()) { |
1661 | 12 | pending_delete_bitmaps.emplace(std::string(delete_bitmap_key)); |
1662 | 12 | } |
1663 | 2 | } |
1664 | 53 | return 0; |
1665 | 53 | } |
1666 | | |
1667 | | int InstanceChecker::check_inverted_index_file_storage_format_v1( |
1668 | | int64_t tablet_id, const std::string& file_path, const std::string& rowset_info, |
1669 | 14 | RowsetIndexesFormatV1& rowset_index_cache_v1) { |
1670 | | // format v1: data/{tablet_id}/{rowset_id}_{seg_num}_{idx_id}{idx_suffix}.idx |
1671 | 14 | std::string rowset_id; |
1672 | 14 | int64_t segment_id; |
1673 | 14 | std::string index_id_with_suffix_name; |
1674 | | // {rowset_id}_{seg_num}_{idx_id}{idx_suffix}.idx |
1675 | 14 | std::vector<std::string> str; |
1676 | 14 | butil::SplitString(rowset_info.substr(0, rowset_info.size() - 4), '_', &str); |
1677 | 14 | if (str.size() < 3) { |
1678 | 0 | LOG(WARNING) << "Split rowset info with '_' error, str size < 3, rowset_info = " |
1679 | 0 | << rowset_info; |
1680 | 0 | return -1; |
1681 | 0 | } |
1682 | 14 | rowset_id = str[0]; |
1683 | 14 | segment_id = std::atoll(str[1].c_str()); |
1684 | 14 | index_id_with_suffix_name = str[2]; |
1685 | | |
1686 | 14 | if (rowset_index_cache_v1.rowset_id == rowset_id) { |
1687 | 0 | if (rowset_index_cache_v1.segment_ids.contains(segment_id)) { |
1688 | 0 | if (auto it = rowset_index_cache_v1.index_ids.find(index_id_with_suffix_name); |
1689 | 0 | it == rowset_index_cache_v1.index_ids.end()) { |
1690 | | // clang-format off |
1691 | 0 | LOG(WARNING) << fmt::format("index_id with suffix name not found, rowset_info = {}, obj_key = {}", rowset_info, file_path); |
1692 | | // clang-format on |
1693 | 0 | return -1; |
1694 | 0 | } |
1695 | 0 | } else { |
1696 | | // clang-format off |
1697 | 0 | LOG(WARNING) << fmt::format("segment id not found, rowset_info = {}, obj_key = {}", rowset_info, file_path); |
1698 | | // clang-format on |
1699 | 0 | return -1; |
1700 | 0 | } |
1701 | 0 | } |
1702 | | |
1703 | 14 | rowset_index_cache_v1.rowset_id = rowset_id; |
1704 | 14 | rowset_index_cache_v1.segment_ids.clear(); |
1705 | 14 | rowset_index_cache_v1.index_ids.clear(); |
1706 | | |
1707 | 14 | std::unique_ptr<Transaction> txn; |
1708 | 14 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
1709 | 14 | if (err != TxnErrorCode::TXN_OK) { |
1710 | 0 | LOG(WARNING) << "failed to create txn"; |
1711 | 0 | return -1; |
1712 | 0 | } |
1713 | 14 | std::unique_ptr<RangeGetIterator> it; |
1714 | 14 | auto begin = meta_rowset_key({instance_id_, tablet_id, 0}); |
1715 | 14 | auto end = meta_rowset_key({instance_id_, tablet_id, INT64_MAX}); |
1716 | 20 | while (it == nullptr /* may be not init */ || (it->more() && !stopped())) { |
1717 | 14 | TxnErrorCode err = txn->get(begin, end, &it); |
1718 | 14 | if (err != TxnErrorCode::TXN_OK) { |
1719 | 0 | LOG(WARNING) << "failed to get rowset kv, err=" << err; |
1720 | 0 | return -1; |
1721 | 0 | } |
1722 | 14 | if (!it->has_next()) { |
1723 | 8 | break; |
1724 | 8 | } |
1725 | 6 | while (it->has_next()) { |
1726 | | // recycle corresponding resources |
1727 | 6 | auto [k, v] = it->next(); |
1728 | 6 | doris::RowsetMetaCloudPB rs_meta; |
1729 | 6 | if (!rs_meta.ParseFromArray(v.data(), v.size())) { |
1730 | 0 | LOG(WARNING) << "malformed rowset meta value, key=" << hex(k); |
1731 | 0 | return -1; |
1732 | 0 | } |
1733 | | |
1734 | 6 | TabletIndexPB tablet_index; |
1735 | 6 | if (get_tablet_idx(txn_kv_.get(), instance_id_, rs_meta.tablet_id(), tablet_index) == |
1736 | 6 | -1) { |
1737 | 0 | LOG(WARNING) << "failedt to get tablet index, tablet_id= " << rs_meta.tablet_id(); |
1738 | 0 | return -1; |
1739 | 0 | } |
1740 | | |
1741 | 6 | auto tablet_schema_key = meta_schema_key( |
1742 | 6 | {instance_id_, tablet_index.index_id(), rs_meta.schema_version()}); |
1743 | 6 | ValueBuf tablet_schema_val; |
1744 | 6 | err = cloud::blob_get(txn.get(), tablet_schema_key, &tablet_schema_val); |
1745 | | |
1746 | 6 | if (err != TxnErrorCode::TXN_OK) { |
1747 | 0 | LOG(WARNING) << "failed to get schema, err=" << err; |
1748 | 0 | return -1; |
1749 | 0 | } |
1750 | | |
1751 | 6 | auto* schema = rs_meta.mutable_tablet_schema(); |
1752 | 6 | if (!parse_schema_value(tablet_schema_val, schema)) { |
1753 | 0 | LOG(WARNING) << "malformed schema value, key=" << hex(tablet_schema_key); |
1754 | 0 | return -1; |
1755 | 0 | } |
1756 | | |
1757 | 12 | for (size_t i = 0; i < rs_meta.num_segments(); i++) { |
1758 | 6 | rowset_index_cache_v1.segment_ids.insert(i); |
1759 | 6 | } |
1760 | | |
1761 | 6 | for (const auto& i : rs_meta.tablet_schema().index()) { |
1762 | 6 | if (i.has_index_type() && i.index_type() == IndexType::INVERTED) { |
1763 | 6 | LOG(INFO) << fmt::format( |
1764 | 6 | "record index info, index_id: {}, index_suffix_name: {}", i.index_id(), |
1765 | 6 | i.index_suffix_name()); |
1766 | 6 | rowset_index_cache_v1.index_ids.insert( |
1767 | 6 | fmt::format("{}{}", i.index_id(), i.index_suffix_name())); |
1768 | 6 | } |
1769 | 6 | } |
1770 | | |
1771 | 6 | if (!it->has_next()) { |
1772 | 6 | begin = k; |
1773 | 6 | begin.push_back('\x00'); // Update to next smallest key for iteration |
1774 | 6 | break; |
1775 | 6 | } |
1776 | 6 | } |
1777 | 6 | } |
1778 | | |
1779 | 14 | if (!rowset_index_cache_v1.segment_ids.contains(segment_id)) { |
1780 | | // Garbage data leak |
1781 | | // clang-format off |
1782 | 8 | LOG(WARNING) << "rowset_index_cache_v1.segment_ids don't contains segment_id, rowset should be recycled," |
1783 | 8 | << " key = " << file_path |
1784 | 8 | << " segment_id = " << segment_id; |
1785 | | // clang-format on |
1786 | 8 | return 1; |
1787 | 8 | } |
1788 | | |
1789 | 6 | if (!rowset_index_cache_v1.index_ids.contains(index_id_with_suffix_name)) { |
1790 | | // Garbage data leak |
1791 | | // clang-format off |
1792 | 0 | LOG(WARNING) << "rowset_index_cache_v1.index_ids don't contains index_id_with_suffix_name," |
1793 | 0 | << " rowset with inde meta should be recycled, key=" << file_path |
1794 | 0 | << " index_id_with_suffix_name=" << index_id_with_suffix_name; |
1795 | | // clang-format on |
1796 | 0 | return 1; |
1797 | 0 | } |
1798 | | |
1799 | 6 | return 0; |
1800 | 6 | } |
1801 | | |
1802 | | int InstanceChecker::check_inverted_index_file_storage_format_v2( |
1803 | | int64_t tablet_id, const std::string& file_path, const std::string& rowset_info, |
1804 | 40 | RowsetIndexesFormatV2& rowset_index_cache_v2) { |
1805 | 40 | std::string rowset_id; |
1806 | 40 | int64_t segment_id; |
1807 | | // {rowset_id}_{seg_num}.idx |
1808 | 40 | std::vector<std::string> str; |
1809 | 40 | butil::SplitString(rowset_info.substr(0, rowset_info.size() - 4), '_', &str); |
1810 | 40 | if (str.size() < 2) { |
1811 | | // clang-format off |
1812 | 0 | LOG(WARNING) << "Split rowset info with '_' error, str size < 2, rowset_info = " << rowset_info; |
1813 | | // clang-format on |
1814 | 0 | return -1; |
1815 | 0 | } |
1816 | 40 | rowset_id = str[0]; |
1817 | 40 | segment_id = std::atoll(str[1].c_str()); |
1818 | | |
1819 | 40 | if (rowset_index_cache_v2.rowset_id == rowset_id) { |
1820 | 0 | if (!rowset_index_cache_v2.segment_ids.contains(segment_id)) { |
1821 | | // clang-format off |
1822 | 0 | LOG(WARNING) << fmt::format("index file not found, rowset_info = {}, obj_key = {}", rowset_info, file_path); |
1823 | | // clang-format on |
1824 | 0 | return -1; |
1825 | 0 | } |
1826 | 0 | } |
1827 | | |
1828 | 40 | rowset_index_cache_v2.rowset_id = rowset_id; |
1829 | 40 | rowset_index_cache_v2.segment_ids.clear(); |
1830 | | |
1831 | 40 | std::unique_ptr<Transaction> txn; |
1832 | 40 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
1833 | 40 | if (err != TxnErrorCode::TXN_OK) { |
1834 | 0 | LOG(WARNING) << "failed to create txn"; |
1835 | 0 | return -1; |
1836 | 0 | } |
1837 | 40 | std::unique_ptr<RangeGetIterator> it; |
1838 | 40 | auto begin = meta_rowset_key({instance_id_, tablet_id, 0}); |
1839 | 40 | auto end = meta_rowset_key({instance_id_, tablet_id, INT64_MAX}); |
1840 | 75 | while (it == nullptr /* may be not init */ || (it->more() && !stopped())) { |
1841 | 40 | TxnErrorCode err = txn->get(begin, end, &it); |
1842 | 40 | if (err != TxnErrorCode::TXN_OK) { |
1843 | 0 | LOG(WARNING) << "failed to get rowset kv, err=" << err; |
1844 | 0 | return -1; |
1845 | 0 | } |
1846 | 40 | if (!it->has_next()) { |
1847 | 5 | break; |
1848 | 5 | } |
1849 | 35 | while (it->has_next()) { |
1850 | | // recycle corresponding resources |
1851 | 35 | auto [k, v] = it->next(); |
1852 | 35 | doris::RowsetMetaCloudPB rs_meta; |
1853 | 35 | if (!rs_meta.ParseFromArray(v.data(), v.size())) { |
1854 | 0 | LOG(WARNING) << "malformed rowset meta value, key=" << hex(k); |
1855 | 0 | return -1; |
1856 | 0 | } |
1857 | | |
1858 | 70 | for (size_t i = 0; i < rs_meta.num_segments(); i++) { |
1859 | 35 | rowset_index_cache_v2.segment_ids.insert(i); |
1860 | 35 | } |
1861 | | |
1862 | 35 | if (!it->has_next()) { |
1863 | 35 | begin = k; |
1864 | 35 | begin.push_back('\x00'); // Update to next smallest key for iteration |
1865 | 35 | break; |
1866 | 35 | } |
1867 | 35 | } |
1868 | 35 | } |
1869 | | |
1870 | 40 | if (!rowset_index_cache_v2.segment_ids.contains(segment_id)) { |
1871 | | // Garbage data leak |
1872 | 5 | LOG(WARNING) << "rowset with index meta should be recycled, key=" << file_path; |
1873 | 5 | return 1; |
1874 | 5 | } |
1875 | | |
1876 | 35 | return 0; |
1877 | 40 | } |
1878 | | |
1879 | | int InstanceChecker::check_delete_bitmap_storage_optimize_v2( |
1880 | | int64_t tablet_id, bool has_sequence_col, |
1881 | 30 | int64_t& rowsets_with_useless_delete_bitmap_version) { |
1882 | | // end_version: create_time |
1883 | 30 | std::map<int64_t, int64_t> tablet_rowsets_map {}; |
1884 | | // rowset_id: {start_version, end_version} |
1885 | 30 | std::map<std::string, std::pair<int64_t, int64_t>> rowset_version_map; |
1886 | | // Get all visible rowsets of this tablet |
1887 | 195 | auto collect_cb = [&](const doris::RowsetMetaCloudPB& rowset) { |
1888 | 195 | if (rowset.start_version() == 0 && rowset.end_version() == 1) { |
1889 | | // ignore dummy rowset [0-1] |
1890 | 0 | return; |
1891 | 0 | } |
1892 | 195 | tablet_rowsets_map[rowset.end_version()] = rowset.creation_time(); |
1893 | 195 | rowset_version_map[rowset.rowset_id_v2()] = |
1894 | 195 | std::make_pair(rowset.start_version(), rowset.end_version()); |
1895 | 195 | }; |
1896 | 30 | if (int ret = collect_tablet_rowsets(tablet_id, collect_cb); ret != 0) { |
1897 | 0 | return ret; |
1898 | 0 | } |
1899 | | |
1900 | 30 | std::unordered_set<std::string> pending_delete_bitmaps; |
1901 | 30 | if (auto ret = get_pending_delete_bitmap_keys(tablet_id, pending_delete_bitmaps); ret < 0) { |
1902 | 0 | return ret; |
1903 | 0 | } |
1904 | | |
1905 | 30 | std::unique_ptr<RangeGetIterator> it; |
1906 | 30 | auto begin = meta_delete_bitmap_key({instance_id_, tablet_id, "", 0, 0}); |
1907 | 30 | auto end = meta_delete_bitmap_key({instance_id_, tablet_id + 1, "", 0, 0}); |
1908 | 30 | std::string last_rowset_id = ""; |
1909 | 30 | int64_t last_version = 0; |
1910 | 30 | int64_t last_failed_version = 0; |
1911 | 30 | std::vector<int64_t> failed_versions; |
1912 | 30 | auto print_failed_versions = [&]() { |
1913 | 4 | TEST_SYNC_POINT_CALLBACK( |
1914 | 4 | "InstanceChecker::check_delete_bitmap_storage_optimize_v2.get_abnormal_" |
1915 | 4 | "rowset", |
1916 | 4 | &tablet_id, &last_rowset_id); |
1917 | 4 | rowsets_with_useless_delete_bitmap_version++; |
1918 | | // some versions are continuous, such as [8, 9, 10, 11, 13, 17, 18] |
1919 | | // print as [8-11, 13, 17-18] |
1920 | 4 | int64_t last_start_version = -1; |
1921 | 4 | int64_t last_end_version = -1; |
1922 | 4 | std::stringstream ss; |
1923 | 4 | ss << "["; |
1924 | 9 | for (int64_t version : failed_versions) { |
1925 | 9 | if (last_start_version == -1) { |
1926 | 4 | last_start_version = version; |
1927 | 4 | last_end_version = version; |
1928 | 4 | continue; |
1929 | 4 | } |
1930 | 5 | if (last_end_version + 1 == version) { |
1931 | 2 | last_end_version = version; |
1932 | 3 | } else { |
1933 | 3 | if (last_start_version == last_end_version) { |
1934 | 3 | ss << last_start_version << ", "; |
1935 | 3 | } else { |
1936 | 0 | ss << last_start_version << "-" << last_end_version << ", "; |
1937 | 0 | } |
1938 | 3 | last_start_version = version; |
1939 | 3 | last_end_version = version; |
1940 | 3 | } |
1941 | 5 | } |
1942 | 4 | if (last_start_version == last_end_version) { |
1943 | 3 | ss << last_start_version; |
1944 | 3 | } else { |
1945 | 1 | ss << last_start_version << "-" << last_end_version; |
1946 | 1 | } |
1947 | 4 | ss << "]"; |
1948 | 4 | std::stringstream version_str; |
1949 | 4 | auto it = rowset_version_map.find(last_rowset_id); |
1950 | 4 | if (it != rowset_version_map.end()) { |
1951 | 4 | version_str << "[" << it->second.first << "-" << it->second.second << "]"; |
1952 | 4 | } |
1953 | 4 | LOG(WARNING) << fmt::format( |
1954 | 4 | "[delete bitmap check fails] delete bitmap storage optimize v2 check fail " |
1955 | 4 | "for instance_id={}, tablet_id={}, rowset_id={}, version={} found delete " |
1956 | 4 | "bitmap with versions={}, size={}", |
1957 | 4 | instance_id_, tablet_id, last_rowset_id, version_str.str(), ss.str(), |
1958 | 4 | failed_versions.size()); |
1959 | 4 | }; |
1960 | 30 | using namespace std::chrono; |
1961 | 30 | int64_t now = duration_cast<seconds>(system_clock::now().time_since_epoch()).count(); |
1962 | 60 | while (it == nullptr /* may be not init */ || (it->more() && !stopped())) { |
1963 | 30 | std::unique_ptr<Transaction> txn; |
1964 | 30 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
1965 | 30 | if (err != TxnErrorCode::TXN_OK) { |
1966 | 0 | LOG(WARNING) << "failed to create txn"; |
1967 | 0 | return -1; |
1968 | 0 | } |
1969 | 30 | err = txn->get(begin, end, &it); |
1970 | 30 | if (err != TxnErrorCode::TXN_OK) { |
1971 | 0 | LOG(WARNING) << "failed to get delete bitmap kv, err=" << err; |
1972 | 0 | return -1; |
1973 | 0 | } |
1974 | 30 | if (!it->has_next()) { |
1975 | 0 | break; |
1976 | 0 | } |
1977 | 691 | while (it->has_next() && !stopped()) { |
1978 | 661 | auto [k, v] = it->next(); |
1979 | 661 | std::string_view k1 = k; |
1980 | 661 | k1.remove_prefix(1); |
1981 | 661 | std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> out; |
1982 | 661 | decode_key(&k1, &out); |
1983 | | // 0x01 "meta" ${instance_id} "delete_bitmap" ${tablet_id} ${rowset_id} ${version} ${segment_id} -> roaringbitmap |
1984 | 661 | auto rowset_id = std::get<std::string>(std::get<0>(out[4])); |
1985 | 661 | auto version = std::get<std::int64_t>(std::get<0>(out[5])); |
1986 | 661 | if (!it->has_next()) { |
1987 | 30 | begin = k; |
1988 | 30 | begin.push_back('\x00'); // Update to next smallest key for iteration |
1989 | 30 | } |
1990 | 661 | if (rowset_id == last_rowset_id && version == last_version) { |
1991 | | // skip the same rowset and version |
1992 | 87 | continue; |
1993 | 87 | } |
1994 | 574 | if (rowset_id != last_rowset_id && !failed_versions.empty()) { |
1995 | 3 | print_failed_versions(); |
1996 | 3 | last_failed_version = 0; |
1997 | 3 | failed_versions.clear(); |
1998 | 3 | } |
1999 | 574 | last_rowset_id = rowset_id; |
2000 | 574 | last_version = version; |
2001 | 574 | if (tablet_rowsets_map.find(version) != tablet_rowsets_map.end()) { |
2002 | 548 | continue; |
2003 | 548 | } |
2004 | 26 | auto version_it = rowset_version_map.find(rowset_id); |
2005 | 26 | if (version_it == rowset_version_map.end()) { |
2006 | | // checked in do_delete_bitmap_inverted_check |
2007 | 1 | continue; |
2008 | 1 | } |
2009 | 25 | if (pending_delete_bitmaps.contains(std::string(k))) { |
2010 | 3 | continue; |
2011 | 3 | } |
2012 | 22 | if (has_sequence_col && version >= version_it->second.first && |
2013 | 22 | version <= version_it->second.second) { |
2014 | 5 | continue; |
2015 | 5 | } |
2016 | | // there may be an interval in this situation: |
2017 | | // 1. finish compaction job; 2. checker; 3. finish agg and remove delete bitmap to ms |
2018 | 17 | auto rowset_it = tablet_rowsets_map.upper_bound(version); |
2019 | 17 | if (rowset_it == tablet_rowsets_map.end()) { |
2020 | 1 | if (version != last_failed_version) { |
2021 | 1 | failed_versions.push_back(version); |
2022 | 1 | } |
2023 | 1 | last_failed_version = version; |
2024 | 1 | continue; |
2025 | 1 | } |
2026 | 16 | if (rowset_it->second + config::delete_bitmap_storage_optimize_v2_check_skip_seconds >= |
2027 | 16 | now) { |
2028 | 8 | continue; |
2029 | 8 | } |
2030 | 8 | if (version != last_failed_version) { |
2031 | 8 | failed_versions.push_back(version); |
2032 | 8 | } |
2033 | 8 | last_failed_version = version; |
2034 | 8 | } |
2035 | 30 | } |
2036 | 30 | if (!failed_versions.empty()) { |
2037 | 1 | print_failed_versions(); |
2038 | 1 | } |
2039 | 30 | LOG(INFO) << fmt::format( |
2040 | 30 | "[delete bitmap checker] finish check delete bitmap storage optimize v2 for " |
2041 | 30 | "instance_id={}, tablet_id={}, rowsets_num={}, " |
2042 | 30 | "rowsets_with_useless_delete_bitmap_version={}", |
2043 | 30 | instance_id_, tablet_id, tablet_rowsets_map.size(), |
2044 | 30 | rowsets_with_useless_delete_bitmap_version); |
2045 | 30 | return (rowsets_with_useless_delete_bitmap_version > 1 ? 1 : 0); |
2046 | 30 | } |
2047 | | |
2048 | 3 | int InstanceChecker::do_delete_bitmap_storage_optimize_check(int version) { |
2049 | 3 | if (version != 2) { |
2050 | 0 | return -1; |
2051 | 0 | } |
2052 | 3 | int64_t total_tablets_num {0}; |
2053 | 3 | int64_t failed_tablets_num {0}; |
2054 | | |
2055 | | // for v2 check |
2056 | 3 | int64_t max_rowsets_with_useless_delete_bitmap_version = 0; |
2057 | 3 | int64_t tablet_id_with_max_rowsets_with_useless_delete_bitmap_version = 0; |
2058 | | |
2059 | | // check that for every visible rowset, there exists at least delete one bitmap in MS |
2060 | 30 | int ret = traverse_mow_tablet([&](int64_t tablet_id, bool has_sequence_col) { |
2061 | 30 | ++total_tablets_num; |
2062 | 30 | int64_t rowsets_with_useless_delete_bitmap_version = 0; |
2063 | 30 | int res = check_delete_bitmap_storage_optimize_v2( |
2064 | 30 | tablet_id, has_sequence_col, rowsets_with_useless_delete_bitmap_version); |
2065 | 30 | if (rowsets_with_useless_delete_bitmap_version > |
2066 | 30 | max_rowsets_with_useless_delete_bitmap_version) { |
2067 | 1 | max_rowsets_with_useless_delete_bitmap_version = |
2068 | 1 | rowsets_with_useless_delete_bitmap_version; |
2069 | 1 | tablet_id_with_max_rowsets_with_useless_delete_bitmap_version = tablet_id; |
2070 | 1 | } |
2071 | 30 | failed_tablets_num += (res != 0); |
2072 | 30 | return res; |
2073 | 30 | }); |
2074 | | |
2075 | 3 | if (ret < 0) { |
2076 | 0 | return ret; |
2077 | 0 | } |
2078 | | |
2079 | 3 | g_bvar_max_rowsets_with_useless_delete_bitmap_version.put( |
2080 | 3 | instance_id_, max_rowsets_with_useless_delete_bitmap_version); |
2081 | | |
2082 | 3 | std::stringstream ss; |
2083 | 3 | ss << "[delete bitmap checker] check delete bitmap storage optimize v" << version |
2084 | 3 | << " for instance_id=" << instance_id_ << ", total_tablets_num=" << total_tablets_num |
2085 | 3 | << ", failed_tablets_num=" << failed_tablets_num |
2086 | 3 | << ". max_rowsets_with_useless_delete_bitmap_version=" |
2087 | 3 | << max_rowsets_with_useless_delete_bitmap_version |
2088 | 3 | << ", tablet_id=" << tablet_id_with_max_rowsets_with_useless_delete_bitmap_version; |
2089 | 3 | LOG(INFO) << ss.str(); |
2090 | | |
2091 | 3 | return (failed_tablets_num > 0) ? 1 : 0; |
2092 | 3 | } |
2093 | | |
2094 | 3 | int InstanceChecker::do_mow_job_key_check() { |
2095 | 3 | std::unique_ptr<RangeGetIterator> it; |
2096 | 3 | std::string begin = mow_tablet_job_key({instance_id_, 0, 0}); |
2097 | 3 | std::string end = mow_tablet_job_key({instance_id_, INT64_MAX, 0}); |
2098 | 3 | MowTabletJobPB mow_tablet_job; |
2099 | 4 | while (it == nullptr /* may be not init */ || (it->more() && !stopped())) { |
2100 | 3 | std::unique_ptr<Transaction> txn; |
2101 | 3 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
2102 | 3 | if (err != TxnErrorCode::TXN_OK) { |
2103 | 0 | LOG(WARNING) << "failed to create txn"; |
2104 | 0 | return -1; |
2105 | 0 | } |
2106 | 3 | err = txn->get(begin, end, &it); |
2107 | 3 | if (err != TxnErrorCode::TXN_OK) { |
2108 | 0 | LOG(WARNING) << "failed to get mow tablet job key, err=" << err; |
2109 | 0 | return -1; |
2110 | 0 | } |
2111 | 3 | int64_t now = duration_cast<std::chrono::seconds>( |
2112 | 3 | std::chrono::system_clock::now().time_since_epoch()) |
2113 | 3 | .count(); |
2114 | 3 | while (it->has_next() && !stopped()) { |
2115 | 2 | auto [k, v] = it->next(); |
2116 | 2 | std::string_view k1 = k; |
2117 | 2 | k1.remove_prefix(1); |
2118 | 2 | std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> out; |
2119 | 2 | decode_key(&k1, &out); |
2120 | | // 0x01 "meta" ${instance_id} "mow_tablet_job" ${table_id} ${initiator} |
2121 | 2 | auto table_id = std::get<int64_t>(std::get<0>(out[3])); |
2122 | 2 | auto initiator = std::get<int64_t>(std::get<0>(out[4])); |
2123 | 2 | if (!mow_tablet_job.ParseFromArray(v.data(), v.size())) [[unlikely]] { |
2124 | 0 | LOG(WARNING) << "failed to parse MowTabletJobPB"; |
2125 | 0 | return -1; |
2126 | 0 | } |
2127 | 2 | int64_t expiration = mow_tablet_job.expiration(); |
2128 | | // check job key failed should meet both following two condition: |
2129 | | // 1. job key is expired |
2130 | | // 2. table lock key is not found or key is not expired |
2131 | 2 | if (expiration < now - config::mow_job_key_check_expiration_diff_seconds) { |
2132 | 2 | std::string lock_key = |
2133 | 2 | meta_delete_bitmap_update_lock_key({instance_id_, table_id, -1}); |
2134 | 2 | std::string lock_val; |
2135 | 2 | err = txn->get(lock_key, &lock_val); |
2136 | 2 | std::string reason = ""; |
2137 | 2 | if (err == TxnErrorCode::TXN_KEY_NOT_FOUND) { |
2138 | 0 | reason = "table lock key not found"; |
2139 | |
|
2140 | 2 | } else { |
2141 | 2 | DeleteBitmapUpdateLockPB lock_info; |
2142 | 2 | if (!lock_info.ParseFromString(lock_val)) [[unlikely]] { |
2143 | 0 | LOG(WARNING) << "failed to parse DeleteBitmapUpdateLockPB"; |
2144 | 0 | return -1; |
2145 | 0 | } |
2146 | 2 | if (lock_info.expiration() > now || lock_info.lock_id() != -1) { |
2147 | 2 | reason = "table lock is not expired,lock_id=" + |
2148 | 2 | std::to_string(lock_info.lock_id()); |
2149 | 2 | } |
2150 | 2 | } |
2151 | 2 | if (reason != "") { |
2152 | 2 | LOG(WARNING) << fmt::format( |
2153 | 2 | "[compaction key check fails] mow job key check fail for " |
2154 | 2 | "instance_id={}, table_id={}, initiator={}, expiration={}, now={}, " |
2155 | 2 | "reason={}", |
2156 | 2 | instance_id_, table_id, initiator, expiration, now, reason); |
2157 | 2 | return -1; |
2158 | 2 | } |
2159 | 2 | } |
2160 | 2 | } |
2161 | 1 | begin = it->next_begin_key(); // Update to next smallest key for iteration |
2162 | 1 | } |
2163 | 1 | return 0; |
2164 | 3 | } |
2165 | 4 | int InstanceChecker::do_tablet_stats_key_check() { |
2166 | 4 | int ret = 0; |
2167 | | |
2168 | 4 | int64_t nums_leak = 0; |
2169 | 4 | int64_t nums_loss = 0; |
2170 | 4 | int64_t nums_scanned = 0; |
2171 | 4 | int64_t nums_abnormal = 0; |
2172 | | |
2173 | 4 | std::string begin = meta_tablet_key({instance_id_, 0, 0, 0, 0}); |
2174 | 4 | std::string end = meta_tablet_key({instance_id_, INT64_MAX, 0, 0, 0}); |
2175 | | // inverted check tablet exists |
2176 | 4 | LOG(INFO) << "begin inverted check stats_tablet_key"; |
2177 | 4 | ret = scan_and_handle_kv(begin, end, [&](std::string_view key, std::string_view value) { |
2178 | 4 | int ret = check_stats_tablet_key_exists(key, value); |
2179 | 4 | nums_scanned++; |
2180 | 4 | if (ret == 1) { |
2181 | 1 | nums_loss++; |
2182 | 1 | } |
2183 | 4 | return ret; |
2184 | 4 | }); |
2185 | 4 | if (ret == -1) { |
2186 | 0 | LOG(WARNING) << "failed to inverted check if stats tablet key exists"; |
2187 | 0 | return -1; |
2188 | 4 | } else if (ret == 1) { |
2189 | 1 | LOG(WARNING) << "stats_tablet_key loss, nums_scanned=" << nums_scanned |
2190 | 1 | << ", nums_loss=" << nums_loss; |
2191 | 1 | return 1; |
2192 | 1 | } |
2193 | 4 | LOG(INFO) << "finish inverted check stats_tablet_key, nums_scanned=" << nums_scanned |
2194 | 3 | << ", nums_loss=" << nums_loss; |
2195 | | |
2196 | 3 | begin = stats_tablet_key({instance_id_, 0, 0, 0, 0}); |
2197 | 3 | end = stats_tablet_key({instance_id_, INT64_MAX, 0, 0, 0}); |
2198 | 3 | nums_scanned = 0; |
2199 | | // check tablet exists |
2200 | 3 | LOG(INFO) << "begin check stats_tablet_key leaked"; |
2201 | 4 | ret = scan_and_handle_kv(begin, end, [&](std::string_view key, std::string_view value) { |
2202 | 4 | int ret = check_stats_tablet_key_leaked(key, value); |
2203 | 4 | nums_scanned++; |
2204 | 4 | if (ret == 1) { |
2205 | 1 | nums_leak++; |
2206 | 1 | } |
2207 | 4 | return ret; |
2208 | 4 | }); |
2209 | 3 | if (ret == -1) { |
2210 | 0 | LOG(WARNING) << "failed to check if stats tablet key exists"; |
2211 | 0 | return -1; |
2212 | 3 | } else if (ret == 1) { |
2213 | 1 | LOG(WARNING) << "stats_tablet_key leaked, nums_scanned=" << nums_scanned |
2214 | 1 | << ", nums_leak=" << nums_leak; |
2215 | 1 | return 1; |
2216 | 1 | } |
2217 | 3 | LOG(INFO) << "finish check stats_tablet_key leaked, nums_scanned=" << nums_scanned |
2218 | 2 | << ", nums_leak=" << nums_leak; |
2219 | | |
2220 | 2 | begin = stats_tablet_key({instance_id_, 0, 0, 0, 0}); |
2221 | 2 | end = stats_tablet_key({instance_id_, INT64_MAX, 0, 0, 0}); |
2222 | 2 | nums_scanned = 0; |
2223 | | // check if key is normal |
2224 | 2 | LOG(INFO) << "begin check stats_tablet_key abnormal"; |
2225 | 2 | ret = scan_and_handle_kv(begin, end, [&](std::string_view key, std::string_view value) { |
2226 | 2 | int ret = check_stats_tablet_key(key, value); |
2227 | 2 | nums_scanned++; |
2228 | 2 | if (ret == 1) { |
2229 | 1 | nums_abnormal++; |
2230 | 1 | } |
2231 | 2 | return ret; |
2232 | 2 | }); |
2233 | 2 | if (ret == -1) { |
2234 | 0 | LOG(WARNING) << "failed to check if stats tablet key exists"; |
2235 | 0 | return -1; |
2236 | 2 | } else if (ret == 1) { |
2237 | 1 | LOG(WARNING) << "stats_tablet_key abnormal, nums_scanned=" << nums_scanned |
2238 | 1 | << ", nums_abnormal=" << nums_abnormal; |
2239 | 1 | return 1; |
2240 | 1 | } |
2241 | 2 | LOG(INFO) << "finish check stats_tablet_key, nums_scanned=" << nums_scanned |
2242 | 1 | << ", nums_abnormal=" << nums_abnormal; |
2243 | 1 | return 0; |
2244 | 2 | } |
2245 | | |
2246 | 4 | int InstanceChecker::check_stats_tablet_key_exists(std::string_view key, std::string_view value) { |
2247 | 4 | std::string_view k1 = key; |
2248 | 4 | k1.remove_prefix(1); |
2249 | 4 | std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> out; |
2250 | 4 | decode_key(&k1, &out); |
2251 | | // 0x01 "meta" ${instance_id} "tablet" ${table_id} ${index_id} ${partition_id} ${tablet_id} |
2252 | 4 | auto table_id = std::get<int64_t>(std::get<0>(out[3])); |
2253 | 4 | auto index_id = std::get<int64_t>(std::get<0>(out[4])); |
2254 | 4 | auto partition_id = std::get<int64_t>(std::get<0>(out[5])); |
2255 | 4 | auto tablet_id = std::get<int64_t>(std::get<0>(out[6])); |
2256 | 4 | std::string tablet_stats_key = |
2257 | 4 | stats_tablet_key({instance_id_, table_id, index_id, partition_id, tablet_id}); |
2258 | 4 | int ret = key_exist(txn_kv_.get(), tablet_stats_key); |
2259 | 4 | if (ret == 1) { |
2260 | | // clang-format off |
2261 | 1 | LOG(WARNING) << "stats tablet key's tablet key loss," |
2262 | 1 | << " stats tablet key=" << hex(tablet_stats_key) |
2263 | 1 | << " meta tablet key=" << hex(key); |
2264 | | // clang-format on |
2265 | 1 | return 1; |
2266 | 3 | } else if (ret == -1) { |
2267 | 0 | LOG(WARNING) << "failed to check key exists, key=" << hex(tablet_stats_key); |
2268 | 0 | return -1; |
2269 | 0 | } |
2270 | 4 | LOG(INFO) << "check stats_tablet_key_exists ok, key=" << hex(key); |
2271 | 3 | return 0; |
2272 | 4 | } |
2273 | | |
2274 | 4 | int InstanceChecker::check_stats_tablet_key_leaked(std::string_view key, std::string_view value) { |
2275 | 4 | std::string_view k1 = key; |
2276 | 4 | k1.remove_prefix(1); |
2277 | 4 | std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> out; |
2278 | 4 | decode_key(&k1, &out); |
2279 | | // 0x01 "stats" ${instance_id} "tablet" ${table_id} ${index_id} ${partition_id} ${tablet_id} |
2280 | 4 | auto table_id = std::get<int64_t>(std::get<0>(out[3])); |
2281 | 4 | auto index_id = std::get<int64_t>(std::get<0>(out[4])); |
2282 | 4 | auto partition_id = std::get<int64_t>(std::get<0>(out[5])); |
2283 | 4 | auto tablet_id = std::get<int64_t>(std::get<0>(out[6])); |
2284 | 4 | std::string tablet_key = |
2285 | 4 | meta_tablet_key({instance_id_, table_id, index_id, partition_id, tablet_id}); |
2286 | 4 | int ret = key_exist(txn_kv_.get(), tablet_key); |
2287 | 4 | if (ret == 1) { |
2288 | | // clang-format off |
2289 | 1 | LOG(WARNING) << "stats tablet key's tablet key leak," |
2290 | 1 | << " stats tablet key=" << hex(key) |
2291 | 1 | << " meta tablet key=" << hex(tablet_key); |
2292 | | // clang-format on |
2293 | 1 | return 1; |
2294 | 3 | } else if (ret == -1) { |
2295 | 0 | LOG(WARNING) << "failed to check key exists, key=" << hex(tablet_key); |
2296 | 0 | return -1; |
2297 | 0 | } |
2298 | 4 | LOG(INFO) << "check stats_tablet_key_leaked ok, key=" << hex(key); |
2299 | 3 | return 0; |
2300 | 4 | } |
2301 | | |
2302 | 2 | int InstanceChecker::check_stats_tablet_key(std::string_view key, std::string_view value) { |
2303 | 2 | TabletStatsPB tablet_stats_pb; |
2304 | 2 | std::string_view k1 = key; |
2305 | 2 | k1.remove_prefix(1); |
2306 | 2 | std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> out; |
2307 | 2 | decode_key(&k1, &out); |
2308 | | // 0x01 "stats" ${instance_id} "tablet" ${table_id} ${index_id} ${partition_id} ${tablet_id} |
2309 | 2 | auto tablet_id = std::get<int64_t>(std::get<0>(out[6])); |
2310 | 2 | std::unique_ptr<Transaction> txn; |
2311 | 2 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
2312 | 2 | if (err != TxnErrorCode::TXN_OK) { |
2313 | 0 | LOG_WARNING("failed to recycle tablet ") |
2314 | 0 | .tag("tablet id", tablet_id) |
2315 | 0 | .tag("instance_id", instance_id_) |
2316 | 0 | .tag("reason", "failed to create txn"); |
2317 | 0 | return -1; |
2318 | 0 | } |
2319 | 2 | std::string tablet_idx_key = meta_tablet_idx_key({instance_id_, tablet_id}); |
2320 | 2 | std::string tablet_idx_val; |
2321 | 2 | TabletIndexPB tablet_idx; |
2322 | 2 | err = txn->get(tablet_idx_key, &tablet_idx_val); |
2323 | 2 | if (err != TxnErrorCode::TXN_OK) { |
2324 | | // clang-format off |
2325 | 0 | LOG(WARNING) << "failed to get tablet index key," |
2326 | 0 | << " key=" << hex(tablet_idx_key) |
2327 | 0 | << " code=" << err; |
2328 | | // clang-format on |
2329 | 0 | return -1; |
2330 | 0 | } |
2331 | 2 | tablet_idx.ParseFromString(tablet_idx_val); |
2332 | 2 | MetaServiceCode code = MetaServiceCode::OK; |
2333 | 2 | std::string msg; |
2334 | 2 | internal_get_tablet_stats(code, msg, txn.get(), instance_id_, tablet_idx, tablet_stats_pb); |
2335 | 2 | if (code != MetaServiceCode::OK) { |
2336 | | // clang-format off |
2337 | 0 | LOG(WARNING) << "failed to get tablet stats," |
2338 | 0 | << " code=" << code |
2339 | 0 | << " msg=" << msg; |
2340 | | // clang-format on |
2341 | 0 | return -1; |
2342 | 0 | } |
2343 | | |
2344 | 2 | GetRowsetResponse resp; |
2345 | | // get rowsets in tablet |
2346 | 2 | internal_get_rowset(txn.get(), 0, std::numeric_limits<int64_t>::max() - 1, instance_id_, |
2347 | 2 | tablet_id, code, msg, &resp); |
2348 | 2 | if (code != MetaServiceCode::OK) { |
2349 | 0 | LOG_WARNING("failed to get rowsets of tablet when check stats tablet key") |
2350 | 0 | .tag("tablet id", tablet_id) |
2351 | 0 | .tag("msg", msg) |
2352 | 0 | .tag("code", code) |
2353 | 0 | .tag("instance id", instance_id_); |
2354 | 0 | return -1; |
2355 | 0 | } |
2356 | 2 | int64_t num_rows = 0; |
2357 | 2 | int64_t num_rowsets = 0; |
2358 | 2 | int64_t num_segments = 0; |
2359 | 2 | int64_t total_data_size = 0; |
2360 | 2 | for (const auto& rs_meta : resp.rowset_meta()) { |
2361 | 2 | num_rows += rs_meta.num_rows(); |
2362 | 2 | num_rowsets++; |
2363 | 2 | num_segments += rs_meta.num_segments(); |
2364 | 2 | total_data_size += rs_meta.total_disk_size(); |
2365 | 2 | } |
2366 | 2 | int ret = 0; |
2367 | 2 | if (tablet_stats_pb.data_size() != total_data_size) { |
2368 | 1 | ret = 1; |
2369 | | // clang-format off |
2370 | 1 | LOG(WARNING) << " tablet_stats_pb's data size is not same with all rowset total data size," |
2371 | 1 | << " tablet_stats_pb's data size=" << tablet_stats_pb.data_size() |
2372 | 1 | << " all rowset total data size=" << total_data_size |
2373 | 1 | << " stats tablet meta=" << tablet_stats_pb.ShortDebugString(); |
2374 | | // clang-format on |
2375 | 1 | } else if (tablet_stats_pb.num_rows() != num_rows) { |
2376 | 0 | ret = 1; |
2377 | | // clang-format off |
2378 | 0 | LOG(WARNING) << " tablet_stats_pb's num_rows is not same with all rowset total num_rows," |
2379 | 0 | << " tablet_stats_pb's num_rows=" << tablet_stats_pb.num_rows() |
2380 | 0 | << " all rowset total num_rows=" << num_rows |
2381 | 0 | << " stats tablet meta=" << tablet_stats_pb.ShortDebugString(); |
2382 | | // clang-format on |
2383 | 1 | } else if (tablet_stats_pb.num_rowsets() != num_rowsets) { |
2384 | 0 | ret = 1; |
2385 | | // clang-format off |
2386 | 0 | LOG(WARNING) << " tablet_stats_pb's num_rowsets is not same with all rowset nums," |
2387 | 0 | << " tablet_stats_pb's num_rowsets=" << tablet_stats_pb.num_rowsets() |
2388 | 0 | << " all rowset nums=" << num_rowsets |
2389 | 0 | << " stats tablet meta=" << tablet_stats_pb.ShortDebugString(); |
2390 | | // clang-format on |
2391 | 1 | } else if (tablet_stats_pb.num_segments() != num_segments) { |
2392 | 0 | ret = 1; |
2393 | | // clang-format off |
2394 | 0 | LOG(WARNING) << " tablet_stats_pb's num_segments is not same with all rowset total num_segments," |
2395 | 0 | << " tablet_stats_pb's num_segments=" << tablet_stats_pb.num_segments() |
2396 | 0 | << " all rowset total num_segments=" << num_segments |
2397 | 0 | << " stats tablet meta=" << tablet_stats_pb.ShortDebugString(); |
2398 | | // clang-format on |
2399 | 0 | } |
2400 | | |
2401 | 2 | return ret; |
2402 | 2 | } |
2403 | | |
2404 | | int InstanceChecker::scan_and_handle_kv( |
2405 | | std::string& start_key, const std::string& end_key, |
2406 | 12 | std::function<int(std::string_view, std::string_view)> handle_kv) { |
2407 | 12 | std::unique_ptr<Transaction> txn; |
2408 | 12 | int ret = 0; |
2409 | 12 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
2410 | 12 | if (err != TxnErrorCode::TXN_OK) { |
2411 | 0 | LOG(WARNING) << "failed to init txn"; |
2412 | 0 | return -1; |
2413 | 0 | } |
2414 | 12 | std::unique_ptr<RangeGetIterator> it; |
2415 | 12 | int limit = 10000; |
2416 | 12 | TEST_SYNC_POINT_CALLBACK("InstanceChecker:scan_and_handle_kv:limit", &limit); |
2417 | 25 | while (it == nullptr /* may be not init */ || (it->more() && !stopped())) { |
2418 | 13 | err = txn->get(start_key, end_key, &it, false, limit); |
2419 | 13 | TEST_SYNC_POINT_CALLBACK("InstanceChecker:scan_and_handle_kv:get_err", &err); |
2420 | 13 | if (err == TxnErrorCode::TXN_TOO_OLD) { |
2421 | 1 | LOG(WARNING) << "failed to get range kv, err=txn too old, " |
2422 | 1 | << " now fallback to non snapshot scan"; |
2423 | 1 | err = txn_kv_->create_txn(&txn); |
2424 | 1 | if (err == TxnErrorCode::TXN_OK) { |
2425 | 1 | err = txn->get(start_key, end_key, &it); |
2426 | 1 | } |
2427 | 1 | } |
2428 | 13 | if (err != TxnErrorCode::TXN_OK) { |
2429 | 0 | LOG(WARNING) << "internal error, failed to get range kv, err=" << err; |
2430 | 0 | return -1; |
2431 | 0 | } |
2432 | | |
2433 | 229 | while (it->has_next() && !stopped()) { |
2434 | 216 | auto [k, v] = it->next(); |
2435 | | |
2436 | 216 | int handle_ret = handle_kv(k, v); |
2437 | 216 | if (handle_ret == -1) { |
2438 | 0 | return -1; |
2439 | 216 | } else { |
2440 | 216 | ret = std::max(ret, handle_ret); |
2441 | 216 | } |
2442 | 216 | if (!it->has_next()) { |
2443 | 13 | start_key = k; |
2444 | 13 | } |
2445 | 216 | } |
2446 | 13 | start_key = it->next_begin_key(); |
2447 | 13 | } |
2448 | 12 | return ret; |
2449 | 12 | } |
2450 | | |
2451 | 2 | int InstanceChecker::do_version_key_check() { |
2452 | 2 | std::unique_ptr<RangeGetIterator> table_it; |
2453 | 2 | std::string begin = table_version_key({instance_id_, 0, 0}); |
2454 | 2 | std::string end = table_version_key({instance_id_, INT64_MAX, 0}); |
2455 | 2 | bool check_res = true; |
2456 | 4 | while (table_it == nullptr /* may be not init */ || (table_it->more() && !stopped())) { |
2457 | 2 | std::unique_ptr<Transaction> txn; |
2458 | 2 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
2459 | 2 | if (err != TxnErrorCode::TXN_OK) { |
2460 | 0 | LOG(WARNING) << "failed to create txn"; |
2461 | 0 | return -1; |
2462 | 0 | } |
2463 | 2 | err = txn->get(begin, end, &table_it); |
2464 | 2 | if (err != TxnErrorCode::TXN_OK) { |
2465 | 0 | LOG(WARNING) << "failed to get mow tablet job key, err=" << err; |
2466 | 0 | return -1; |
2467 | 0 | } |
2468 | 4 | while (table_it->has_next() && !stopped()) { |
2469 | 2 | auto [k, v] = table_it->next(); |
2470 | 2 | std::string_view k1 = k; |
2471 | 2 | k1.remove_prefix(1); |
2472 | 2 | std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> out; |
2473 | 2 | decode_key(&k1, &out); |
2474 | 2 | int64_t table_version = -1; |
2475 | | // 0x01 "version" ${instance_id} "table" ${db_id} ${tbl_id} |
2476 | 2 | if (!txn->decode_atomic_int(v, &table_version)) { |
2477 | 0 | LOG(WARNING) << "malformed table version value"; |
2478 | 0 | return -1; |
2479 | 0 | } |
2480 | 2 | auto table_id = std::get<int64_t>(std::get<0>(out[4])); |
2481 | 2 | auto db_id = std::get<int64_t>(std::get<0>(out[3])); |
2482 | 2 | std::string partition_version_key_begin = |
2483 | 2 | partition_version_key({instance_id_, db_id, table_id, 0}); |
2484 | 2 | std::string partition_version_key_end = |
2485 | 2 | partition_version_key({instance_id_, db_id, table_id, INT64_MAX}); |
2486 | 2 | VersionPB partition_version_pb; |
2487 | | |
2488 | 2 | std::unique_ptr<RangeGetIterator> part_it; |
2489 | 4 | while (part_it == nullptr /* may be not init */ || (part_it->more() && !stopped())) { |
2490 | 2 | std::unique_ptr<Transaction> txn; |
2491 | 2 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
2492 | 2 | if (err != TxnErrorCode::TXN_OK) { |
2493 | 0 | LOG(WARNING) << "failed to create txn"; |
2494 | 0 | return -1; |
2495 | 0 | } |
2496 | 2 | err = txn->get(partition_version_key_begin, partition_version_key_end, &part_it); |
2497 | 2 | if (err != TxnErrorCode::TXN_OK) { |
2498 | 0 | LOG(WARNING) << "failed to get mow tablet job key, err=" << err; |
2499 | 0 | return -1; |
2500 | 0 | } |
2501 | 13 | while (part_it->has_next() && !stopped()) { |
2502 | 11 | auto [k, v] = part_it->next(); |
2503 | | // 0x01 "version" ${instance_id} "partition" ${db_id} ${tbl_id} ${partition_id} |
2504 | 11 | std::string_view k1 = k; |
2505 | 11 | k1.remove_prefix(1); |
2506 | 11 | std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> out; |
2507 | 11 | decode_key(&k1, &out); |
2508 | 11 | if (!partition_version_pb.ParseFromArray(v.data(), v.size())) [[unlikely]] { |
2509 | 0 | LOG(WARNING) << "failed to parse partition VersionPB"; |
2510 | 0 | return -1; |
2511 | 0 | } |
2512 | 11 | auto partition_id = std::get<int64_t>(std::get<0>(out[5])); |
2513 | 11 | int64_t partition_version = partition_version_pb.version(); |
2514 | 11 | if (table_version < partition_version) { |
2515 | 3 | check_res = false; |
2516 | 3 | LOG(WARNING) |
2517 | 3 | << "table version is less than partition version," |
2518 | 3 | << " table_id: " << table_id << "tablet_version: " << table_version |
2519 | 3 | << " partition_id: " << partition_id |
2520 | 3 | << " partition_version: " << partition_version; |
2521 | 3 | } |
2522 | 11 | } |
2523 | 2 | partition_version_key_begin = part_it->next_begin_key(); |
2524 | 2 | } |
2525 | 2 | } |
2526 | 2 | begin = table_it->next_begin_key(); // Update to next smallest key for iteration |
2527 | 2 | } |
2528 | 2 | return check_res ? 0 : -1; |
2529 | 2 | } |
2530 | | |
2531 | 1 | int InstanceChecker::do_restore_job_check() { |
2532 | 1 | int64_t num_prepared = 0; |
2533 | 1 | int64_t num_committed = 0; |
2534 | 1 | int64_t num_dropped = 0; |
2535 | 1 | int64_t num_completed = 0; |
2536 | 1 | int64_t num_recycling = 0; |
2537 | 1 | int64_t num_cost_many_time = 0; |
2538 | 1 | const int64_t COST_MANY_THRESHOLD = 3600; |
2539 | | |
2540 | 1 | using namespace std::chrono; |
2541 | 1 | auto start_time = steady_clock::now(); |
2542 | 1 | DORIS_CLOUD_DEFER { |
2543 | 1 | g_bvar_checker_restore_job_prepared_state.put(instance_id_, num_prepared); |
2544 | 1 | g_bvar_checker_restore_job_committed_state.put(instance_id_, num_committed); |
2545 | 1 | g_bvar_checker_restore_job_dropped_state.put(instance_id_, num_dropped); |
2546 | 1 | g_bvar_checker_restore_job_completed_state.put(instance_id_, num_completed); |
2547 | 1 | g_bvar_checker_restore_job_recycling_state.put(instance_id_, num_recycling); |
2548 | 1 | g_bvar_checker_restore_job_cost_many_time.put(instance_id_, num_cost_many_time); |
2549 | 1 | auto cost_ms = |
2550 | 1 | duration_cast<std::chrono::milliseconds>(steady_clock::now() - start_time).count(); |
2551 | 1 | LOG(INFO) << "check instance restore jobs finished, cost=" << cost_ms |
2552 | 1 | << "ms. instance_id=" << instance_id_ << " num_prepared=" << num_prepared |
2553 | 1 | << " num_committed=" << num_committed << " num_dropped=" << num_dropped |
2554 | 1 | << " num_completed=" << num_completed << " num_recycling=" << num_recycling |
2555 | 1 | << " num_cost_many_time=" << num_cost_many_time; |
2556 | 1 | }; |
2557 | | |
2558 | 1 | LOG_INFO("begin to check restore jobs").tag("instance_id", instance_id_); |
2559 | | |
2560 | 1 | JobRestoreTabletKeyInfo restore_job_key_info0 {instance_id_, 0}; |
2561 | 1 | JobRestoreTabletKeyInfo restore_job_key_info1 {instance_id_, INT64_MAX}; |
2562 | 1 | std::string begin; |
2563 | 1 | std::string end; |
2564 | 1 | job_restore_tablet_key(restore_job_key_info0, &begin); |
2565 | 1 | job_restore_tablet_key(restore_job_key_info1, &end); |
2566 | 1 | std::unique_ptr<RangeGetIterator> it; |
2567 | 2 | while (it == nullptr /* may be not init */ || (it->more() && !stopped())) { |
2568 | 1 | std::unique_ptr<Transaction> txn; |
2569 | 1 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
2570 | 1 | if (err != TxnErrorCode::TXN_OK) { |
2571 | 0 | LOG(WARNING) << "failed to create txn"; |
2572 | 0 | return -1; |
2573 | 0 | } |
2574 | 1 | err = txn->get(begin, end, &it); |
2575 | 1 | if (err != TxnErrorCode::TXN_OK) { |
2576 | 0 | LOG(WARNING) << "failed to get mow tablet job key, err=" << err; |
2577 | 0 | return -1; |
2578 | 0 | } |
2579 | | |
2580 | 1 | if (!it->has_next()) { |
2581 | 0 | break; |
2582 | 0 | } |
2583 | 3 | while (it->has_next()) { |
2584 | 3 | auto [k, v] = it->next(); |
2585 | 3 | RestoreJobCloudPB restore_job_pb; |
2586 | 3 | if (!restore_job_pb.ParseFromArray(v.data(), v.size())) { |
2587 | 0 | LOG_WARNING("malformed restore job value").tag("key", hex(k)); |
2588 | 0 | return -1; |
2589 | 0 | } |
2590 | | |
2591 | 3 | switch (restore_job_pb.state()) { |
2592 | 1 | case RestoreJobCloudPB::PREPARED: |
2593 | 1 | ++num_prepared; |
2594 | 1 | break; |
2595 | 1 | case RestoreJobCloudPB::COMMITTED: |
2596 | 1 | ++num_committed; |
2597 | 1 | break; |
2598 | 0 | case RestoreJobCloudPB::DROPPED: |
2599 | 0 | ++num_dropped; |
2600 | 0 | break; |
2601 | 1 | case RestoreJobCloudPB::COMPLETED: |
2602 | 1 | ++num_completed; |
2603 | 1 | break; |
2604 | 0 | case RestoreJobCloudPB::RECYCLING: |
2605 | 0 | ++num_recycling; |
2606 | 0 | break; |
2607 | 0 | default: |
2608 | 0 | break; |
2609 | 3 | } |
2610 | | |
2611 | 3 | int64_t current_time = ::time(nullptr); |
2612 | 3 | if ((restore_job_pb.state() == RestoreJobCloudPB::PREPARED || |
2613 | 3 | restore_job_pb.state() == RestoreJobCloudPB::COMMITTED) && |
2614 | 3 | current_time > restore_job_pb.ctime_s() + COST_MANY_THRESHOLD) { |
2615 | | // restore job run more than 1 hour |
2616 | 1 | ++num_cost_many_time; |
2617 | 1 | LOG_WARNING("restore job cost too many time") |
2618 | 1 | .tag("key", hex(k)) |
2619 | 1 | .tag("tablet_id", restore_job_pb.tablet_id()) |
2620 | 1 | .tag("state", restore_job_pb.state()) |
2621 | 1 | .tag("ctime_s", restore_job_pb.ctime_s()) |
2622 | 1 | .tag("mtime_s", restore_job_pb.mtime_s()); |
2623 | 1 | } |
2624 | | |
2625 | 3 | if (!it->has_next()) { |
2626 | 1 | begin = k; |
2627 | 1 | begin.push_back('\x00'); // Update to next smallest key for iteration |
2628 | 1 | break; |
2629 | 1 | } |
2630 | 3 | } |
2631 | 1 | } |
2632 | 1 | return 0; |
2633 | 1 | } |
2634 | | |
2635 | 3 | int InstanceChecker::check_txn_info_key(std::string_view key, std::string_view value) { |
2636 | 3 | std::unordered_map<int64_t, std::string> txn_info_; |
2637 | 3 | TxnLabelPB txn_label_pb; |
2638 | | |
2639 | 6 | auto handle_check_txn_label_key = [&](std::string_view key, std::string_view value) -> int { |
2640 | 6 | TxnInfoPB txn_info_pb; |
2641 | 6 | std::string_view k1 = key; |
2642 | 6 | k1.remove_prefix(1); |
2643 | 6 | std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> out; |
2644 | 6 | decode_key(&k1, &out); |
2645 | | // 0x01 "txn" ${instance_id} "txn_info" ${db_id} ${txn_id} |
2646 | 6 | if (!txn_info_pb.ParseFromArray(value.data(), value.size())) { |
2647 | 0 | LOG(WARNING) << "failed to parse TxnInfoPB"; |
2648 | 0 | return -1; |
2649 | 0 | } |
2650 | 6 | auto txn_id = std::get<int64_t>(std::get<0>(out[4])); |
2651 | 6 | auto it = txn_info_.find(txn_id); |
2652 | 6 | if (it == txn_info_.end()) { |
2653 | 0 | return 0; |
2654 | 6 | } else { |
2655 | 6 | if (it->second != txn_info_pb.label()) { |
2656 | 1 | LOG(WARNING) << "txn_info_pb's txn_label not same with txn_label_pb's txn_label," |
2657 | 1 | << " txn_info_pb's txn_label: " << txn_info_pb.label() |
2658 | 1 | << " txn_label_pb meta: " << txn_label_pb.ShortDebugString(); |
2659 | 1 | return 1; |
2660 | 1 | } |
2661 | 6 | } |
2662 | 5 | return 0; |
2663 | 6 | }; |
2664 | 3 | std::string_view k1 = key; |
2665 | 3 | k1.remove_prefix(1); |
2666 | 3 | std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> out; |
2667 | 3 | decode_key(&k1, &out); |
2668 | | // 0x01 "txn" ${instance_id} "txn_label" ${db_id} ${label} |
2669 | 3 | if (!txn_label_pb.ParseFromArray(value.data(), value.size() - VERSION_STAMP_LEN)) { |
2670 | 1 | LOG(WARNING) << "failed to parse TxnLabelPB"; |
2671 | 1 | return -1; |
2672 | 1 | } |
2673 | 2 | auto db_id = std::get<int64_t>(std::get<0>(out[3])); |
2674 | 2 | auto label = std::get<std::string>(std::get<0>(out[4])); |
2675 | | // txn_id -> txn_label |
2676 | 6 | for (const auto& txn_id : txn_label_pb.txn_ids()) { |
2677 | 6 | txn_info_.insert({txn_id, label}); |
2678 | 6 | } |
2679 | 2 | std::string txn_info_key_begin = txn_info_key({instance_id_, db_id, 0}); |
2680 | 2 | std::string txn_info_key_end = txn_info_key({instance_id_, db_id, INT64_MAX}); |
2681 | 2 | return scan_and_handle_kv(txn_info_key_begin, txn_info_key_end, |
2682 | 6 | [&](std::string_view k, std::string_view v) -> int { |
2683 | 6 | return handle_check_txn_label_key(k, v); |
2684 | 6 | }); |
2685 | 3 | } |
2686 | | |
2687 | 6 | int InstanceChecker::check_txn_label_key(std::string_view key, std::string_view value) { |
2688 | 6 | TxnInfoPB txn_info_pb; |
2689 | 6 | std::string_view k1 = key; |
2690 | 6 | k1.remove_prefix(1); |
2691 | 6 | std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> out; |
2692 | 6 | decode_key(&k1, &out); |
2693 | | // 0x01 "txn" ${instance_id} "txn_info" ${db_id} ${txn_id} |
2694 | 6 | if (!txn_info_pb.ParseFromArray(value.data(), value.size())) { |
2695 | 1 | LOG(WARNING) << "failed to parse TxnInfoPB"; |
2696 | 1 | return -1; |
2697 | 1 | } |
2698 | 5 | auto txn_id = std::get<int64_t>(std::get<0>(out[4])); |
2699 | 5 | auto db_id = std::get<int64_t>(std::get<0>(out[3])); |
2700 | 5 | auto label = txn_info_pb.label(); |
2701 | 5 | std::string txn_label = txn_label_key({instance_id_, db_id, label}); |
2702 | 5 | std::string txn_label_val; |
2703 | 5 | TxnLabelPB txn_label_pb; |
2704 | 5 | std::unique_ptr<Transaction> txn; |
2705 | 5 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
2706 | 5 | if (err != TxnErrorCode::TXN_OK) { |
2707 | 0 | LOG(WARNING) << "failed to init txn"; |
2708 | 0 | return -1; |
2709 | 0 | } |
2710 | 5 | if (txn->get(txn_label, &txn_label_val) != TxnErrorCode::TXN_OK) { |
2711 | 1 | LOG(WARNING) << "failed to get txn label key, key=" << hex(txn_label); |
2712 | 1 | return -1; |
2713 | 1 | } |
2714 | 4 | txn_label_pb.ParseFromString(txn_label_val); |
2715 | 4 | auto txn_ids = txn_label_pb.txn_ids(); |
2716 | 4 | if (!std::count(txn_ids.begin(), txn_ids.end(), txn_id)) { |
2717 | | // clang-format off txn_info_pb |
2718 | 1 | LOG(WARNING) << "txn_info_pb's txn_id not found in txn_label_pb info," |
2719 | 1 | << " txn_id: " << txn_id |
2720 | 1 | << " txn_label_pb meta: " << txn_label_pb.ShortDebugString(); |
2721 | | // clang-format on |
2722 | 1 | return 1; |
2723 | 1 | } |
2724 | 3 | return 0; |
2725 | 4 | } |
2726 | | |
2727 | 4 | int InstanceChecker::check_txn_index_key(std::string_view key, std::string_view value) { |
2728 | 4 | TxnInfoPB txn_info_pb; |
2729 | 4 | std::string_view k1 = key; |
2730 | 4 | k1.remove_prefix(1); |
2731 | 4 | std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> out; |
2732 | 4 | decode_key(&k1, &out); |
2733 | | // 0x01 "txn" ${instance_id} "txn_info" ${db_id} ${txn_id} |
2734 | 4 | if (!txn_info_pb.ParseFromArray(value.data(), value.size())) { |
2735 | 1 | LOG(WARNING) << "failed to parse TxnInfoPB"; |
2736 | 1 | return -1; |
2737 | 1 | } |
2738 | 3 | auto txn_id = std::get<int64_t>(std::get<0>(out[4])); |
2739 | 3 | auto db_id = std::get<int64_t>(std::get<0>(out[3])); |
2740 | | /// get tablet id |
2741 | 3 | std::string txn_index = txn_index_key({instance_id_, txn_id}); |
2742 | 3 | std::string txn_index_val; |
2743 | 3 | TxnIndexPB txn_index_pb; |
2744 | 3 | std::unique_ptr<Transaction> txn; |
2745 | 3 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
2746 | 3 | if (err != TxnErrorCode::TXN_OK) { |
2747 | 0 | LOG(WARNING) << "failed to init txn"; |
2748 | 0 | return -1; |
2749 | 0 | } |
2750 | 3 | if (txn->get(txn_index, &txn_index_val) != TxnErrorCode::TXN_OK) { |
2751 | 1 | LOG(WARNING) << "failed to get txn label key, key=" << hex(txn_index); |
2752 | 1 | return -1; |
2753 | 1 | } |
2754 | 2 | txn_index_pb.ParseFromString(txn_index_val); |
2755 | 2 | if (txn_index_pb.tablet_index().db_id() != db_id) { |
2756 | | // clang-format off txn_info_pb |
2757 | 1 | LOG(WARNING) << "txn_index_pb's db_id not same with txn_info_pb's db_id," |
2758 | 1 | << " txn_index_pb meta: " << txn_index_pb.ShortDebugString() |
2759 | 1 | << " txn_info_pb meta: " << txn_info_pb.ShortDebugString(); |
2760 | | // clang-format on |
2761 | 1 | return 1; |
2762 | 1 | } |
2763 | 1 | return 0; |
2764 | 2 | } |
2765 | | |
2766 | 3 | int InstanceChecker::check_txn_running_key(std::string_view key, std::string_view value) { |
2767 | 3 | TxnRunningPB txn_running_pb; |
2768 | 3 | int64_t current_time = |
2769 | 3 | duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); |
2770 | 3 | if (!txn_running_pb.ParseFromArray(value.data(), value.size())) { |
2771 | 1 | LOG(WARNING) << "failed to parse TxnRunningPB"; |
2772 | 1 | return -1; |
2773 | 1 | } |
2774 | 2 | if (txn_running_pb.timeout_time() <= current_time) { |
2775 | 1 | LOG(WARNING) << "txn_running_pb.timeout_time() is less than current_time," |
2776 | 1 | << " but txn_running_key exists, " |
2777 | 1 | << " txn_running_pb meta: " << txn_running_pb.ShortDebugString(); |
2778 | 1 | return 1; |
2779 | 1 | } |
2780 | 1 | return 0; |
2781 | 2 | } |
2782 | | |
2783 | 0 | int InstanceChecker::do_txn_key_check() { |
2784 | 0 | int ret = 0; |
2785 | | |
2786 | | // check txn info key depend on txn label key |
2787 | 0 | std::string begin = txn_label_key({instance_id_, 0, ""}); |
2788 | 0 | std::string end = txn_label_key({instance_id_, INT64_MAX, ""}); |
2789 | 0 | int64_t num_scanned = 0; |
2790 | 0 | int64_t num_abnormal = 0; |
2791 | 0 | LOG(INFO) << "begin check txn_label_key and txn_info_key"; |
2792 | 0 | ret = scan_and_handle_kv(begin, end, [&, this](std::string_view k, std::string_view v) -> int { |
2793 | 0 | num_scanned++; |
2794 | 0 | int ret = check_txn_info_key(k, v); |
2795 | 0 | if (ret == 1) { |
2796 | 0 | num_abnormal++; |
2797 | 0 | } |
2798 | 0 | return ret; |
2799 | 0 | }); |
2800 | |
|
2801 | 0 | if (ret == 1) { |
2802 | 0 | LOG(WARNING) << "failed to check txn_info_key depending on txn_label_key, num_scanned=" |
2803 | 0 | << num_scanned << ", num_abnormal=" << num_abnormal; |
2804 | 0 | return 1; |
2805 | 0 | } else if (ret == -1) { |
2806 | 0 | LOG(WARNING) << "failed to check txn label key and txn info key"; |
2807 | 0 | return -1; |
2808 | 0 | } |
2809 | | |
2810 | | // check txn label key depend on txn info key |
2811 | 0 | begin = txn_info_key({instance_id_, 0, 0}); |
2812 | 0 | end = txn_info_key({instance_id_, INT64_MAX, 0}); |
2813 | 0 | num_scanned = 0; |
2814 | 0 | num_abnormal = 0; |
2815 | 0 | LOG(INFO) << "begin check txn_label_key and txn_info_key"; |
2816 | 0 | ret = scan_and_handle_kv(begin, end, [&, this](std::string_view k, std::string_view v) -> int { |
2817 | 0 | num_scanned++; |
2818 | 0 | int ret = check_txn_label_key(k, v); |
2819 | 0 | if (ret == 1) { |
2820 | 0 | num_abnormal++; |
2821 | 0 | } |
2822 | 0 | return ret; |
2823 | 0 | }); |
2824 | 0 | if (ret == 1) { |
2825 | 0 | LOG(WARNING) << "failed to check txn_label_key depending on txn_info_key, num_scanned=" |
2826 | 0 | << num_scanned << ", num_abnormal=" << num_abnormal; |
2827 | 0 | return 1; |
2828 | 0 | } else if (ret == -1) { |
2829 | 0 | LOG(WARNING) << "failed to inverted check txn label key and txn info key"; |
2830 | 0 | return -1; |
2831 | 0 | } |
2832 | 0 | LOG(INFO) << "finish check txn_label_key and txn_info_key, num_scanned=" << num_scanned |
2833 | 0 | << ", num_abnormal=" << num_abnormal; |
2834 | | |
2835 | | // check txn index key depend on txn info key |
2836 | 0 | begin = txn_info_key({instance_id_, 0, 0}); |
2837 | 0 | end = txn_info_key({instance_id_, INT64_MAX, 0}); |
2838 | 0 | num_scanned = 0; |
2839 | 0 | num_abnormal = 0; |
2840 | 0 | LOG(INFO) << "begin check txn_index_key and txn_info_key"; |
2841 | 0 | ret = scan_and_handle_kv(begin, end, [&, this](std::string_view k, std::string_view v) -> int { |
2842 | 0 | num_scanned++; |
2843 | 0 | int ret = check_txn_index_key(k, v); |
2844 | 0 | if (ret == 1) { |
2845 | 0 | num_abnormal++; |
2846 | 0 | } |
2847 | 0 | return ret; |
2848 | 0 | }); |
2849 | 0 | if (ret == 1) { |
2850 | 0 | LOG(WARNING) << "failed to check txn_idx_key depending on txn_info_key, num_scanned=" |
2851 | 0 | << num_scanned << ", num_abnormal=" << num_abnormal; |
2852 | 0 | return 1; |
2853 | 0 | } else if (ret == -1) { |
2854 | 0 | LOG(WARNING) << "failed to check txn index key"; |
2855 | 0 | return -1; |
2856 | 0 | } |
2857 | 0 | LOG(INFO) << "finish check txn_index_key and txn_info_key, num_scanned=" << num_scanned |
2858 | 0 | << ", num_abnormal=" << num_abnormal; |
2859 | | |
2860 | | // check txn running key |
2861 | 0 | begin = txn_running_key({instance_id_, 0, 0}); |
2862 | 0 | end = txn_running_key({instance_id_, INT64_MAX, 0}); |
2863 | 0 | num_scanned = 0; |
2864 | 0 | num_abnormal = 0; |
2865 | 0 | LOG(INFO) << "begin check txn_running_key"; |
2866 | 0 | ret = scan_and_handle_kv(begin, end, [&, this](std::string_view k, std::string_view v) -> int { |
2867 | 0 | num_scanned++; |
2868 | 0 | int ret = check_txn_running_key(k, v); |
2869 | 0 | if (ret == 1) { |
2870 | 0 | num_abnormal++; |
2871 | 0 | } |
2872 | 0 | return ret; |
2873 | 0 | }); |
2874 | 0 | if (ret == 1) { |
2875 | 0 | LOG(WARNING) << "failed to check txn_running_key, num_scanned=" << num_scanned |
2876 | 0 | << ", num_abnormal=" << num_abnormal; |
2877 | 0 | return 1; |
2878 | 0 | } else if (ret == -1) { |
2879 | 0 | LOG(WARNING) << "failed to check txn running key"; |
2880 | 0 | return -1; |
2881 | 0 | } |
2882 | 0 | LOG(INFO) << "finish check txn_running_key, num_scanned=" << num_scanned |
2883 | 0 | << ", num_abnormal=" << num_abnormal; |
2884 | 0 | return 0; |
2885 | 0 | } |
2886 | | |
2887 | 2 | int InstanceChecker::check_meta_tmp_rowset_key(std::string_view key, std::string_view value) { |
2888 | 2 | TxnInfoPB txn_info_pb; |
2889 | 2 | std::string_view k1 = key; |
2890 | 2 | k1.remove_prefix(1); |
2891 | 2 | std::vector<std::tuple<std::variant<int64_t, std::string>, int, int>> out; |
2892 | 2 | decode_key(&k1, &out); |
2893 | | // 0x01 "txn" ${instance_id} "txn_info" ${db_id} ${txn_id} |
2894 | 2 | if (!txn_info_pb.ParseFromArray(value.data(), value.size())) { |
2895 | 0 | LOG(WARNING) << "failed to parse TxnInfoPB"; |
2896 | 0 | return -1; |
2897 | 0 | } |
2898 | | /// get tablet id |
2899 | 2 | auto txn_id = std::get<int64_t>(std::get<0>(out[4])); |
2900 | 2 | std::string txn_index = txn_index_key({instance_id_, txn_id}); |
2901 | 2 | std::string txn_index_val; |
2902 | 2 | TxnIndexPB txn_index_pb; |
2903 | 2 | std::unique_ptr<Transaction> txn; |
2904 | 2 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
2905 | 2 | if (err != TxnErrorCode::TXN_OK) { |
2906 | 0 | LOG(WARNING) << "failed to init txn"; |
2907 | 0 | return -1; |
2908 | 0 | } |
2909 | 2 | if (txn->get(txn_index, &txn_index_val) != TxnErrorCode::TXN_OK) { |
2910 | 0 | LOG(WARNING) << "failed to get txn index key, key=" << txn_index; |
2911 | 0 | return -1; |
2912 | 0 | } |
2913 | 2 | txn_index_pb.ParseFromString(txn_index_val); |
2914 | 2 | auto tablet_id = txn_index_pb.tablet_index().tablet_id(); |
2915 | 2 | std::string meta_tmp_rowset_key = meta_rowset_tmp_key({instance_id_, txn_id, tablet_id}); |
2916 | 2 | int is_key_exist = key_exist(txn_kv_.get(), meta_tmp_rowset_key); |
2917 | 2 | if (is_key_exist == 1) { |
2918 | 0 | if (txn_info_pb.status() != TxnStatusPB::TXN_STATUS_VISIBLE) { |
2919 | | // clang-format off |
2920 | 0 | LOG(INFO) << "meta tmp rowset key not exist but txn status != TXN_STATUS_VISIBLE" |
2921 | 0 | << "meta tmp rowset key=" << meta_tmp_rowset_key |
2922 | 0 | << "txn_info=" << txn_info_pb.ShortDebugString(); |
2923 | | // clang-format on |
2924 | 0 | return 1; |
2925 | 0 | } |
2926 | 2 | } else if (is_key_exist == 0) { |
2927 | 2 | if (txn_info_pb.status() != TxnStatusPB::TXN_STATUS_PREPARED) { |
2928 | | // clang-format off |
2929 | 1 | LOG(INFO) << "meta tmp rowset key exist but txn status != TXN_STATUS_PREPARED" |
2930 | 1 | << "meta tmp rowset key=" << meta_tmp_rowset_key |
2931 | 1 | << "txn_info=" << txn_info_pb.ShortDebugString(); |
2932 | | // clang-format on |
2933 | 1 | return 1; |
2934 | 1 | } |
2935 | 2 | } else { |
2936 | 0 | LOG(WARNING) << "failed to get key, key=" << meta_tmp_rowset_key; |
2937 | 0 | return -1; |
2938 | 0 | } |
2939 | 1 | return 0; |
2940 | 2 | } |
2941 | | |
2942 | 2 | int InstanceChecker::check_meta_rowset_key(std::string_view key, std::string_view value) { |
2943 | 2 | RowsetMetaCloudPB meta_rowset_pb; |
2944 | 2 | if (!meta_rowset_pb.ParseFromArray(value.data(), value.size())) { |
2945 | 0 | LOG(WARNING) << "failed to parse RowsetMetaCloudPB"; |
2946 | 0 | return -1; |
2947 | 0 | } |
2948 | 2 | std::string tablet_index_key = meta_tablet_idx_key({instance_id_, meta_rowset_pb.tablet_id()}); |
2949 | 2 | if (key_exist(txn_kv_.get(), tablet_index_key) == 1) { |
2950 | 1 | LOG(WARNING) << "rowset's tablet id not found in fdb" |
2951 | 1 | << "tablet_index_key: " << tablet_index_key |
2952 | 1 | << "rowset meta: " << meta_rowset_pb.ShortDebugString(); |
2953 | 1 | return 1; |
2954 | 1 | } |
2955 | 1 | return 0; |
2956 | 2 | } |
2957 | | |
2958 | 0 | int InstanceChecker::do_meta_rowset_key_check() { |
2959 | 0 | int ret = 0; |
2960 | |
|
2961 | 0 | std::string begin = meta_rowset_key({instance_id_, 0, 0}); |
2962 | 0 | std::string end = meta_rowset_key({instance_id_, INT64_MAX, 0}); |
2963 | 0 | int64_t num_scanned = 0; |
2964 | 0 | int64_t num_loss = 0; |
2965 | |
|
2966 | 0 | ret = scan_and_handle_kv(begin, end, [&](std::string_view k, std::string_view v) { |
2967 | 0 | num_scanned++; |
2968 | 0 | int ret = check_meta_rowset_key(k, v); |
2969 | 0 | if (ret == 1) { |
2970 | 0 | num_loss++; |
2971 | 0 | } |
2972 | 0 | return ret; |
2973 | 0 | }); |
2974 | 0 | if (ret == -1) { |
2975 | 0 | LOG(WARNING) << "failed to check meta rowset key,"; |
2976 | 0 | return -1; |
2977 | 0 | } else if (ret == 1) { |
2978 | 0 | LOG(WARNING) << "meta rowset key may be loss, num_scanned=" << num_scanned |
2979 | 0 | << ", num_loss=" << num_loss; |
2980 | 0 | } |
2981 | 0 | LOG(INFO) << "meta rowset key check finish, num_scanned=" << num_scanned |
2982 | 0 | << ", num_loss=" << num_loss; |
2983 | |
|
2984 | 0 | begin = txn_info_key({instance_id_, 0, 0}); |
2985 | 0 | end = txn_info_key({instance_id_, INT64_MAX, 0}); |
2986 | 0 | num_scanned = 0; |
2987 | 0 | num_loss = 0; |
2988 | |
|
2989 | 0 | ret = scan_and_handle_kv(begin, end, [&](std::string_view k, std::string_view v) { |
2990 | 0 | num_scanned++; |
2991 | 0 | int ret = check_meta_tmp_rowset_key(k, v); |
2992 | 0 | if (ret == 1) { |
2993 | 0 | num_loss++; |
2994 | 0 | } |
2995 | 0 | return ret; |
2996 | 0 | }); |
2997 | 0 | if (ret == -1) { |
2998 | 0 | LOG(WARNING) << "failed to check tmp meta rowset key"; |
2999 | 0 | return -1; |
3000 | 0 | } else if (ret == 1) { |
3001 | 0 | LOG(WARNING) << "meta tmp rowset key may be loss, num_scanned=" << num_scanned |
3002 | 0 | << ", num_loss=" << num_loss; |
3003 | 0 | } |
3004 | 0 | LOG(INFO) << "meta tmp rowset key check finish, num_scanned=" << num_scanned |
3005 | 0 | << ", num_loss=" << num_loss; |
3006 | |
|
3007 | 0 | return ret; |
3008 | 0 | } |
3009 | | |
3010 | 0 | StorageVaultAccessor* InstanceChecker::get_accessor(const std::string& id) { |
3011 | 0 | auto it = accessor_map_.find(id); |
3012 | 0 | if (it == accessor_map_.end()) { |
3013 | 0 | return nullptr; |
3014 | 0 | } |
3015 | 0 | return it->second.get(); |
3016 | 0 | } |
3017 | | |
3018 | 0 | void InstanceChecker::get_all_accessor(std::vector<StorageVaultAccessor*>* accessors) { |
3019 | 0 | for (const auto& [_, accessor] : accessor_map_) { |
3020 | 0 | accessors->push_back(accessor.get()); |
3021 | 0 | } |
3022 | 0 | } |
3023 | | |
3024 | 0 | int InstanceChecker::do_packed_file_check() { |
3025 | 0 | LOG(INFO) << "begin to check packed files, instance_id=" << instance_id_; |
3026 | 0 | int check_ret = 0; |
3027 | 0 | long num_scanned_rowsets = 0; |
3028 | 0 | long num_scanned_packed_files = 0; |
3029 | 0 | long num_packed_file_loss = 0; |
3030 | 0 | long num_packed_file_leak = 0; |
3031 | 0 | long num_ref_count_mismatch = 0; |
3032 | 0 | long num_small_file_ref_mismatch = 0; |
3033 | 0 | using namespace std::chrono; |
3034 | 0 | auto start_time = steady_clock::now(); |
3035 | 0 | DORIS_CLOUD_DEFER { |
3036 | 0 | auto cost = duration<float>(steady_clock::now() - start_time).count(); |
3037 | 0 | LOG(INFO) << "check packed files finished, cost=" << cost |
3038 | 0 | << "s. instance_id=" << instance_id_ |
3039 | 0 | << " num_scanned_rowsets=" << num_scanned_rowsets |
3040 | 0 | << " num_scanned_packed_files=" << num_scanned_packed_files |
3041 | 0 | << " num_packed_file_loss=" << num_packed_file_loss |
3042 | 0 | << " num_packed_file_leak=" << num_packed_file_leak |
3043 | 0 | << " num_ref_count_mismatch=" << num_ref_count_mismatch |
3044 | 0 | << " num_small_file_ref_mismatch=" << num_small_file_ref_mismatch; |
3045 | 0 | }; |
3046 | | |
3047 | | // Map to track expected reference count for each packed file |
3048 | | // packed_file_path -> expected_ref_count (from rowset metas) |
3049 | 0 | std::unordered_map<std::string, int64_t> expected_ref_counts; |
3050 | | // Map to track small files referenced in packed files |
3051 | | // packed_file_path -> set of small_file_paths |
3052 | 0 | std::unordered_map<std::string, std::unordered_set<std::string>> packed_file_small_files; |
3053 | | |
3054 | | // Step 1: Scan all rowset metas to collect packed_slice_locations references |
3055 | | // Use efficient range scan instead of iterating through each tablet_id |
3056 | 0 | auto collect_packed_refs = [&](const doris::RowsetMetaCloudPB& rs_meta) { |
3057 | 0 | const auto& index_map = rs_meta.packed_slice_locations(); |
3058 | 0 | for (const auto& [small_file_path, index_pb] : index_map) { |
3059 | 0 | if (!index_pb.has_packed_file_path() || index_pb.packed_file_path().empty()) { |
3060 | 0 | continue; |
3061 | 0 | } |
3062 | 0 | const std::string& packed_file_path = index_pb.packed_file_path(); |
3063 | 0 | expected_ref_counts[packed_file_path]++; |
3064 | 0 | packed_file_small_files[packed_file_path].insert(small_file_path); |
3065 | 0 | } |
3066 | 0 | }; |
3067 | |
|
3068 | 0 | { |
3069 | 0 | std::string start_key = meta_rowset_key({instance_id_, 0, 0}); |
3070 | 0 | std::string end_key = meta_rowset_key({instance_id_, INT64_MAX, 0}); |
3071 | |
|
3072 | 0 | std::unique_ptr<RangeGetIterator> it; |
3073 | 0 | while (it == nullptr /* may be not init */ || (it->more() && !stopped())) { |
3074 | 0 | if (stopped()) { |
3075 | 0 | return -1; |
3076 | 0 | } |
3077 | | |
3078 | 0 | std::unique_ptr<Transaction> txn; |
3079 | 0 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
3080 | 0 | if (err != TxnErrorCode::TXN_OK) { |
3081 | 0 | LOG(WARNING) << "failed to create txn for packed file check"; |
3082 | 0 | return -1; |
3083 | 0 | } |
3084 | | |
3085 | 0 | err = txn->get(start_key, end_key, &it); |
3086 | 0 | if (err != TxnErrorCode::TXN_OK) { |
3087 | 0 | LOG(WARNING) << "failed to scan rowset metas, err=" << err; |
3088 | 0 | check_ret = -1; |
3089 | 0 | break; |
3090 | 0 | } |
3091 | | |
3092 | 0 | while (it->has_next() && !stopped()) { |
3093 | 0 | auto [k, v] = it->next(); |
3094 | 0 | if (!it->has_next()) { |
3095 | 0 | start_key = k; |
3096 | 0 | } |
3097 | |
|
3098 | 0 | doris::RowsetMetaCloudPB rs_meta; |
3099 | 0 | if (!rs_meta.ParseFromArray(v.data(), v.size())) { |
3100 | 0 | LOG(WARNING) << "malformed rowset meta, key=" << hex(k); |
3101 | 0 | check_ret = -1; |
3102 | 0 | continue; |
3103 | 0 | } |
3104 | | |
3105 | 0 | num_scanned_rowsets++; |
3106 | |
|
3107 | 0 | collect_packed_refs(rs_meta); |
3108 | 0 | } |
3109 | 0 | start_key.push_back('\x00'); // Update to next smallest key for iteration |
3110 | 0 | } |
3111 | 0 | } |
3112 | | |
3113 | | // Rowsets in recycle keys may still hold packed file references while ref count |
3114 | | // updates are pending, so include them when calculating expected references. |
3115 | 0 | { |
3116 | 0 | std::string start_key = recycle_rowset_key({instance_id_, 0, ""}); |
3117 | 0 | std::string end_key = recycle_rowset_key({instance_id_, INT64_MAX, "\xff"}); |
3118 | |
|
3119 | 0 | std::unique_ptr<RangeGetIterator> it; |
3120 | 0 | while (it == nullptr /* may be not init */ || it->more()) { |
3121 | 0 | if (stopped()) { |
3122 | 0 | return -1; |
3123 | 0 | } |
3124 | 0 | std::unique_ptr<Transaction> txn; |
3125 | 0 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
3126 | 0 | if (err != TxnErrorCode::TXN_OK) { |
3127 | 0 | LOG(WARNING) << "failed to create txn for recycle rowset scan in packed file check"; |
3128 | 0 | return -1; |
3129 | 0 | } |
3130 | | |
3131 | 0 | err = txn->get(start_key, end_key, &it); |
3132 | 0 | if (err != TxnErrorCode::TXN_OK) { |
3133 | 0 | LOG(WARNING) << "failed to scan recycle rowset metas, err=" << err; |
3134 | 0 | check_ret = -1; |
3135 | 0 | break; |
3136 | 0 | } |
3137 | | |
3138 | 0 | while (it->has_next() && !stopped()) { |
3139 | 0 | auto [k, v] = it->next(); |
3140 | 0 | if (!it->has_next()) { |
3141 | 0 | start_key = k; |
3142 | 0 | } |
3143 | |
|
3144 | 0 | RecycleRowsetPB recycle_rowset; |
3145 | 0 | if (!recycle_rowset.ParseFromArray(v.data(), v.size())) { |
3146 | 0 | LOG(WARNING) << "malformed recycle rowset, key=" << hex(k); |
3147 | 0 | check_ret = -1; |
3148 | 0 | continue; |
3149 | 0 | } |
3150 | | |
3151 | 0 | if (!recycle_rowset.has_rowset_meta()) { |
3152 | 0 | continue; |
3153 | 0 | } |
3154 | | |
3155 | 0 | num_scanned_rowsets++; |
3156 | 0 | collect_packed_refs(recycle_rowset.rowset_meta()); |
3157 | 0 | } |
3158 | 0 | start_key.push_back('\x00'); // Update to next smallest key for iteration |
3159 | 0 | } |
3160 | 0 | } |
3161 | | |
3162 | | // Step 2: Scan all packed file metadata and verify |
3163 | | // Also collect all packed file paths from metadata for Step 3 |
3164 | | // Map: resource_id -> set of packed_file_paths |
3165 | 0 | std::unordered_map<std::string, std::unordered_set<std::string>> packed_files_in_metadata; |
3166 | 0 | std::string begin = packed_file_key({instance_id_, ""}); |
3167 | 0 | std::string end = packed_file_key({instance_id_, "\xff"}); |
3168 | 0 | std::string scan_begin = begin; |
3169 | |
|
3170 | 0 | while (true) { |
3171 | 0 | if (stopped()) { |
3172 | 0 | return -1; |
3173 | 0 | } |
3174 | | |
3175 | 0 | std::unique_ptr<Transaction> txn; |
3176 | 0 | TxnErrorCode err = txn_kv_->create_txn(&txn); |
3177 | 0 | if (err != TxnErrorCode::TXN_OK) { |
3178 | 0 | LOG(WARNING) << "failed to create txn for scanning packed files"; |
3179 | 0 | return -1; |
3180 | 0 | } |
3181 | | |
3182 | 0 | std::unique_ptr<RangeGetIterator> it; |
3183 | 0 | err = txn->get(scan_begin, end, &it); |
3184 | 0 | if (err != TxnErrorCode::TXN_OK) { |
3185 | 0 | LOG(WARNING) << "failed to scan packed file keys, err=" << err; |
3186 | 0 | return -1; |
3187 | 0 | } |
3188 | 0 | if (!it->has_next()) { |
3189 | 0 | break; |
3190 | 0 | } |
3191 | | |
3192 | 0 | std::string last_key; |
3193 | 0 | while (it->has_next()) { |
3194 | 0 | auto [k, v] = it->next(); |
3195 | 0 | last_key.assign(k.data(), k.size()); |
3196 | 0 | num_scanned_packed_files++; |
3197 | |
|
3198 | 0 | std::string packed_file_path; |
3199 | 0 | if (!InstanceRecycler::decode_packed_file_key(k, &packed_file_path)) { |
3200 | 0 | LOG(WARNING) << "failed to decode packed file key, key=" << hex(k); |
3201 | 0 | check_ret = -1; |
3202 | 0 | continue; |
3203 | 0 | } |
3204 | | |
3205 | 0 | cloud::PackedFileInfoPB packed_info; |
3206 | 0 | if (!packed_info.ParseFromArray(v.data(), v.size())) { |
3207 | 0 | LOG(WARNING) << "failed to parse packed file info, packed_file_path=" |
3208 | 0 | << packed_file_path; |
3209 | 0 | check_ret = -1; |
3210 | 0 | continue; |
3211 | 0 | } |
3212 | | |
3213 | | // Step 2.1: Verify packed file exists in storage |
3214 | 0 | if (!packed_info.resource_id().empty()) { |
3215 | | // Collect packed file path for Step 3 |
3216 | 0 | packed_files_in_metadata[packed_info.resource_id()].insert(packed_file_path); |
3217 | |
|
3218 | 0 | auto* accessor = get_accessor(packed_info.resource_id()); |
3219 | 0 | if (accessor == nullptr) { |
3220 | 0 | LOG(WARNING) << "accessor not found for packed file, resource_id=" |
3221 | 0 | << packed_info.resource_id() |
3222 | 0 | << ", packed_file_path=" << packed_file_path; |
3223 | 0 | check_ret = -1; |
3224 | 0 | continue; |
3225 | 0 | } |
3226 | | |
3227 | 0 | int ret = accessor->exists(packed_file_path); |
3228 | 0 | if (ret < 0) { |
3229 | 0 | LOG(WARNING) << "failed to check packed file existence, packed_file_path=" |
3230 | 0 | << packed_file_path << ", ret=" << ret; |
3231 | 0 | check_ret = -1; |
3232 | 0 | continue; |
3233 | 0 | } |
3234 | | |
3235 | 0 | if (ret != 0) { |
3236 | | // ret == 1 means file not found, ret > 1 means other error |
3237 | | // When packed file doesn't exist in storage, ref_cnt must be 0 and state must be RECYCLING |
3238 | 0 | bool ref_cnt_valid = (packed_info.ref_cnt() == 0); |
3239 | 0 | bool state_valid = (packed_info.state() == cloud::PackedFileInfoPB::RECYCLING); |
3240 | 0 | if (!ref_cnt_valid || !state_valid) { |
3241 | 0 | LOG(WARNING) << "packed file not found in storage but metadata is invalid, " |
3242 | 0 | "packed_file_path=" |
3243 | 0 | << packed_file_path << ", ref_cnt=" << packed_info.ref_cnt() |
3244 | 0 | << " (expected=0), state=" << packed_info.state() |
3245 | 0 | << " (expected=RECYCLING), ret=" << ret; |
3246 | 0 | num_packed_file_loss++; |
3247 | 0 | check_ret = 1; // Data inconsistency identified |
3248 | 0 | } |
3249 | | // If ref_cnt == 0 and state == RECYCLING, this is expected (file is being recycled) |
3250 | 0 | } |
3251 | | // ret == 0 means file exists, which is expected |
3252 | 0 | } |
3253 | | |
3254 | | // Step 2.2: Verify reference count matches expected count |
3255 | 0 | int64_t expected_ref = expected_ref_counts[packed_file_path]; |
3256 | 0 | if (packed_info.ref_cnt() != expected_ref) { |
3257 | 0 | LOG(WARNING) << "packed file ref count mismatch, packed_file_path=" |
3258 | 0 | << packed_file_path << ", expected=" << expected_ref |
3259 | 0 | << ", actual=" << packed_info.ref_cnt(); |
3260 | 0 | num_ref_count_mismatch++; |
3261 | 0 | check_ret = 1; // Data inconsistency identified |
3262 | 0 | } |
3263 | | |
3264 | | // Step 2.3: Verify small files in packed_info match rowset references |
3265 | 0 | std::unordered_set<std::string> small_files_in_meta; |
3266 | 0 | for (const auto& small_file : packed_info.slices()) { |
3267 | 0 | if (!small_file.deleted()) { |
3268 | 0 | small_files_in_meta.insert(small_file.path()); |
3269 | 0 | } |
3270 | 0 | } |
3271 | |
|
3272 | 0 | const auto& expected_small_files = packed_file_small_files[packed_file_path]; |
3273 | 0 | if (small_files_in_meta != expected_small_files) { |
3274 | | // Check for missing small files |
3275 | 0 | for (const auto& expected_path : expected_small_files) { |
3276 | 0 | if (small_files_in_meta.find(expected_path) == small_files_in_meta.end()) { |
3277 | 0 | LOG(WARNING) << "small file missing in packed file info, packed_file_path=" |
3278 | 0 | << packed_file_path << ", small_file_path=" << expected_path; |
3279 | 0 | num_small_file_ref_mismatch++; |
3280 | 0 | check_ret = 1; |
3281 | 0 | } |
3282 | 0 | } |
3283 | | // Check for extra small files (may be deleted, so less critical) |
3284 | 0 | for (const auto& meta_path : small_files_in_meta) { |
3285 | 0 | if (expected_small_files.find(meta_path) == expected_small_files.end()) { |
3286 | 0 | LOG(INFO) << "small file in packed file info not found in rowset metas, " |
3287 | 0 | "may be deleted, packed_file_path=" |
3288 | 0 | << packed_file_path << ", small_file_path=" << meta_path; |
3289 | 0 | } |
3290 | 0 | } |
3291 | 0 | } |
3292 | 0 | } |
3293 | |
|
3294 | 0 | if (!it->more()) { |
3295 | 0 | break; |
3296 | 0 | } |
3297 | 0 | scan_begin = last_key; |
3298 | 0 | scan_begin.push_back('\x00'); |
3299 | 0 | } |
3300 | | |
3301 | | // Step 3: Check for leaked packed files (exist in storage but not in metadata) |
3302 | | // Scan all storage vaults to find packed files and verify they are in metadata |
3303 | 0 | { |
3304 | 0 | std::vector<StorageVaultAccessor*> accessors; |
3305 | 0 | get_all_accessor(&accessors); |
3306 | |
|
3307 | 0 | for (StorageVaultAccessor* accessor : accessors) { |
3308 | 0 | if (stopped()) { |
3309 | 0 | return -1; |
3310 | 0 | } |
3311 | | |
3312 | | // Find resource_id for this accessor |
3313 | 0 | std::string resource_id; |
3314 | 0 | for (const auto& [id, acc] : accessor_map_) { |
3315 | 0 | if (acc.get() == accessor) { |
3316 | 0 | resource_id = id; |
3317 | 0 | break; |
3318 | 0 | } |
3319 | 0 | } |
3320 | |
|
3321 | 0 | if (resource_id.empty()) { |
3322 | 0 | continue; |
3323 | 0 | } |
3324 | | |
3325 | | // List all files under data/packed_file/ directory |
3326 | 0 | std::unique_ptr<ListIterator> list_it; |
3327 | 0 | int ret = accessor->list_directory("data/packed_file", &list_it); |
3328 | 0 | if (ret != 0) { |
3329 | | // Directory may not exist, which is fine |
3330 | 0 | if (ret < 0) { |
3331 | 0 | LOG(WARNING) << "failed to list packed_file directory, resource_id=" |
3332 | 0 | << resource_id << ", ret=" << ret; |
3333 | 0 | check_ret = -1; |
3334 | 0 | } |
3335 | 0 | continue; |
3336 | 0 | } |
3337 | | |
3338 | 0 | const auto& expected_packed_files = packed_files_in_metadata[resource_id]; |
3339 | 0 | while (list_it->has_next()) { |
3340 | 0 | if (stopped()) { |
3341 | 0 | return -1; |
3342 | 0 | } |
3343 | | |
3344 | 0 | auto file_meta = list_it->next(); |
3345 | 0 | if (!file_meta.has_value()) { |
3346 | 0 | break; |
3347 | 0 | } |
3348 | | |
3349 | 0 | const std::string& file_path = file_meta->path; |
3350 | | // Only check files (not directories), and ensure it's a packed file |
3351 | | // Skip directories (paths ending with '/') and non-packed-file paths |
3352 | 0 | if (file_path.empty() || file_path.back() == '/' || |
3353 | 0 | !file_path.starts_with("data/packed_file/")) { |
3354 | 0 | continue; |
3355 | 0 | } |
3356 | | |
3357 | | // Check if this packed file is in metadata |
3358 | 0 | if (expected_packed_files.find(file_path) == expected_packed_files.end()) { |
3359 | 0 | LOG(WARNING) << "packed file found in storage but not in metadata, " |
3360 | 0 | "resource_id=" |
3361 | 0 | << resource_id << ", packed_file_path=" << file_path; |
3362 | 0 | num_packed_file_leak++; |
3363 | 0 | check_ret = 1; // Data leak identified |
3364 | 0 | } |
3365 | 0 | } |
3366 | 0 | } |
3367 | 0 | } |
3368 | | |
3369 | 0 | if (num_packed_file_loss > 0 || num_packed_file_leak > 0 || num_ref_count_mismatch > 0 || |
3370 | 0 | num_small_file_ref_mismatch > 0) { |
3371 | 0 | return 1; // Data loss or inconsistency identified |
3372 | 0 | } |
3373 | | |
3374 | 0 | if (check_ret < 0) { |
3375 | 0 | return check_ret; // Temporary error |
3376 | 0 | } |
3377 | | |
3378 | 0 | return 0; // Success |
3379 | 0 | } |
3380 | | } // namespace doris::cloud |