Skip to content

Commit

Permalink
Merge branch 'master' into hb3
Browse files Browse the repository at this point in the history
  • Loading branch information
dangleptr authored May 17, 2019
2 parents ff04dfc + f2bdf99 commit c8bd872
Show file tree
Hide file tree
Showing 25 changed files with 875 additions and 87 deletions.
1 change: 0 additions & 1 deletion src/daemons/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ nebula_link_libraries(
${THRIFT_LIBRARIES}
wangle
)
install(TARGETS nebula-storaged DESTINATION bin)


add_executable(
Expand Down
3 changes: 2 additions & 1 deletion src/daemons/GraphDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ int main(int argc, char *argv[]) {
}

LOG(INFO) << "Starting Graph HTTP Service";
nebula::WebService::registerHandler("/graph", [] {
// http://127.0.0.1:XXXX/status is equivalent to http://127.0.0.1:XXXX
nebula::WebService::registerHandler("/status", [] {
return new nebula::graph::GraphHttpHandler();
});
status = nebula::WebService::start();
Expand Down
2 changes: 1 addition & 1 deletion src/daemons/MetaDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ int main(int argc, char *argv[]) {
}

LOG(INFO) << "Starting Meta HTTP Service";
nebula::WebService::registerHandler("/meta", [] {
nebula::WebService::registerHandler("/status", [] {
return new nebula::meta::MetaHttpHandler();
});
status = nebula::WebService::start();
Expand Down
20 changes: 11 additions & 9 deletions src/daemons/StorageDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,21 @@ DEFINE_string(data_path, "", "Root data path, multi paths should be split by com
DEFINE_string(local_ip, "", "Local ip speicified for NetworkUtils::getLocalIP");
DEFINE_bool(mock_server, true, "start mock server");
DEFINE_bool(daemonize, true, "Whether to run the process as a daemon");
DEFINE_string(pid_file, "pids/nebula-storaged.pid", "");

DEFINE_string(pid_file, "pids/nebula-storaged.pid", "File to hold the process id");
DEFINE_string(meta_server_addrs, "", "list of meta server addresses,"
"the format looks like ip1:port1, ip2:port2, ip3:port3");
DEFINE_int32(io_handlers, 10, "io handlers");


using nebula::Status;
using nebula::HostAddr;
using nebula::storage::StorageServiceHandler;
using nebula::kvstore::KVStore;
using nebula::meta::SchemaManager;
using nebula::meta::MetaClient;
using nebula::network::NetworkUtils;
using nebula::ProcessUtils;

static std::unique_ptr<apache::thrift::ThriftServer> gServer;

Expand All @@ -43,13 +52,6 @@ int main(int argc, char *argv[]) {
} else {
google::SetStderrLogging(google::INFO);
}
using nebula::HostAddr;
using nebula::storage::StorageServiceHandler;
using nebula::kvstore::KVStore;
using nebula::meta::SchemaManager;
using nebula::meta::MetaClient;
using nebula::network::NetworkUtils;
using nebula::ProcessUtils;

// Detect if the server has already been started
auto pidPath = FLAGS_pid_file;
Expand Down Expand Up @@ -125,7 +127,7 @@ int main(int argc, char *argv[]) {
schemaMan->init(metaClient.get());

LOG(INFO) << "Starting Storage HTTP Service";
nebula::WebService::registerHandler("/storage", [] {
nebula::WebService::registerHandler("/status", [] {
return new nebula::storage::StorageHttpHandler();
});

Expand Down
5 changes: 3 additions & 2 deletions src/graph/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
add_library(
graph_obj
OBJECT
graph_obj OBJECT
GraphFlags.cpp
GraphService.cpp
GraphHttpHandler.cpp
Expand Down Expand Up @@ -44,6 +43,8 @@ add_dependencies(
meta_thrift_obj
meta_client
schema_obj
stats_obj
)


add_subdirectory(test)
2 changes: 1 addition & 1 deletion src/graph/GraphFlags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ DEFINE_int32(num_accept_threads, 1, "Number of threads to accept incoming connec
DEFINE_bool(reuse_port, true, "Whether to turn on the SO_REUSEPORT option");
DEFINE_int32(listen_backlog, 1024, "Backlog of the listen socket");
DEFINE_string(listen_netdev, "any", "The network device to listen on");
DEFINE_string(pid_file, "nebula-graphd.pid", "File to hold the process id");
DEFINE_string(pid_file, "pids/nebula-graphd.pid", "File to hold the process id");

DEFINE_bool(redirect_stdout, true, "Whether to redirect stdout and stderr to separate files");
DEFINE_string(stdout_log_file, "graphd-stdout.log", "Destination filename of stdout");
Expand Down
106 changes: 103 additions & 3 deletions src/graph/GraphHttpHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "graph/GraphHttpHandler.h"
#include "webservice/Common.h"

#include <proxygen/httpserver/RequestHandler.h>
#include <proxygen/lib/http/ProxygenErrorEnum.h>
#include <proxygen/httpserver/ResponseBuilder.h>

Expand All @@ -17,27 +17,127 @@ using proxygen::HTTPMessage;
using proxygen::HTTPMethod;
using proxygen::ProxygenError;
using proxygen::UpgradeProtocol;
using proxygen::ResponseBuilder;

void GraphHttpHandler::onRequest(std::unique_ptr<HTTPMessage> headers) noexcept {
if (headers->getMethod().value() != HTTPMethod::GET) {
// Unsupported method
err_ = HttpCode::E_UNSUPPORTED_METHOD;
return;
}

if (headers->getQueryParamPtr("returnjson") != nullptr) {
returnJson_ = true;
}

void GraphHttpHandler::onRequest(std::unique_ptr<HTTPMessage>) noexcept {
auto* statusStr = headers->getQueryParamPtr("daemon");
if (statusStr != nullptr) {
folly::split(",", *statusStr, statusNames_, true);
}
}


void GraphHttpHandler::onBody(std::unique_ptr<folly::IOBuf>) noexcept {
// Do nothing, we only support GET
}


void GraphHttpHandler::onEOM() noexcept {
switch (err_) {
case HttpCode::E_UNSUPPORTED_METHOD:
ResponseBuilder(downstream_)
.status(405, "Method Not Allowed")
.sendWithEOM();
return;
default:
break;
}

// read graph daemon status
folly::dynamic vals = getStatus();
if (returnJson_) {
ResponseBuilder(downstream_)
.status(200, "OK")
.body(folly::toJson(vals))
.sendWithEOM();
} else {
ResponseBuilder(downstream_)
.status(200, "OK")
.body(toStr(vals))
.sendWithEOM();
}
}


void GraphHttpHandler::onUpgrade(UpgradeProtocol) noexcept {
// Do nothing
}


void GraphHttpHandler::requestComplete() noexcept {
delete this;
}


void GraphHttpHandler::onError(ProxygenError error) noexcept {
LOG(ERROR) << "Graph Handler : "
LOG(ERROR) << "Web service GraphHttpHandler got error : "
<< proxygen::getErrorString(error);
}


void GraphHttpHandler::addOneStatus(folly::dynamic& vals,
const std::string& statusName,
const std::string& statusValue) const {
folly::dynamic status = folly::dynamic::object();
status["name"] = statusName;
status["value"] = statusValue;
vals.push_back(std::move(status));
}


std::string GraphHttpHandler::readValue(std::string& statusName) {
folly::toLowerAscii(statusName);
if (statusName == "status") {
return "running";
} else {
return "unknown";
}
}


void GraphHttpHandler::readAllValue(folly::dynamic& vals) {
for (auto& sn : statusAllNames_) {
std::string statusValue = readValue(sn);
addOneStatus(vals, sn, statusValue);
}
}


folly::dynamic GraphHttpHandler::getStatus() {
auto status = folly::dynamic::array();
if (statusNames_.empty()) {
// Read all status
readAllValue(status);
} else {
for (auto& sn : statusNames_) {
std::string statusValue = readValue(sn);
addOneStatus(status, sn, statusValue);
}
}
return status;
}


std::string GraphHttpHandler::toStr(folly::dynamic& vals) const {
std::stringstream ss;
for (auto& counter : vals) {
auto& val = counter["value"];
ss << counter["name"].asString() << "="
<< val.asString()
<< "\n";
}
return ss.str();
}

} // namespace graph
} // namespace nebula
19 changes: 16 additions & 3 deletions src/graph/GraphHttpHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
namespace nebula {
namespace graph {

using nebula::HttpCode;

class GraphHttpHandler : public proxygen::RequestHandler {
public:
GraphHttpHandler() = default;
Expand All @@ -31,9 +33,20 @@ class GraphHttpHandler : public proxygen::RequestHandler {
void onError(proxygen::ProxygenError error) noexcept override;

private:
nebula::HttpCode err_{nebula::HttpCode::SUCCEEDED};
std::string name;
std::string value;
void addOneStatus(folly::dynamic& vals,
const std::string& statusName,
const std::string& statusValue) const;

std::string readValue(std::string& statusName);
void readAllValue(folly::dynamic& vals);
folly::dynamic getStatus();
std::string toStr(folly::dynamic& vals) const;

private:
HttpCode err_{HttpCode::SUCCEEDED};
bool returnJson_{false};
std::vector<std::string> statusNames_;
std::vector<std::string> statusAllNames_{"status"};
};

} // namespace graph
Expand Down
43 changes: 43 additions & 0 deletions src/graph/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,46 @@ nebula_link_libraries(
gtest
)
nebula_add_test(go_test)


add_executable(
graph_http_test
TestMain.cpp
TestEnv.cpp
TestBase.cpp
GraphHttpHandlerTest.cpp
$<TARGET_OBJECTS:graph_obj>
$<TARGET_OBJECTS:client_cpp_obj>
$<TARGET_OBJECTS:base_obj>
$<TARGET_OBJECTS:graph_thrift_obj>
$<TARGET_OBJECTS:storage_thrift_obj>
$<TARGET_OBJECTS:common_thrift_obj>
$<TARGET_OBJECTS:meta_thrift_obj>
$<TARGET_OBJECTS:schema_obj>
$<TARGET_OBJECTS:time_obj>
$<TARGET_OBJECTS:network_obj>
$<TARGET_OBJECTS:fs_obj>
$<TARGET_OBJECTS:thread_obj>
$<TARGET_OBJECTS:thrift_obj>
$<TARGET_OBJECTS:parser_obj>
$<TARGET_OBJECTS:filter_obj>
$<TARGET_OBJECTS:dataman_obj>
$<TARGET_OBJECTS:storage_client>
$<TARGET_OBJECTS:meta_client>
$<TARGET_OBJECTS:meta_service_handler>
$<TARGET_OBJECTS:kvstore_obj>
$<TARGET_OBJECTS:process_obj>
$<TARGET_OBJECTS:ws_obj>
$<TARGET_OBJECTS:stats_obj>
$<TARGET_OBJECTS:process_obj>
)
nebula_link_libraries(
graph_http_test
proxygenhttpserver
proxygenlib
${THRIFT_LIBRARIES}
${ROCKSDB_LIBRARIES}
wangle
gtest
)
nebula_add_test(graph_http_test)
Loading

0 comments on commit c8bd872

Please sign in to comment.