Skip to content

Commit

Permalink
Added PUT method in /v1/info/state for transitioning the server to th…
Browse files Browse the repository at this point in the history
…e SHUTING_DOWN state for graceful shutdown
  • Loading branch information
Shakyan Kushwaha authored and Shakyan Kushwaha committed Oct 4, 2024
1 parent 7c814ae commit 32b7b6a
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions presto-native-execution/presto_cpp/main/PrestoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,44 @@ void PrestoServer::run() {
json infoStateJson = convertNodeState(server->nodeState());
http::sendOkResponse(downstream, infoStateJson);
});
httpServer_->registerPut(
"/v1/info/state",
[server = this](
proxygen::HTTPMessage* /*message*/,
const std::vector<std::unique_ptr<folly::IOBuf>>& body ,
proxygen::ResponseHandler* downstream) {
std::string requestBody;
for (const auto& buf : body) {
// Convert IOBuf to fbstring and then to std::string
requestBody += buf->moveToFbString().toStdString();
}
try {
folly::dynamic jsonBody = folly::parseJson(requestBody);
if (jsonBody == "SHUTTING_DOWN") {
// Print message and initiate shutdown
LOG(INFO) << "Shutdown requested";
if (server->nodeState() == NodeState::kActive) {
// Call stop() to handle the shutdown process
std::thread([server]() {
server->stop();
}).detach();
} else {
// Print message and indicate that shutdown is already requested
LOG(INFO) << "Shutdown already requested";
}
http::sendOkResponse(downstream);
} else {
// Throw a 400 Bad Request error if the body has some other value
http::sendErrorResponse(
downstream,
"Bad Request: Body must contain 'SHUTTING_DOWN'.",400);
}
}
catch (const folly::json::parse_error& e) {
// Handle JSON parse errors and send a 400 Bad Request response
http::sendErrorResponse(downstream, e.what(), 400); // 400 Bad Request
}
});
httpServer_->registerGet(
"/v1/status",
[server = this](
Expand Down

0 comments on commit 32b7b6a

Please sign in to comment.