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 7, 2024
1 parent a6f0cab commit cc0a389
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
46 changes: 21 additions & 25 deletions presto-native-execution/presto_cpp/main/PrestoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
#include "velox/functions/prestosql/window/WindowFunctionsRegistration.h"
#include "velox/serializers/PrestoSerializer.h"


#ifdef PRESTO_ENABLE_REMOTE_FUNCTIONS
#include "presto_cpp/main/RemoteFunctionRegisterer.h"
#endif
Expand Down Expand Up @@ -373,7 +372,7 @@ void PrestoServer::run() {
proxygen::HTTPMessage* /*message*/,
const std::vector<std::unique_ptr<folly::IOBuf>>& body ,
proxygen::ResponseHandler* downstream) {
server->handleGracefulShutdown(body,downstream);
server->handleGracefulShutdown(body, downstream);
});
httpServer_->registerGet(
"/v1/status",
Expand Down Expand Up @@ -1414,31 +1413,28 @@ void PrestoServer::reportNodeStatus(proxygen::ResponseHandler* downstream) {
http::sendOkResponse(downstream, json(fetchNodeStatus()));
}

void PrestoServer::handleGracefulShutdown(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();
}
void PrestoServer::handleGracefulShutdown(const std::vector<std::unique_ptr<folly::IOBuf>>& body, proxygen::ResponseHandler* downstream){
auto requestBody = body[0]->moveToFbString();
// Remove the trailing newline characters from the requestBody
requestBody.erase(requestBody.length()-2);
if (requestBody == "\"SHUTTING_DOWN\"") {
// Print message and initiate shutdown
LOG(INFO) << "Shutdown requested";
if (nodeState() == NodeState::kActive) {
// Call stop() to handle the shutdown process
std::thread([this]() {
this->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);
if (body.size()==1 && requestBody == "\"SHUTTING_DOWN\"") {
// Print message and initiate shutdown
LOG(INFO) << "Shutdown requested";
if (nodeState() == NodeState::kActive) {
// Call stop() to handle the shutdown process
std::thread([this]() {
this->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);
// Throw a 400 Bad Request error if the body has some other value
http::sendErrorResponse(
downstream,
"Bad Request", http::kHttpBadRequest);
}
}

Expand Down
2 changes: 1 addition & 1 deletion presto-native-execution/presto_cpp/main/PrestoServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class PrestoServer {

void reportNodeStatus(proxygen::ResponseHandler* downstream);

void handleGracefulShutdown(const std::vector<std::unique_ptr<folly::IOBuf>>& body , proxygen::ResponseHandler* downstream);
void handleGracefulShutdown(const std::vector<std::unique_ptr<folly::IOBuf>>& body, proxygen::ResponseHandler* downstream);

protocol::NodeStatus fetchNodeStatus();

Expand Down

0 comments on commit cc0a389

Please sign in to comment.