Skip to content

Commit

Permalink
fix divide zone should be failed if two zones use the same host (veso…
Browse files Browse the repository at this point in the history
…ft-inc#499)

Co-authored-by: yaphet <[email protected]>
  • Loading branch information
nebula-bots and darionyaphet authored Jan 13, 2022
1 parent 59cbc90 commit 96f763b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/meta/processors/zone/DivideZoneProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void DivideZoneProcessor::process(const cpp2::DivideZoneReq& req) {

std::vector<std::string> zoneNames;
std::unordered_set<HostAddr> totalHosts;
size_t totalHostsSize = 0;
auto batchHolder = std::make_unique<kvstore::BatchHolder>();
nebula::cpp2::ErrorCode code = nebula::cpp2::ErrorCode::SUCCEEDED;
for (auto iter = zoneItems.begin(); iter != zoneItems.end(); iter++) {
Expand Down Expand Up @@ -65,6 +66,7 @@ void DivideZoneProcessor::process(const cpp2::DivideZoneReq& req) {
break;
}

totalHostsSize += hosts.size();
std::copy(hosts.begin(), hosts.end(), std::inserter(totalHosts, totalHosts.end()));

auto key = MetaKeyUtils::zoneKey(std::move(zone));
Expand All @@ -85,6 +87,13 @@ void DivideZoneProcessor::process(const cpp2::DivideZoneReq& req) {
return;
}

if (totalHostsSize != totalHosts.size()) {
LOG(ERROR) << "The host in zone list have duplicate element";
handleErrorCode(nebula::cpp2::ErrorCode::E_INVALID_PARM);
onFinished();
return;
}

for (auto& host : totalHosts) {
auto iter = std::find(zoneHosts.begin(), zoneHosts.end(), host);
if (iter == zoneHosts.end()) {
Expand Down
20 changes: 20 additions & 0 deletions src/meta/test/MetaClientTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2479,6 +2479,26 @@ TEST(MetaClientTest, DivideZoneTest) {
auto result = client->divideZone("default_zone", std::move(zoneItems)).get();
EXPECT_FALSE(result.ok());
}
{
std::unordered_map<std::string, std::vector<HostAddr>> zoneItems;
std::vector<HostAddr> oneHosts = {
{"127.0.0.1", 8986}, {"127.0.0.1", 8987}, {"127.0.0.1", 8988}};
zoneItems.emplace("one_zone", std::move(oneHosts));
std::vector<HostAddr> anotherHosts = {{"127.0.0.1", 8988}, {"127.0.0.1", 8989}};
zoneItems.emplace("another_zone", std::move(anotherHosts));
auto result = client->divideZone("default_zone", std::move(zoneItems)).get();
EXPECT_FALSE(result.ok());
}
{
std::unordered_map<std::string, std::vector<HostAddr>> zoneItems;
std::vector<HostAddr> oneHosts = {
{"127.0.0.1", 8986}, {"127.0.0.1", 8987}, {"127.0.0.1", 8987}};
zoneItems.emplace("one_zone", std::move(oneHosts));
std::vector<HostAddr> anotherHosts = {{"127.0.0.1", 8988}, {"127.0.0.1", 8989}};
zoneItems.emplace("another_zone", std::move(anotherHosts));
auto result = client->divideZone("default_zone", std::move(zoneItems)).get();
EXPECT_FALSE(result.ok());
}
{
std::unordered_map<std::string, std::vector<HostAddr>> zoneItems;
std::vector<HostAddr> hosts0 = {};
Expand Down
32 changes: 32 additions & 0 deletions src/meta/test/ProcessorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4102,6 +4102,38 @@ TEST(ProcessorTest, DivideZoneTest) {
auto resp = std::move(f).get();
ASSERT_EQ(nebula::cpp2::ErrorCode::E_INVALID_PARM, resp.get_code());
}
{
cpp2::DivideZoneReq req;
req.zone_name_ref() = "default_zone";
std::unordered_map<std::string, std::vector<HostAddr>> zoneItems;
std::vector<HostAddr> oneHosts = {
{"127.0.0.1", 8986}, {"127.0.0.1", 8987}, {"127.0.0.1", 8988}};
zoneItems.emplace("one_zone", std::move(oneHosts));
std::vector<HostAddr> anotherHosts = {{"127.0.0.1", 8988}, {"127.0.0.1", 8989}};
zoneItems.emplace("another_zone", std::move(anotherHosts));
req.zone_items_ref() = std::move(zoneItems);
auto* processor = DivideZoneProcessor::instance(kv.get());
auto f = processor->getFuture();
processor->process(req);
auto resp = std::move(f).get();
ASSERT_EQ(nebula::cpp2::ErrorCode::E_INVALID_PARM, resp.get_code());
}
{
cpp2::DivideZoneReq req;
req.zone_name_ref() = "default_zone";
std::unordered_map<std::string, std::vector<HostAddr>> zoneItems;
std::vector<HostAddr> oneHosts = {
{"127.0.0.1", 8986}, {"127.0.0.1", 8987}, {"127.0.0.1", 8987}};
zoneItems.emplace("one_zone", std::move(oneHosts));
std::vector<HostAddr> anotherHosts = {{"127.0.0.1", 8988}, {"127.0.0.1", 8989}};
zoneItems.emplace("another_zone", std::move(anotherHosts));
req.zone_items_ref() = std::move(zoneItems);
auto* processor = DivideZoneProcessor::instance(kv.get());
auto f = processor->getFuture();
processor->process(req);
auto resp = std::move(f).get();
ASSERT_EQ(nebula::cpp2::ErrorCode::E_INVALID_PARM, resp.get_code());
}
{
cpp2::DivideZoneReq req;
req.zone_name_ref() = "default_zone";
Expand Down

0 comments on commit 96f763b

Please sign in to comment.