-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dup): support replica http api for duplication state query (#415)
- Loading branch information
Wu Tao
authored
Mar 16, 2020
1 parent
6af76cf
commit bda453c
Showing
10 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/dist/replication/lib/duplication/test/replica_http_service_test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) 2017-present, 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 "dist/replication/lib/replica_http_service.h" | ||
#include "duplication_test_base.h" | ||
|
||
namespace dsn { | ||
namespace replication { | ||
|
||
class replica_http_service_test : public duplication_test_base | ||
{ | ||
}; | ||
|
||
TEST_F(replica_http_service_test, query_duplication_handler) | ||
{ | ||
auto pri = stub->add_primary_replica(1, 1); | ||
auto sec = stub->add_non_primary_replica(1, 2); | ||
sec->as_secondary(); | ||
|
||
// primary confirmed_decree | ||
duplication_entry ent; | ||
ent.dupid = 1583306653; | ||
ent.progress[pri->get_gpid().get_partition_index()] = 1000; | ||
ent.status = duplication_status::DS_PAUSE; | ||
auto dup = dsn::make_unique<replica_duplicator>(ent, pri); | ||
add_dup(pri, std::move(dup)); | ||
|
||
sec->get_duplication_manager()->update_confirmed_decree_if_secondary(899); | ||
|
||
replica_http_service http_svc(stub.get()); | ||
|
||
http_request req; | ||
http_response resp; | ||
http_svc.query_duplication_handler(req, resp); | ||
ASSERT_EQ(resp.status_code, http_status_code::bad_request); // no appid | ||
|
||
req.query_args["appid"] = "2"; | ||
http_svc.query_duplication_handler(req, resp); | ||
ASSERT_EQ(resp.status_code, http_status_code::not_found); | ||
|
||
req.query_args["appid"] = "2xx"; | ||
http_svc.query_duplication_handler(req, resp); | ||
ASSERT_EQ(resp.status_code, http_status_code::bad_request); | ||
|
||
req.query_args["appid"] = "1"; | ||
http_svc.query_duplication_handler(req, resp); | ||
ASSERT_EQ(resp.status_code, http_status_code::ok); | ||
ASSERT_EQ(resp.body, | ||
R"({)" | ||
R"("1583306653":)" | ||
R"({"1.1":{"confirmed_decree":1000,"duplicating":false,"last_decree":1000}},)" | ||
R"("non-primaries":)" | ||
R"({"1.2":{"confirmed_decree":899}})" | ||
R"(})"); | ||
} | ||
|
||
} // namespace replication | ||
} // namespace dsn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) 2017-present, 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 <nlohmann/json.hpp> | ||
#include <fmt/format.h> | ||
#include "replica_http_service.h" | ||
#include "duplication/duplication_sync_timer.h" | ||
|
||
namespace dsn { | ||
namespace replication { | ||
|
||
void replica_http_service::query_duplication_handler(const http_request &req, http_response &resp) | ||
{ | ||
if (!_stub->_duplication_sync_timer) { | ||
resp.body = "duplication is not enabled [duplication_enabled=false]"; | ||
resp.status_code = http_status_code::not_found; | ||
return; | ||
} | ||
auto it = req.query_args.find("appid"); | ||
if (it == req.query_args.end()) { | ||
resp.body = "appid should not be empty"; | ||
resp.status_code = http_status_code::bad_request; | ||
return; | ||
} | ||
int32_t appid = -1; | ||
if (!buf2int32(it->second, appid) || appid < 0) { | ||
resp.status_code = http_status_code::bad_request; | ||
resp.body = fmt::format("invalid appid={}", it->second); | ||
return; | ||
} | ||
bool app_found = false; | ||
auto states = _stub->_duplication_sync_timer->get_dup_states(appid, &app_found); | ||
if (!app_found) { | ||
resp.status_code = http_status_code::not_found; | ||
resp.body = fmt::format("no primary for app [appid={}]", appid); | ||
return; | ||
} | ||
if (states.empty()) { | ||
resp.status_code = http_status_code::not_found; | ||
resp.body = fmt::format("no duplication assigned for app [appid={}]", appid); | ||
return; | ||
} | ||
|
||
nlohmann::json json; | ||
for (const auto &s : states) { | ||
json[std::to_string(s.first)][s.second.id.to_string()] = nlohmann::json{ | ||
{"duplicating", s.second.duplicating}, | ||
{"not_confirmed", s.second.not_confirmed}, | ||
{"not_duplicated", s.second.not_duplicated}, | ||
}; | ||
} | ||
resp.status_code = http_status_code::ok; | ||
resp.body = json.dump(); | ||
} | ||
|
||
} // namespace replication | ||
} // namespace dsn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) 2017-present, 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 "replica_stub.h" | ||
|
||
namespace dsn { | ||
namespace replication { | ||
|
||
class replica_http_service : public http_service | ||
{ | ||
public: | ||
explicit replica_http_service(replica_stub *stub) : _stub(stub) | ||
{ | ||
register_handler("duplication", | ||
std::bind(&replica_http_service::query_duplication_handler, | ||
this, | ||
std::placeholders::_1, | ||
std::placeholders::_2), | ||
"ip:port/replica/duplication?appid=<appid>"); | ||
} | ||
|
||
std::string path() const override { return "replica"; } | ||
|
||
void query_duplication_handler(const http_request &req, http_response &resp); | ||
|
||
private: | ||
replica_stub *_stub; | ||
}; | ||
|
||
} // namespace replication | ||
} // namespace dsn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters