Skip to content

Commit f273f97

Browse files
authored
Hoststatus remove stats creation. (#8689)
* Update traffic_ctl to query HostStatus directly - Updates HostStatus to eliminate stats creation. - Updates traffic_ctl JSON-RPC to query HostStatus directly. - adds in a new persistance mechanism for HosStatus. * Changes from review comments. * remove redundant 'const'.
1 parent 68e69e2 commit f273f97

13 files changed

Lines changed: 269 additions & 225 deletions

File tree

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,10 @@ traffic_ctl host
325325
----------------
326326
.. program:: traffic_ctl host
327327

328-
A stat to track status is created for each host. The name is the host fqdn with a prefix of
329-
"proxy.process.host_status". The value of the stat is a string which is the serialized
330-
representation of the status. This contains the overall status and the status for each reason. The
331-
stats may be viewed using the :program:`traffic_ctl metric` command or through the `stats_over_http`
332-
endpoint.
328+
A record to track status is created for each host. The name is the host fqdn. The value of the
329+
record when retrieved, is a serialized string representation of the status.
330+
This contains the overall status and the status for each reason. The
331+
records may be viewed using the :program:`traffic_ctl host status` command.
333332

334333
.. option:: --time count
335334

@@ -366,7 +365,7 @@ endpoint.
366365
:ref:`admin_lookup_records`
367366

368367
Get the current status of the specified hosts with respect to their use as targets for parent
369-
selection. This returns the same information as the per host stat.
368+
selection. If the HOSTNAME arguments are omitted, all host records available are returned.
370369

371370
.. option:: down HOSTNAME [HOSTNAME ...]
372371

doc/developer-guide/jsonrpc/jsonrpc-api.en.rst

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,9 +1106,9 @@ admin_host_set_status
11061106
Description
11071107
~~~~~~~~~~~
11081108

1109-
A stat to track status is created for each host. The name is the host fqdn with a prefix of `proxy.process.host_status`. The value of
1110-
the stat is a string which is the serialized representation of the status. This contains the overall status and the status for each reason.
1111-
The stats may be viewed using the `admin_lookup_records`_ rpc api or through the ``stats_over_http`` endpoint.
1109+
A record to track status is created for each host. The name is the host fqdn.
1110+
This record contains the overall status and the status for each reason.
1111+
The records may be viewed using the `admin_host_get_status` rpc api.
11121112

11131113
Parameters
11141114
~~~~~~~~~~
@@ -1204,11 +1204,8 @@ Response:
12041204
Getting the host status
12051205
~~~~~~~~~~~~~~~~~~~~~~~
12061206

1207-
Get the current status of the specified hosts with respect to their use as targets for parent selection. This returns the same
1208-
information as the per host stat.
1209-
1210-
Although there is no specialized API that you can call to get a status from a particular host you can work away by pulling the right records.
1211-
For instance, the ``host1`` that we just set up can be easily fetch for a status:
1207+
Get the current status of the specified hosts with respect to their use as targets for parent selection. This returns the serialized
1208+
information for the host.
12121209

12131210
Request:
12141211

@@ -1218,11 +1215,10 @@ Request:
12181215
{
12191216
"id": "ded7018e-0720-11eb-abe2-001fc69cc946",
12201217
"jsonrpc": "2.0",
1221-
"method": "admin_lookup_records",
1222-
"params": [{
1223-
"record_name": "proxy.process.host_status.host1"
1224-
}
1225-
]
1218+
"method": "admin_host_get_status",
1219+
"params": [
1220+
"host1.mycdn.net"
1221+
]
12261222
}
12271223
12281224
Response:
@@ -1234,23 +1230,11 @@ Response:
12341230
"jsonrpc": "2.0",
12351231
"id": "ded7018e-0720-11eb-abe2-001fc69cc946",
12361232
"result": {
1237-
"recordList": [{
1238-
"record": {
1239-
"record_name": "proxy.process.host_status.host1",
1240-
"record_type": "3",
1241-
"version": "0",
1242-
"raw_stat_block": "0",
1243-
"order": "1134",
1244-
"stat_meta": {
1245-
"persist_type": "1"
1246-
},
1247-
"record_class": "2",
1248-
"overridable": "false",
1249-
"data_type": "STRING",
1250-
"current_value": "HOST_STATUS_UP,ACTIVE:UP:0:0,LOCAL:UP:0:0,MANUAL:UP:0:0,SELF_DETECT:UP:0",
1251-
"default_value": "HOST_STATUS_UP,ACTIVE:UP:0:0,LOCAL:UP:0:0,MANUAL:UP:0:0,SELF_DETECT:UP:0"
1233+
"statusList": [{
1234+
"hostname": "host1.mycdn.net",
1235+
"status": "HOST_STATUS_DOWN,ACTIVE:UP:0:0,LOCAL:UP:0:0,MANUAL:UP:0:0,SELF_DETECT:DOWN:1646248306"
12521236
}
1253-
}]
1237+
]
12541238
,"errorList":[]
12551239
}
12561240
}

include/shared/rpc/yaml_codecs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,4 @@ class yamlcpp_json_emitter
240240

241241
return resp;
242242
}
243-
};
243+
};

include/tscore/Filenames.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace filename
4646
///////////////////////////////////////////////////////////////////
4747
// Various other file names
4848
constexpr const char *RECORDS_STATS = "records.snap";
49+
constexpr const char *HOST_RECORDS = "host_records.yaml";
4950

5051
} // namespace filename
5152
} // namespace ts

proxy/HostStatus.h

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include <ctime>
3434
#include <string>
3535
#include <sstream>
36+
#include "tscore/Filenames.h"
37+
#include "tscore/I_Layout.h"
3638
#include "tscore/ink_rwlock.h"
3739
#include "records/P_RecProcess.h"
3840

@@ -89,6 +91,11 @@ struct Reason {
8991
}
9092
};
9193

94+
struct HostStatuses {
95+
std::string hostname;
96+
std::string status;
97+
};
98+
9299
// host status POD
93100
struct HostStatRec {
94101
TSHostStatus status;
@@ -168,6 +175,33 @@ struct HostStatRec {
168175
}
169176
};
170177

178+
struct HostStatusSync : public Continuation {
179+
std::string hostRecordsFile;
180+
181+
void sync_task();
182+
void
183+
getHostStatusPersistentFilePath()
184+
{
185+
std::string rundir(RecConfigReadRuntimeDir());
186+
hostRecordsFile = Layout::relative_to(rundir, ts::filename::HOST_RECORDS);
187+
}
188+
189+
public:
190+
HostStatusSync()
191+
{
192+
getHostStatusPersistentFilePath();
193+
194+
SET_HANDLER(&HostStatusSync::mainEvent);
195+
}
196+
197+
int
198+
mainEvent(int event, Event *e)
199+
{
200+
sync_task();
201+
return EVENT_DONE;
202+
}
203+
};
204+
171205
/**
172206
* Singleton placeholder for next hop status.
173207
*/
@@ -182,11 +216,16 @@ struct HostStatus {
182216
}
183217
void setHostStatus(const std::string_view name, const TSHostStatus status, const unsigned int down_time,
184218
const unsigned int reason);
185-
HostStatRec *getHostStatus(const std::string_view name);
186-
void createHostStat(const std::string_view name, const char *data = nullptr);
187-
void loadHostStatusFromStats();
219+
void loadFromPersistentStore();
188220
void loadRecord(std::string_view name, HostStatRec &h);
189-
RecErrT getHostStat(std::string &stat_name, char *buf, unsigned int buf_len);
221+
HostStatRec *getHostStatus(const std::string_view name);
222+
void getAllHostStatuses(std::vector<HostStatuses> &hosts);
223+
std::string
224+
getHostStatusPersistentFilePath()
225+
{
226+
std::string rundir(RecConfigReadRuntimeDir());
227+
return Layout::relative_to(rundir, ts::filename::HOST_RECORDS);
228+
}
190229

191230
private:
192231
HostStatus();

src/traffic_ctl_jsonrpc/CtrlCommands.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,8 @@ void
312312
HostCommand::status_get()
313313
{
314314
auto const &data = _arguments.get("status");
315-
HostGetStatusRequest request;
316-
for (auto it : data) {
317-
std::string name = std::string{HostGetStatusRequest::STATUS_PREFIX} + "." + it;
318-
request.emplace_rec(name, shared::rpc::NOT_REGEX, shared::rpc::METRIC_REC_TYPES);
319-
}
315+
HostGetStatusRequest request{{std::begin(data), std::end(data)}};
316+
320317
auto response = invoke_rpc(request);
321318

322319
_printer->write_output(response);

src/traffic_ctl_jsonrpc/CtrlPrinters.cc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,17 @@ RecordDescribePrinter::write_output_pretty(shared::rpc::RecordLookUpResponse con
282282
void
283283
GetHostStatusPrinter::write_output(YAML::Node const &result)
284284
{
285-
auto response = result.as<shared::rpc::RecordLookUpResponse>();
286-
for (auto &&recordInfo : response.recordList) {
287-
std::cout << recordInfo.name << " " << recordInfo.currentValue << '\n';
285+
auto resp = result.as<HostStatusLookUpResponse>();
286+
287+
if (resp.statusList.size() > 0) {
288+
for (auto &&host : resp.statusList) {
289+
std::cout << host.hostName << " " << host.status << '\n';
290+
}
291+
std::cout << '\n';
288292
}
289-
for (auto &&e : response.errorList) {
290-
std::cout << "Failed to fetch " << e.recordName << '\n';
293+
294+
for (auto &&e : resp.errorList) {
295+
std::cout << e << '\n';
291296
}
292297
}
293298

src/traffic_ctl_jsonrpc/jsonrpc/CtrlRPCRequests.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ struct ConfigSetRecordResponse {
108108
std::vector<UpdatedRec> data;
109109
};
110110
//------------------------------------------------------------------------------------------------------------------------------------
111+
struct HostStatusLookUpResponse {
112+
struct HostStatusInfo {
113+
std::string hostName;
114+
std::string status;
115+
};
116+
117+
std::vector<HostStatusInfo> statusList;
118+
std::vector<std::string> errorList;
119+
};
120+
//------------------------------------------------------------------------------------------------------------------------------------
111121
struct HostSetStatusRequest : shared::rpc::ClientRequest {
112122
using super = shared::rpc::ClientRequest;
113123
struct Params {
@@ -129,10 +139,16 @@ struct HostSetStatusRequest : shared::rpc::ClientRequest {
129139
}
130140
};
131141

132-
struct HostGetStatusRequest : shared::rpc::RecordLookupRequest {
133-
static constexpr auto STATUS_PREFIX = "proxy.process.host_status";
134-
using super = shared::rpc::RecordLookupRequest;
135-
HostGetStatusRequest() : super() {}
142+
struct HostGetStatusRequest : shared::rpc::ClientRequest {
143+
using super = shared::rpc::ClientRequest;
144+
using Params = std::vector<std::string>;
145+
HostGetStatusRequest(Params p) { super::params = std::move(p); }
146+
147+
std::string
148+
get_method() const
149+
{
150+
return "admin_host_get_status";
151+
}
136152
};
137153
//------------------------------------------------------------------------------------------------------------------------------------
138154
struct BasicPluginMessageRequest : shared::rpc::ClientRequest {

src/traffic_ctl_jsonrpc/jsonrpc/ctrl_yaml_codecs.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,23 @@ template <> struct convert<ConfigSetRecordResponse> {
157157
return true;
158158
}
159159
};
160-
//------------------------------------------------------------------------------------------------------------------------------------
161-
} // namespace YAML
160+
161+
template <> struct convert<HostStatusLookUpResponse> {
162+
static bool
163+
decode(Node const &node, HostStatusLookUpResponse &info)
164+
{
165+
YAML::Node statusList = node["statusList"];
166+
YAML::Node errorList = node["errorList"];
167+
for (auto &&item : statusList) {
168+
HostStatusLookUpResponse::HostStatusInfo hi;
169+
hi.hostName = item["hostname"].Scalar();
170+
hi.status = item["status"].Scalar();
171+
info.statusList.push_back(hi);
172+
}
173+
for (auto &&item : errorList) {
174+
info.errorList.push_back(item.Scalar());
175+
}
176+
return true;
177+
}
178+
};
179+
} // namespace YAML

src/traffic_ctl_jsonrpc/traffic_ctl.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ main(int argc, const char **argv)
9393
config_command.add_command("registry", "Show configuration file registry", [&]() { command->execute(); })
9494
.add_example_usage("traffic_ctl config registry");
9595
// host commands
96-
host_command.add_command("status", "Get one or more host statuses", "", MORE_THAN_ONE_ARG_N, [&]() { command->execute(); })
96+
host_command.add_command("status", "Get one or more host statuses", "", MORE_THAN_ZERO_ARG_N, [&]() { command->execute(); })
9797
.add_example_usage("traffic_ctl host status HOST [HOST ...]");
9898
host_command.add_command("down", "Set down one or more host(s)", "", MORE_THAN_ONE_ARG_N, [&]() { command->execute(); })
9999
.add_example_usage("traffic_ctl host down HOST [OPTIONS]")

0 commit comments

Comments
 (0)