Copilot commented on code in PR #14066:
URL: https://github.com/apache/trafficserver/pull/14066#discussion_r4565045585
##########
include/proxy/HostStatus.h:
##########
@@ -90,6 +92,51 @@ struct Reason {
// default is MANUAL
return MANUAL;
}
+
+ // Render a reason mask for diagnostics. Unknown bits render as @c
unknown(0x..) instead of
+ // vanishing, because @c TSHostStatusSet passes masks through unvalidated.
+ static std::string
+ getReasonName(const unsigned int reason)
+ {
+ if (reason == 0) {
+ return "none";
+ }
+
+ unsigned int const unknown = reason & ~ALL;
+
+ std::string reason_name;
+ auto append = [&reason_name](std::string_view const name) {
+ if (!reason_name.empty()) {
+ reason_name += ",";
+ }
+ reason_name += name;
+ };
+
+ if ((reason & ALL) == ALL) {
+ append(ALL_REASON);
+ } else {
+ if (reason & ACTIVE) {
+ append(ACTIVE_REASON);
+ }
+ if (reason & LOCAL) {
+ append(LOCAL_REASON);
+ }
+ if (reason & MANUAL) {
+ append(MANUAL_REASON);
+ }
+ if (reason & SELF_DETECT) {
+ append(SELF_DETECT_REASON);
+ }
+ }
+
+ if (unknown != 0) {
+ char detail[64];
+ snprintf(detail, sizeof(detail), "unknown(0x%x)", unknown);
Review Comment:
In a C++ header, calling `snprintf` unqualified can be non-portable because
`<cstdio>` guarantees `std::snprintf`, but does not require `::snprintf` to be
available in the global namespace on all toolchains. Prefer
`std::snprintf(...)` here to avoid build portability issues.
##########
src/proxy/HostStatus.cc:
##########
@@ -281,14 +284,20 @@ HostStatus::setHostStatus(const std::string_view name,
TSHostStatus status, cons
host_stat->status = status;
Dbg(dbg_ctl_host_statuses, "reasons: %d, status: %s",
host_stat->reasons, HostStatusNames[host_stat->status]);
}
+ effective_status = host_stat->status;
+ remaining_reasons = host_stat->reasons;
}
- // log it.
+ // An UP request clears one reason; the host recovers only when none remain.
+ auto reason_name = Reason::getReasonName(reason);
if (status == TSHostStatus::TS_HOST_STATUS_DOWN) {
- Note("Host %.*s has been marked down, down_time: %d - %s.",
int(name.size()), name.data(), down_time,
- down_time == 0 ? "indefinitely." : "seconds.");
+ Note("Host %.*s has been marked down, down_time: %u - %s, reason: %s.",
int(name.size()), name.data(), down_time,
+ down_time == 0 ? "indefinitely" : "seconds", reason_name.c_str());
+ } else if (effective_status == TSHostStatus::TS_HOST_STATUS_UP) {
+ Note("Host %.*s has been marked up, cleared reason: %s.",
int(name.size()), name.data(), reason_name.c_str());
} else {
- Note("Host %.*s has been marked up.", int(name.size()), name.data());
+ Note("Host %.*s is still down, cleared reason: %s, remaining reason: %s.",
int(name.size()), name.data(), reason_name.c_str(),
Review Comment:
The log message says `remaining reason` but the value can be a mask
containing multiple reasons (and `getReasonName` can return a comma-separated
list). Consider changing the text to `remaining reasons` to better reflect
what’s being logged.
##########
src/proxy/HostStatus.cc:
##########
@@ -281,14 +284,20 @@ HostStatus::setHostStatus(const std::string_view name,
TSHostStatus status, cons
host_stat->status = status;
Dbg(dbg_ctl_host_statuses, "reasons: %d, status: %s",
host_stat->reasons, HostStatusNames[host_stat->status]);
}
+ effective_status = host_stat->status;
+ remaining_reasons = host_stat->reasons;
}
- // log it.
+ // An UP request clears one reason; the host recovers only when none remain.
+ auto reason_name = Reason::getReasonName(reason);
if (status == TSHostStatus::TS_HOST_STATUS_DOWN) {
- Note("Host %.*s has been marked down, down_time: %d - %s.",
int(name.size()), name.data(), down_time,
- down_time == 0 ? "indefinitely." : "seconds.");
+ Note("Host %.*s has been marked down, down_time: %u - %s, reason: %s.",
int(name.size()), name.data(), down_time,
+ down_time == 0 ? "indefinitely" : "seconds", reason_name.c_str());
Review Comment:
The `Note()` format string was changed to print `down_time` using `%u`.
Previously this was `%d`, and if `down_time` is not an unsigned type this
becomes undefined behavior (and may print incorrectly on some platforms).
Please align the format specifier with `down_time`’s actual type (or cast to
the intended type at the call site) to ensure correct and portable logging.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]