Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Shakyan Kushwaha authored and Shakyan Kushwaha committed Oct 4, 2024
1 parent d79ef4f commit d54c6a2
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions presto-native-execution/presto_cpp/main/PrestoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
#include "velox/functions/prestosql/registration/RegistrationFunctions.h"
#include "velox/functions/prestosql/window/WindowFunctionsRegistration.h"
#include "velox/serializers/PrestoSerializer.h"
#include <folly/json_patch.h>
#include <folly/dynamic.h>
#include <folly/json.h>


#ifdef PRESTO_ENABLE_REMOTE_FUNCTIONS
#include "presto_cpp/main/RemoteFunctionRegisterer.h"
Expand Down Expand Up @@ -376,39 +380,39 @@ void PrestoServer::run() {
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=getRequestBody(body);
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'.",http::kHttpBadRequest);
}
}
catch (const folly::json::parse_error& e) {
// Handle JSON parse errors and send a 400 Bad Request response
http::sendErrorResponse(downstream, e.what(), http::kHttpBadRequest); // 400 Bad Request
}
});
"/v1/info/state",
[server = this](
proxygen::HTTPMessage* /*message*/,
const std::vector<std::unique_ptr<folly::IOBuf>>& body ,
proxygen::ResponseHandler* downstream) {
std::string requestBody=getRequestBody(body);
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 node is inactive or shutdown is already requested
LOG(INFO) << "Node is inactive or shutdown is 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'.",http::kHttpBadRequest);
}
}
catch (const folly::json::parse_error& e) {
// Handle JSON parse errors and send a 400 Bad Request response
http::sendErrorResponse(downstream, e.what(), http::kHttpBadRequest); // 400 Bad Request
}
});
httpServer_->registerGet(
"/v1/status",
[server = this](
Expand Down

0 comments on commit d54c6a2

Please sign in to comment.