|
3 | 3 | #include <algorithm> |
4 | 4 | #include <array> |
5 | 5 | #include <cassert> |
| 6 | +#include <charconv> |
6 | 7 | #include <chrono> |
7 | 8 | #include <cinttypes> |
8 | 9 | #include <limits> |
@@ -89,6 +90,39 @@ namespace kv_cache_manager { |
89 | 90 | } while (0) |
90 | 91 |
|
91 | 92 | namespace { |
| 93 | +struct ReporterIdentityView { |
| 94 | + std::string_view base_host; |
| 95 | + std::optional<uint64_t> engine_rank; |
| 96 | +}; |
| 97 | + |
| 98 | +bool ParseReporterIdentity(std::string_view host_ip_port, ReporterIdentityView &out) { |
| 99 | + out = {}; |
| 100 | + if (host_ip_port.empty()) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + const size_t separator = host_ip_port.find('@'); |
| 104 | + if (separator == std::string_view::npos) { |
| 105 | + out.base_host = host_ip_port; |
| 106 | + return true; |
| 107 | + } |
| 108 | + if (separator == 0 || separator + 1 >= host_ip_port.size() || |
| 109 | + host_ip_port.find('@', separator + 1) != std::string_view::npos) { |
| 110 | + return false; |
| 111 | + } |
| 112 | + const std::string_view rank_text = host_ip_port.substr(separator + 1); |
| 113 | + if (rank_text.size() > 1 && rank_text.front() == '0') { |
| 114 | + return false; |
| 115 | + } |
| 116 | + uint64_t rank = 0; |
| 117 | + const auto [end, ec] = std::from_chars(rank_text.data(), rank_text.data() + rank_text.size(), rank); |
| 118 | + if (ec != std::errc{} || end != rank_text.data() + rank_text.size()) { |
| 119 | + return false; |
| 120 | + } |
| 121 | + out.base_host = host_ip_port.substr(0, separator); |
| 122 | + out.engine_rank = rank; |
| 123 | + return true; |
| 124 | +} |
| 125 | + |
92 | 126 | CacheManager::KeyVector GenKeyVector(const CacheManager::TokenIdsVector &tokens, int64_t block_size) { |
93 | 127 | std::vector<int64_t> block_keys; |
94 | 128 | size_t total_blocks = tokens.size() / block_size; |
@@ -2668,7 +2702,9 @@ ErrorCode CacheManager::ReportEvent(RequestContext *request_context, |
2668 | 2702 | const std::string &host_ip_port = request->host_ip_port(); |
2669 | 2703 | auto *response_status = response->mutable_header()->mutable_status(); |
2670 | 2704 |
|
2671 | | - if (instance_id.empty() || !SnapshotUriUtils::IsValidLocationIdComponent(host_ip_port)) { |
| 2705 | + ReporterIdentityView reporter_identity; |
| 2706 | + if (instance_id.empty() || !SnapshotUriUtils::IsValidLocationIdComponent(host_ip_port) || |
| 2707 | + !ParseReporterIdentity(host_ip_port, reporter_identity)) { |
2672 | 2708 | KVCM_LOG_WARN("trace_id [%s] | ReportEvent: invalid instance_id or host_ip_port", trace_id.c_str()); |
2673 | 2709 | response_status->set_code(proto::meta::INVALID_ARGUMENT); |
2674 | 2710 | response_status->set_message("invalid instance_id or host_ip_port"); |
@@ -4596,6 +4632,7 @@ CacheManager::GetHostCacheStateCheckLocDataExistFunc(const std::string &instance |
4596 | 4632 | struct EventVisibilitySnapshot { |
4597 | 4633 | std::shared_ptr<EventReportBackend> backend; |
4598 | 4634 | EventReportBackend::QueryVisibilitySnapshot reporters; |
| 4635 | + std::map<std::string, std::vector<std::string>, std::less<>> logical_hosts_by_reporter; |
4599 | 4636 | }; |
4600 | 4637 | struct EventVisibilitySnapshots { |
4601 | 4638 | std::once_flag initialize_once; |
@@ -4627,6 +4664,42 @@ CacheManager::GetHostCacheStateCheckLocDataExistFunc(const std::string &instance |
4627 | 4664 | snapshot.backend->GetQueryVisibilitySnapshot(instance_id, snapshot.reporters); |
4628 | 4665 | event_snapshots->by_storage_type.emplace(storage_type, std::move(snapshot)); |
4629 | 4666 | } |
| 4667 | + |
| 4668 | + std::map<std::string, std::vector<std::string>, std::less<>> ranked_hosts_by_base; |
| 4669 | + const auto l1p5_it = event_snapshots->by_storage_type.find( |
| 4670 | + DataStorageType::DATA_STORAGE_TYPE_EVENT_REPORT_L1P5); |
| 4671 | + if (l1p5_it != event_snapshots->by_storage_type.end()) { |
| 4672 | + for (const auto &[reporter, state] : l1p5_it->second.reporters) { |
| 4673 | + (void)state; |
| 4674 | + ReporterIdentityView identity; |
| 4675 | + if (ParseReporterIdentity(reporter, identity) && identity.engine_rank.has_value()) { |
| 4676 | + ranked_hosts_by_base[std::string(identity.base_host)].push_back(reporter); |
| 4677 | + } |
| 4678 | + } |
| 4679 | + } |
| 4680 | + for (auto &[base, ranked_hosts] : ranked_hosts_by_base) { |
| 4681 | + (void)base; |
| 4682 | + std::sort(ranked_hosts.begin(), ranked_hosts.end()); |
| 4683 | + ranked_hosts.erase(std::unique(ranked_hosts.begin(), ranked_hosts.end()), ranked_hosts.end()); |
| 4684 | + } |
| 4685 | + for (auto &[storage_type, snapshot] : event_snapshots->by_storage_type) { |
| 4686 | + for (const auto &[reporter, state] : snapshot.reporters) { |
| 4687 | + (void)state; |
| 4688 | + auto &logical_hosts = snapshot.logical_hosts_by_reporter[reporter]; |
| 4689 | + ReporterIdentityView identity; |
| 4690 | + const bool parsed = ParseReporterIdentity(reporter, identity); |
| 4691 | + if (parsed && storage_type == DataStorageType::DATA_STORAGE_TYPE_EVENT_REPORT_L2 && |
| 4692 | + !identity.engine_rank.has_value()) { |
| 4693 | + const auto ranked_it = ranked_hosts_by_base.find(identity.base_host); |
| 4694 | + if (ranked_it != ranked_hosts_by_base.end()) { |
| 4695 | + logical_hosts = ranked_it->second; |
| 4696 | + } |
| 4697 | + } |
| 4698 | + if (logical_hosts.empty()) { |
| 4699 | + logical_hosts.push_back(reporter); |
| 4700 | + } |
| 4701 | + } |
| 4702 | + } |
4630 | 4703 | }; |
4631 | 4704 |
|
4632 | 4705 | return [fallback = std::move(fallback), |
@@ -4661,6 +4734,10 @@ CacheManager::GetHostCacheStateCheckLocDataExistFunc(const std::string &instance |
4661 | 4734 | out_info.has_reporter_identity = true; |
4662 | 4735 | out_info.reporter_medium = reporter_medium; |
4663 | 4736 | out_info.reporter_host = reporter_host; |
| 4737 | + const auto logical_hosts_it = snapshot_it->second.logical_hosts_by_reporter.find(reporter_host); |
| 4738 | + if (logical_hosts_it != snapshot_it->second.logical_hosts_by_reporter.end()) { |
| 4739 | + out_info.logical_hosts = &logical_hosts_it->second; |
| 4740 | + } |
4664 | 4741 | return true; |
4665 | 4742 | }; |
4666 | 4743 | } |
|
0 commit comments