|
| 1 | +/** |
| 2 | + @section license License |
| 3 | +
|
| 4 | + Licensed to the Apache Software Foundation (ASF) under one |
| 5 | + or more contributor license agreements. See the NOTICE file |
| 6 | + distributed with this work for additional information |
| 7 | + regarding copyright ownership. The ASF licenses this file |
| 8 | + to you under the Apache License, Version 2.0 (the |
| 9 | + "License"); you may not use this file except in compliance |
| 10 | + with the License. You may obtain a copy of the License at |
| 11 | +
|
| 12 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +
|
| 14 | + Unless required by applicable law or agreed to in writing, software |
| 15 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + See the License for the specific language governing permissions and |
| 18 | + limitations under the License. |
| 19 | +*/ |
| 20 | + |
| 21 | +#include "mgmt/rpc/handlers/hostdb/HostDB.h" |
| 22 | +#include "mgmt/rpc/handlers/common/ErrorUtils.h" |
| 23 | + |
| 24 | +#include "iocore/hostdb/HostDBProcessor.h" |
| 25 | +#include "../src/iocore/hostdb/P_HostDBProcessor.h" |
| 26 | +#include "swoc/MemSpan.h" |
| 27 | +#include "tsutil/TsSharedMutex.h" |
| 28 | +#include "yaml-cpp/node/node.h" |
| 29 | +#include <shared_mutex> |
| 30 | +#include <string> |
| 31 | + |
| 32 | +namespace |
| 33 | +{ |
| 34 | +DbgCtl dbg_ctl_rpc_server{"rpc.server"}; |
| 35 | +DbgCtl dbg_ctl_rpc_handler_server{"rpc.handler.hostdb"}; |
| 36 | + |
| 37 | +constexpr std::string_view |
| 38 | +str(HostDBType type) |
| 39 | +{ |
| 40 | + // No default to find HostDBType change |
| 41 | + switch (type) { |
| 42 | + case HostDBType::ADDR: |
| 43 | + return "ADDR"; |
| 44 | + case HostDBType::SRV: |
| 45 | + return "SRV"; |
| 46 | + case HostDBType::HOST: |
| 47 | + return "HOST"; |
| 48 | + case HostDBType::UNSPEC: |
| 49 | + return "UNSPEC"; |
| 50 | + } |
| 51 | + |
| 52 | + return ""; |
| 53 | +} |
| 54 | + |
| 55 | +constexpr std::string_view |
| 56 | +str(sa_family_t type) |
| 57 | +{ |
| 58 | + switch (type) { |
| 59 | + case AF_UNIX: |
| 60 | + return "AF_UNIX"; |
| 61 | + case AF_INET: |
| 62 | + return "AF_INET"; |
| 63 | + case AF_INET6: |
| 64 | + return "AF_INET6"; |
| 65 | + case AF_UNSPEC: |
| 66 | + return "UNSPEC"; |
| 67 | + default: |
| 68 | + return "UNKNOWN"; |
| 69 | + } |
| 70 | +} |
| 71 | +} // end anonymous namespace |
| 72 | + |
| 73 | +namespace YAML |
| 74 | +{ |
| 75 | +template <> struct convert<HostDBCache> { |
| 76 | + static Node |
| 77 | + encode(const HostDBCache *const hostDB) |
| 78 | + { |
| 79 | + Node partitions; |
| 80 | + for (size_t i = 0; i < hostDB->refcountcache->partition_count(); i++) { |
| 81 | + auto &partition = hostDB->refcountcache->get_partition(i); |
| 82 | + std::vector<RefCountCacheHashEntry *> partition_entries; |
| 83 | + |
| 84 | + { |
| 85 | + std::shared_lock<ts::shared_mutex> shared_lock{partition.lock}; |
| 86 | + partition_entries.reserve(partition.count()); |
| 87 | + partition.copy(partition_entries); |
| 88 | + } |
| 89 | + |
| 90 | + Node partition_node; |
| 91 | + partition_node["id"] = i; |
| 92 | + |
| 93 | + for (RefCountCacheHashEntry *entry : partition_entries) { |
| 94 | + HostDBRecord *record = static_cast<HostDBRecord *>(entry->item.get()); |
| 95 | + partition_node["records"].push_back(*record); |
| 96 | + } |
| 97 | + |
| 98 | + partitions.push_back(partition_node); |
| 99 | + } |
| 100 | + |
| 101 | + auto &version = AppVersionInfo::get_version(); |
| 102 | + |
| 103 | + Node node; |
| 104 | + node["metadata"]["timestamp"] = |
| 105 | + std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count(); |
| 106 | + node["metadata"]["version"] = version.full_version(); |
| 107 | + node["partitions"] = partitions; |
| 108 | + |
| 109 | + return node; |
| 110 | + } |
| 111 | +}; |
| 112 | + |
| 113 | +template <> struct convert<HostDBRecord> { |
| 114 | + static Node |
| 115 | + encode(const HostDBRecord &record) |
| 116 | + { |
| 117 | + Node metadata; |
| 118 | + metadata["name"] = record.name(); |
| 119 | + metadata["type"] = str(record.record_type); |
| 120 | + metadata["af_familiy"] = str(record.af_family); |
| 121 | + metadata["failed"] = record.is_failed(); |
| 122 | + metadata["ip_timestamp"] = record.ip_timestamp.time_since_epoch().count(); |
| 123 | + |
| 124 | + Node node; |
| 125 | + node["metadata"] = metadata; |
| 126 | + |
| 127 | + swoc::MemSpan<const HostDBInfo> span = record.rr_info(); |
| 128 | + for (const HostDBInfo &info : span) { |
| 129 | + YAML::Node info_node; |
| 130 | + if (record.is_srv()) { |
| 131 | + YAML::Node srv_node; |
| 132 | + srv_node["weight"] = info.data.srv.srv_weight; |
| 133 | + srv_node["priority"] = info.data.srv.srv_priority; |
| 134 | + srv_node["port"] = info.data.srv.srv_port; |
| 135 | + srv_node["target"] = info.srvname(); |
| 136 | + |
| 137 | + info_node["srv"] = srv_node; |
| 138 | + } else { |
| 139 | + char buf[INET6_ADDRSTRLEN]; |
| 140 | + info.data.ip.toString(buf, sizeof(buf)); |
| 141 | + |
| 142 | + info_node["ip"] = std::string(buf); |
| 143 | + } |
| 144 | + |
| 145 | + info_node["health"]["last_failure"] = info.last_failure.load().time_since_epoch().count(); |
| 146 | + info_node["health"]["fail_count"] = static_cast<int>(info.fail_count.load()); |
| 147 | + |
| 148 | + node["info"].push_back(info_node); |
| 149 | + } |
| 150 | + |
| 151 | + return node; |
| 152 | + } |
| 153 | +}; |
| 154 | +} // namespace YAML |
| 155 | + |
| 156 | +namespace rpc::handlers::hostdb |
| 157 | +{ |
| 158 | +namespace err = rpc::handlers::errors; |
| 159 | + |
| 160 | +swoc::Rv<YAML::Node> |
| 161 | +get_hostdb_status(std::string_view const & /* params ATS_UNUSED */, YAML::Node const & /* params ATS_UNUSED */) |
| 162 | +{ |
| 163 | + swoc::Rv<YAML::Node> resp; |
| 164 | + try { |
| 165 | + YAML::Node data = YAML::convert<HostDBCache>::encode(hostDBProcessor.cache()); |
| 166 | + |
| 167 | + resp.result()["data"] = data; |
| 168 | + } catch (std::exception const &ex) { |
| 169 | + resp.errata() |
| 170 | + .assign(std::error_code{errors::Codes::SERVER}) |
| 171 | + .note("Error found when calling get_hostdb_status API: {}", ex.what()); |
| 172 | + } |
| 173 | + return resp; |
| 174 | +} |
| 175 | +} // namespace rpc::handlers::hostdb |
0 commit comments