Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

http: embed http server in both meta and replica #139

Merged
merged 30 commits into from
Aug 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a73f00f
rpc: minor refactoring on message parser
Jul 13, 2018
03aaf7a
rpc: remove message_parser::factory2
Jul 14, 2018
1caf570
Merge branch 'master' of https://github.com/XiaoMi/rdsn
Jul 14, 2018
ea330f4
fix
Jul 14, 2018
633a86b
remove char_ptr
Jul 14, 2018
db77fa3
update
Jul 14, 2018
8d00cac
utility: update blob
Jul 15, 2018
b2ad911
http: initiate a web server
Jul 15, 2018
e09accb
refactor http receiving
Jul 16, 2018
bfe635d
remove rpc_message::write_append
Jul 16, 2018
1aada86
refactor http sending
Jul 16, 2018
3eb43ad
Merge branch 'master' of https://github.com/XiaoMi/rdsn into http
Jul 16, 2018
1c27d0b
add root_http_service
Jul 16, 2018
b3bc7dc
add http server for meta app
Jul 16, 2018
44e8f5e
fix http response
Jul 16, 2018
efa32ac
update
Jul 17, 2018
8d58b10
Merge branch 'master' of https://github.com/XiaoMi/rdsn into http
Jul 17, 2018
6d711f3
Merge branch 'master' of https://github.com/XiaoMi/rdsn into http
Jul 17, 2018
5e02960
remove redundant files
Jul 17, 2018
026a477
fix
Jul 17, 2018
b8b36c5
fix
Jul 17, 2018
83894cc
fix
Jul 18, 2018
5aad349
fix fail_point_test failure
Jul 18, 2018
ba843e7
Merge branch 'master' of https://github.com/XiaoMi/rdsn into http
Jul 18, 2018
d4fc4ad
fix fail_point_test failure
Jul 18, 2018
8f5764f
fix fail_point_test failure
Jul 18, 2018
8f04e6e
test http url path parsing
Jul 19, 2018
652fcaf
Merge branch 'master' into http
Aug 9, 2018
a659426
fix comment
Aug 10, 2018
8532285
Merge branch 'master' into http
qinzuoyan Aug 10, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/dsn/dist/replication/meta_service_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class test_checker;
}

namespace dsn {

class http_server;

namespace service {

class meta_service_app : public service_app
Expand All @@ -69,6 +72,7 @@ class meta_service_app : public service_app
friend class ::dsn::replication::replication_checker;
friend class ::dsn::replication::test::test_checker;
std::unique_ptr<dsn::replication::meta_service> _service;
std::unique_ptr<http_server> _http_server;
};
}
}
4 changes: 4 additions & 0 deletions include/dsn/dist/replication/replication_service_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
#include <dsn/cpp/service_app.h>

namespace dsn {

class http_server;

namespace replication {

class replication_checker;
Expand Down Expand Up @@ -68,6 +71,7 @@ class replication_service_app : public ::dsn::service_app
friend class ::dsn::replication::replication_checker;
friend class ::dsn::replication::test::test_checker;
replica_stub_ptr _stub;
std::unique_ptr<http_server> _http_server;

static const char *replica_service_app_info(int argc, char **argv);
static void replica_service_app_info_free(const char *response);
Expand Down
88 changes: 88 additions & 0 deletions include/dsn/tool-api/http_server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) 2018, Xiaomi, Inc. All rights reserved.
// This source code is licensed under the Apache License Version 2.0, which
// can be found in the LICENSE file in the root directory of this source tree.

#pragma once

#include <dsn/c/api_common.h>
#include <dsn/tool-api/rpc_message.h>
#include <dsn/cpp/serverlet.h>
#include <dsn/utility/errors.h>

namespace dsn {

struct http_request
{
static error_with<http_request> parse(dsn_message_t msg);

std::pair<std::string, std::string> service_method;
blob body;
blob full_url;
};

enum class http_status_code
{
ok, // 200
bad_request, // 400
not_found, // 404
internal_server_error, // 500
};

extern std::string http_status_code_to_string(http_status_code code);

struct http_response
{
std::string body;
http_status_code status_code{http_status_code::ok};
std::string content_type = "text/plain";

ref_ptr<message_ex> to_message(dsn_message_t req) const;
};

class http_service
{
public:
typedef std::function<void(const http_request &req, http_response &resp)> http_callback;

virtual std::string path() const = 0;

void register_handler(std::string path, http_callback cb)
{
_cb_map.emplace(std::move(path), std::move(cb));
}

void call(const http_request &req, http_response &resp)
{
auto it = _cb_map.find(req.service_method.second);
if (it != _cb_map.end()) {
it->second(req, resp);
} else {
resp.status_code = http_status_code::bad_request;
resp.body = "method not found";
}
}

private:
std::map<std::string, http_callback> _cb_map;
};

class http_server : public serverlet<http_server>
{
public:
http_server();

~http_server() override = default;

void add_service(http_service *service);

void serve(dsn_message_t msg);

private:
std::map<std::string, std::unique_ptr<http_service>> _service_map;
};

/// The rpc code for all the HTTP RPCs.
/// Since http is used only for system monitoring, it is restricted to lowest priority.
DEFINE_TASK_CODE_RPC(RPC_HTTP_SERVICE, TASK_PRIORITY_LOW, THREAD_POOL_DEFAULT);

} // namespace dsn
16 changes: 15 additions & 1 deletion include/dsn/tool-api/rpc_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ struct fast_code

typedef struct message_header
{
// For thrift protocol this is "THFT".
// For dsn protocol this is "RDSN".
// For http protocol this is either a "GET " or "POST".
uint32_t hdr_type;

uint32_t hdr_version;
uint32_t hdr_length;
uint32_t hdr_crc32;
Expand Down Expand Up @@ -154,8 +158,19 @@ class message_ex : public ref_counter,
int thread_hash = 0,
uint64_t partition_hash = 0);

/// This method is only used for receiving request.
/// The returned message:
/// - msg->buffers[0] = message_header
/// - msg->buffers[1] = data
DSN_API static message_ex *create_receive_message_with_standalone_header(const blob &data);

/// The returned message:
/// - msg->buffers[0] = message_header
/// - msg->_is_read = false
/// - msg->_rw_index = 0
/// - msg->_rw_offset = 48 (size of message_header)
DSN_API message_ex *create_response();

DSN_API message_ex *copy(bool clone_content, bool copy_for_receive);
DSN_API message_ex *copy_and_prepare_send(bool clone_content);

Expand All @@ -164,7 +179,6 @@ class message_ex : public ref_counter,
//
DSN_API void write_next(void **ptr, size_t *size, size_t min_size);
DSN_API void write_commit(size_t size);
DSN_API void write_append(const blob &data);
DSN_API bool read_next(void **ptr, size_t *size);
bool read_next(blob &data);
DSN_API void read_commit(size_t size);
Expand Down
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ add_library(dsn_runtime STATIC
$<TARGET_OBJECTS:dsn.tools.common>
$<TARGET_OBJECTS:dsn.tools.hpc>
$<TARGET_OBJECTS:dsn.tools.simulator>
$<TARGET_OBJECTS:dsn.tools.http>
)
16 changes: 0 additions & 16 deletions src/core/core/rpc_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,22 +558,6 @@ void message_ex::write_commit(size_t size)
this->header->body_length += (int)size;
}

void message_ex::write_append(const blob &data)
{
// printf("%p %s\n", this, __FUNCTION__);
dassert(!this->_is_read && this->_rw_committed,
"there are pending msg write not committed"
", please invoke dsn_msg_write_next and dsn_msg_write_commit in pairs");

int size = data.length();
if (size > 0) {
this->_rw_index++;
this->_rw_offset += size;
this->buffers.push_back(data);
this->header->body_length += size;
}
}

bool message_ex::read_next(void **ptr, size_t *size)
{
// printf("%p %s %d\n", this, __FUNCTION__, utils::get_current_tid());
Expand Down
8 changes: 4 additions & 4 deletions src/core/tests/fail_point_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ TEST(fail_point, print)
TEST(fail_point, frequency_and_count)
{
fail_point p;
p.set_action("80%100*return()");
p.set_action("80%10000*return()");

int cnt = 0;
double times = 0;
while (cnt < 100) {
while (cnt < 10000) {
if (p.eval() != nullptr) {
cnt++;
}
times++;
}
ASSERT_TRUE(100 / 0.9 < times);
ASSERT_TRUE(100 / 0.7 > times);
ASSERT_TRUE(10000 / 0.9 < times);
ASSERT_TRUE(10000 / 0.7 > times);

for (int i = 0; i < times; i++) {
ASSERT_EQ(p.eval(), nullptr);
Expand Down
43 changes: 43 additions & 0 deletions src/core/tests/http_server_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2018, Xiaomi, Inc. All rights reserved.
// This source code is licensed under the Apache License Version 2.0, which
// can be found in the LICENSE file in the root directory of this source tree.

#include <dsn/tool-api/http_server.h>
#include <gtest/gtest.h>

namespace dsn {

TEST(http_server, parse_url)
{
struct test_case
{
std::string url;

error_code err;
std::pair<std::string, std::string> result;
} tests[] = {
{"http://127.0.0.1:34601", ERR_OK, {"", ""}},
{"http://127.0.0.1:34601/", ERR_OK, {"", ""}},
{"http://127.0.0.1:34601///", ERR_OK, {"", ""}},
{"http://127.0.0.1:34601/threads", ERR_OK, {"threads", ""}},
{"http://127.0.0.1:34601/threads/", ERR_OK, {"threads", ""}},
{"http://127.0.0.1:34601//pprof/heap/", ERR_OK, {"pprof", "heap"}},
{"http://127.0.0.1:34601//pprof///heap", ERR_OK, {"pprof", "heap"}},
{"http://127.0.0.1:34601/pprof/heap/arg/", ERR_INVALID_PARAMETERS, {}},
};

for (auto tt : tests) {
ref_ptr<message_ex> m = message_ex::create_receive_message_with_standalone_header(
blob::create_from_bytes(std::string("POST")));
m->buffers.emplace_back(blob::create_from_bytes(std::string(tt.url)));

auto res = http_request::parse(m.get());
if (res.is_ok()) {
ASSERT_EQ(res.get_value().service_method, tt.result) << tt.url;
} else {
ASSERT_EQ(res.get_error().code(), tt.err);
}
}
}

} // namespace dsn
1 change: 1 addition & 0 deletions src/core/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_subdirectory(common)
add_subdirectory(simulator)
add_subdirectory(hpc)
add_subdirectory(http)
Loading