diff --git a/src/fne/restapi/RESTAPI.cpp b/src/fne/restapi/RESTAPI.cpp index 7d22affe..dfeb344b 100644 --- a/src/fne/restapi/RESTAPI.cpp +++ b/src/fne/restapi/RESTAPI.cpp @@ -41,6 +41,29 @@ using namespace fne_restapi; #define REST_API_BIND(funcAddr, classInstance) std::bind(&funcAddr, classInstance, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3) +// --------------------------------------------------------------------------- +// Constants +// --------------------------------------------------------------------------- + +#define HEALTH_INIT "INITIALIZING" +#define HEALTH_IDLE "IDLE" +#define HEALTH_DEGRADED "DEGRADED" +#define HEALTH_FAILED "FAILED" +#define HEALTH_PARTITIONED "PARTITIONED" +#define HEALTH_HEALTHY "HEALTHY" +#define HEALTH_FAILED "FAILED" +#define HEALTH_UNAVAILABLE "UNAVAILABLE" + +#define HEALTH_REASON_ALL_UPSTREAMS_DISCONNECTED "ALL_UPSTREAMS_DISCONNECTED" +#define HEALTH_REASON_TRAFFIC_DATA_PLANE_STALLED "TRAFFIC_DATA_PLANE_STALLED" +#define HEALTH_REASON_METADATA_NETWORK_UNAVAILABLE "METADATA_NETWORK_UNAVAILABLE" +#define HEALTH_REASON_METADATA_DATA_PLANE_STALLED "METADATA_DATA_PLANE_STALLED" +#define HEALTH_REASON_TRAFFIC_INBOUND_QUEUE_STALLED "TRAFFIC_INBOUND_QUEUE_STALLED" +#define HEALTH_REASON_METADATA_INBOUND_QUEUE_STALLED "METADATA_INBOUND_QUEUE_STALLED" +#define HEALTH_REASON_UPSTREAM_CONNECTIVITY_DEGRADED "UPSTREAM_CONNECTIVITY_DEGRADED" +#define HEALTH_REASON_METADATA_UPDATES_MISSED "METADATA_UPDATES_MISSED" +#define HEALTH_REASON_TRAFFIC_NETWORK_UNAVAILABLE "TRAFFIC_NETWORK_UNAVAILABLE" + // --------------------------------------------------------------------------- // Global Functions // --------------------------------------------------------------------------- @@ -2225,15 +2248,15 @@ void RESTAPI::restAPI_GetHealth(const HTTPPayload& request, HTTPPayload& reply, health["observedAtMs"].set(now); json::object process = json::object(); - process["status"].set("healthy"); + process["status"].set(HEALTH_HEALTHY); const bool restApiResponsive = true; process["restApiResponsive"].set(restApiResponsive); health["process"].set(process); // check if the traffic network is available and healthy if (m_network == nullptr) { - health["overall"].set("failed"); - reasonCodes.push_back(json::value("TRAFFIC_NETWORK_UNAVAILABLE")); + health["overall"].set(HEALTH_FAILED); + reasonCodes.push_back(json::value(HEALTH_REASON_TRAFFIC_NETWORK_UNAVAILABLE)); reasons.push_back(json::value("traffic network is not initialized")); health["reasonCodes"].set(reasonCodes); health["reasons"].set(reasons); @@ -2262,17 +2285,17 @@ void RESTAPI::restAPI_GetHealth(const HTTPPayload& request, HTTPPayload& reply, // construct JSON objects representing the status of the traffic, metadata, and overall data planes json::object trafficPlane = json::object(); - trafficPlane["status"].set(trafficPlaneInitializing ? "initializing" : (trafficPlaneStalled ? "failed" : "healthy")); + trafficPlane["status"].set(trafficPlaneInitializing ? HEALTH_INIT : (trafficPlaneStalled ? HEALTH_FAILED : HEALTH_HEALTHY)); trafficPlane["epoch"].set(trafficEpoch); trafficPlane["lastTickAgeMs"].set(trafficTickAge); json::object metadataPlane = json::object(); - metadataPlane["status"].set(metadataUnavailable ? "unavailable" : (metadataPlaneInitializing ? "initializing" : (metadataPlaneStalled ? "failed" : "healthy"))); + metadataPlane["status"].set(metadataUnavailable ? HEALTH_UNAVAILABLE : (metadataPlaneInitializing ? HEALTH_INIT : (metadataPlaneStalled ? HEALTH_FAILED : HEALTH_HEALTHY))); metadataPlane["epoch"].set(metadataEpoch); metadataPlane["lastTickAgeMs"].set(metadataTickAge); json::object dataPlane = json::object(); - dataPlane["status"].set(dataPlaneInitializing ? "initializing" : (dataPlaneStalled ? "failed" : "healthy")); + dataPlane["status"].set(dataPlaneInitializing ? HEALTH_INIT : (dataPlaneStalled ? HEALTH_FAILED : HEALTH_HEALTHY)); dataPlane["loopStallThresholdMs"].set(loopStallThreshold); dataPlane["traffic"].set(trafficPlane); dataPlane["metadata"].set(metadataPlane); @@ -2316,7 +2339,7 @@ void RESTAPI::restAPI_GetHealth(const HTTPPayload& request, HTTPPayload& reply, metadataQueue["progressing"].set(metadataQueueProgressing); json::object queues = json::object(); - queues["status"].set(queueStalled ? "failed" : "healthy"); + queues["status"].set(queueStalled ? HEALTH_FAILED : HEALTH_HEALTHY); queues["stallThresholdMs"].set(queueStallThreshold); queues["trafficInboundWorkerQueue"].set(inboundQueue); queues["metadataInboundWorkerQueue"].set(metadataQueue); @@ -2386,7 +2409,7 @@ void RESTAPI::restAPI_GetHealth(const HTTPPayload& request, HTTPPayload& reply, const bool partitioned = configuredUpstreams > 0U && connectedUpstreams == 0U; const bool upstreamDegraded = configuredUpstreams > connectedUpstreams && connectedUpstreams > 0U; json::object connectivity = json::object(); - connectivity["status"].set(partitioned ? "partitioned" : ((upstreamDegraded || metadataDegraded) ? "degraded" : "healthy")); + connectivity["status"].set(partitioned ? HEALTH_PARTITIONED : ((upstreamDegraded || metadataDegraded) ? HEALTH_DEGRADED : HEALTH_HEALTHY)); connectivity["configuredUpstreams"].set(configuredUpstreams); connectivity["connectedUpstreams"].set(connectedUpstreams); connectivity["connectedPeers"].set(connectedPeers); @@ -2402,7 +2425,7 @@ void RESTAPI::restAPI_GetHealth(const HTTPPayload& request, HTTPPayload& reply, const bool trafficSeen = lastProtocolPacket != 0U; const bool trafficIdle = !trafficSeen || ageMs(lastProtocolPacket) > trafficIdleThreshold; json::object traffic = json::object(); - traffic["status"].set(trafficIdle ? "idle" : "healthy"); + traffic["status"].set(trafficIdle ? HEALTH_IDLE : HEALTH_HEALTHY); const uint64_t packetsReceived = m_network->m_healthPacketsReceived.load(std::memory_order_relaxed); const uint64_t protocolPacketsReceived = m_network->m_healthProtocolPacketsReceived.load(std::memory_order_relaxed); const uint64_t lastPacketAge = ageMs(lastPacket); @@ -2424,7 +2447,7 @@ void RESTAPI::restAPI_GetHealth(const HTTPPayload& request, HTTPPayload& reply, json::object spanningTree = json::object(); const bool treeAvailable = m_network->m_treeRoot != nullptr; const uint32_t knownNodes = treeAvailable ? network::SpanningTree::countChildren(m_network->m_treeRoot) + 1U : 0U; - spanningTree["status"].set(treeAvailable ? "healthy" : "unavailable"); + spanningTree["status"].set(treeAvailable ? HEALTH_HEALTHY : HEALTH_UNAVAILABLE); const bool spanningTreeEnabled = m_network->m_enableSpanningTree; const uint32_t rootId = treeAvailable ? m_network->m_treeRoot->id() : 0U; spanningTree["enabled"].set(spanningTreeEnabled); @@ -2434,56 +2457,56 @@ void RESTAPI::restAPI_GetHealth(const HTTPPayload& request, HTTPPayload& reply, health["spanningTree"].set(spanningTree); // determine the overall health status of the network based on individual components - std::string overall = "healthy"; + std::string overall = HEALTH_HEALTHY; if (dataPlaneInitializing) { - overall = "initializing"; + overall = HEALTH_INIT; } - if (trafficIdle && overall == "healthy") { - overall = "idle"; + if (trafficIdle && overall == HEALTH_HEALTHY) { + overall = HEALTH_IDLE; } if (upstreamDegraded || metadataDegraded || !treeAvailable) { - overall = "degraded"; + overall = HEALTH_DEGRADED; } if (partitioned) { - overall = "partitioned"; - reasonCodes.push_back(json::value("ALL_UPSTREAMS_DISCONNECTED")); + overall = HEALTH_PARTITIONED; + reasonCodes.push_back(json::value(HEALTH_REASON_ALL_UPSTREAMS_DISCONNECTED)); reasons.push_back(json::value("all configured upstream FNE connections are disconnected")); } if (dataPlaneStalled || queueStalled) { - overall = "failed"; + overall = HEALTH_FAILED; } if (trafficPlaneStalled) { - reasonCodes.push_back(json::value("TRAFFIC_DATA_PLANE_STALLED")); + reasonCodes.push_back(json::value(HEALTH_REASON_TRAFFIC_DATA_PLANE_STALLED)); reasons.push_back(json::value("traffic processing loop is not progressing")); } if (metadataPlaneStalled) { - reasonCodes.push_back(json::value(metadataUnavailable ? "METADATA_NETWORK_UNAVAILABLE" : "METADATA_DATA_PLANE_STALLED")); + reasonCodes.push_back(json::value(metadataUnavailable ? HEALTH_REASON_METADATA_NETWORK_UNAVAILABLE : HEALTH_REASON_METADATA_DATA_PLANE_STALLED)); reasons.push_back(json::value(metadataUnavailable ? "metadata network is not initialized" : "metadata processing loop is not progressing")); } if (trafficQueueStalled) { - reasonCodes.push_back(json::value("TRAFFIC_INBOUND_QUEUE_STALLED")); + reasonCodes.push_back(json::value(HEALTH_REASON_TRAFFIC_INBOUND_QUEUE_STALLED)); reasons.push_back(json::value("traffic inbound worker queue contains stale work")); } if (metadataQueueStalled) { - reasonCodes.push_back(json::value("METADATA_INBOUND_QUEUE_STALLED")); + reasonCodes.push_back(json::value(HEALTH_REASON_METADATA_INBOUND_QUEUE_STALLED)); reasons.push_back(json::value("metadata inbound worker queue contains stale work")); } if (upstreamDegraded) { - reasonCodes.push_back(json::value("UPSTREAM_CONNECTIVITY_DEGRADED")); + reasonCodes.push_back(json::value(HEALTH_REASON_UPSTREAM_CONNECTIVITY_DEGRADED)); reasons.push_back(json::value("one or more configured upstream FNE connections are disconnected")); } if (metadataDegraded) { - reasonCodes.push_back(json::value("METADATA_UPDATES_MISSED")); + reasonCodes.push_back(json::value(HEALTH_REASON_METADATA_UPDATES_MISSED)); reasons.push_back(json::value("one or more child FNEs have missed metadata updates")); }