Skip to content

Commit

Permalink
feat: add update rocksdb scenario interface (#707)
Browse files Browse the repository at this point in the history
  • Loading branch information
hycdong authored Dec 26, 2020
1 parent 37a13a9 commit 00e3d98
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/meta/meta_http_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,39 @@ void meta_http_service::start_compaction_handler(const http_request &req, http_r
update_app_env(info.app_name, keys, values, resp);
}

void meta_http_service::update_scenario_handler(const http_request &req, http_response &resp)
{
if (!redirect_if_not_primary(req, resp)) {
return;
}

// validate paramters
usage_scenario_info info;
bool ret = json::json_forwarder<usage_scenario_info>::decode(req.body, info);
if (!ret) {
resp.body = "invalid request structure";
resp.status_code = http_status_code::bad_request;
return;
}
if (info.app_name.empty()) {
resp.body = "app_name should not be empty";
resp.status_code = http_status_code::bad_request;
return;
}
if (info.scenario.empty() || (info.scenario != "bulk_load" && info.scenario != "normal")) {
resp.body = "scenario should ony be 'normal' or 'bulk_load'";
resp.status_code = http_status_code::bad_request;
return;
}

// create configuration_update_app_env_request
std::vector<std::string> keys;
std::vector<std::string> values;
keys.emplace_back(replica_envs::ROCKSDB_USAGE_SCENARIO);
values.emplace_back(info.scenario);
update_app_env(info.app_name, keys, values, resp);
}

bool meta_http_service::redirect_if_not_primary(const http_request &req, http_response &resp)
{
#ifdef DSN_MOCK_TEST
Expand Down
15 changes: 15 additions & 0 deletions src/meta/meta_http_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ struct manual_compaction_info
trigger_time)
};

struct usage_scenario_info
{
std::string app_name;
std::string scenario; // normal or bulk_load
DEFINE_JSON_SERIALIZATION(app_name, scenario)
};

class meta_service;
class meta_http_service : public http_service
{
Expand Down Expand Up @@ -99,6 +106,13 @@ class meta_http_service : public http_service
std::placeholders::_1,
std::placeholders::_2),
"ip:port/meta/start_compaction");
// request body should be usage_scenario_info
register_handler("app/usage_scenario",
std::bind(&meta_http_service::update_scenario_handler,
this,
std::placeholders::_1,
std::placeholders::_2),
"ip:port/meta/app/usage_scenario");
}

std::string path() const override { return "meta"; }
Expand All @@ -113,6 +127,7 @@ class meta_http_service : public http_service
void start_bulk_load_handler(const http_request &req, http_response &resp);
void query_bulk_load_handler(const http_request &req, http_response &resp);
void start_compaction_handler(const http_request &req, http_response &resp);
void update_scenario_handler(const http_request &req, http_response &resp);

private:
// set redirect location if current server is not primary
Expand Down
37 changes: 37 additions & 0 deletions src/meta/test/meta_http_service_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@ class meta_bulk_load_http_test : public meta_test_base
return resp;
}

http_response test_update_scenario(std::string req_body_json)
{
http_request req;
http_response resp;
req.body = blob::create_from_bytes(std::move(req_body_json));
_mhs->update_scenario_handler(req, resp);
return resp;
}

void mock_bulk_load_context(const bulk_load_status::type &status)
{
auto app = find_app(APP_NAME);
Expand Down Expand Up @@ -346,5 +355,33 @@ TEST_F(meta_bulk_load_http_test, start_compaction_test)
}
}

TEST_F(meta_bulk_load_http_test, update_scenario_test)
{
struct update_scenario_test
{
std::string request_json;
http_status_code expected_code;
std::string expected_response_json;
} tests[] = {{R"({"app":"test_bulk_load","scenario":"normal"})",
http_status_code::bad_request,
"invalid request structure"},
{R"({"app_name":"test_bulk_load","scenario":"wrong"})",
http_status_code::bad_request,
"scenario should ony be 'normal' or 'bulk_load'"},
{R"({"app_name":"test_bulk_load","scenario":"bulk_load"})",
http_status_code::ok,
R"({"error":"ERR_OK","hint_message":""})"}};

for (const auto &test : tests) {
http_response resp = test_update_scenario(test.request_json);
ASSERT_EQ(resp.status_code, test.expected_code);
std::string expected_json = test.expected_response_json;
if (test.expected_code == http_status_code::ok) {
expected_json += "\n";
}
ASSERT_EQ(resp.body, expected_json);
}
}

} // namespace replication
} // namespace dsn

0 comments on commit 00e3d98

Please sign in to comment.