Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix divide zone should be failed if two zones use the same host #3699

Merged
merged 2 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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