Complete M7.8 operations endpoints and region server image
ADR-0066: evo-server --ops-listen serves /livez (loop responding, never authority), /readyz (200 only while the host holds the lease with a verified recovered head) and /metrics (Prometheus text: 30 fixed series for readiness, phase, lease epoch/remaining, simulated/committed ticks, commit lag, checkpoint age, tick debt, queue use and bounds, and counters; the only labels are a 6-value phase and 4-value drop reason). The dependency-free OpsServer runs on the host loop with no thread: GET on exact paths, 405/404/400/431 responses, 8 connections with excess refused on accept, 4 KiB requests, 1 s deadlines, 16 KiB responses. HostStatus/phase types moved to the portable evo::ops library with a Windows-tested renderer. evo-server now refuses to start without its required identity and connection settings. Adds the server-runtime Docker target (evo-server + pinned libpq, UID 10001, no credentials), a CI server-image job with smoke script and optional publishing, and a Kubernetes example outside Argo. Validated: Windows CTest 59/59; Linux Debug/Release/clang-tidy/ASan CTest 59/59; PostgreSQL suites under Release, clang-tidy and ASan with real process probes; two ops mutations caught. Docker build/smoke and Gitea CI not executed locally. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
#include "evo/server/ops_http.hpp"
|
||||
#include <arpa/inet.h>
|
||||
#include <cerrno>
|
||||
#include <charconv>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace evo::server {
|
||||
namespace {
|
||||
using Clock = std::chrono::steady_clock;
|
||||
const char* reason(int status) {
|
||||
switch (status) {
|
||||
case 200:
|
||||
return "OK";
|
||||
case 400:
|
||||
return "Bad Request";
|
||||
case 404:
|
||||
return "Not Found";
|
||||
case 405:
|
||||
return "Method Not Allowed";
|
||||
case 431:
|
||||
return "Request Header Fields Too Large";
|
||||
case 503:
|
||||
return "Service Unavailable";
|
||||
default:
|
||||
return "Internal Server Error";
|
||||
}
|
||||
}
|
||||
std::string encode(const OpsResponse& r) {
|
||||
std::string out = "HTTP/1.1 " + std::to_string(r.status) + " " + reason(r.status) +
|
||||
"\r\nContent-Type: " + r.content_type +
|
||||
"\r\nContent-Length: " + std::to_string(r.body.size()) +
|
||||
"\r\nCache-Control: no-store\r\nConnection: close\r\n";
|
||||
if (r.status == 405)
|
||||
out += "Allow: GET\r\n";
|
||||
return out + "\r\n" + r.body;
|
||||
}
|
||||
bool printable(std::string_view s) {
|
||||
for (const char ch : s)
|
||||
if (ch <= ' ' || ch > '~')
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<std::unique_ptr<OpsServer>, OpsError> OpsServer::listen(std::string_view address) {
|
||||
const auto colon = address.rfind(':');
|
||||
if (colon == std::string_view::npos || colon + 1 >= address.size())
|
||||
return std::unexpected{OpsError::address};
|
||||
unsigned port{};
|
||||
const auto digits = address.substr(colon + 1);
|
||||
const auto [end, error] = std::from_chars(digits.data(), digits.data() + digits.size(), port);
|
||||
if (error != std::errc{} || end != digits.data() + digits.size() || port > 65535)
|
||||
return std::unexpected{OpsError::address};
|
||||
auto host = std::string(address.substr(0, colon));
|
||||
sockaddr_storage storage{};
|
||||
socklen_t length{};
|
||||
int family{};
|
||||
if (host.size() >= 2 && host.front() == '[' && host.back() == ']') {
|
||||
host = host.substr(1, host.size() - 2);
|
||||
auto* v6 = reinterpret_cast<sockaddr_in6*>(&storage);
|
||||
if (inet_pton(AF_INET6, host.c_str(), &v6->sin6_addr) != 1)
|
||||
return std::unexpected{OpsError::address};
|
||||
v6->sin6_family = AF_INET6;
|
||||
v6->sin6_port = htons(static_cast<std::uint16_t>(port));
|
||||
family = AF_INET6;
|
||||
length = sizeof(sockaddr_in6);
|
||||
} else {
|
||||
auto* v4 = reinterpret_cast<sockaddr_in*>(&storage);
|
||||
if (inet_pton(AF_INET, host.c_str(), &v4->sin_addr) != 1)
|
||||
return std::unexpected{OpsError::address};
|
||||
v4->sin_family = AF_INET;
|
||||
v4->sin_port = htons(static_cast<std::uint16_t>(port));
|
||||
family = AF_INET;
|
||||
length = sizeof(sockaddr_in);
|
||||
}
|
||||
const int fd = ::socket(family, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
|
||||
if (fd < 0)
|
||||
return std::unexpected{OpsError::socket};
|
||||
const int on = 1;
|
||||
::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on);
|
||||
if (::bind(fd, reinterpret_cast<sockaddr*>(&storage), length) != 0 || ::listen(fd, 16) != 0) {
|
||||
::close(fd);
|
||||
return std::unexpected{OpsError::socket};
|
||||
}
|
||||
sockaddr_storage bound{};
|
||||
socklen_t bound_length = sizeof bound;
|
||||
::getsockname(fd, reinterpret_cast<sockaddr*>(&bound), &bound_length);
|
||||
const auto actual = family == AF_INET6
|
||||
? ntohs(reinterpret_cast<sockaddr_in6*>(&bound)->sin6_port)
|
||||
: ntohs(reinterpret_cast<sockaddr_in*>(&bound)->sin_port);
|
||||
return std::unique_ptr<OpsServer>(new OpsServer(fd, actual));
|
||||
}
|
||||
|
||||
OpsServer::~OpsServer() {
|
||||
for (auto& c : connections_)
|
||||
::close(c.fd);
|
||||
::close(fd_);
|
||||
}
|
||||
|
||||
void OpsServer::respond(Connection& c, const OpsHandler& handler) {
|
||||
OpsResponse response;
|
||||
const auto line_end = c.in.find("\r\n");
|
||||
const std::string_view line = std::string_view(c.in).substr(0, line_end);
|
||||
const auto first = line.find(' ');
|
||||
const auto second = first == std::string_view::npos ? first : line.find(' ', first + 1);
|
||||
if (second == std::string_view::npos || line.substr(second + 1).rfind("HTTP/1.", 0) != 0) {
|
||||
response = {400, "bad request\n"};
|
||||
} else {
|
||||
const auto method = line.substr(0, first);
|
||||
const auto path = line.substr(first + 1, second - first - 1);
|
||||
if (method != "GET")
|
||||
response = {405, "method not allowed\n"};
|
||||
else if (path.empty() || path.front() != '/' || path.size() > 256 || !printable(path))
|
||||
response = {400, "bad request\n"};
|
||||
else
|
||||
response = handler(path);
|
||||
}
|
||||
if (response.body.size() > max_response_bytes)
|
||||
response = {500, "response too large\n"};
|
||||
c.out = encode(response);
|
||||
}
|
||||
|
||||
void OpsServer::serve(const OpsHandler& handler) {
|
||||
const auto now = Clock::now();
|
||||
for (;;) {
|
||||
const int fd = ::accept4(fd_, nullptr, nullptr, SOCK_NONBLOCK | SOCK_CLOEXEC);
|
||||
if (fd < 0)
|
||||
break; // EAGAIN: nothing pending (other errors: retried next call).
|
||||
if (connections_.size() >= max_connections) {
|
||||
::close(fd); // Bounded: excess connections are refused, never queued.
|
||||
++refused_;
|
||||
continue;
|
||||
}
|
||||
connections_.push_back({fd, {}, {}, 0, now + connection_deadline});
|
||||
}
|
||||
for (auto it = connections_.begin(); it != connections_.end();) {
|
||||
auto& c = *it;
|
||||
bool done = false;
|
||||
// Read before expiring: a complete request that waited behind a bounded host
|
||||
// step is still answered; only incomplete or unsent work expires.
|
||||
if (c.out.empty()) {
|
||||
char chunk[1024];
|
||||
const auto n = ::recv(c.fd, chunk, sizeof chunk, 0);
|
||||
if (n == 0 || (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)) {
|
||||
done = true;
|
||||
} else if (n > 0) {
|
||||
c.in.append(chunk, static_cast<std::size_t>(n));
|
||||
if (c.in.size() > max_request_bytes)
|
||||
c.out = encode({431, "request too large\n"});
|
||||
else if (c.in.find("\r\n\r\n") != std::string::npos)
|
||||
respond(c, handler);
|
||||
}
|
||||
}
|
||||
if (!done && !c.out.empty()) {
|
||||
const auto n = ::send(c.fd, c.out.data() + c.sent, c.out.size() - c.sent, MSG_NOSIGNAL);
|
||||
if (n > 0)
|
||||
c.sent += static_cast<std::size_t>(n);
|
||||
done = c.sent == c.out.size() ||
|
||||
(n < 0 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR);
|
||||
}
|
||||
// Slow client: an incomplete request expires at the deadline; a response the
|
||||
// client will not take gets one more deadline, then is dropped.
|
||||
if (!done && now > c.deadline && (c.out.empty() || now > c.deadline + connection_deadline))
|
||||
done = true;
|
||||
if (done) {
|
||||
::close(c.fd);
|
||||
it = connections_.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace evo::server
|
||||
Reference in New Issue
Block a user