Skip to content

Commit

Permalink
fix test case
Browse files Browse the repository at this point in the history
  • Loading branch information
darionyaphet committed Dec 31, 2021
1 parent 7b4b693 commit 4259548
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 38 deletions.
9 changes: 9 additions & 0 deletions src/meta/processors/BaseProcessor-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,15 @@ nebula::cpp2::ErrorCode BaseProcessor<RESP>::machineExist(const std::string& mac
return nebula::error(ret);
}

template <typename RESP>
nebula::cpp2::ErrorCode BaseProcessor<RESP>::hostExist(const std::string& hostKey) {
auto ret = doGet(hostKey);
if (nebula::ok(ret)) {
return nebula::cpp2::ErrorCode::SUCCEEDED;
}
return nebula::error(ret);
}

template <typename RESP>
nebula::cpp2::ErrorCode BaseProcessor<RESP>::includeByZone(const std::vector<HostAddr>& hosts) {
const auto& prefix = MetaKeyUtils::zonePrefix();
Expand Down
2 changes: 2 additions & 0 deletions src/meta/processors/BaseProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ class BaseProcessor {
* */
nebula::cpp2::ErrorCode machineExist(const std::string& machineKey);

nebula::cpp2::ErrorCode hostExist(const std::string& hostKey);

/**
* Check hosts has been include by zone or not.
* */
Expand Down
33 changes: 16 additions & 17 deletions src/meta/processors/parts/CreateSpaceProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,27 @@ void CreateSpaceProcessor::process(const cpp2::CreateSpaceReq& req) {
zoneIter->next();
}

int32_t zoneNum = zones.size();
if (replicaFactor > zoneNum) {
LOG(ERROR) << "Replication number should less than or equal to zone number.";
handleErrorCode(nebula::cpp2::ErrorCode::E_INVALID_PARM);
onFinished();
return;
}

properties.zone_names_ref() = zones;
} else {
zones = properties.get_zone_names();
}

auto it = std::unique(zones.begin(), zones.end());
if (it != zones.end()) {
LOG(ERROR) << "Zones have duplicated.";
handleErrorCode(nebula::cpp2::ErrorCode::E_INVALID_PARM);
onFinished();
return;
}

int32_t zoneNum = zones.size();
if (replicaFactor > zoneNum) {
LOG(ERROR) << "Replication number should less than or equal to zone number.";
handleErrorCode(nebula::cpp2::ErrorCode::E_INVALID_PARM);
onFinished();
return;
}

data.emplace_back(MetaKeyUtils::indexSpaceKey(spaceName),
std::string(reinterpret_cast<const char*>(&spaceId), sizeof(spaceId)));
data.emplace_back(MetaKeyUtils::spaceKey(spaceId), MetaKeyUtils::spaceVal(properties));
Expand All @@ -166,15 +174,6 @@ void CreateSpaceProcessor::process(const cpp2::CreateSpaceReq& req) {
return;
}

int32_t zoneNum = zones.size();
if (replicaFactor > zoneNum) {
LOG(ERROR) << "Replication number should less than or equal to zone number.";
LOG(ERROR) << "Replication number: " << replicaFactor << ", Zones size: " << zones.size();
handleErrorCode(nebula::cpp2::ErrorCode::E_INVALID_PARM);
onFinished();
return;
}

std::unordered_map<std::string, Hosts> zoneHosts;
for (auto& zone : zones) {
auto zoneKey = MetaKeyUtils::zoneKey(zone);
Expand Down
24 changes: 24 additions & 0 deletions src/meta/processors/parts/ListHostsProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ nebula::cpp2::ErrorCode ListHostsProcessor::allHostsWithStatus(cpp2::HostRole ro

auto now = time::WallClock::fastNowInMilliSec();
std::vector<std::string> removeHostsKey;
std::vector<HostAddr> heartbeatHosts;
for (auto iter = nebula::value(ret).get(); iter->valid(); iter->next()) {
HostInfo info = HostInfo::decode(iter->val());
if (info.role_ != role) {
Expand All @@ -120,6 +121,7 @@ nebula::cpp2::ErrorCode ListHostsProcessor::allHostsWithStatus(cpp2::HostRole ro

cpp2::HostItem item;
auto host = MetaKeyUtils::parseHostKey(iter->key());
heartbeatHosts.emplace_back(host);
item.hostAddr_ref() = std::move(host);

item.role_ref() = info.role_;
Expand Down Expand Up @@ -147,6 +149,28 @@ nebula::cpp2::ErrorCode ListHostsProcessor::allHostsWithStatus(cpp2::HostRole ro
}
}

if (role == cpp2::HostRole::STORAGE) {
const auto& machinePrefix = MetaKeyUtils::machinePrefix();
auto machineRet = doPrefix(machinePrefix);
if (!nebula::ok(machineRet)) {
auto retCode = nebula::error(machineRet);
LOG(ERROR) << "List Machines Failed, error: " << apache::thrift::util::enumNameSafe(retCode);
return retCode;
}

for (auto iter = nebula::value(machineRet).get(); iter->valid(); iter->next()) {
auto host = MetaKeyUtils::parseMachineKey(iter->key());
auto it = std::find(heartbeatHosts.begin(), heartbeatHosts.end(), host);
if (it == heartbeatHosts.end()) {
cpp2::HostItem item;
item.hostAddr_ref() = std::move(host);
item.role_ref() = cpp2::HostRole::STORAGE;
item.status_ref() = cpp2::HostStatus::OFFLINE;
hostItems_.emplace_back(std::move(item));
}
}
}

removeExpiredHosts(std::move(removeHostsKey));
return nebula::cpp2::ErrorCode::SUCCEEDED;
}
Expand Down
7 changes: 0 additions & 7 deletions src/meta/processors/zone/AddHostsIntoZoneProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

#include "meta/processors/zone/AddHostsIntoZoneProcessor.h"

DECLARE_int32(heartbeat_interval_secs);

namespace nebula {
namespace meta {

Expand Down Expand Up @@ -98,11 +96,6 @@ void AddHostsIntoZoneProcessor::process(const cpp2::AddHostsIntoZoneReq& req) {
zoneHosts.insert(zoneHosts.end(), hosts.begin(), hosts.end());
data.emplace_back(std::move(zoneKey), MetaKeyUtils::zoneVal(std::move(zoneHosts)));

HostInfo info(0, cpp2::HostRole::STORAGE, "");
for (auto& host : hosts) {
data.emplace_back(MetaKeyUtils::hostKey(host.host, host.port), HostInfo::encodeV2(info));
}

LOG(INFO) << "Add Hosts Into Zone " << zoneName;
doSyncPutAndUpdate(std::move(data));
}
Expand Down
7 changes: 3 additions & 4 deletions src/meta/processors/zone/AddHostsProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

#include "version/Version.h"

DECLARE_uint32(expired_time_factor);
DECLARE_int32(removed_threshold_sec);

namespace nebula {
namespace meta {

Expand Down Expand Up @@ -62,10 +65,6 @@ void AddHostsProcessor::process(const cpp2::AddHostsReq& req) {
return;
}

HostInfo info(0, cpp2::HostRole::STORAGE, "");
for (auto& host : hosts) {
data.emplace_back(MetaKeyUtils::hostKey(host.host, host.port), HostInfo::encodeV2(info));
}
doPut(std::move(data));
}

Expand Down
11 changes: 10 additions & 1 deletion src/meta/processors/zone/DropHostsProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,20 @@ void DropHostsProcessor::process(const cpp2::DropHostsReq& req) {
auto machineKey = MetaKeyUtils::machineKey(host.host, host.port);
auto ret = machineExist(machineKey);
if (ret != nebula::cpp2::ErrorCode::SUCCEEDED) {
LOG(ERROR) << "The host " << host << " not existed!";
LOG(ERROR) << "The machine " << host << " not existed!";
code = nebula::cpp2::ErrorCode::E_NO_HOSTS;
break;
}
holder->remove(std::move(machineKey));

auto hostKey = MetaKeyUtils::hostKey(host.host, host.port);
ret = hostExist(hostKey);
if (ret != nebula::cpp2::ErrorCode::SUCCEEDED) {
LOG(ERROR) << "The host " << host << " not existed!";
code = nebula::cpp2::ErrorCode::E_NO_HOSTS;
break;
}
holder->remove(std::move(hostKey));
}

if (code != nebula::cpp2::ErrorCode::SUCCEEDED) {
Expand Down
3 changes: 1 addition & 2 deletions src/meta/processors/zone/RenameZoneProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ void RenameZoneProcessor::process(const cpp2::RenameZoneReq& req) {
return;
}

// std::vector<kvstore::KV> data;
auto batchHolder = std::make_unique<kvstore::BatchHolder>();
auto iter = nebula::value(ret).get();
while (iter->valid()) {
Expand All @@ -62,7 +61,7 @@ void RenameZoneProcessor::process(const cpp2::RenameZoneReq& req) {
iter->next();
}

batchHolder->remove(MetaKeyUtils::zoneKey(originalZoneKey));
batchHolder->remove(MetaKeyUtils::zoneKey(originalZoneName));
batchHolder->put(std::move(zoneKey), std::move(originalZoneValue));
auto batch = encodeBatchValue(std::move(batchHolder)->getBatch());
doBatchOperation(std::move(batch));
Expand Down
4 changes: 4 additions & 0 deletions src/meta/test/AuthProcessorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ TEST(AuthProcessorTest, GrantRevokeTest) {
auto resp = std::move(f).get();
ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, resp.get_code());
}
{
std::vector<HostAddr> hosts = {{"0", 0}, {"1", 1}, {"2", 2}, {"3", 3}};
TestUtils::registerHB(kv.get(), hosts);
}
GraphSpaceID space1, space2;
// create space1
{
Expand Down
4 changes: 4 additions & 0 deletions src/meta/test/IndexProcessorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2047,6 +2047,10 @@ TEST(ProcessorTest, IndexIdInSpaceRangeTest) {
auto resp = std::move(f).get();
ASSERT_EQ(nebula::cpp2::ErrorCode::SUCCEEDED, resp.get_code());
}
{
std::vector<HostAddr> hosts = {{"0", 0}, {"1", 1}, {"2", 2}, {"3", 3}};
TestUtils::registerHB(kv.get(), hosts);
}
// mock one space and ten tag, ten edge
{
// space Id is 1
Expand Down
21 changes: 14 additions & 7 deletions src/meta/test/MetaClientTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ TEST(MetaClientTest, InterfacesTest) {
GraphSpaceID spaceId = 0;
{
std::vector<HostAddr> hosts = {{"0", 0}, {"1", 1}, {"2", 2}, {"3", 3}};
auto result = client->addHosts(std::move(hosts)).get();
auto result = client->addHosts(hosts).get();
TestUtils::registerHB(cluster.metaKV_.get(), hosts);
EXPECT_TRUE(result.ok());
}
{
Expand Down Expand Up @@ -356,7 +357,8 @@ TEST(MetaClientTest, TagTest) {
auto* client = cluster.metaClient_.get();
{
std::vector<HostAddr> hosts = {{"0", 0}, {"1", 1}, {"2", 2}, {"3", 3}};
auto result = client->addHosts(std::move(hosts)).get();
auto result = client->addHosts(hosts).get();
TestUtils::registerHB(cluster.metaKV_.get(), hosts);
EXPECT_TRUE(result.ok());
}
meta::cpp2::SpaceDesc spaceDesc;
Expand Down Expand Up @@ -592,7 +594,8 @@ TEST(MetaClientTest, EdgeTest) {
auto* client = cluster.metaClient_.get();
{
std::vector<HostAddr> hosts = {{"0", 0}, {"1", 1}, {"2", 2}, {"3", 3}};
auto result = client->addHosts(std::move(hosts)).get();
auto result = client->addHosts(hosts).get();
TestUtils::registerHB(cluster.metaKV_.get(), hosts);
EXPECT_TRUE(result.ok());
}
meta::cpp2::SpaceDesc spaceDesc;
Expand Down Expand Up @@ -702,7 +705,8 @@ TEST(MetaClientTest, TagIndexTest) {
auto* client = cluster.metaClient_.get();
{
std::vector<HostAddr> hosts = {{"0", 0}, {"1", 1}, {"2", 2}, {"3", 3}};
auto result = client->addHosts(std::move(hosts)).get();
auto result = client->addHosts(hosts).get();
TestUtils::registerHB(cluster.metaKV_.get(), hosts);
EXPECT_TRUE(result.ok());
}

Expand Down Expand Up @@ -882,7 +886,8 @@ TEST(MetaClientTest, EdgeIndexTest) {
auto* client = cluster.metaClient_.get();
{
std::vector<HostAddr> hosts = {{"0", 0}, {"1", 1}, {"2", 2}, {"3", 3}};
auto result = client->addHosts(std::move(hosts)).get();
auto result = client->addHosts(hosts).get();
TestUtils::registerHB(cluster.metaKV_.get(), hosts);
EXPECT_TRUE(result.ok());
}

Expand Down Expand Up @@ -1281,7 +1286,8 @@ TEST(MetaClientTest, ListenerDiffTest) {
auto client = std::make_unique<meta::MetaClient>(threadPool, metaAddrs, options);
{
std::vector<HostAddr> hosts = {{"0", 0}};
auto result = client->addHosts(std::move(hosts)).get();
auto result = client->addHosts(hosts).get();
TestUtils::registerHB(cluster.metaKV_.get(), hosts);
EXPECT_TRUE(result.ok());
}
client->waitForMetadReady();
Expand Down Expand Up @@ -1700,7 +1706,8 @@ TEST(MetaClientTest, ListenerTest) {
auto client = std::make_shared<MetaClient>(threadPool, localhosts);
{
std::vector<HostAddr> hosts = {{"0", 0}, {"1", 1}, {"2", 2}, {"3", 3}};
auto result = client->addHosts(std::move(hosts)).get();
auto result = client->addHosts(hosts).get();
TestUtils::registerHB(cluster.metaKV_.get(), hosts);
EXPECT_TRUE(result.ok());
}
client->waitForMetadReady();
Expand Down
Loading

0 comments on commit 4259548

Please sign in to comment.