Copilot commented on code in PR #14066:
URL: https://github.com/apache/trafficserver/pull/14066#discussion_r4565489694


##########
src/proxy/HostStatus.cc:
##########
@@ -281,14 +328,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, reasons: %s.", 
int(name.size()), name.data(), down_time,

Review Comment:
   The `Note()` format string was changed from `%d` to `%u` for `down_time`. If 
`down_time` is still a signed `int` (as implied by the previous `%d`), this 
becomes a format-specifier mismatch and can lead to undefined behavior. Either 
revert to `%d` or cast `down_time` to an explicitly unsigned type that matches 
`%u`.



##########
src/proxy/HostStatus.cc:
##########
@@ -281,14 +328,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, reasons: %s.", 
int(name.size()), name.data(), down_time,
+         down_time == 0 ? "indefinitely" : "seconds", reason_name.c_str());

Review Comment:
   The new log message reads awkwardly as `down_time: <N> - seconds` and 
`down_time: 0 - indefinitely`. Consider emitting `down_time: indefinitely` when 
`down_time == 0`, otherwise `down_time: <N> seconds` (no hyphen), to make the 
diagnostic easier to read and avoid mixing the special-case label with a 
numeric value.



##########
src/proxy/HostStatus.cc:
##########
@@ -52,6 +53,49 @@ DbgCtl dbg_ctl_host_statuses{"host_statuses"};
 swoc::Rv<YAML::Node> server_get_status(std::string_view const id, YAML::Node 
const &params);
 swoc::Rv<YAML::Node> server_set_status(std::string_view const id, YAML::Node 
const &params);
 
+std::string
+Reason::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);
+    }
+  }

Review Comment:
   The mapping from reason bits to names is implemented as a hard-coded 
sequence of `if` checks. To reduce the chance of missing future reason bits 
(and to simplify changes), consider refactoring this into a small static table 
of `{bit, name}` entries and iterating it, while keeping the special-case `ALL` 
behavior.



-- 
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]

Reply via email to