Skip to content

Commit 6b10f87

Browse files
authored
Add HOSTNAME option to traffic_ctl hostdb status (#12858)
1 parent fea8566 commit 6b10f87

5 files changed

Lines changed: 55 additions & 10 deletions

File tree

doc/appendices/command-line/traffic_ctl.en.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,12 +683,14 @@ traffic_ctl hostdb
683683
------------------
684684
.. program:: traffic_ctl hostdb
685685

686-
.. option:: status
686+
.. option:: status [HOSTNAME]
687687

688688
:ref:`admin_lookup_records`
689689

690690
Get the current status of HostDB.
691691

692+
If ``HOSTNAME`` is specified, the output is filtered to show only records whose names contain the given string.
693+
692694
traffic_ctl rpc
693695
---------------
694696
.. program:: traffic_ctl rpc

src/mgmt/rpc/handlers/hostdb/HostDB.cc

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,22 @@
2424
#include "iocore/hostdb/HostDBProcessor.h"
2525
#include "../src/iocore/hostdb/P_HostDBProcessor.h"
2626
#include "swoc/MemSpan.h"
27+
#include "swoc/TextView.h"
2728
#include "tsutil/TsSharedMutex.h"
2829
#include "yaml-cpp/node/node.h"
2930
#include <shared_mutex>
3031
#include <string>
32+
#include <string_view>
3133

3234
namespace
3335
{
3436
DbgCtl dbg_ctl_rpc_server{"rpc.server"};
3537
DbgCtl dbg_ctl_rpc_handler_server{"rpc.handler.hostdb"};
3638

39+
struct HostDBGetStatusCmdInfo {
40+
std::string hostname;
41+
};
42+
3743
constexpr std::string_view
3844
str(HostDBType type)
3945
{
@@ -74,7 +80,7 @@ namespace YAML
7480
{
7581
template <> struct convert<HostDBCache> {
7682
static Node
77-
encode(const HostDBCache *const hostDB)
83+
encode(const HostDBCache *const hostDB, std::string_view hostname)
7884
{
7985
Node partitions;
8086
for (size_t i = 0; i < hostDB->refcountcache->partition_count(); i++) {
@@ -96,6 +102,9 @@ template <> struct convert<HostDBCache> {
96102

97103
for (RefCountCacheHashEntry *entry : partition_entries) {
98104
HostDBRecord *record = static_cast<HostDBRecord *>(entry->item.get());
105+
if (!hostname.empty() && record->name_view().find(hostname) == std::string_view::npos) {
106+
continue;
107+
}
99108
partition_node["records"].push_back(*record);
100109
}
101110

@@ -157,18 +166,34 @@ template <> struct convert<HostDBRecord> {
157166
return node;
158167
}
159168
};
169+
170+
template <> struct convert<HostDBGetStatusCmdInfo> {
171+
static bool
172+
decode(const Node &node, HostDBGetStatusCmdInfo &rhs)
173+
{
174+
if (auto n = node["hostname"]) {
175+
rhs.hostname = n.as<std::string>();
176+
} else {
177+
return false;
178+
}
179+
180+
return true;
181+
}
182+
};
160183
} // namespace YAML
161184

162185
namespace rpc::handlers::hostdb
163186
{
164187
namespace err = rpc::handlers::errors;
165188

166189
swoc::Rv<YAML::Node>
167-
get_hostdb_status(std::string_view const & /* params ATS_UNUSED */, YAML::Node const & /* params ATS_UNUSED */)
190+
get_hostdb_status(std::string_view const & /* id ATS_UNUSED */, YAML::Node const &params)
168191
{
169192
swoc::Rv<YAML::Node> resp;
170193
try {
171-
YAML::Node data = YAML::convert<HostDBCache>::encode(hostDBProcessor.cache());
194+
HostDBGetStatusCmdInfo cmd = params.as<HostDBGetStatusCmdInfo>();
195+
196+
YAML::Node data = YAML::convert<HostDBCache>::encode(hostDBProcessor.cache(), cmd.hostname);
172197

173198
resp.result()["data"] = data;
174199
} catch (std::exception const &ex) {

src/traffic_ctl/CtrlCommands.cc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -506,10 +506,16 @@ HostDBCommand::HostDBCommand(ts::Arguments *args) : CtrlCommand(args)
506506
void
507507
HostDBCommand::status_get()
508508
{
509-
auto const &data = get_parsed_arguments()->get(STATUS_STR);
510-
HostDBGetStatusRequest request{
511-
{std::begin(data), std::end(data)}
512-
};
509+
HostDBGetStatusRequest::Params params;
510+
511+
auto const &data = get_parsed_arguments()->get(STATUS_STR);
512+
if (data.size() >= 1) {
513+
params = {
514+
data[0],
515+
};
516+
}
517+
518+
HostDBGetStatusRequest request{params};
513519

514520
auto response = invoke_rpc(request);
515521

src/traffic_ctl/jsonrpc/CtrlRPCRequests.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,10 @@ struct HostGetStatusRequest : shared::rpc::ClientRequest {
126126
};
127127
//------------------------------------------------------------------------------------------------------------------------------------
128128
struct HostDBGetStatusRequest : shared::rpc::ClientRequest {
129-
using super = shared::rpc::ClientRequest;
130-
using Params = std::vector<std::string>;
129+
using super = shared::rpc::ClientRequest;
130+
struct Params {
131+
std::string hostname;
132+
};
131133
HostDBGetStatusRequest(Params p) { super::params = std::move(p); }
132134

133135
std::string

src/traffic_ctl/jsonrpc/ctrl_yaml_codecs.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ template <> struct convert<HostSetStatusRequest::Params> {
7171
}
7272
};
7373
//------------------------------------------------------------------------------------------------------------------------------------
74+
template <> struct convert<HostDBGetStatusRequest::Params> {
75+
static Node
76+
encode(HostDBGetStatusRequest::Params const &params)
77+
{
78+
Node node;
79+
node["hostname"] = params.hostname;
80+
return node;
81+
}
82+
};
83+
//------------------------------------------------------------------------------------------------------------------------------------
7484
template <> struct convert<BasicPluginMessageRequest::Params> {
7585
static Node
7686
encode(BasicPluginMessageRequest::Params const &params)

0 commit comments

Comments
 (0)